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