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