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