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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * Copyright (c) 2012, 2013, Intel Corporation.
32 * Use is subject to license terms.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/osd-zfs/osd_object.c
40 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
41 * Author: Mike Pershin <tappro@whamcloud.com>
42 * Author: Johann Lombardi <johann@whamcloud.com>
45 #define DEBUG_SUBSYSTEM S_OSD
47 #include <lustre_ver.h>
48 #include <libcfs/libcfs.h>
49 #include <obd_support.h>
50 #include <lustre_net.h>
52 #include <obd_class.h>
53 #include <lustre_disk.h>
54 #include <lustre_fid.h>
56 #include "osd_internal.h"
58 #include <sys/dnode.h>
63 #include <sys/spa_impl.h>
64 #include <sys/zfs_znode.h>
65 #include <sys/dmu_tx.h>
66 #include <sys/dmu_objset.h>
67 #include <sys/dsl_prop.h>
68 #include <sys/sa_impl.h>
71 static char *osd_obj_tag = "osd_object";
73 static struct dt_object_operations osd_obj_ops;
74 static struct lu_object_operations osd_lu_obj_ops;
75 extern struct dt_body_operations osd_body_ops;
76 static struct dt_object_operations osd_obj_otable_it_ops;
78 extern struct kmem_cache *osd_object_kmem;
81 osd_object_sa_fini(struct osd_object *obj)
84 sa_handle_destroy(obj->oo_sa_hdl);
85 obj->oo_sa_hdl = NULL;
90 osd_object_sa_init(struct osd_object *obj, udmu_objset_t *uos)
94 LASSERT(obj->oo_sa_hdl == NULL);
95 LASSERT(obj->oo_db != NULL);
97 rc = -sa_handle_get(uos->os, obj->oo_db->db_object, obj,
98 SA_HDL_PRIVATE, &obj->oo_sa_hdl);
102 /* Cache the xattr object id, valid for the life of the object */
103 rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_XATTR(uos), &obj->oo_xattr, 8);
105 obj->oo_xattr = ZFS_NO_OBJECT;
108 osd_object_sa_fini(obj);
115 * Add object to list of dirty objects in tx handle.
118 osd_object_sa_dirty_add(struct osd_object *obj, struct osd_thandle *oh)
120 if (!cfs_list_empty(&obj->oo_sa_linkage))
123 down(&oh->ot_sa_lock);
124 write_lock(&obj->oo_attr_lock);
125 if (likely(cfs_list_empty(&obj->oo_sa_linkage)))
126 cfs_list_add(&obj->oo_sa_linkage, &oh->ot_sa_list);
127 write_unlock(&obj->oo_attr_lock);
132 * Release spill block dbuf hold for all dirty SAs.
134 void osd_object_sa_dirty_rele(struct osd_thandle *oh)
136 struct osd_object *obj;
138 down(&oh->ot_sa_lock);
139 while (!cfs_list_empty(&oh->ot_sa_list)) {
140 obj = cfs_list_entry(oh->ot_sa_list.next,
141 struct osd_object, oo_sa_linkage);
142 sa_spill_rele(obj->oo_sa_hdl);
143 write_lock(&obj->oo_attr_lock);
144 cfs_list_del_init(&obj->oo_sa_linkage);
145 write_unlock(&obj->oo_attr_lock);
151 * Update the SA and add the object to the dirty list.
153 int osd_object_sa_update(struct osd_object *obj, sa_attr_type_t type,
154 void *buf, uint32_t buflen, struct osd_thandle *oh)
158 LASSERT(obj->oo_sa_hdl != NULL);
159 LASSERT(oh->ot_tx != NULL);
161 rc = -sa_update(obj->oo_sa_hdl, type, buf, buflen, oh->ot_tx);
162 osd_object_sa_dirty_add(obj, oh);
168 * Bulk update the SA and add the object to the dirty list.
171 osd_object_sa_bulk_update(struct osd_object *obj, sa_bulk_attr_t *attrs,
172 int count, struct osd_thandle *oh)
176 LASSERT(obj->oo_sa_hdl != NULL);
177 LASSERT(oh->ot_tx != NULL);
179 rc = -sa_bulk_update(obj->oo_sa_hdl, attrs, count, oh->ot_tx);
180 osd_object_sa_dirty_add(obj, oh);
186 * Retrieve the attributes of a DMU object
188 int __osd_object_attr_get(const struct lu_env *env, udmu_objset_t *uos,
189 struct osd_object *obj, struct lu_attr *la)
191 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
193 sa_bulk_attr_t *bulk;
198 LASSERT(obj->oo_db != NULL);
200 rc = -sa_handle_get(uos->os, obj->oo_db->db_object, NULL,
201 SA_HDL_PRIVATE, &sa_hdl);
205 OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 9);
207 GOTO(out_sa, rc = -ENOMEM);
209 la->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE | LA_TYPE |
210 LA_SIZE | LA_UID | LA_GID | LA_FLAGS | LA_NLINK;
212 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(uos), NULL, osa->atime, 16);
213 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(uos), NULL, osa->mtime, 16);
214 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(uos), NULL, osa->ctime, 16);
215 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(uos), NULL, &osa->mode, 8);
216 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(uos), NULL, &osa->size, 8);
217 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(uos), NULL, &osa->nlink, 8);
218 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(uos), NULL, &osa->uid, 8);
219 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(uos), NULL, &osa->gid, 8);
220 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(uos), NULL, &osa->flags, 8);
222 rc = -sa_bulk_lookup(sa_hdl, bulk, cnt);
226 la->la_atime = osa->atime[0];
227 la->la_mtime = osa->mtime[0];
228 la->la_ctime = osa->ctime[0];
229 la->la_mode = osa->mode;
230 la->la_uid = osa->uid;
231 la->la_gid = osa->gid;
232 la->la_nlink = osa->nlink;
233 la->la_flags = attrs_zfs2fs(osa->flags);
234 la->la_size = osa->size;
236 if (S_ISCHR(la->la_mode) || S_ISBLK(la->la_mode)) {
237 rc = -sa_lookup(sa_hdl, SA_ZPL_RDEV(uos), &osa->rdev, 8);
240 la->la_rdev = osa->rdev;
241 la->la_valid |= LA_RDEV;
244 OBD_FREE(bulk, sizeof(sa_bulk_attr_t) * 9);
246 sa_handle_destroy(sa_hdl);
251 int __osd_obj2dbuf(const struct lu_env *env, objset_t *os,
252 uint64_t oid, dmu_buf_t **dbp, void *tag)
254 dmu_object_info_t *doi = &osd_oti_get(env)->oti_doi;
259 rc = -sa_buf_hold(os, oid, tag, dbp);
263 dmu_object_info_from_db(*dbp, doi);
264 if (unlikely (oid != DMU_USERUSED_OBJECT &&
265 oid != DMU_GROUPUSED_OBJECT && doi->doi_bonus_type != DMU_OT_SA)) {
266 sa_buf_rele(*dbp, tag);
272 LASSERT((*dbp)->db_object == oid);
273 LASSERT((*dbp)->db_offset == -1);
274 LASSERT((*dbp)->db_data != NULL);
280 * Concurrency: no concurrent access is possible that early in object
283 struct lu_object *osd_object_alloc(const struct lu_env *env,
284 const struct lu_object_header *hdr,
287 struct osd_object *mo;
289 OBD_SLAB_ALLOC_PTR_GFP(mo, osd_object_kmem, GFP_NOFS);
293 l = &mo->oo_dt.do_lu;
294 dt_object_init(&mo->oo_dt, NULL, d);
295 mo->oo_dt.do_ops = &osd_obj_ops;
296 l->lo_ops = &osd_lu_obj_ops;
297 CFS_INIT_LIST_HEAD(&mo->oo_sa_linkage);
298 init_rwsem(&mo->oo_sem);
299 sema_init(&mo->oo_guard, 1);
300 rwlock_init(&mo->oo_attr_lock);
308 * Concurrency: shouldn't matter.
310 int osd_object_init0(const struct lu_env *env, struct osd_object *obj)
312 struct osd_device *osd = osd_obj2dev(obj);
313 udmu_objset_t *uos = &osd->od_objset;
314 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
318 if (obj->oo_db == NULL)
323 rc = osd_object_sa_init(obj, uos);
327 /* cache attrs in object */
328 rc = __osd_object_attr_get(env, &osd->od_objset,
333 if (likely(!fid_is_acct(fid)))
334 /* no body operations for accounting objects */
335 obj->oo_dt.do_body_ops = &osd_body_ops;
338 * initialize object before marking it existing
340 obj->oo_dt.do_lu.lo_header->loh_attr |= obj->oo_attr.la_mode & S_IFMT;
343 obj->oo_dt.do_lu.lo_header->loh_attr |= LOHA_EXISTS;
348 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
350 struct osd_thread_info *info = osd_oti_get(env);
353 struct lustre_mdt_attrs *lma;
356 CLASSERT(sizeof(info->oti_buf) >= sizeof(*lma));
357 lma = (struct lustre_mdt_attrs *)info->oti_buf;
359 buf.lb_len = sizeof(info->oti_buf);
361 rc = osd_xattr_get(env, &obj->oo_dt, &buf, XATTR_NAME_LMA, BYPASS_CAPA);
364 lustre_lma_swab(lma);
365 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
366 CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
367 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
368 "fid = "DFID"\n", osd_obj2dev(obj)->od_svname,
369 lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
370 PFID(lu_object_fid(&obj->oo_dt.do_lu)));
373 } else if (rc == -ENODATA) {
374 /* haven't initialize LMA xattr */
382 * Concurrency: no concurrent access is possible that early in object
385 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
386 const struct lu_object_conf *conf)
388 struct osd_object *obj = osd_obj(l);
389 struct osd_device *osd = osd_obj2dev(obj);
394 LASSERT(osd_invariant(obj));
396 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
397 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
398 l->lo_header->loh_attr |= LOHA_EXISTS;
402 rc = osd_fid_lookup(env, osd, lu_object_fid(l), &oid);
404 LASSERT(obj->oo_db == NULL);
405 rc = __osd_obj2dbuf(env, osd->od_objset.os, oid,
406 &obj->oo_db, osd_obj_tag);
408 CERROR("%s: lookup "DFID"/"LPX64" failed: rc = %d\n",
409 osd->od_svname, PFID(lu_object_fid(l)), oid, rc);
413 rc = osd_object_init0(env, obj);
417 rc = osd_check_lma(env, obj);
420 } else if (rc == -ENOENT) {
423 LASSERT(osd_invariant(obj));
429 * Concurrency: no concurrent access is possible that late in object
432 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
434 struct osd_object *obj = osd_obj(l);
436 LASSERT(osd_invariant(obj));
438 dt_object_fini(&obj->oo_dt);
439 OBD_SLAB_FREE_PTR(obj, osd_object_kmem);
442 static void __osd_declare_object_destroy(const struct lu_env *env,
443 struct osd_object *obj,
444 struct osd_thandle *oh)
446 struct osd_device *osd = osd_obj2dev(obj);
447 udmu_objset_t *uos = &osd->od_objset;
448 dmu_buf_t *db = obj->oo_db;
449 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
450 uint64_t oid = db->db_object, xid;
451 dmu_tx_t *tx = oh->ot_tx;
455 dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
457 /* zap holding xattrs */
458 if (obj->oo_xattr != ZFS_NO_OBJECT) {
461 dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
463 rc = -udmu_zap_cursor_init(&zc, uos, oid, 0);
467 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
468 BUG_ON(za->za_integer_length != sizeof(uint64_t));
469 BUG_ON(za->za_num_integers != 1);
471 rc = -zap_lookup(uos->os, obj->oo_xattr, za->za_name,
472 sizeof(uint64_t), 1, &xid);
474 CERROR("%s: xattr lookup failed: rc = %d\n",
478 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
480 zap_cursor_advance(zc);
485 udmu_zap_cursor_fini(zc);
488 if (rc && tx->tx_err == 0)
492 static int osd_declare_object_destroy(const struct lu_env *env,
493 struct dt_object *dt,
496 char *buf = osd_oti_get(env)->oti_str;
497 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
498 struct osd_object *obj = osd_dt_obj(dt);
499 struct osd_device *osd = osd_obj2dev(obj);
500 struct osd_thandle *oh;
506 LASSERT(dt_object_exists(dt));
508 oh = container_of0(th, struct osd_thandle, ot_super);
509 LASSERT(oh->ot_tx != NULL);
511 /* declare that we'll destroy the object */
512 __osd_declare_object_destroy(env, obj, oh);
514 /* declare that we'll remove object from fid-dnode mapping */
515 zapid = osd_get_name_n_idx(env, osd, fid, buf);
516 dmu_tx_hold_bonus(oh->ot_tx, zapid);
517 dmu_tx_hold_zap(oh->ot_tx, zapid, 0, buf);
519 /* declare that we'll remove object from inode accounting ZAPs */
520 dmu_tx_hold_bonus(oh->ot_tx, osd->od_iusr_oid);
521 dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, 0, buf);
522 dmu_tx_hold_bonus(oh->ot_tx, osd->od_igrp_oid);
523 dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, 0, buf);
526 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
527 obj->oo_attr.la_gid, -1, oh, false, NULL, false);
531 /* data to be truncated */
532 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
533 obj->oo_attr.la_gid, 0, oh, true, NULL, false);
537 int __osd_object_free(udmu_objset_t *uos, uint64_t oid, dmu_tx_t *tx)
539 LASSERT(uos->objects != 0);
540 spin_lock(&uos->lock);
542 spin_unlock(&uos->lock);
544 return -dmu_object_free(uos->os, oid, tx);
548 * Delete a DMU object
550 * The transaction passed to this routine must have
551 * dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END) called
552 * and then assigned to a transaction group.
554 * This will release db and set it to NULL to prevent further dbuf releases.
556 static int __osd_object_destroy(const struct lu_env *env,
557 struct osd_object *obj,
558 dmu_tx_t *tx, void *tag)
560 struct osd_device *osd = osd_obj2dev(obj);
561 udmu_objset_t *uos = &osd->od_objset;
563 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
567 /* Assert that the transaction has been assigned to a
568 transaction group. */
569 LASSERT(tx->tx_txg != 0);
571 /* zap holding xattrs */
572 if (obj->oo_xattr != ZFS_NO_OBJECT) {
573 rc = -udmu_zap_cursor_init(&zc, uos, obj->oo_xattr, 0);
576 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
577 BUG_ON(za->za_integer_length != sizeof(uint64_t));
578 BUG_ON(za->za_num_integers != 1);
580 rc = -zap_lookup(uos->os, obj->oo_xattr, za->za_name,
581 sizeof(uint64_t), 1, &xid);
583 CERROR("%s: lookup xattr %s failed: rc = %d\n",
584 osd->od_svname, za->za_name, rc);
587 rc = __osd_object_free(uos, xid, tx);
589 CERROR("%s: fetch xattr %s failed: rc = %d\n",
590 osd->od_svname, za->za_name, rc);
591 zap_cursor_advance(zc);
593 udmu_zap_cursor_fini(zc);
595 rc = __osd_object_free(uos, obj->oo_xattr, tx);
597 CERROR("%s: freeing xattr failed: rc = %d\n",
601 return __osd_object_free(uos, obj->oo_db->db_object, tx);
604 static int osd_object_destroy(const struct lu_env *env,
605 struct dt_object *dt,
608 char *buf = osd_oti_get(env)->oti_str;
609 struct osd_object *obj = osd_dt_obj(dt);
610 struct osd_device *osd = osd_obj2dev(obj);
611 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
612 struct osd_thandle *oh;
617 LASSERT(obj->oo_db != NULL);
618 LASSERT(dt_object_exists(dt));
619 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
621 oh = container_of0(th, struct osd_thandle, ot_super);
623 LASSERT(oh->ot_tx != NULL);
625 zapid = osd_get_name_n_idx(env, osd, fid, buf);
627 /* remove obj ref from index dir (it depends) */
628 rc = -zap_remove(osd->od_objset.os, zapid, buf, oh->ot_tx);
630 CERROR("%s: zap_remove() failed: rc = %d\n",
635 /* do object accounting */
636 osd_zfs_acct_uid(env, osd, obj->oo_attr.la_uid, -1, oh);
637 osd_zfs_acct_gid(env, osd, obj->oo_attr.la_gid, -1, oh);
640 rc = __osd_object_destroy(env, obj, oh->ot_tx, osd_obj_tag);
642 CERROR("%s: __osd_object_destroy() failed: rc = %d\n",
648 /* not needed in the cache anymore */
649 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
654 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
656 struct osd_object *obj = osd_obj(l);
658 if (obj->oo_db != NULL) {
659 osd_object_sa_fini(obj);
660 if (obj->oo_sa_xattr) {
661 nvlist_free(obj->oo_sa_xattr);
662 obj->oo_sa_xattr = NULL;
664 sa_buf_rele(obj->oo_db, osd_obj_tag);
665 cfs_list_del(&obj->oo_sa_linkage);
671 * Concurrency: ->loo_object_release() is called under site spin-lock.
673 static void osd_object_release(const struct lu_env *env,
679 * Concurrency: shouldn't matter.
681 static int osd_object_print(const struct lu_env *env, void *cookie,
682 lu_printer_t p, const struct lu_object *l)
684 struct osd_object *o = osd_obj(l);
686 return (*p)(env, cookie, LUSTRE_OSD_ZFS_NAME"-object@%p", o);
689 static void osd_object_read_lock(const struct lu_env *env,
690 struct dt_object *dt, unsigned role)
692 struct osd_object *obj = osd_dt_obj(dt);
694 LASSERT(osd_invariant(obj));
696 down_read(&obj->oo_sem);
699 static void osd_object_write_lock(const struct lu_env *env,
700 struct dt_object *dt, unsigned role)
702 struct osd_object *obj = osd_dt_obj(dt);
704 LASSERT(osd_invariant(obj));
706 down_write(&obj->oo_sem);
709 static void osd_object_read_unlock(const struct lu_env *env,
710 struct dt_object *dt)
712 struct osd_object *obj = osd_dt_obj(dt);
714 LASSERT(osd_invariant(obj));
715 up_read(&obj->oo_sem);
718 static void osd_object_write_unlock(const struct lu_env *env,
719 struct dt_object *dt)
721 struct osd_object *obj = osd_dt_obj(dt);
723 LASSERT(osd_invariant(obj));
724 up_write(&obj->oo_sem);
727 static int osd_object_write_locked(const struct lu_env *env,
728 struct dt_object *dt)
730 struct osd_object *obj = osd_dt_obj(dt);
733 LASSERT(osd_invariant(obj));
735 if (down_write_trylock(&obj->oo_sem)) {
737 up_write(&obj->oo_sem);
742 static int osd_attr_get(const struct lu_env *env,
743 struct dt_object *dt,
744 struct lu_attr *attr,
745 struct lustre_capa *capa)
747 struct osd_object *obj = osd_dt_obj(dt);
751 LASSERT(dt_object_exists(dt));
752 LASSERT(osd_invariant(obj));
755 read_lock(&obj->oo_attr_lock);
756 *attr = obj->oo_attr;
757 read_unlock(&obj->oo_attr_lock);
759 /* with ZFS_DEBUG zrl_add_debug() called by DB_DNODE_ENTER()
760 * from within sa_object_size() can block on a mutex, so
761 * we can't call sa_object_size() holding rwlock */
762 sa_object_size(obj->oo_sa_hdl, &blksize, &blocks);
763 /* we do not control size of indices, so always calculate
764 * it from number of blocks reported by DMU */
765 if (S_ISDIR(attr->la_mode))
766 attr->la_size = 512 * blocks;
767 /* Block size may be not set; suggest maximal I/O transfers. */
769 blksize = 1ULL << SPA_MAXBLOCKSHIFT;
771 attr->la_blksize = blksize;
772 attr->la_blocks = blocks;
773 attr->la_valid |= LA_BLOCKS | LA_BLKSIZE;
778 /* Simple wrapper on top of qsd API which implement quota transfer for osd
779 * setattr needs. As a reminder, only the root user can change ownership of
780 * a file, that's why EDQUOT & EINPROGRESS errors are discarded */
781 static inline int qsd_transfer(const struct lu_env *env,
782 struct qsd_instance *qsd,
783 struct lquota_trans *trans, int qtype,
784 __u64 orig_id, __u64 new_id, __u64 bspace,
785 struct lquota_id_info *qi)
789 if (unlikely(qsd == NULL))
792 LASSERT(qtype >= 0 && qtype < MAXQUOTAS);
793 qi->lqi_type = qtype;
795 /* inode accounting */
796 qi->lqi_is_blk = false;
798 /* one more inode for the new owner ... */
799 qi->lqi_id.qid_uid = new_id;
801 rc = qsd_op_begin(env, qsd, trans, qi, NULL);
802 if (rc == -EDQUOT || rc == -EINPROGRESS)
807 /* and one less inode for the current id */
808 qi->lqi_id.qid_uid = orig_id;;
810 /* can't get EDQUOT when reducing usage */
811 rc = qsd_op_begin(env, qsd, trans, qi, NULL);
812 if (rc == -EINPROGRESS)
817 /* block accounting */
818 qi->lqi_is_blk = true;
820 /* more blocks for the new owner ... */
821 qi->lqi_id.qid_uid = new_id;
822 qi->lqi_space = bspace;
823 rc = qsd_op_begin(env, qsd, trans, qi, NULL);
824 if (rc == -EDQUOT || rc == -EINPROGRESS)
829 /* and finally less blocks for the current owner */
830 qi->lqi_id.qid_uid = orig_id;
831 qi->lqi_space = -bspace;
832 rc = qsd_op_begin(env, qsd, trans, qi, NULL);
833 /* can't get EDQUOT when reducing usage */
834 if (rc == -EINPROGRESS)
839 static int osd_declare_attr_set(const struct lu_env *env,
840 struct dt_object *dt,
841 const struct lu_attr *attr,
842 struct thandle *handle)
844 struct osd_thread_info *info = osd_oti_get(env);
845 char *buf = osd_oti_get(env)->oti_str;
846 struct osd_object *obj = osd_dt_obj(dt);
847 struct osd_device *osd = osd_obj2dev(obj);
848 struct osd_thandle *oh;
854 if (!dt_object_exists(dt)) {
855 /* XXX: sanity check that object creation is declared */
859 LASSERT(handle != NULL);
860 LASSERT(osd_invariant(obj));
862 oh = container_of0(handle, struct osd_thandle, ot_super);
864 LASSERT(obj->oo_sa_hdl != NULL);
865 LASSERT(oh->ot_tx != NULL);
866 dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
867 if (oh->ot_tx->tx_err != 0)
868 RETURN(-oh->ot_tx->tx_err);
870 sa_object_size(obj->oo_sa_hdl, &blksize, &bspace);
871 bspace = toqb(bspace * blksize);
873 if (attr && attr->la_valid & LA_UID) {
874 /* account for user inode tracking ZAP update */
875 dmu_tx_hold_bonus(oh->ot_tx, osd->od_iusr_oid);
876 dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, TRUE, buf);
878 /* quota enforcement for user */
879 if (attr->la_uid != obj->oo_attr.la_uid) {
880 rc = qsd_transfer(env, osd->od_quota_slave,
881 &oh->ot_quota_trans, USRQUOTA,
882 obj->oo_attr.la_uid, attr->la_uid,
883 bspace, &info->oti_qi);
888 if (attr && attr->la_valid & LA_GID) {
889 /* account for user inode tracking ZAP update */
890 dmu_tx_hold_bonus(oh->ot_tx, osd->od_igrp_oid);
891 dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, TRUE, buf);
893 /* quota enforcement for group */
894 if (attr->la_gid != obj->oo_attr.la_gid) {
895 rc = qsd_transfer(env, osd->od_quota_slave,
896 &oh->ot_quota_trans, GRPQUOTA,
897 obj->oo_attr.la_gid, attr->la_gid,
898 bspace, &info->oti_qi);
908 * Set the attributes of an object
910 * The transaction passed to this routine must have
911 * dmu_tx_hold_bonus(tx, oid) called and then assigned
912 * to a transaction group.
914 static int osd_attr_set(const struct lu_env *env, struct dt_object *dt,
915 const struct lu_attr *la, struct thandle *handle,
916 struct lustre_capa *capa)
918 struct osd_object *obj = osd_dt_obj(dt);
919 struct osd_device *osd = osd_obj2dev(obj);
920 udmu_objset_t *uos = &osd->od_objset;
921 struct osd_thandle *oh;
922 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
923 sa_bulk_attr_t *bulk;
928 LASSERT(handle != NULL);
929 LASSERT(dt_object_exists(dt));
930 LASSERT(osd_invariant(obj));
931 LASSERT(obj->oo_sa_hdl);
933 oh = container_of0(handle, struct osd_thandle, ot_super);
934 /* Assert that the transaction has been assigned to a
935 transaction group. */
936 LASSERT(oh->ot_tx->tx_txg != 0);
938 if (la->la_valid == 0)
941 OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 10);
945 /* do both accounting updates outside oo_attr_lock below */
946 if ((la->la_valid & LA_UID) && (la->la_uid != obj->oo_attr.la_uid)) {
947 /* do object accounting */
948 osd_zfs_acct_uid(env, osd, la->la_uid, 1, oh);
949 osd_zfs_acct_uid(env, osd, obj->oo_attr.la_uid, -1, oh);
951 if ((la->la_valid & LA_GID) && (la->la_gid != obj->oo_attr.la_gid)) {
952 /* do object accounting */
953 osd_zfs_acct_gid(env, osd, la->la_gid, 1, oh);
954 osd_zfs_acct_gid(env, osd, obj->oo_attr.la_gid, -1, oh);
957 write_lock(&obj->oo_attr_lock);
959 if (la->la_valid & LA_ATIME) {
960 osa->atime[0] = obj->oo_attr.la_atime = la->la_atime;
961 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(uos), NULL,
964 if (la->la_valid & LA_MTIME) {
965 osa->mtime[0] = obj->oo_attr.la_mtime = la->la_mtime;
966 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(uos), NULL,
969 if (la->la_valid & LA_CTIME) {
970 osa->ctime[0] = obj->oo_attr.la_ctime = la->la_ctime;
971 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(uos), NULL,
974 if (la->la_valid & LA_MODE) {
975 /* mode is stored along with type, so read it first */
976 obj->oo_attr.la_mode = (obj->oo_attr.la_mode & S_IFMT) |
977 (la->la_mode & ~S_IFMT);
978 osa->mode = obj->oo_attr.la_mode;
979 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(uos), NULL,
982 if (la->la_valid & LA_SIZE) {
983 osa->size = obj->oo_attr.la_size = la->la_size;
984 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(uos), NULL,
987 if (la->la_valid & LA_NLINK) {
988 osa->nlink = obj->oo_attr.la_nlink = la->la_nlink;
989 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(uos), NULL,
992 if (la->la_valid & LA_RDEV) {
993 osa->rdev = obj->oo_attr.la_rdev = la->la_rdev;
994 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(uos), NULL,
997 if (la->la_valid & LA_FLAGS) {
998 osa->flags = attrs_fs2zfs(la->la_flags);
999 /* many flags are not supported by zfs, so ensure a good cached
1001 obj->oo_attr.la_flags = attrs_zfs2fs(osa->flags);
1002 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(uos), NULL,
1005 if (la->la_valid & LA_UID) {
1006 osa->uid = obj->oo_attr.la_uid = la->la_uid;
1007 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(uos), NULL,
1010 if (la->la_valid & LA_GID) {
1011 osa->gid = obj->oo_attr.la_gid = la->la_gid;
1012 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(uos), NULL,
1015 obj->oo_attr.la_valid |= la->la_valid;
1016 write_unlock(&obj->oo_attr_lock);
1018 rc = osd_object_sa_bulk_update(obj, bulk, cnt, oh);
1020 OBD_FREE(bulk, sizeof(sa_bulk_attr_t) * 10);
1027 * XXX temporary solution.
1030 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1031 struct dt_object *parent, struct dt_object *child,
1036 memset(ah, 0, sizeof(*ah));
1037 ah->dah_parent = parent;
1038 ah->dah_mode = child_mode;
1041 static int osd_declare_object_create(const struct lu_env *env,
1042 struct dt_object *dt,
1043 struct lu_attr *attr,
1044 struct dt_allocation_hint *hint,
1045 struct dt_object_format *dof,
1046 struct thandle *handle)
1048 char *buf = osd_oti_get(env)->oti_str;
1049 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1050 struct osd_object *obj = osd_dt_obj(dt);
1051 struct osd_device *osd = osd_obj2dev(obj);
1052 struct osd_thandle *oh;
1059 switch (dof->dof_type) {
1063 if (obj->oo_dt.do_body_ops == NULL)
1064 obj->oo_dt.do_body_ops = &osd_body_ops;
1070 LASSERT(handle != NULL);
1071 oh = container_of0(handle, struct osd_thandle, ot_super);
1072 LASSERT(oh->ot_tx != NULL);
1074 switch (dof->dof_type) {
1077 /* for zap create */
1078 dmu_tx_hold_zap(oh->ot_tx, DMU_NEW_OBJECT, 1, NULL);
1083 /* first, we'll create new object */
1084 dmu_tx_hold_bonus(oh->ot_tx, DMU_NEW_OBJECT);
1092 /* and we'll add it to some mapping */
1093 zapid = osd_get_name_n_idx(env, osd, fid, buf);
1094 dmu_tx_hold_bonus(oh->ot_tx, zapid);
1095 dmu_tx_hold_zap(oh->ot_tx, zapid, TRUE, buf);
1097 /* we will also update inode accounting ZAPs */
1098 dmu_tx_hold_bonus(oh->ot_tx, osd->od_iusr_oid);
1099 dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, TRUE, buf);
1100 dmu_tx_hold_bonus(oh->ot_tx, osd->od_igrp_oid);
1101 dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, TRUE, buf);
1103 dmu_tx_hold_sa_create(oh->ot_tx, ZFS_SA_BASE_ATTR_SIZE);
1105 __osd_xattr_declare_set(env, obj, sizeof(struct lustre_mdt_attrs),
1106 XATTR_NAME_LMA, oh);
1108 rc = osd_declare_quota(env, osd, attr->la_uid, attr->la_gid, 1, oh,
1109 false, NULL, false);
1113 int __osd_attr_init(const struct lu_env *env, udmu_objset_t *uos, uint64_t oid,
1114 dmu_tx_t *tx, struct lu_attr *la, uint64_t parent)
1116 sa_bulk_attr_t *bulk;
1117 sa_handle_t *sa_hdl;
1118 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
1126 gen = dmu_tx_get_txg(tx);
1128 ZFS_TIME_ENCODE(&now, crtime);
1130 osa->atime[0] = la->la_atime;
1131 osa->ctime[0] = la->la_ctime;
1132 osa->mtime[0] = la->la_mtime;
1133 osa->mode = la->la_mode;
1134 osa->uid = la->la_uid;
1135 osa->gid = la->la_gid;
1136 osa->rdev = la->la_rdev;
1137 osa->nlink = la->la_nlink;
1138 osa->flags = attrs_fs2zfs(la->la_flags);
1139 osa->size = la->la_size;
1141 /* Now add in all of the "SA" attributes */
1142 rc = -sa_handle_get(uos->os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
1146 OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 13);
1152 * we need to create all SA below upon object create.
1154 * XXX The attribute order matters since the accounting callback relies
1155 * on static offsets (i.e. SA_*_OFFSET, see zfs_space_delta_cb()) to
1156 * look up the UID/GID attributes. Moreover, the callback does not seem
1157 * to support the spill block.
1158 * We define attributes in the same order as SA_*_OFFSET in order to
1159 * work around the problem. See ORI-610.
1162 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(uos), NULL, &osa->mode, 8);
1163 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(uos), NULL, &osa->size, 8);
1164 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(uos), NULL, &gen, 8);
1165 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(uos), NULL, &osa->uid, 8);
1166 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(uos), NULL, &osa->gid, 8);
1167 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(uos), NULL, &parent, 8);
1168 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(uos), NULL, &osa->flags, 8);
1169 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(uos), NULL, osa->atime, 16);
1170 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(uos), NULL, osa->mtime, 16);
1171 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(uos), NULL, osa->ctime, 16);
1172 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(uos), NULL, crtime, 16);
1173 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(uos), NULL, &osa->nlink, 8);
1174 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(uos), NULL, &osa->rdev, 8);
1176 rc = -sa_replace_all_by_template(sa_hdl, bulk, cnt, tx);
1178 OBD_FREE(bulk, sizeof(sa_bulk_attr_t) * 13);
1180 sa_handle_destroy(sa_hdl);
1185 * The transaction passed to this routine must have
1186 * dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT) called and then assigned
1187 * to a transaction group.
1189 int __osd_object_create(const struct lu_env *env, udmu_objset_t *uos,
1190 dmu_buf_t **dbp, dmu_tx_t *tx, struct lu_attr *la,
1191 uint64_t parent, void *tag)
1197 spin_lock(&uos->lock);
1199 spin_unlock(&uos->lock);
1201 /* Assert that the transaction has been assigned to a
1202 transaction group. */
1203 LASSERT(tx->tx_txg != 0);
1205 /* Create a new DMU object. */
1206 oid = dmu_object_alloc(uos->os, DMU_OT_PLAIN_FILE_CONTENTS, 0,
1207 DMU_OT_SA, DN_MAX_BONUSLEN, tx);
1208 rc = -sa_buf_hold(uos->os, oid, tag, dbp);
1212 LASSERT(la->la_valid & LA_MODE);
1216 return __osd_attr_init(env, uos, oid, tx, la, parent);
1220 * The transaction passed to this routine must have
1221 * dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, ...) called and then assigned
1222 * to a transaction group.
1224 * Using ZAP_FLAG_HASH64 will force the ZAP to always be a FAT ZAP.
1225 * This is fine for directories today, because storing the FID in the dirent
1226 * will also require a FAT ZAP. If there is a new type of micro ZAP created
1227 * then we might need to re-evaluate the use of this flag and instead do
1228 * a conversion from the different internal ZAP hash formats being used. */
1229 int __osd_zap_create(const struct lu_env *env, udmu_objset_t *uos,
1230 dmu_buf_t **zap_dbp, dmu_tx_t *tx,
1231 struct lu_attr *la, uint64_t parent,
1232 void *tag, zap_flags_t flags)
1239 spin_lock(&uos->lock);
1241 spin_unlock(&uos->lock);
1243 /* Assert that the transaction has been assigned to a
1244 transaction group. */
1245 LASSERT(tx->tx_txg != 0);
1247 oid = zap_create_flags(uos->os, 0, flags | ZAP_FLAG_HASH64,
1248 DMU_OT_DIRECTORY_CONTENTS, 12, 12,
1249 DMU_OT_SA, DN_MAX_BONUSLEN, tx);
1251 rc = -sa_buf_hold(uos->os, oid, tag, zap_dbp);
1255 LASSERT(la->la_valid & LA_MODE);
1259 return __osd_attr_init(env, uos, oid, tx, la, parent);
1262 static dmu_buf_t *osd_mkidx(const struct lu_env *env, struct osd_device *osd,
1263 struct lu_attr *la, uint64_t parent,
1264 struct osd_thandle *oh)
1269 /* Index file should be created as regular file in order not to confuse
1270 * ZPL which could interpret them as directory.
1271 * We set ZAP_FLAG_UINT64_KEY to let ZFS know than we are going to use
1273 LASSERT(S_ISREG(la->la_mode));
1274 rc = __osd_zap_create(env, &osd->od_objset, &db, oh->ot_tx, la,
1275 parent, osd_obj_tag, ZAP_FLAG_UINT64_KEY);
1281 static dmu_buf_t *osd_mkdir(const struct lu_env *env, struct osd_device *osd,
1282 struct lu_attr *la, uint64_t parent,
1283 struct osd_thandle *oh)
1288 LASSERT(S_ISDIR(la->la_mode));
1289 rc = __osd_zap_create(env, &osd->od_objset, &db, oh->ot_tx, la,
1290 parent, osd_obj_tag, 0);
1296 static dmu_buf_t* osd_mkreg(const struct lu_env *env, struct osd_device *osd,
1297 struct lu_attr *la, uint64_t parent,
1298 struct osd_thandle *oh)
1303 LASSERT(S_ISREG(la->la_mode));
1304 rc = __osd_object_create(env, &osd->od_objset, &db, oh->ot_tx, la,
1305 parent, osd_obj_tag);
1310 * XXX: a hack, OST to use bigger blocksize. we need
1311 * a method in OSD API to control this from OFD/MDD
1313 if (!lu_device_is_md(osd2lu_dev(osd))) {
1314 rc = -dmu_object_set_blocksize(osd->od_objset.os,
1316 128 << 10, 0, oh->ot_tx);
1318 CERROR("%s: can't change blocksize: %d\n",
1319 osd->od_svname, rc);
1327 static dmu_buf_t *osd_mksym(const struct lu_env *env, struct osd_device *osd,
1328 struct lu_attr *la, uint64_t parent,
1329 struct osd_thandle *oh)
1334 LASSERT(S_ISLNK(la->la_mode));
1335 rc = __osd_object_create(env, &osd->od_objset, &db, oh->ot_tx, la,
1336 parent, osd_obj_tag);
1342 static dmu_buf_t *osd_mknod(const struct lu_env *env, struct osd_device *osd,
1343 struct lu_attr *la, uint64_t parent,
1344 struct osd_thandle *oh)
1349 la->la_valid = LA_MODE;
1350 if (S_ISCHR(la->la_mode) || S_ISBLK(la->la_mode))
1351 la->la_valid |= LA_RDEV;
1353 rc = __osd_object_create(env, &osd->od_objset, &db, oh->ot_tx, la,
1354 parent, osd_obj_tag);
1360 typedef dmu_buf_t *(*osd_obj_type_f)(const struct lu_env *env,
1361 struct osd_device *osd,
1364 struct osd_thandle *oh);
1366 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1368 osd_obj_type_f result;
1394 * Primitives for directory (i.e. ZAP) handling
1396 static inline int osd_init_lma(const struct lu_env *env, struct osd_object *obj,
1397 const struct lu_fid *fid, struct osd_thandle *oh)
1399 struct osd_thread_info *info = osd_oti_get(env);
1400 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
1404 lustre_lma_init(lma, fid, 0, 0);
1405 lustre_lma_swab(lma);
1407 buf.lb_len = sizeof(*lma);
1409 rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
1410 LU_XATTR_CREATE, oh, BYPASS_CAPA);
1416 * Concurrency: @dt is write locked.
1418 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1419 struct lu_attr *attr,
1420 struct dt_allocation_hint *hint,
1421 struct dt_object_format *dof,
1424 struct zpl_direntry *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
1425 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1426 struct osd_object *obj = osd_dt_obj(dt);
1427 struct osd_device *osd = osd_obj2dev(obj);
1428 char *buf = osd_oti_get(env)->oti_str;
1429 struct osd_thandle *oh;
1436 /* concurrent create declarations should not see
1437 * the object inconsistent (db, attr, etc).
1438 * in regular cases acquisition should be cheap */
1439 down(&obj->oo_guard);
1441 LASSERT(osd_invariant(obj));
1442 LASSERT(!dt_object_exists(dt));
1443 LASSERT(dof != NULL);
1445 LASSERT(th != NULL);
1446 oh = container_of0(th, struct osd_thandle, ot_super);
1449 * XXX missing: Quote handling.
1452 LASSERT(obj->oo_db == NULL);
1454 /* to follow ZFS on-disk format we need
1455 * to initialize parent dnode properly */
1457 if (hint && hint->dah_parent)
1458 zapid = osd_dt_obj(hint->dah_parent)->oo_db->db_object;
1460 db = osd_create_type_f(dof->dof_type)(env, osd, attr, zapid, oh);
1462 GOTO(out, rc = PTR_ERR(db));
1465 zde->zde_dnode = db->db_object;
1466 zde->zde_type = IFTODT(attr->la_mode & S_IFMT);
1468 zapid = osd_get_name_n_idx(env, osd, fid, buf);
1470 rc = -zap_add(osd->od_objset.os, zapid, buf, 8, 1, zde, oh->ot_tx);
1474 /* Add new object to inode accounting.
1475 * Errors are not considered as fatal */
1476 /* XXX: UID/GID must be defined otherwise we can break accounting */
1477 osd_zfs_acct_uid(env, osd, attr->la_uid, 1, oh);
1478 osd_zfs_acct_gid(env, osd, attr->la_gid, 1, oh);
1480 /* configure new osd object */
1482 rc = osd_object_init0(env, obj);
1483 LASSERT(ergo(rc == 0, dt_object_exists(dt)));
1484 LASSERT(osd_invariant(obj));
1486 rc = osd_init_lma(env, obj, fid, oh);
1488 CERROR("%s: can not set LMA on "DFID": rc = %d\n",
1489 osd->od_svname, PFID(fid), rc);
1490 /* ignore errors during LMA initialization */
1499 static int osd_declare_object_ref_add(const struct lu_env *env,
1500 struct dt_object *dt,
1503 return osd_declare_attr_set(env, dt, NULL, th);
1507 * Concurrency: @dt is write locked.
1509 static int osd_object_ref_add(const struct lu_env *env,
1510 struct dt_object *dt,
1511 struct thandle *handle)
1513 struct osd_object *obj = osd_dt_obj(dt);
1514 struct osd_thandle *oh;
1515 struct osd_device *osd = osd_obj2dev(obj);
1516 udmu_objset_t *uos = &osd->od_objset;
1522 LASSERT(osd_invariant(obj));
1523 LASSERT(dt_object_exists(dt));
1524 LASSERT(obj->oo_sa_hdl != NULL);
1526 oh = container_of0(handle, struct osd_thandle, ot_super);
1528 write_lock(&obj->oo_attr_lock);
1529 nlink = ++obj->oo_attr.la_nlink;
1530 write_unlock(&obj->oo_attr_lock);
1532 rc = osd_object_sa_update(obj, SA_ZPL_LINKS(uos), &nlink, 8, oh);
1536 static int osd_declare_object_ref_del(const struct lu_env *env,
1537 struct dt_object *dt,
1538 struct thandle *handle)
1540 return osd_declare_attr_set(env, dt, NULL, handle);
1544 * Concurrency: @dt is write locked.
1546 static int osd_object_ref_del(const struct lu_env *env,
1547 struct dt_object *dt,
1548 struct thandle *handle)
1550 struct osd_object *obj = osd_dt_obj(dt);
1551 struct osd_thandle *oh;
1552 struct osd_device *osd = osd_obj2dev(obj);
1553 udmu_objset_t *uos = &osd->od_objset;
1559 LASSERT(osd_invariant(obj));
1560 LASSERT(dt_object_exists(dt));
1561 LASSERT(obj->oo_sa_hdl != NULL);
1563 oh = container_of0(handle, struct osd_thandle, ot_super);
1564 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1566 write_lock(&obj->oo_attr_lock);
1567 nlink = --obj->oo_attr.la_nlink;
1568 write_unlock(&obj->oo_attr_lock);
1570 rc = osd_object_sa_update(obj, SA_ZPL_LINKS(uos), &nlink, 8, oh);
1574 static int capa_is_sane(const struct lu_env *env, struct osd_device *dev,
1575 struct lustre_capa *capa, struct lustre_capa_key *keys)
1577 struct osd_thread_info *oti = osd_oti_get(env);
1578 struct obd_capa *oc;
1582 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1584 if (capa_is_expired(oc)) {
1585 DEBUG_CAPA(D_ERROR, capa, "expired");
1592 spin_lock(&capa_lock);
1593 for (i = 0; i < 2; i++) {
1594 if (keys[i].lk_keyid == capa->lc_keyid) {
1595 oti->oti_capa_key = keys[i];
1599 spin_unlock(&capa_lock);
1602 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1606 rc = capa_hmac(oti->oti_capa.lc_hmac, capa, oti->oti_capa_key.lk_key);
1609 if (memcmp(oti->oti_capa.lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac)))
1611 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1615 oc = capa_add(dev->od_capa_hash, capa);
1621 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1622 struct lustre_capa *capa, __u64 opc)
1624 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1625 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1628 if (!dev->od_fl_capa)
1631 if (capa == BYPASS_CAPA)
1635 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1639 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1640 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",PFID(fid));
1644 if (!capa_opc_supported(capa, opc)) {
1645 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1649 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1650 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1657 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1658 struct dt_object *dt,
1659 struct lustre_capa *old,
1662 struct osd_thread_info *info = osd_oti_get(env);
1663 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1664 struct osd_object *obj = osd_dt_obj(dt);
1665 struct osd_device *dev = osd_obj2dev(obj);
1666 struct lustre_capa_key *key = &info->oti_capa_key;
1667 struct lustre_capa *capa = &info->oti_capa;
1668 struct obd_capa *oc;
1672 if (!dev->od_fl_capa)
1673 RETURN(ERR_PTR(-ENOENT));
1675 LASSERT(dt_object_exists(dt));
1676 LASSERT(osd_invariant(obj));
1678 /* renewal sanity check */
1679 if (old && osd_object_auth(env, dt, old, opc))
1680 RETURN(ERR_PTR(-EACCES));
1682 capa->lc_fid = *fid;
1685 capa->lc_flags = dev->od_capa_alg << 24;
1686 capa->lc_timeout = dev->od_capa_timeout;
1687 capa->lc_expiry = 0;
1689 oc = capa_lookup(dev->od_capa_hash, capa, 1);
1691 LASSERT(!capa_is_expired(oc));
1695 spin_lock(&capa_lock);
1696 *key = dev->od_capa_keys[1];
1697 spin_unlock(&capa_lock);
1699 capa->lc_keyid = key->lk_keyid;
1700 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
1702 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
1704 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
1706 RETURN(ERR_PTR(rc));
1709 oc = capa_add(dev->od_capa_hash, capa);
1713 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
1715 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1718 /* XXX: no other option than syncing the whole filesystem until we
1720 txg_wait_synced(dmu_objset_pool(osd->od_objset.os), 0ULL);
1725 static struct dt_object_operations osd_obj_ops = {
1726 .do_read_lock = osd_object_read_lock,
1727 .do_write_lock = osd_object_write_lock,
1728 .do_read_unlock = osd_object_read_unlock,
1729 .do_write_unlock = osd_object_write_unlock,
1730 .do_write_locked = osd_object_write_locked,
1731 .do_attr_get = osd_attr_get,
1732 .do_declare_attr_set = osd_declare_attr_set,
1733 .do_attr_set = osd_attr_set,
1734 .do_ah_init = osd_ah_init,
1735 .do_declare_create = osd_declare_object_create,
1736 .do_create = osd_object_create,
1737 .do_declare_destroy = osd_declare_object_destroy,
1738 .do_destroy = osd_object_destroy,
1739 .do_index_try = osd_index_try,
1740 .do_declare_ref_add = osd_declare_object_ref_add,
1741 .do_ref_add = osd_object_ref_add,
1742 .do_declare_ref_del = osd_declare_object_ref_del,
1743 .do_ref_del = osd_object_ref_del,
1744 .do_xattr_get = osd_xattr_get,
1745 .do_declare_xattr_set = osd_declare_xattr_set,
1746 .do_xattr_set = osd_xattr_set,
1747 .do_declare_xattr_del = osd_declare_xattr_del,
1748 .do_xattr_del = osd_xattr_del,
1749 .do_xattr_list = osd_xattr_list,
1750 .do_capa_get = osd_capa_get,
1751 .do_object_sync = osd_object_sync,
1754 static struct lu_object_operations osd_lu_obj_ops = {
1755 .loo_object_init = osd_object_init,
1756 .loo_object_delete = osd_object_delete,
1757 .loo_object_release = osd_object_release,
1758 .loo_object_free = osd_object_free,
1759 .loo_object_print = osd_object_print,
1760 .loo_object_invariant = osd_object_invariant,
1763 static int osd_otable_it_attr_get(const struct lu_env *env,
1764 struct dt_object *dt,
1765 struct lu_attr *attr,
1766 struct lustre_capa *capa)
1772 static struct dt_object_operations osd_obj_otable_it_ops = {
1773 .do_attr_get = osd_otable_it_attr_get,
1774 .do_index_try = osd_index_try,