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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/osd-zfs/osd_object.c
34 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
35 * Author: Mike Pershin <tappro@whamcloud.com>
36 * Author: Johann Lombardi <johann@whamcloud.com>
39 #define DEBUG_SUBSYSTEM S_OSD
41 #include <lustre_ver.h>
42 #include <libcfs/libcfs.h>
43 #include <obd_support.h>
44 #include <lustre_net.h>
46 #include <obd_class.h>
47 #include <lustre_disk.h>
48 #include <lustre_fid.h>
50 #include "osd_internal.h"
52 #include <sys/dnode.h>
57 #include <sys/spa_impl.h>
58 #include <sys/zfs_znode.h>
59 #include <sys/dmu_tx.h>
60 #include <sys/dmu_objset.h>
61 #include <sys/dsl_prop.h>
62 #include <sys/sa_impl.h>
65 char *osd_obj_tag = "osd_object";
67 static struct dt_object_operations osd_obj_ops;
68 static struct lu_object_operations osd_lu_obj_ops;
69 extern struct dt_body_operations osd_body_ops;
70 static struct dt_object_operations osd_obj_otable_it_ops;
72 extern struct kmem_cache *osd_object_kmem;
75 osd_object_sa_fini(struct osd_object *obj)
78 sa_handle_destroy(obj->oo_sa_hdl);
79 obj->oo_sa_hdl = NULL;
84 osd_object_sa_init(struct osd_object *obj, struct osd_device *o)
88 LASSERT(obj->oo_sa_hdl == NULL);
89 LASSERT(obj->oo_db != NULL);
91 rc = -sa_handle_get(o->od_os, obj->oo_db->db_object, obj,
92 SA_HDL_PRIVATE, &obj->oo_sa_hdl);
96 /* Cache the xattr object id, valid for the life of the object */
97 rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_XATTR(o), &obj->oo_xattr, 8);
99 obj->oo_xattr = ZFS_NO_OBJECT;
102 osd_object_sa_fini(obj);
109 * Add object to list of dirty objects in tx handle.
112 osd_object_sa_dirty_add(struct osd_object *obj, struct osd_thandle *oh)
114 if (!list_empty(&obj->oo_sa_linkage))
117 down(&oh->ot_sa_lock);
118 write_lock(&obj->oo_attr_lock);
119 if (likely(list_empty(&obj->oo_sa_linkage)))
120 list_add(&obj->oo_sa_linkage, &oh->ot_sa_list);
121 write_unlock(&obj->oo_attr_lock);
126 * Release spill block dbuf hold for all dirty SAs.
128 void osd_object_sa_dirty_rele(struct osd_thandle *oh)
130 struct osd_object *obj;
132 down(&oh->ot_sa_lock);
133 while (!list_empty(&oh->ot_sa_list)) {
134 obj = list_entry(oh->ot_sa_list.next,
135 struct osd_object, oo_sa_linkage);
136 sa_spill_rele(obj->oo_sa_hdl);
137 write_lock(&obj->oo_attr_lock);
138 list_del_init(&obj->oo_sa_linkage);
139 write_unlock(&obj->oo_attr_lock);
145 * Update the SA and add the object to the dirty list.
147 int osd_object_sa_update(struct osd_object *obj, sa_attr_type_t type,
148 void *buf, uint32_t buflen, struct osd_thandle *oh)
152 LASSERT(obj->oo_sa_hdl != NULL);
153 LASSERT(oh->ot_tx != NULL);
155 rc = -sa_update(obj->oo_sa_hdl, type, buf, buflen, oh->ot_tx);
156 osd_object_sa_dirty_add(obj, oh);
162 * Bulk update the SA and add the object to the dirty list.
165 osd_object_sa_bulk_update(struct osd_object *obj, sa_bulk_attr_t *attrs,
166 int count, struct osd_thandle *oh)
170 LASSERT(obj->oo_sa_hdl != NULL);
171 LASSERT(oh->ot_tx != NULL);
173 rc = -sa_bulk_update(obj->oo_sa_hdl, attrs, count, oh->ot_tx);
174 osd_object_sa_dirty_add(obj, oh);
180 * Retrieve the attributes of a DMU object
182 int __osd_object_attr_get(const struct lu_env *env, struct osd_device *o,
183 struct osd_object *obj, struct lu_attr *la)
185 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
186 sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk;
192 LASSERT(obj->oo_db != NULL);
194 rc = -sa_handle_get(o->od_os, obj->oo_db->db_object, NULL,
195 SA_HDL_PRIVATE, &sa_hdl);
199 la->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE | LA_TYPE |
200 LA_SIZE | LA_UID | LA_GID | LA_FLAGS | LA_NLINK;
202 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(o), NULL, osa->atime, 16);
203 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(o), NULL, osa->mtime, 16);
204 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(o), NULL, osa->ctime, 16);
205 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(o), NULL, &osa->mode, 8);
206 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(o), NULL, &osa->size, 8);
207 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(o), NULL, &osa->nlink, 8);
208 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(o), NULL, &osa->uid, 8);
209 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(o), NULL, &osa->gid, 8);
210 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(o), NULL, &osa->flags, 8);
211 LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
213 rc = -sa_bulk_lookup(sa_hdl, bulk, cnt);
217 la->la_atime = osa->atime[0];
218 la->la_mtime = osa->mtime[0];
219 la->la_ctime = osa->ctime[0];
220 la->la_mode = osa->mode;
221 la->la_uid = osa->uid;
222 la->la_gid = osa->gid;
223 la->la_nlink = osa->nlink;
224 la->la_flags = attrs_zfs2fs(osa->flags);
225 la->la_size = osa->size;
227 /* Try to get extra flag from LMA. Right now, only LMAI_ORPHAN
228 * flags is stored in LMA, and it is only for orphan directory */
229 if (S_ISDIR(la->la_mode) && dt_object_exists(&obj->oo_dt)) {
230 struct osd_thread_info *info = osd_oti_get(env);
231 struct lustre_mdt_attrs *lma;
234 lma = (struct lustre_mdt_attrs *)info->oti_buf;
236 buf.lb_len = sizeof(info->oti_buf);
237 rc = osd_xattr_get(env, &obj->oo_dt, &buf, XATTR_NAME_LMA);
240 lma->lma_incompat = le32_to_cpu(lma->lma_incompat);
242 lma_to_lustre_flags(lma->lma_incompat);
244 } else if (rc == -ENODATA) {
249 if (S_ISCHR(la->la_mode) || S_ISBLK(la->la_mode)) {
250 rc = -sa_lookup(sa_hdl, SA_ZPL_RDEV(o), &osa->rdev, 8);
253 la->la_rdev = osa->rdev;
254 la->la_valid |= LA_RDEV;
257 sa_handle_destroy(sa_hdl);
262 int __osd_obj2dbuf(const struct lu_env *env, objset_t *os,
263 uint64_t oid, dmu_buf_t **dbp)
265 dmu_object_info_t *doi = &osd_oti_get(env)->oti_doi;
268 rc = -sa_buf_hold(os, oid, osd_obj_tag, dbp);
272 dmu_object_info_from_db(*dbp, doi);
273 if (unlikely (oid != DMU_USERUSED_OBJECT &&
274 oid != DMU_GROUPUSED_OBJECT && doi->doi_bonus_type != DMU_OT_SA)) {
275 sa_buf_rele(*dbp, osd_obj_tag);
281 LASSERT((*dbp)->db_object == oid);
282 LASSERT((*dbp)->db_offset == -1);
283 LASSERT((*dbp)->db_data != NULL);
289 * Concurrency: no concurrent access is possible that early in object
292 struct lu_object *osd_object_alloc(const struct lu_env *env,
293 const struct lu_object_header *hdr,
296 struct osd_object *mo;
298 OBD_SLAB_ALLOC_PTR_GFP(mo, osd_object_kmem, GFP_NOFS);
302 l = &mo->oo_dt.do_lu;
303 dt_object_init(&mo->oo_dt, NULL, d);
304 mo->oo_dt.do_ops = &osd_obj_ops;
305 l->lo_ops = &osd_lu_obj_ops;
306 INIT_LIST_HEAD(&mo->oo_sa_linkage);
307 INIT_LIST_HEAD(&mo->oo_unlinked_linkage);
308 init_rwsem(&mo->oo_sem);
309 init_rwsem(&mo->oo_guard);
310 rwlock_init(&mo->oo_attr_lock);
311 mo->oo_destroy = OSD_DESTROY_NONE;
319 * Concurrency: shouldn't matter.
321 int osd_object_init0(const struct lu_env *env, struct osd_object *obj)
323 struct osd_device *osd = osd_obj2dev(obj);
324 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
328 if (obj->oo_db == NULL)
333 rc = osd_object_sa_init(obj, osd);
337 /* cache attrs in object */
338 rc = __osd_object_attr_get(env, osd, obj, &obj->oo_attr);
342 if (likely(!fid_is_acct(fid)))
343 /* no body operations for accounting objects */
344 obj->oo_dt.do_body_ops = &osd_body_ops;
347 * initialize object before marking it existing
349 obj->oo_dt.do_lu.lo_header->loh_attr |= obj->oo_attr.la_mode & S_IFMT;
352 obj->oo_dt.do_lu.lo_header->loh_attr |= LOHA_EXISTS;
357 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
359 struct osd_thread_info *info = osd_oti_get(env);
362 struct lustre_mdt_attrs *lma;
365 CLASSERT(sizeof(info->oti_buf) >= sizeof(*lma));
366 lma = (struct lustre_mdt_attrs *)info->oti_buf;
368 buf.lb_len = sizeof(info->oti_buf);
370 rc = osd_xattr_get(env, &obj->oo_dt, &buf, XATTR_NAME_LMA);
373 lustre_lma_swab(lma);
374 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
375 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
376 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
377 "fid = "DFID"\n", osd_obj2dev(obj)->od_svname,
378 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
379 PFID(lu_object_fid(&obj->oo_dt.do_lu)));
382 } else if (rc == -ENODATA) {
383 /* haven't initialize LMA xattr */
391 * Concurrency: no concurrent access is possible that early in object
394 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
395 const struct lu_object_conf *conf)
397 struct osd_object *obj = osd_obj(l);
398 struct osd_device *osd = osd_obj2dev(obj);
403 LASSERT(osd_invariant(obj));
405 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
406 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
407 l->lo_header->loh_attr |= LOHA_EXISTS;
411 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
414 rc = osd_fid_lookup(env, osd, lu_object_fid(l), &oid);
416 LASSERT(obj->oo_db == NULL);
417 rc = __osd_obj2dbuf(env, osd->od_os, oid, &obj->oo_db);
418 /* EEXIST will be returned if object is being deleted in ZFS */
424 CERROR("%s: lookup "DFID"/%#llx failed: rc = %d\n",
425 osd->od_svname, PFID(lu_object_fid(l)), oid, rc);
429 rc = osd_object_init0(env, obj);
433 rc = osd_check_lma(env, obj);
436 } else if (rc == -ENOENT) {
439 LASSERT(osd_invariant(obj));
445 * Concurrency: no concurrent access is possible that late in object
448 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
450 struct osd_object *obj = osd_obj(l);
452 LASSERT(osd_invariant(obj));
454 dt_object_fini(&obj->oo_dt);
455 OBD_SLAB_FREE_PTR(obj, osd_object_kmem);
459 osd_object_unlinked_add(struct osd_object *obj, struct osd_thandle *oh)
463 LASSERT(obj->oo_destroy == OSD_DESTROY_ASYNC);
465 /* the object is supposed to be exclusively locked by
466 * the caller (osd_object_destroy()), while the transaction
467 * (oh) is per-thread and not shared */
468 if (likely(list_empty(&obj->oo_unlinked_linkage))) {
469 list_add(&obj->oo_unlinked_linkage, &oh->ot_unlinked_list);
476 /* Default to max data size covered by a level-1 indirect block */
477 static unsigned long osd_sync_destroy_max_size =
478 1UL << (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT + SPA_MAXBLOCKSHIFT);
479 module_param(osd_sync_destroy_max_size, ulong, 0444);
480 MODULE_PARM_DESC(osd_sync_destroy_max_size, "Maximum object size to use synchronous destroy.");
483 osd_object_set_destroy_type(struct osd_object *obj)
486 * Lock-less OST_WRITE can race with OST_DESTROY, so set destroy type
487 * only once and use it consistently thereafter.
489 down_write(&obj->oo_guard);
490 if (obj->oo_destroy == OSD_DESTROY_NONE) {
491 if (obj->oo_attr.la_size <= osd_sync_destroy_max_size)
492 obj->oo_destroy = OSD_DESTROY_SYNC;
493 else /* Larger objects are destroyed asynchronously */
494 obj->oo_destroy = OSD_DESTROY_ASYNC;
496 up_write(&obj->oo_guard);
499 static int osd_declare_object_destroy(const struct lu_env *env,
500 struct dt_object *dt,
503 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
504 struct osd_object *obj = osd_dt_obj(dt);
505 struct osd_device *osd = osd_obj2dev(obj);
506 struct osd_thandle *oh;
512 LASSERT(dt_object_exists(dt));
514 oh = container_of0(th, struct osd_thandle, ot_super);
515 LASSERT(oh->ot_tx != NULL);
517 /* declare that we'll remove object from fid-dnode mapping */
518 zapid = osd_get_name_n_idx(env, osd, fid, NULL, 0);
519 dmu_tx_hold_zap(oh->ot_tx, zapid, FALSE, NULL);
521 osd_declare_xattrs_destroy(env, obj, oh);
523 /* declare that we'll remove object from inode accounting ZAPs */
524 dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, FALSE, NULL);
525 dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, FALSE, NULL);
528 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
529 obj->oo_attr.la_gid, -1, oh, false, NULL, false);
533 /* data to be truncated */
534 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
535 obj->oo_attr.la_gid, 0, oh, true, NULL, false);
539 osd_object_set_destroy_type(obj);
540 if (obj->oo_destroy == OSD_DESTROY_SYNC)
541 dmu_tx_hold_free(oh->ot_tx, obj->oo_db->db_object,
544 dmu_tx_hold_zap(oh->ot_tx, osd->od_unlinkedid, TRUE, NULL);
546 /* will help to find FID->ino when this object is being
547 * added to PENDING/ */
548 osd_idc_find_and_init(env, osd, obj);
553 static int osd_object_destroy(const struct lu_env *env,
554 struct dt_object *dt, struct thandle *th)
556 struct osd_thread_info *info = osd_oti_get(env);
557 char *buf = info->oti_str;
558 struct osd_object *obj = osd_dt_obj(dt);
559 struct osd_device *osd = osd_obj2dev(obj);
560 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
561 struct osd_thandle *oh;
566 down_write(&obj->oo_guard);
568 if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
569 GOTO(out, rc = -ENOENT);
571 LASSERT(obj->oo_db != NULL);
573 oh = container_of0(th, struct osd_thandle, ot_super);
575 LASSERT(oh->ot_tx != NULL);
577 /* remove obj ref from index dir (it depends) */
578 zapid = osd_get_name_n_idx(env, osd, fid, buf, sizeof(info->oti_str));
579 rc = -zap_remove(osd->od_os, zapid, buf, oh->ot_tx);
581 CERROR("%s: zap_remove(%s) failed: rc = %d\n",
582 osd->od_svname, buf, rc);
586 rc = osd_xattrs_destroy(env, obj, oh);
588 CERROR("%s: cannot destroy xattrs for %s: rc = %d\n",
589 osd->od_svname, buf, rc);
593 /* Remove object from inode accounting. It is not fatal for the destroy
594 * operation if something goes wrong while updating accounting, but we
595 * still log an error message to notify the administrator */
596 rc = -zap_increment_int(osd->od_os, osd->od_iusr_oid,
597 obj->oo_attr.la_uid, -1, oh->ot_tx);
599 CERROR("%s: failed to remove "DFID" from accounting ZAP for usr"
600 " %d: rc = %d\n", osd->od_svname, PFID(fid),
601 obj->oo_attr.la_uid, rc);
602 rc = -zap_increment_int(osd->od_os, osd->od_igrp_oid,
603 obj->oo_attr.la_gid, -1, oh->ot_tx);
605 CERROR("%s: failed to remove "DFID" from accounting ZAP for grp"
606 " %d: rc = %d\n", osd->od_svname, PFID(fid),
607 obj->oo_attr.la_gid, rc);
609 oid = obj->oo_db->db_object;
610 if (unlikely(obj->oo_destroy == OSD_DESTROY_NONE)) {
611 /* this may happen if the destroy wasn't declared
612 * e.g. when the object is created and then destroyed
613 * in the same transaction - we don't need additional
614 * space for destroy specifically */
615 LASSERT(obj->oo_attr.la_size <= osd_sync_destroy_max_size);
616 rc = -dmu_object_free(osd->od_os, oid, oh->ot_tx);
618 CERROR("%s: failed to free %s %llu: rc = %d\n",
619 osd->od_svname, buf, oid, rc);
620 } else if (obj->oo_destroy == OSD_DESTROY_SYNC) {
621 rc = -dmu_object_free(osd->od_os, oid, oh->ot_tx);
623 CERROR("%s: failed to free %s %llu: rc = %d\n",
624 osd->od_svname, buf, oid, rc);
625 } else { /* asynchronous destroy */
626 rc = osd_object_unlinked_add(obj, oh);
630 rc = -zap_add_int(osd->od_os, osd->od_unlinkedid,
633 CERROR("%s: zap_add_int() failed %s %llu: rc = %d\n",
634 osd->od_svname, buf, oid, rc);
638 /* not needed in the cache anymore */
639 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
641 obj->oo_destroyed = 1;
642 up_write(&obj->oo_guard);
646 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
648 struct osd_object *obj = osd_obj(l);
650 if (obj->oo_db != NULL) {
651 osd_object_sa_fini(obj);
652 if (obj->oo_sa_xattr) {
653 nvlist_free(obj->oo_sa_xattr);
654 obj->oo_sa_xattr = NULL;
656 sa_buf_rele(obj->oo_db, osd_obj_tag);
657 list_del(&obj->oo_sa_linkage);
663 * Concurrency: ->loo_object_release() is called under site spin-lock.
665 static void osd_object_release(const struct lu_env *env,
671 * Concurrency: shouldn't matter.
673 static int osd_object_print(const struct lu_env *env, void *cookie,
674 lu_printer_t p, const struct lu_object *l)
676 struct osd_object *o = osd_obj(l);
678 return (*p)(env, cookie, LUSTRE_OSD_ZFS_NAME"-object@%p", o);
681 static void osd_object_read_lock(const struct lu_env *env,
682 struct dt_object *dt, unsigned role)
684 struct osd_object *obj = osd_dt_obj(dt);
686 LASSERT(osd_invariant(obj));
688 down_read_nested(&obj->oo_sem, role);
691 static void osd_object_write_lock(const struct lu_env *env,
692 struct dt_object *dt, unsigned role)
694 struct osd_object *obj = osd_dt_obj(dt);
696 LASSERT(osd_invariant(obj));
698 down_write_nested(&obj->oo_sem, role);
701 static void osd_object_read_unlock(const struct lu_env *env,
702 struct dt_object *dt)
704 struct osd_object *obj = osd_dt_obj(dt);
706 LASSERT(osd_invariant(obj));
707 up_read(&obj->oo_sem);
710 static void osd_object_write_unlock(const struct lu_env *env,
711 struct dt_object *dt)
713 struct osd_object *obj = osd_dt_obj(dt);
715 LASSERT(osd_invariant(obj));
716 up_write(&obj->oo_sem);
719 static int osd_object_write_locked(const struct lu_env *env,
720 struct dt_object *dt)
722 struct osd_object *obj = osd_dt_obj(dt);
725 LASSERT(osd_invariant(obj));
727 if (down_write_trylock(&obj->oo_sem)) {
729 up_write(&obj->oo_sem);
734 static int osd_attr_get(const struct lu_env *env,
735 struct dt_object *dt,
736 struct lu_attr *attr)
738 struct osd_object *obj = osd_dt_obj(dt);
743 down_read(&obj->oo_guard);
745 if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
746 GOTO(out, rc = -ENOENT);
748 LASSERT(osd_invariant(obj));
751 read_lock(&obj->oo_attr_lock);
752 *attr = obj->oo_attr;
753 if (obj->oo_lma_flags & LUSTRE_ORPHAN_FL)
754 attr->la_flags |= LUSTRE_ORPHAN_FL;
755 read_unlock(&obj->oo_attr_lock);
757 /* with ZFS_DEBUG zrl_add_debug() called by DB_DNODE_ENTER()
758 * from within sa_object_size() can block on a mutex, so
759 * we can't call sa_object_size() holding rwlock */
760 sa_object_size(obj->oo_sa_hdl, &blksize, &blocks);
761 /* we do not control size of indices, so always calculate
762 * it from number of blocks reported by DMU */
763 if (S_ISDIR(attr->la_mode))
764 attr->la_size = 512 * blocks;
765 /* Block size may be not set; suggest maximal I/O transfers. */
767 blksize = osd_spa_maxblocksize(
768 dmu_objset_spa(osd_obj2dev(obj)->od_os));
770 attr->la_blksize = blksize;
771 attr->la_blocks = blocks;
772 attr->la_valid |= LA_BLOCKS | LA_BLKSIZE;
775 up_read(&obj->oo_guard);
779 /* Simple wrapper on top of qsd API which implement quota transfer for osd
780 * setattr needs. As a reminder, only the root user can change ownership of
781 * a file, that's why EDQUOT & EINPROGRESS errors are discarded */
782 static inline int qsd_transfer(const struct lu_env *env,
783 struct qsd_instance *qsd,
784 struct lquota_trans *trans, int qtype,
785 __u64 orig_id, __u64 new_id, __u64 bspace,
786 struct lquota_id_info *qi)
790 if (unlikely(qsd == NULL))
793 LASSERT(qtype >= 0 && qtype < LL_MAXQUOTAS);
794 qi->lqi_type = qtype;
796 /* inode accounting */
797 qi->lqi_is_blk = false;
799 /* one more inode for the new owner ... */
800 qi->lqi_id.qid_uid = new_id;
802 rc = qsd_op_begin(env, qsd, trans, qi, NULL);
803 if (rc == -EDQUOT || rc == -EINPROGRESS)
808 /* and one less inode for the current id */
809 qi->lqi_id.qid_uid = orig_id;;
811 /* can't get EDQUOT when reducing usage */
812 rc = qsd_op_begin(env, qsd, trans, qi, NULL);
813 if (rc == -EINPROGRESS)
818 /* block accounting */
819 qi->lqi_is_blk = true;
821 /* more blocks for the new owner ... */
822 qi->lqi_id.qid_uid = new_id;
823 qi->lqi_space = bspace;
824 rc = qsd_op_begin(env, qsd, trans, qi, NULL);
825 if (rc == -EDQUOT || rc == -EINPROGRESS)
830 /* and finally less blocks for the current owner */
831 qi->lqi_id.qid_uid = orig_id;
832 qi->lqi_space = -bspace;
833 rc = qsd_op_begin(env, qsd, trans, qi, NULL);
834 /* can't get EDQUOT when reducing usage */
835 if (rc == -EINPROGRESS)
840 static int osd_declare_attr_set(const struct lu_env *env,
841 struct dt_object *dt,
842 const struct lu_attr *attr,
843 struct thandle *handle)
845 struct osd_thread_info *info = osd_oti_get(env);
846 struct osd_object *obj = osd_dt_obj(dt);
847 struct osd_device *osd = osd_obj2dev(obj);
849 struct osd_thandle *oh;
857 LASSERT(handle != NULL);
858 LASSERT(osd_invariant(obj));
860 oh = container_of0(handle, struct osd_thandle, ot_super);
862 down_read(&obj->oo_guard);
863 if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
866 LASSERT(obj->oo_sa_hdl != NULL);
867 LASSERT(oh->ot_tx != NULL);
868 /* regular attributes are part of the bonus buffer */
869 /* let's check whether this object is already part of
872 for (txh = list_head(&oh->ot_tx->tx_holds); txh;
873 txh = list_next(&oh->ot_tx->tx_holds, txh)) {
874 if (txh->txh_dnode == NULL)
876 if (txh->txh_dnode->dn_object != obj->oo_db->db_object)
878 /* this object is part of the transaction already
879 * we don't need to declare bonus again */
884 dmu_tx_hold_bonus(oh->ot_tx, obj->oo_db->db_object);
885 if (oh->ot_tx->tx_err != 0)
886 GOTO(out, rc = -oh->ot_tx->tx_err);
888 if (attr && attr->la_valid & LA_FLAGS) {
889 /* LMA is usually a part of bonus, no need to declare
893 if (attr && (attr->la_valid & (LA_UID | LA_GID))) {
894 sa_object_size(obj->oo_sa_hdl, &blksize, &bspace);
895 bspace = toqb(bspace * blksize);
898 if (attr && attr->la_valid & LA_UID) {
899 /* account for user inode tracking ZAP update */
900 dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, FALSE, NULL);
902 /* quota enforcement for user */
903 if (attr->la_uid != obj->oo_attr.la_uid) {
904 rc = qsd_transfer(env, osd->od_quota_slave,
905 &oh->ot_quota_trans, USRQUOTA,
906 obj->oo_attr.la_uid, attr->la_uid,
907 bspace, &info->oti_qi);
912 if (attr && attr->la_valid & LA_GID) {
913 /* account for user inode tracking ZAP update */
914 dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, FALSE, NULL);
916 /* quota enforcement for group */
917 if (attr->la_gid != obj->oo_attr.la_gid) {
918 rc = qsd_transfer(env, osd->od_quota_slave,
919 &oh->ot_quota_trans, GRPQUOTA,
920 obj->oo_attr.la_gid, attr->la_gid,
921 bspace, &info->oti_qi);
928 up_read(&obj->oo_guard);
933 * Set the attributes of an object
935 * The transaction passed to this routine must have
936 * dmu_tx_hold_bonus(tx, oid) called and then assigned
937 * to a transaction group.
939 static int osd_attr_set(const struct lu_env *env, struct dt_object *dt,
940 const struct lu_attr *la, struct thandle *handle)
942 struct osd_thread_info *info = osd_oti_get(env);
943 sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk;
944 struct osd_object *obj = osd_dt_obj(dt);
945 struct osd_device *osd = osd_obj2dev(obj);
946 struct osd_thandle *oh;
947 struct osa_attr *osa = &info->oti_osa;
948 __u64 valid = la->la_valid;
954 down_read(&obj->oo_guard);
955 if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
956 GOTO(out, rc = -ENOENT);
958 LASSERT(handle != NULL);
959 LASSERT(osd_invariant(obj));
960 LASSERT(obj->oo_sa_hdl);
962 oh = container_of0(handle, struct osd_thandle, ot_super);
963 /* Assert that the transaction has been assigned to a
964 transaction group. */
965 LASSERT(oh->ot_tx->tx_txg != 0);
967 /* Only allow set size for regular file */
968 if (!S_ISREG(dt->do_lu.lo_header->loh_attr))
969 valid &= ~(LA_SIZE | LA_BLOCKS);
971 if (valid & LA_CTIME && la->la_ctime == obj->oo_attr.la_ctime)
974 if (valid & LA_MTIME && la->la_mtime == obj->oo_attr.la_mtime)
977 if (valid & LA_ATIME && la->la_atime == obj->oo_attr.la_atime)
983 if (valid & LA_FLAGS) {
984 struct lustre_mdt_attrs *lma;
987 if (la->la_flags & LUSTRE_LMA_FL_MASKS) {
988 CLASSERT(sizeof(info->oti_buf) >= sizeof(*lma));
989 lma = (struct lustre_mdt_attrs *)&info->oti_buf;
991 buf.lb_len = sizeof(info->oti_buf);
992 rc = osd_xattr_get(env, &obj->oo_dt, &buf,
996 le32_to_cpu(lma->lma_incompat);
998 lustre_to_lma_flags(la->la_flags);
1000 cpu_to_le32(lma->lma_incompat);
1002 buf.lb_len = sizeof(*lma);
1003 rc = osd_xattr_set_internal(env, obj, &buf,
1009 CWARN("%s: failed to set LMA flags: rc = %d\n",
1010 osd->od_svname, rc);
1016 /* do both accounting updates outside oo_attr_lock below */
1017 if ((valid & LA_UID) && (la->la_uid != obj->oo_attr.la_uid)) {
1018 /* Update user accounting. Failure isn't fatal, but we still
1019 * log an error message */
1020 rc = -zap_increment_int(osd->od_os, osd->od_iusr_oid,
1021 la->la_uid, 1, oh->ot_tx);
1023 CERROR("%s: failed to update accounting ZAP for user "
1024 "%d (%d)\n", osd->od_svname, la->la_uid, rc);
1025 rc = -zap_increment_int(osd->od_os, osd->od_iusr_oid,
1026 obj->oo_attr.la_uid, -1, oh->ot_tx);
1028 CERROR("%s: failed to update accounting ZAP for user "
1029 "%d (%d)\n", osd->od_svname,
1030 obj->oo_attr.la_uid, rc);
1032 if ((valid & LA_GID) && (la->la_gid != obj->oo_attr.la_gid)) {
1033 /* Update group accounting. Failure isn't fatal, but we still
1034 * log an error message */
1035 rc = -zap_increment_int(osd->od_os, osd->od_igrp_oid,
1036 la->la_gid, 1, oh->ot_tx);
1038 CERROR("%s: failed to update accounting ZAP for user "
1039 "%d (%d)\n", osd->od_svname, la->la_gid, rc);
1040 rc = -zap_increment_int(osd->od_os, osd->od_igrp_oid,
1041 obj->oo_attr.la_gid, -1, oh->ot_tx);
1043 CERROR("%s: failed to update accounting ZAP for user "
1044 "%d (%d)\n", osd->od_svname,
1045 obj->oo_attr.la_gid, rc);
1048 write_lock(&obj->oo_attr_lock);
1050 if (valid & LA_ATIME) {
1051 osa->atime[0] = obj->oo_attr.la_atime = la->la_atime;
1052 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL,
1055 if (valid & LA_MTIME) {
1056 osa->mtime[0] = obj->oo_attr.la_mtime = la->la_mtime;
1057 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL,
1060 if (valid & LA_CTIME) {
1061 osa->ctime[0] = obj->oo_attr.la_ctime = la->la_ctime;
1062 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL,
1065 if (valid & LA_MODE) {
1066 /* mode is stored along with type, so read it first */
1067 obj->oo_attr.la_mode = (obj->oo_attr.la_mode & S_IFMT) |
1068 (la->la_mode & ~S_IFMT);
1069 osa->mode = obj->oo_attr.la_mode;
1070 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL,
1073 if (valid & LA_SIZE) {
1074 osa->size = obj->oo_attr.la_size = la->la_size;
1075 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL,
1078 if (valid & LA_NLINK) {
1079 osa->nlink = obj->oo_attr.la_nlink = la->la_nlink;
1080 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL,
1083 if (valid & LA_RDEV) {
1084 osa->rdev = obj->oo_attr.la_rdev = la->la_rdev;
1085 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL,
1088 if (valid & LA_FLAGS) {
1089 osa->flags = attrs_fs2zfs(la->la_flags);
1090 /* many flags are not supported by zfs, so ensure a good cached
1092 obj->oo_attr.la_flags = attrs_zfs2fs(osa->flags);
1093 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL,
1096 if (valid & LA_UID) {
1097 osa->uid = obj->oo_attr.la_uid = la->la_uid;
1098 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL,
1101 if (valid & LA_GID) {
1102 osa->gid = obj->oo_attr.la_gid = la->la_gid;
1103 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL,
1106 obj->oo_attr.la_valid |= valid;
1107 write_unlock(&obj->oo_attr_lock);
1109 LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
1110 rc = osd_object_sa_bulk_update(obj, bulk, cnt, oh);
1113 up_read(&obj->oo_guard);
1120 * XXX temporary solution.
1123 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1124 struct dt_object *parent, struct dt_object *child,
1129 ah->dah_parent = parent;
1130 ah->dah_mode = child_mode;
1132 if (parent != NULL && !dt_object_remote(parent)) {
1133 /* will help to find FID->ino at dt_insert("..") */
1134 struct osd_object *pobj = osd_dt_obj(parent);
1136 osd_idc_find_and_init(env, osd_obj2dev(pobj), pobj);
1140 static int osd_declare_object_create(const struct lu_env *env,
1141 struct dt_object *dt,
1142 struct lu_attr *attr,
1143 struct dt_allocation_hint *hint,
1144 struct dt_object_format *dof,
1145 struct thandle *handle)
1147 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1148 struct osd_object *obj = osd_dt_obj(dt);
1149 struct osd_device *osd = osd_obj2dev(obj);
1150 struct osd_thandle *oh;
1157 switch (dof->dof_type) {
1161 if (obj->oo_dt.do_body_ops == NULL)
1162 obj->oo_dt.do_body_ops = &osd_body_ops;
1168 LASSERT(handle != NULL);
1169 oh = container_of0(handle, struct osd_thandle, ot_super);
1170 LASSERT(oh->ot_tx != NULL);
1172 /* this is the minimum set of EAs on every Lustre object */
1173 obj->oo_ea_in_bonus = ZFS_SA_BASE_ATTR_SIZE +
1174 sizeof(__u64) + /* VBR VERSION */
1175 sizeof(struct lustre_mdt_attrs); /* LMA */
1176 /* reserve 32 bytes for extra stuff like ACLs */
1177 dnode_size = size_roundup_power2(obj->oo_ea_in_bonus + 32);
1179 switch (dof->dof_type) {
1181 dt->do_index_ops = &osd_dir_ops;
1183 /* for zap create */
1184 dmu_tx_hold_zap(oh->ot_tx, DMU_NEW_OBJECT, FALSE, NULL);
1185 dmu_tx_hold_sa_create(oh->ot_tx, dnode_size);
1190 /* first, we'll create new object */
1191 dmu_tx_hold_sa_create(oh->ot_tx, dnode_size);
1199 /* and we'll add it to some mapping */
1200 zapid = osd_get_name_n_idx(env, osd, fid, NULL, 0);
1201 dmu_tx_hold_zap(oh->ot_tx, zapid, TRUE, NULL);
1203 /* we will also update inode accounting ZAPs */
1204 dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, FALSE, NULL);
1205 dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, FALSE, NULL);
1207 /* will help to find FID->ino mapping at dt_insert() */
1208 osd_idc_find_and_init(env, osd, obj);
1210 rc = osd_declare_quota(env, osd, attr->la_uid, attr->la_gid, 1, oh,
1211 false, NULL, false);
1216 int __osd_attr_init(const struct lu_env *env, struct osd_device *osd,
1217 sa_handle_t *sa_hdl, dmu_tx_t *tx,
1218 struct lu_attr *la, uint64_t parent)
1220 sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk;
1221 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
1230 gen = dmu_tx_get_txg(tx);
1232 ZFS_TIME_ENCODE(&now, crtime);
1234 osa->atime[0] = la->la_atime;
1235 osa->ctime[0] = la->la_ctime;
1236 osa->mtime[0] = la->la_mtime;
1237 osa->mode = la->la_mode;
1238 osa->uid = la->la_uid;
1239 osa->gid = la->la_gid;
1240 osa->rdev = la->la_rdev;
1241 osa->nlink = la->la_nlink;
1242 osa->flags = attrs_fs2zfs(la->la_flags);
1243 osa->size = la->la_size;
1246 * we need to create all SA below upon object create.
1248 * XXX The attribute order matters since the accounting callback relies
1249 * on static offsets (i.e. SA_*_OFFSET, see zfs_space_delta_cb()) to
1250 * look up the UID/GID attributes. Moreover, the callback does not seem
1251 * to support the spill block.
1252 * We define attributes in the same order as SA_*_OFFSET in order to
1253 * work around the problem. See ORI-610.
1256 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL, &osa->mode, 8);
1257 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL, &osa->size, 8);
1258 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(osd), NULL, &gen, 8);
1259 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL, &osa->uid, 8);
1260 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL, &osa->gid, 8);
1261 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(osd), NULL, &parent, 8);
1262 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL, &osa->flags, 8);
1263 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL, osa->atime, 16);
1264 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL, osa->mtime, 16);
1265 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL, osa->ctime, 16);
1266 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, crtime, 16);
1267 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL, &osa->nlink, 8);
1268 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL, &osa->rdev, 8);
1269 LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
1271 rc = -sa_replace_all_by_template(sa_hdl, bulk, cnt, tx);
1277 * The transaction passed to this routine must have
1278 * dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT) called and then assigned
1279 * to a transaction group.
1281 int __osd_object_create(const struct lu_env *env, struct osd_object *obj,
1282 dmu_buf_t **dbp, dmu_tx_t *tx, struct lu_attr *la)
1286 struct osd_device *osd = osd_obj2dev(obj);
1287 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
1288 dmu_object_type_t type = DMU_OT_PLAIN_FILE_CONTENTS;
1290 /* Use DMU_OTN_UINT8_METADATA for local objects so their data blocks
1291 * would get an additional ditto copy */
1292 if (unlikely(S_ISREG(la->la_mode) &&
1293 fid_seq_is_local_file(fid_seq(fid))))
1294 type = DMU_OTN_UINT8_METADATA;
1296 /* Create a new DMU object using the default dnode size. */
1297 oid = osd_dmu_object_alloc(osd->od_os, type, 0, 0, tx);
1298 rc = -sa_buf_hold(osd->od_os, oid, osd_obj_tag, dbp);
1299 LASSERTF(rc == 0, "sa_buf_hold %llu failed: %d\n", oid, rc);
1301 LASSERT(la->la_valid & LA_MODE);
1309 * The transaction passed to this routine must have
1310 * dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, ...) called and then assigned
1311 * to a transaction group.
1313 * Using ZAP_FLAG_HASH64 will force the ZAP to always be a FAT ZAP.
1314 * This is fine for directories today, because storing the FID in the dirent
1315 * will also require a FAT ZAP. If there is a new type of micro ZAP created
1316 * then we might need to re-evaluate the use of this flag and instead do
1317 * a conversion from the different internal ZAP hash formats being used. */
1318 int __osd_zap_create(const struct lu_env *env, struct osd_device *osd,
1319 dmu_buf_t **zap_dbp, dmu_tx_t *tx,
1320 struct lu_attr *la, zap_flags_t flags)
1325 /* Assert that the transaction has been assigned to a
1326 transaction group. */
1327 LASSERT(tx->tx_txg != 0);
1329 oid = osd_zap_create_flags(osd->od_os, 0, flags | ZAP_FLAG_HASH64,
1330 DMU_OT_DIRECTORY_CONTENTS,
1331 14, /* == ZFS fzap_default_blockshift */
1332 DN_MAX_INDBLKSHIFT, /* indirect blockshift */
1335 rc = -sa_buf_hold(osd->od_os, oid, osd_obj_tag, zap_dbp);
1345 static dmu_buf_t *osd_mkidx(const struct lu_env *env, struct osd_object *obj,
1346 struct lu_attr *la, struct osd_thandle *oh)
1351 /* Index file should be created as regular file in order not to confuse
1352 * ZPL which could interpret them as directory.
1353 * We set ZAP_FLAG_UINT64_KEY to let ZFS know than we are going to use
1355 LASSERT(S_ISREG(la->la_mode));
1356 rc = __osd_zap_create(env, osd_obj2dev(obj), &db, oh->ot_tx, la,
1357 ZAP_FLAG_UINT64_KEY);
1363 static dmu_buf_t *osd_mkdir(const struct lu_env *env, struct osd_object *obj,
1364 struct lu_attr *la, struct osd_thandle *oh)
1369 LASSERT(S_ISDIR(la->la_mode));
1370 rc = __osd_zap_create(env, osd_obj2dev(obj), &db, oh->ot_tx, la, 0);
1376 static dmu_buf_t *osd_mkreg(const struct lu_env *env, struct osd_object *obj,
1377 struct lu_attr *la, struct osd_thandle *oh)
1379 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
1382 struct osd_device *osd = osd_obj2dev(obj);
1384 LASSERT(S_ISREG(la->la_mode));
1385 rc = __osd_object_create(env, obj, &db, oh->ot_tx, la);
1389 if ((fid_is_idif(fid) || fid_is_norm(fid) || fid_is_echo(fid)) &&
1391 /* The minimum block size must be at least page size otherwise
1392 * it will break the assumption in tgt_thread_big_cache where
1393 * the array size is PTLRPC_MAX_BRW_PAGES. It will also affect
1394 * RDMA due to subpage transfer size */
1395 rc = -dmu_object_set_blocksize(osd->od_os, db->db_object,
1396 PAGE_SIZE, 0, oh->ot_tx);
1398 CERROR("%s: can't change blocksize: %d\n",
1399 osd->od_svname, rc);
1407 static dmu_buf_t *osd_mksym(const struct lu_env *env, struct osd_object *obj,
1408 struct lu_attr *la, struct osd_thandle *oh)
1413 LASSERT(S_ISLNK(la->la_mode));
1414 rc = __osd_object_create(env, obj, &db, oh->ot_tx, la);
1420 static dmu_buf_t *osd_mknod(const struct lu_env *env, struct osd_object *obj,
1421 struct lu_attr *la, struct osd_thandle *oh)
1426 if (S_ISCHR(la->la_mode) || S_ISBLK(la->la_mode))
1427 la->la_valid |= LA_RDEV;
1429 rc = __osd_object_create(env, obj, &db, oh->ot_tx, la);
1435 typedef dmu_buf_t *(*osd_obj_type_f)(const struct lu_env *env,
1436 struct osd_object *obj,
1438 struct osd_thandle *oh);
1440 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1442 osd_obj_type_f result;
1468 * Concurrency: @dt is write locked.
1470 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1471 struct lu_attr *attr,
1472 struct dt_allocation_hint *hint,
1473 struct dt_object_format *dof,
1476 struct osd_thread_info *info = osd_oti_get(env);
1477 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
1478 struct zpl_direntry *zde = &info->oti_zde.lzd_reg;
1479 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1480 struct osd_object *obj = osd_dt_obj(dt);
1481 struct osd_device *osd = osd_obj2dev(obj);
1482 char *buf = info->oti_str;
1483 struct osd_thandle *oh;
1484 dmu_buf_t *db = NULL;
1485 uint64_t zapid, parent = 0;
1490 /* concurrent create declarations should not see
1491 * the object inconsistent (db, attr, etc).
1492 * in regular cases acquisition should be cheap */
1493 down_write(&obj->oo_guard);
1495 if (unlikely(dt_object_exists(dt)))
1496 GOTO(out, rc = -EEXIST);
1498 LASSERT(osd_invariant(obj));
1499 LASSERT(dof != NULL);
1501 LASSERT(th != NULL);
1502 oh = container_of0(th, struct osd_thandle, ot_super);
1505 * XXX missing: Quote handling.
1508 LASSERT(obj->oo_db == NULL);
1510 /* to follow ZFS on-disk format we need
1511 * to initialize parent dnode properly */
1512 if (hint != NULL && hint->dah_parent != NULL &&
1513 !dt_object_remote(hint->dah_parent))
1514 parent = osd_dt_obj(hint->dah_parent)->oo_db->db_object;
1516 /* we may fix some attributes, better do not change the source */
1517 obj->oo_attr = *attr;
1518 obj->oo_attr.la_valid |= LA_SIZE | LA_NLINK | LA_TYPE;
1520 db = osd_create_type_f(dof->dof_type)(env, obj, &obj->oo_attr, oh);
1528 zde->zde_dnode = db->db_object;
1529 zde->zde_type = IFTODT(attr->la_mode & S_IFMT);
1531 zapid = osd_get_name_n_idx(env, osd, fid, buf, sizeof(info->oti_str));
1533 rc = -zap_add(osd->od_os, zapid, buf, 8, 1, zde, oh->ot_tx);
1537 /* Now add in all of the "SA" attributes */
1538 rc = -sa_handle_get(osd->od_os, db->db_object, NULL,
1539 SA_HDL_PRIVATE, &obj->oo_sa_hdl);
1543 /* configure new osd object */
1545 parent = parent != 0 ? parent : zapid;
1546 rc = __osd_attr_init(env, osd, obj->oo_sa_hdl, oh->ot_tx,
1547 &obj->oo_attr, parent);
1551 /* XXX: oo_lma_flags */
1552 obj->oo_dt.do_lu.lo_header->loh_attr |= obj->oo_attr.la_mode & S_IFMT;
1554 obj->oo_dt.do_lu.lo_header->loh_attr |= LOHA_EXISTS;
1555 if (likely(!fid_is_acct(lu_object_fid(&obj->oo_dt.do_lu))))
1556 /* no body operations for accounting objects */
1557 obj->oo_dt.do_body_ops = &osd_body_ops;
1559 rc = -nvlist_alloc(&obj->oo_sa_xattr, NV_UNIQUE_NAME, KM_SLEEP);
1563 /* initialize LMA */
1564 lustre_lma_init(lma, lu_object_fid(&obj->oo_dt.do_lu), 0, 0);
1565 lustre_lma_swab(lma);
1566 rc = -nvlist_add_byte_array(obj->oo_sa_xattr, XATTR_NAME_LMA,
1567 (uchar_t *)lma, sizeof(*lma));
1570 rc = __osd_sa_xattr_update(env, obj, oh);
1573 osd_idc_find_and_init(env, osd, obj);
1575 /* Add new object to inode accounting.
1576 * Errors are not considered as fatal */
1577 rc = -zap_increment_int(osd->od_os, osd->od_iusr_oid,
1578 (attr->la_valid & LA_UID) ? attr->la_uid : 0, 1,
1581 CERROR("%s: failed to add "DFID" to accounting ZAP for usr %d "
1582 "(%d)\n", osd->od_svname, PFID(fid), attr->la_uid, rc);
1583 rc = -zap_increment_int(osd->od_os, osd->od_igrp_oid,
1584 (attr->la_valid & LA_GID) ? attr->la_gid : 0, 1,
1587 CERROR("%s: failed to add "DFID" to accounting ZAP for grp %d "
1588 "(%d)\n", osd->od_svname, PFID(fid), attr->la_gid, rc);
1591 if (unlikely(rc && db)) {
1592 dmu_object_free(osd->od_os, db->db_object, oh->ot_tx);
1593 sa_buf_rele(db, osd_obj_tag);
1596 up_write(&obj->oo_guard);
1600 static int osd_declare_object_ref_add(const struct lu_env *env,
1601 struct dt_object *dt,
1604 return osd_declare_attr_set(env, dt, NULL, th);
1608 * Concurrency: @dt is write locked.
1610 static int osd_object_ref_add(const struct lu_env *env,
1611 struct dt_object *dt,
1612 struct thandle *handle)
1614 struct osd_object *obj = osd_dt_obj(dt);
1615 struct osd_thandle *oh;
1616 struct osd_device *osd = osd_obj2dev(obj);
1622 down_read(&obj->oo_guard);
1623 if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
1624 GOTO(out, rc = -ENOENT);
1626 LASSERT(osd_invariant(obj));
1627 LASSERT(obj->oo_sa_hdl != NULL);
1629 oh = container_of0(handle, struct osd_thandle, ot_super);
1631 write_lock(&obj->oo_attr_lock);
1632 nlink = ++obj->oo_attr.la_nlink;
1633 write_unlock(&obj->oo_attr_lock);
1635 rc = osd_object_sa_update(obj, SA_ZPL_LINKS(osd), &nlink, 8, oh);
1638 up_read(&obj->oo_guard);
1642 static int osd_declare_object_ref_del(const struct lu_env *env,
1643 struct dt_object *dt,
1644 struct thandle *handle)
1646 return osd_declare_attr_set(env, dt, NULL, handle);
1650 * Concurrency: @dt is write locked.
1652 static int osd_object_ref_del(const struct lu_env *env,
1653 struct dt_object *dt,
1654 struct thandle *handle)
1656 struct osd_object *obj = osd_dt_obj(dt);
1657 struct osd_thandle *oh;
1658 struct osd_device *osd = osd_obj2dev(obj);
1664 down_read(&obj->oo_guard);
1666 if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
1667 GOTO(out, rc = -ENOENT);
1669 LASSERT(osd_invariant(obj));
1670 LASSERT(obj->oo_sa_hdl != NULL);
1672 oh = container_of0(handle, struct osd_thandle, ot_super);
1673 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1675 write_lock(&obj->oo_attr_lock);
1676 nlink = --obj->oo_attr.la_nlink;
1677 write_unlock(&obj->oo_attr_lock);
1679 rc = osd_object_sa_update(obj, SA_ZPL_LINKS(osd), &nlink, 8, oh);
1682 up_read(&obj->oo_guard);
1686 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
1687 __u64 start, __u64 end)
1689 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1692 /* XXX: no other option than syncing the whole filesystem until we
1693 * support ZIL. If the object tracked the txg that it was last
1694 * modified in, it could pass that txg here instead of "0". Maybe
1695 * the changes are already committed, so no wait is needed at all? */
1696 txg_wait_synced(dmu_objset_pool(osd->od_os), 0ULL);
1701 static int osd_invalidate(const struct lu_env *env, struct dt_object *dt)
1706 static struct dt_object_operations osd_obj_ops = {
1707 .do_read_lock = osd_object_read_lock,
1708 .do_write_lock = osd_object_write_lock,
1709 .do_read_unlock = osd_object_read_unlock,
1710 .do_write_unlock = osd_object_write_unlock,
1711 .do_write_locked = osd_object_write_locked,
1712 .do_attr_get = osd_attr_get,
1713 .do_declare_attr_set = osd_declare_attr_set,
1714 .do_attr_set = osd_attr_set,
1715 .do_ah_init = osd_ah_init,
1716 .do_declare_create = osd_declare_object_create,
1717 .do_create = osd_object_create,
1718 .do_declare_destroy = osd_declare_object_destroy,
1719 .do_destroy = osd_object_destroy,
1720 .do_index_try = osd_index_try,
1721 .do_declare_ref_add = osd_declare_object_ref_add,
1722 .do_ref_add = osd_object_ref_add,
1723 .do_declare_ref_del = osd_declare_object_ref_del,
1724 .do_ref_del = osd_object_ref_del,
1725 .do_xattr_get = osd_xattr_get,
1726 .do_declare_xattr_set = osd_declare_xattr_set,
1727 .do_xattr_set = osd_xattr_set,
1728 .do_declare_xattr_del = osd_declare_xattr_del,
1729 .do_xattr_del = osd_xattr_del,
1730 .do_xattr_list = osd_xattr_list,
1731 .do_object_sync = osd_object_sync,
1732 .do_invalidate = osd_invalidate,
1735 static struct lu_object_operations osd_lu_obj_ops = {
1736 .loo_object_init = osd_object_init,
1737 .loo_object_delete = osd_object_delete,
1738 .loo_object_release = osd_object_release,
1739 .loo_object_free = osd_object_free,
1740 .loo_object_print = osd_object_print,
1741 .loo_object_invariant = osd_object_invariant,
1744 static int osd_otable_it_attr_get(const struct lu_env *env,
1745 struct dt_object *dt,
1746 struct lu_attr *attr)
1752 static struct dt_object_operations osd_obj_otable_it_ops = {
1753 .do_attr_get = osd_otable_it_attr_get,
1754 .do_index_try = osd_index_try,