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, 2015, 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 bool is_ost_obj(struct lu_object *lo)
131 return !lu2osp_dev(lo->lo_dev)->opd_connect_mdt;
135 * Assign FID to the OST object.
137 * This function will assign the FID to the OST object of a striped file.
139 * \param[in] env pointer to the thread context
140 * \param[in] d pointer to the OSP device
141 * \param[in] o pointer to the OSP object that the FID will be
144 static void osp_object_assign_fid(const struct lu_env *env,
145 struct osp_device *d, struct osp_object *o)
147 struct osp_thread_info *osi = osp_env_info(env);
149 LASSERT(fid_is_zero(lu_object_fid(&o->opo_obj.do_lu)));
150 LASSERT(o->opo_reserved);
153 osp_precreate_get_fid(env, d, &osi->osi_fid);
155 lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
159 * Find the named extended attribute in the OSP object attributes cache.
161 * The caller should take the osp_object::opo_lock before calling
164 * \param[in] obj pointer to the OSP object
165 * \param[in] name the name of the extended attribute
166 * \param[in] namelen the name length of the extended attribute
168 * \retval pointer to the found extended attribute entry
169 * \retval NULL if the specified extended attribute is not
172 static struct osp_xattr_entry *
173 osp_oac_xattr_find_locked(struct osp_object *obj, const char *name,
176 struct osp_xattr_entry *oxe;
178 list_for_each_entry(oxe, &obj->opo_xattr_list, oxe_list) {
179 if (namelen == oxe->oxe_namelen &&
180 strncmp(name, oxe->oxe_buf, namelen) == 0)
188 * Find the named extended attribute in the OSP object attributes cache.
190 * Call osp_oac_xattr_find_locked() with the osp_object::opo_lock held.
192 * \param[in] obj pointer to the OSP object
193 * \param[in] name the name of the extended attribute
194 * \param[in] unlink true if the extended attribute entry is to be removed
197 * \retval pointer to the found extended attribute entry
198 * \retval NULL if the specified extended attribute is not
201 static struct osp_xattr_entry *osp_oac_xattr_find(struct osp_object *obj,
202 const char *name, bool unlink)
204 struct osp_xattr_entry *oxe = NULL;
206 spin_lock(&obj->opo_lock);
207 oxe = osp_oac_xattr_find_locked(obj, name, strlen(name));
210 list_del_init(&oxe->oxe_list);
212 atomic_inc(&oxe->oxe_ref);
214 spin_unlock(&obj->opo_lock);
220 * Find the named extended attribute in the OSP object attributes cache.
222 * If it is not in the cache, then add an empty entry (that will be
223 * filled later) to cache with the given name.
225 * \param[in] obj pointer to the OSP object
226 * \param[in] name the name of the extended attribute
227 * \param[in] len the length of the extended attribute value
229 * \retval pointer to the found or new-created extended
231 * \retval NULL if the specified extended attribute is not in the
232 * cache or fail to add new empty entry to the cache.
234 static struct osp_xattr_entry *
235 osp_oac_xattr_find_or_add(struct osp_object *obj, const char *name, size_t len)
237 struct osp_xattr_entry *oxe;
238 struct osp_xattr_entry *tmp = NULL;
239 size_t namelen = strlen(name);
240 size_t size = sizeof(*oxe) + namelen + 1 + len;
242 oxe = osp_oac_xattr_find(obj, name, false);
246 OBD_ALLOC(oxe, size);
247 if (unlikely(oxe == NULL))
250 INIT_LIST_HEAD(&oxe->oxe_list);
251 oxe->oxe_buflen = size;
252 oxe->oxe_namelen = namelen;
253 memcpy(oxe->oxe_buf, name, namelen);
254 oxe->oxe_value = oxe->oxe_buf + namelen + 1;
255 /* One ref is for the caller, the other is for the entry on the list. */
256 atomic_set(&oxe->oxe_ref, 2);
258 spin_lock(&obj->opo_lock);
259 tmp = osp_oac_xattr_find_locked(obj, name, namelen);
261 list_add_tail(&oxe->oxe_list, &obj->opo_xattr_list);
263 atomic_inc(&tmp->oxe_ref);
264 spin_unlock(&obj->opo_lock);
275 * Add the given extended attribute to the OSP object attributes cache.
277 * If there is an old extended attributed entry with the same name,
278 * remove it from the cache and return it via the parameter \a poxe.
280 * \param[in] obj pointer to the OSP object
281 * \param[in,out] poxe double pointer to the OSP object extended attribute
282 * entry: the new extended attribute entry is transferred
283 * via such pointer target, and if old the extended
284 * attribute entry exists, then it will be returned back
285 * via such pointer target.
286 * \param[in] len the length of the (new) extended attribute value
288 * \retval pointer to the new extended attribute entry
289 * \retval NULL for failure cases.
291 static struct osp_xattr_entry *
292 osp_oac_xattr_replace(struct osp_object *obj,
293 struct osp_xattr_entry **poxe, size_t len)
295 struct osp_xattr_entry *oxe;
296 size_t namelen = (*poxe)->oxe_namelen;
297 size_t size = sizeof(*oxe) + namelen + 1 + len;
299 OBD_ALLOC(oxe, size);
300 if (unlikely(oxe == NULL))
303 INIT_LIST_HEAD(&oxe->oxe_list);
304 oxe->oxe_buflen = size;
305 oxe->oxe_namelen = namelen;
306 memcpy(oxe->oxe_buf, (*poxe)->oxe_buf, namelen);
307 oxe->oxe_value = oxe->oxe_buf + namelen + 1;
308 /* One ref is for the caller, the other is for the entry on the list. */
309 atomic_set(&oxe->oxe_ref, 2);
311 spin_lock(&obj->opo_lock);
312 *poxe = osp_oac_xattr_find_locked(obj, oxe->oxe_buf, namelen);
313 LASSERT(*poxe != NULL);
315 list_del_init(&(*poxe)->oxe_list);
316 list_add_tail(&oxe->oxe_list, &obj->opo_xattr_list);
317 spin_unlock(&obj->opo_lock);
323 * Release reference from the OSP object extended attribute entry.
325 * If it is the last reference, then free the entry.
327 * \param[in] oxe pointer to the OSP object extended attribute entry.
329 static inline void osp_oac_xattr_put(struct osp_xattr_entry *oxe)
331 if (atomic_dec_and_test(&oxe->oxe_ref)) {
332 LASSERT(list_empty(&oxe->oxe_list));
334 OBD_FREE(oxe, oxe->oxe_buflen);
339 * Parse the OSP object attribute from the RPC reply.
341 * If the attribute is valid, then it will be added to the OSP object
344 * \param[in] env pointer to the thread context
345 * \param[in] reply pointer to the RPC reply
346 * \param[in] req pointer to the RPC request
347 * \param[out] attr pointer to buffer to hold the output attribute
348 * \param[in] obj pointer to the OSP object
349 * \param[in] index the index of the attribute buffer in the reply
351 * \retval 0 for success
352 * \retval negative error number on failure
354 static int osp_get_attr_from_reply(const struct lu_env *env,
355 struct object_update_reply *reply,
356 struct ptlrpc_request *req,
357 struct lu_attr *attr,
358 struct osp_object *obj, int index)
360 struct osp_thread_info *osi = osp_env_info(env);
361 struct lu_buf *rbuf = &osi->osi_lb2;
362 struct obdo *lobdo = &osi->osi_obdo;
366 rc = object_update_result_data_get(reply, rbuf, index);
370 wobdo = rbuf->lb_buf;
371 if (rbuf->lb_len != sizeof(*wobdo))
374 LASSERT(req != NULL);
375 if (ptlrpc_req_need_swab(req))
376 lustre_swab_obdo(wobdo);
378 lustre_get_wire_obdo(NULL, lobdo, wobdo);
379 spin_lock(&obj->opo_lock);
380 la_from_obdo(&obj->opo_attr, lobdo, lobdo->o_valid);
382 *attr = obj->opo_attr;
383 spin_unlock(&obj->opo_lock);
389 * Interpreter function for getting OSP object attribute asynchronously.
391 * Called to interpret the result of an async mode RPC for getting the
392 * OSP object attribute.
394 * \param[in] env pointer to the thread context
395 * \param[in] reply pointer to the RPC reply
396 * \param[in] req pointer to the RPC request
397 * \param[in] obj pointer to the OSP object
398 * \param[out] data pointer to buffer to hold the output attribute
399 * \param[in] index the index of the attribute buffer in the reply
400 * \param[in] rc the result for handling the RPC
402 * \retval 0 for success
403 * \retval negative error number on failure
405 static int osp_attr_get_interpterer(const struct lu_env *env,
406 struct object_update_reply *reply,
407 struct ptlrpc_request *req,
408 struct osp_object *obj,
409 void *data, int index, int rc)
411 struct lu_attr *attr = data;
414 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
415 obj->opo_non_exist = 0;
417 return osp_get_attr_from_reply(env, reply, req, NULL, obj,
421 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
422 obj->opo_non_exist = 1;
425 spin_lock(&obj->opo_lock);
427 spin_unlock(&obj->opo_lock);
434 * Implement OSP layer dt_object_operations::do_declare_attr_get() interface.
436 * Declare that the caller will get attribute from the specified OST object.
438 * This function adds an Object Unified Target (OUT) sub-request to the per-OSP
439 * based shared asynchronous request queue. The osp_attr_get_interpterer()
440 * is registered as the interpreter function to handle the result of this
443 * \param[in] env pointer to the thread context
444 * \param[in] dt pointer to the OSP layer dt_object
446 * \retval 0 for success
447 * \retval negative error number on failure
449 static int osp_declare_attr_get(const struct lu_env *env, struct dt_object *dt)
451 struct osp_object *obj = dt2osp_obj(dt);
452 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
455 mutex_lock(&osp->opd_async_requests_mutex);
456 rc = osp_insert_async_request(env, OUT_ATTR_GET, obj, 0, NULL, NULL,
457 &obj->opo_attr, sizeof(struct obdo),
458 osp_attr_get_interpterer);
459 mutex_unlock(&osp->opd_async_requests_mutex);
465 * Implement OSP layer dt_object_operations::do_attr_get() interface.
467 * Get attribute from the specified MDT/OST object.
469 * If the attribute is in the OSP object attributes cache, then return
470 * the cached attribute directly. Otherwise it will trigger an OUT RPC
471 * to the peer to get the attribute synchronously, if successful, add it
472 * to the OSP attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
474 * \param[in] env pointer to the thread context
475 * \param[in] dt pointer to the OSP layer dt_object
476 * \param[out] attr pointer to the buffer to hold the output attribute
478 * \retval 0 for success
479 * \retval negative error number on failure
481 int osp_attr_get(const struct lu_env *env, struct dt_object *dt,
482 struct lu_attr *attr)
484 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
485 struct osp_object *obj = dt2osp_obj(dt);
486 struct dt_device *dev = &osp->opd_dt_dev;
487 struct osp_update_request *update;
488 struct object_update_reply *reply;
489 struct ptlrpc_request *req = NULL;
493 if (is_ost_obj(&dt->do_lu) && obj->opo_non_exist)
496 spin_lock(&obj->opo_lock);
497 if (obj->opo_attr.la_valid != 0 && !obj->opo_stale) {
498 *attr = obj->opo_attr;
499 spin_unlock(&obj->opo_lock);
503 spin_unlock(&obj->opo_lock);
505 update = osp_update_request_create(dev);
507 RETURN(PTR_ERR(update));
509 rc = osp_update_rpc_pack(env, attr_get, update, OUT_ATTR_GET,
510 lu_object_fid(&dt->do_lu));
512 CERROR("%s: Insert update error "DFID": rc = %d\n",
513 dev->dd_lu_dev.ld_obd->obd_name,
514 PFID(lu_object_fid(&dt->do_lu)), rc);
519 rc = osp_remote_sync(env, osp, update, &req);
522 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
523 obj->opo_non_exist = 1;
525 CERROR("%s:osp_attr_get update error "DFID": rc = %d\n",
526 dev->dd_lu_dev.ld_obd->obd_name,
527 PFID(lu_object_fid(&dt->do_lu)), rc);
533 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
534 obj->opo_non_exist = 0;
535 reply = req_capsule_server_sized_get(&req->rq_pill,
536 &RMF_OUT_UPDATE_REPLY,
537 OUT_UPDATE_REPLY_SIZE);
538 if (reply == NULL || reply->ourp_magic != UPDATE_REPLY_MAGIC)
539 GOTO(out, rc = -EPROTO);
541 rc = osp_get_attr_from_reply(env, reply, req, attr, obj, 0);
545 spin_lock(&obj->opo_lock);
547 spin_unlock(&obj->opo_lock);
553 ptlrpc_req_finished(req);
555 osp_update_request_destroy(env, update);
561 * Implement OSP layer dt_object_operations::do_declare_attr_set() interface.
563 * If the transaction is not remote one, then declare the credits that will
564 * be used for the subsequent llog record for the object's attributes.
566 * \param[in] env pointer to the thread context
567 * \param[in] dt pointer to the OSP layer dt_object
568 * \param[in] attr pointer to the attribute to be set
569 * \param[in] th pointer to the transaction handler
571 * \retval 0 for success
572 * \retval negative error number on failure
574 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
575 const struct lu_attr *attr, struct thandle *th)
577 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
578 struct osp_object *o = dt2osp_obj(dt);
581 if (is_only_remote_trans(th))
582 return osp_md_declare_attr_set(env, dt, attr, th);
584 * Usually we don't allow server stack to manipulate size
585 * but there is a special case when striping is created
586 * late, after stripeless file got truncated to non-zero.
588 * In this case we do the following:
590 * 1) grab id in declare - this can lead to leaked OST objects
591 * but we don't currently have proper mechanism and the only
592 * options we have are to do truncate RPC holding transaction
593 * open (very bad) or to grab id in declare at cost of leaked
594 * OST object in same very rare unfortunate case (just bad)
595 * notice 1.6-2.0 do assignment outside of running transaction
596 * all the time, meaning many more chances for leaked objects.
598 * 2) send synchronous truncate RPC with just assigned id
601 /* there are few places in MDD code still passing NULL
602 * XXX: to be fixed soon */
606 if (attr->la_valid & LA_SIZE && attr->la_size > 0 &&
607 fid_is_zero(lu_object_fid(&o->opo_obj.do_lu))) {
608 LASSERT(!dt_object_exists(dt));
609 osp_object_assign_fid(env, d, o);
610 rc = osp_object_truncate(env, dt, attr->la_size);
615 if (!(attr->la_valid & (LA_UID | LA_GID)))
618 /* track all UID/GID changes via llog */
619 rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
625 * Implement OSP layer dt_object_operations::do_attr_set() interface.
627 * Set attribute to the specified OST object.
629 * If the transaction is a remote one, then add OUT_ATTR_SET sub-request
630 * in the OUT RPC that will be flushed when the remote transaction stop.
631 * Otherwise, it will generate a MDS_SETATTR64_REC record in the llog that
632 * will be handled by a dedicated thread asynchronously.
634 * If the attribute entry exists in the OSP object attributes cache,
635 * then update the cached attribute according to given attribute.
637 * \param[in] env pointer to the thread context
638 * \param[in] dt pointer to the OSP layer dt_object
639 * \param[in] attr pointer to the attribute to be set
640 * \param[in] th pointer to the transaction handler
642 * \retval 0 for success
643 * \retval negative error number on failure
645 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
646 const struct lu_attr *attr, struct thandle *th)
648 struct osp_object *o = dt2osp_obj(dt);
652 /* we're interested in uid/gid changes only */
653 if (!(attr->la_valid & (LA_UID | LA_GID)))
656 if (!is_only_remote_trans(th)) {
657 rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
658 /* XXX: send new uid/gid to OST ASAP? */
662 /* It is for OST-object attr_set directly without updating
663 * local MDT-object attribute. It is usually used by LFSCK. */
664 rc = osp_md_attr_set(env, dt, attr, th);
665 CDEBUG(D_INFO, "(1) set attr "DFID": rc = %d\n",
666 PFID(&dt->do_lu.lo_header->loh_fid), rc);
671 /* Update the OSP object attributes cache. */
673 spin_lock(&o->opo_lock);
674 if (attr->la_valid & LA_UID) {
675 la->la_uid = attr->la_uid;
676 la->la_valid |= LA_UID;
679 if (attr->la_valid & LA_GID) {
680 la->la_gid = attr->la_gid;
681 la->la_valid |= LA_GID;
683 spin_unlock(&o->opo_lock);
690 * Interpreter function for getting OSP object extended attribute asynchronously
692 * Called to interpret the result of an async mode RPC for getting the
693 * OSP object extended attribute.
695 * \param[in] env pointer to the thread context
696 * \param[in] reply pointer to the RPC reply
697 * \param[in] req pointer to the RPC request
698 * \param[in] obj pointer to the OSP object
699 * \param[out] data pointer to OSP object attributes cache
700 * \param[in] index the index of the attribute buffer in the reply
701 * \param[in] rc the result for handling the RPC
703 * \retval 0 for success
704 * \retval negative error number on failure
706 static int osp_xattr_get_interpterer(const struct lu_env *env,
707 struct object_update_reply *reply,
708 struct ptlrpc_request *req,
709 struct osp_object *obj,
710 void *data, int index, int rc)
712 struct osp_xattr_entry *oxe = data;
713 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
716 size_t len = sizeof(*oxe) + oxe->oxe_namelen + 1;
718 rc = object_update_result_data_get(reply, rbuf, index);
719 if (rc < 0 || rbuf->lb_len > (oxe->oxe_buflen - len)) {
720 spin_lock(&obj->opo_lock);
722 spin_unlock(&obj->opo_lock);
723 osp_oac_xattr_put(oxe);
725 return rc < 0 ? rc : -ERANGE;
728 spin_lock(&obj->opo_lock);
729 oxe->oxe_vallen = rbuf->lb_len;
730 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
733 spin_unlock(&obj->opo_lock);
734 } else if (rc == -ENOENT || rc == -ENODATA) {
735 spin_lock(&obj->opo_lock);
738 spin_unlock(&obj->opo_lock);
740 spin_lock(&obj->opo_lock);
742 spin_unlock(&obj->opo_lock);
745 osp_oac_xattr_put(oxe);
751 * Implement OSP dt_object_operations::do_declare_xattr_get() interface.
753 * Declare that the caller will get extended attribute from the specified
756 * This function will add an OUT_XATTR_GET sub-request to the per OSP
757 * based shared asynchronous request queue with the interpreter function:
758 * osp_xattr_get_interpterer().
760 * \param[in] env pointer to the thread context
761 * \param[in] dt pointer to the OSP layer dt_object
762 * \param[out] buf pointer to the lu_buf to hold the extended attribute
763 * \param[in] name the name for the expected extended attribute
765 * \retval 0 for success
766 * \retval negative error number on failure
768 static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
769 struct lu_buf *buf, const char *name)
771 struct osp_object *obj = dt2osp_obj(dt);
772 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
773 struct osp_xattr_entry *oxe;
774 __u16 namelen = strlen(name);
777 LASSERT(buf != NULL);
778 LASSERT(name != NULL);
780 /* If only for xattr size, return directly. */
781 if (unlikely(buf->lb_len == 0))
784 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
788 mutex_lock(&osp->opd_async_requests_mutex);
789 rc = osp_insert_async_request(env, OUT_XATTR_GET, obj, 1,
790 &namelen, (const void **)&name,
792 osp_xattr_get_interpterer);
794 mutex_unlock(&osp->opd_async_requests_mutex);
795 osp_oac_xattr_put(oxe);
797 struct osp_update_request *our;
798 struct osp_update_request_sub *ours;
800 /* XXX: Currently, we trigger the batched async OUT
801 * RPC via dt_declare_xattr_get(). It is not
802 * perfect solution, but works well now.
804 * We will improve it in the future. */
805 our = osp->opd_async_requests;
806 ours = osp_current_object_update_request(our);
807 if (ours != NULL && ours->ours_req != NULL &&
808 ours->ours_req->ourq_count > 0) {
809 osp->opd_async_requests = NULL;
810 mutex_unlock(&osp->opd_async_requests_mutex);
811 rc = osp_unplug_async_request(env, osp, our);
813 mutex_unlock(&osp->opd_async_requests_mutex);
821 * Implement OSP layer dt_object_operations::do_xattr_get() interface.
823 * Get extended attribute from the specified MDT/OST object.
825 * If the extended attribute is in the OSP object attributes cache, then
826 * return the cached extended attribute directly. Otherwise it will get
827 * the extended attribute synchronously, if successful, add it to the OSP
828 * attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
830 * There is a race condition: some other thread has added the named extended
831 * attributed entry to the OSP object attributes cache during the current
832 * OUT_XATTR_GET handling. If such case happens, the OSP will replace the
833 * (just) existing extended attribute entry with the new replied one.
835 * \param[in] env pointer to the thread context
836 * \param[in] dt pointer to the OSP layer dt_object
837 * \param[out] buf pointer to the lu_buf to hold the extended attribute
838 * \param[in] name the name for the expected extended attribute
840 * \retval 0 for success
841 * \retval negative error number on failure
843 int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
844 struct lu_buf *buf, const char *name)
846 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
847 struct osp_object *obj = dt2osp_obj(dt);
848 struct dt_device *dev = &osp->opd_dt_dev;
849 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
850 struct osp_update_request *update = NULL;
851 struct ptlrpc_request *req = NULL;
852 struct object_update_reply *reply;
853 struct osp_xattr_entry *oxe = NULL;
854 const char *dname = dt->do_lu.lo_dev->ld_obd->obd_name;
858 LASSERT(buf != NULL);
859 LASSERT(name != NULL);
861 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NETWORK) &&
862 osp->opd_index == cfs_fail_val) {
863 if (is_ost_obj(&dt->do_lu)) {
864 if (osp_dev2node(osp) == cfs_fail_val)
867 if (strcmp(name, XATTR_NAME_LINK) == 0)
872 if (unlikely(obj->opo_non_exist))
875 oxe = osp_oac_xattr_find(obj, name, false);
877 spin_lock(&obj->opo_lock);
878 if (oxe->oxe_ready) {
880 GOTO(unlock, rc = -ENODATA);
882 if (buf->lb_buf == NULL)
883 GOTO(unlock, rc = oxe->oxe_vallen);
885 if (buf->lb_len < oxe->oxe_vallen)
886 GOTO(unlock, rc = -ERANGE);
888 memcpy(buf->lb_buf, oxe->oxe_value,
891 GOTO(unlock, rc = oxe->oxe_vallen);
894 spin_unlock(&obj->opo_lock);
895 osp_oac_xattr_put(oxe);
899 spin_unlock(&obj->opo_lock);
901 update = osp_update_request_create(dev);
903 GOTO(out, rc = PTR_ERR(update));
905 rc = osp_update_rpc_pack(env, xattr_get, update, OUT_XATTR_GET,
906 lu_object_fid(&dt->do_lu), name, buf->lb_len);
908 CERROR("%s: Insert update error "DFID": rc = %d\n",
909 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
913 rc = osp_remote_sync(env, osp, update, &req);
916 dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
917 obj->opo_non_exist = 1;
921 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
924 CWARN("%s: Fail to add xattr (%s) to cache for "
925 DFID" (1): rc = %d\n", dname, name,
926 PFID(lu_object_fid(&dt->do_lu)), rc);
931 spin_lock(&obj->opo_lock);
932 if (rc == -ENOENT || rc == -ENODATA) {
938 spin_unlock(&obj->opo_lock);
943 reply = req_capsule_server_sized_get(&req->rq_pill,
944 &RMF_OUT_UPDATE_REPLY,
945 OUT_UPDATE_REPLY_SIZE);
946 if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
947 CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
948 dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
949 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
951 GOTO(out, rc = -EPROTO);
954 rc = object_update_result_data_get(reply, rbuf, 0);
958 if (buf->lb_buf == NULL)
961 if (unlikely(buf->lb_len < rbuf->lb_len))
962 GOTO(out, rc = -ERANGE);
964 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
967 oxe = osp_oac_xattr_find_or_add(obj, name, rbuf->lb_len);
969 CWARN("%s: Fail to add xattr (%s) to "
970 "cache for "DFID" (2): rc = %d\n",
971 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
977 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < rbuf->lb_len) {
978 struct osp_xattr_entry *old = oxe;
979 struct osp_xattr_entry *tmp;
981 tmp = osp_oac_xattr_replace(obj, &old, rbuf->lb_len);
982 osp_oac_xattr_put(oxe);
985 CWARN("%s: Fail to update xattr (%s) to "
986 "cache for "DFID": rc = %d\n",
987 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
988 spin_lock(&obj->opo_lock);
990 spin_unlock(&obj->opo_lock);
995 /* Drop the ref for entry on list. */
996 osp_oac_xattr_put(old);
999 spin_lock(&obj->opo_lock);
1000 oxe->oxe_vallen = rbuf->lb_len;
1001 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
1004 spin_unlock(&obj->opo_lock);
1010 ptlrpc_req_finished(req);
1012 if (update != NULL && !IS_ERR(update))
1013 osp_update_request_destroy(env, update);
1016 osp_oac_xattr_put(oxe);
1022 * Implement OSP layer dt_object_operations::do_declare_xattr_set() interface.
1024 * Declare that the caller will set extended attribute to the specified
1027 * If it is non-remote transaction, it will add an OUT_XATTR_SET sub-request
1028 * to the OUT RPC that will be flushed when the transaction start. And if the
1029 * OSP attributes cache is initialized, then check whether the name extended
1030 * attribute entry exists in the cache or not. If yes, replace it; otherwise,
1031 * add the extended attribute to the cache.
1033 * \param[in] env pointer to the thread context
1034 * \param[in] dt pointer to the OSP layer dt_object
1035 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1036 * \param[in] name the name of the extended attribute to be set
1037 * \param[in] flag to indicate the detailed set operation: LU_XATTR_CREATE
1038 * or LU_XATTR_REPLACE or others
1039 * \param[in] th pointer to the transaction handler
1041 * \retval 0 for success
1042 * \retval negative error number on failure
1044 int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
1045 const struct lu_buf *buf, const char *name,
1046 int flag, struct thandle *th)
1048 return osp_trans_update_request_create(th);
1052 * Implement OSP layer dt_object_operations::do_xattr_set() interface.
1054 * Set extended attribute to the specified MDT/OST object.
1056 * Add an OUT_XATTR_SET sub-request into the OUT RPC that will be flushed in
1057 * the transaction stop. And if the OSP attributes cache is initialized, then
1058 * check whether the name extended attribute entry exists in the cache or not.
1059 * If yes, replace it; otherwise, add the extended attribute to the cache.
1061 * \param[in] env pointer to the thread context
1062 * \param[in] dt pointer to the OSP layer dt_object
1063 * \param[in] buf pointer to the lu_buf to hold the extended attribute
1064 * \param[in] name the name of the extended attribute to be set
1065 * \param[in] fl to indicate the detailed set operation: LU_XATTR_CREATE
1066 * or LU_XATTR_REPLACE or others
1067 * \param[in] th pointer to the transaction handler
1069 * \retval 0 for success
1070 * \retval negative error number on failure
1072 int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
1073 const struct lu_buf *buf, const char *name, int fl,
1076 struct osp_object *o = dt2osp_obj(dt);
1077 struct osp_update_request *update;
1078 struct osp_xattr_entry *oxe;
1082 update = thandle_to_osp_update_request(th);
1083 LASSERT(update != NULL);
1085 CDEBUG(D_INODE, DFID" set xattr '%s' with size %zd\n",
1086 PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
1088 rc = osp_update_rpc_pack(env, xattr_set, update, OUT_XATTR_SET,
1089 lu_object_fid(&dt->do_lu), buf, name, fl);
1093 oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
1095 CWARN("%s: cannot cache xattr '%s' of "DFID"\n",
1096 dt->do_lu.lo_dev->ld_obd->obd_name,
1097 name, PFID(lu_object_fid(&dt->do_lu)));
1102 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < buf->lb_len) {
1103 struct osp_xattr_entry *old = oxe;
1104 struct osp_xattr_entry *tmp;
1106 tmp = osp_oac_xattr_replace(o, &old, buf->lb_len);
1107 osp_oac_xattr_put(oxe);
1110 CWARN("%s: cannot update cached xattr '%s' of "DFID"\n",
1111 dt->do_lu.lo_dev->ld_obd->obd_name,
1112 name, PFID(lu_object_fid(&dt->do_lu)));
1113 spin_lock(&o->opo_lock);
1115 spin_unlock(&o->opo_lock);
1120 /* Drop the ref for entry on list. */
1121 osp_oac_xattr_put(old);
1124 spin_lock(&o->opo_lock);
1125 oxe->oxe_vallen = buf->lb_len;
1126 memcpy(oxe->oxe_value, buf->lb_buf, buf->lb_len);
1129 spin_unlock(&o->opo_lock);
1130 osp_oac_xattr_put(oxe);
1136 * Implement OSP layer dt_object_operations::do_declare_xattr_del() interface.
1138 * Declare that the caller will delete extended attribute on the specified
1141 * If it is non-remote transaction, it will add an OUT_XATTR_DEL sub-request
1142 * to the OUT RPC that will be flushed when the transaction start. And if the
1143 * name extended attribute entry exists in the OSP attributes cache, then remove
1144 * it from the cache.
1146 * \param[in] env pointer to the thread context
1147 * \param[in] dt pointer to the OSP layer dt_object
1148 * \param[in] name the name of the extended attribute to be set
1149 * \param[in] th pointer to the transaction handler
1151 * \retval 0 for success
1152 * \retval negative error number on failure
1154 int osp_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
1155 const char *name, struct thandle *th)
1157 return osp_trans_update_request_create(th);
1161 * Implement OSP layer dt_object_operations::do_xattr_del() interface.
1163 * Delete extended attribute on the specified MDT/OST object.
1165 * If it is remote transaction, it will add an OUT_XATTR_DEL sub-request into
1166 * the OUT RPC that will be flushed when the transaction stop. And if the name
1167 * extended attribute entry exists in the OSP attributes cache, then remove it
1170 * \param[in] env pointer to the thread context
1171 * \param[in] dt pointer to the OSP layer dt_object
1172 * \param[in] name the name of the extended attribute to be set
1173 * \param[in] th pointer to the transaction handler
1175 * \retval 0 for success
1176 * \retval negative error number on failure
1178 int osp_xattr_del(const struct lu_env *env, struct dt_object *dt,
1179 const char *name, struct thandle *th)
1181 struct osp_update_request *update;
1182 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1183 struct osp_object *o = dt2osp_obj(dt);
1184 struct osp_xattr_entry *oxe;
1187 update = thandle_to_osp_update_request(th);
1188 LASSERT(update != NULL);
1190 rc = osp_update_rpc_pack(env, xattr_del, update, OUT_XATTR_DEL,
1195 oxe = osp_oac_xattr_find(o, name, true);
1197 /* Drop the ref for entry on list. */
1198 osp_oac_xattr_put(oxe);
1203 void osp_obj_invalidate_cache(struct osp_object *obj)
1205 struct osp_xattr_entry *oxe;
1206 struct osp_xattr_entry *tmp;
1208 spin_lock(&obj->opo_lock);
1209 list_for_each_entry_safe(oxe, tmp, &obj->opo_xattr_list, oxe_list) {
1211 list_del_init(&oxe->oxe_list);
1212 osp_oac_xattr_put(oxe);
1214 obj->opo_attr.la_valid = 0;
1215 spin_unlock(&obj->opo_lock);
1219 * Implement OSP layer dt_object_operations::do_invalidate() interface.
1221 * Invalidate attributes cached on the specified MDT/OST object.
1223 * \param[in] env pointer to the thread context
1224 * \param[in] dt pointer to the OSP layer dt_object
1226 * \retval 0 for success
1227 * \retval negative error number on failure
1229 int osp_invalidate(const struct lu_env *env, struct dt_object *dt)
1231 struct osp_object *obj = dt2osp_obj(dt);
1234 osp_obj_invalidate_cache(obj);
1236 spin_lock(&obj->opo_lock);
1238 spin_unlock(&obj->opo_lock);
1244 * Implement OSP layer dt_object_operations::do_declare_create() interface.
1246 * Declare that the caller will create the OST object.
1248 * If the transaction is a remote transaction and the FID for the OST-object
1249 * has been assigned already, then handle it as creating (remote) MDT object
1250 * via osp_md_declare_object_create(). This function is usually used for LFSCK
1251 * to re-create the lost OST object. Otherwise, if it is not replay case, the
1252 * OSP will reserve pre-created object for the subsequent create operation;
1253 * if the MDT side cached pre-created objects are less than some threshold,
1254 * then it will wakeup the pre-create thread.
1256 * \param[in] env pointer to the thread context
1257 * \param[in] dt pointer to the OSP layer dt_object
1258 * \param[in] attr the attribute for the object to be created
1259 * \param[in] hint pointer to the hint for creating the object, such as
1261 * \param[in] dof pointer to the dt_object_format for help the creation
1262 * \param[in] th pointer to the transaction handler
1264 * \retval 0 for success
1265 * \retval negative error number on failure
1267 static int osp_declare_object_create(const struct lu_env *env,
1268 struct dt_object *dt,
1269 struct lu_attr *attr,
1270 struct dt_allocation_hint *hint,
1271 struct dt_object_format *dof,
1274 struct osp_thread_info *osi = osp_env_info(env);
1275 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1276 struct osp_object *o = dt2osp_obj(dt);
1277 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1278 struct thandle *local_th;
1283 if (is_only_remote_trans(th) && !fid_is_zero(fid)) {
1284 LASSERT(fid_is_sane(fid));
1286 rc = osp_md_declare_object_create(env, dt, attr, hint, dof, th);
1291 /* should happen to non-0 OSP only so that at least one object
1292 * has been already declared in the scenario and LOD should
1294 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
1297 LASSERT(d->opd_last_used_oid_file);
1300 * There can be gaps in precreated ids and record to unlink llog
1301 * XXX: we do not handle gaps yet, implemented before solution
1302 * was found to be racy, so we disabled that. there is no
1303 * point in making useless but expensive llog declaration.
1305 /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
1307 local_th = osp_get_storage_thandle(env, th, d);
1308 if (IS_ERR(local_th))
1309 RETURN(PTR_ERR(local_th));
1311 if (unlikely(!fid_is_zero(fid))) {
1312 /* replay case: caller knows fid */
1313 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1314 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1315 osi->osi_lb.lb_buf = NULL;
1317 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1318 &osi->osi_lb, osi->osi_off,
1324 * in declaration we need to reserve object so that we don't block
1325 * awaiting precreation RPC to complete
1327 rc = osp_precreate_reserve(env, d);
1329 * we also need to declare update to local "last used id" file for
1330 * recovery if object isn't used for a reason, we need to release
1331 * reservation, this can be made in osd_object_release()
1334 /* mark id is reserved: in create we don't want to talk
1336 LASSERT(o->opo_reserved == 0);
1337 o->opo_reserved = 1;
1339 /* common for all OSPs file hystorically */
1340 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1341 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1342 osi->osi_lb.lb_buf = NULL;
1343 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1344 &osi->osi_lb, osi->osi_off,
1347 /* not needed in the cache anymore */
1348 set_bit(LU_OBJECT_HEARD_BANSHEE,
1349 &dt->do_lu.lo_header->loh_flags);
1355 * Implement OSP layer dt_object_operations::do_create() interface.
1357 * Create the OST object.
1359 * If the transaction is a remote transaction and the FID for the OST-object
1360 * has been assigned already, then handle it as handling MDT object via the
1361 * osp_md_object_create(). For other cases, the OSP will assign FID to the
1362 * object to be created, and update last_used Object ID (OID) file.
1364 * \param[in] env pointer to the thread context
1365 * \param[in] dt pointer to the OSP layer dt_object
1366 * \param[in] attr the attribute for the object to be created
1367 * \param[in] hint pointer to the hint for creating the object, such as
1369 * \param[in] dof pointer to the dt_object_format for help the creation
1370 * \param[in] th pointer to the transaction handler
1372 * \retval 0 for success
1373 * \retval negative error number on failure
1375 static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
1376 struct lu_attr *attr,
1377 struct dt_allocation_hint *hint,
1378 struct dt_object_format *dof, struct thandle *th)
1380 struct osp_thread_info *osi = osp_env_info(env);
1381 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
1382 struct osp_object *o = dt2osp_obj(dt);
1384 struct lu_fid *fid = &osi->osi_fid;
1385 struct thandle *local_th;
1388 if (is_only_remote_trans(th) &&
1389 !fid_is_zero(lu_object_fid(&dt->do_lu))) {
1390 LASSERT(fid_is_sane(lu_object_fid(&dt->do_lu)));
1392 rc = osp_md_object_create(env, dt, attr, hint, dof, th);
1394 o->opo_non_exist = 0;
1399 o->opo_non_exist = 0;
1400 if (o->opo_reserved) {
1401 /* regular case, fid is assigned holding transaction open */
1402 osp_object_assign_fid(env, d, o);
1405 memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
1407 LASSERTF(fid_is_sane(fid), "fid for osp_object %p is insane"DFID"!\n",
1410 if (!o->opo_reserved) {
1411 /* special case, id was assigned outside of transaction
1412 * see comments in osp_declare_attr_set */
1413 LASSERT(d->opd_pre != NULL);
1414 spin_lock(&d->opd_pre_lock);
1415 osp_update_last_fid(d, fid);
1416 spin_unlock(&d->opd_pre_lock);
1419 CDEBUG(D_INODE, "fid for osp_object %p is "DFID"\n", o, PFID(fid));
1421 /* If the precreate ends, it means it will be ready to rollover to
1422 * the new sequence soon, all the creation should be synchronized,
1423 * otherwise during replay, the replay fid will be inconsistent with
1424 * last_used/create fid */
1425 if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
1428 local_th = osp_get_storage_thandle(env, th, d);
1429 if (IS_ERR(local_th))
1430 RETURN(PTR_ERR(local_th));
1432 * it's OK if the import is inactive by this moment - id was created
1433 * by OST earlier, we just need to maintain it consistently on the disk
1434 * once import is reconnected, OSP will claim this and other objects
1435 * used and OST either keep them, if they exist or recreate
1438 /* we might have lost precreated objects */
1439 if (unlikely(d->opd_gap_count) > 0) {
1440 LASSERT(d->opd_pre != NULL);
1441 spin_lock(&d->opd_pre_lock);
1442 if (d->opd_gap_count > 0) {
1443 int count = d->opd_gap_count;
1445 ostid_set_id(&osi->osi_oi,
1446 fid_oid(&d->opd_gap_start_fid));
1447 d->opd_gap_count = 0;
1448 spin_unlock(&d->opd_pre_lock);
1450 CDEBUG(D_HA, "Writing gap "DFID"+%d in llog\n",
1451 PFID(&d->opd_gap_start_fid), count);
1452 /* real gap handling is disabled intil ORI-692 will be
1453 * fixed, now we only report gaps */
1455 spin_unlock(&d->opd_pre_lock);
1459 /* Only need update last_used oid file, seq file will only be update
1460 * during seq rollover */
1461 osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
1462 &d->opd_last_used_fid.f_oid, d->opd_index);
1464 rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
1465 &osi->osi_off, local_th);
1467 CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
1468 d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
1474 * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1476 * Declare that the caller will destroy the specified OST object.
1478 * The OST object destroy will be handled via llog asynchronously. This
1479 * function will declare the credits for generating MDS_UNLINK64_REC llog.
1481 * \param[in] env pointer to the thread context
1482 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1483 * \param[in] th pointer to the transaction handler
1485 * \retval 0 for success
1486 * \retval negative error number on failure
1488 int osp_declare_object_destroy(const struct lu_env *env,
1489 struct dt_object *dt, struct thandle *th)
1491 struct osp_object *o = dt2osp_obj(dt);
1492 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1497 LASSERT(!osp->opd_connect_mdt);
1498 rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
1504 * Implement OSP layer dt_object_operations::do_destroy() interface.
1506 * Destroy the specified OST object.
1508 * The OSP generates a MDS_UNLINK64_REC record in the llog. There
1509 * will be some dedicated thread to handle the llog asynchronously.
1511 * It also marks the object as non-cached.
1513 * \param[in] env pointer to the thread context
1514 * \param[in] dt pointer to the OSP layer dt_object to be destroyed
1515 * \param[in] th pointer to the transaction handler
1517 * \retval 0 for success
1518 * \retval negative error number on failure
1520 static int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
1523 struct osp_object *o = dt2osp_obj(dt);
1524 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
1529 o->opo_non_exist = 1;
1531 LASSERT(!osp->opd_connect_mdt);
1532 /* once transaction is committed put proper command on
1533 * the queue going to our OST. */
1534 rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
1538 /* not needed in cache any more */
1539 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1544 static int osp_orphan_index_lookup(const struct lu_env *env,
1545 struct dt_object *dt,
1547 const struct dt_key *key)
1552 static int osp_orphan_index_declare_insert(const struct lu_env *env,
1553 struct dt_object *dt,
1554 const struct dt_rec *rec,
1555 const struct dt_key *key,
1556 struct thandle *handle)
1561 static int osp_orphan_index_insert(const struct lu_env *env,
1562 struct dt_object *dt,
1563 const struct dt_rec *rec,
1564 const struct dt_key *key,
1565 struct thandle *handle,
1571 static int osp_orphan_index_declare_delete(const struct lu_env *env,
1572 struct dt_object *dt,
1573 const struct dt_key *key,
1574 struct thandle *handle)
1579 static int osp_orphan_index_delete(const struct lu_env *env,
1580 struct dt_object *dt,
1581 const struct dt_key *key,
1582 struct thandle *handle)
1588 * Initialize the OSP layer index iteration.
1590 * \param[in] env pointer to the thread context
1591 * \param[in] dt pointer to the index object to be iterated
1592 * \param[in] attr unused
1594 * \retval pointer to the iteration structure
1595 * \retval negative error number on failure
1597 struct dt_it *osp_it_init(const struct lu_env *env, struct dt_object *dt,
1604 return ERR_PTR(-ENOMEM);
1606 it->ooi_pos_ent = -1;
1608 it->ooi_attr = attr;
1610 return (struct dt_it *)it;
1614 * Finalize the OSP layer index iteration.
1616 * \param[in] env pointer to the thread context
1617 * \param[in] di pointer to the iteration structure
1619 void osp_it_fini(const struct lu_env *env, struct dt_it *di)
1621 struct osp_it *it = (struct osp_it *)di;
1622 struct page **pages = it->ooi_pages;
1623 int npages = it->ooi_total_npages;
1626 if (pages != NULL) {
1627 for (i = 0; i < npages; i++) {
1628 if (pages[i] != NULL) {
1629 if (pages[i] == it->ooi_cur_page) {
1631 it->ooi_cur_page = NULL;
1633 __free_page(pages[i]);
1636 OBD_FREE(pages, npages * sizeof(*pages));
1642 * Get more records for the iteration from peer.
1644 * The new records will be filled in an array of pages. The OSP side
1645 * allows 1MB bulk data to be transferred.
1647 * \param[in] env pointer to the thread context
1648 * \param[in] it pointer to the iteration structure
1650 * \retval 0 for success
1651 * \retval negative error number on failure
1653 static int osp_it_fetch(const struct lu_env *env, struct osp_it *it)
1655 struct lu_device *dev = it->ooi_obj->do_lu.lo_dev;
1656 struct osp_device *osp = lu2osp_dev(dev);
1657 struct page **pages;
1658 struct ptlrpc_request *req = NULL;
1659 struct ptlrpc_bulk_desc *desc;
1660 struct idx_info *ii;
1667 npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
1668 npages /= PAGE_SIZE;
1670 OBD_ALLOC(pages, npages * sizeof(*pages));
1674 it->ooi_pages = pages;
1675 it->ooi_total_npages = npages;
1676 for (i = 0; i < npages; i++) {
1677 pages[i] = alloc_page(GFP_NOFS);
1678 if (pages[i] == NULL)
1682 req = ptlrpc_request_alloc(osp->opd_obd->u.cli.cl_import,
1687 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, OBD_IDX_READ);
1689 ptlrpc_request_free(req);
1693 osp_set_req_replay(osp, req);
1694 req->rq_request_portal = OUT_PORTAL;
1695 ii = req_capsule_client_get(&req->rq_pill, &RMF_IDX_INFO);
1696 memset(ii, 0, sizeof(*ii));
1697 if (fid_is_last_id(lu_object_fid(&it->ooi_obj->do_lu))) {
1698 /* LFSCK will iterate orphan object[FID_SEQ_LAYOUT_BTREE,
1699 * ost_index, 0] with LAST_ID FID, so it needs to replace
1700 * the FID with orphan FID here */
1701 ii->ii_fid.f_seq = FID_SEQ_LAYOUT_RBTREE;
1702 ii->ii_fid.f_oid = osp->opd_index;
1703 ii->ii_fid.f_ver = 0;
1704 ii->ii_flags = II_FL_NOHASH;
1705 ii->ii_attrs = osp_dev2node(osp);
1707 ii->ii_fid = *lu_object_fid(&it->ooi_obj->do_lu);
1708 ii->ii_flags = II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
1710 ii->ii_attrs = it->ooi_attr;
1712 ii->ii_magic = IDX_INFO_MAGIC;
1713 ii->ii_count = npages * LU_PAGE_COUNT;
1714 ii->ii_hash_start = it->ooi_next;
1716 ptlrpc_at_set_req_timeout(req);
1718 desc = ptlrpc_prep_bulk_imp(req, npages, 1,
1719 PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1721 &ptlrpc_bulk_kiov_pin_ops);
1723 ptlrpc_request_free(req);
1727 for (i = 0; i < npages; i++)
1728 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1731 ptlrpc_request_set_replen(req);
1732 rc = ptlrpc_queue_wait(req);
1736 rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1737 req->rq_bulk->bd_nob_transferred);
1742 ii = req_capsule_server_get(&req->rq_pill, &RMF_IDX_INFO);
1743 if (ii->ii_magic != IDX_INFO_MAGIC)
1744 GOTO(out, rc = -EPROTO);
1746 npages = (ii->ii_count + LU_PAGE_COUNT - 1) >>
1747 (PAGE_SHIFT - LU_PAGE_SHIFT);
1748 if (npages > it->ooi_total_npages) {
1749 CERROR("%s: returned more pages than expected, %u > %u\n",
1750 osp->opd_obd->obd_name, npages, it->ooi_total_npages);
1751 GOTO(out, rc = -EINVAL);
1754 it->ooi_valid_npages = npages;
1755 if (ptlrpc_rep_need_swab(req))
1758 it->ooi_next = ii->ii_hash_end;
1761 ptlrpc_req_finished(req);
1767 * Move the iteration cursor to the next lu_page.
1769 * One system page (PAGE_SIZE) may contain multiple lu_page (4KB),
1770 * that depends on the LU_PAGE_COUNT. If it is not the last lu_page
1771 * in current system page, then move the iteration cursor to the next
1772 * lu_page in current system page. Otherwise, if there are more system
1773 * pages in the cache, then move the iteration cursor to the next system
1774 * page. If all the cached records (pages) have been iterated, then fetch
1775 * more records via osp_it_fetch().
1777 * \param[in] env pointer to the thread context
1778 * \param[in] di pointer to the iteration structure
1780 * \retval positive for end of the directory
1781 * \retval 0 for success
1782 * \retval negative error number on failure
1784 int osp_it_next_page(const struct lu_env *env, struct dt_it *di)
1786 struct osp_it *it = (struct osp_it *)di;
1787 struct lu_idxpage *idxpage;
1788 struct page **pages;
1794 idxpage = it->ooi_cur_idxpage;
1795 if (idxpage != NULL) {
1796 if (idxpage->lip_nr == 0)
1799 if (it->ooi_pos_ent < idxpage->lip_nr) {
1800 CDEBUG(D_INFO, "ooi_pos %d nr %d\n",
1801 (int)it->ooi_pos_ent, (int)idxpage->lip_nr);
1804 it->ooi_cur_idxpage = NULL;
1805 it->ooi_pos_lu_page++;
1808 if (it->ooi_pos_lu_page < LU_PAGE_COUNT) {
1809 it->ooi_cur_idxpage = (void *)it->ooi_cur_page +
1810 LU_PAGE_SIZE * it->ooi_pos_lu_page;
1812 lustre_swab_lip_header(it->ooi_cur_idxpage);
1813 if (it->ooi_cur_idxpage->lip_magic != LIP_MAGIC) {
1814 struct osp_device *osp =
1815 lu2osp_dev(it->ooi_obj->do_lu.lo_dev);
1817 CERROR("%s: invalid magic (%x != %x) for page "
1818 "%d/%d while read layout orphan index\n",
1819 osp->opd_obd->obd_name,
1820 it->ooi_cur_idxpage->lip_magic,
1821 LIP_MAGIC, it->ooi_pos_page,
1822 it->ooi_pos_lu_page);
1823 /* Skip this lu_page next time. */
1824 it->ooi_pos_ent = idxpage->lip_nr - 1;
1827 it->ooi_pos_ent = -1;
1831 kunmap(it->ooi_cur_page);
1832 it->ooi_cur_page = NULL;
1836 pages = it->ooi_pages;
1837 if (it->ooi_pos_page < it->ooi_valid_npages) {
1838 it->ooi_cur_page = kmap(pages[it->ooi_pos_page]);
1839 it->ooi_pos_lu_page = 0;
1843 for (i = 0; i < it->ooi_total_npages; i++) {
1844 if (pages[i] != NULL)
1845 __free_page(pages[i]);
1847 OBD_FREE(pages, it->ooi_total_npages * sizeof(*pages));
1849 it->ooi_pos_page = 0;
1850 it->ooi_total_npages = 0;
1851 it->ooi_valid_npages = 0;
1854 it->ooi_cur_page = NULL;
1855 it->ooi_cur_idxpage = NULL;
1856 it->ooi_pages = NULL;
1859 if (it->ooi_next == II_END_OFF)
1862 rc = osp_it_fetch(env, it);
1870 * Move the iteration cursor to the next record.
1872 * If there are more records in the lu_page, then move the iteration
1873 * cursor to the next record directly. Otherwise, move the iteration
1874 * cursor to the record in the next lu_page via osp_it_next_page()
1876 * \param[in] env pointer to the thread context
1877 * \param[in] di pointer to the iteration structure
1879 * \retval positive for end of the directory
1880 * \retval 0 for success
1881 * \retval negative error number on failure
1883 static int osp_orphan_it_next(const struct lu_env *env, struct dt_it *di)
1885 struct osp_it *it = (struct osp_it *)di;
1886 struct lu_idxpage *idxpage;
1891 idxpage = it->ooi_cur_idxpage;
1892 if (idxpage != NULL) {
1893 if (idxpage->lip_nr == 0)
1897 if (it->ooi_pos_ent < idxpage->lip_nr) {
1899 (struct lu_orphan_ent *)idxpage->lip_entries +
1902 lustre_swab_orphan_ent(it->ooi_ent);
1907 rc = osp_it_next_page(env, di);
1914 int osp_it_get(const struct lu_env *env, struct dt_it *di,
1915 const struct dt_key *key)
1920 void osp_it_put(const struct lu_env *env, struct dt_it *di)
1924 static struct dt_key *osp_orphan_it_key(const struct lu_env *env,
1925 const struct dt_it *di)
1927 struct osp_it *it = (struct osp_it *)di;
1928 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
1930 if (likely(ent != NULL))
1931 return (struct dt_key *)(&ent->loe_key);
1936 static int osp_orphan_it_key_size(const struct lu_env *env,
1937 const struct dt_it *di)
1939 return sizeof(struct lu_fid);
1942 static int osp_orphan_it_rec(const struct lu_env *env, const struct dt_it *di,
1943 struct dt_rec *rec, __u32 attr)
1945 struct osp_it *it = (struct osp_it *)di;
1946 struct lu_orphan_ent *ent = (struct lu_orphan_ent *)it->ooi_ent;
1948 if (likely(ent != NULL)) {
1949 *(struct lu_orphan_rec *)rec = ent->loe_rec;
1956 __u64 osp_it_store(const struct lu_env *env, const struct dt_it *di)
1958 struct osp_it *it = (struct osp_it *)di;
1960 return it->ooi_next;
1964 * Locate the iteration cursor to the specified position (cookie).
1966 * \param[in] env pointer to the thread context
1967 * \param[in] di pointer to the iteration structure
1968 * \param[in] hash the specified position
1970 * \retval positive number for locating to the exactly position
1972 * \retval 0 for arriving at the end of the iteration
1973 * \retval negative error number on failure
1975 int osp_orphan_it_load(const struct lu_env *env, const struct dt_it *di,
1978 struct osp_it *it = (struct osp_it *)di;
1981 it->ooi_next = hash;
1982 rc = osp_orphan_it_next(env, (struct dt_it *)di);
1992 int osp_it_key_rec(const struct lu_env *env, const struct dt_it *di,
1998 static const struct dt_index_operations osp_orphan_index_ops = {
1999 .dio_lookup = osp_orphan_index_lookup,
2000 .dio_declare_insert = osp_orphan_index_declare_insert,
2001 .dio_insert = osp_orphan_index_insert,
2002 .dio_declare_delete = osp_orphan_index_declare_delete,
2003 .dio_delete = osp_orphan_index_delete,
2005 .init = osp_it_init,
2006 .fini = osp_it_fini,
2007 .next = osp_orphan_it_next,
2010 .key = osp_orphan_it_key,
2011 .key_size = osp_orphan_it_key_size,
2012 .rec = osp_orphan_it_rec,
2013 .store = osp_it_store,
2014 .load = osp_orphan_it_load,
2015 .key_rec = osp_it_key_rec,
2020 * Implement OSP layer dt_object_operations::do_index_try() interface.
2022 * Negotiate the index type.
2024 * If the target index is an IDIF object, then use osp_orphan_index_ops.
2025 * Otherwise, assign osp_md_index_ops to the dt_object::do_index_ops.
2026 * (\see lustre/include/lustre_fid.h for IDIF.)
2028 * \param[in] env pointer to the thread context
2029 * \param[in] dt pointer to the OSP layer dt_object
2030 * \param[in] feat unused
2032 * \retval 0 for success
2034 static int osp_index_try(const struct lu_env *env,
2035 struct dt_object *dt,
2036 const struct dt_index_features *feat)
2038 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2040 if (fid_is_last_id(fid) && fid_is_idif(fid))
2041 dt->do_index_ops = &osp_orphan_index_ops;
2043 dt->do_index_ops = &osp_md_index_ops;
2047 static struct dt_object_operations osp_obj_ops = {
2048 .do_declare_attr_get = osp_declare_attr_get,
2049 .do_attr_get = osp_attr_get,
2050 .do_declare_attr_set = osp_declare_attr_set,
2051 .do_attr_set = osp_attr_set,
2052 .do_declare_xattr_get = osp_declare_xattr_get,
2053 .do_xattr_get = osp_xattr_get,
2054 .do_declare_xattr_set = osp_declare_xattr_set,
2055 .do_xattr_set = osp_xattr_set,
2056 .do_declare_create = osp_declare_object_create,
2057 .do_create = osp_object_create,
2058 .do_declare_destroy = osp_declare_object_destroy,
2059 .do_destroy = osp_object_destroy,
2060 .do_index_try = osp_index_try,
2064 * Implement OSP layer lu_object_operations::loo_object_init() interface.
2066 * Initialize the object.
2068 * If it is a remote MDT object, then call do_attr_get() to fetch
2069 * the attribute from the peer.
2071 * \param[in] env pointer to the thread context
2072 * \param[in] o pointer to the OSP layer lu_object
2073 * \param[in] conf unused
2075 * \retval 0 for success
2076 * \retval negative error number on failure
2078 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
2079 const struct lu_object_conf *conf)
2081 struct osp_object *po = lu2osp_obj(o);
2085 spin_lock_init(&po->opo_lock);
2086 o->lo_header->loh_attr |= LOHA_REMOTE;
2087 INIT_LIST_HEAD(&po->opo_xattr_list);
2088 INIT_LIST_HEAD(&po->opo_invalidate_cb_list);
2090 if (is_ost_obj(o)) {
2091 po->opo_obj.do_ops = &osp_obj_ops;
2093 struct lu_attr *la = &osp_env_info(env)->osi_attr;
2095 po->opo_obj.do_ops = &osp_md_obj_ops;
2096 po->opo_obj.do_body_ops = &osp_md_body_ops;
2098 if (conf != NULL && conf->loc_flags & LOC_F_NEW) {
2099 po->opo_non_exist = 1;
2101 rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o),
2104 o->lo_header->loh_attr |=
2105 LOHA_EXISTS | (la->la_mode & S_IFMT);
2106 if (rc == -ENOENT) {
2107 po->opo_non_exist = 1;
2111 init_rwsem(&po->opo_sem);
2117 * Implement OSP layer lu_object_operations::loo_object_free() interface.
2119 * Finalize the object.
2121 * If the OSP object has attributes cache, then destroy the cache.
2122 * Free the object finally.
2124 * \param[in] env pointer to the thread context
2125 * \param[in] o pointer to the OSP layer lu_object
2127 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
2129 struct osp_object *obj = lu2osp_obj(o);
2130 struct lu_object_header *h = o->lo_header;
2131 struct osp_xattr_entry *oxe;
2132 struct osp_xattr_entry *tmp;
2135 dt_object_fini(&obj->opo_obj);
2136 lu_object_header_fini(h);
2137 list_for_each_entry_safe(oxe, tmp, &obj->opo_xattr_list, oxe_list) {
2138 list_del(&oxe->oxe_list);
2139 count = atomic_read(&oxe->oxe_ref);
2140 LASSERTF(count == 1,
2141 "Still has %d users on the xattr entry %.*s\n",
2142 count-1, (int)oxe->oxe_namelen, oxe->oxe_buf);
2144 OBD_FREE(oxe, oxe->oxe_buflen);
2146 OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
2150 * Implement OSP layer lu_object_operations::loo_object_release() interface.
2152 * Cleanup (not free) the object.
2154 * If it is a reserved object but failed to be created, or it is an OST
2155 * object, then mark the object as non-cached.
2157 * \param[in] env pointer to the thread context
2158 * \param[in] o pointer to the OSP layer lu_object
2160 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
2162 struct osp_object *po = lu2osp_obj(o);
2163 struct osp_device *d = lu2osp_dev(o->lo_dev);
2168 * release reservation if object was declared but not created
2169 * this may require lu_object_put() in LOD
2171 if (unlikely(po->opo_reserved)) {
2172 LASSERT(d->opd_pre != NULL);
2173 LASSERT(d->opd_pre_reserved > 0);
2174 spin_lock(&d->opd_pre_lock);
2175 d->opd_pre_reserved--;
2176 spin_unlock(&d->opd_pre_lock);
2178 /* not needed in cache any more */
2179 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2183 /* XXX: Currently, NOT cache OST-object on MDT because:
2184 * 1. it is not often accessed on MDT.
2185 * 2. avoid up layer (such as LFSCK) to load too many
2186 * once-used OST-objects. */
2187 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2192 static int osp_object_print(const struct lu_env *env, void *cookie,
2193 lu_printer_t p, const struct lu_object *l)
2195 const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
2197 return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
2200 static int osp_object_invariant(const struct lu_object *o)
2205 struct lu_object_operations osp_lu_obj_ops = {
2206 .loo_object_init = osp_object_init,
2207 .loo_object_free = osp_object_free,
2208 .loo_object_release = osp_object_release,
2209 .loo_object_print = osp_object_print,
2210 .loo_object_invariant = osp_object_invariant