Whamcloud - gitweb
LU-6719 osd-zfs: Ignore EEXIST during object init
[fs/lustre-release.git] / lustre / osd-zfs / osd_object.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osd-zfs/osd_object.c
37  *
38  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39  * Author: Mike Pershin <tappro@whamcloud.com>
40  * Author: Johann Lombardi <johann@whamcloud.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_OSD
44
45 #include <lustre_ver.h>
46 #include <libcfs/libcfs.h>
47 #include <obd_support.h>
48 #include <lustre_net.h>
49 #include <obd.h>
50 #include <obd_class.h>
51 #include <lustre_disk.h>
52 #include <lustre_fid.h>
53
54 #include "osd_internal.h"
55
56 #include <sys/dnode.h>
57 #include <sys/dbuf.h>
58 #include <sys/spa.h>
59 #include <sys/stat.h>
60 #include <sys/zap.h>
61 #include <sys/spa_impl.h>
62 #include <sys/zfs_znode.h>
63 #include <sys/dmu_tx.h>
64 #include <sys/dmu_objset.h>
65 #include <sys/dsl_prop.h>
66 #include <sys/sa_impl.h>
67 #include <sys/txg.h>
68
69 char *osd_obj_tag = "osd_object";
70
71 static struct dt_object_operations osd_obj_ops;
72 static struct lu_object_operations osd_lu_obj_ops;
73 extern struct dt_body_operations osd_body_ops;
74 static struct dt_object_operations osd_obj_otable_it_ops;
75
76 extern struct kmem_cache *osd_object_kmem;
77
78 static void
79 osd_object_sa_fini(struct osd_object *obj)
80 {
81         if (obj->oo_sa_hdl) {
82                 sa_handle_destroy(obj->oo_sa_hdl);
83                 obj->oo_sa_hdl = NULL;
84         }
85 }
86
87 static int
88 osd_object_sa_init(struct osd_object *obj, struct osd_device *o)
89 {
90         int rc;
91
92         LASSERT(obj->oo_sa_hdl == NULL);
93         LASSERT(obj->oo_db != NULL);
94
95         rc = -sa_handle_get(o->od_os, obj->oo_db->db_object, obj,
96                             SA_HDL_PRIVATE, &obj->oo_sa_hdl);
97         if (rc)
98                 return rc;
99
100         /* Cache the xattr object id, valid for the life of the object */
101         rc = -sa_lookup(obj->oo_sa_hdl, SA_ZPL_XATTR(o), &obj->oo_xattr, 8);
102         if (rc == -ENOENT) {
103                 obj->oo_xattr = ZFS_NO_OBJECT;
104                 rc = 0;
105         } else if (rc) {
106                 osd_object_sa_fini(obj);
107         }
108
109         return rc;
110 }
111
112 /*
113  * Add object to list of dirty objects in tx handle.
114  */
115 static void
116 osd_object_sa_dirty_add(struct osd_object *obj, struct osd_thandle *oh)
117 {
118         if (!list_empty(&obj->oo_sa_linkage))
119                 return;
120
121         down(&oh->ot_sa_lock);
122         write_lock(&obj->oo_attr_lock);
123         if (likely(list_empty(&obj->oo_sa_linkage)))
124                 list_add(&obj->oo_sa_linkage, &oh->ot_sa_list);
125         write_unlock(&obj->oo_attr_lock);
126         up(&oh->ot_sa_lock);
127 }
128
129 /*
130  * Release spill block dbuf hold for all dirty SAs.
131  */
132 void osd_object_sa_dirty_rele(struct osd_thandle *oh)
133 {
134         struct osd_object *obj;
135
136         down(&oh->ot_sa_lock);
137         while (!list_empty(&oh->ot_sa_list)) {
138                 obj = list_entry(oh->ot_sa_list.next,
139                                  struct osd_object, oo_sa_linkage);
140                 sa_spill_rele(obj->oo_sa_hdl);
141                 write_lock(&obj->oo_attr_lock);
142                 list_del_init(&obj->oo_sa_linkage);
143                 write_unlock(&obj->oo_attr_lock);
144         }
145         up(&oh->ot_sa_lock);
146 }
147
148 /*
149  * Update the SA and add the object to the dirty list.
150  */
151 int osd_object_sa_update(struct osd_object *obj, sa_attr_type_t type,
152                          void *buf, uint32_t buflen, struct osd_thandle *oh)
153 {
154         int rc;
155
156         LASSERT(obj->oo_sa_hdl != NULL);
157         LASSERT(oh->ot_tx != NULL);
158
159         rc = -sa_update(obj->oo_sa_hdl, type, buf, buflen, oh->ot_tx);
160         osd_object_sa_dirty_add(obj, oh);
161
162         return rc;
163 }
164
165 /*
166  * Bulk update the SA and add the object to the dirty list.
167  */
168 static int
169 osd_object_sa_bulk_update(struct osd_object *obj, sa_bulk_attr_t *attrs,
170                           int count, struct osd_thandle *oh)
171 {
172         int rc;
173
174         LASSERT(obj->oo_sa_hdl != NULL);
175         LASSERT(oh->ot_tx != NULL);
176
177         rc = -sa_bulk_update(obj->oo_sa_hdl, attrs, count, oh->ot_tx);
178         osd_object_sa_dirty_add(obj, oh);
179
180         return rc;
181 }
182
183 /*
184  * Retrieve the attributes of a DMU object
185  */
186 int __osd_object_attr_get(const struct lu_env *env, struct osd_device *o,
187                           struct osd_object *obj, struct lu_attr *la)
188 {
189         struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
190         sa_handle_t     *sa_hdl;
191         sa_bulk_attr_t  *bulk;
192         int              cnt = 0;
193         int              rc;
194         ENTRY;
195
196         LASSERT(obj->oo_db != NULL);
197
198         rc = -sa_handle_get(o->od_os, obj->oo_db->db_object, NULL,
199                             SA_HDL_PRIVATE, &sa_hdl);
200         if (rc)
201                 RETURN(rc);
202
203         OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 9);
204         if (bulk == NULL)
205                 GOTO(out_sa, rc = -ENOMEM);
206
207         la->la_valid |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE | LA_TYPE |
208                         LA_SIZE | LA_UID | LA_GID | LA_FLAGS | LA_NLINK;
209
210         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(o), NULL, osa->atime, 16);
211         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(o), NULL, osa->mtime, 16);
212         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(o), NULL, osa->ctime, 16);
213         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(o), NULL, &osa->mode, 8);
214         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(o), NULL, &osa->size, 8);
215         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(o), NULL, &osa->nlink, 8);
216         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(o), NULL, &osa->uid, 8);
217         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(o), NULL, &osa->gid, 8);
218         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(o), NULL, &osa->flags, 8);
219
220         rc = -sa_bulk_lookup(sa_hdl, bulk, cnt);
221         if (rc)
222                 GOTO(out_bulk, rc);
223
224         la->la_atime = osa->atime[0];
225         la->la_mtime = osa->mtime[0];
226         la->la_ctime = osa->ctime[0];
227         la->la_mode = osa->mode;
228         la->la_uid = osa->uid;
229         la->la_gid = osa->gid;
230         la->la_nlink = osa->nlink;
231         la->la_flags = attrs_zfs2fs(osa->flags);
232         la->la_size = osa->size;
233
234         if (S_ISCHR(la->la_mode) || S_ISBLK(la->la_mode)) {
235                 rc = -sa_lookup(sa_hdl, SA_ZPL_RDEV(o), &osa->rdev, 8);
236                 if (rc)
237                         GOTO(out_bulk, rc);
238                 la->la_rdev = osa->rdev;
239                 la->la_valid |= LA_RDEV;
240         }
241 out_bulk:
242         OBD_FREE(bulk, sizeof(sa_bulk_attr_t) * 9);
243 out_sa:
244         sa_handle_destroy(sa_hdl);
245
246         RETURN(rc);
247 }
248
249 int __osd_obj2dbuf(const struct lu_env *env, objset_t *os,
250                    uint64_t oid, dmu_buf_t **dbp)
251 {
252         dmu_object_info_t *doi = &osd_oti_get(env)->oti_doi;
253         int rc;
254
255         rc = -sa_buf_hold(os, oid, osd_obj_tag, dbp);
256         if (rc)
257                 return rc;
258
259         dmu_object_info_from_db(*dbp, doi);
260         if (unlikely (oid != DMU_USERUSED_OBJECT &&
261             oid != DMU_GROUPUSED_OBJECT && doi->doi_bonus_type != DMU_OT_SA)) {
262                 sa_buf_rele(*dbp, osd_obj_tag);
263                 *dbp = NULL;
264                 return -EINVAL;
265         }
266
267         LASSERT(*dbp);
268         LASSERT((*dbp)->db_object == oid);
269         LASSERT((*dbp)->db_offset == -1);
270         LASSERT((*dbp)->db_data != NULL);
271
272         return 0;
273 }
274
275 /*
276  * Concurrency: no concurrent access is possible that early in object
277  * life-cycle.
278  */
279 struct lu_object *osd_object_alloc(const struct lu_env *env,
280                                    const struct lu_object_header *hdr,
281                                    struct lu_device *d)
282 {
283         struct osd_object *mo;
284
285         OBD_SLAB_ALLOC_PTR_GFP(mo, osd_object_kmem, GFP_NOFS);
286         if (mo != NULL) {
287                 struct lu_object *l;
288
289                 l = &mo->oo_dt.do_lu;
290                 dt_object_init(&mo->oo_dt, NULL, d);
291                 mo->oo_dt.do_ops = &osd_obj_ops;
292                 l->lo_ops = &osd_lu_obj_ops;
293                 INIT_LIST_HEAD(&mo->oo_sa_linkage);
294                 INIT_LIST_HEAD(&mo->oo_unlinked_linkage);
295                 init_rwsem(&mo->oo_sem);
296                 sema_init(&mo->oo_guard, 1);
297                 rwlock_init(&mo->oo_attr_lock);
298                 mo->oo_destroy = OSD_DESTROY_NONE;
299                 return l;
300         } else {
301                 return NULL;
302         }
303 }
304
305 /*
306  * Concurrency: shouldn't matter.
307  */
308 int osd_object_init0(const struct lu_env *env, struct osd_object *obj)
309 {
310         struct osd_device       *osd = osd_obj2dev(obj);
311         const struct lu_fid     *fid = lu_object_fid(&obj->oo_dt.do_lu);
312         int                      rc = 0;
313         ENTRY;
314
315         if (obj->oo_db == NULL)
316                 RETURN(0);
317
318         /* object exist */
319
320         rc = osd_object_sa_init(obj, osd);
321         if (rc)
322                 RETURN(rc);
323
324         /* cache attrs in object */
325         rc = __osd_object_attr_get(env, osd, obj, &obj->oo_attr);
326         if (rc)
327                 RETURN(rc);
328
329         if (likely(!fid_is_acct(fid)))
330                 /* no body operations for accounting objects */
331                 obj->oo_dt.do_body_ops = &osd_body_ops;
332
333         /*
334          * initialize object before marking it existing
335          */
336         obj->oo_dt.do_lu.lo_header->loh_attr |= obj->oo_attr.la_mode & S_IFMT;
337
338         smp_mb();
339         obj->oo_dt.do_lu.lo_header->loh_attr |= LOHA_EXISTS;
340
341         RETURN(0);
342 }
343
344 static int osd_check_lma(const struct lu_env *env, struct osd_object *obj)
345 {
346         struct osd_thread_info  *info = osd_oti_get(env);
347         struct lu_buf           buf;
348         int                     rc;
349         struct lustre_mdt_attrs *lma;
350         ENTRY;
351
352         CLASSERT(sizeof(info->oti_buf) >= sizeof(*lma));
353         lma = (struct lustre_mdt_attrs *)info->oti_buf;
354         buf.lb_buf = lma;
355         buf.lb_len = sizeof(info->oti_buf);
356
357         rc = osd_xattr_get(env, &obj->oo_dt, &buf, XATTR_NAME_LMA);
358         if (rc > 0) {
359                 rc = 0;
360                 lustre_lma_swab(lma);
361                 if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
362                              CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
363                         CWARN("%s: unsupported incompat LMA feature(s) %#x for "
364                               "fid = "DFID"\n", osd_obj2dev(obj)->od_svname,
365                               lma->lma_incompat & ~LMA_INCOMPAT_SUPP,
366                               PFID(lu_object_fid(&obj->oo_dt.do_lu)));
367                         rc = -EOPNOTSUPP;
368                 }
369         } else if (rc == -ENODATA) {
370                 /* haven't initialize LMA xattr */
371                 rc = 0;
372         }
373
374         RETURN(rc);
375 }
376
377 /*
378  * Concurrency: no concurrent access is possible that early in object
379  * life-cycle.
380  */
381 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
382                            const struct lu_object_conf *conf)
383 {
384         struct osd_object       *obj = osd_obj(l);
385         struct osd_device       *osd = osd_obj2dev(obj);
386         uint64_t                 oid;
387         int                      rc;
388         ENTRY;
389
390         LASSERT(osd_invariant(obj));
391
392         if (fid_is_otable_it(&l->lo_header->loh_fid)) {
393                 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
394                 l->lo_header->loh_attr |= LOHA_EXISTS;
395                 RETURN(0);
396         }
397
398         rc = osd_fid_lookup(env, osd, lu_object_fid(l), &oid);
399         if (rc == 0) {
400                 LASSERT(obj->oo_db == NULL);
401                 rc = __osd_obj2dbuf(env, osd->od_os, oid, &obj->oo_db);
402                 /* EEXIST will be returned if object is being deleted in ZFS */
403                 if (rc == -EEXIST) {
404                         rc = 0;
405                         GOTO(out, rc);
406                 }
407                 if (rc != 0) {
408                         CERROR("%s: lookup "DFID"/"LPX64" failed: rc = %d\n",
409                                osd->od_svname, PFID(lu_object_fid(l)), oid, rc);
410                         GOTO(out, rc);
411                 }
412                 LASSERT(obj->oo_db);
413                 rc = osd_object_init0(env, obj);
414                 if (rc != 0)
415                         GOTO(out, rc);
416
417                 rc = osd_check_lma(env, obj);
418                 if (rc != 0)
419                         GOTO(out, rc);
420         } else if (rc == -ENOENT) {
421                 rc = 0;
422         }
423         LASSERT(osd_invariant(obj));
424 out:
425         RETURN(rc);
426 }
427
428 /*
429  * Concurrency: no concurrent access is possible that late in object
430  * life-cycle.
431  */
432 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
433 {
434         struct osd_object *obj = osd_obj(l);
435
436         LASSERT(osd_invariant(obj));
437
438         dt_object_fini(&obj->oo_dt);
439         OBD_SLAB_FREE_PTR(obj, osd_object_kmem);
440 }
441
442 static int
443 osd_object_unlinked_add(struct osd_object *obj, struct osd_thandle *oh)
444 {
445         int rc = -EBUSY;
446
447         down(&obj->oo_guard);
448
449         LASSERT(obj->oo_destroy == OSD_DESTROY_ASYNC);
450
451         if (likely(list_empty(&obj->oo_unlinked_linkage))) {
452                 list_add(&obj->oo_unlinked_linkage, &oh->ot_unlinked_list);
453                 rc = 0;
454         }
455
456         up(&obj->oo_guard);
457
458         return rc;
459 }
460
461 /* Default to max data size covered by a level-1 indirect block */
462 static unsigned long osd_sync_destroy_max_size =
463         1UL << (DN_MAX_INDBLKSHIFT - SPA_BLKPTRSHIFT + SPA_MAXBLOCKSHIFT);
464 CFS_MODULE_PARM(osd_sync_destroy_max_size, "ul", ulong, 0444,
465                 "Maximum object size to use synchronous destroy.");
466
467 static inline void
468 osd_object_set_destroy_type(struct osd_object *obj)
469 {
470         /*
471          * Lock-less OST_WRITE can race with OST_DESTROY, so set destroy type
472          * only once and use it consistently thereafter.
473          */
474         down(&obj->oo_guard);
475         if (obj->oo_destroy == OSD_DESTROY_NONE) {
476                 if (obj->oo_attr.la_size <= osd_sync_destroy_max_size)
477                         obj->oo_destroy = OSD_DESTROY_SYNC;
478                 else /* Larger objects are destroyed asynchronously */
479                         obj->oo_destroy = OSD_DESTROY_ASYNC;
480         }
481         up(&obj->oo_guard);
482 }
483
484 static int osd_declare_object_destroy(const struct lu_env *env,
485                                       struct dt_object *dt,
486                                       struct thandle *th)
487 {
488         char                    *buf = osd_oti_get(env)->oti_str;
489         const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
490         struct osd_object       *obj = osd_dt_obj(dt);
491         struct osd_device       *osd = osd_obj2dev(obj);
492         struct osd_thandle      *oh;
493         int                      rc;
494         uint64_t                 zapid;
495         ENTRY;
496
497         LASSERT(th != NULL);
498         LASSERT(dt_object_exists(dt));
499
500         oh = container_of0(th, struct osd_thandle, ot_super);
501         LASSERT(oh->ot_tx != NULL);
502
503         /* declare that we'll remove object from fid-dnode mapping */
504         zapid = osd_get_name_n_idx(env, osd, fid, buf);
505         dmu_tx_hold_bonus(oh->ot_tx, zapid);
506         dmu_tx_hold_zap(oh->ot_tx, zapid, FALSE, buf);
507
508         osd_declare_xattrs_destroy(env, obj, oh);
509
510         /* declare that we'll remove object from inode accounting ZAPs */
511         dmu_tx_hold_bonus(oh->ot_tx, osd->od_iusr_oid);
512         dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, FALSE, buf);
513         dmu_tx_hold_bonus(oh->ot_tx, osd->od_igrp_oid);
514         dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, FALSE, buf);
515
516         /* one less inode */
517         rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
518                                obj->oo_attr.la_gid, -1, oh, false, NULL, false);
519         if (rc)
520                 RETURN(rc);
521
522         /* data to be truncated */
523         rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
524                                obj->oo_attr.la_gid, 0, oh, true, NULL, false);
525         if (rc)
526                 RETURN(rc);
527
528         osd_object_set_destroy_type(obj);
529         if (obj->oo_destroy == OSD_DESTROY_SYNC)
530                 dmu_tx_hold_free(oh->ot_tx, obj->oo_db->db_object,
531                                  0, DMU_OBJECT_END);
532         else
533                 dmu_tx_hold_zap(oh->ot_tx, osd->od_unlinkedid, TRUE, NULL);
534
535         RETURN(0);
536 }
537
538 static int osd_object_destroy(const struct lu_env *env,
539                               struct dt_object *dt, struct thandle *th)
540 {
541         char                    *buf = osd_oti_get(env)->oti_str;
542         struct osd_object       *obj = osd_dt_obj(dt);
543         struct osd_device       *osd = osd_obj2dev(obj);
544         const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
545         struct osd_thandle      *oh;
546         int                      rc;
547         uint64_t                 oid, zapid;
548         ENTRY;
549
550         LASSERT(obj->oo_db != NULL);
551         LASSERT(dt_object_exists(dt));
552         LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
553
554         oh = container_of0(th, struct osd_thandle, ot_super);
555         LASSERT(oh != NULL);
556         LASSERT(oh->ot_tx != NULL);
557
558         /* remove obj ref from index dir (it depends) */
559         zapid = osd_get_name_n_idx(env, osd, fid, buf);
560         rc = -zap_remove(osd->od_os, zapid, buf, oh->ot_tx);
561         if (rc) {
562                 CERROR("%s: zap_remove(%s) failed: rc = %d\n",
563                        osd->od_svname, buf, rc);
564                 GOTO(out, rc);
565         }
566
567         rc = osd_xattrs_destroy(env, obj, oh);
568         if (rc) {
569                 CERROR("%s: cannot destroy xattrs for %s: rc = %d\n",
570                        osd->od_svname, buf, rc);
571                 GOTO(out, rc);
572         }
573
574         /* Remove object from inode accounting. It is not fatal for the destroy
575          * operation if something goes wrong while updating accounting, but we
576          * still log an error message to notify the administrator */
577         rc = -zap_increment_int(osd->od_os, osd->od_iusr_oid,
578                                 obj->oo_attr.la_uid, -1, oh->ot_tx);
579         if (rc)
580                 CERROR("%s: failed to remove "DFID" from accounting ZAP for usr"
581                        " %d: rc = %d\n", osd->od_svname, PFID(fid),
582                        obj->oo_attr.la_uid, rc);
583         rc = -zap_increment_int(osd->od_os, osd->od_igrp_oid,
584                                 obj->oo_attr.la_gid, -1, oh->ot_tx);
585         if (rc)
586                 CERROR("%s: failed to remove "DFID" from accounting ZAP for grp"
587                        " %d: rc = %d\n", osd->od_svname, PFID(fid),
588                        obj->oo_attr.la_gid, rc);
589
590         oid = obj->oo_db->db_object;
591         if (unlikely(obj->oo_destroy == OSD_DESTROY_NONE)) {
592                 /* this may happen if the destroy wasn't declared
593                  * e.g. when the object is created and then destroyed
594                  * in the same transaction - we don't need additional
595                  * space for destroy specifically */
596                 LASSERT(obj->oo_attr.la_size <= osd_sync_destroy_max_size);
597                 rc = -dmu_object_free(osd->od_os, oid, oh->ot_tx);
598                 if (rc)
599                         CERROR("%s: failed to free %s "LPU64": rc = %d\n",
600                                osd->od_svname, buf, oid, rc);
601         } else if (obj->oo_destroy == OSD_DESTROY_SYNC) {
602                 rc = -dmu_object_free(osd->od_os, oid, oh->ot_tx);
603                 if (rc)
604                         CERROR("%s: failed to free %s "LPU64": rc = %d\n",
605                                osd->od_svname, buf, oid, rc);
606         } else { /* asynchronous destroy */
607                 rc = osd_object_unlinked_add(obj, oh);
608                 if (rc)
609                         GOTO(out, rc);
610
611                 rc = -zap_add_int(osd->od_os, osd->od_unlinkedid,
612                                   oid, oh->ot_tx);
613                 if (rc)
614                         CERROR("%s: zap_add_int() failed %s "LPU64": rc = %d\n",
615                                osd->od_svname, buf, oid, rc);
616         }
617
618 out:
619         /* not needed in the cache anymore */
620         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
621         if (rc == 0)
622                 obj->oo_destroyed = 1;
623         RETURN (0);
624 }
625
626 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
627 {
628         struct osd_object *obj = osd_obj(l);
629
630         if (obj->oo_db != NULL) {
631                 osd_object_sa_fini(obj);
632                 if (obj->oo_sa_xattr) {
633                         nvlist_free(obj->oo_sa_xattr);
634                         obj->oo_sa_xattr = NULL;
635                 }
636                 sa_buf_rele(obj->oo_db, osd_obj_tag);
637                 list_del(&obj->oo_sa_linkage);
638                 obj->oo_db = NULL;
639         }
640 }
641
642 /*
643  * Concurrency: ->loo_object_release() is called under site spin-lock.
644  */
645 static void osd_object_release(const struct lu_env *env,
646                                struct lu_object *l)
647 {
648 }
649
650 /*
651  * Concurrency: shouldn't matter.
652  */
653 static int osd_object_print(const struct lu_env *env, void *cookie,
654                             lu_printer_t p, const struct lu_object *l)
655 {
656         struct osd_object *o = osd_obj(l);
657
658         return (*p)(env, cookie, LUSTRE_OSD_ZFS_NAME"-object@%p", o);
659 }
660
661 static void osd_object_read_lock(const struct lu_env *env,
662                                  struct dt_object *dt, unsigned role)
663 {
664         struct osd_object *obj = osd_dt_obj(dt);
665
666         LASSERT(osd_invariant(obj));
667
668         down_read_nested(&obj->oo_sem, role);
669 }
670
671 static void osd_object_write_lock(const struct lu_env *env,
672                                   struct dt_object *dt, unsigned role)
673 {
674         struct osd_object *obj = osd_dt_obj(dt);
675
676         LASSERT(osd_invariant(obj));
677
678         down_write_nested(&obj->oo_sem, role);
679 }
680
681 static void osd_object_read_unlock(const struct lu_env *env,
682                                    struct dt_object *dt)
683 {
684         struct osd_object *obj = osd_dt_obj(dt);
685
686         LASSERT(osd_invariant(obj));
687         up_read(&obj->oo_sem);
688 }
689
690 static void osd_object_write_unlock(const struct lu_env *env,
691                                     struct dt_object *dt)
692 {
693         struct osd_object *obj = osd_dt_obj(dt);
694
695         LASSERT(osd_invariant(obj));
696         up_write(&obj->oo_sem);
697 }
698
699 static int osd_object_write_locked(const struct lu_env *env,
700                                    struct dt_object *dt)
701 {
702         struct osd_object *obj = osd_dt_obj(dt);
703         int rc = 1;
704
705         LASSERT(osd_invariant(obj));
706
707         if (down_write_trylock(&obj->oo_sem)) {
708                 rc = 0;
709                 up_write(&obj->oo_sem);
710         }
711         return rc;
712 }
713
714 static int osd_attr_get(const struct lu_env *env,
715                         struct dt_object *dt,
716                         struct lu_attr *attr)
717 {
718         struct osd_object       *obj = osd_dt_obj(dt);
719         uint64_t                 blocks;
720         uint32_t                 blksize;
721
722         LASSERT(dt_object_exists(dt));
723         LASSERT(osd_invariant(obj));
724         LASSERT(obj->oo_db);
725
726         read_lock(&obj->oo_attr_lock);
727         *attr = obj->oo_attr;
728         read_unlock(&obj->oo_attr_lock);
729
730         /* with ZFS_DEBUG zrl_add_debug() called by DB_DNODE_ENTER()
731          * from within sa_object_size() can block on a mutex, so
732          * we can't call sa_object_size() holding rwlock */
733         sa_object_size(obj->oo_sa_hdl, &blksize, &blocks);
734         /* we do not control size of indices, so always calculate
735          * it from number of blocks reported by DMU */
736         if (S_ISDIR(attr->la_mode))
737                 attr->la_size = 512 * blocks;
738         /* Block size may be not set; suggest maximal I/O transfers. */
739         if (blksize == 0)
740                 blksize = osd_spa_maxblocksize(
741                         dmu_objset_spa(osd_obj2dev(obj)->od_os));
742
743         attr->la_blksize = blksize;
744         attr->la_blocks = blocks;
745         attr->la_valid |= LA_BLOCKS | LA_BLKSIZE;
746
747         return 0;
748 }
749
750 /* Simple wrapper on top of qsd API which implement quota transfer for osd
751  * setattr needs. As a reminder, only the root user can change ownership of
752  * a file, that's why EDQUOT & EINPROGRESS errors are discarded */
753 static inline int qsd_transfer(const struct lu_env *env,
754                                struct qsd_instance *qsd,
755                                struct lquota_trans *trans, int qtype,
756                                __u64 orig_id, __u64 new_id, __u64 bspace,
757                                struct lquota_id_info *qi)
758 {
759         int     rc;
760
761         if (unlikely(qsd == NULL))
762                 return 0;
763
764         LASSERT(qtype >= 0 && qtype < MAXQUOTAS);
765         qi->lqi_type = qtype;
766
767         /* inode accounting */
768         qi->lqi_is_blk = false;
769
770         /* one more inode for the new owner ... */
771         qi->lqi_id.qid_uid = new_id;
772         qi->lqi_space      = 1;
773         rc = qsd_op_begin(env, qsd, trans, qi, NULL);
774         if (rc == -EDQUOT || rc == -EINPROGRESS)
775                 rc = 0;
776         if (rc)
777                 return rc;
778
779         /* and one less inode for the current id */
780         qi->lqi_id.qid_uid = orig_id;;
781         qi->lqi_space      = -1;
782         /* can't get EDQUOT when reducing usage */
783         rc = qsd_op_begin(env, qsd, trans, qi, NULL);
784         if (rc == -EINPROGRESS)
785                 rc = 0;
786         if (rc)
787                 return rc;
788
789         /* block accounting */
790         qi->lqi_is_blk = true;
791
792         /* more blocks for the new owner ... */
793         qi->lqi_id.qid_uid = new_id;
794         qi->lqi_space      = bspace;
795         rc = qsd_op_begin(env, qsd, trans, qi, NULL);
796         if (rc == -EDQUOT || rc == -EINPROGRESS)
797                 rc = 0;
798         if (rc)
799                 return rc;
800
801         /* and finally less blocks for the current owner */
802         qi->lqi_id.qid_uid = orig_id;
803         qi->lqi_space      = -bspace;
804         rc = qsd_op_begin(env, qsd, trans, qi, NULL);
805         /* can't get EDQUOT when reducing usage */
806         if (rc == -EINPROGRESS)
807                 rc = 0;
808         return rc;
809 }
810
811 static int osd_declare_attr_set(const struct lu_env *env,
812                                 struct dt_object *dt,
813                                 const struct lu_attr *attr,
814                                 struct thandle *handle)
815 {
816         struct osd_thread_info  *info = osd_oti_get(env);
817         char                    *buf = osd_oti_get(env)->oti_str;
818         struct osd_object       *obj = osd_dt_obj(dt);
819         struct osd_device       *osd = osd_obj2dev(obj);
820         struct osd_thandle      *oh;
821         uint64_t                 bspace;
822         uint32_t                 blksize;
823         int                      rc;
824         ENTRY;
825
826         if (!dt_object_exists(dt)) {
827                 /* XXX: sanity check that object creation is declared */
828                 RETURN(0);
829         }
830
831         LASSERT(handle != NULL);
832         LASSERT(osd_invariant(obj));
833
834         oh = container_of0(handle, struct osd_thandle, ot_super);
835
836         LASSERT(obj->oo_sa_hdl != NULL);
837         LASSERT(oh->ot_tx != NULL);
838         dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
839         if (oh->ot_tx->tx_err != 0)
840                 RETURN(-oh->ot_tx->tx_err);
841
842         sa_object_size(obj->oo_sa_hdl, &blksize, &bspace);
843         bspace = toqb(bspace * blksize);
844
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);
849
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);
856                         if (rc)
857                                 RETURN(rc);
858                 }
859         }
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);
864
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);
871                         if (rc)
872                                 RETURN(rc);
873                 }
874         }
875
876         RETURN(0);
877 }
878
879 /*
880  * Set the attributes of an object
881  *
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.
885  */
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 {
889         struct osd_object       *obj = osd_dt_obj(dt);
890         struct osd_device       *osd = osd_obj2dev(obj);
891         struct osd_thandle      *oh;
892         struct osa_attr         *osa = &osd_oti_get(env)->oti_osa;
893         sa_bulk_attr_t          *bulk;
894         __u64                    valid = la->la_valid;
895         int                      cnt;
896         int                      rc = 0;
897
898         ENTRY;
899         LASSERT(handle != NULL);
900         LASSERT(dt_object_exists(dt));
901         LASSERT(osd_invariant(obj));
902         LASSERT(obj->oo_sa_hdl);
903
904         oh = container_of0(handle, struct osd_thandle, ot_super);
905         /* Assert that the transaction has been assigned to a
906            transaction group. */
907         LASSERT(oh->ot_tx->tx_txg != 0);
908
909         /* Only allow set size for regular file */
910         if (!S_ISREG(dt->do_lu.lo_header->loh_attr))
911                 valid &= ~(LA_SIZE | LA_BLOCKS);
912
913         if (valid == 0)
914                 RETURN(0);
915
916         OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 10);
917         if (bulk == NULL)
918                 RETURN(-ENOMEM);
919
920         /* do both accounting updates outside oo_attr_lock below */
921         if ((valid & LA_UID) && (la->la_uid != obj->oo_attr.la_uid)) {
922                 /* Update user accounting. Failure isn't fatal, but we still
923                  * log an error message */
924                 rc = -zap_increment_int(osd->od_os, osd->od_iusr_oid,
925                                         la->la_uid, 1, oh->ot_tx);
926                 if (rc)
927                         CERROR("%s: failed to update accounting ZAP for user "
928                                 "%d (%d)\n", osd->od_svname, la->la_uid, rc);
929                 rc = -zap_increment_int(osd->od_os, osd->od_iusr_oid,
930                                         obj->oo_attr.la_uid, -1, oh->ot_tx);
931                 if (rc)
932                         CERROR("%s: failed to update accounting ZAP for user "
933                                 "%d (%d)\n", osd->od_svname,
934                                 obj->oo_attr.la_uid, rc);
935         }
936         if ((valid & LA_GID) && (la->la_gid != obj->oo_attr.la_gid)) {
937                 /* Update group accounting. Failure isn't fatal, but we still
938                  * log an error message */
939                 rc = -zap_increment_int(osd->od_os, osd->od_igrp_oid,
940                                         la->la_gid, 1, oh->ot_tx);
941                 if (rc)
942                         CERROR("%s: failed to update accounting ZAP for user "
943                                 "%d (%d)\n", osd->od_svname, la->la_gid, rc);
944                 rc = -zap_increment_int(osd->od_os, osd->od_igrp_oid,
945                                         obj->oo_attr.la_gid, -1, oh->ot_tx);
946                 if (rc)
947                         CERROR("%s: failed to update accounting ZAP for user "
948                                 "%d (%d)\n", osd->od_svname,
949                                 obj->oo_attr.la_gid, rc);
950         }
951
952         write_lock(&obj->oo_attr_lock);
953         cnt = 0;
954         if (valid & LA_ATIME) {
955                 osa->atime[0] = obj->oo_attr.la_atime = la->la_atime;
956                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL,
957                                  osa->atime, 16);
958         }
959         if (valid & LA_MTIME) {
960                 osa->mtime[0] = obj->oo_attr.la_mtime = la->la_mtime;
961                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL,
962                                  osa->mtime, 16);
963         }
964         if (valid & LA_CTIME) {
965                 osa->ctime[0] = obj->oo_attr.la_ctime = la->la_ctime;
966                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL,
967                                  osa->ctime, 16);
968         }
969         if (valid & LA_MODE) {
970                 /* mode is stored along with type, so read it first */
971                 obj->oo_attr.la_mode = (obj->oo_attr.la_mode & S_IFMT) |
972                         (la->la_mode & ~S_IFMT);
973                 osa->mode = obj->oo_attr.la_mode;
974                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL,
975                                  &osa->mode, 8);
976         }
977         if (valid & LA_SIZE) {
978                 osa->size = obj->oo_attr.la_size = la->la_size;
979                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL,
980                                  &osa->size, 8);
981         }
982         if (valid & LA_NLINK) {
983                 osa->nlink = obj->oo_attr.la_nlink = la->la_nlink;
984                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL,
985                                  &osa->nlink, 8);
986         }
987         if (valid & LA_RDEV) {
988                 osa->rdev = obj->oo_attr.la_rdev = la->la_rdev;
989                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL,
990                                  &osa->rdev, 8);
991         }
992         if (valid & LA_FLAGS) {
993                 osa->flags = attrs_fs2zfs(la->la_flags);
994                 /* many flags are not supported by zfs, so ensure a good cached
995                  * copy */
996                 obj->oo_attr.la_flags = attrs_zfs2fs(osa->flags);
997                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL,
998                                  &osa->flags, 8);
999         }
1000         if (valid & LA_UID) {
1001                 osa->uid = obj->oo_attr.la_uid = la->la_uid;
1002                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL,
1003                                  &osa->uid, 8);
1004         }
1005         if (valid & LA_GID) {
1006                 osa->gid = obj->oo_attr.la_gid = la->la_gid;
1007                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL,
1008                                  &osa->gid, 8);
1009         }
1010         obj->oo_attr.la_valid |= valid;
1011         write_unlock(&obj->oo_attr_lock);
1012
1013         rc = osd_object_sa_bulk_update(obj, bulk, cnt, oh);
1014
1015         OBD_FREE(bulk, sizeof(sa_bulk_attr_t) * 10);
1016         RETURN(rc);
1017 }
1018
1019 /*
1020  * Object creation.
1021  *
1022  * XXX temporary solution.
1023  */
1024
1025 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1026                         struct dt_object *parent, struct dt_object *child,
1027                         umode_t child_mode)
1028 {
1029         LASSERT(ah);
1030
1031         ah->dah_parent = parent;
1032         ah->dah_mode = child_mode;
1033 }
1034
1035 static int osd_declare_object_create(const struct lu_env *env,
1036                                      struct dt_object *dt,
1037                                      struct lu_attr *attr,
1038                                      struct dt_allocation_hint *hint,
1039                                      struct dt_object_format *dof,
1040                                      struct thandle *handle)
1041 {
1042         char                    *buf = osd_oti_get(env)->oti_str;
1043         const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
1044         struct osd_object       *obj = osd_dt_obj(dt);
1045         struct osd_device       *osd = osd_obj2dev(obj);
1046         struct osd_thandle      *oh;
1047         uint64_t                 zapid;
1048         int                      rc;
1049         ENTRY;
1050
1051         LASSERT(dof);
1052
1053         switch (dof->dof_type) {
1054                 case DFT_REGULAR:
1055                 case DFT_SYM:
1056                 case DFT_NODE:
1057                         if (obj->oo_dt.do_body_ops == NULL)
1058                                 obj->oo_dt.do_body_ops = &osd_body_ops;
1059                         break;
1060                 default:
1061                         break;
1062         }
1063
1064         LASSERT(handle != NULL);
1065         oh = container_of0(handle, struct osd_thandle, ot_super);
1066         LASSERT(oh->ot_tx != NULL);
1067
1068         switch (dof->dof_type) {
1069                 case DFT_DIR:
1070                         dt->do_index_ops = &osd_dir_ops;
1071                 case DFT_INDEX:
1072                         /* for zap create */
1073                         dmu_tx_hold_zap(oh->ot_tx, DMU_NEW_OBJECT, 1, NULL);
1074                         break;
1075                 case DFT_REGULAR:
1076                 case DFT_SYM:
1077                 case DFT_NODE:
1078                         /* first, we'll create new object */
1079                         dmu_tx_hold_bonus(oh->ot_tx, DMU_NEW_OBJECT);
1080                         break;
1081
1082                 default:
1083                         LBUG();
1084                         break;
1085         }
1086
1087         /* and we'll add it to some mapping */
1088         zapid = osd_get_name_n_idx(env, osd, fid, buf);
1089         dmu_tx_hold_bonus(oh->ot_tx, zapid);
1090         dmu_tx_hold_zap(oh->ot_tx, zapid, TRUE, buf);
1091
1092         /* we will also update inode accounting ZAPs */
1093         dmu_tx_hold_bonus(oh->ot_tx, osd->od_iusr_oid);
1094         dmu_tx_hold_zap(oh->ot_tx, osd->od_iusr_oid, TRUE, buf);
1095         dmu_tx_hold_bonus(oh->ot_tx, osd->od_igrp_oid);
1096         dmu_tx_hold_zap(oh->ot_tx, osd->od_igrp_oid, TRUE, buf);
1097
1098         dmu_tx_hold_sa_create(oh->ot_tx, ZFS_SA_BASE_ATTR_SIZE);
1099
1100         __osd_xattr_declare_set(env, obj, sizeof(struct lustre_mdt_attrs),
1101                                 XATTR_NAME_LMA, oh);
1102
1103         rc = osd_declare_quota(env, osd, attr->la_uid, attr->la_gid, 1, oh,
1104                                false, NULL, false);
1105         RETURN(rc);
1106 }
1107
1108 int __osd_attr_init(const struct lu_env *env, struct osd_device *osd,
1109                     uint64_t oid, dmu_tx_t *tx, struct lu_attr *la,
1110                     uint64_t parent)
1111 {
1112         sa_bulk_attr_t  *bulk;
1113         sa_handle_t     *sa_hdl;
1114         struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
1115         uint64_t         gen;
1116         uint64_t         crtime[2];
1117         timestruc_t      now;
1118         int              cnt;
1119         int              rc;
1120
1121         gethrestime(&now);
1122         gen = dmu_tx_get_txg(tx);
1123
1124         ZFS_TIME_ENCODE(&now, crtime);
1125
1126         osa->atime[0] = la->la_atime;
1127         osa->ctime[0] = la->la_ctime;
1128         osa->mtime[0] = la->la_mtime;
1129         osa->mode = la->la_mode;
1130         osa->uid = la->la_uid;
1131         osa->gid = la->la_gid;
1132         osa->rdev = la->la_rdev;
1133         osa->nlink = la->la_nlink;
1134         osa->flags = attrs_fs2zfs(la->la_flags);
1135         osa->size  = la->la_size;
1136
1137         /* Now add in all of the "SA" attributes */
1138         rc = -sa_handle_get(osd->od_os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
1139         if (rc)
1140                 return rc;
1141
1142         OBD_ALLOC(bulk, sizeof(sa_bulk_attr_t) * 13);
1143         if (bulk == NULL) {
1144                 rc = -ENOMEM;
1145                 goto out;
1146         }
1147         /*
1148          * we need to create all SA below upon object create.
1149          *
1150          * XXX The attribute order matters since the accounting callback relies
1151          * on static offsets (i.e. SA_*_OFFSET, see zfs_space_delta_cb()) to
1152          * look up the UID/GID attributes. Moreover, the callback does not seem
1153          * to support the spill block.
1154          * We define attributes in the same order as SA_*_OFFSET in order to
1155          * work around the problem. See ORI-610.
1156          */
1157         cnt = 0;
1158         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL, &osa->mode, 8);
1159         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL, &osa->size, 8);
1160         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(osd), NULL, &gen, 8);
1161         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL, &osa->uid, 8);
1162         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL, &osa->gid, 8);
1163         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(osd), NULL, &parent, 8);
1164         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL, &osa->flags, 8);
1165         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL, osa->atime, 16);
1166         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL, osa->mtime, 16);
1167         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL, osa->ctime, 16);
1168         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, crtime, 16);
1169         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL, &osa->nlink, 8);
1170         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL, &osa->rdev, 8);
1171
1172         rc = -sa_replace_all_by_template(sa_hdl, bulk, cnt, tx);
1173
1174         OBD_FREE(bulk, sizeof(sa_bulk_attr_t) * 13);
1175 out:
1176         sa_handle_destroy(sa_hdl);
1177         return rc;
1178 }
1179
1180 /*
1181  * The transaction passed to this routine must have
1182  * dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT) called and then assigned
1183  * to a transaction group.
1184  */
1185 int __osd_object_create(const struct lu_env *env, struct osd_object *obj,
1186                         dmu_buf_t **dbp, dmu_tx_t *tx, struct lu_attr *la,
1187                         uint64_t parent)
1188 {
1189         uint64_t             oid;
1190         int                  rc;
1191         struct osd_device   *osd = osd_obj2dev(obj);
1192         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
1193         dmu_object_type_t    type = DMU_OT_PLAIN_FILE_CONTENTS;
1194
1195         /* Assert that the transaction has been assigned to a
1196            transaction group. */
1197         LASSERT(tx->tx_txg != 0);
1198
1199         /* Use DMU_OTN_UINT8_METADATA for local objects so their data blocks
1200          * would get an additional ditto copy */
1201         if (unlikely(S_ISREG(la->la_mode) &&
1202                      fid_seq_is_local_file(fid_seq(fid))))
1203                 type = DMU_OTN_UINT8_METADATA;
1204
1205         /* Create a new DMU object. */
1206         oid = dmu_object_alloc(osd->od_os, type, 0,
1207                                DMU_OT_SA, DN_MAX_BONUSLEN, tx);
1208         rc = -sa_buf_hold(osd->od_os, oid, osd_obj_tag, dbp);
1209         LASSERTF(rc == 0, "sa_buf_hold "LPU64" failed: %d\n", oid, rc);
1210
1211         LASSERT(la->la_valid & LA_MODE);
1212         la->la_size = 0;
1213         la->la_nlink = 1;
1214
1215         rc = __osd_attr_init(env, osd, oid, tx, la, parent);
1216         if (rc != 0) {
1217                 sa_buf_rele(*dbp, osd_obj_tag);
1218                 *dbp = NULL;
1219                 dmu_object_free(osd->od_os, oid, tx);
1220                 return rc;
1221         }
1222
1223         return 0;
1224 }
1225
1226 /*
1227  * The transaction passed to this routine must have
1228  * dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, ...) called and then assigned
1229  * to a transaction group.
1230  *
1231  * Using ZAP_FLAG_HASH64 will force the ZAP to always be a FAT ZAP.
1232  * This is fine for directories today, because storing the FID in the dirent
1233  * will also require a FAT ZAP.  If there is a new type of micro ZAP created
1234  * then we might need to re-evaluate the use of this flag and instead do
1235  * a conversion from the different internal ZAP hash formats being used. */
1236 int __osd_zap_create(const struct lu_env *env, struct osd_device *osd,
1237                      dmu_buf_t **zap_dbp, dmu_tx_t *tx,
1238                      struct lu_attr *la, uint64_t parent, zap_flags_t flags)
1239 {
1240         uint64_t oid;
1241         int      rc;
1242
1243         /* Assert that the transaction has been assigned to a
1244            transaction group. */
1245         LASSERT(tx->tx_txg != 0);
1246
1247         oid = zap_create_flags(osd->od_os, 0, flags | ZAP_FLAG_HASH64,
1248                                DMU_OT_DIRECTORY_CONTENTS,
1249                                14, /* == ZFS fzap_default_block_shift */
1250                                DN_MAX_INDBLKSHIFT, /* indirect block shift */
1251                                DMU_OT_SA, DN_MAX_BONUSLEN, tx);
1252
1253         rc = -sa_buf_hold(osd->od_os, oid, osd_obj_tag, zap_dbp);
1254         if (rc)
1255                 return rc;
1256
1257         LASSERT(la->la_valid & LA_MODE);
1258         la->la_size = 2;
1259         la->la_nlink = 1;
1260
1261         return __osd_attr_init(env, osd, oid, tx, la, parent);
1262 }
1263
1264 static dmu_buf_t *osd_mkidx(const struct lu_env *env, struct osd_object *obj,
1265                             struct lu_attr *la, uint64_t parent,
1266                             struct osd_thandle *oh)
1267 {
1268         dmu_buf_t *db;
1269         int        rc;
1270
1271         /* Index file should be created as regular file in order not to confuse
1272          * ZPL which could interpret them as directory.
1273          * We set ZAP_FLAG_UINT64_KEY to let ZFS know than we are going to use
1274          * binary keys */
1275         LASSERT(S_ISREG(la->la_mode));
1276         rc = __osd_zap_create(env, osd_obj2dev(obj), &db, oh->ot_tx, la, parent,
1277                               ZAP_FLAG_UINT64_KEY);
1278         if (rc)
1279                 return ERR_PTR(rc);
1280         return db;
1281 }
1282
1283 static dmu_buf_t *osd_mkdir(const struct lu_env *env, struct osd_object *obj,
1284                             struct lu_attr *la, uint64_t parent,
1285                             struct osd_thandle *oh)
1286 {
1287         dmu_buf_t *db;
1288         int        rc;
1289
1290         LASSERT(S_ISDIR(la->la_mode));
1291         rc = __osd_zap_create(env, osd_obj2dev(obj), &db,
1292                               oh->ot_tx, la, parent, 0);
1293         if (rc)
1294                 return ERR_PTR(rc);
1295         return db;
1296 }
1297
1298 static dmu_buf_t *osd_mkreg(const struct lu_env *env, struct osd_object *obj,
1299                             struct lu_attr *la, uint64_t parent,
1300                             struct osd_thandle *oh)
1301 {
1302         dmu_buf_t         *db;
1303         int                rc;
1304         struct osd_device *osd = osd_obj2dev(obj);
1305
1306         LASSERT(S_ISREG(la->la_mode));
1307         rc = __osd_object_create(env, obj, &db, oh->ot_tx, la, parent);
1308         if (rc)
1309                 return ERR_PTR(rc);
1310
1311         /*
1312          * XXX: This heuristic is non-optimal.  It would be better to
1313          * increase the blocksize up to osd->od_max_blksz during the write.
1314          * This is exactly how the ZPL behaves and it ensures that the right
1315          * blocksize is selected based on the file size rather than the
1316          * making broad assumptions based on the osd type.
1317          */
1318         if (!lu_device_is_md(osd2lu_dev(osd))) {
1319                 rc = -dmu_object_set_blocksize(osd->od_os, db->db_object,
1320                                                osd->od_max_blksz, 0, oh->ot_tx);
1321                 if (unlikely(rc)) {
1322                         CERROR("%s: can't change blocksize: %d\n",
1323                                osd->od_svname, rc);
1324                         return ERR_PTR(rc);
1325                 }
1326         }
1327
1328         return db;
1329 }
1330
1331 static dmu_buf_t *osd_mksym(const struct lu_env *env, struct osd_object *obj,
1332                             struct lu_attr *la, uint64_t parent,
1333                             struct osd_thandle *oh)
1334 {
1335         dmu_buf_t *db;
1336         int        rc;
1337
1338         LASSERT(S_ISLNK(la->la_mode));
1339         rc = __osd_object_create(env, obj, &db, oh->ot_tx, la, parent);
1340         if (rc)
1341                 return ERR_PTR(rc);
1342         return db;
1343 }
1344
1345 static dmu_buf_t *osd_mknod(const struct lu_env *env, struct osd_object *obj,
1346                             struct lu_attr *la, uint64_t parent,
1347                             struct osd_thandle *oh)
1348 {
1349         dmu_buf_t *db;
1350         int        rc;
1351
1352         la->la_valid = LA_MODE;
1353         if (S_ISCHR(la->la_mode) || S_ISBLK(la->la_mode))
1354                 la->la_valid |= LA_RDEV;
1355
1356         rc = __osd_object_create(env, obj, &db, oh->ot_tx, la, parent);
1357         if (rc)
1358                 return ERR_PTR(rc);
1359         return db;
1360 }
1361
1362 typedef dmu_buf_t *(*osd_obj_type_f)(const struct lu_env *env,
1363                                      struct osd_object *obj,
1364                                      struct lu_attr *la,
1365                                      uint64_t parent,
1366                                      struct osd_thandle *oh);
1367
1368 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1369 {
1370         osd_obj_type_f result;
1371
1372         switch (type) {
1373         case DFT_DIR:
1374                 result = osd_mkdir;
1375                 break;
1376         case DFT_INDEX:
1377                 result = osd_mkidx;
1378                 break;
1379         case DFT_REGULAR:
1380                 result = osd_mkreg;
1381                 break;
1382         case DFT_SYM:
1383                 result = osd_mksym;
1384                 break;
1385         case DFT_NODE:
1386                 result = osd_mknod;
1387                 break;
1388         default:
1389                 LBUG();
1390                 break;
1391         }
1392         return result;
1393 }
1394
1395 /*
1396  * Primitives for directory (i.e. ZAP) handling
1397  */
1398 static inline int osd_init_lma(const struct lu_env *env, struct osd_object *obj,
1399                                const struct lu_fid *fid, struct osd_thandle *oh)
1400 {
1401         struct osd_thread_info  *info = osd_oti_get(env);
1402         struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
1403         struct lu_buf            buf;
1404         int rc;
1405
1406         lustre_lma_init(lma, fid, 0, 0);
1407         lustre_lma_swab(lma);
1408         buf.lb_buf = lma;
1409         buf.lb_len = sizeof(*lma);
1410
1411         rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
1412                                     LU_XATTR_CREATE, oh);
1413
1414         return rc;
1415 }
1416
1417 /*
1418  * Concurrency: @dt is write locked.
1419  */
1420 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1421                              struct lu_attr *attr,
1422                              struct dt_allocation_hint *hint,
1423                              struct dt_object_format *dof,
1424                              struct thandle *th)
1425 {
1426         struct zpl_direntry     *zde = &osd_oti_get(env)->oti_zde.lzd_reg;
1427         const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
1428         struct osd_object       *obj = osd_dt_obj(dt);
1429         struct osd_device       *osd = osd_obj2dev(obj);
1430         char                    *buf = osd_oti_get(env)->oti_str;
1431         struct osd_thandle      *oh;
1432         dmu_buf_t               *db;
1433         uint64_t                 zapid;
1434         int                      rc;
1435
1436         ENTRY;
1437
1438         /* concurrent create declarations should not see
1439          * the object inconsistent (db, attr, etc).
1440          * in regular cases acquisition should be cheap */
1441         down(&obj->oo_guard);
1442
1443         LASSERT(osd_invariant(obj));
1444         LASSERT(!dt_object_exists(dt));
1445         LASSERT(dof != NULL);
1446
1447         LASSERT(th != NULL);
1448         oh = container_of0(th, struct osd_thandle, ot_super);
1449
1450         /*
1451          * XXX missing: Quote handling.
1452          */
1453
1454         LASSERT(obj->oo_db == NULL);
1455
1456         /* to follow ZFS on-disk format we need
1457          * to initialize parent dnode properly */
1458         zapid = 0;
1459         if (hint != NULL && hint->dah_parent != NULL &&
1460             !dt_object_remote(hint->dah_parent))
1461                 zapid = osd_dt_obj(hint->dah_parent)->oo_db->db_object;
1462
1463         db = osd_create_type_f(dof->dof_type)(env, obj, attr, zapid, oh);
1464         if (IS_ERR(db))
1465                 GOTO(out, rc = PTR_ERR(db));
1466
1467         zde->zde_pad = 0;
1468         zde->zde_dnode = db->db_object;
1469         zde->zde_type = IFTODT(attr->la_mode & S_IFMT);
1470
1471         zapid = osd_get_name_n_idx(env, osd, fid, buf);
1472
1473         rc = -zap_add(osd->od_os, zapid, buf, 8, 1, zde, oh->ot_tx);
1474         if (rc)
1475                 GOTO(out, rc);
1476
1477         /* Add new object to inode accounting.
1478          * Errors are not considered as fatal */
1479         rc = -zap_increment_int(osd->od_os, osd->od_iusr_oid,
1480                                 (attr->la_valid & LA_UID) ? attr->la_uid : 0, 1,
1481                                 oh->ot_tx);
1482         if (rc)
1483                 CERROR("%s: failed to add "DFID" to accounting ZAP for usr %d "
1484                         "(%d)\n", osd->od_svname, PFID(fid), attr->la_uid, rc);
1485         rc = -zap_increment_int(osd->od_os, osd->od_igrp_oid,
1486                                 (attr->la_valid & LA_GID) ? attr->la_gid : 0, 1,
1487                                 oh->ot_tx);
1488         if (rc)
1489                 CERROR("%s: failed to add "DFID" to accounting ZAP for grp %d "
1490                         "(%d)\n", osd->od_svname, PFID(fid), attr->la_gid, rc);
1491
1492         /* configure new osd object */
1493         obj->oo_db = db;
1494         rc = osd_object_init0(env, obj);
1495         LASSERT(ergo(rc == 0, dt_object_exists(dt)));
1496         LASSERT(osd_invariant(obj));
1497
1498         rc = osd_init_lma(env, obj, fid, oh);
1499         if (rc) {
1500                 CERROR("%s: can not set LMA on "DFID": rc = %d\n",
1501                        osd->od_svname, PFID(fid), rc);
1502                 /* ignore errors during LMA initialization */
1503                 rc = 0;
1504         }
1505
1506 out:
1507         up(&obj->oo_guard);
1508         RETURN(rc);
1509 }
1510
1511 static int osd_declare_object_ref_add(const struct lu_env *env,
1512                                       struct dt_object *dt,
1513                                       struct thandle *th)
1514 {
1515         return osd_declare_attr_set(env, dt, NULL, th);
1516 }
1517
1518 /*
1519  * Concurrency: @dt is write locked.
1520  */
1521 static int osd_object_ref_add(const struct lu_env *env,
1522                               struct dt_object *dt,
1523                               struct thandle *handle)
1524 {
1525         struct osd_object       *obj = osd_dt_obj(dt);
1526         struct osd_thandle      *oh;
1527         struct osd_device       *osd = osd_obj2dev(obj);
1528         uint64_t                 nlink;
1529         int rc;
1530
1531         ENTRY;
1532
1533         if (!dt_object_exists(dt))
1534                 RETURN(-ENOENT);
1535
1536         LASSERT(osd_invariant(obj));
1537         LASSERT(obj->oo_sa_hdl != NULL);
1538
1539         oh = container_of0(handle, struct osd_thandle, ot_super);
1540
1541         write_lock(&obj->oo_attr_lock);
1542         nlink = ++obj->oo_attr.la_nlink;
1543         write_unlock(&obj->oo_attr_lock);
1544
1545         rc = osd_object_sa_update(obj, SA_ZPL_LINKS(osd), &nlink, 8, oh);
1546         return rc;
1547 }
1548
1549 static int osd_declare_object_ref_del(const struct lu_env *env,
1550                                       struct dt_object *dt,
1551                                       struct thandle *handle)
1552 {
1553         return osd_declare_attr_set(env, dt, NULL, handle);
1554 }
1555
1556 /*
1557  * Concurrency: @dt is write locked.
1558  */
1559 static int osd_object_ref_del(const struct lu_env *env,
1560                               struct dt_object *dt,
1561                               struct thandle *handle)
1562 {
1563         struct osd_object       *obj = osd_dt_obj(dt);
1564         struct osd_thandle      *oh;
1565         struct osd_device       *osd = osd_obj2dev(obj);
1566         uint64_t                 nlink;
1567         int                      rc;
1568
1569         ENTRY;
1570
1571         LASSERT(osd_invariant(obj));
1572         LASSERT(dt_object_exists(dt));
1573         LASSERT(obj->oo_sa_hdl != NULL);
1574
1575         oh = container_of0(handle, struct osd_thandle, ot_super);
1576         LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
1577
1578         write_lock(&obj->oo_attr_lock);
1579         nlink = --obj->oo_attr.la_nlink;
1580         write_unlock(&obj->oo_attr_lock);
1581
1582         rc = osd_object_sa_update(obj, SA_ZPL_LINKS(osd), &nlink, 8, oh);
1583         RETURN(rc);
1584 }
1585
1586 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt,
1587                            __u64 start, __u64 end)
1588 {
1589         struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
1590         ENTRY;
1591
1592         /* XXX: no other option than syncing the whole filesystem until we
1593          * support ZIL.  If the object tracked the txg that it was last
1594          * modified in, it could pass that txg here instead of "0".  Maybe
1595          * the changes are already committed, so no wait is needed at all? */
1596         txg_wait_synced(dmu_objset_pool(osd->od_os), 0ULL);
1597
1598         RETURN(0);
1599 }
1600
1601 static struct dt_object_operations osd_obj_ops = {
1602         .do_read_lock           = osd_object_read_lock,
1603         .do_write_lock          = osd_object_write_lock,
1604         .do_read_unlock         = osd_object_read_unlock,
1605         .do_write_unlock        = osd_object_write_unlock,
1606         .do_write_locked        = osd_object_write_locked,
1607         .do_attr_get            = osd_attr_get,
1608         .do_declare_attr_set    = osd_declare_attr_set,
1609         .do_attr_set            = osd_attr_set,
1610         .do_ah_init             = osd_ah_init,
1611         .do_declare_create      = osd_declare_object_create,
1612         .do_create              = osd_object_create,
1613         .do_declare_destroy     = osd_declare_object_destroy,
1614         .do_destroy             = osd_object_destroy,
1615         .do_index_try           = osd_index_try,
1616         .do_declare_ref_add     = osd_declare_object_ref_add,
1617         .do_ref_add             = osd_object_ref_add,
1618         .do_declare_ref_del     = osd_declare_object_ref_del,
1619         .do_ref_del             = osd_object_ref_del,
1620         .do_xattr_get           = osd_xattr_get,
1621         .do_declare_xattr_set   = osd_declare_xattr_set,
1622         .do_xattr_set           = osd_xattr_set,
1623         .do_declare_xattr_del   = osd_declare_xattr_del,
1624         .do_xattr_del           = osd_xattr_del,
1625         .do_xattr_list          = osd_xattr_list,
1626         .do_object_sync         = osd_object_sync,
1627 };
1628
1629 static struct lu_object_operations osd_lu_obj_ops = {
1630         .loo_object_init        = osd_object_init,
1631         .loo_object_delete      = osd_object_delete,
1632         .loo_object_release     = osd_object_release,
1633         .loo_object_free        = osd_object_free,
1634         .loo_object_print       = osd_object_print,
1635         .loo_object_invariant   = osd_object_invariant,
1636 };
1637
1638 static int osd_otable_it_attr_get(const struct lu_env *env,
1639                                 struct dt_object *dt,
1640                                 struct lu_attr *attr)
1641 {
1642         attr->la_valid = 0;
1643         return 0;
1644 }
1645
1646 static struct dt_object_operations osd_obj_otable_it_ops = {
1647         .do_attr_get    = osd_otable_it_attr_get,
1648         .do_index_try   = osd_index_try,
1649 };