4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * lustre/osp/osp_object.c
31 * Lustre OST Proxy Device (OSP) is the agent on the local MDT for the OST
34 * OSP object attributes cache
35 * ---------------------------
36 * OSP object is the stub of the remote OST-object or MDT-object. Both the
37 * attribute and the extended attributes are stored on the peer side remotely.
38 * It is inefficient to send RPC to peer to fetch those attributes when every
39 * get_attr()/get_xattr() called. For a large system, the LFSCK synchronous
40 * mode scanning is prohibitively inefficient.
42 * So the OSP maintains the OSP object attributes cache to cache some
43 * attributes on the local MDT. The cache is organized against the OSP
46 * struct osp_xattr_entry {
47 * struct list_head oxe_list;
53 * unsigned int oxe_exist:1,
60 * struct lu_attr opo_attr;
61 * struct list_head opo_xattr_list;
62 * spinlock_t opo_lock;
66 * The basic attributes, such as owner/mode/flags, are stored in the
67 * osp_object::opo_attr. The extended attributes will be stored
68 * as osp_xattr_entry. Every extended attribute has an independent
69 * osp_xattr_entry, and all the osp_xattr_entry are linked into the
70 * osp_object::opo_xattr_list. The OSP object attributes cache
71 * is protected by the osp_object::opo_lock.
73 * Not all OSP objects have an attributes cache because maintaining
74 * the cache requires some resources. Currently, the OSP object
75 * attributes cache will be initialized when the attributes or the
76 * extended attributes are pre-fetched via osp_declare_attr_get()
77 * or osp_declare_xattr_get(). That is usually for LFSCK purpose,
78 * but it also can be shared by others.
81 * XXX: NOT prepare out RPC for remote transaction. ((please refer to the
82 * comment of osp_trans_create() for remote transaction)
84 * According to our current transaction/dt_object_lock framework (to make
85 * the cross-MDTs modification for DNE1 to be workable), the transaction
86 * sponsor will start the transaction firstly, then try to acquire related
87 * dt_object_lock if needed. Under such rules, if we want to prepare the
88 * OUT RPC in the transaction declare phase, then related attr/xattr
89 * should be known without dt_object_lock. But such condition maybe not
90 * true for some remote transaction case. For example:
92 * For linkEA repairing (by LFSCK) case, before the LFSCK thread obtained
93 * the dt_object_lock on the target MDT-object, it cannot know whether
94 * the MDT-object has linkEA or not, neither invalid or not.
96 * Since the LFSCK thread cannot hold dt_object_lock before the remote
97 * transaction start (otherwise there will be some potential deadlock),
98 * it cannot prepare related OUT RPC for repairing during the declare
99 * phase as other normal transactions do.
101 * To resolve the trouble, we will make OSP to prepare related OUT RPC
102 * after remote transaction started, and trigger the remote updating
103 * (send RPC) when trans_stop. Then the up layer users, such as LFSCK,
104 * can follow the general rule to handle trans_start/dt_object_lock
105 * for repairing linkEA inconsistency without distinguishing remote
108 * In fact, above solution for remote transaction should be the normal
109 * model without considering DNE1. The trouble brought by DNE1 will be
110 * resolved in DNE2. At that time, this patch can be removed.
113 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
114 * Author: Mikhail Pershin <mike.tappro@intel.com>
117 #define DEBUG_SUBSYSTEM S_MDS
119 #include <lustre_obdo.h>
120 #include <lustre_swab.h>
122 #include "osp_internal.h"
124 static inline __u32 osp_dev2node(struct osp_device *osp)
126 return osp->opd_storage->dd_lu_dev.ld_site->ld_seq_site->ss_node_id;
129 static inline const char *osp_dto2name(struct osp_object *obj)
131 return obj->opo_obj.do_lu.lo_dev->ld_obd->obd_name;
134 static inline bool is_ost_obj(struct lu_object *lo)
136 return !lu2osp_dev(lo->lo_dev)->opd_connect_mdt;
139 static inline void __osp_oac_xattr_assignment(struct osp_object *obj,
140 struct osp_xattr_entry *oxe,
141 const struct lu_buf *buf)
144 memcpy(oxe->oxe_value, buf->lb_buf, buf->lb_len);
146 oxe->oxe_vallen = buf->lb_len;
152 * Assign FID to the OST object.
154 * This function will assign the FID to the OST object of a striped file.
156 * \param[in] env pointer to the thread context
157 * \param[in] d pointer to the OSP device
158 * \param[in] o pointer to the OSP object that the FID will be
161 static void osp_object_assign_fid(const struct lu_env *env,
162 struct osp_device *d, struct osp_object *o)
164 struct osp_thread_info *osi = osp_env_info(env);
166 LASSERT(fid_is_zero(lu_object_fid(&o->opo_obj.do_lu)));
167 LASSERT(o->opo_reserved);
170 osp_precreate_get_fid(env, d, &osi->osi_fid);
172 lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
175 #define OXE_DEFAULT_LEN 16
178 * Release reference from the OSP object extended attribute entry.
180 * If it is the last reference, then free the entry.
182 * \param[in] oxe pointer to the OSP object extended attribute entry.
184 static inline void osp_oac_xattr_put(struct osp_xattr_entry *oxe)
186 if (atomic_dec_and_test(&oxe->oxe_ref)) {
187 LASSERT(list_empty(&oxe->oxe_list));
189 OBD_FREE(oxe, oxe->oxe_buflen);
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] obj pointer to the OSP object
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 *obj, const char *name,
211 struct osp_xattr_entry *oxe;
213 list_for_each_entry(oxe, &obj->opo_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 oxe = osp_oac_xattr_find_locked(obj, name, strlen(name));
245 list_del_init(&oxe->oxe_list);
247 atomic_inc(&oxe->oxe_ref);
249 spin_unlock(&obj->opo_lock);
255 * Find the named extended attribute in the OSP object attributes cache.
257 * If it is not in the cache, then add an empty entry (that will be
258 * filled later) to cache with the given name.
260 * \param[in] obj pointer to the OSP object
261 * \param[in] name the name of the extended attribute
262 * \param[in] len the length of the extended attribute value
264 * \retval pointer to the found or new-created extended
266 * \retval NULL if the specified extended attribute is not in the
267 * cache or fail to add new empty entry to the cache.
269 static struct osp_xattr_entry *
270 osp_oac_xattr_find_or_add(struct osp_object *obj, const char *name, size_t len)
272 struct osp_xattr_entry *oxe;
273 struct osp_xattr_entry *tmp = NULL;
274 size_t namelen = strlen(name);
275 size_t size = sizeof(*oxe) + namelen + 1 +
276 (len ? len : OXE_DEFAULT_LEN);
278 oxe = osp_oac_xattr_find(obj, name, false);
282 OBD_ALLOC(oxe, size);
283 if (unlikely(oxe == NULL))
286 INIT_LIST_HEAD(&oxe->oxe_list);
287 oxe->oxe_buflen = size;
288 oxe->oxe_namelen = namelen;
289 memcpy(oxe->oxe_buf, name, namelen);
290 oxe->oxe_value = oxe->oxe_buf + namelen + 1;
291 /* One ref is for the caller, the other is for the entry on the list. */
292 atomic_set(&oxe->oxe_ref, 2);
294 spin_lock(&obj->opo_lock);
295 tmp = osp_oac_xattr_find_locked(obj, name, namelen);
297 list_add_tail(&oxe->oxe_list, &obj->opo_xattr_list);
299 atomic_inc(&tmp->oxe_ref);
300 spin_unlock(&obj->opo_lock);
311 * Assign the cached OST-object's EA with the given value.
313 * If the current EA entry in cache has not enough space to hold the new
314 * value, remove it, create a new one, then assign with the given value.
316 * \param[in] obj pointer to the OSP object
317 * \param[in] oxe pointer to the cached EA entry to be assigned
318 * \param[in] buf pointer to the buffer with new EA value
320 * \retval pointer to the new created EA entry in cache if
321 * current entry is not big enough; otherwise, the
322 * input 'oxe' will be returned.
324 static struct osp_xattr_entry *
325 osp_oac_xattr_assignment(struct osp_object *obj, struct osp_xattr_entry *oxe,
326 const struct lu_buf *buf)
328 struct osp_xattr_entry *new = NULL;
329 struct osp_xattr_entry *old = NULL;
330 int namelen = oxe->oxe_namelen;
331 bool unlink_only = false;
333 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < buf->lb_len) {
334 size_t size = sizeof(*oxe) + namelen + 1 + buf->lb_len;
336 OBD_ALLOC(new, size);
338 INIT_LIST_HEAD(&new->oxe_list);
339 new->oxe_buflen = size;
340 new->oxe_namelen = namelen;
341 memcpy(new->oxe_buf, oxe->oxe_buf, namelen);
342 new->oxe_value = new->oxe_buf + namelen + 1;
343 /* One ref is for the caller,
344 * the other is for the entry on the list. */
345 atomic_set(&new->oxe_ref, 2);
346 __osp_oac_xattr_assignment(obj, new, buf);
349 CWARN("%s: cannot update cached xattr %.*s of "DFID"\n",
350 osp_dto2name(obj), namelen, oxe->oxe_buf,
351 PFID(lu_object_fid(&obj->opo_obj.do_lu)));
355 spin_lock(&obj->opo_lock);
356 old = osp_oac_xattr_find_locked(obj, oxe->oxe_buf, namelen);
359 /* Unlink the 'old'. */
360 list_del_init(&old->oxe_list);
362 /* Drop the ref for 'old' on list. */
363 osp_oac_xattr_put(old);
365 /* Insert 'new' into list. */
366 list_add_tail(&new->oxe_list, &obj->opo_xattr_list);
368 /* Drop the ref for current using. */
369 osp_oac_xattr_put(oxe);
371 } else if (unlink_only) {
372 /* Unlink the 'old'. */
373 list_del_init(&old->oxe_list);
375 /* Drop the ref for 'old' on list. */
376 osp_oac_xattr_put(old);
378 __osp_oac_xattr_assignment(obj, oxe, buf);
381 /* Someone unlinked the 'old' by race, need NOT to assign
382 * for unlinked 'oxe', just add the 'new' one. */
383 list_add_tail(&new->oxe_list, &obj->opo_xattr_list);
385 /* Drop the ref for current using. */
386 osp_oac_xattr_put(oxe);
389 spin_unlock(&obj->opo_lock);
395 * Parse the OSP object attribute from the RPC reply.
397 * If the attribute is valid, then it will be added to the OSP object
400 * \param[in] env pointer to the thread context
401 * \param[in] reply pointer to the RPC reply
402 * \param[in] req pointer to the RPC request
403 * \param[out] attr pointer to buffer to hold the output attribute
404 * \param[in] obj pointer to the OSP object
405 * \param[in] index the index of the attribute buffer in the reply
407 * \retval 0 for success
408 * \retval negative error number on failure
410 static int osp_get_attr_from_reply(const struct lu_env *env,
411 struct object_update_reply *reply,
412 struct ptlrpc_request *req,
413 struct lu_attr *attr,
414 struct osp_object *obj, int index)
416 struct osp_thread_info *osi = osp_env_info(env);
417 struct lu_buf *rbuf = &osi->osi_lb2;
418 struct obdo *lobdo = &osi->osi_obdo;
422 rc = object_update_result_data_get(reply, rbuf, index);
426 wobdo = rbuf->lb_buf;
427 if (rbuf->lb_len != sizeof(*wobdo))
430 LASSERT(req != NULL);
431 if (ptlrpc_req_need_swab(req))
432 lustre_swab_obdo(wobdo);
434 lustre_get_wire_obdo(NULL, lobdo, wobdo);
435 spin_lock(&obj->opo_lock);
436 la_from_obdo(&obj->opo_attr, lobdo, lobdo->o_valid);
438 *attr = obj->opo_attr;
439 spin_unlock(&obj->opo_lock);
445 * Interpreter function for getting OSP object attribute asynchronously.
447 * Called to interpret the result of an async mode RPC for getting the
448 * OSP object attribute.
450 * \param[in] env pointer to the thread context
451 * \param[in] reply pointer to the RPC reply
452 * \param[in] req pointer to the RPC request
453 * \param[in] obj pointer to the OSP object
454 * \param[out] data pointer to buffer to hold the output attribute
455 * \param[in] index the index of the attribute buffer in the reply
456 * \param[in] rc the result for handling the RPC
458 * \retval 0 for success
459 * \retval negative error number on failure
461 static int osp_attr_get_interpterer(const struct lu_env *env,
462 struct object_update_reply *reply,
463 struct ptlrpc_request *req,
464 struct osp_object *obj,
465 void *data, int index, int rc)
467 struct lu_attr *attr = data;
470 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
471 obj->opo_non_exist = 0;
473 return osp_get_attr_from_reply(env, reply, req, NULL, obj,
477 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
478 obj->opo_non_exist = 1;
481 spin_lock(&obj->opo_lock);
483 spin_unlock(&obj->opo_lock);
490 * Implement OSP layer dt_object_operations::do_declare_attr_get() interface.
492 * Declare that the caller will get attribute from the specified OST object.
494 * This function adds an Object Unified Target (OUT) sub-request to the per-OSP
495 * based shared asynchronous request queue. The osp_attr_get_interpterer()
496 * is registered as the interpreter function to handle the result of this
499 * \param[in] env pointer to the thread context
500 * \param[in] dt pointer to the OSP layer dt_object
502 * \retval 0 for success
503 * \retval negative error number on failure
505 static int osp_declare_attr_get(const struct lu_env *env, struct dt_object *dt)
507 struct osp_object *obj = dt2osp_obj(dt);
508 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
511 mutex_lock(&osp->opd_async_requests_mutex);
512 rc = osp_insert_async_request(env, OUT_ATTR_GET, obj, 0, NULL, NULL,
513 &obj->opo_attr, sizeof(struct obdo),
514 osp_attr_get_interpterer);
515 mutex_unlock(&osp->opd_async_requests_mutex);
521 * Implement OSP layer dt_object_operations::do_attr_get() interface.
523 * Get attribute from the specified MDT/OST object.
525 * If the attribute is in the OSP object attributes cache, then return
526 * the cached attribute directly. Otherwise it will trigger an OUT RPC
527 * to the peer to get the attribute synchronously, if successful, add it
528 * to the OSP attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
530 * \param[in] env pointer to the thread context
531 * \param[in] dt pointer to the OSP layer dt_object
532 * \param[out] attr pointer to the buffer to hold the output attribute
534 * \retval 0 for success
535 * \retval negative error number on failure
537 int osp_attr_get(const struct lu_env *env, struct dt_object *dt,
538 struct lu_attr *attr)
540 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
541 struct osp_object *obj = dt2osp_obj(dt);
542 struct dt_device *dev = &osp->opd_dt_dev;
543 struct osp_update_request *update;
544 struct object_update_reply *reply;
545 struct ptlrpc_request *req = NULL;
549 if (is_ost_obj(&dt->do_lu) && obj->opo_non_exist)
552 spin_lock(&obj->opo_lock);
553 if (obj->opo_attr.la_valid != 0 && !obj->opo_stale) {
554 *attr = obj->opo_attr;
555 spin_unlock(&obj->opo_lock);
559 spin_unlock(&obj->opo_lock);
561 update = osp_update_request_create(dev);
563 RETURN(PTR_ERR(update));
565 rc = osp_update_rpc_pack(env, attr_get, update, OUT_ATTR_GET,
566 lu_object_fid(&dt->do_lu));
568 CERROR("%s: Insert update error "DFID": rc = %d\n",
569 dev->dd_lu_dev.ld_obd->obd_name,
570 PFID(lu_object_fid(&dt->do_lu)), rc);
575 rc = osp_remote_sync(env, osp, update, &req);
578 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
579 obj->opo_non_exist = 1;
581 CERROR("%s:osp_attr_get update error "DFID": rc = %d\n",
582 dev->dd_lu_dev.ld_obd->obd_name,
583 PFID(lu_object_fid(&dt->do_lu)), rc);
589 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
590 obj->opo_non_exist = 0;
591 reply = req_capsule_server_sized_get(&req->rq_pill,
592 &RMF_OUT_UPDATE_REPLY,
593 OUT_UPDATE_REPLY_SIZE);
594 if (reply == NULL || reply->ourp_magic != UPDATE_REPLY_MAGIC)
595 GOTO(out, rc = -EPROTO);
597 rc = osp_get_attr_from_reply(env, reply, req, attr, obj, 0);
601 spin_lock(&obj->opo_lock);
603 spin_unlock(&obj->opo_lock);
609 ptlrpc_req_finished(req);
611 osp_update_request_destroy(env, update);
617 * Implement OSP layer dt_object_operations::do_declare_attr_set() interface.
619 * If the transaction is not remote one, then declare the credits that will
620 * be used for the subsequent llog record for the object's attributes.
622 * \param[in] env pointer to the thread context
623 * \param[in] dt pointer to the OSP layer dt_object
624 * \param[in] attr pointer to the attribute to be set
625 * \param[in] th pointer to the transaction handler
627 * \retval 0 for success
628 * \retval negative error number on failure
630 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
631 const struct lu_attr *attr, struct thandle *th)
633 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
634 struct osp_object *o = dt2osp_obj(dt);
637 if (is_only_remote_trans(th))
638 return osp_md_declare_attr_set(env, dt, attr, th);
640 * Usually we don't allow server stack to manipulate size
641 * but there is a special case when striping is created
642 * late, after stripeless file got truncated to non-zero.
644 * In this case we do the following:
646 * 1) grab id in declare - this can lead to leaked OST objects
647 * but we don't currently have proper mechanism and the only
648 * options we have are to do truncate RPC holding transaction
649 * open (very bad) or to grab id in declare at cost of leaked
650 * OST object in same very rare unfortunate case (just bad)
651 * notice 1.6-2.0 do assignment outside of running transaction
652 * all the time, meaning many more chances for leaked objects.
654 * 2) send synchronous truncate RPC with just assigned id
657 /* there are few places in MDD code still passing NULL
658 * XXX: to be fixed soon */
662 if (attr->la_valid & LA_SIZE && attr->la_size > 0 &&
663 fid_is_zero(lu_object_fid(&o->opo_obj.do_lu))) {
664 LASSERT(!dt_object_exists(dt));
665 osp_object_assign_fid(env, d, o);
666 rc = osp_object_truncate(env, dt, attr->la_size);
671 if (!(attr->la_valid & (LA_UID | LA_GID)))
674 /* track all UID/GID changes via llog */
675 rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
681 * Implement OSP layer dt_object_operations::do_attr_set() interface.
683 * Set attribute to the specified OST object.
685 * If the transaction is a remote one, then add OUT_ATTR_SET sub-request
686 * in the OUT RPC that will be flushed when the remote transaction stop.
687 * Otherwise, it will generate a MDS_SETATTR64_REC record in the llog that
688 * will be handled by a dedicated thread asynchronously.
690 * If the attribute entry exists in the OSP object attributes cache,
691 * then update the cached attribute according to given attribute.
693 * \param[in] env pointer to the thread context
694 * \param[in] dt pointer to the OSP layer dt_object
695 * \param[in] attr pointer to the attribute to be set
696 * \param[in] th pointer to the transaction handler
698 * \retval 0 for success
699 * \retval negative error number on failure
701 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
702 const struct lu_attr *attr, struct thandle *th)
704 struct osp_object *o = dt2osp_obj(dt);
708 /* we're interested in uid/gid changes only */
709 if (!(attr->la_valid & (LA_UID | LA_GID)))
712 if (!is_only_remote_trans(th)) {
713 rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
714 /* XXX: send new uid/gid to OST ASAP? */
718 /* It is for OST-object attr_set directly without updating
719 * local MDT-object attribute. It is usually used by LFSCK. */
720 rc = osp_md_attr_set(env, dt, attr, th);
721 CDEBUG(D_INFO, "(1) set attr "DFID": rc = %d\n",
722 PFID(&dt->do_lu.lo_header->loh_fid), rc);
727 /* Update the OSP object attributes cache. */
729 spin_lock(&o->opo_lock);
730 if (attr->la_valid & LA_UID) {
731 la->la_uid = attr->la_uid;
732 la->la_valid |= LA_UID;
735 if (attr->la_valid & LA_GID) {
736 la->la_gid = attr->la_gid;
737 la->la_valid |= LA_GID;
739 spin_unlock(&o->opo_lock);
746 * Interpreter function for getting OSP object extended attribute asynchronously
748 * Called to interpret the result of an async mode RPC for getting the
749 * OSP object extended attribute.
751 * \param[in] env pointer to the thread context
752 * \param[in] reply pointer to the RPC reply
753 * \param[in] req pointer to the RPC request
754 * \param[in] obj pointer to the OSP object
755 * \param[out] data pointer to OSP object attributes cache
756 * \param[in] index the index of the attribute buffer in the reply
757 * \param[in] rc the result for handling the RPC
759 * \retval 0 for success
760 * \retval negative error number on failure
762 static int osp_xattr_get_interpterer(const struct lu_env *env,
763 struct object_update_reply *reply,
764 struct ptlrpc_request *req,
765 struct osp_object *obj,
766 void *data, int index, int rc)
768 struct osp_xattr_entry *oxe = data;
769 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
772 size_t len = sizeof(*oxe) + oxe->oxe_namelen + 1;
774 rc = object_update_result_data_get(reply, rbuf, index);
775 if (rc < 0 || rbuf->lb_len > (oxe->oxe_buflen - len)) {
776 spin_lock(&obj->opo_lock);
777 if (unlikely(rc == -ENODATA)) {
783 spin_unlock(&obj->opo_lock);
784 osp_oac_xattr_put(oxe);
786 return rc < 0 ? rc : -ERANGE;
789 spin_lock(&obj->opo_lock);
790 __osp_oac_xattr_assignment(obj, oxe, rbuf);
791 spin_unlock(&obj->opo_lock);
792 } else if (rc == -ENOENT || rc == -ENODATA) {
793 spin_lock(&obj->opo_lock);
796 spin_unlock(&obj->opo_lock);
798 spin_lock(&obj->opo_lock);
800 spin_unlock(&obj->opo_lock);
803 osp_oac_xattr_put(oxe);
809 * Implement OSP dt_object_operations::do_declare_xattr_get() interface.
811 * Declare that the caller will get extended attribute from the specified
814 * This function will add an OUT_XATTR_GET sub-request to the per OSP
815 * based shared asynchronous request queue with the interpreter function:
816 * osp_xattr_get_interpterer().
818 * \param[in] env pointer to the thread context
819 * \param[in] dt pointer to the OSP layer dt_object
820 * \param[out] buf pointer to the lu_buf to hold the extended attribute
821 * \param[in] name the name for the expected extended attribute
823 * \retval 0 for success
824 * \retval negative error number on failure
826 static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
827 struct lu_buf *buf, const char *name)
829 struct osp_object *obj = dt2osp_obj(dt);
830 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
831 struct osp_xattr_entry *oxe;
835 LASSERT(buf != NULL);
836 LASSERT(name != NULL);
838 if (unlikely(buf->lb_len == 0))
841 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
845 namelen = strlen(name);
846 mutex_lock(&osp->opd_async_requests_mutex);
847 rc = osp_insert_async_request(env, OUT_XATTR_GET, obj, 1,
848 &namelen, (const void **)&name,
850 osp_xattr_get_interpterer);
852 mutex_unlock(&osp->opd_async_requests_mutex);
853 osp_oac_xattr_put(oxe);
855 struct osp_update_request *our;
856 struct osp_update_request_sub *ours;
858 /* XXX: Currently, we trigger the batched async OUT
859 * RPC via dt_declare_xattr_get(). It is not
860 * perfect solution, but works well now.
862 * We will improve it in the future. */
863 our = osp->opd_async_requests;
864 ours = osp_current_object_update_request(our);
865 if (ours != NULL && ours->ours_req != NULL &&
866 ours->ours_req->ourq_count > 0) {
867 osp->opd_async_requests = NULL;
868 mutex_unlock(&osp->opd_async_requests_mutex);
869 rc = osp_unplug_async_request(env, osp, our);
871 mutex_unlock(&osp->opd_async_requests_mutex);
879 * Implement OSP layer dt_object_operations::do_xattr_get() interface.
881 * Get extended attribute from the specified MDT/OST object.
883 * If the extended attribute is in the OSP object attributes cache, then
884 * return the cached extended attribute directly. Otherwise it will get
885 * the extended attribute synchronously, if successful, add it to the OSP
886 * attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
888 * There is a race condition: some other thread has added the named extended
889 * attributed entry to the OSP object attributes cache during the current
890 * OUT_XATTR_GET handling. If such case happens, the OSP will replace the
891 * (just) existing extended attribute entry with the new replied one.
893 * \param[in] env pointer to the thread context
894 * \param[in] dt pointer to the OSP layer dt_object
895 * \param[out] buf pointer to the lu_buf to hold the extended attribute
896 * \param[in] name the name for the expected extended attribute
898 * \retval 0 for success
899 * \retval negative error number on failure
901 int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
902 struct lu_buf *buf, const char *name)
904 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
905 struct osp_object *obj = dt2osp_obj(dt);
906 struct dt_device *dev = &osp->opd_dt_dev;
907 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
908 struct osp_update_request *update = NULL;
909 struct ptlrpc_request *req = NULL;
910 struct object_update_reply *reply;
911 struct osp_xattr_entry *oxe = NULL;
912 const char *dname = osp_dto2name(obj);
916 LASSERT(buf != NULL);
917 LASSERT(name != NULL);
919 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NETWORK) &&
920 osp->opd_index == cfs_fail_val) {
921 if (is_ost_obj(&dt->do_lu)) {
922 if (osp_dev2node(osp) == cfs_fail_val)
925 if (strcmp(name, XATTR_NAME_LINK) == 0)
930 if (unlikely(obj->opo_non_exist))
933 oxe = osp_oac_xattr_find(obj, name, false);
935 spin_lock(&obj->opo_lock);
936 if (oxe->oxe_ready) {
938 GOTO(unlock, rc = -ENODATA);
940 if (buf->lb_buf == NULL)
941 GOTO(unlock, rc = oxe->oxe_vallen);
943 if (buf->lb_len < oxe->oxe_vallen)
944 GOTO(unlock, rc = -ERANGE);
946 memcpy(buf->lb_buf, oxe->oxe_value,
949 GOTO(unlock, rc = oxe->oxe_vallen);
952 spin_unlock(&obj->opo_lock);
953 osp_oac_xattr_put(oxe);
957 spin_unlock(&obj->opo_lock);
959 update = osp_update_request_create(dev);
961 GOTO(out, rc = PTR_ERR(update));
963 rc = osp_update_rpc_pack(env, xattr_get, update, OUT_XATTR_GET,
964 lu_object_fid(&dt->do_lu), name, buf->lb_len);
966 CERROR("%s: Insert update error "DFID": rc = %d\n",
967 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
971 rc = osp_remote_sync(env, osp, update, &req);
974 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
975 obj->opo_non_exist = 1;
979 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
982 CWARN("%s: Fail to add xattr (%s) to cache for "
983 DFID" (1): rc = %d\n", dname, name,
984 PFID(lu_object_fid(&dt->do_lu)), rc);
989 spin_lock(&obj->opo_lock);
990 if (rc == -ENOENT || rc == -ENODATA) {
996 spin_unlock(&obj->opo_lock);
1001 reply = req_capsule_server_sized_get(&req->rq_pill,
1002 &RMF_OUT_UPDATE_REPLY,
1003 OUT_UPDATE_REPLY_SIZE);
1004 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1005 CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
1006 dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
1007 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
1009 GOTO(out, rc = -EPROTO);
1012 rc = object_update_result_data_get(reply, rbuf, 0);
1016 /* The peer should have converted '0' as '-ENODATA'. */
1018 GOTO(out, rc = -EINVAL);
1020 /* For detecting EA size. */
1025 oxe = osp_oac_xattr_find_or_add(obj, name, rbuf->lb_len);
1027 CWARN("%s: Fail to add xattr (%s) to "
1028 "cache for "DFID" (2): rc = %d\n",
1029 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1035 oxe = osp_oac_xattr_assignment(obj, oxe, rbuf);
1040 if (rc > 0 && buf->lb_buf) {
1041 if (unlikely(buf->lb_len < rbuf->lb_len))
1044 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
1048 ptlrpc_req_finished(req);
1050 if (update != NULL && !IS_ERR(update))
1051 osp_update_request_destroy(env, update);
1054 osp_oac_xattr_put(oxe);
1060 * Implement OSP layer dt_object_operations::do_declare_xattr_set() interface.
1062 * Declare that the caller will set extended attribute to the specified
1065 * If it is non-remote transaction, it will add an OUT_XATTR_SET sub-request
1066 * to the OUT RPC that will be flushed when the transaction start. And if the
1067 * OSP attributes cache is initialized, then check whether the name extended
1068 * attribute entry exists in the cache or not. If yes, replace it; otherwise,
1069 * add the extended attribute to the cache.
1071 * \param[in] env pointer to the thread context
1072 * \param[in] dt pointer to the OSP layer dt_object
1073 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1074 * \param[in] name the name of the extended attribute to be set
1075 * \param[in] flag to indicate the detailed set operation: LU_XATTR_CREATE
1076 * or LU_XATTR_REPLACE or others
1077 * \param[in] th pointer to the transaction handler
1079 * \retval 0 for success
1080 * \retval negative error number on failure
1082 int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
1083 const struct lu_buf *buf, const char *name,
1084 int flag, struct thandle *th)
1086 return osp_trans_update_request_create(th);
1090 * Implement OSP layer dt_object_operations::do_xattr_set() interface.
1092 * Set extended attribute to the specified MDT/OST object.
1094 * Add an OUT_XATTR_SET sub-request into the OUT RPC that will be flushed in
1095 * the transaction stop. And if the OSP attributes cache is initialized, then
1096 * check whether the name extended attribute entry exists in the cache or not.
1097 * If yes, replace it; otherwise, add the extended attribute to the cache.
1099 * \param[in] env pointer to the thread context
1100 * \param[in] dt pointer to the OSP layer dt_object
1101 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1102 * \param[in] name the name of the extended attribute to be set
1103 * \param[in] fl to indicate the detailed set operation: LU_XATTR_CREATE
1104 * or LU_XATTR_REPLACE or others
1105 * \param[in] th pointer to the transaction handler
1107 * \retval 0 for success
1108 * \retval negative error number on failure
1110 int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
1111 const struct lu_buf *buf, const char *name, int fl,
1114 struct osp_object *o = dt2osp_obj(dt);
1115 struct osp_update_request *update;
1116 struct osp_xattr_entry *oxe;
1120 update = thandle_to_osp_update_request(th);
1121 LASSERT(update != NULL);
1123 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zd\n",
1124 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
1126 rc = osp_update_rpc_pack(env, xattr_set, update, OUT_XATTR_SET,
1127 lu_object_fid(&dt->do_lu), buf, name, fl);
1131 /* Do not cache linkEA that may be self-adjusted by peers
1132 * under EA overflow case. */
1133 if (strcmp(name, XATTR_NAME_LINK) == 0) {
1134 oxe = osp_oac_xattr_find(o, name, true);
1136 osp_oac_xattr_put(oxe);
1141 oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
1143 CWARN("%s: cannot cache xattr '%s' of "DFID"\n",
1144 osp_dto2name(o), name, PFID(lu_object_fid(&dt->do_lu)));
1149 oxe = osp_oac_xattr_assignment(o, oxe, buf);
1151 osp_oac_xattr_put(oxe);
1157 * Implement OSP layer dt_object_operations::do_declare_xattr_del() interface.
1159 * Declare that the caller will delete extended attribute on the specified
1162 * If it is non-remote transaction, it will add an OUT_XATTR_DEL sub-request
1163 * to the OUT RPC that will be flushed when the transaction start. And if the
1164 * name extended attribute entry exists in the OSP attributes cache, then remove
1165 * it from the cache.
1167 * \param[in] env pointer to the thread context
1168 * \param[in] dt pointer to the OSP layer dt_object
1169 * \param[in] name the name of the extended attribute to be set
1170 * \param[in] th pointer to the transaction handler
1172 * \retval 0 for success
1173 * \retval negative error number on failure
1175 int osp_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
1176 const char *name, struct thandle *th)
1178 return osp_trans_update_request_create(th);
1182 * Implement OSP layer dt_object_operations::do_xattr_del() interface.
1184 * Delete extended attribute on the specified MDT/OST object.
1186 * If it is remote transaction, it will add an OUT_XATTR_DEL sub-request into
1187 * the OUT RPC that will be flushed when the transaction stop. And if the name
1188 * extended attribute entry exists in the OSP attributes cache, then remove it
1191 * \param[in] env pointer to the thread context
1192 * \param[in] dt pointer to the OSP layer dt_object
1193 * \param[in] name the name of the extended attribute to be set
1194 * \param[in] th pointer to the transaction handler
1196 * \retval 0 for success
1197 * \retval negative error number on failure
1199 int osp_xattr_del(const struct lu_env *env, struct dt_object *dt,
1200 const char *name, struct thandle *th)
1202 struct osp_update_request *update;
1203 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1204 struct osp_object *o = dt2osp_obj(dt);
1205 struct osp_xattr_entry *oxe;
1208 update = thandle_to_osp_update_request(th);
1209 LASSERT(update != NULL);
1211 rc = osp_update_rpc_pack(env, xattr_del, update, OUT_XATTR_DEL,
1216 oxe = osp_oac_xattr_find(o, name, true);
1218 /* Drop the ref for entry on list. */
1219 osp_oac_xattr_put(oxe);
1224 void osp_obj_invalidate_cache(struct osp_object *obj)
1226 struct osp_xattr_entry *oxe;
1227 struct osp_xattr_entry *tmp;
1229 spin_lock(&obj->opo_lock);
1230 list_for_each_entry_safe(oxe, tmp, &obj->opo_xattr_list, oxe_list) {
1232 list_del_init(&oxe->oxe_list);
1233 osp_oac_xattr_put(oxe);
1235 obj->opo_attr.la_valid = 0;
1236 spin_unlock(&obj->opo_lock);
1240 * Implement OSP layer dt_object_operations::do_invalidate() interface.
1242 * Invalidate attributes cached on the specified MDT/OST object.
1244 * \param[in] env pointer to the thread context
1245 * \param[in] dt pointer to the OSP layer dt_object
1247 * \retval 0 for success
1248 * \retval negative error number on failure
1250 int osp_invalidate(const struct lu_env *env, struct dt_object *dt)
1252 struct osp_object *obj = dt2osp_obj(dt);
1255 CDEBUG(D_HA, "Invalidate osp_object "DFID"\n",
1256 PFID(lu_object_fid(&dt->do_lu)));
1257 osp_obj_invalidate_cache(obj);
1259 spin_lock(&obj->opo_lock);
1261 spin_unlock(&obj->opo_lock);
1267 * Implement OSP layer dt_object_operations::do_declare_create() interface.
1269 * Declare that the caller will create the OST object.
1271 * If the transaction is a remote transaction and the FID for the OST-object
1272 * has been assigned already, then handle it as creating (remote) MDT object
1273 * via osp_md_declare_object_create(). This function is usually used for LFSCK
1274 * to re-create the lost OST object. Otherwise, if it is not replay case, the
1275 * OSP will reserve pre-created object for the subsequent create operation;
1276 * if the MDT side cached pre-created objects are less than some threshold,
1277 * then it will wakeup the pre-create thread.
1279 * \param[in] env pointer to the thread context
1280 * \param[in] dt pointer to the OSP layer dt_object
1281 * \param[in] attr the attribute for the object to be created
1282 * \param[in] hint pointer to the hint for creating the object, such as
1284 * \param[in] dof pointer to the dt_object_format for help the creation
1285 * \param[in] th pointer to the transaction handler
1287 * \retval 0 for success
1288 * \retval negative error number on failure
1290 static int osp_declare_object_create(const struct lu_env *env,
1291 struct dt_object *dt,
1292 struct lu_attr *attr,
1293 struct dt_allocation_hint *hint,
1294 struct dt_object_format *dof,
1297 struct osp_thread_info *osi = osp_env_info(env);
1298 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1299 struct osp_object *o = dt2osp_obj(dt);
1300 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1301 struct thandle *local_th;
1306 if (is_only_remote_trans(th) && !fid_is_zero(fid)) {
1307 LASSERT(fid_is_sane(fid));
1309 rc = osp_md_declare_object_create(env, dt, attr, hint, dof, th);
1314 /* should happen to non-0 OSP only so that at least one object
1315 * has been already declared in the scenario and LOD should
1317 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
1320 LASSERT(d->opd_last_used_oid_file);
1323 * There can be gaps in precreated ids and record to unlink llog
1324 * XXX: we do not handle gaps yet, implemented before solution
1325 * was found to be racy, so we disabled that. there is no
1326 * point in making useless but expensive llog declaration.
1328 /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
1330 local_th = osp_get_storage_thandle(env, th, d);
1331 if (IS_ERR(local_th))
1332 RETURN(PTR_ERR(local_th));
1334 if (unlikely(!fid_is_zero(fid))) {
1335 /* replay case: caller knows fid */
1336 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1337 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1338 osi->osi_lb.lb_buf = NULL;
1340 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1341 &osi->osi_lb, osi->osi_off,
1347 * in declaration we need to reserve object so that we don't block
1348 * awaiting precreation RPC to complete
1350 rc = osp_precreate_reserve(env, d);
1352 * we also need to declare update to local "last used id" file for
1353 * recovery if object isn't used for a reason, we need to release
1354 * reservation, this can be made in osd_object_release()
1357 /* mark id is reserved: in create we don't want to talk
1359 LASSERT(o->opo_reserved == 0);
1360 o->opo_reserved = 1;
1362 /* common for all OSPs file hystorically */
1363 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1364 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1365 osi->osi_lb.lb_buf = NULL;
1366 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1367 &osi->osi_lb, osi->osi_off,
1370 /* not needed in the cache anymore */
1371 set_bit(LU_OBJECT_HEARD_BANSHEE,
1372 &dt->do_lu.lo_header->loh_flags);
1378 * Implement OSP layer dt_object_operations::do_create() interface.
1380 * Create the OST object.
1382 * If the transaction is a remote transaction and the FID for the OST-object
1383 * has been assigned already, then handle it as handling MDT object via the
1384 * osp_md_object_create(). For other cases, the OSP will assign FID to the
1385 * object to be created, and update last_used Object ID (OID) file.
1387 * \param[in] env pointer to the thread context
1388 * \param[in] dt pointer to the OSP layer dt_object
1389 * \param[in] attr the attribute for the object to be created
1390 * \param[in] hint pointer to the hint for creating the object, such as
1392 * \param[in] dof pointer to the dt_object_format for help the creation
1393 * \param[in] th pointer to the transaction handler
1395 * \retval 0 for success
1396 * \retval negative error number on failure
1398 static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
1399 struct lu_attr *attr,
1400 struct dt_allocation_hint *hint,
1401 struct dt_object_format *dof, struct thandle *th)
1403 struct osp_thread_info *osi = osp_env_info(env);
1404 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1405 struct osp_object *o = dt2osp_obj(dt);
1407 struct lu_fid *fid = &osi->osi_fid;
1408 struct thandle *local_th;
1411 if (is_only_remote_trans(th) &&
1412 !fid_is_zero(lu_object_fid(&dt->do_lu))) {
1413 LASSERT(fid_is_sane(lu_object_fid(&dt->do_lu)));
1415 rc = osp_md_object_create(env, dt, attr, hint, dof, th);
1417 o->opo_non_exist = 0;
1422 o->opo_non_exist = 0;
1423 if (o->opo_reserved) {
1424 /* regular case, fid is assigned holding transaction open */
1425 osp_object_assign_fid(env, d, o);
1428 memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
1430 LASSERTF(fid_is_sane(fid), "fid for osp_object %p is insane"DFID"!\n",
1433 if (!o->opo_reserved) {
1434 /* special case, id was assigned outside of transaction
1435 * see comments in osp_declare_attr_set */
1436 LASSERT(d->opd_pre != NULL);
1437 spin_lock(&d->opd_pre_lock);
1438 osp_update_last_fid(d, fid);
1439 spin_unlock(&d->opd_pre_lock);
1442 CDEBUG(D_INODE, "fid for osp_object %p is "DFID"\n", o, PFID(fid));
1444 /* If the precreate ends, it means it will be ready to rollover to
1445 * the new sequence soon, all the creation should be synchronized,
1446 * otherwise during replay, the replay fid will be inconsistent with
1447 * last_used/create fid */
1448 if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
1451 local_th = osp_get_storage_thandle(env, th, d);
1452 if (IS_ERR(local_th))
1453 RETURN(PTR_ERR(local_th));
1455 * it's OK if the import is inactive by this moment - id was created
1456 * by OST earlier, we just need to maintain it consistently on the disk
1457 * once import is reconnected, OSP will claim this and other objects
1458 * used and OST either keep them, if they exist or recreate
1461 /* we might have lost precreated objects */
1462 if (unlikely(d->opd_gap_count) > 0) {
1463 LASSERT(d->opd_pre != NULL);
1464 spin_lock(&d->opd_pre_lock);
1465 if (d->opd_gap_count > 0) {
1466 int count = d->opd_gap_count;
1468 ostid_set_id(&osi->osi_oi,
1469 fid_oid(&d->opd_gap_start_fid));
1470 d->opd_gap_count = 0;
1471 spin_unlock(&d->opd_pre_lock);
1473 CDEBUG(D_HA, "Writing gap "DFID"+%d in llog\n",
1474 PFID(&d->opd_gap_start_fid), count);
1475 /* real gap handling is disabled intil ORI-692 will be
1476 * fixed, now we only report gaps */
1478 spin_unlock(&d->opd_pre_lock);
1482 /* Only need update last_used oid file, seq file will only be update
1483 * during seq rollover */
1484 osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
1485 &d->opd_last_used_fid.f_oid, d->opd_index);
1487 rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
1488 &osi->osi_off, local_th);
1490 CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
1491 d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
1497 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1499 * Declare that the caller will destroy the specified OST object.
1501 * The OST object destroy will be handled via llog asynchronously. This
1502 * function will declare the credits for generating MDS_UNLINK64_REC llog.
1504 * \param[in] env pointer to the thread context
1505 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1506 * \param[in] th pointer to the transaction handler
1508 * \retval 0 for success
1509 * \retval negative error number on failure
1511 int osp_declare_object_destroy(const struct lu_env *env,
1512 struct dt_object *dt, struct thandle *th)
1514 struct osp_object *o = dt2osp_obj(dt);
1515 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1520 LASSERT(!osp->opd_connect_mdt);
1521 rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
1527 * Implement OSP layer dt_object_operations::do_destroy() interface.
1529 * Destroy the specified OST object.
1531 * The OSP generates a MDS_UNLINK64_REC record in the llog. There
1532 * will be some dedicated thread to handle the llog asynchronously.
1534 * It also marks the object as non-cached.
1536 * \param[in] env pointer to the thread context
1537 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1538 * \param[in] th pointer to the transaction handler
1540 * \retval 0 for success
1541 * \retval negative error number on failure
1543 static int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
1546 struct osp_object *o = dt2osp_obj(dt);
1547 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1552 o->opo_non_exist = 1;
1554 LASSERT(!osp->opd_connect_mdt);
1555 /* once transaction is committed put proper command on
1556 * the queue going to our OST. */
1557 rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
1561 /* not needed in cache any more */
1562 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1567 static int osp_orphan_index_lookup(const struct lu_env *env,
1568 struct dt_object *dt,
1570 const struct dt_key *key)
1575 static int osp_orphan_index_declare_insert(const struct lu_env *env,
1576 struct dt_object *dt,
1577 const struct dt_rec *rec,
1578 const struct dt_key *key,
1579 struct thandle *handle)
1584 static int osp_orphan_index_insert(const struct lu_env *env,
1585 struct dt_object *dt,
1586 const struct dt_rec *rec,
1587 const struct dt_key *key,
1588 struct thandle *handle,
1594 static int osp_orphan_index_declare_delete(const struct lu_env *env,
1595 struct dt_object *dt,
1596 const struct dt_key *key,
1597 struct thandle *handle)
1602 static int osp_orphan_index_delete(const struct lu_env *env,
1603 struct dt_object *dt,
1604 const struct dt_key *key,
1605 struct thandle *handle)
1611 * Initialize the OSP layer index iteration.
1613 * \param[in] env pointer to the thread context
1614 * \param[in] dt pointer to the index object to be iterated
1615 * \param[in] attr unused
1617 * \retval pointer to the iteration structure
1618 * \retval negative error number on failure
1620 struct dt_it *osp_it_init(const struct lu_env *env, struct dt_object *dt,
1627 return ERR_PTR(-ENOMEM);
1629 it->ooi_pos_ent = -1;
1631 it->ooi_attr = attr;
1633 return (struct dt_it *)it;
1637 * Finalize the OSP layer index iteration.
1639 * \param[in] env pointer to the thread context
1640 * \param[in] di pointer to the iteration structure
1642 void osp_it_fini(const struct lu_env *env, struct dt_it *di)
1644 struct osp_it *it = (struct osp_it *)di;
1645 struct page **pages = it->ooi_pages;
1646 int npages = it->ooi_total_npages;
1649 if (pages != NULL) {
1650 for (i = 0; i < npages; i++) {
1651 if (pages[i] != NULL) {
1652 if (pages[i] == it->ooi_cur_page) {
1654 it->ooi_cur_page = NULL;
1656 __free_page(pages[i]);
1659 OBD_FREE(pages, npages * sizeof(*pages));
1665 * Get more records for the iteration from peer.
1667 * The new records will be filled in an array of pages. The OSP side
1668 * allows 1MB bulk data to be transferred.
1670 * \param[in] env pointer to the thread context
1671 * \param[in] it pointer to the iteration structure
1673 * \retval 0 for success
1674 * \retval negative error number on failure
1676 static int osp_it_fetch(const struct lu_env *env, struct osp_it *it)
1678 struct lu_device *dev = it->ooi_obj->do_lu.lo_dev;
1679 struct osp_device *osp = lu2osp_dev(dev);
1680 struct page **pages;
1681 struct ptlrpc_request *req = NULL;
1682 struct ptlrpc_bulk_desc *desc;
1683 struct idx_info *ii;
1690 npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
1691 npages /= PAGE_SIZE;
1693 OBD_ALLOC(pages, npages * sizeof(*pages));
1697 it->ooi_pages = pages;
1698 it->ooi_total_npages = npages;
1699 for (i = 0; i < npages; i++) {
1700 pages[i] = alloc_page(GFP_NOFS);
1701 if (pages[i] == NULL)
1705 req = ptlrpc_request_alloc(osp->opd_obd->u.cli.cl_import,
1710 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, OBD_IDX_READ);
1712 ptlrpc_request_free(req);
1716 osp_set_req_replay(osp, req);
1717 req->rq_request_portal = OUT_PORTAL;
1718 ii = req_capsule_client_get(&req->rq_pill, &RMF_IDX_INFO);
1719 memset(ii, 0, sizeof(*ii));
1720 if (fid_is_last_id(lu_object_fid(&it->ooi_obj->do_lu))) {
1721 /* LFSCK will iterate orphan object[FID_SEQ_LAYOUT_BTREE,
1722 * ost_index, 0] with LAST_ID FID, so it needs to replace
1723 * the FID with orphan FID here */
1724 ii->ii_fid.f_seq = FID_SEQ_LAYOUT_RBTREE;
1725 ii->ii_fid.f_oid = osp->opd_index;
1726 ii->ii_fid.f_ver = 0;
1727 ii->ii_flags = II_FL_NOHASH;
1728 ii->ii_attrs = osp_dev2node(osp);
1730 ii->ii_fid = *lu_object_fid(&it->ooi_obj->do_lu);
1731 ii->ii_flags = II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
1733 ii->ii_attrs = it->ooi_attr;
1735 ii->ii_magic = IDX_INFO_MAGIC;
1736 ii->ii_count = npages * LU_PAGE_COUNT;
1737 ii->ii_hash_start = it->ooi_next;
1739 ptlrpc_at_set_req_timeout(req);
1741 desc = ptlrpc_prep_bulk_imp(req, npages, 1,
1742 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1744 &ptlrpc_bulk_kiov_pin_ops);
1746 ptlrpc_request_free(req);
1750 for (i = 0; i < npages; i++)
1751 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1754 ptlrpc_request_set_replen(req);
1755 rc = ptlrpc_queue_wait(req);
1759 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1760 req->rq_bulk->bd_nob_transferred);
1765 ii = req_capsule_server_get(&req->rq_pill, &RMF_IDX_INFO);
1766 if (ii->ii_magic != IDX_INFO_MAGIC)
1767 GOTO(out, rc = -EPROTO);
1769 npages = (ii->ii_count + LU_PAGE_COUNT - 1) >>
1770 (PAGE_SHIFT - LU_PAGE_SHIFT);
1771 if (npages > it->ooi_total_npages) {
1772 CERROR("%s: returned more pages than expected, %u > %u\n",
1773 osp->opd_obd->obd_name, npages, it->ooi_total_npages);
1774 GOTO(out, rc = -EINVAL);
1777 it->ooi_valid_npages = npages;
1778 if (ptlrpc_rep_need_swab(req))
1781 it->ooi_next = ii->ii_hash_end;
1784 ptlrpc_req_finished(req);
1790 * Move the iteration cursor to the next lu_page.
1792 * One system page (PAGE_SIZE) may contain multiple lu_page (4KB),
1793 * that depends on the LU_PAGE_COUNT. If it is not the last lu_page
1794 * in current system page, then move the iteration cursor to the next
1795 * lu_page in current system page. Otherwise, if there are more system
1796 * pages in the cache, then move the iteration cursor to the next system
1797 * page. If all the cached records (pages) have been iterated, then fetch
1798 * more records via osp_it_fetch().
1800 * \param[in] env pointer to the thread context
1801 * \param[in] di pointer to the iteration structure
1803 * \retval positive for end of the directory
1804 * \retval 0 for success
1805 * \retval negative error number on failure
1807 int osp_it_next_page(const struct lu_env *env, struct dt_it *di)
1809 struct osp_it *it = (struct osp_it *)di;
1810 struct lu_idxpage *idxpage;
1811 struct page **pages;
1817 idxpage = it->ooi_cur_idxpage;
1818 if (idxpage != NULL) {
1819 if (idxpage->lip_nr == 0)
1822 if (it->ooi_pos_ent < idxpage->lip_nr) {
1823 CDEBUG(D_INFO, "ooi_pos %d nr %d\n",
1824 (int)it->ooi_pos_ent, (int)idxpage->lip_nr);
1827 it->ooi_cur_idxpage = NULL;
1828 it->ooi_pos_lu_page++;
1831 if (it->ooi_pos_lu_page < LU_PAGE_COUNT) {
1832 it->ooi_cur_idxpage = (void *)it->ooi_cur_page +
1833 LU_PAGE_SIZE * it->ooi_pos_lu_page;
1835 lustre_swab_lip_header(it->ooi_cur_idxpage);
1836 if (it->ooi_cur_idxpage->lip_magic != LIP_MAGIC) {
1837 struct osp_device *osp =
1838 lu2osp_dev(it->ooi_obj->do_lu.lo_dev);
1840 CERROR("%s: invalid magic (%x != %x) for page "
1841 "%d/%d while read layout orphan index\n",
1842 osp->opd_obd->obd_name,
1843 it->ooi_cur_idxpage->lip_magic,
1844 LIP_MAGIC, it->ooi_pos_page,
1845 it->ooi_pos_lu_page);
1846 /* Skip this lu_page next time. */
1847 it->ooi_pos_ent = idxpage->lip_nr - 1;
1850 it->ooi_pos_ent = -1;
1854 kunmap(it->ooi_cur_page);
1855 it->ooi_cur_page = NULL;
1859 pages = it->ooi_pages;
1860 if (it->ooi_pos_page < it->ooi_valid_npages) {
1861 it->ooi_cur_page = kmap(pages[it->ooi_pos_page]);
1862 it->ooi_pos_lu_page = 0;
1866 for (i = 0; i < it->ooi_total_npages; i++) {
1867 if (pages[i] != NULL)
1868 __free_page(pages[i]);
1870 OBD_FREE(pages, it->ooi_total_npages * sizeof(*pages));
1872 it->ooi_pos_page = 0;
1873 it->ooi_total_npages = 0;
1874 it->ooi_valid_npages = 0;
1877 it->ooi_cur_page = NULL;
1878 it->ooi_cur_idxpage = NULL;
1879 it->ooi_pages = NULL;
1882 if (it->ooi_next == II_END_OFF)
1885 rc = osp_it_fetch(env, it);
1893 * Move the iteration cursor to the next record.
1895 * If there are more records in the lu_page, then move the iteration
1896 * cursor to the next record directly. Otherwise, move the iteration
1897 * cursor to the record in the next lu_page via osp_it_next_page()
1899 * \param[in] env pointer to the thread context
1900 * \param[in] di pointer to the iteration structure
1902 * \retval positive for end of the directory
1903 * \retval 0 for success
1904 * \retval negative error number on failure
1906 static int osp_orphan_it_next(const struct lu_env *env, struct dt_it *di)
1908 struct osp_it *it = (struct osp_it *)di;
1909 struct lu_idxpage *idxpage;
1914 idxpage = it->ooi_cur_idxpage;
1915 if (idxpage != NULL) {
1916 if (idxpage->lip_nr == 0)
1920 if (it->ooi_pos_ent < idxpage->lip_nr) {
1922 (struct lu_orphan_ent *)idxpage->lip_entries +
1925 lustre_swab_orphan_ent(it->ooi_ent);
1930 rc = osp_it_next_page(env, di);
1937 int osp_it_get(const struct lu_env *env, struct dt_it *di,
1938 const struct dt_key *key)
1943 void osp_it_put(const struct lu_env *env, struct dt_it *di)
1947 static struct dt_key *osp_orphan_it_key(const struct lu_env *env,
1948 const struct dt_it *di)
1950 struct osp_it *it = (struct osp_it *)di;
1951 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
1953 if (likely(ent != NULL))
1954 return (struct dt_key *)(&ent->loe_key);
1959 static int osp_orphan_it_key_size(const struct lu_env *env,
1960 const struct dt_it *di)
1962 return sizeof(struct lu_fid);
1965 static int osp_orphan_it_rec(const struct lu_env *env, const struct dt_it *di,
1966 struct dt_rec *rec, __u32 attr)
1968 struct osp_it *it = (struct osp_it *)di;
1969 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
1971 if (likely(ent != NULL)) {
1972 *(struct lu_orphan_rec *)rec = ent->loe_rec;
1979 __u64 osp_it_store(const struct lu_env *env, const struct dt_it *di)
1981 struct osp_it *it = (struct osp_it *)di;
1983 return it->ooi_next;
1987 * Locate the iteration cursor to the specified position (cookie).
1989 * \param[in] env pointer to the thread context
1990 * \param[in] di pointer to the iteration structure
1991 * \param[in] hash the specified position
1993 * \retval positive number for locating to the exactly position
1995 * \retval 0 for arriving at the end of the iteration
1996 * \retval negative error number on failure
1998 int osp_orphan_it_load(const struct lu_env *env, const struct dt_it *di,
2001 struct osp_it *it = (struct osp_it *)di;
2004 it->ooi_next = hash;
2005 rc = osp_orphan_it_next(env, (struct dt_it *)di);
2015 int osp_it_key_rec(const struct lu_env *env, const struct dt_it *di,
2021 static const struct dt_index_operations osp_orphan_index_ops = {
2022 .dio_lookup = osp_orphan_index_lookup,
2023 .dio_declare_insert = osp_orphan_index_declare_insert,
2024 .dio_insert = osp_orphan_index_insert,
2025 .dio_declare_delete = osp_orphan_index_declare_delete,
2026 .dio_delete = osp_orphan_index_delete,
2028 .init = osp_it_init,
2029 .fini = osp_it_fini,
2030 .next = osp_orphan_it_next,
2033 .key = osp_orphan_it_key,
2034 .key_size = osp_orphan_it_key_size,
2035 .rec = osp_orphan_it_rec,
2036 .store = osp_it_store,
2037 .load = osp_orphan_it_load,
2038 .key_rec = osp_it_key_rec,
2043 * Implement OSP layer dt_object_operations::do_index_try() interface.
2045 * Negotiate the index type.
2047 * If the target index is an IDIF object, then use osp_orphan_index_ops.
2048 * Otherwise, assign osp_md_index_ops to the dt_object::do_index_ops.
2049 * (\see lustre/include/lustre_fid.h for IDIF.)
2051 * \param[in] env pointer to the thread context
2052 * \param[in] dt pointer to the OSP layer dt_object
2053 * \param[in] feat unused
2055 * \retval 0 for success
2057 static int osp_index_try(const struct lu_env *env,
2058 struct dt_object *dt,
2059 const struct dt_index_features *feat)
2061 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2063 if (fid_is_last_id(fid) && fid_is_idif(fid))
2064 dt->do_index_ops = &osp_orphan_index_ops;
2066 dt->do_index_ops = &osp_md_index_ops;
2070 static struct dt_object_operations osp_obj_ops = {
2071 .do_declare_attr_get = osp_declare_attr_get,
2072 .do_attr_get = osp_attr_get,
2073 .do_declare_attr_set = osp_declare_attr_set,
2074 .do_attr_set = osp_attr_set,
2075 .do_declare_xattr_get = osp_declare_xattr_get,
2076 .do_xattr_get = osp_xattr_get,
2077 .do_declare_xattr_set = osp_declare_xattr_set,
2078 .do_xattr_set = osp_xattr_set,
2079 .do_declare_create = osp_declare_object_create,
2080 .do_create = osp_object_create,
2081 .do_declare_destroy = osp_declare_object_destroy,
2082 .do_destroy = osp_object_destroy,
2083 .do_index_try = osp_index_try,
2087 * Implement OSP layer lu_object_operations::loo_object_init() interface.
2089 * Initialize the object.
2091 * If it is a remote MDT object, then call do_attr_get() to fetch
2092 * the attribute from the peer.
2094 * \param[in] env pointer to the thread context
2095 * \param[in] o pointer to the OSP layer lu_object
2096 * \param[in] conf unused
2098 * \retval 0 for success
2099 * \retval negative error number on failure
2101 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
2102 const struct lu_object_conf *conf)
2104 struct osp_object *po = lu2osp_obj(o);
2108 spin_lock_init(&po->opo_lock);
2109 o->lo_header->loh_attr |= LOHA_REMOTE;
2110 INIT_LIST_HEAD(&po->opo_xattr_list);
2111 INIT_LIST_HEAD(&po->opo_invalidate_cb_list);
2113 if (is_ost_obj(o)) {
2114 po->opo_obj.do_ops = &osp_obj_ops;
2116 struct lu_attr *la = &osp_env_info(env)->osi_attr;
2118 po->opo_obj.do_ops = &osp_md_obj_ops;
2119 po->opo_obj.do_body_ops = &osp_md_body_ops;
2121 if (conf != NULL && conf->loc_flags & LOC_F_NEW) {
2122 po->opo_non_exist = 1;
2124 rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o),
2127 o->lo_header->loh_attr |=
2128 LOHA_EXISTS | (la->la_mode & S_IFMT);
2129 if (rc == -ENOENT) {
2130 po->opo_non_exist = 1;
2134 init_rwsem(&po->opo_sem);
2140 * Implement OSP layer lu_object_operations::loo_object_free() interface.
2142 * Finalize the object.
2144 * If the OSP object has attributes cache, then destroy the cache.
2145 * Free the object finally.
2147 * \param[in] env pointer to the thread context
2148 * \param[in] o pointer to the OSP layer lu_object
2150 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
2152 struct osp_object *obj = lu2osp_obj(o);
2153 struct lu_object_header *h = o->lo_header;
2154 struct osp_xattr_entry *oxe;
2155 struct osp_xattr_entry *tmp;
2158 dt_object_fini(&obj->opo_obj);
2159 lu_object_header_fini(h);
2160 list_for_each_entry_safe(oxe, tmp, &obj->opo_xattr_list, oxe_list) {
2161 list_del(&oxe->oxe_list);
2162 count = atomic_read(&oxe->oxe_ref);
2163 LASSERTF(count == 1,
2164 "Still has %d users on the xattr entry %.*s\n",
2165 count-1, (int)oxe->oxe_namelen, oxe->oxe_buf);
2167 OBD_FREE(oxe, oxe->oxe_buflen);
2169 OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
2173 * Implement OSP layer lu_object_operations::loo_object_release() interface.
2175 * Cleanup (not free) the object.
2177 * If it is a reserved object but failed to be created, or it is an OST
2178 * object, then mark the object as non-cached.
2180 * \param[in] env pointer to the thread context
2181 * \param[in] o pointer to the OSP layer lu_object
2183 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
2185 struct osp_object *po = lu2osp_obj(o);
2186 struct osp_device *d = lu2osp_dev(o->lo_dev);
2191 * release reservation if object was declared but not created
2192 * this may require lu_object_put() in LOD
2194 if (unlikely(po->opo_reserved)) {
2195 LASSERT(d->opd_pre != NULL);
2196 LASSERT(d->opd_pre_reserved > 0);
2197 spin_lock(&d->opd_pre_lock);
2198 d->opd_pre_reserved--;
2199 spin_unlock(&d->opd_pre_lock);
2201 /* not needed in cache any more */
2202 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2206 /* XXX: Currently, NOT cache OST-object on MDT because:
2207 * 1. it is not often accessed on MDT.
2208 * 2. avoid up layer (such as LFSCK) to load too many
2209 * once-used OST-objects. */
2210 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2215 static int osp_object_print(const struct lu_env *env, void *cookie,
2216 lu_printer_t p, const struct lu_object *l)
2218 const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
2220 return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
2223 static int osp_object_invariant(const struct lu_object *o)
2228 struct lu_object_operations osp_lu_obj_ops = {
2229 .loo_object_init = osp_object_init,
2230 .loo_object_free = osp_object_free,
2231 .loo_object_release = osp_object_release,
2232 .loo_object_print = osp_object_print,
2233 .loo_object_invariant = osp_object_invariant