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, 2017, 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);
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 size_t size = sizeof(*oxe) + namelen + 1 + buf->lb_len;
332 bool unlink_only = false;
334 if (oxe->oxe_buflen < size) {
335 OBD_ALLOC(new, size);
337 INIT_LIST_HEAD(&new->oxe_list);
338 new->oxe_buflen = size;
339 new->oxe_namelen = namelen;
340 memcpy(new->oxe_buf, oxe->oxe_buf, namelen);
341 new->oxe_value = new->oxe_buf + namelen + 1;
342 /* One ref is for the caller,
343 * the other is for the entry on the list. */
344 atomic_set(&new->oxe_ref, 2);
345 __osp_oac_xattr_assignment(obj, new, buf);
348 CWARN("%s: cannot update cached xattr %.*s of "DFID"\n",
349 osp_dto2name(obj), namelen, oxe->oxe_buf,
350 PFID(lu_object_fid(&obj->opo_obj.do_lu)));
354 spin_lock(&obj->opo_lock);
355 old = osp_oac_xattr_find_locked(obj, oxe->oxe_buf, namelen);
358 /* Unlink the 'old'. */
359 list_del_init(&old->oxe_list);
361 /* Drop the ref for 'old' on list. */
362 osp_oac_xattr_put(old);
364 /* Drop the ref for current using. */
365 osp_oac_xattr_put(oxe);
368 /* Insert 'new' into list. */
369 list_add_tail(&new->oxe_list, &obj->opo_xattr_list);
370 } else if (unlink_only) {
371 /* Unlink the 'old'. */
372 list_del_init(&old->oxe_list);
374 /* Drop the ref for 'old' on list. */
375 osp_oac_xattr_put(old);
377 __osp_oac_xattr_assignment(obj, oxe, buf);
380 /* Drop the ref for current using. */
381 osp_oac_xattr_put(oxe);
384 /* Someone unlinked the 'old' by race,
385 * insert the 'new' one into list. */
386 list_add_tail(&new->oxe_list, &obj->opo_xattr_list);
388 spin_unlock(&obj->opo_lock);
394 * Parse the OSP object attribute from the RPC reply.
396 * If the attribute is valid, then it will be added to the OSP object
399 * \param[in] env pointer to the thread context
400 * \param[in] reply pointer to the RPC reply
401 * \param[in] req pointer to the RPC request
402 * \param[out] attr pointer to buffer to hold the output attribute
403 * \param[in] obj pointer to the OSP object
404 * \param[in] index the index of the attribute buffer in the reply
406 * \retval 0 for success
407 * \retval negative error number on failure
409 static int osp_get_attr_from_reply(const struct lu_env *env,
410 struct object_update_reply *reply,
411 struct ptlrpc_request *req,
412 struct lu_attr *attr,
413 struct osp_object *obj, int index)
415 struct osp_thread_info *osi = osp_env_info(env);
416 struct lu_buf *rbuf = &osi->osi_lb2;
417 struct obdo *lobdo = &osi->osi_obdo;
421 rc = object_update_result_data_get(reply, rbuf, index);
425 wobdo = rbuf->lb_buf;
426 if (rbuf->lb_len != sizeof(*wobdo))
429 LASSERT(req != NULL);
430 if (ptlrpc_req_need_swab(req))
431 lustre_swab_obdo(wobdo);
433 lustre_get_wire_obdo(NULL, lobdo, wobdo);
434 spin_lock(&obj->opo_lock);
435 la_from_obdo(&obj->opo_attr, lobdo, lobdo->o_valid);
437 *attr = obj->opo_attr;
438 spin_unlock(&obj->opo_lock);
444 * Interpreter function for getting OSP object attribute asynchronously.
446 * Called to interpret the result of an async mode RPC for getting the
447 * OSP object attribute.
449 * \param[in] env pointer to the thread context
450 * \param[in] reply pointer to the RPC reply
451 * \param[in] req pointer to the RPC request
452 * \param[in] obj pointer to the OSP object
453 * \param[out] data pointer to buffer to hold the output attribute
454 * \param[in] index the index of the attribute buffer in the reply
455 * \param[in] rc the result for handling the RPC
457 * \retval 0 for success
458 * \retval negative error number on failure
460 static int osp_attr_get_interpterer(const struct lu_env *env,
461 struct object_update_reply *reply,
462 struct ptlrpc_request *req,
463 struct osp_object *obj,
464 void *data, int index, int rc)
466 struct lu_attr *attr = data;
469 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
470 obj->opo_non_exist = 0;
472 return osp_get_attr_from_reply(env, reply, req, NULL, obj,
476 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
477 obj->opo_non_exist = 1;
480 spin_lock(&obj->opo_lock);
482 spin_unlock(&obj->opo_lock);
489 * Implement OSP layer dt_object_operations::do_declare_attr_get() interface.
491 * Declare that the caller will get attribute from the specified OST object.
493 * This function adds an Object Unified Target (OUT) sub-request to the per-OSP
494 * based shared asynchronous request queue. The osp_attr_get_interpterer()
495 * is registered as the interpreter function to handle the result of this
498 * \param[in] env pointer to the thread context
499 * \param[in] dt pointer to the OSP layer dt_object
501 * \retval 0 for success
502 * \retval negative error number on failure
504 static int osp_declare_attr_get(const struct lu_env *env, struct dt_object *dt)
506 struct osp_object *obj = dt2osp_obj(dt);
507 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
510 mutex_lock(&osp->opd_async_requests_mutex);
511 rc = osp_insert_async_request(env, OUT_ATTR_GET, obj, 0, NULL, NULL,
512 &obj->opo_attr, sizeof(struct obdo),
513 osp_attr_get_interpterer);
514 mutex_unlock(&osp->opd_async_requests_mutex);
520 * Implement OSP layer dt_object_operations::do_attr_get() interface.
522 * Get attribute from the specified MDT/OST object.
524 * If the attribute is in the OSP object attributes cache, then return
525 * the cached attribute directly. Otherwise it will trigger an OUT RPC
526 * to the peer to get the attribute synchronously, if successful, add it
527 * to the OSP attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
529 * \param[in] env pointer to the thread context
530 * \param[in] dt pointer to the OSP layer dt_object
531 * \param[out] attr pointer to the buffer to hold the output attribute
533 * \retval 0 for success
534 * \retval negative error number on failure
536 int osp_attr_get(const struct lu_env *env, struct dt_object *dt,
537 struct lu_attr *attr)
539 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
540 struct osp_object *obj = dt2osp_obj(dt);
541 struct dt_device *dev = &osp->opd_dt_dev;
542 struct osp_update_request *update;
543 struct object_update_reply *reply;
544 struct ptlrpc_request *req = NULL;
548 if (is_ost_obj(&dt->do_lu) && obj->opo_non_exist)
551 spin_lock(&obj->opo_lock);
552 if (obj->opo_attr.la_valid != 0 && !obj->opo_stale) {
553 *attr = obj->opo_attr;
554 spin_unlock(&obj->opo_lock);
558 spin_unlock(&obj->opo_lock);
560 update = osp_update_request_create(dev);
562 RETURN(PTR_ERR(update));
564 rc = osp_update_rpc_pack(env, attr_get, update, OUT_ATTR_GET,
565 lu_object_fid(&dt->do_lu));
567 CERROR("%s: Insert update error "DFID": rc = %d\n",
568 dev->dd_lu_dev.ld_obd->obd_name,
569 PFID(lu_object_fid(&dt->do_lu)), rc);
574 rc = osp_remote_sync(env, osp, update, &req);
577 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
578 obj->opo_non_exist = 1;
580 CERROR("%s:osp_attr_get update error "DFID": rc = %d\n",
581 dev->dd_lu_dev.ld_obd->obd_name,
582 PFID(lu_object_fid(&dt->do_lu)), rc);
588 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
589 obj->opo_non_exist = 0;
590 reply = req_capsule_server_sized_get(&req->rq_pill,
591 &RMF_OUT_UPDATE_REPLY,
592 OUT_UPDATE_REPLY_SIZE);
593 if (reply == NULL || reply->ourp_magic != UPDATE_REPLY_MAGIC)
594 GOTO(out, rc = -EPROTO);
596 rc = osp_get_attr_from_reply(env, reply, req, attr, obj, 0);
600 spin_lock(&obj->opo_lock);
602 spin_unlock(&obj->opo_lock);
608 ptlrpc_req_finished(req);
610 osp_update_request_destroy(env, update);
616 * Implement OSP layer dt_object_operations::do_declare_attr_set() interface.
618 * If the transaction is not remote one, then declare the credits that will
619 * be used for the subsequent llog record for the object's attributes.
621 * \param[in] env pointer to the thread context
622 * \param[in] dt pointer to the OSP layer dt_object
623 * \param[in] attr pointer to the attribute to be set
624 * \param[in] th pointer to the transaction handler
626 * \retval 0 for success
627 * \retval negative error number on failure
629 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
630 const struct lu_attr *attr, struct thandle *th)
632 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
633 struct osp_object *o = dt2osp_obj(dt);
636 if (is_only_remote_trans(th))
637 return osp_md_declare_attr_set(env, dt, attr, th);
639 * Usually we don't allow server stack to manipulate size
640 * but there is a special case when striping is created
641 * late, after stripeless file got truncated to non-zero.
643 * In this case we do the following:
645 * 1) grab id in declare - this can lead to leaked OST objects
646 * but we don't currently have proper mechanism and the only
647 * options we have are to do truncate RPC holding transaction
648 * open (very bad) or to grab id in declare at cost of leaked
649 * OST object in same very rare unfortunate case (just bad)
650 * notice 1.6-2.0 do assignment outside of running transaction
651 * all the time, meaning many more chances for leaked objects.
653 * 2) send synchronous truncate RPC with just assigned id
656 /* there are few places in MDD code still passing NULL
657 * XXX: to be fixed soon */
661 if (attr->la_valid & LA_SIZE && attr->la_size > 0 &&
662 fid_is_zero(lu_object_fid(&o->opo_obj.do_lu))) {
663 LASSERT(!dt_object_exists(dt));
664 osp_object_assign_fid(env, d, o);
665 rc = osp_object_truncate(env, dt, attr->la_size);
670 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
673 /* track all UID/GID, projid, and layout version changes via llog */
674 rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
680 * Implement OSP layer dt_object_operations::do_attr_set() interface.
682 * Set attribute to the specified OST object.
684 * If the transaction is a remote one, then add OUT_ATTR_SET sub-request
685 * in the OUT RPC that will be flushed when the remote transaction stop.
686 * Otherwise, it will generate a MDS_SETATTR64_REC record in the llog that
687 * will be handled by a dedicated thread asynchronously.
689 * If the attribute entry exists in the OSP object attributes cache,
690 * then update the cached attribute according to given attribute.
692 * \param[in] env pointer to the thread context
693 * \param[in] dt pointer to the OSP layer dt_object
694 * \param[in] attr pointer to the attribute to be set
695 * \param[in] th pointer to the transaction handler
697 * \retval 0 for success
698 * \retval negative error number on failure
700 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
701 const struct lu_attr *attr, struct thandle *th)
703 struct osp_object *o = dt2osp_obj(dt);
707 /* we're interested in uid/gid/projid/layout version changes only */
708 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
711 if (!is_only_remote_trans(th)) {
712 if (attr->la_flags & LUSTRE_SET_SYNC_FL) {
713 struct ptlrpc_request *req = NULL;
714 struct osp_update_request *update = NULL;
715 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
717 update = osp_update_request_create(&osp->opd_dt_dev);
719 RETURN(PTR_ERR(update));
721 rc = osp_update_rpc_pack(env, attr_set, update,
723 lu_object_fid(&dt->do_lu),
726 CERROR("%s: update error "DFID": rc = %d\n",
727 osp->opd_obd->obd_name,
728 PFID(lu_object_fid(&dt->do_lu)), rc);
730 osp_update_request_destroy(env, update);
734 rc = osp_remote_sync(env, osp, update, &req);
736 ptlrpc_req_finished(req);
738 osp_update_request_destroy(env, update);
740 rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
741 /* XXX: send new uid/gid to OST ASAP? */
746 /* It is for OST-object attr_set directly without updating
747 * local MDT-object attribute. It is usually used by LFSCK. */
748 rc = osp_md_attr_set(env, dt, attr, th);
749 CDEBUG(D_INFO, "(1) set attr "DFID": rc = %d\n",
750 PFID(&dt->do_lu.lo_header->loh_fid), rc);
755 /* Update the OSP object attributes cache. */
757 spin_lock(&o->opo_lock);
758 if (attr->la_valid & LA_UID) {
759 la->la_uid = attr->la_uid;
760 la->la_valid |= LA_UID;
763 if (attr->la_valid & LA_GID) {
764 la->la_gid = attr->la_gid;
765 la->la_valid |= LA_GID;
767 if (attr->la_valid & LA_PROJID) {
768 la->la_projid = attr->la_projid;
769 la->la_valid |= LA_PROJID;
771 spin_unlock(&o->opo_lock);
778 * Interpreter function for getting OSP object extended attribute asynchronously
780 * Called to interpret the result of an async mode RPC for getting the
781 * OSP object extended attribute.
783 * \param[in] env pointer to the thread context
784 * \param[in] reply pointer to the RPC reply
785 * \param[in] req pointer to the RPC request
786 * \param[in] obj pointer to the OSP object
787 * \param[out] data pointer to OSP object attributes cache
788 * \param[in] index the index of the attribute buffer in the reply
789 * \param[in] rc the result for handling the RPC
791 * \retval 0 for success
792 * \retval negative error number on failure
794 static int osp_xattr_get_interpterer(const struct lu_env *env,
795 struct object_update_reply *reply,
796 struct ptlrpc_request *req,
797 struct osp_object *obj,
798 void *data, int index, int rc)
800 struct osp_xattr_entry *oxe = data;
802 spin_lock(&obj->opo_lock);
804 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
805 size_t len = sizeof(*oxe) + oxe->oxe_namelen + 1;
807 rc = object_update_result_data_get(reply, rbuf, index);
808 if (rc == -ENOENT || rc == -ENODATA || rc == 0) {
814 if (unlikely(rc < 0) ||
815 rbuf->lb_len > (oxe->oxe_buflen - len)) {
820 __osp_oac_xattr_assignment(obj, oxe, rbuf);
821 } else if (rc == -ENOENT || rc == -ENODATA) {
829 spin_unlock(&obj->opo_lock);
831 /* Put the reference obtained in the osp_declare_xattr_get(). */
832 osp_oac_xattr_put(oxe);
838 * Implement OSP dt_object_operations::do_declare_xattr_get() interface.
840 * Declare that the caller will get extended attribute from the specified
843 * This function will add an OUT_XATTR_GET sub-request to the per OSP
844 * based shared asynchronous request queue with the interpreter function:
845 * osp_xattr_get_interpterer().
847 * \param[in] env pointer to the thread context
848 * \param[in] dt pointer to the OSP layer dt_object
849 * \param[out] buf pointer to the lu_buf to hold the extended attribute
850 * \param[in] name the name for the expected extended attribute
852 * \retval 0 for success
853 * \retval negative error number on failure
855 static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
856 struct lu_buf *buf, const char *name)
858 struct osp_object *obj = dt2osp_obj(dt);
859 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
860 struct osp_xattr_entry *oxe;
864 LASSERT(buf != NULL);
865 LASSERT(name != NULL);
867 if (unlikely(buf->lb_len == 0))
870 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
874 len = strlen(name) + 1;
875 mutex_lock(&osp->opd_async_requests_mutex);
876 rc = osp_insert_async_request(env, OUT_XATTR_GET, obj, 1,
877 &len, (const void **)&name,
879 osp_xattr_get_interpterer);
881 mutex_unlock(&osp->opd_async_requests_mutex);
882 osp_oac_xattr_put(oxe);
884 struct osp_update_request *our;
885 struct osp_update_request_sub *ours;
887 /* XXX: Currently, we trigger the batched async OUT
888 * RPC via dt_declare_xattr_get(). It is not
889 * perfect solution, but works well now.
891 * We will improve it in the future. */
892 our = osp->opd_async_requests;
893 ours = osp_current_object_update_request(our);
894 if (ours != NULL && ours->ours_req != NULL &&
895 ours->ours_req->ourq_count > 0) {
896 osp->opd_async_requests = NULL;
897 mutex_unlock(&osp->opd_async_requests_mutex);
898 rc = osp_unplug_async_request(env, osp, our);
900 mutex_unlock(&osp->opd_async_requests_mutex);
908 * Implement OSP layer dt_object_operations::do_xattr_get() interface.
910 * Get extended attribute from the specified MDT/OST object.
912 * If the extended attribute is in the OSP object attributes cache, then
913 * return the cached extended attribute directly. Otherwise it will get
914 * the extended attribute synchronously, if successful, add it to the OSP
915 * attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
917 * There is a race condition: some other thread has added the named extended
918 * attributed entry to the OSP object attributes cache during the current
919 * OUT_XATTR_GET handling. If such case happens, the OSP will replace the
920 * (just) existing extended attribute entry with the new replied one.
922 * \param[in] env pointer to the thread context
923 * \param[in] dt pointer to the OSP layer dt_object
924 * \param[out] buf pointer to the lu_buf to hold the extended attribute
925 * \param[in] name the name for the expected extended attribute
927 * \retval 0 for success
928 * \retval negative error number on failure
930 int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
931 struct lu_buf *buf, const char *name)
933 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
934 struct osp_object *obj = dt2osp_obj(dt);
935 struct dt_device *dev = &osp->opd_dt_dev;
936 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
937 struct osp_update_request *update = NULL;
938 struct ptlrpc_request *req = NULL;
939 struct object_update_reply *reply;
940 struct osp_xattr_entry *oxe = NULL;
941 const char *dname = osp_dto2name(obj);
945 LASSERT(buf != NULL);
946 LASSERT(name != NULL);
948 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NETWORK) &&
949 osp->opd_index == cfs_fail_val) {
950 if (is_ost_obj(&dt->do_lu)) {
951 if (osp_dev2node(osp) == cfs_fail_val)
954 if (strcmp(name, XATTR_NAME_LINK) == 0)
959 if (unlikely(obj->opo_non_exist))
962 oxe = osp_oac_xattr_find(obj, name, false);
964 spin_lock(&obj->opo_lock);
965 if (oxe->oxe_ready) {
967 GOTO(unlock, rc = -ENODATA);
969 if (buf->lb_buf == NULL)
970 GOTO(unlock, rc = oxe->oxe_vallen);
972 if (buf->lb_len < oxe->oxe_vallen)
973 GOTO(unlock, rc = -ERANGE);
975 memcpy(buf->lb_buf, oxe->oxe_value,
978 GOTO(unlock, rc = oxe->oxe_vallen);
981 spin_unlock(&obj->opo_lock);
982 osp_oac_xattr_put(oxe);
986 spin_unlock(&obj->opo_lock);
988 update = osp_update_request_create(dev);
990 GOTO(out, rc = PTR_ERR(update));
992 rc = osp_update_rpc_pack(env, xattr_get, update, OUT_XATTR_GET,
993 lu_object_fid(&dt->do_lu), name, buf->lb_len);
995 CERROR("%s: Insert update error "DFID": rc = %d\n",
996 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
1000 rc = osp_remote_sync(env, osp, update, &req);
1002 if (rc == -ENOENT) {
1003 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
1004 obj->opo_non_exist = 1;
1008 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
1011 CWARN("%s: Fail to add xattr (%s) to cache for "
1012 DFID" (1): rc = %d\n", dname, name,
1013 PFID(lu_object_fid(&dt->do_lu)), rc);
1018 spin_lock(&obj->opo_lock);
1019 if (rc == -ENOENT || rc == -ENODATA) {
1025 spin_unlock(&obj->opo_lock);
1030 reply = req_capsule_server_sized_get(&req->rq_pill,
1031 &RMF_OUT_UPDATE_REPLY,
1032 OUT_UPDATE_REPLY_SIZE);
1033 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1034 CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
1035 dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
1036 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
1038 GOTO(out, rc = -EPROTO);
1041 rc = object_update_result_data_get(reply, rbuf, 0);
1042 if (rc < 0 || rbuf->lb_len == 0) {
1043 if (oxe == NULL && rc == -ENODATA) {
1044 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
1047 CWARN("%s: Fail to add xattr (%s) to cache for "
1048 DFID" (1): rc = %d\n", dname, name,
1049 PFID(lu_object_fid(&dt->do_lu)), rc);
1055 spin_lock(&obj->opo_lock);
1056 if (unlikely(rc == -ENODATA)) {
1062 spin_unlock(&obj->opo_lock);
1068 /* For detecting EA size. */
1073 oxe = osp_oac_xattr_find_or_add(obj, name, rbuf->lb_len);
1075 CWARN("%s: Fail to add xattr (%s) to "
1076 "cache for "DFID" (2): rc = %d\n",
1077 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1083 oxe = osp_oac_xattr_assignment(obj, oxe, rbuf);
1088 if (rc > 0 && buf->lb_buf) {
1089 if (unlikely(buf->lb_len < rbuf->lb_len))
1092 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
1096 ptlrpc_req_finished(req);
1098 if (update && !IS_ERR(update))
1099 osp_update_request_destroy(env, update);
1102 osp_oac_xattr_put(oxe);
1108 * Implement OSP layer dt_object_operations::do_declare_xattr_set() interface.
1110 * Declare that the caller will set extended attribute to the specified
1113 * If it is non-remote transaction, it will add an OUT_XATTR_SET sub-request
1114 * to the OUT RPC that will be flushed when the transaction start. And if the
1115 * OSP attributes cache is initialized, then check whether the name extended
1116 * attribute entry exists in the cache or not. If yes, replace it; otherwise,
1117 * add the extended attribute to the cache.
1119 * \param[in] env pointer to the thread context
1120 * \param[in] dt pointer to the OSP layer dt_object
1121 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1122 * \param[in] name the name of the extended attribute to be set
1123 * \param[in] flag to indicate the detailed set operation: LU_XATTR_CREATE
1124 * or LU_XATTR_REPLACE or others
1125 * \param[in] th pointer to the transaction handler
1127 * \retval 0 for success
1128 * \retval negative error number on failure
1130 int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
1131 const struct lu_buf *buf, const char *name,
1132 int flag, struct thandle *th)
1134 return osp_trans_update_request_create(th);
1138 * Implement OSP layer dt_object_operations::do_xattr_set() interface.
1140 * Set extended attribute to the specified MDT/OST object.
1142 * Add an OUT_XATTR_SET sub-request into the OUT RPC that will be flushed in
1143 * the transaction stop. And if the OSP attributes cache is initialized, then
1144 * check whether the name extended attribute entry exists in the cache or not.
1145 * If yes, replace it; otherwise, add the extended attribute to the cache.
1147 * \param[in] env pointer to the thread context
1148 * \param[in] dt pointer to the OSP layer dt_object
1149 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1150 * \param[in] name the name of the extended attribute to be set
1151 * \param[in] fl to indicate the detailed set operation: LU_XATTR_CREATE
1152 * or LU_XATTR_REPLACE or others
1153 * \param[in] th pointer to the transaction handler
1155 * \retval 0 for success
1156 * \retval negative error number on failure
1158 int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
1159 const struct lu_buf *buf, const char *name, int fl,
1162 struct osp_object *o = dt2osp_obj(dt);
1163 struct osp_update_request *update;
1164 struct osp_xattr_entry *oxe;
1168 update = thandle_to_osp_update_request(th);
1169 LASSERT(update != NULL);
1171 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zd\n",
1172 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
1174 rc = osp_update_rpc_pack(env, xattr_set, update, OUT_XATTR_SET,
1175 lu_object_fid(&dt->do_lu), buf, name, fl);
1179 /* Do not cache linkEA that may be self-adjusted by peers
1180 * under EA overflow case. */
1181 if (strcmp(name, XATTR_NAME_LINK) == 0) {
1182 oxe = osp_oac_xattr_find(o, name, true);
1184 osp_oac_xattr_put(oxe);
1189 oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
1191 CWARN("%s: cannot cache xattr '%s' of "DFID"\n",
1192 osp_dto2name(o), name, PFID(lu_object_fid(&dt->do_lu)));
1197 oxe = osp_oac_xattr_assignment(o, oxe, buf);
1199 osp_oac_xattr_put(oxe);
1205 * Implement OSP layer dt_object_operations::do_declare_xattr_del() interface.
1207 * Declare that the caller will delete extended attribute on the specified
1210 * If it is non-remote transaction, it will add an OUT_XATTR_DEL sub-request
1211 * to the OUT RPC that will be flushed when the transaction start. And if the
1212 * name extended attribute entry exists in the OSP attributes cache, then remove
1213 * it from the cache.
1215 * \param[in] env pointer to the thread context
1216 * \param[in] dt pointer to the OSP layer dt_object
1217 * \param[in] name the name of the extended attribute to be set
1218 * \param[in] th pointer to the transaction handler
1220 * \retval 0 for success
1221 * \retval negative error number on failure
1223 int osp_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
1224 const char *name, struct thandle *th)
1226 return osp_trans_update_request_create(th);
1230 * Implement OSP layer dt_object_operations::do_xattr_del() interface.
1232 * Delete extended attribute on the specified MDT/OST object.
1234 * If it is remote transaction, it will add an OUT_XATTR_DEL sub-request into
1235 * the OUT RPC that will be flushed when the transaction stop. And if the name
1236 * extended attribute entry exists in the OSP attributes cache, then remove it
1239 * \param[in] env pointer to the thread context
1240 * \param[in] dt pointer to the OSP layer dt_object
1241 * \param[in] name the name of the extended attribute to be set
1242 * \param[in] th pointer to the transaction handler
1244 * \retval 0 for success
1245 * \retval negative error number on failure
1247 int osp_xattr_del(const struct lu_env *env, struct dt_object *dt,
1248 const char *name, struct thandle *th)
1250 struct osp_update_request *update;
1251 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1252 struct osp_object *o = dt2osp_obj(dt);
1253 struct osp_xattr_entry *oxe;
1256 update = thandle_to_osp_update_request(th);
1257 LASSERT(update != NULL);
1259 rc = osp_update_rpc_pack(env, xattr_del, update, OUT_XATTR_DEL,
1264 oxe = osp_oac_xattr_find(o, name, true);
1266 /* Drop the ref for entry on list. */
1267 osp_oac_xattr_put(oxe);
1272 void osp_obj_invalidate_cache(struct osp_object *obj)
1274 struct osp_xattr_entry *oxe;
1275 struct osp_xattr_entry *tmp;
1277 spin_lock(&obj->opo_lock);
1278 list_for_each_entry_safe(oxe, tmp, &obj->opo_xattr_list, oxe_list) {
1280 list_del_init(&oxe->oxe_list);
1281 osp_oac_xattr_put(oxe);
1283 obj->opo_attr.la_valid = 0;
1284 spin_unlock(&obj->opo_lock);
1288 * Implement OSP layer dt_object_operations::do_invalidate() interface.
1290 * Invalidate attributes cached on the specified MDT/OST object.
1292 * \param[in] env pointer to the thread context
1293 * \param[in] dt pointer to the OSP layer dt_object
1295 * \retval 0 for success
1296 * \retval negative error number on failure
1298 int osp_invalidate(const struct lu_env *env, struct dt_object *dt)
1300 struct osp_object *obj = dt2osp_obj(dt);
1303 CDEBUG(D_HA, "Invalidate osp_object "DFID"\n",
1304 PFID(lu_object_fid(&dt->do_lu)));
1305 osp_obj_invalidate_cache(obj);
1307 spin_lock(&obj->opo_lock);
1309 spin_unlock(&obj->opo_lock);
1315 * Implement OSP layer dt_object_operations::do_declare_create() interface.
1317 * Declare that the caller will create the OST object.
1319 * If the transaction is a remote transaction and the FID for the OST-object
1320 * has been assigned already, then handle it as creating (remote) MDT object
1321 * via osp_md_declare_create(). This function is usually used for LFSCK
1322 * to re-create the lost OST object. Otherwise, if it is not replay case, the
1323 * OSP will reserve pre-created object for the subsequent create operation;
1324 * if the MDT side cached pre-created objects are less than some threshold,
1325 * then it will wakeup the pre-create thread.
1327 * \param[in] env pointer to the thread context
1328 * \param[in] dt pointer to the OSP layer dt_object
1329 * \param[in] attr the attribute for the object to be created
1330 * \param[in] hint pointer to the hint for creating the object, such as
1332 * \param[in] dof pointer to the dt_object_format for help the creation
1333 * \param[in] th pointer to the transaction handler
1335 * \retval 0 for success
1336 * \retval negative error number on failure
1338 static int osp_declare_create(const struct lu_env *env, struct dt_object *dt,
1339 struct lu_attr *attr,
1340 struct dt_allocation_hint *hint,
1341 struct dt_object_format *dof, struct thandle *th)
1343 struct osp_thread_info *osi = osp_env_info(env);
1344 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1345 struct osp_object *o = dt2osp_obj(dt);
1346 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1347 struct thandle *local_th;
1352 if (is_only_remote_trans(th) && !fid_is_zero(fid)) {
1353 LASSERT(fid_is_sane(fid));
1355 rc = osp_md_declare_create(env, dt, attr, hint, dof, th);
1360 /* should happen to non-0 OSP only so that at least one object
1361 * has been already declared in the scenario and LOD should
1363 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
1366 LASSERT(d->opd_last_used_oid_file);
1369 * There can be gaps in precreated ids and record to unlink llog
1370 * XXX: we do not handle gaps yet, implemented before solution
1371 * was found to be racy, so we disabled that. there is no
1372 * point in making useless but expensive llog declaration.
1374 /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
1376 local_th = osp_get_storage_thandle(env, th, d);
1377 if (IS_ERR(local_th))
1378 RETURN(PTR_ERR(local_th));
1380 if (unlikely(!fid_is_zero(fid))) {
1381 /* replay case: caller knows fid */
1382 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1383 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1384 osi->osi_lb.lb_buf = NULL;
1386 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1387 &osi->osi_lb, osi->osi_off,
1393 * in declaration we need to reserve object so that we don't block
1394 * awaiting precreation RPC to complete
1396 rc = osp_precreate_reserve(env, d);
1398 * we also need to declare update to local "last used id" file for
1399 * recovery if object isn't used for a reason, we need to release
1400 * reservation, this can be made in osd_object_release()
1403 /* mark id is reserved: in create we don't want to talk
1405 LASSERT(o->opo_reserved == 0);
1406 o->opo_reserved = 1;
1408 /* common for all OSPs file hystorically */
1409 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1410 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1411 osi->osi_lb.lb_buf = NULL;
1412 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1413 &osi->osi_lb, osi->osi_off,
1416 /* not needed in the cache anymore */
1417 set_bit(LU_OBJECT_HEARD_BANSHEE,
1418 &dt->do_lu.lo_header->loh_flags);
1424 * Implement OSP layer dt_object_operations::do_create() interface.
1426 * Create the OST object.
1428 * If the transaction is a remote transaction and the FID for the OST-object
1429 * has been assigned already, then handle it as handling MDT object via the
1430 * osp_md_create(). For other cases, the OSP will assign FID to the
1431 * object to be created, and update last_used Object ID (OID) file.
1433 * \param[in] env pointer to the thread context
1434 * \param[in] dt pointer to the OSP layer dt_object
1435 * \param[in] attr the attribute for the object to be created
1436 * \param[in] hint pointer to the hint for creating the object, such as
1438 * \param[in] dof pointer to the dt_object_format for help the creation
1439 * \param[in] th pointer to the transaction handler
1441 * \retval 0 for success
1442 * \retval negative error number on failure
1444 static int osp_create(const struct lu_env *env, struct dt_object *dt,
1445 struct lu_attr *attr, struct dt_allocation_hint *hint,
1446 struct dt_object_format *dof, struct thandle *th)
1448 struct osp_thread_info *osi = osp_env_info(env);
1449 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1450 struct osp_object *o = dt2osp_obj(dt);
1452 struct lu_fid *fid = &osi->osi_fid;
1453 struct thandle *local_th;
1454 struct lu_fid *last_fid = &d->opd_last_used_fid;
1457 if (is_only_remote_trans(th) &&
1458 !fid_is_zero(lu_object_fid(&dt->do_lu))) {
1459 LASSERT(fid_is_sane(lu_object_fid(&dt->do_lu)));
1461 rc = osp_md_create(env, dt, attr, hint, dof, th);
1463 o->opo_non_exist = 0;
1468 o->opo_non_exist = 0;
1469 if (o->opo_reserved) {
1470 /* regular case, fid is assigned holding transaction open */
1471 osp_object_assign_fid(env, d, o);
1474 memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
1476 LASSERTF(fid_is_sane(fid), "fid for osp_object %p is insane"DFID"!\n",
1479 if (!o->opo_reserved) {
1480 /* special case, id was assigned outside of transaction
1481 * see comments in osp_declare_attr_set */
1482 LASSERT(d->opd_pre != NULL);
1483 spin_lock(&d->opd_pre_lock);
1484 osp_update_last_fid(d, fid);
1485 spin_unlock(&d->opd_pre_lock);
1488 CDEBUG(D_INODE, "fid for osp_object %p is "DFID"\n", o, PFID(fid));
1490 /* If the precreate ends, it means it will be ready to rollover to
1491 * the new sequence soon, all the creation should be synchronized,
1492 * otherwise during replay, the replay fid will be inconsistent with
1493 * last_used/create fid */
1494 if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
1497 local_th = osp_get_storage_thandle(env, th, d);
1498 if (IS_ERR(local_th))
1499 RETURN(PTR_ERR(local_th));
1501 * it's OK if the import is inactive by this moment - id was created
1502 * by OST earlier, we just need to maintain it consistently on the disk
1503 * once import is reconnected, OSP will claim this and other objects
1504 * used and OST either keep them, if they exist or recreate
1507 /* we might have lost precreated objects */
1508 if (unlikely(d->opd_gap_count) > 0) {
1509 LASSERT(d->opd_pre != NULL);
1510 spin_lock(&d->opd_pre_lock);
1511 if (d->opd_gap_count > 0) {
1512 int count = d->opd_gap_count;
1514 rc = ostid_set_id(&osi->osi_oi,
1515 fid_oid(&d->opd_gap_start_fid));
1517 spin_unlock(&d->opd_pre_lock);
1520 d->opd_gap_count = 0;
1521 spin_unlock(&d->opd_pre_lock);
1523 CDEBUG(D_HA, "Writing gap "DFID"+%d in llog\n",
1524 PFID(&d->opd_gap_start_fid), count);
1525 /* real gap handling is disabled intil ORI-692 will be
1526 * fixed, now we only report gaps */
1528 spin_unlock(&d->opd_pre_lock);
1532 /* Only need update last_used oid file, seq file will only be update
1533 * during seq rollover */
1534 if (fid_is_idif((last_fid)))
1535 osi->osi_id = fid_idif_id(fid_seq(last_fid),
1536 fid_oid(last_fid), fid_ver(last_fid));
1538 osi->osi_id = fid_oid(last_fid);
1539 osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
1540 &osi->osi_id, d->opd_index);
1542 rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
1543 &osi->osi_off, local_th);
1545 CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
1546 d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
1552 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1554 * Declare that the caller will destroy the specified OST object.
1556 * The OST object destroy will be handled via llog asynchronously. This
1557 * function will declare the credits for generating MDS_UNLINK64_REC llog.
1559 * \param[in] env pointer to the thread context
1560 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1561 * \param[in] th pointer to the transaction handler
1563 * \retval 0 for success
1564 * \retval negative error number on failure
1566 int osp_declare_destroy(const struct lu_env *env, struct dt_object *dt,
1569 struct osp_object *o = dt2osp_obj(dt);
1570 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1575 LASSERT(!osp->opd_connect_mdt);
1576 rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
1582 * Implement OSP layer dt_object_operations::do_destroy() interface.
1584 * Destroy the specified OST object.
1586 * The OSP generates a MDS_UNLINK64_REC record in the llog. There
1587 * will be some dedicated thread to handle the llog asynchronously.
1589 * It also marks the object as non-cached.
1591 * \param[in] env pointer to the thread context
1592 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1593 * \param[in] th pointer to the transaction handler
1595 * \retval 0 for success
1596 * \retval negative error number on failure
1598 static int osp_destroy(const struct lu_env *env, struct dt_object *dt,
1601 struct osp_object *o = dt2osp_obj(dt);
1602 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1607 o->opo_non_exist = 1;
1609 LASSERT(!osp->opd_connect_mdt);
1610 /* once transaction is committed put proper command on
1611 * the queue going to our OST. */
1612 rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
1616 /* not needed in cache any more */
1617 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1622 static int osp_orphan_index_lookup(const struct lu_env *env,
1623 struct dt_object *dt,
1625 const struct dt_key *key)
1630 static int osp_orphan_index_declare_insert(const struct lu_env *env,
1631 struct dt_object *dt,
1632 const struct dt_rec *rec,
1633 const struct dt_key *key,
1634 struct thandle *handle)
1639 static int osp_orphan_index_insert(const struct lu_env *env,
1640 struct dt_object *dt,
1641 const struct dt_rec *rec,
1642 const struct dt_key *key,
1643 struct thandle *handle,
1649 static int osp_orphan_index_declare_delete(const struct lu_env *env,
1650 struct dt_object *dt,
1651 const struct dt_key *key,
1652 struct thandle *handle)
1657 static int osp_orphan_index_delete(const struct lu_env *env,
1658 struct dt_object *dt,
1659 const struct dt_key *key,
1660 struct thandle *handle)
1666 * Initialize the OSP layer index iteration.
1668 * \param[in] env pointer to the thread context
1669 * \param[in] dt pointer to the index object to be iterated
1670 * \param[in] attr unused
1672 * \retval pointer to the iteration structure
1673 * \retval negative error number on failure
1675 struct dt_it *osp_it_init(const struct lu_env *env, struct dt_object *dt,
1682 return ERR_PTR(-ENOMEM);
1684 it->ooi_pos_ent = -1;
1686 it->ooi_attr = attr;
1688 return (struct dt_it *)it;
1692 * Finalize the OSP layer index iteration.
1694 * \param[in] env pointer to the thread context
1695 * \param[in] di pointer to the iteration structure
1697 void osp_it_fini(const struct lu_env *env, struct dt_it *di)
1699 struct osp_it *it = (struct osp_it *)di;
1700 struct page **pages = it->ooi_pages;
1701 int npages = it->ooi_total_npages;
1704 if (pages != NULL) {
1705 for (i = 0; i < npages; i++) {
1706 if (pages[i] != NULL) {
1707 if (pages[i] == it->ooi_cur_page) {
1709 it->ooi_cur_page = NULL;
1711 __free_page(pages[i]);
1714 OBD_FREE(pages, npages * sizeof(*pages));
1720 * Get more records for the iteration from peer.
1722 * The new records will be filled in an array of pages. The OSP side
1723 * allows 1MB bulk data to be transferred.
1725 * \param[in] env pointer to the thread context
1726 * \param[in] it pointer to the iteration structure
1728 * \retval 0 for success
1729 * \retval negative error number on failure
1731 static int osp_it_fetch(const struct lu_env *env, struct osp_it *it)
1733 struct lu_device *dev = it->ooi_obj->do_lu.lo_dev;
1734 struct osp_device *osp = lu2osp_dev(dev);
1735 struct page **pages;
1736 struct ptlrpc_request *req = NULL;
1737 struct ptlrpc_bulk_desc *desc;
1738 struct idx_info *ii;
1745 npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
1746 npages /= PAGE_SIZE;
1748 OBD_ALLOC(pages, npages * sizeof(*pages));
1752 it->ooi_pages = pages;
1753 it->ooi_total_npages = npages;
1754 for (i = 0; i < npages; i++) {
1755 pages[i] = alloc_page(GFP_NOFS);
1756 if (pages[i] == NULL)
1760 req = ptlrpc_request_alloc(osp->opd_obd->u.cli.cl_import,
1765 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, OBD_IDX_READ);
1767 ptlrpc_request_free(req);
1771 osp_set_req_replay(osp, req);
1772 req->rq_request_portal = OUT_PORTAL;
1773 ii = req_capsule_client_get(&req->rq_pill, &RMF_IDX_INFO);
1774 memset(ii, 0, sizeof(*ii));
1775 if (fid_is_last_id(lu_object_fid(&it->ooi_obj->do_lu))) {
1776 /* LFSCK will iterate orphan object[FID_SEQ_LAYOUT_BTREE,
1777 * ost_index, 0] with LAST_ID FID, so it needs to replace
1778 * the FID with orphan FID here */
1779 ii->ii_fid.f_seq = FID_SEQ_LAYOUT_RBTREE;
1780 ii->ii_fid.f_oid = osp->opd_index;
1781 ii->ii_fid.f_ver = 0;
1782 ii->ii_flags = II_FL_NOHASH;
1783 ii->ii_attrs = osp_dev2node(osp);
1785 ii->ii_fid = *lu_object_fid(&it->ooi_obj->do_lu);
1786 ii->ii_flags = II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
1788 ii->ii_attrs = it->ooi_attr;
1790 ii->ii_magic = IDX_INFO_MAGIC;
1791 ii->ii_count = npages * LU_PAGE_COUNT;
1792 ii->ii_hash_start = it->ooi_next;
1794 ptlrpc_at_set_req_timeout(req);
1796 desc = ptlrpc_prep_bulk_imp(req, npages, 1,
1797 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1799 &ptlrpc_bulk_kiov_pin_ops);
1801 GOTO(out, rc = -ENOMEM);
1803 for (i = 0; i < npages; i++)
1804 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1807 ptlrpc_request_set_replen(req);
1808 rc = ptlrpc_queue_wait(req);
1812 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1813 req->rq_bulk->bd_nob_transferred);
1818 ii = req_capsule_server_get(&req->rq_pill, &RMF_IDX_INFO);
1819 if (ii->ii_magic != IDX_INFO_MAGIC)
1820 GOTO(out, rc = -EPROTO);
1822 npages = (ii->ii_count + LU_PAGE_COUNT - 1) >>
1823 (PAGE_SHIFT - LU_PAGE_SHIFT);
1824 if (npages > it->ooi_total_npages) {
1825 CERROR("%s: returned more pages than expected, %u > %u\n",
1826 osp->opd_obd->obd_name, npages, it->ooi_total_npages);
1827 GOTO(out, rc = -EINVAL);
1830 it->ooi_rec_size = ii->ii_recsize;
1831 it->ooi_valid_npages = npages;
1832 if (ptlrpc_rep_need_swab(req))
1835 it->ooi_next = ii->ii_hash_end;
1838 ptlrpc_req_finished(req);
1844 * Move the iteration cursor to the next lu_page.
1846 * One system page (PAGE_SIZE) may contain multiple lu_page (4KB),
1847 * that depends on the LU_PAGE_COUNT. If it is not the last lu_page
1848 * in current system page, then move the iteration cursor to the next
1849 * lu_page in current system page. Otherwise, if there are more system
1850 * pages in the cache, then move the iteration cursor to the next system
1851 * page. If all the cached records (pages) have been iterated, then fetch
1852 * more records via osp_it_fetch().
1854 * \param[in] env pointer to the thread context
1855 * \param[in] di pointer to the iteration structure
1857 * \retval positive for end of the directory
1858 * \retval 0 for success
1859 * \retval negative error number on failure
1861 int osp_it_next_page(const struct lu_env *env, struct dt_it *di)
1863 struct osp_it *it = (struct osp_it *)di;
1864 struct lu_idxpage *idxpage;
1865 struct page **pages;
1871 idxpage = it->ooi_cur_idxpage;
1872 if (idxpage != NULL) {
1873 if (idxpage->lip_nr == 0)
1876 if (it->ooi_pos_ent < idxpage->lip_nr) {
1877 CDEBUG(D_INFO, "ooi_pos %d nr %d\n",
1878 (int)it->ooi_pos_ent, (int)idxpage->lip_nr);
1881 it->ooi_cur_idxpage = NULL;
1882 it->ooi_pos_lu_page++;
1885 if (it->ooi_pos_lu_page < LU_PAGE_COUNT) {
1886 it->ooi_cur_idxpage = (void *)it->ooi_cur_page +
1887 LU_PAGE_SIZE * it->ooi_pos_lu_page;
1889 lustre_swab_lip_header(it->ooi_cur_idxpage);
1890 if (it->ooi_cur_idxpage->lip_magic != LIP_MAGIC) {
1891 struct osp_device *osp =
1892 lu2osp_dev(it->ooi_obj->do_lu.lo_dev);
1894 CERROR("%s: invalid magic (%x != %x) for page "
1895 "%d/%d while read layout orphan index\n",
1896 osp->opd_obd->obd_name,
1897 it->ooi_cur_idxpage->lip_magic,
1898 LIP_MAGIC, it->ooi_pos_page,
1899 it->ooi_pos_lu_page);
1900 /* Skip this lu_page next time. */
1901 it->ooi_pos_ent = idxpage->lip_nr - 1;
1904 it->ooi_pos_ent = -1;
1908 kunmap(it->ooi_cur_page);
1909 it->ooi_cur_page = NULL;
1913 pages = it->ooi_pages;
1914 if (it->ooi_pos_page < it->ooi_valid_npages) {
1915 it->ooi_cur_page = kmap(pages[it->ooi_pos_page]);
1916 it->ooi_pos_lu_page = 0;
1920 for (i = 0; i < it->ooi_total_npages; i++) {
1921 if (pages[i] != NULL)
1922 __free_page(pages[i]);
1924 OBD_FREE(pages, it->ooi_total_npages * sizeof(*pages));
1926 it->ooi_pos_page = 0;
1927 it->ooi_total_npages = 0;
1928 it->ooi_valid_npages = 0;
1931 it->ooi_cur_page = NULL;
1932 it->ooi_cur_idxpage = NULL;
1933 it->ooi_pages = NULL;
1936 if (it->ooi_next == II_END_OFF)
1939 rc = osp_it_fetch(env, it);
1947 * Move the iteration cursor to the next record.
1949 * If there are more records in the lu_page, then move the iteration
1950 * cursor to the next record directly. Otherwise, move the iteration
1951 * cursor to the record in the next lu_page via osp_it_next_page()
1953 * \param[in] env pointer to the thread context
1954 * \param[in] di pointer to the iteration structure
1956 * \retval positive for end of the directory
1957 * \retval 0 for success
1958 * \retval negative error number on failure
1960 static int osp_orphan_it_next(const struct lu_env *env, struct dt_it *di)
1962 struct osp_it *it = (struct osp_it *)di;
1963 struct lu_idxpage *idxpage;
1968 idxpage = it->ooi_cur_idxpage;
1969 if (idxpage != NULL) {
1970 if (idxpage->lip_nr == 0)
1974 if (it->ooi_pos_ent < idxpage->lip_nr) {
1975 if (it->ooi_rec_size ==
1976 sizeof(struct lu_orphan_rec_v2)) {
1978 (struct lu_orphan_ent_v2 *)idxpage->lip_entries+
1981 lustre_swab_orphan_ent_v2(it->ooi_ent);
1984 (struct lu_orphan_ent *)idxpage->lip_entries +
1987 lustre_swab_orphan_ent(it->ooi_ent);
1993 rc = osp_it_next_page(env, di);
2000 int osp_it_get(const struct lu_env *env, struct dt_it *di,
2001 const struct dt_key *key)
2006 void osp_it_put(const struct lu_env *env, struct dt_it *di)
2010 static struct dt_key *osp_orphan_it_key(const struct lu_env *env,
2011 const struct dt_it *di)
2013 struct osp_it *it = (struct osp_it *)di;
2014 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
2016 if (likely(ent != NULL))
2017 return (struct dt_key *)(&ent->loe_key);
2022 static int osp_orphan_it_key_size(const struct lu_env *env,
2023 const struct dt_it *di)
2025 return sizeof(struct lu_fid);
2028 static int osp_orphan_it_rec(const struct lu_env *env, const struct dt_it *di,
2029 struct dt_rec *rec, __u32 attr)
2031 struct osp_it *it = (struct osp_it *)di;
2033 if (likely(it->ooi_ent)) {
2034 if (it->ooi_rec_size == sizeof(struct lu_orphan_rec_v2)) {
2035 struct lu_orphan_ent_v2 *ent =
2036 (struct lu_orphan_ent_v2 *)it->ooi_ent;
2038 *(struct lu_orphan_rec_v2 *)rec = ent->loe_rec;
2040 struct lu_orphan_ent *ent =
2041 (struct lu_orphan_ent *)it->ooi_ent;
2043 *(struct lu_orphan_rec *)rec = ent->loe_rec;
2051 __u64 osp_it_store(const struct lu_env *env, const struct dt_it *di)
2053 struct osp_it *it = (struct osp_it *)di;
2055 return it->ooi_next;
2059 * Locate the iteration cursor to the specified position (cookie).
2061 * \param[in] env pointer to the thread context
2062 * \param[in] di pointer to the iteration structure
2063 * \param[in] hash the specified position
2065 * \retval positive number for locating to the exactly position
2067 * \retval 0 for arriving at the end of the iteration
2068 * \retval negative error number on failure
2070 int osp_orphan_it_load(const struct lu_env *env, const struct dt_it *di,
2073 struct osp_it *it = (struct osp_it *)di;
2076 it->ooi_next = hash;
2077 rc = osp_orphan_it_next(env, (struct dt_it *)di);
2087 int osp_it_key_rec(const struct lu_env *env, const struct dt_it *di,
2093 static const struct dt_index_operations osp_orphan_index_ops = {
2094 .dio_lookup = osp_orphan_index_lookup,
2095 .dio_declare_insert = osp_orphan_index_declare_insert,
2096 .dio_insert = osp_orphan_index_insert,
2097 .dio_declare_delete = osp_orphan_index_declare_delete,
2098 .dio_delete = osp_orphan_index_delete,
2100 .init = osp_it_init,
2101 .fini = osp_it_fini,
2102 .next = osp_orphan_it_next,
2105 .key = osp_orphan_it_key,
2106 .key_size = osp_orphan_it_key_size,
2107 .rec = osp_orphan_it_rec,
2108 .store = osp_it_store,
2109 .load = osp_orphan_it_load,
2110 .key_rec = osp_it_key_rec,
2115 * Implement OSP layer dt_object_operations::do_index_try() interface.
2117 * Negotiate the index type.
2119 * If the target index is an IDIF object, then use osp_orphan_index_ops.
2120 * Otherwise, assign osp_md_index_ops to the dt_object::do_index_ops.
2121 * (\see lustre/include/lustre_fid.h for IDIF.)
2123 * \param[in] env pointer to the thread context
2124 * \param[in] dt pointer to the OSP layer dt_object
2125 * \param[in] feat unused
2127 * \retval 0 for success
2129 static int osp_index_try(const struct lu_env *env,
2130 struct dt_object *dt,
2131 const struct dt_index_features *feat)
2133 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2135 if (fid_is_last_id(fid) && fid_is_idif(fid))
2136 dt->do_index_ops = &osp_orphan_index_ops;
2138 dt->do_index_ops = &osp_md_index_ops;
2142 static struct dt_object_operations osp_obj_ops = {
2143 .do_declare_attr_get = osp_declare_attr_get,
2144 .do_attr_get = osp_attr_get,
2145 .do_declare_attr_set = osp_declare_attr_set,
2146 .do_attr_set = osp_attr_set,
2147 .do_declare_xattr_get = osp_declare_xattr_get,
2148 .do_xattr_get = osp_xattr_get,
2149 .do_declare_xattr_set = osp_declare_xattr_set,
2150 .do_xattr_set = osp_xattr_set,
2151 .do_declare_create = osp_declare_create,
2152 .do_create = osp_create,
2153 .do_declare_destroy = osp_declare_destroy,
2154 .do_destroy = osp_destroy,
2155 .do_index_try = osp_index_try,
2159 * Implement OSP layer lu_object_operations::loo_object_init() interface.
2161 * Initialize the object.
2163 * If it is a remote MDT object, then call do_attr_get() to fetch
2164 * the attribute from the peer.
2166 * \param[in] env pointer to the thread context
2167 * \param[in] o pointer to the OSP layer lu_object
2168 * \param[in] conf unused
2170 * \retval 0 for success
2171 * \retval negative error number on failure
2173 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
2174 const struct lu_object_conf *conf)
2176 struct osp_object *po = lu2osp_obj(o);
2180 spin_lock_init(&po->opo_lock);
2181 o->lo_header->loh_attr |= LOHA_REMOTE;
2182 INIT_LIST_HEAD(&po->opo_xattr_list);
2183 INIT_LIST_HEAD(&po->opo_invalidate_cb_list);
2185 if (is_ost_obj(o)) {
2186 po->opo_obj.do_ops = &osp_obj_ops;
2188 struct lu_attr *la = &osp_env_info(env)->osi_attr;
2190 po->opo_obj.do_ops = &osp_md_obj_ops;
2191 po->opo_obj.do_body_ops = &osp_md_body_ops;
2193 if (conf != NULL && conf->loc_flags & LOC_F_NEW) {
2194 po->opo_non_exist = 1;
2196 rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o),
2199 o->lo_header->loh_attr |=
2200 LOHA_EXISTS | (la->la_mode & S_IFMT);
2201 if (rc == -ENOENT) {
2202 po->opo_non_exist = 1;
2206 init_rwsem(&po->opo_sem);
2212 * Implement OSP layer lu_object_operations::loo_object_free() interface.
2214 * Finalize the object.
2216 * If the OSP object has attributes cache, then destroy the cache.
2217 * Free the object finally.
2219 * \param[in] env pointer to the thread context
2220 * \param[in] o pointer to the OSP layer lu_object
2222 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
2224 struct osp_object *obj = lu2osp_obj(o);
2225 struct lu_object_header *h = o->lo_header;
2226 struct osp_xattr_entry *oxe;
2227 struct osp_xattr_entry *tmp;
2230 dt_object_fini(&obj->opo_obj);
2231 lu_object_header_fini(h);
2232 list_for_each_entry_safe(oxe, tmp, &obj->opo_xattr_list, oxe_list) {
2233 list_del(&oxe->oxe_list);
2234 count = atomic_read(&oxe->oxe_ref);
2235 LASSERTF(count == 1,
2236 "Still has %d users on the xattr entry %.*s\n",
2237 count-1, (int)oxe->oxe_namelen, oxe->oxe_buf);
2239 OBD_FREE(oxe, oxe->oxe_buflen);
2241 OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
2245 * Implement OSP layer lu_object_operations::loo_object_release() interface.
2247 * Cleanup (not free) the object.
2249 * If it is a reserved object but failed to be created, or it is an OST
2250 * object, then mark the object as non-cached.
2252 * \param[in] env pointer to the thread context
2253 * \param[in] o pointer to the OSP layer lu_object
2255 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
2257 struct osp_object *po = lu2osp_obj(o);
2258 struct osp_device *d = lu2osp_dev(o->lo_dev);
2263 * release reservation if object was declared but not created
2264 * this may require lu_object_put() in LOD
2266 if (unlikely(po->opo_reserved)) {
2267 LASSERT(d->opd_pre != NULL);
2268 LASSERT(d->opd_pre_reserved > 0);
2269 spin_lock(&d->opd_pre_lock);
2270 d->opd_pre_reserved--;
2271 spin_unlock(&d->opd_pre_lock);
2273 /* not needed in cache any more */
2274 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2278 /* XXX: Currently, NOT cache OST-object on MDT because:
2279 * 1. it is not often accessed on MDT.
2280 * 2. avoid up layer (such as LFSCK) to load too many
2281 * once-used OST-objects. */
2282 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2287 static int osp_object_print(const struct lu_env *env, void *cookie,
2288 lu_printer_t p, const struct lu_object *l)
2290 const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
2292 return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
2295 static int osp_object_invariant(const struct lu_object *o)
2300 struct lu_object_operations osp_lu_obj_ops = {
2301 .loo_object_init = osp_object_init,
2302 .loo_object_free = osp_object_free,
2303 .loo_object_release = osp_object_release,
2304 .loo_object_print = osp_object_print,
2305 .loo_object_invariant = osp_object_invariant