4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osp/osp_object.c
38 * Lustre OST Proxy Device
40 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 * Author: Mikhail Pershin <mike.tappro@intel.com>
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include "osp_internal.h"
48 static inline bool is_ost_obj(struct lu_object *lo)
50 return !lu2osp_dev(lo->lo_dev)->opd_connect_mdt;
53 static void osp_object_assign_fid(const struct lu_env *env,
54 struct osp_device *d, struct osp_object *o)
56 struct osp_thread_info *osi = osp_env_info(env);
58 LASSERT(fid_is_zero(lu_object_fid(&o->opo_obj.do_lu)));
59 LASSERT(o->opo_reserved);
62 osp_precreate_get_fid(env, d, &osi->osi_fid);
64 lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
67 static int osp_oac_init(struct osp_object *obj)
69 struct osp_object_attr *ooa;
75 INIT_LIST_HEAD(&ooa->ooa_xattr_list);
76 spin_lock(&obj->opo_lock);
77 if (likely(obj->opo_ooa == NULL)) {
79 spin_unlock(&obj->opo_lock);
81 spin_unlock(&obj->opo_lock);
88 static struct osp_xattr_entry *
89 osp_oac_xattr_find_locked(struct osp_object_attr *ooa,
90 const char *name, int namelen, bool unlink)
92 struct osp_xattr_entry *oxe;
94 list_for_each_entry(oxe, &ooa->ooa_xattr_list, oxe_list) {
95 if (namelen == oxe->oxe_namelen &&
96 strncmp(name, oxe->oxe_buf, namelen) == 0) {
98 list_del_init(&oxe->oxe_list);
100 atomic_inc(&oxe->oxe_ref);
109 static struct osp_xattr_entry *osp_oac_xattr_find(struct osp_object *obj,
112 struct osp_xattr_entry *oxe = NULL;
114 spin_lock(&obj->opo_lock);
115 if (obj->opo_ooa != NULL)
116 oxe = osp_oac_xattr_find_locked(obj->opo_ooa, name,
117 strlen(name), false);
118 spin_unlock(&obj->opo_lock);
123 static struct osp_xattr_entry *
124 osp_oac_xattr_find_or_add(struct osp_object *obj, const char *name, int len)
126 struct osp_object_attr *ooa = obj->opo_ooa;
127 struct osp_xattr_entry *oxe;
128 struct osp_xattr_entry *tmp = NULL;
129 int namelen = strlen(name);
130 int size = sizeof(*oxe) + namelen + 1 + len;
132 LASSERT(ooa != NULL);
134 oxe = osp_oac_xattr_find(obj, name);
138 OBD_ALLOC(oxe, size);
139 if (unlikely(oxe == NULL))
142 INIT_LIST_HEAD(&oxe->oxe_list);
143 oxe->oxe_buflen = size;
144 oxe->oxe_namelen = namelen;
145 memcpy(oxe->oxe_buf, name, namelen);
146 oxe->oxe_value = oxe->oxe_buf + namelen + 1;
147 /* One ref is for the caller, the other is for the entry on the list. */
148 atomic_set(&oxe->oxe_ref, 2);
150 spin_lock(&obj->opo_lock);
151 tmp = osp_oac_xattr_find_locked(ooa, name, namelen, false);
153 list_add_tail(&oxe->oxe_list, &ooa->ooa_xattr_list);
154 spin_unlock(&obj->opo_lock);
164 static struct osp_xattr_entry *
165 osp_oac_xattr_replace(struct osp_object *obj,
166 struct osp_xattr_entry **poxe, int len)
168 struct osp_object_attr *ooa = obj->opo_ooa;
169 struct osp_xattr_entry *old = *poxe;
170 struct osp_xattr_entry *oxe;
171 struct osp_xattr_entry *tmp = NULL;
172 int namelen = old->oxe_namelen;
173 int size = sizeof(*oxe) + namelen + 1 + len;
175 LASSERT(ooa != NULL);
177 OBD_ALLOC(oxe, size);
178 if (unlikely(oxe == NULL))
181 INIT_LIST_HEAD(&oxe->oxe_list);
182 oxe->oxe_buflen = size;
183 oxe->oxe_namelen = namelen;
184 memcpy(oxe->oxe_buf, old->oxe_buf, namelen);
185 oxe->oxe_value = oxe->oxe_buf + namelen + 1;
186 /* One ref is for the caller, the other is for the entry on the list. */
187 atomic_set(&oxe->oxe_ref, 2);
189 spin_lock(&obj->opo_lock);
190 tmp = osp_oac_xattr_find_locked(ooa, oxe->oxe_buf, namelen, true);
191 list_add_tail(&oxe->oxe_list, &ooa->ooa_xattr_list);
192 spin_unlock(&obj->opo_lock);
195 LASSERT(tmp != NULL);
200 static inline void osp_oac_xattr_put(struct osp_xattr_entry *oxe)
202 if (atomic_dec_and_test(&oxe->oxe_ref)) {
203 LASSERT(list_empty(&oxe->oxe_list));
205 OBD_FREE(oxe, oxe->oxe_buflen);
209 static int osp_get_attr_from_reply(const struct lu_env *env,
210 struct update_reply *reply,
211 struct lu_attr *attr,
212 struct osp_object *obj, int index)
214 struct osp_thread_info *osi = osp_env_info(env);
215 struct lu_buf *rbuf = &osi->osi_lb2;
216 struct obdo *lobdo = &osi->osi_obdo;
220 rc = update_get_reply_buf(reply, rbuf, index);
224 wobdo = rbuf->lb_buf;
225 if (rbuf->lb_len != sizeof(*wobdo))
228 obdo_le_to_cpu(wobdo, wobdo);
229 lustre_get_wire_obdo(NULL, lobdo, wobdo);
230 spin_lock(&obj->opo_lock);
231 if (obj->opo_ooa != NULL) {
232 la_from_obdo(&obj->opo_ooa->ooa_attr, lobdo, lobdo->o_valid);
234 *attr = obj->opo_ooa->ooa_attr;
236 LASSERT(attr != NULL);
238 la_from_obdo(attr, lobdo, lobdo->o_valid);
240 spin_unlock(&obj->opo_lock);
245 static int osp_attr_get_interpterer(const struct lu_env *env,
246 struct update_reply *reply,
247 struct osp_object *obj,
248 void *data, int index, int rc)
250 struct lu_attr *attr = data;
252 LASSERT(obj->opo_ooa != NULL);
255 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
256 obj->opo_non_exist = 0;
258 return osp_get_attr_from_reply(env, reply, NULL, obj, index);
261 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
262 obj->opo_non_exist = 1;
265 spin_lock(&obj->opo_lock);
267 spin_unlock(&obj->opo_lock);
273 static int osp_declare_attr_get(const struct lu_env *env, struct dt_object *dt,
274 struct lustre_capa *capa)
276 struct osp_object *obj = dt2osp_obj(dt);
277 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
278 struct update_request *update;
281 if (obj->opo_ooa == NULL) {
282 rc = osp_oac_init(obj);
287 mutex_lock(&osp->opd_async_requests_mutex);
288 update = osp_find_or_create_async_update_request(osp);
290 rc = PTR_ERR(update);
292 rc = osp_insert_async_update(env, update, OBJ_ATTR_GET, obj, 0,
294 &obj->opo_ooa->ooa_attr,
295 osp_attr_get_interpterer);
296 mutex_unlock(&osp->opd_async_requests_mutex);
301 int osp_attr_get(const struct lu_env *env, struct dt_object *dt,
302 struct lu_attr *attr, struct lustre_capa *capa)
304 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
305 struct osp_object *obj = dt2osp_obj(dt);
306 struct dt_device *dev = &osp->opd_dt_dev;
307 struct update_request *update;
308 struct update_reply *reply;
309 struct ptlrpc_request *req = NULL;
313 if (is_ost_obj(&dt->do_lu) && obj->opo_non_exist)
316 if (obj->opo_ooa != NULL) {
317 spin_lock(&obj->opo_lock);
318 if (obj->opo_ooa->ooa_attr.la_valid != 0) {
319 *attr = obj->opo_ooa->ooa_attr;
320 spin_unlock(&obj->opo_lock);
324 spin_unlock(&obj->opo_lock);
327 update = out_create_update_req(dev);
329 RETURN(PTR_ERR(update));
331 rc = out_insert_update(env, update, OBJ_ATTR_GET,
332 lu_object_fid(&dt->do_lu), 0, NULL, NULL);
334 CERROR("%s: Insert update error "DFID": rc = %d\n",
335 dev->dd_lu_dev.ld_obd->obd_name,
336 PFID(lu_object_fid(&dt->do_lu)), rc);
341 rc = out_remote_sync(env, osp->opd_obd->u.cli.cl_import, update, &req);
344 osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
345 obj->opo_non_exist = 1;
347 CERROR("%s:osp_attr_get update error "DFID": rc = %d\n",
348 dev->dd_lu_dev.ld_obd->obd_name,
349 PFID(lu_object_fid(&dt->do_lu)), rc);
355 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
356 obj->opo_non_exist = 0;
357 reply = req_capsule_server_sized_get(&req->rq_pill, &RMF_UPDATE_REPLY,
359 if (reply == NULL || reply->ur_version != UPDATE_REPLY_V1)
360 GOTO(out, rc = -EPROTO);
362 rc = osp_get_attr_from_reply(env, reply, attr, obj, 0);
366 if (!is_ost_obj(&dt->do_lu)) {
367 if (attr->la_flags == 1)
377 ptlrpc_req_finished(req);
379 out_destroy_update_req(update);
384 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
385 const struct lu_attr *attr, struct thandle *th)
387 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
388 struct osp_object *o = dt2osp_obj(dt);
395 * Usually we don't allow server stack to manipulate size
396 * but there is a special case when striping is created
397 * late, after stripless file got truncated to non-zero.
399 * In this case we do the following:
401 * 1) grab id in declare - this can lead to leaked OST objects
402 * but we don't currently have proper mechanism and the only
403 * options we have are to do truncate RPC holding transaction
404 * open (very bad) or to grab id in declare at cost of leaked
405 * OST object in same very rare unfortunate case (just bad)
406 * notice 1.6-2.0 do assignment outside of running transaction
407 * all the time, meaning many more chances for leaked objects.
409 * 2) send synchronous truncate RPC with just assigned id
412 /* there are few places in MDD code still passing NULL
413 * XXX: to be fixed soon */
417 if (attr->la_valid & LA_SIZE && attr->la_size > 0 &&
418 fid_is_zero(lu_object_fid(&o->opo_obj.do_lu))) {
419 LASSERT(!dt_object_exists(dt));
420 osp_object_assign_fid(env, d, o);
421 rc = osp_object_truncate(env, dt, attr->la_size);
427 /* no need in logging for new objects being created */
431 if (!(attr->la_valid & (LA_UID | LA_GID)))
434 if (!is_only_remote_trans(th))
436 * track all UID/GID changes via llog
438 rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
440 /* It is for OST-object attr_set directly without updating
441 * local MDT-object attribute. It is usually used by LFSCK. */
442 rc = osp_md_declare_attr_set(env, dt, attr, th);
444 if (rc != 0 || o->opo_ooa == NULL)
447 la = &o->opo_ooa->ooa_attr;
448 spin_lock(&o->opo_lock);
449 if (attr->la_valid & LA_UID) {
450 la->la_uid = attr->la_uid;
451 la->la_valid |= LA_UID;
454 if (attr->la_valid & LA_GID) {
455 la->la_gid = attr->la_gid;
456 la->la_valid |= LA_GID;
458 spin_unlock(&o->opo_lock);
463 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
464 const struct lu_attr *attr, struct thandle *th,
465 struct lustre_capa *capa)
467 struct osp_object *o = dt2osp_obj(dt);
472 /* we're interested in uid/gid changes only */
473 if (!(attr->la_valid & (LA_UID | LA_GID)))
476 /* new object, the very first ->attr_set()
477 * initializing attributes needs no logging
478 * all subsequent one are subject to the
479 * logging and synchronization with OST */
485 if (!is_only_remote_trans(th))
487 * once transaction is committed put proper command on
488 * the queue going to our OST
490 rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
491 /* XXX: send new uid/gid to OST ASAP? */
493 /* It is for OST-object attr_set directly without updating
494 * local MDT-object attribute. It is usually used by LFSCK. */
495 rc = osp_md_attr_set(env, dt, attr, th, capa);
500 static int osp_xattr_get_interpterer(const struct lu_env *env,
501 struct update_reply *reply,
502 struct osp_object *obj,
503 void *data, int index, int rc)
505 struct osp_object_attr *ooa = obj->opo_ooa;
506 struct osp_xattr_entry *oxe = data;
507 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
509 LASSERT(ooa != NULL);
512 int len = sizeof(*oxe) + oxe->oxe_namelen + 1;
514 rc = update_get_reply_buf(reply, rbuf, index);
515 if (rc < 0 || rbuf->lb_len > (oxe->oxe_buflen - len)) {
516 spin_lock(&obj->opo_lock);
518 spin_unlock(&obj->opo_lock);
519 osp_oac_xattr_put(oxe);
521 return rc < 0 ? rc : -ERANGE;
524 spin_lock(&obj->opo_lock);
525 oxe->oxe_vallen = rbuf->lb_len;
526 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
529 spin_unlock(&obj->opo_lock);
530 } else if (rc == -ENOENT || rc == -ENODATA) {
531 spin_lock(&obj->opo_lock);
534 spin_unlock(&obj->opo_lock);
536 spin_lock(&obj->opo_lock);
538 spin_unlock(&obj->opo_lock);
541 osp_oac_xattr_put(oxe);
546 static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
547 struct lu_buf *buf, const char *name,
548 struct lustre_capa *capa)
550 struct osp_object *obj = dt2osp_obj(dt);
551 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
552 struct update_request *update;
553 struct osp_xattr_entry *oxe;
554 int namelen = strlen(name);
557 LASSERT(buf != NULL);
558 LASSERT(name != NULL);
560 /* If only for xattr size, return directly. */
561 if (unlikely(buf->lb_len == 0))
564 if (obj->opo_ooa == NULL) {
565 rc = osp_oac_init(obj);
570 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
574 mutex_lock(&osp->opd_async_requests_mutex);
575 update = osp_find_or_create_async_update_request(osp);
576 if (IS_ERR(update)) {
577 rc = PTR_ERR(update);
578 mutex_unlock(&osp->opd_async_requests_mutex);
579 osp_oac_xattr_put(oxe);
581 rc = osp_insert_async_update(env, update, OBJ_XATTR_GET, obj,
582 1, &namelen, &name, oxe,
583 osp_xattr_get_interpterer);
585 mutex_unlock(&osp->opd_async_requests_mutex);
586 osp_oac_xattr_put(oxe);
588 /* XXX: Currently, we trigger the batched async OUT
589 * RPC via dt_declare_xattr_get(). It is not
590 * perfect solution, but works well now.
592 * We will improve it in the future. */
593 update = osp->opd_async_requests;
594 if (update != NULL && update->ur_buf != NULL &&
595 update->ur_buf->ub_count > 0) {
596 osp->opd_async_requests = NULL;
597 mutex_unlock(&osp->opd_async_requests_mutex);
598 rc = osp_unplug_async_update(env, osp, update);
600 mutex_unlock(&osp->opd_async_requests_mutex);
608 int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
609 struct lu_buf *buf, const char *name,
610 struct lustre_capa *capa)
612 struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
613 struct osp_object *obj = dt2osp_obj(dt);
614 struct dt_device *dev = &osp->opd_dt_dev;
615 struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
616 struct update_request *update = NULL;
617 struct ptlrpc_request *req = NULL;
618 struct update_reply *reply;
619 struct osp_xattr_entry *oxe = NULL;
620 const char *dname = dt->do_lu.lo_dev->ld_obd->obd_name;
625 LASSERT(buf != NULL);
626 LASSERT(name != NULL);
628 if (unlikely(obj->opo_non_exist))
631 oxe = osp_oac_xattr_find(obj, name);
633 spin_lock(&obj->opo_lock);
634 if (oxe->oxe_ready) {
636 GOTO(unlock, rc = -ENODATA);
638 if (buf->lb_buf == NULL)
639 GOTO(unlock, rc = oxe->oxe_vallen);
641 if (buf->lb_len < oxe->oxe_vallen)
642 GOTO(unlock, rc = -ERANGE);
644 memcpy(buf->lb_buf, oxe->oxe_value, oxe->oxe_vallen);
646 GOTO(unlock, rc = oxe->oxe_vallen);
649 spin_unlock(&obj->opo_lock);
650 osp_oac_xattr_put(oxe);
654 spin_unlock(&obj->opo_lock);
657 update = out_create_update_req(dev);
659 GOTO(out, rc = PTR_ERR(update));
661 namelen = strlen(name);
662 rc = out_insert_update(env, update, OBJ_XATTR_GET,
663 lu_object_fid(&dt->do_lu), 1, &namelen, &name);
665 CERROR("%s: Insert update error "DFID": rc = %d\n",
666 dname, PFID(lu_object_fid(&dt->do_lu)), rc);
671 rc = out_remote_sync(env, osp->opd_obd->u.cli.cl_import, update, &req);
673 if (obj->opo_ooa == NULL)
677 oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
680 CWARN("%s: Fail to add xattr (%s) to cache for "
681 DFID" (1): rc = %d\n", dname, name,
682 PFID(lu_object_fid(&dt->do_lu)), rc);
687 spin_lock(&obj->opo_lock);
688 if (rc == -ENOENT || rc == -ENODATA) {
694 spin_unlock(&obj->opo_lock);
699 reply = req_capsule_server_sized_get(&req->rq_pill, &RMF_UPDATE_REPLY,
701 if (reply->ur_version != UPDATE_REPLY_V1) {
702 CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
703 dname, reply->ur_version, UPDATE_REPLY_V1,
704 PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
706 GOTO(out, rc = -EPROTO);
709 rc = update_get_reply_buf(reply, rbuf, 0);
713 LASSERT(rbuf->lb_len > 0 && rbuf->lb_len < PAGE_CACHE_SIZE);
715 if (buf->lb_buf == NULL)
716 GOTO(out, rc = rbuf->lb_len);
718 if (unlikely(buf->lb_len < rbuf->lb_len))
719 GOTO(out, rc = -ERANGE);
721 memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
723 if (obj->opo_ooa == NULL)
727 oxe = osp_oac_xattr_find_or_add(obj, name, rbuf->lb_len);
729 CWARN("%s: Fail to add xattr (%s) to "
730 "cache for "DFID" (2): rc = %d\n",
731 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
737 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < rbuf->lb_len) {
738 struct osp_xattr_entry *old = oxe;
739 struct osp_xattr_entry *tmp;
741 tmp = osp_oac_xattr_replace(obj, &old, rbuf->lb_len);
742 osp_oac_xattr_put(oxe);
745 CWARN("%s: Fail to update xattr (%s) to "
746 "cache for "DFID": rc = %d\n",
747 dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
748 spin_lock(&obj->opo_lock);
750 spin_unlock(&obj->opo_lock);
755 /* Drop the ref for entry on list. */
756 osp_oac_xattr_put(old);
759 spin_lock(&obj->opo_lock);
760 oxe->oxe_vallen = rbuf->lb_len;
761 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
764 spin_unlock(&obj->opo_lock);
770 ptlrpc_req_finished(req);
772 if (update != NULL && !IS_ERR(update))
773 out_destroy_update_req(update);
776 osp_oac_xattr_put(oxe);
781 int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
782 const struct lu_buf *buf, const char *name,
783 int flag, struct thandle *th)
785 struct osp_object *o = dt2osp_obj(dt);
786 struct update_request *update;
788 struct osp_xattr_entry *oxe;
789 int sizes[3] = {strlen(name), buf->lb_len,
791 char *bufs[3] = {(char *)name, (char *)buf->lb_buf };
794 LASSERT(buf->lb_len > 0 && buf->lb_buf != NULL);
796 update = out_find_create_update_loc(th, dt);
797 if (IS_ERR(update)) {
798 CERROR("%s: Get OSP update buf failed "DFID": rc = %d\n",
799 dt->do_lu.lo_dev->ld_obd->obd_name,
800 PFID(lu_object_fid(&dt->do_lu)),
801 (int)PTR_ERR(update));
803 return PTR_ERR(update);
806 flag = cpu_to_le32(flag);
807 bufs[2] = (char *)&flag;
809 fid = (struct lu_fid *)lu_object_fid(&dt->do_lu);
810 rc = out_insert_update(env, update, OBJ_XATTR_SET, fid,
811 ARRAY_SIZE(sizes), sizes, (const char **)bufs);
812 if (rc != 0 || o->opo_ooa == NULL)
815 oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
817 CWARN("%s: Fail to add xattr (%s) to cache for "DFID
818 ": rc = %d\n", dt->do_lu.lo_dev->ld_obd->obd_name,
819 name, PFID(lu_object_fid(&dt->do_lu)), rc);
824 if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < buf->lb_len) {
825 struct osp_xattr_entry *old = oxe;
826 struct osp_xattr_entry *tmp;
828 tmp = osp_oac_xattr_replace(o, &old, buf->lb_len);
829 osp_oac_xattr_put(oxe);
832 CWARN("%s: Fail to update xattr (%s) to cache for "DFID
833 ": rc = %d\n", dt->do_lu.lo_dev->ld_obd->obd_name,
834 name, PFID(lu_object_fid(&dt->do_lu)), rc);
835 spin_lock(&o->opo_lock);
837 spin_unlock(&o->opo_lock);
842 /* Drop the ref for entry on list. */
843 osp_oac_xattr_put(old);
846 spin_lock(&o->opo_lock);
847 oxe->oxe_vallen = buf->lb_len;
848 memcpy(oxe->oxe_value, buf->lb_buf, buf->lb_len);
851 spin_unlock(&o->opo_lock);
852 osp_oac_xattr_put(oxe);
857 int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
858 const struct lu_buf *buf, const char *name, int fl,
859 struct thandle *th, struct lustre_capa *capa)
861 CDEBUG(D_INFO, "xattr %s set object "DFID"\n", name,
862 PFID(&dt->do_lu.lo_header->loh_fid));
867 static int osp_declare_object_create(const struct lu_env *env,
868 struct dt_object *dt,
869 struct lu_attr *attr,
870 struct dt_allocation_hint *hint,
871 struct dt_object_format *dof,
874 struct osp_thread_info *osi = osp_env_info(env);
875 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
876 struct osp_object *o = dt2osp_obj(dt);
877 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
882 if (is_only_remote_trans(th)) {
883 LASSERT(fid_is_sane(fid));
885 rc = osp_md_declare_object_create(env, dt, attr, hint, dof, th);
890 /* should happen to non-0 OSP only so that at least one object
891 * has been already declared in the scenario and LOD should
893 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
896 LASSERT(d->opd_last_used_oid_file);
899 * There can be gaps in precreated ids and record to unlink llog
900 * XXX: we do not handle gaps yet, implemented before solution
901 * was found to be racy, so we disabled that. there is no
902 * point in making useless but expensive llog declaration.
904 /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
906 if (unlikely(!fid_is_zero(fid))) {
907 /* replay case: caller knows fid */
908 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
909 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
910 sizeof(osi->osi_id), osi->osi_off,
916 * in declaration we need to reserve object so that we don't block
917 * awaiting precreation RPC to complete
919 rc = osp_precreate_reserve(env, d);
921 * we also need to declare update to local "last used id" file for
922 * recovery if object isn't used for a reason, we need to release
923 * reservation, this can be made in osd_object_release()
926 /* mark id is reserved: in create we don't want to talk
928 LASSERT(o->opo_reserved == 0);
931 /* common for all OSPs file hystorically */
932 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
933 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
934 sizeof(osi->osi_id), osi->osi_off,
937 /* not needed in the cache anymore */
938 set_bit(LU_OBJECT_HEARD_BANSHEE,
939 &dt->do_lu.lo_header->loh_flags);
944 static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
945 struct lu_attr *attr,
946 struct dt_allocation_hint *hint,
947 struct dt_object_format *dof, struct thandle *th)
949 struct osp_thread_info *osi = osp_env_info(env);
950 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
951 struct osp_object *o = dt2osp_obj(dt);
953 struct lu_fid *fid = &osi->osi_fid;
956 if (is_only_remote_trans(th)) {
957 LASSERT(fid_is_sane(lu_object_fid(&dt->do_lu)));
959 rc = osp_md_object_create(env, dt, attr, hint, dof, th);
961 o->opo_non_exist = 0;
966 o->opo_non_exist = 0;
967 if (o->opo_reserved) {
968 /* regular case, fid is assigned holding trunsaction open */
969 osp_object_assign_fid(env, d, o);
972 memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
974 LASSERTF(fid_is_sane(fid), "fid for osp_object %p is insane"DFID"!\n",
977 if (!o->opo_reserved) {
978 /* special case, id was assigned outside of transaction
979 * see comments in osp_declare_attr_set */
980 LASSERT(d->opd_pre != NULL);
981 spin_lock(&d->opd_pre_lock);
982 osp_update_last_fid(d, fid);
983 spin_unlock(&d->opd_pre_lock);
986 CDEBUG(D_INODE, "fid for osp_object %p is "DFID"\n", o, PFID(fid));
988 /* If the precreate ends, it means it will be ready to rollover to
989 * the new sequence soon, all the creation should be synchronized,
990 * otherwise during replay, the replay fid will be inconsistent with
991 * last_used/create fid */
992 if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
996 * it's OK if the import is inactive by this moment - id was created
997 * by OST earlier, we just need to maintain it consistently on the disk
998 * once import is reconnected, OSP will claim this and other objects
999 * used and OST either keep them, if they exist or recreate
1002 /* we might have lost precreated objects */
1003 if (unlikely(d->opd_gap_count) > 0) {
1004 LASSERT(d->opd_pre != NULL);
1005 spin_lock(&d->opd_pre_lock);
1006 if (d->opd_gap_count > 0) {
1007 int count = d->opd_gap_count;
1009 ostid_set_id(&osi->osi_oi,
1010 fid_oid(&d->opd_gap_start_fid));
1011 d->opd_gap_count = 0;
1012 spin_unlock(&d->opd_pre_lock);
1014 CDEBUG(D_HA, "Writting gap "DFID"+%d in llog\n",
1015 PFID(&d->opd_gap_start_fid), count);
1016 /* real gap handling is disabled intil ORI-692 will be
1017 * fixed, now we only report gaps */
1019 spin_unlock(&d->opd_pre_lock);
1023 /* new object, the very first ->attr_set()
1024 * initializing attributes needs no logging */
1027 /* Only need update last_used oid file, seq file will only be update
1028 * during seq rollover */
1029 osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
1030 &d->opd_last_used_fid.f_oid, d->opd_index);
1032 rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
1035 CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
1036 d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
1041 int osp_declare_object_destroy(const struct lu_env *env,
1042 struct dt_object *dt, struct thandle *th)
1044 struct osp_object *o = dt2osp_obj(dt);
1050 * track objects to be destroyed via llog
1052 rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
1057 int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
1060 struct osp_object *o = dt2osp_obj(dt);
1065 o->opo_non_exist = 1;
1067 * once transaction is committed put proper command on
1068 * the queue going to our OST
1070 rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
1072 /* not needed in cache any more */
1073 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1078 struct dt_object_operations osp_obj_ops = {
1079 .do_declare_attr_get = osp_declare_attr_get,
1080 .do_attr_get = osp_attr_get,
1081 .do_declare_attr_set = osp_declare_attr_set,
1082 .do_attr_set = osp_attr_set,
1083 .do_declare_xattr_get = osp_declare_xattr_get,
1084 .do_xattr_get = osp_xattr_get,
1085 .do_declare_xattr_set = osp_declare_xattr_set,
1086 .do_xattr_set = osp_xattr_set,
1087 .do_declare_create = osp_declare_object_create,
1088 .do_create = osp_object_create,
1089 .do_declare_destroy = osp_declare_object_destroy,
1090 .do_destroy = osp_object_destroy,
1093 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
1094 const struct lu_object_conf *conf)
1096 struct osp_object *po = lu2osp_obj(o);
1100 spin_lock_init(&po->opo_lock);
1101 o->lo_header->loh_attr |= LOHA_REMOTE;
1103 if (is_ost_obj(o)) {
1104 po->opo_obj.do_ops = &osp_obj_ops;
1106 struct lu_attr *la = &osp_env_info(env)->osi_attr;
1108 po->opo_obj.do_ops = &osp_md_obj_ops;
1109 rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o),
1112 o->lo_header->loh_attr |=
1113 LOHA_EXISTS | (la->la_mode & S_IFMT);
1114 if (rc == -ENOENT) {
1115 po->opo_non_exist = 1;
1118 init_rwsem(&po->opo_sem);
1123 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
1125 struct osp_object *obj = lu2osp_obj(o);
1126 struct lu_object_header *h = o->lo_header;
1128 dt_object_fini(&obj->opo_obj);
1129 lu_object_header_fini(h);
1130 if (obj->opo_ooa != NULL) {
1131 struct osp_xattr_entry *oxe;
1132 struct osp_xattr_entry *tmp;
1135 list_for_each_entry_safe(oxe, tmp,
1136 &obj->opo_ooa->ooa_xattr_list,
1138 list_del(&oxe->oxe_list);
1139 count = atomic_read(&oxe->oxe_ref);
1140 LASSERTF(count == 1,
1141 "Still has %d users on the xattr entry %.*s\n",
1142 count - 1, oxe->oxe_namelen, oxe->oxe_buf);
1144 OBD_FREE(oxe, oxe->oxe_buflen);
1146 OBD_FREE_PTR(obj->opo_ooa);
1148 OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
1151 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
1153 struct osp_object *po = lu2osp_obj(o);
1154 struct osp_device *d = lu2osp_dev(o->lo_dev);
1159 * release reservation if object was declared but not created
1160 * this may require lu_object_put() in LOD
1162 if (unlikely(po->opo_reserved)) {
1163 LASSERT(d->opd_pre != NULL);
1164 LASSERT(d->opd_pre_reserved > 0);
1165 spin_lock(&d->opd_pre_lock);
1166 d->opd_pre_reserved--;
1167 spin_unlock(&d->opd_pre_lock);
1169 /* not needed in cache any more */
1170 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
1174 /* XXX: Currently, NOT cache OST-object on MDT because:
1175 * 1. it is not often accessed on MDT.
1176 * 2. avoid up layer (such as LFSCK) to load too many
1177 * once-used OST-objects. */
1178 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
1183 static int osp_object_print(const struct lu_env *env, void *cookie,
1184 lu_printer_t p, const struct lu_object *l)
1186 const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
1188 return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
1191 static int osp_object_invariant(const struct lu_object *o)
1196 struct lu_object_operations osp_lu_obj_ops = {
1197 .loo_object_init = osp_object_init,
1198 .loo_object_free = osp_object_free,
1199 .loo_object_release = osp_object_release,
1200 .loo_object_print = osp_object_print,
1201 .loo_object_invariant = osp_object_invariant