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