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, 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>
46 # define EXPORT_SYMTAB
48 #define DEBUG_SUBSYSTEM S_OSD
50 #include <lustre_ver.h>
51 #include <libcfs/libcfs.h>
52 #include <lustre_fsfilt.h>
53 #include <obd_support.h>
54 #include <lustre_net.h>
56 #include <obd_class.h>
57 #include <lustre_disk.h>
58 #include <lustre_fid.h>
60 #include "osd_internal.h"
62 #include <sys/dnode.h>
67 #include <sys/spa_impl.h>
68 #include <sys/zfs_znode.h>
69 #include <sys/dmu_tx.h>
70 #include <sys/dmu_objset.h>
71 #include <sys/dsl_prop.h>
72 #include <sys/sa_impl.h>
75 static char *osd_obj_tag = "osd_object";
77 static struct dt_object_operations osd_obj_ops;
78 static struct lu_object_operations osd_lu_obj_ops;
79 extern struct dt_body_operations osd_body_ops;
80 static struct dt_object_operations osd_obj_otable_it_ops;
82 extern cfs_mem_cache_t *osd_object_kmem;
85 osd_object_sa_fini(struct osd_object *obj)
88 sa_handle_destroy(obj->oo_sa_hdl);
89 obj->oo_sa_hdl = NULL;
94 osd_object_sa_init(struct osd_object *obj, udmu_objset_t *uos)
98 LASSERT(obj->oo_sa_hdl == NULL);
99 LASSERT(obj->oo_db != NULL);
101 rc = -sa_handle_get(uos->os, obj->oo_db->db_object, obj,
102 SA_HDL_PRIVATE, &obj->oo_sa_hdl);
106 /* Cache the xattr object id, valid for the life of the object */
107 rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_XATTR(uos), &obj->oo_xattr, 8);
109 obj->oo_xattr = ZFS_NO_OBJECT;
112 osd_object_sa_fini(obj);
119 * Add object to list of dirty objects in tx handle.
122 osd_object_sa_dirty_add(struct osd_object *obj, struct osd_thandle *oh)
124 if (!cfs_list_empty(&obj->oo_sa_linkage))
127 down(&oh->ot_sa_lock);
128 write_lock(&obj->oo_attr_lock);
129 if (likely(cfs_list_empty(&obj->oo_sa_linkage)))
130 cfs_list_add(&obj->oo_sa_linkage, &oh->ot_sa_list);
131 write_unlock(&obj->oo_attr_lock);
136 * Release spill block dbuf hold for all dirty SAs.
138 void osd_object_sa_dirty_rele(struct osd_thandle *oh)
140 struct osd_object *obj;
142 down(&oh->ot_sa_lock);
143 while (!cfs_list_empty(&oh->ot_sa_list)) {
144 obj = cfs_list_entry(oh->ot_sa_list.next,
145 struct osd_object, oo_sa_linkage);
146 sa_spill_rele(obj->oo_sa_hdl);
147 write_lock(&obj->oo_attr_lock);
148 cfs_list_del_init(&obj->oo_sa_linkage);
149 write_unlock(&obj->oo_attr_lock);
155 * Update the SA and add the object to the dirty list.
157 int osd_object_sa_update(struct osd_object *obj, sa_attr_type_t type,
158 void *buf, uint32_t buflen, struct osd_thandle *oh)
162 LASSERT(obj->oo_sa_hdl != NULL);
163 LASSERT(oh->ot_tx != NULL);
165 rc = -sa_update(obj->oo_sa_hdl, type, buf, buflen, oh->ot_tx);
166 osd_object_sa_dirty_add(obj, oh);
172 * Bulk update the SA and add the object to the dirty list.
175 osd_object_sa_bulk_update(struct osd_object *obj, sa_bulk_attr_t *attrs,
176 int count, struct osd_thandle *oh)
180 LASSERT(obj->oo_sa_hdl != NULL);
181 LASSERT(oh->ot_tx != NULL);
183 rc = -sa_bulk_update(obj->oo_sa_hdl, attrs, count, oh->ot_tx);
184 osd_object_sa_dirty_add(obj, oh);
190 * Retrieve the attributes of a DMU object
192 int __osd_object_attr_get(const struct lu_env *env, udmu_objset_t *uos,
193 struct osd_object *obj, struct lu_attr *la)
195 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
197 sa_bulk_attr_t *bulk;
202 LASSERT(obj->oo_db != NULL);
204 rc = -sa_handle_get(uos->os, obj->oo_db->db_object, NULL,
205 SA_HDL_PRIVATE, &sa_hdl);
209 OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 9);
211 GOTO(out_sa, rc = -ENOMEM);
213 la->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE | LA_TYPE |
214 LA_SIZE | LA_UID | LA_GID | LA_FLAGS | LA_NLINK;
216 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(uos), NULL, osa->atime, 16);
217 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(uos), NULL, osa->mtime, 16);
218 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(uos), NULL, osa->ctime, 16);
219 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(uos), NULL, &osa->mode, 8);
220 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(uos), NULL, &osa->size, 8);
221 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(uos), NULL, &osa->nlink, 8);
222 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(uos), NULL, &osa->uid, 8);
223 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(uos), NULL, &osa->gid, 8);
224 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(uos), NULL, &osa->flags, 8);
226 rc = -sa_bulk_lookup(sa_hdl, bulk, cnt);
230 la->la_atime = osa->atime[0];
231 la->la_mtime = osa->mtime[0];
232 la->la_ctime = osa->ctime[0];
233 la->la_mode = osa->mode;
234 la->la_uid = osa->uid;
235 la->la_gid = osa->gid;
236 la->la_nlink = osa->nlink;
237 la->la_flags = osa->flags;
238 la->la_size = osa->size;
240 if (S_ISCHR(la->la_mode) || S_ISBLK(la->la_mode)) {
241 rc = -sa_lookup(sa_hdl, SA_ZPL_RDEV(uos), &osa->rdev, 8);
244 la->la_rdev = osa->rdev;
245 la->la_valid |= LA_RDEV;
248 OBD_FREE(bulk, sizeof(sa_bulk_attr_t) * 9);
250 sa_handle_destroy(sa_hdl);
255 int __osd_obj2dbuf(const struct lu_env *env, objset_t *os,
256 uint64_t oid, dmu_buf_t **dbp, void *tag)
258 dmu_object_info_t *doi = &osd_oti_get(env)->oti_doi;
263 rc = -sa_buf_hold(os, oid, tag, dbp);
267 dmu_object_info_from_db(*dbp, doi);
268 if (unlikely (oid != DMU_USERUSED_OBJECT &&
269 oid != DMU_GROUPUSED_OBJECT && doi->doi_bonus_type != DMU_OT_SA)) {
270 sa_buf_rele(*dbp, tag);
276 LASSERT((*dbp)->db_object == oid);
277 LASSERT((*dbp)->db_offset == -1);
278 LASSERT((*dbp)->db_data != NULL);
284 * Concurrency: no concurrent access is possible that early in object
287 struct lu_object *osd_object_alloc(const struct lu_env *env,
288 const struct lu_object_header *hdr,
291 struct osd_object *mo;
293 OBD_SLAB_ALLOC_PTR_GFP(mo, osd_object_kmem, CFS_ALLOC_IO);
297 l = &mo->oo_dt.do_lu;
298 dt_object_init(&mo->oo_dt, NULL, d);
299 mo->oo_dt.do_ops = &osd_obj_ops;
300 l->lo_ops = &osd_lu_obj_ops;
301 CFS_INIT_LIST_HEAD(&mo->oo_sa_linkage);
302 init_rwsem(&mo->oo_sem);
303 sema_init(&mo->oo_guard, 1);
304 rwlock_init(&mo->oo_attr_lock);
312 * Concurrency: shouldn't matter.
314 int osd_object_init0(const struct lu_env *env, struct osd_object *obj)
316 struct osd_device *osd = osd_obj2dev(obj);
317 udmu_objset_t *uos = &osd->od_objset;
318 const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
322 if (obj->oo_db == NULL)
327 rc = osd_object_sa_init(obj, uos);
331 /* cache attrs in object */
332 rc = __osd_object_attr_get(env, &osd->od_objset,
337 if (likely(!fid_is_acct(fid)))
338 /* no body operations for accounting objects */
339 obj->oo_dt.do_body_ops = &osd_body_ops;
342 * initialize object before marking it existing
344 obj->oo_dt.do_lu.lo_header->loh_attr |= obj->oo_attr.la_mode & S_IFMT;
347 obj->oo_dt.do_lu.lo_header->loh_attr |= LOHA_EXISTS;
353 * Concurrency: no concurrent access is possible that early in object
356 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
357 const struct lu_object_conf *conf)
359 struct osd_object *obj = osd_obj(l);
360 struct osd_device *osd = osd_obj2dev(obj);
365 LASSERT(osd_invariant(obj));
367 rc = osd_fid_lookup(env, osd, lu_object_fid(l), &oid);
369 LASSERT(obj->oo_db == NULL);
370 rc = __osd_obj2dbuf(env, osd->od_objset.os, oid,
371 &obj->oo_db, osd_obj_tag);
374 rc = osd_object_init0(env, obj);
376 CERROR("%s: lookup "DFID"/"LPX64" failed: rc = %d\n",
377 osd->od_svname, PFID(lu_object_fid(l)), oid, rc);
379 } else if (rc == -ENOENT) {
380 if (fid_is_otable_it(&l->lo_header->loh_fid)) {
381 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
382 /* LFSCK iterator object is special without inode */
383 l->lo_header->loh_attr |= LOHA_EXISTS;
387 LASSERT(osd_invariant(obj));
392 * Concurrency: no concurrent access is possible that late in object
395 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
397 struct osd_object *obj = osd_obj(l);
399 LASSERT(osd_invariant(obj));
401 dt_object_fini(&obj->oo_dt);
402 OBD_SLAB_FREE_PTR(obj, osd_object_kmem);
405 static void __osd_declare_object_destroy(const struct lu_env *env,
406 struct osd_object *obj,
407 struct osd_thandle *oh)
409 struct osd_device *osd = osd_obj2dev(obj);
410 udmu_objset_t *uos = &osd->od_objset;
411 dmu_buf_t *db = obj->oo_db;
412 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
413 uint64_t oid = db->db_object, xid;
414 dmu_tx_t *tx = oh->ot_tx;
418 dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
420 /* zap holding xattrs */
421 if (obj->oo_xattr != ZFS_NO_OBJECT) {
424 dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
426 rc = -udmu_zap_cursor_init(&zc, uos, oid, 0);
430 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
431 BUG_ON(za->za_integer_length != sizeof(uint64_t));
432 BUG_ON(za->za_num_integers != 1);
434 rc = -zap_lookup(uos->os, obj->oo_xattr, za->za_name,
435 sizeof(uint64_t), 1, &xid);
437 CERROR("%s: xattr lookup failed: rc = %d\n",
441 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
443 zap_cursor_advance(zc);
448 udmu_zap_cursor_fini(zc);
451 if (rc && tx->tx_err == 0)
455 static int osd_declare_object_destroy(const struct lu_env *env,
456 struct dt_object *dt,
459 char *buf = osd_oti_get(env)->oti_str;
460 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
461 struct osd_object *obj = osd_dt_obj(dt);
462 struct osd_device *osd = osd_obj2dev(obj);
463 struct osd_thandle *oh;
469 LASSERT(dt_object_exists(dt));
471 oh = container_of0(th, struct osd_thandle, ot_super);
472 LASSERT(oh->ot_tx != NULL);
474 /* declare that we'll destroy the object */
475 __osd_declare_object_destroy(env, obj, oh);
477 /* declare that we'll remove object from fid-dnode mapping */
478 zapid = osd_get_name_n_idx(env, osd, fid, buf);
479 dmu_tx_hold_bonus(oh->ot_tx, zapid);
480 dmu_tx_hold_zap(oh->ot_tx, zapid, 0, buf);
482 /* declare that we'll remove object from inode accounting ZAPs */
483 dmu_tx_hold_bonus(oh->ot_tx, osd->od_iusr_oid);
484 dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, 0, buf);
485 dmu_tx_hold_bonus(oh->ot_tx, osd->od_igrp_oid);
486 dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, 0, buf);
489 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
490 obj->oo_attr.la_gid, -1, oh, false, NULL, false);
494 /* data to be truncated */
495 rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
496 obj->oo_attr.la_gid, 0, oh, true, NULL, false);
500 int __osd_object_free(udmu_objset_t *uos, uint64_t oid, dmu_tx_t *tx)
502 LASSERT(uos->objects != 0);
503 spin_lock(&uos->lock);
505 spin_unlock(&uos->lock);
507 return -dmu_object_free(uos->os, oid, tx);
511 * Delete a DMU object
513 * The transaction passed to this routine must have
514 * dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END) called
515 * and then assigned to a transaction group.
517 * This will release db and set it to NULL to prevent further dbuf releases.
519 static int __osd_object_destroy(const struct lu_env *env,
520 struct osd_object *obj,
521 dmu_tx_t *tx, void *tag)
523 struct osd_device *osd = osd_obj2dev(obj);
524 udmu_objset_t *uos = &osd->od_objset;
526 zap_attribute_t *za = &osd_oti_get(env)->oti_za;
530 /* Assert that the transaction has been assigned to a
531 transaction group. */
532 LASSERT(tx->tx_txg != 0);
534 /* zap holding xattrs */
535 if (obj->oo_xattr != ZFS_NO_OBJECT) {
536 rc = -udmu_zap_cursor_init(&zc, uos, obj->oo_xattr, 0);
539 while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
540 BUG_ON(za->za_integer_length != sizeof(uint64_t));
541 BUG_ON(za->za_num_integers != 1);
543 rc = -zap_lookup(uos->os, obj->oo_xattr, za->za_name,
544 sizeof(uint64_t), 1, &xid);
546 CERROR("%s: lookup xattr %s failed: rc = %d\n",
547 osd->od_svname, za->za_name, rc);
550 rc = __osd_object_free(uos, xid, tx);
552 CERROR("%s: fetch xattr %s failed: rc = %d\n",
553 osd->od_svname, za->za_name, rc);
554 zap_cursor_advance(zc);
556 udmu_zap_cursor_fini(zc);
558 rc = __osd_object_free(uos, obj->oo_xattr, tx);
560 CERROR("%s: freeing xattr failed: rc = %d\n",
564 return __osd_object_free(uos, obj->oo_db->db_object, tx);
567 static int osd_object_destroy(const struct lu_env *env,
568 struct dt_object *dt,
571 char *buf = osd_oti_get(env)->oti_str;
572 struct osd_object *obj = osd_dt_obj(dt);
573 struct osd_device *osd = osd_obj2dev(obj);
574 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
575 struct osd_thandle *oh;
580 LASSERT(obj->oo_db != NULL);
581 LASSERT(dt_object_exists(dt));
582 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
584 oh = container_of0(th, struct osd_thandle, ot_super);
586 LASSERT(oh->ot_tx != NULL);
588 zapid = osd_get_name_n_idx(env, osd, fid, buf);
590 /* remove obj ref from index dir (it depends) */
591 rc = -zap_remove(osd->od_objset.os, zapid, buf, oh->ot_tx);
593 CERROR("%s: zap_remove() failed: rc = %d\n",
598 /* Remove object from inode accounting. It is not fatal for the destroy
599 * operation if something goes wrong while updating accounting, but we
600 * still log an error message to notify the administrator */
601 rc = -zap_increment_int(osd->od_objset.os, osd->od_iusr_oid,
602 obj->oo_attr.la_uid, -1, oh->ot_tx);
604 CERROR("%s: failed to remove "DFID" from accounting ZAP for usr"
605 " %d: rc = %d\n", osd->od_svname, PFID(fid),
606 obj->oo_attr.la_uid, rc);
607 rc = -zap_increment_int(osd->od_objset.os, osd->od_igrp_oid,
608 obj->oo_attr.la_gid, -1, oh->ot_tx);
610 CERROR("%s: failed to remove "DFID" from accounting ZAP for grp"
611 " %d: rc = %d\n", osd->od_svname, PFID(fid),
612 obj->oo_attr.la_gid, rc);
615 rc = __osd_object_destroy(env, obj, oh->ot_tx, osd_obj_tag);
617 CERROR("%s: __osd_object_destroy() failed: rc = %d\n",
623 /* not needed in the cache anymore */
624 set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
629 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
631 struct osd_object *obj = osd_obj(l);
633 if (obj->oo_db != NULL) {
634 osd_object_sa_fini(obj);
635 if (obj->oo_sa_xattr) {
636 nvlist_free(obj->oo_sa_xattr);
637 obj->oo_sa_xattr = NULL;
639 sa_buf_rele(obj->oo_db, osd_obj_tag);
640 cfs_list_del(&obj->oo_sa_linkage);
646 * Concurrency: ->loo_object_release() is called under site spin-lock.
648 static void osd_object_release(const struct lu_env *env,
654 * Concurrency: shouldn't matter.
656 static int osd_object_print(const struct lu_env *env, void *cookie,
657 lu_printer_t p, const struct lu_object *l)
659 struct osd_object *o = osd_obj(l);
661 return (*p)(env, cookie, LUSTRE_OSD_ZFS_NAME"-object@%p", o);
664 static void osd_object_read_lock(const struct lu_env *env,
665 struct dt_object *dt, unsigned role)
667 struct osd_object *obj = osd_dt_obj(dt);
669 LASSERT(osd_invariant(obj));
671 down_read(&obj->oo_sem);
674 static void osd_object_write_lock(const struct lu_env *env,
675 struct dt_object *dt, unsigned role)
677 struct osd_object *obj = osd_dt_obj(dt);
679 LASSERT(osd_invariant(obj));
681 down_write(&obj->oo_sem);
684 static void osd_object_read_unlock(const struct lu_env *env,
685 struct dt_object *dt)
687 struct osd_object *obj = osd_dt_obj(dt);
689 LASSERT(osd_invariant(obj));
690 up_read(&obj->oo_sem);
693 static void osd_object_write_unlock(const struct lu_env *env,
694 struct dt_object *dt)
696 struct osd_object *obj = osd_dt_obj(dt);
698 LASSERT(osd_invariant(obj));
699 up_write(&obj->oo_sem);
702 static int osd_object_write_locked(const struct lu_env *env,
703 struct dt_object *dt)
705 struct osd_object *obj = osd_dt_obj(dt);
708 LASSERT(osd_invariant(obj));
710 if (down_write_trylock(&obj->oo_sem)) {
712 up_write(&obj->oo_sem);
717 static int osd_attr_get(const struct lu_env *env,
718 struct dt_object *dt,
719 struct lu_attr *attr,
720 struct lustre_capa *capa)
722 struct osd_object *obj = osd_dt_obj(dt);
726 LASSERT(dt_object_exists(dt));
727 LASSERT(osd_invariant(obj));
730 read_lock(&obj->oo_attr_lock);
731 *attr = obj->oo_attr;
732 read_unlock(&obj->oo_attr_lock);
734 /* with ZFS_DEBUG zrl_add_debug() called by DB_DNODE_ENTER()
735 * from within sa_object_size() can block on a mutex, so
736 * we can't call sa_object_size() holding rwlock */
737 sa_object_size(obj->oo_sa_hdl, &blksize, &blocks);
738 /* we do not control size of indices, so always calculate
739 * it from number of blocks reported by DMU */
740 if (S_ISDIR(attr->la_mode))
741 attr->la_size = 512 * blocks;
742 /* Block size may be not set; suggest maximal I/O transfers. */
744 blksize = 1ULL << SPA_MAXBLOCKSHIFT;
746 attr->la_blksize = blksize;
747 attr->la_blocks = blocks;
748 attr->la_valid |= LA_BLOCKS | LA_BLKSIZE;
753 /* Simple wrapper on top of qsd API which implement quota transfer for osd
754 * setattr needs. As a reminder, only the root user can change ownership of
755 * a file, that's why EDQUOT & EINPROGRESS errors are discarded */
756 static inline int qsd_transfer(const struct lu_env *env,
757 struct qsd_instance *qsd,
758 struct lquota_trans *trans, int qtype,
759 __u64 orig_id, __u64 new_id, __u64 bspace,
760 struct lquota_id_info *qi)
764 if (unlikely(qsd == NULL))
767 LASSERT(qtype >= 0 && qtype < MAXQUOTAS);
768 qi->lqi_type = qtype;
770 /* inode accounting */
771 qi->lqi_is_blk = false;
773 /* one more inode for the new owner ... */
774 qi->lqi_id.qid_uid = new_id;
776 rc = qsd_op_begin(env, qsd, trans, qi, NULL);
777 if (rc == -EDQUOT || rc == -EINPROGRESS)
782 /* and one less inode for the current id */
783 qi->lqi_id.qid_uid = orig_id;;
785 /* can't get EDQUOT when reducing usage */
786 rc = qsd_op_begin(env, qsd, trans, qi, NULL);
787 if (rc == -EINPROGRESS)
792 /* block accounting */
793 qi->lqi_is_blk = true;
795 /* more blocks for the new owner ... */
796 qi->lqi_id.qid_uid = new_id;
797 qi->lqi_space = bspace;
798 rc = qsd_op_begin(env, qsd, trans, qi, NULL);
799 if (rc == -EDQUOT || rc == -EINPROGRESS)
804 /* and finally less blocks for the current owner */
805 qi->lqi_id.qid_uid = orig_id;
806 qi->lqi_space = -bspace;
807 rc = qsd_op_begin(env, qsd, trans, qi, NULL);
808 /* can't get EDQUOT when reducing usage */
809 if (rc == -EINPROGRESS)
814 static int osd_declare_attr_set(const struct lu_env *env,
815 struct dt_object *dt,
816 const struct lu_attr *attr,
817 struct thandle *handle)
819 struct osd_thread_info *info = osd_oti_get(env);
820 char *buf = osd_oti_get(env)->oti_str;
821 struct osd_object *obj = osd_dt_obj(dt);
822 struct osd_device *osd = osd_obj2dev(obj);
823 struct osd_thandle *oh;
829 if (!dt_object_exists(dt)) {
830 /* XXX: sanity check that object creation is declared */
834 LASSERT(handle != NULL);
835 LASSERT(osd_invariant(obj));
837 oh = container_of0(handle, struct osd_thandle, ot_super);
839 LASSERT(obj->oo_sa_hdl != NULL);
840 dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
842 sa_object_size(obj->oo_sa_hdl, &blksize, &bspace);
843 bspace = toqb(bspace * blksize);
845 if (attr && attr->la_valid & LA_UID) {
846 /* account for user inode tracking ZAP update */
847 dmu_tx_hold_bonus(oh->ot_tx, osd->od_iusr_oid);
848 dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, TRUE, buf);
850 /* quota enforcement for user */
851 if (attr->la_uid != obj->oo_attr.la_uid) {
852 rc = qsd_transfer(env, osd->od_quota_slave,
853 &oh->ot_quota_trans, USRQUOTA,
854 obj->oo_attr.la_uid, attr->la_uid,
855 bspace, &info->oti_qi);
860 if (attr && attr->la_valid & LA_GID) {
861 /* account for user inode tracking ZAP update */
862 dmu_tx_hold_bonus(oh->ot_tx, osd->od_igrp_oid);
863 dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, TRUE, buf);
865 /* quota enforcement for group */
866 if (attr->la_gid != obj->oo_attr.la_gid) {
867 rc = qsd_transfer(env, osd->od_quota_slave,
868 &oh->ot_quota_trans, GRPQUOTA,
869 obj->oo_attr.la_gid, attr->la_gid,
870 bspace, &info->oti_qi);
880 * Set the attributes of an object
882 * The transaction passed to this routine must have
883 * dmu_tx_hold_bonus(tx, oid) called and then assigned
884 * to a transaction group.
886 static int osd_attr_set(const struct lu_env *env, struct dt_object *dt,
887 const struct lu_attr *la, struct thandle *handle,
888 struct lustre_capa *capa)
890 struct osd_object *obj = osd_dt_obj(dt);
891 struct osd_device *osd = osd_obj2dev(obj);
892 udmu_objset_t *uos = &osd->od_objset;
893 struct osd_thandle *oh;
894 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
895 sa_bulk_attr_t *bulk;
900 LASSERT(handle != NULL);
901 LASSERT(dt_object_exists(dt));
902 LASSERT(osd_invariant(obj));
903 LASSERT(obj->oo_sa_hdl);
905 oh = container_of0(handle, struct osd_thandle, ot_super);
906 /* Assert that the transaction has been assigned to a
907 transaction group. */
908 LASSERT(oh->ot_tx->tx_txg != 0);
910 if (la->la_valid == 0)
913 OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 10);
917 /* do both accounting updates outside oo_attr_lock below */
918 if ((la->la_valid & LA_UID) && (la->la_uid != obj->oo_attr.la_uid)) {
919 /* Update user accounting. Failure isn't fatal, but we still
920 * log an error message */
921 rc = -zap_increment_int(osd->od_objset.os, osd->od_iusr_oid,
922 la->la_uid, 1, oh->ot_tx);
924 CERROR("%s: failed to update accounting ZAP for user "
925 "%d (%d)\n", osd->od_svname, la->la_uid, rc);
926 rc = -zap_increment_int(osd->od_objset.os, osd->od_iusr_oid,
927 obj->oo_attr.la_uid, -1, oh->ot_tx);
929 CERROR("%s: failed to update accounting ZAP for user "
930 "%d (%d)\n", osd->od_svname,
931 obj->oo_attr.la_uid, rc);
933 if ((la->la_valid & LA_GID) && (la->la_gid != obj->oo_attr.la_gid)) {
934 /* Update group accounting. Failure isn't fatal, but we still
935 * log an error message */
936 rc = -zap_increment_int(osd->od_objset.os, osd->od_igrp_oid,
937 la->la_gid, 1, oh->ot_tx);
939 CERROR("%s: failed to update accounting ZAP for user "
940 "%d (%d)\n", osd->od_svname, la->la_gid, rc);
941 rc = -zap_increment_int(osd->od_objset.os, osd->od_igrp_oid,
942 obj->oo_attr.la_gid, -1, oh->ot_tx);
944 CERROR("%s: failed to update accounting ZAP for user "
945 "%d (%d)\n", osd->od_svname,
946 obj->oo_attr.la_gid, rc);
949 write_lock(&obj->oo_attr_lock);
951 if (la->la_valid & LA_ATIME) {
952 osa->atime[0] = obj->oo_attr.la_atime = la->la_atime;
953 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(uos), NULL,
956 if (la->la_valid & LA_MTIME) {
957 osa->mtime[0] = obj->oo_attr.la_mtime = la->la_mtime;
958 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(uos), NULL,
961 if (la->la_valid & LA_CTIME) {
962 osa->ctime[0] = obj->oo_attr.la_ctime = la->la_ctime;
963 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(uos), NULL,
966 if (la->la_valid & LA_MODE) {
967 /* mode is stored along with type, so read it first */
968 obj->oo_attr.la_mode = (obj->oo_attr.la_mode & S_IFMT) |
969 (la->la_mode & ~S_IFMT);
970 osa->mode = obj->oo_attr.la_mode;
971 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(uos), NULL,
974 if (la->la_valid & LA_SIZE) {
975 osa->size = obj->oo_attr.la_size = la->la_size;
976 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(uos), NULL,
979 if (la->la_valid & LA_NLINK) {
980 osa->nlink = obj->oo_attr.la_nlink = la->la_nlink;
981 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(uos), NULL,
984 if (la->la_valid & LA_RDEV) {
985 osa->rdev = obj->oo_attr.la_rdev = la->la_rdev;
986 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(uos), NULL,
989 if (la->la_valid & LA_FLAGS) {
990 osa->flags = obj->oo_attr.la_flags = la->la_flags;
991 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(uos), NULL,
994 if (la->la_valid & LA_UID) {
995 osa->uid = obj->oo_attr.la_uid = la->la_uid;
996 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(uos), NULL,
999 if (la->la_valid & LA_GID) {
1000 osa->gid = obj->oo_attr.la_gid = la->la_gid;
1001 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(uos), NULL,
1004 obj->oo_attr.la_valid |= la->la_valid;
1005 write_unlock(&obj->oo_attr_lock);
1007 rc = osd_object_sa_bulk_update(obj, bulk, cnt, oh);
1009 OBD_FREE(bulk, sizeof(sa_bulk_attr_t) * 10);
1016 * XXX temporary solution.
1019 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1020 struct dt_object *parent, struct dt_object *child,
1021 cfs_umode_t child_mode)
1025 memset(ah, 0, sizeof(*ah));
1026 ah->dah_mode = child_mode;
1029 static int osd_declare_object_create(const struct lu_env *env,
1030 struct dt_object *dt,
1031 struct lu_attr *attr,
1032 struct dt_allocation_hint *hint,
1033 struct dt_object_format *dof,
1034 struct thandle *handle)
1036 char *buf = osd_oti_get(env)->oti_str;
1037 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1038 struct osd_object *obj = osd_dt_obj(dt);
1039 struct osd_device *osd = osd_obj2dev(obj);
1040 struct osd_thandle *oh;
1047 switch (dof->dof_type) {
1051 if (obj->oo_dt.do_body_ops == NULL)
1052 obj->oo_dt.do_body_ops = &osd_body_ops;
1058 LASSERT(handle != NULL);
1059 oh = container_of0(handle, struct osd_thandle, ot_super);
1060 LASSERT(oh->ot_tx != NULL);
1062 switch (dof->dof_type) {
1065 /* for zap create */
1066 dmu_tx_hold_zap(oh->ot_tx, DMU_NEW_OBJECT, 1, NULL);
1071 /* first, we'll create new object */
1072 dmu_tx_hold_bonus(oh->ot_tx, DMU_NEW_OBJECT);
1080 /* and we'll add it to some mapping */
1081 zapid = osd_get_name_n_idx(env, osd, fid, buf);
1082 dmu_tx_hold_bonus(oh->ot_tx, zapid);
1083 dmu_tx_hold_zap(oh->ot_tx, zapid, TRUE, buf);
1085 /* we will also update inode accounting ZAPs */
1086 dmu_tx_hold_bonus(oh->ot_tx, osd->od_iusr_oid);
1087 dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, TRUE, buf);
1088 dmu_tx_hold_bonus(oh->ot_tx, osd->od_igrp_oid);
1089 dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, TRUE, buf);
1091 dmu_tx_hold_sa_create(oh->ot_tx, ZFS_SA_BASE_ATTR_SIZE);
1093 __osd_xattr_declare_set(env, obj, sizeof(struct lustre_mdt_attrs),
1094 XATTR_NAME_LMA, oh);
1096 rc = osd_declare_quota(env, osd, attr->la_uid, attr->la_gid, 1, oh,
1097 false, NULL, false);
1101 int __osd_attr_init(const struct lu_env *env, udmu_objset_t *uos,
1102 uint64_t oid, dmu_tx_t *tx, struct lu_attr *la)
1104 sa_bulk_attr_t *bulk;
1105 sa_handle_t *sa_hdl;
1106 struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
1115 gen = dmu_tx_get_txg(tx);
1117 ZFS_TIME_ENCODE(&now, crtime);
1118 /* XXX: this should be real id of parent for ZPL access, but we have no
1119 * such info in OSD, probably it can be part of dt_object_format */
1122 osa->atime[0] = la->la_atime;
1123 osa->ctime[0] = la->la_ctime;
1124 osa->mtime[0] = la->la_mtime;
1125 osa->mode = la->la_mode;
1126 osa->uid = la->la_uid;
1127 osa->gid = la->la_gid;
1128 osa->rdev = la->la_rdev;
1129 osa->nlink = la->la_nlink;
1130 osa->flags = la->la_flags;
1131 osa->size = la->la_size;
1133 /* Now add in all of the "SA" attributes */
1134 rc = -sa_handle_get(uos->os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
1138 OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 13);
1144 * we need to create all SA below upon object create.
1146 * XXX The attribute order matters since the accounting callback relies
1147 * on static offsets (i.e. SA_*_OFFSET, see zfs_space_delta_cb()) to
1148 * look up the UID/GID attributes. Moreover, the callback does not seem
1149 * to support the spill block.
1150 * We define attributes in the same order as SA_*_OFFSET in order to
1151 * work around the problem. See ORI-610.
1154 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(uos), NULL, &osa->mode, 8);
1155 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(uos), NULL, &osa->size, 8);
1156 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(uos), NULL, &gen, 8);
1157 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(uos), NULL, &osa->uid, 8);
1158 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(uos), NULL, &osa->gid, 8);
1159 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(uos), NULL, &parent, 8);
1160 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(uos), NULL, &osa->flags, 8);
1161 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(uos), NULL, osa->atime, 16);
1162 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(uos), NULL, osa->mtime, 16);
1163 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(uos), NULL, osa->ctime, 16);
1164 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(uos), NULL, crtime, 16);
1165 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(uos), NULL, &osa->nlink, 8);
1166 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(uos), NULL, &osa->rdev, 8);
1168 rc = -sa_replace_all_by_template(sa_hdl, bulk, cnt, tx);
1170 OBD_FREE(bulk, sizeof(sa_bulk_attr_t) * 13);
1172 sa_handle_destroy(sa_hdl);
1177 * The transaction passed to this routine must have
1178 * dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT) called and then assigned
1179 * to a transaction group.
1181 int __osd_object_create(const struct lu_env *env, udmu_objset_t *uos,
1182 dmu_buf_t **dbp, dmu_tx_t *tx,
1183 struct lu_attr *la, void *tag)
1189 spin_lock(&uos->lock);
1191 spin_unlock(&uos->lock);
1193 /* Assert that the transaction has been assigned to a
1194 transaction group. */
1195 LASSERT(tx->tx_txg != 0);
1197 /* Create a new DMU object. */
1198 oid = dmu_object_alloc(uos->os, DMU_OT_PLAIN_FILE_CONTENTS, 0,
1199 DMU_OT_SA, DN_MAX_BONUSLEN, tx);
1200 rc = -sa_buf_hold(uos->os, oid, tag, dbp);
1204 LASSERT(la->la_valid & LA_MODE);
1208 return __osd_attr_init(env, uos, oid, tx, la);
1212 * The transaction passed to this routine must have
1213 * dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, ...) called and then assigned
1214 * to a transaction group.
1216 * Using ZAP_FLAG_HASH64 will force the ZAP to always be a FAT ZAP.
1217 * This is fine for directories today, because storing the FID in the dirent
1218 * will also require a FAT ZAP. If there is a new type of micro ZAP created
1219 * then we might need to re-evaluate the use of this flag and instead do
1220 * a conversion from the different internal ZAP hash formats being used. */
1221 int __osd_zap_create(const struct lu_env *env, udmu_objset_t *uos,
1222 dmu_buf_t **zap_dbp, dmu_tx_t *tx,
1223 struct lu_attr *la, void *tag, zap_flags_t flags)
1230 spin_lock(&uos->lock);
1232 spin_unlock(&uos->lock);
1234 /* Assert that the transaction has been assigned to a
1235 transaction group. */
1236 LASSERT(tx->tx_txg != 0);
1238 oid = zap_create_flags(uos->os, 0, flags | ZAP_FLAG_HASH64,
1239 DMU_OT_DIRECTORY_CONTENTS, 12, 12,
1240 DMU_OT_SA, DN_MAX_BONUSLEN, tx);
1242 rc = -sa_buf_hold(uos->os, oid, tag, zap_dbp);
1246 LASSERT(la->la_valid & LA_MODE);
1250 return __osd_attr_init(env, uos, oid, tx, la);
1253 static dmu_buf_t *osd_mkidx(const struct lu_env *env, struct osd_device *osd,
1254 struct lu_attr *la, struct osd_thandle *oh)
1259 /* Index file should be created as regular file in order not to confuse
1260 * ZPL which could interpret them as directory.
1261 * We set ZAP_FLAG_UINT64_KEY to let ZFS know than we are going to use
1263 LASSERT(S_ISREG(la->la_mode));
1264 rc = __osd_zap_create(env, &osd->od_objset, &db, oh->ot_tx, la,
1265 osd_obj_tag, ZAP_FLAG_UINT64_KEY);
1271 static dmu_buf_t *osd_mkdir(const struct lu_env *env, struct osd_device *osd,
1272 struct lu_attr *la, struct osd_thandle *oh)
1277 LASSERT(S_ISDIR(la->la_mode));
1278 rc = __osd_zap_create(env, &osd->od_objset, &db, oh->ot_tx, la,
1285 static dmu_buf_t* osd_mkreg(const struct lu_env *env, struct osd_device *osd,
1286 struct lu_attr *la, struct osd_thandle *oh)
1291 LASSERT(S_ISREG(la->la_mode));
1292 rc = __osd_object_create(env, &osd->od_objset, &db, oh->ot_tx, la,
1298 * XXX: a hack, OST to use bigger blocksize. we need
1299 * a method in OSD API to control this from OFD/MDD
1301 if (!lu_device_is_md(osd2lu_dev(osd))) {
1302 rc = -dmu_object_set_blocksize(osd->od_objset.os,
1304 128 << 10, 0, oh->ot_tx);
1306 CERROR("%s: can't change blocksize: %d\n",
1307 osd->od_svname, rc);
1315 static dmu_buf_t *osd_mksym(const struct lu_env *env, struct osd_device *osd,
1316 struct lu_attr *la, struct osd_thandle *oh)
1321 LASSERT(S_ISLNK(la->la_mode));
1322 rc = __osd_object_create(env, &osd->od_objset, &db, oh->ot_tx, la,
1329 static dmu_buf_t *osd_mknod(const struct lu_env *env, struct osd_device *osd,
1330 struct lu_attr *la, struct osd_thandle *oh)
1335 la->la_valid = LA_MODE;
1336 if (S_ISCHR(la->la_mode) || S_ISBLK(la->la_mode))
1337 la->la_valid |= LA_RDEV;
1339 rc = __osd_object_create(env, &osd->od_objset, &db, oh->ot_tx, la,
1346 typedef dmu_buf_t *(*osd_obj_type_f)(const struct lu_env *env, struct osd_device *osd,
1347 struct lu_attr *la, struct osd_thandle *oh);
1349 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1351 osd_obj_type_f result;
1377 * Primitives for directory (i.e. ZAP) handling
1379 static inline int osd_init_lma(const struct lu_env *env, struct osd_object *obj,
1380 const struct lu_fid *fid, struct osd_thandle *oh)
1382 struct osd_thread_info *info = osd_oti_get(env);
1383 struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
1387 lustre_lma_init(lma, fid);
1388 lustre_lma_swab(lma);
1390 buf.lb_len = sizeof(*lma);
1392 rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
1393 LU_XATTR_CREATE, oh, BYPASS_CAPA);
1399 * Concurrency: @dt is write locked.
1401 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1402 struct lu_attr *attr,
1403 struct dt_allocation_hint *hint,
1404 struct dt_object_format *dof,
1407 struct zpl_direntry *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
1408 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1409 struct osd_object *obj = osd_dt_obj(dt);
1410 struct osd_device *osd = osd_obj2dev(obj);
1411 char *buf = osd_oti_get(env)->oti_str;
1412 struct osd_thandle *oh;
1419 /* concurrent create declarations should not see
1420 * the object inconsistent (db, attr, etc).
1421 * in regular cases acquisition should be cheap */
1422 down(&obj->oo_guard);
1424 LASSERT(osd_invariant(obj));
1425 LASSERT(!dt_object_exists(dt));
1426 LASSERT(dof != NULL);
1428 LASSERT(th != NULL);
1429 oh = container_of0(th, struct osd_thandle, ot_super);
1432 * XXX missing: Quote handling.
1435 LASSERT(obj->oo_db == NULL);
1437 db = osd_create_type_f(dof->dof_type)(env, osd, attr, oh);
1439 GOTO(out, rc = PTR_ERR(th));
1442 zde->zde_dnode = db->db_object;
1443 zde->zde_type = IFTODT(attr->la_mode & S_IFMT);
1445 zapid = osd_get_name_n_idx(env, osd, fid, buf);
1447 rc = -zap_add(osd->od_objset.os, zapid, buf, 8, 1, zde, oh->ot_tx);
1451 /* Add new object to inode accounting.
1452 * Errors are not considered as fatal */
1453 rc = -zap_increment_int(osd->od_objset.os, osd->od_iusr_oid,
1454 (attr->la_valid & LA_UID) ? attr->la_uid : 0, 1,
1457 CERROR("%s: failed to add "DFID" to accounting ZAP for usr %d "
1458 "(%d)\n", osd->od_svname, PFID(fid), attr->la_uid, rc);
1459 rc = -zap_increment_int(osd->od_objset.os, osd->od_igrp_oid,
1460 (attr->la_valid & LA_GID) ? attr->la_gid : 0, 1,
1463 CERROR("%s: failed to add "DFID" to accounting ZAP for grp %d "
1464 "(%d)\n", osd->od_svname, PFID(fid), attr->la_gid, rc);
1466 /* configure new osd object */
1468 rc = osd_object_init0(env, obj);
1469 LASSERT(ergo(rc == 0, dt_object_exists(dt)));
1470 LASSERT(osd_invariant(obj));
1472 rc = osd_init_lma(env, obj, fid, oh);
1474 CERROR("%s: can not set LMA on "DFID": rc = %d\n",
1475 osd->od_svname, PFID(fid), rc);
1476 /* ignore errors during LMA initialization */
1485 static int osd_declare_object_ref_add(const struct lu_env *env,
1486 struct dt_object *dt,
1489 return osd_declare_attr_set(env, dt, NULL, th);
1493 * Concurrency: @dt is write locked.
1495 static int osd_object_ref_add(const struct lu_env *env,
1496 struct dt_object *dt,
1497 struct thandle *handle)
1499 struct osd_object *obj = osd_dt_obj(dt);
1500 struct osd_thandle *oh;
1501 struct osd_device *osd = osd_obj2dev(obj);
1502 udmu_objset_t *uos = &osd->od_objset;
1508 LASSERT(osd_invariant(obj));
1509 LASSERT(dt_object_exists(dt));
1510 LASSERT(obj->oo_sa_hdl != NULL);
1512 oh = container_of0(handle, struct osd_thandle, ot_super);
1514 write_lock(&obj->oo_attr_lock);
1515 nlink = ++obj->oo_attr.la_nlink;
1516 write_unlock(&obj->oo_attr_lock);
1518 rc = osd_object_sa_update(obj, SA_ZPL_LINKS(uos), &nlink, 8, oh);
1522 static int osd_declare_object_ref_del(const struct lu_env *env,
1523 struct dt_object *dt,
1524 struct thandle *handle)
1526 return osd_declare_attr_set(env, dt, NULL, handle);
1530 * Concurrency: @dt is write locked.
1532 static int osd_object_ref_del(const struct lu_env *env,
1533 struct dt_object *dt,
1534 struct thandle *handle)
1536 struct osd_object *obj = osd_dt_obj(dt);
1537 struct osd_thandle *oh;
1538 struct osd_device *osd = osd_obj2dev(obj);
1539 udmu_objset_t *uos = &osd->od_objset;
1545 LASSERT(osd_invariant(obj));
1546 LASSERT(dt_object_exists(dt));
1547 LASSERT(obj->oo_sa_hdl != NULL);
1549 oh = container_of0(handle, struct osd_thandle, ot_super);
1550 LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1552 write_lock(&obj->oo_attr_lock);
1553 nlink = --obj->oo_attr.la_nlink;
1554 write_unlock(&obj->oo_attr_lock);
1556 rc = osd_object_sa_update(obj, SA_ZPL_LINKS(uos), &nlink, 8, oh);
1560 static int capa_is_sane(const struct lu_env *env, struct osd_device *dev,
1561 struct lustre_capa *capa, struct lustre_capa_key *keys)
1563 struct osd_thread_info *oti = osd_oti_get(env);
1564 struct obd_capa *oc;
1568 oc = capa_lookup(dev->od_capa_hash, capa, 0);
1570 if (capa_is_expired(oc)) {
1571 DEBUG_CAPA(D_ERROR, capa, "expired");
1578 spin_lock(&capa_lock);
1579 for (i = 0; i < 2; i++) {
1580 if (keys[i].lk_keyid == capa->lc_keyid) {
1581 oti->oti_capa_key = keys[i];
1585 spin_unlock(&capa_lock);
1588 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1592 rc = capa_hmac(oti->oti_capa.lc_hmac, capa, oti->oti_capa_key.lk_key);
1595 if (memcmp(oti->oti_capa.lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac)))
1597 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1601 oc = capa_add(dev->od_capa_hash, capa);
1607 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1608 struct lustre_capa *capa, __u64 opc)
1610 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1611 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1614 if (!dev->od_fl_capa)
1617 if (capa == BYPASS_CAPA)
1621 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1625 if (!lu_fid_eq(fid, &capa->lc_fid)) {
1626 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",PFID(fid));
1630 if (!capa_opc_supported(capa, opc)) {
1631 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1635 if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1636 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1643 static struct obd_capa *osd_capa_get(const struct lu_env *env,
1644 struct dt_object *dt,
1645 struct lustre_capa *old,
1648 struct osd_thread_info *info = osd_oti_get(env);
1649 const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1650 struct osd_object *obj = osd_dt_obj(dt);
1651 struct osd_device *dev = osd_obj2dev(obj);
1652 struct lustre_capa_key *key = &info->oti_capa_key;
1653 struct lustre_capa *capa = &info->oti_capa;
1654 struct obd_capa *oc;
1658 if (!dev->od_fl_capa)
1659 RETURN(ERR_PTR(-ENOENT));
1661 LASSERT(dt_object_exists(dt));
1662 LASSERT(osd_invariant(obj));
1664 /* renewal sanity check */
1665 if (old && osd_object_auth(env, dt, old, opc))
1666 RETURN(ERR_PTR(-EACCES));
1668 capa->lc_fid = *fid;
1671 capa->lc_flags = dev->od_capa_alg << 24;
1672 capa->lc_timeout = dev->od_capa_timeout;
1673 capa->lc_expiry = 0;
1675 oc = capa_lookup(dev->od_capa_hash, capa, 1);
1677 LASSERT(!capa_is_expired(oc));
1681 spin_lock(&capa_lock);
1682 *key = dev->od_capa_keys[1];
1683 spin_unlock(&capa_lock);
1685 capa->lc_keyid = key->lk_keyid;
1686 capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
1688 rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
1690 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
1692 RETURN(ERR_PTR(rc));
1695 oc = capa_add(dev->od_capa_hash, capa);
1699 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
1701 struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1704 /* XXX: no other option than syncing the whole filesystem until we
1706 txg_wait_synced(dmu_objset_pool(osd->od_objset.os), 0ULL);
1711 static struct dt_object_operations osd_obj_ops = {
1712 .do_read_lock = osd_object_read_lock,
1713 .do_write_lock = osd_object_write_lock,
1714 .do_read_unlock = osd_object_read_unlock,
1715 .do_write_unlock = osd_object_write_unlock,
1716 .do_write_locked = osd_object_write_locked,
1717 .do_attr_get = osd_attr_get,
1718 .do_declare_attr_set = osd_declare_attr_set,
1719 .do_attr_set = osd_attr_set,
1720 .do_ah_init = osd_ah_init,
1721 .do_declare_create = osd_declare_object_create,
1722 .do_create = osd_object_create,
1723 .do_declare_destroy = osd_declare_object_destroy,
1724 .do_destroy = osd_object_destroy,
1725 .do_index_try = osd_index_try,
1726 .do_declare_ref_add = osd_declare_object_ref_add,
1727 .do_ref_add = osd_object_ref_add,
1728 .do_declare_ref_del = osd_declare_object_ref_del,
1729 .do_ref_del = osd_object_ref_del,
1730 .do_xattr_get = osd_xattr_get,
1731 .do_declare_xattr_set = osd_declare_xattr_set,
1732 .do_xattr_set = osd_xattr_set,
1733 .do_declare_xattr_del = osd_declare_xattr_del,
1734 .do_xattr_del = osd_xattr_del,
1735 .do_xattr_list = osd_xattr_list,
1736 .do_capa_get = osd_capa_get,
1737 .do_object_sync = osd_object_sync,
1740 static struct lu_object_operations osd_lu_obj_ops = {
1741 .loo_object_init = osd_object_init,
1742 .loo_object_delete = osd_object_delete,
1743 .loo_object_release = osd_object_release,
1744 .loo_object_free = osd_object_free,
1745 .loo_object_print = osd_object_print,
1746 .loo_object_invariant = osd_object_invariant,
1749 static int osd_otable_it_attr_get(const struct lu_env *env,
1750 struct dt_object *dt,
1751 struct lu_attr *attr,
1752 struct lustre_capa *capa)
1758 static struct dt_object_operations osd_obj_otable_it_ops = {
1759 .do_attr_get = osd_otable_it_attr_get,
1760 .do_index_try = osd_index_try,