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