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