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