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