Whamcloud - gitweb
9190f653d8040d3b1af472f6b122e5ff7caa77f1
[fs/lustre-release.git] / lustre / osd-zfs / osd_index.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_index.c
33  *
34  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
35  * Author: Mike Pershin <tappro@whamcloud.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_OSD
39
40 #include <lustre_ver.h>
41 #include <libcfs/libcfs.h>
42 #include <obd_support.h>
43 #include <lustre_net.h>
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <lustre_disk.h>
47 #include <lustre_fid.h>
48
49 #include "osd_internal.h"
50
51 #include <sys/dnode.h>
52 #include <sys/spa.h>
53 #include <sys/stat.h>
54 #include <sys/zap.h>
55 #include <sys/spa_impl.h>
56 #include <sys/zfs_znode.h>
57 #include <sys/dmu_tx.h>
58 #include <sys/dmu_objset.h>
59 #include <sys/dsl_prop.h>
60 #include <sys/sa_impl.h>
61 #include <sys/txg.h>
62
63 static inline int osd_object_is_zap(dnode_t *dn)
64 {
65         return (dn->dn_type == DMU_OT_DIRECTORY_CONTENTS ||
66                         dn->dn_type == DMU_OT_USERGROUP_USED);
67 }
68
69 /* We don't actually have direct access to the zap_hashbits() function
70  * so just pretend like we do for now.  If this ever breaks we can look at
71  * it at that time. */
72 #define zap_hashbits(zc) 48
73 /*
74  * ZFS hash format:
75  * | cd (16 bits) | hash (48 bits) |
76  * we need it in other form:
77  * |0| hash (48 bit) | cd (15 bit) |
78  * to be a full 64-bit ordered hash so that Lustre readdir can use it to merge
79  * the readdir hashes from multiple directory stripes uniformly on the client.
80  * Another point is sign bit, the hash range should be in [0, 2^63-1] because
81  * loff_t (for llseek) needs to be a positive value.  This means the "cd" field
82  * should only be the low 15 bits.
83  */
84 uint64_t osd_zap_cursor_serialize(zap_cursor_t *zc)
85 {
86         uint64_t zfs_hash = zap_cursor_serialize(zc) & (~0ULL >> 1);
87
88         return (zfs_hash >> zap_hashbits(zc)) |
89                 (zfs_hash << (63 - zap_hashbits(zc)));
90 }
91
92 void osd_zap_cursor_init_serialized(zap_cursor_t *zc, struct objset *os,
93                                     uint64_t id, uint64_t dirhash)
94 {
95         uint64_t zfs_hash = ((dirhash << zap_hashbits(zc)) & (~0ULL >> 1)) |
96                 (dirhash >> (63 - zap_hashbits(zc)));
97
98         zap_cursor_init_serialized(zc, os, id, zfs_hash);
99 }
100
101 int osd_zap_cursor_init(zap_cursor_t **zc, struct objset *os,
102                         uint64_t id, uint64_t dirhash)
103 {
104         zap_cursor_t *t;
105
106         OBD_ALLOC_PTR(t);
107         if (unlikely(t == NULL))
108                 return -ENOMEM;
109
110         osd_zap_cursor_init_serialized(t, os, id, dirhash);
111         *zc = t;
112
113         return 0;
114 }
115
116 void osd_zap_cursor_fini(zap_cursor_t *zc)
117 {
118         zap_cursor_fini(zc);
119         OBD_FREE_PTR(zc);
120 }
121
122 static inline void osd_obj_cursor_init_serialized(zap_cursor_t *zc,
123                                                  struct osd_object *o,
124                                                  uint64_t dirhash)
125 {
126         struct osd_device *d = osd_obj2dev(o);
127         osd_zap_cursor_init_serialized(zc, d->od_os,
128                                        o->oo_dn->dn_object, dirhash);
129 }
130
131 static inline int osd_obj_cursor_init(zap_cursor_t **zc, struct osd_object *o,
132                         uint64_t dirhash)
133 {
134         struct osd_device *d = osd_obj2dev(o);
135         return osd_zap_cursor_init(zc, d->od_os, o->oo_dn->dn_object, dirhash);
136 }
137
138 static struct dt_it *osd_index_it_init(const struct lu_env *env,
139                                        struct dt_object *dt,
140                                        __u32 unused)
141 {
142         struct osd_thread_info  *info = osd_oti_get(env);
143         struct osd_zap_it       *it;
144         struct osd_object       *obj = osd_dt_obj(dt);
145         struct lu_object        *lo  = &dt->do_lu;
146         int                      rc;
147         ENTRY;
148
149         if (obj->oo_destroyed)
150                 RETURN(ERR_PTR(-ENOENT));
151
152         LASSERT(lu_object_exists(lo));
153         LASSERT(obj->oo_dn);
154         LASSERT(info);
155
156         OBD_SLAB_ALLOC_PTR_GFP(it, osd_zapit_cachep, GFP_NOFS);
157         if (it == NULL)
158                 RETURN(ERR_PTR(-ENOMEM));
159
160         rc = osd_obj_cursor_init(&it->ozi_zc, obj, 0);
161         if (rc != 0) {
162                 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
163                 RETURN(ERR_PTR(rc));
164         }
165
166         it->ozi_obj   = obj;
167         it->ozi_reset = 1;
168         lu_object_get(lo);
169
170         RETURN((struct dt_it *)it);
171 }
172
173 static void osd_index_it_fini(const struct lu_env *env, struct dt_it *di)
174 {
175         struct osd_zap_it       *it     = (struct osd_zap_it *)di;
176         struct osd_object       *obj;
177         ENTRY;
178
179         LASSERT(it);
180         LASSERT(it->ozi_obj);
181
182         obj = it->ozi_obj;
183
184         osd_zap_cursor_fini(it->ozi_zc);
185         osd_object_put(env, obj);
186         OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
187
188         EXIT;
189 }
190
191
192 static void osd_index_it_put(const struct lu_env *env, struct dt_it *di)
193 {
194         /* PBS: do nothing : ref are incremented at retrive and decreamented
195          *      next/finish. */
196 }
197
198 static inline void osd_it_append_attrs(struct lu_dirent *ent, __u32 attr,
199                                        int len, __u16 type)
200 {
201         const unsigned    align = sizeof(struct luda_type) - 1;
202         struct luda_type *lt;
203
204         /* check if file type is required */
205         if (attr & LUDA_TYPE) {
206                 len = (len + align) & ~align;
207
208                 lt = (void *)ent->lde_name + len;
209                 lt->lt_type = cpu_to_le16(DTTOIF(type));
210                 ent->lde_attrs |= LUDA_TYPE;
211         }
212
213         ent->lde_attrs = cpu_to_le32(ent->lde_attrs);
214 }
215
216 /**
217  * Get the object's FID from its LMA EA.
218  *
219  * \param[in] env       pointer to the thread context
220  * \param[in] osd       pointer to the OSD device
221  * \param[in] oid       the object's local identifier
222  * \param[out] fid      the buffer to hold the object's FID
223  *
224  * \retval              0 for success
225  * \retval              negative error number on failure
226  */
227 static int osd_get_fid_by_oid(const struct lu_env *env, struct osd_device *osd,
228                               uint64_t oid, struct lu_fid *fid)
229 {
230         struct objset           *os       = osd->od_os;
231         struct osd_thread_info  *oti      = osd_oti_get(env);
232         struct lustre_mdt_attrs *lma      =
233                         (struct lustre_mdt_attrs *)oti->oti_buf;
234         struct lu_buf            buf;
235         nvlist_t                *sa_xattr = NULL;
236         sa_handle_t             *sa_hdl   = NULL;
237         uchar_t                 *nv_value = NULL;
238         uint64_t                 xattr    = ZFS_NO_OBJECT;
239         int                      size     = 0;
240         int                      rc;
241         ENTRY;
242
243         rc = __osd_xattr_load(osd, oid, &sa_xattr);
244         if (rc == -ENOENT)
245                 goto regular;
246
247         if (rc != 0)
248                 GOTO(out, rc);
249
250         rc = -nvlist_lookup_byte_array(sa_xattr, XATTR_NAME_LMA, &nv_value,
251                                        &size);
252         if (rc == -ENOENT)
253                 goto regular;
254
255         if (rc != 0)
256                 GOTO(out, rc);
257
258         if (unlikely(size > sizeof(oti->oti_buf)))
259                 GOTO(out, rc = -ERANGE);
260
261         memcpy(lma, nv_value, size);
262
263         goto found;
264
265 regular:
266         rc = -sa_handle_get(os, oid, NULL, SA_HDL_PRIVATE, &sa_hdl);
267         if (rc != 0)
268                 GOTO(out, rc);
269
270         rc = -sa_lookup(sa_hdl, SA_ZPL_XATTR(osd), &xattr, 8);
271         sa_handle_destroy(sa_hdl);
272         if (rc != 0)
273                 GOTO(out, rc);
274
275         buf.lb_buf = lma;
276         buf.lb_len = sizeof(oti->oti_buf);
277         rc = __osd_xattr_get_large(env, osd, xattr, &buf,
278                                    XATTR_NAME_LMA, &size);
279         if (rc != 0)
280                 GOTO(out, rc);
281
282 found:
283         if (size < sizeof(*lma))
284                 GOTO(out, rc = -EIO);
285
286         lustre_lma_swab(lma);
287         if (unlikely((lma->lma_incompat & ~LMA_INCOMPAT_SUPP) ||
288                      CFS_FAIL_CHECK(OBD_FAIL_OSD_LMA_INCOMPAT))) {
289                 CWARN("%s: unsupported incompat LMA feature(s) %#x for "
290                       "oid = %#llx\n", osd->od_svname,
291                       lma->lma_incompat & ~LMA_INCOMPAT_SUPP, oid);
292                 GOTO(out, rc = -EOPNOTSUPP);
293         } else {
294                 *fid = lma->lma_self_fid;
295                 GOTO(out, rc = 0);
296         }
297
298 out:
299         if (sa_xattr != NULL)
300                 nvlist_free(sa_xattr);
301         return rc;
302 }
303
304 /*
305  * As we don't know FID, we can't use LU object, so this function
306  * partially duplicate __osd_xattr_get() which is built around
307  * LU-object and uses it to cache data like regular EA dnode, etc
308  */
309 static int osd_find_parent_by_dnode(const struct lu_env *env,
310                                     struct dt_object *o,
311                                     struct lu_fid *fid)
312 {
313         struct osd_device       *osd = osd_obj2dev(osd_dt_obj(o));
314         sa_handle_t             *sa_hdl;
315         uint64_t                 dnode = ZFS_NO_OBJECT;
316         int                      rc;
317         ENTRY;
318
319         /* first of all, get parent dnode from own attributes */
320         LASSERT(osd_dt_obj(o)->oo_dn);
321         rc = -sa_handle_get(osd->od_os, osd_dt_obj(o)->oo_dn->dn_object,
322                             NULL, SA_HDL_PRIVATE, &sa_hdl);
323         if (rc != 0)
324                 RETURN(rc);
325
326         rc = -sa_lookup(sa_hdl, SA_ZPL_PARENT(osd), &dnode, 8);
327         sa_handle_destroy(sa_hdl);
328         if (rc == 0)
329                 rc = osd_get_fid_by_oid(env, osd, dnode, fid);
330
331         RETURN(rc);
332 }
333
334 static int osd_find_parent_fid(const struct lu_env *env, struct dt_object *o,
335                                struct lu_fid *fid)
336 {
337         struct link_ea_header  *leh;
338         struct link_ea_entry   *lee;
339         struct lu_buf           buf;
340         int                     rc;
341         ENTRY;
342
343         buf.lb_buf = osd_oti_get(env)->oti_buf;
344         buf.lb_len = sizeof(osd_oti_get(env)->oti_buf);
345
346         rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
347         if (rc == -ERANGE) {
348                 rc = osd_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LINK);
349                 if (rc < 0)
350                         RETURN(rc);
351                 LASSERT(rc > 0);
352                 OBD_ALLOC(buf.lb_buf, rc);
353                 if (buf.lb_buf == NULL)
354                         RETURN(-ENOMEM);
355                 buf.lb_len = rc;
356                 rc = osd_xattr_get(env, o, &buf, XATTR_NAME_LINK);
357         }
358         if (rc < 0)
359                 GOTO(out, rc);
360         if (rc < sizeof(*leh) + sizeof(*lee))
361                 GOTO(out, rc = -EINVAL);
362
363         leh = buf.lb_buf;
364         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
365                 leh->leh_magic = LINK_EA_MAGIC;
366                 leh->leh_reccount = __swab32(leh->leh_reccount);
367                 leh->leh_len = __swab64(leh->leh_len);
368         }
369         if (leh->leh_magic != LINK_EA_MAGIC)
370                 GOTO(out, rc = -EINVAL);
371         if (leh->leh_reccount == 0)
372                 GOTO(out, rc = -ENODATA);
373
374         lee = (struct link_ea_entry *)(leh + 1);
375         fid_be_to_cpu(fid, (const struct lu_fid *)&lee->lee_parent_fid);
376         rc = 0;
377
378 out:
379         if (buf.lb_buf != osd_oti_get(env)->oti_buf)
380                 OBD_FREE(buf.lb_buf, buf.lb_len);
381
382 #if 0
383         /* this block can be enabled for additional verification
384          * it's trying to match FID from LinkEA vs. FID from LMA */
385         if (rc == 0) {
386                 struct lu_fid fid2;
387                 int rc2;
388                 rc2 = osd_find_parent_by_dnode(env, o, &fid2);
389                 if (rc2 == 0)
390                         if (lu_fid_eq(fid, &fid2) == 0)
391                                 CERROR("wrong parent: "DFID" != "DFID"\n",
392                                        PFID(fid), PFID(&fid2));
393         }
394 #endif
395
396         /* no LinkEA is found, let's try to find the fid in parent's LMA */
397         if (unlikely(rc != 0))
398                 rc = osd_find_parent_by_dnode(env, o, fid);
399
400         RETURN(rc);
401 }
402
403 static int osd_dir_lookup(const struct lu_env *env, struct dt_object *dt,
404                           struct dt_rec *rec, const struct dt_key *key)
405 {
406         struct osd_thread_info *oti = osd_oti_get(env);
407         struct osd_object  *obj = osd_dt_obj(dt);
408         struct osd_device  *osd = osd_obj2dev(obj);
409         char               *name = (char *)key;
410         int                 rc;
411         ENTRY;
412
413         if (name[0] == '.') {
414                 if (name[1] == 0) {
415                         const struct lu_fid *f = lu_object_fid(&dt->do_lu);
416                         memcpy(rec, f, sizeof(*f));
417                         RETURN(1);
418                 } else if (name[1] == '.' && name[2] == 0) {
419                         rc = osd_find_parent_fid(env, dt, (struct lu_fid *)rec);
420                         RETURN(rc == 0 ? 1 : rc);
421                 }
422         }
423
424         memset(&oti->oti_zde.lzd_fid, 0, sizeof(struct lu_fid));
425         rc = -zap_lookup(osd->od_os, obj->oo_dn->dn_object,
426                          (char *)key, 8, sizeof(oti->oti_zde) / 8,
427                          (void *)&oti->oti_zde);
428         if (rc != 0)
429                 RETURN(rc);
430
431         if (likely(fid_is_sane(&oti->oti_zde.lzd_fid))) {
432                 memcpy(rec, &oti->oti_zde.lzd_fid, sizeof(struct lu_fid));
433                 RETURN(1);
434         }
435
436         rc = osd_get_fid_by_oid(env, osd, oti->oti_zde.lzd_reg.zde_dnode,
437                                 (struct lu_fid *)rec);
438
439         RETURN(rc == 0 ? 1 : (rc == -ENOENT ? -ENODATA : rc));
440 }
441
442 static int osd_declare_dir_insert(const struct lu_env *env,
443                                   struct dt_object *dt,
444                                   const struct dt_rec *rec,
445                                   const struct dt_key *key,
446                                   struct thandle *th)
447 {
448         struct osd_object       *obj = osd_dt_obj(dt);
449         struct osd_device       *osd = osd_obj2dev(obj);
450         const struct dt_insert_rec *rec1;
451         const struct lu_fid     *fid;
452         struct osd_thandle      *oh;
453         uint64_t                 object;
454         ENTRY;
455
456         rec1 = (struct dt_insert_rec *)rec;
457         fid = rec1->rec_fid;
458         LASSERT(fid != NULL);
459         LASSERT(rec1->rec_type != 0);
460
461         LASSERT(th != NULL);
462         oh = container_of0(th, struct osd_thandle, ot_super);
463
464         /* This is for inserting dot/dotdot for new created dir. */
465         if (obj->oo_dn == NULL)
466                 object = DMU_NEW_OBJECT;
467         else
468                 object = obj->oo_dn->dn_object;
469
470         /* do not specify the key as then DMU is trying to look it up
471          * which is very expensive. usually the layers above lookup
472          * before insertion */
473         dmu_tx_hold_zap(oh->ot_tx, object, TRUE, NULL);
474
475         osd_idc_find_or_init(env, osd, fid);
476
477         RETURN(0);
478 }
479
480 static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
481                           u64 seq)
482 {
483         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
484         struct seq_server_site  *ss = osd_seq_site(osd);
485         int                     rc;
486         ENTRY;
487
488         LASSERT(ss != NULL);
489         LASSERT(ss->ss_server_fld != NULL);
490
491         rc = osd_fld_lookup(env, osd, seq, range);
492         if (rc != 0) {
493                 if (rc != -ENOENT)
494                         CERROR("%s: Can not lookup fld for %#llx\n",
495                                osd_name(osd), seq);
496                 RETURN(0);
497         }
498
499         RETURN(ss->ss_node_id == range->lsr_index);
500 }
501
502 int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
503                    const struct lu_fid *fid)
504 {
505         struct seq_server_site  *ss = osd_seq_site(osd);
506         ENTRY;
507
508         /* FID seqs not in FLDB, must be local seq */
509         if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
510                 RETURN(0);
511
512         /* If FLD is not being initialized yet, it only happens during the
513          * initialization, likely during mgs initialization, and we assume
514          * this is local FID. */
515         if (ss == NULL || ss->ss_server_fld == NULL)
516                 RETURN(0);
517
518         /* Only check the local FLDB here */
519         if (osd_seq_exists(env, osd, fid_seq(fid)))
520                 RETURN(0);
521
522         RETURN(1);
523 }
524
525 /**
526  *      Inserts (key, value) pair in \a directory object.
527  *
528  *      \param  dt      osd index object
529  *      \param  key     key for index
530  *      \param  rec     record reference
531  *      \param  th      transaction handler
532  *      \param  ignore_quota update should not affect quota
533  *
534  *      \retval  0  success
535  *      \retval -ve failure
536  */
537 static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
538                           const struct dt_rec *rec, const struct dt_key *key,
539                           struct thandle *th, int ignore_quota)
540 {
541         struct osd_thread_info *oti = osd_oti_get(env);
542         struct osd_object   *parent = osd_dt_obj(dt);
543         struct osd_device   *osd = osd_obj2dev(parent);
544         struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
545         const struct lu_fid *fid = rec1->rec_fid;
546         struct osd_thandle *oh;
547         struct osd_idmap_cache *idc;
548         char                *name = (char *)key;
549         int                  rc;
550         ENTRY;
551
552         LASSERT(parent->oo_dn);
553
554         LASSERT(dt_object_exists(dt));
555         LASSERT(osd_invariant(parent));
556
557         LASSERT(th != NULL);
558         oh = container_of0(th, struct osd_thandle, ot_super);
559
560         idc = osd_idc_find(env, osd, fid);
561         if (unlikely(idc == NULL)) {
562                 /* this dt_insert() wasn't declared properly, so
563                  * FID is missing in OI cache. we better do not
564                  * lookup FID in FLDB/OI and don't risk to deadlock,
565                  * but in some special cases (lfsck testing, etc)
566                  * it's much simpler than fixing a caller */
567                 CERROR("%s: "DFID" wasn't declared for insert\n",
568                        osd_name(osd), PFID(fid));
569                 idc = osd_idc_find_or_init(env, osd, fid);
570                 if (IS_ERR(idc))
571                         RETURN(PTR_ERR(idc));
572         }
573
574         if (idc->oic_remote) {
575                 /* Insert remote entry */
576                 memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
577                 oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
578         } else {
579                 if (unlikely(idc->oic_dnode == 0)) {
580                         /* for a reason OI cache wasn't filled properly */
581                         CERROR("%s: OIC for "DFID" isn't filled\n",
582                                osd_name(osd), PFID(fid));
583                         RETURN(-EINVAL);
584                 }
585                 if (name[0] == '.') {
586                         if (name[1] == 0) {
587                                 /* do not store ".", instead generate it
588                                  * during iteration */
589                                 GOTO(out, rc = 0);
590                         } else if (name[1] == '.' && name[2] == 0) {
591                                 uint64_t dnode = idc->oic_dnode;
592                                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT))
593                                         dnode--;
594
595                                 /* update parent dnode in the child.
596                                  * later it will be used to generate ".." */
597                                 rc = osd_object_sa_update(parent,
598                                                  SA_ZPL_PARENT(osd),
599                                                  &dnode, 8, oh);
600
601                                 GOTO(out, rc);
602                         }
603                 }
604                 CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
605                 CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
606                 oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
607                 oti->oti_zde.lzd_reg.zde_dnode = idc->oic_dnode;
608         }
609
610         oti->oti_zde.lzd_fid = *fid;
611         /* Insert (key,oid) into ZAP */
612         rc = -zap_add(osd->od_os, parent->oo_dn->dn_object,
613                       (char *)key, 8, sizeof(oti->oti_zde) / 8,
614                       (void *)&oti->oti_zde, oh->ot_tx);
615         if (unlikely(rc == -EEXIST &&
616                      name[0] == '.' && name[1] == '.' && name[2] == 0))
617                 /* Update (key,oid) in ZAP */
618                 rc = -zap_update(osd->od_os, parent->oo_dn->dn_object,
619                                 (char *)key, 8, sizeof(oti->oti_zde) / 8,
620                                 (void *)&oti->oti_zde, oh->ot_tx);
621
622 out:
623
624         RETURN(rc);
625 }
626
627 static int osd_declare_dir_delete(const struct lu_env *env,
628                                   struct dt_object *dt,
629                                   const struct dt_key *key,
630                                   struct thandle *th)
631 {
632         struct osd_object  *obj = osd_dt_obj(dt);
633         struct osd_thandle *oh;
634         uint64_t            dnode;
635         ENTRY;
636
637         LASSERT(dt_object_exists(dt));
638         LASSERT(osd_invariant(obj));
639
640         LASSERT(th != NULL);
641         oh = container_of0(th, struct osd_thandle, ot_super);
642
643         if (dt_object_exists(dt)) {
644                 LASSERT(obj->oo_dn);
645                 dnode = obj->oo_dn->dn_object;
646         } else {
647                 dnode = DMU_NEW_OBJECT;
648         }
649
650         /* do not specify the key as then DMU is trying to look it up
651          * which is very expensive. usually the layers above lookup
652          * before deletion */
653         dmu_tx_hold_zap(oh->ot_tx, dnode, FALSE, NULL);
654
655         RETURN(0);
656 }
657
658 static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
659                           const struct dt_key *key, struct thandle *th)
660 {
661         struct osd_object *obj = osd_dt_obj(dt);
662         struct osd_device *osd = osd_obj2dev(obj);
663         struct osd_thandle *oh;
664         dnode_t *zap_dn = obj->oo_dn;
665         char      *name = (char *)key;
666         int rc;
667         ENTRY;
668
669         LASSERT(zap_dn);
670
671         LASSERT(th != NULL);
672         oh = container_of0(th, struct osd_thandle, ot_super);
673
674         /*
675          * In Orion . and .. were stored in the directory (not generated upon
676          * request as now). we preserve them for backward compatibility
677          */
678         if (name[0] == '.') {
679                 if (name[1] == 0) {
680                         RETURN(0);
681                 } else if (name[1] == '.' && name[2] == 0) {
682                         RETURN(0);
683                 }
684         }
685
686         /* Remove key from the ZAP */
687         rc = -zap_remove(osd->od_os, zap_dn->dn_object,
688                          (char *) key, oh->ot_tx);
689
690         if (unlikely(rc && rc != -ENOENT))
691                 CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
692
693         RETURN(rc);
694 }
695
696 static struct dt_it *osd_dir_it_init(const struct lu_env *env,
697                                      struct dt_object *dt,
698                                      __u32 unused)
699 {
700         struct osd_zap_it *it;
701
702         it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused);
703         if (!IS_ERR(it))
704                 it->ozi_pos = 0;
705
706         RETURN((struct dt_it *)it);
707 }
708
709 /**
710  *  Move Iterator to record specified by \a key
711  *
712  *  \param  di      osd iterator
713  *  \param  key     key for index
714  *
715  *  \retval +ve  di points to record with least key not larger than key
716  *  \retval  0   di points to exact matched key
717  *  \retval -ve  failure
718  */
719 static int osd_dir_it_get(const struct lu_env *env,
720                           struct dt_it *di, const struct dt_key *key)
721 {
722         struct osd_zap_it *it = (struct osd_zap_it *)di;
723         struct osd_object *obj = it->ozi_obj;
724         char              *name = (char *)key;
725         int                rc;
726         ENTRY;
727
728         LASSERT(it);
729         LASSERT(it->ozi_zc);
730
731         /* reset the cursor */
732         zap_cursor_fini(it->ozi_zc);
733         osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
734
735         /* XXX: implementation of the API is broken at the moment */
736         LASSERT(((const char *)key)[0] == 0);
737
738         if (name[0] == 0) {
739                 it->ozi_pos = 0;
740                 RETURN(1);
741         }
742
743         if (name[0] == '.') {
744                 if (name[1] == 0) {
745                         it->ozi_pos = 1;
746                         GOTO(out, rc = 1);
747                 } else if (name[1] == '.' && name[2] == 0) {
748                         it->ozi_pos = 2;
749                         GOTO(out, rc = 1);
750                 }
751         }
752
753         /* neither . nor .. - some real record */
754         it->ozi_pos = 3;
755         rc = +1;
756
757 out:
758         RETURN(rc);
759 }
760
761 static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
762 {
763         /* PBS: do nothing : ref are incremented at retrive and decreamented
764          *      next/finish. */
765 }
766
767 /*
768  * in Orion . and .. were stored in the directory, while ZPL
769  * and current osd-zfs generate them up on request. so, we
770  * need to ignore previously stored . and ..
771  */
772 static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
773                                         zap_attribute_t *za)
774 {
775         int rc, isdot;
776
777         do {
778                 rc = -zap_cursor_retrieve(it->ozi_zc, za);
779
780                 isdot = 0;
781                 if (unlikely(rc == 0 && za->za_name[0] == '.')) {
782                         if (za->za_name[1] == 0) {
783                                 isdot = 1;
784                         } else if (za->za_name[1] == '.' &&
785                                    za->za_name[2] == 0) {
786                                 isdot = 1;
787                         }
788                         if (unlikely(isdot))
789                                 zap_cursor_advance(it->ozi_zc);
790                 }
791         } while (unlikely(rc == 0 && isdot));
792
793         return rc;
794 }
795
796 /**
797  * to load a directory entry at a time and stored it in
798  * iterator's in-memory data structure.
799  *
800  * \param di, struct osd_it_ea, iterator's in memory structure
801  *
802  * \retval +ve, iterator reached to end
803  * \retval   0, iterator not reached to end
804  * \retval -ve, on error
805  */
806 static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
807 {
808         struct osd_zap_it *it = (struct osd_zap_it *)di;
809         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
810         int                rc;
811
812         ENTRY;
813
814         /* temp. storage should be enough for any key supported by ZFS */
815         CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
816
817         /*
818          * the first ->next() moves the cursor to .
819          * the second ->next() moves the cursor to ..
820          * then we get to the real records and have to verify any exist
821          */
822         if (it->ozi_pos <= 2) {
823                 it->ozi_pos++;
824                 if (it->ozi_pos <=2)
825                         RETURN(0);
826
827         } else {
828                 zap_cursor_advance(it->ozi_zc);
829         }
830
831         /*
832          * According to current API we need to return error if its last entry.
833          * zap_cursor_advance() does not return any value. So we need to call
834          * retrieve to check if there is any record.  We should make
835          * changes to Iterator API to not return status for this API
836          */
837         rc = osd_index_retrieve_skip_dots(it, za);
838
839         if (rc == -ENOENT) /* end of dir */
840                 RETURN(+1);
841
842         RETURN(rc);
843 }
844
845 static struct dt_key *osd_dir_it_key(const struct lu_env *env,
846                                      const struct dt_it *di)
847 {
848         struct osd_zap_it *it = (struct osd_zap_it *)di;
849         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
850         int                rc = 0;
851         ENTRY;
852
853         if (it->ozi_pos <= 1) {
854                 it->ozi_pos = 1;
855                 RETURN((struct dt_key *)".");
856         } else if (it->ozi_pos == 2) {
857                 RETURN((struct dt_key *)"..");
858         }
859
860         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)))
861                 RETURN(ERR_PTR(rc));
862
863         strcpy(it->ozi_name, za->za_name);
864
865         RETURN((struct dt_key *)it->ozi_name);
866 }
867
868 static int osd_dir_it_key_size(const struct lu_env *env, const struct dt_it *di)
869 {
870         struct osd_zap_it *it = (struct osd_zap_it *)di;
871         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
872         int                rc;
873         ENTRY;
874
875         if (it->ozi_pos <= 1) {
876                 it->ozi_pos = 1;
877                 RETURN(2);
878         } else if (it->ozi_pos == 2) {
879                 RETURN(3);
880         }
881
882         if ((rc = -zap_cursor_retrieve(it->ozi_zc, za)) == 0)
883                 rc = strlen(za->za_name);
884
885         RETURN(rc);
886 }
887
888 static int osd_dir_it_rec(const struct lu_env *env, const struct dt_it *di,
889                           struct dt_rec *dtrec, __u32 attr)
890 {
891         struct osd_zap_it   *it = (struct osd_zap_it *)di;
892         struct lu_dirent    *lde = (struct lu_dirent *)dtrec;
893         struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
894         zap_attribute_t     *za = &osd_oti_get(env)->oti_za;
895         int                  rc, namelen;
896         ENTRY;
897
898         if (it->ozi_pos <= 1) {
899                 lde->lde_hash = cpu_to_le64(1);
900                 strcpy(lde->lde_name, ".");
901                 lde->lde_namelen = cpu_to_le16(1);
902                 lde->lde_fid = *lu_object_fid(&it->ozi_obj->oo_dt.do_lu);
903                 lde->lde_attrs = LUDA_FID;
904                 /* append lustre attributes */
905                 osd_it_append_attrs(lde, attr, 1, IFTODT(S_IFDIR));
906                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(1, attr));
907                 it->ozi_pos = 1;
908                 GOTO(out, rc = 0);
909
910         } else if (it->ozi_pos == 2) {
911                 lde->lde_hash = cpu_to_le64(2);
912                 strcpy(lde->lde_name, "..");
913                 lde->lde_namelen = cpu_to_le16(2);
914                 lde->lde_attrs = LUDA_FID;
915                 /* append lustre attributes */
916                 osd_it_append_attrs(lde, attr, 2, IFTODT(S_IFDIR));
917                 lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(2, attr));
918                 rc = osd_find_parent_fid(env, &it->ozi_obj->oo_dt, &lde->lde_fid);
919
920                 /* ENOENT happens at the root of filesystem so ignore it */
921                 if (rc == -ENOENT)
922                         rc = 0;
923                 GOTO(out, rc);
924         }
925
926         LASSERT(lde);
927
928         rc = -zap_cursor_retrieve(it->ozi_zc, za);
929         if (unlikely(rc != 0))
930                 GOTO(out, rc);
931
932         lde->lde_hash = cpu_to_le64(osd_zap_cursor_serialize(it->ozi_zc));
933         namelen = strlen(za->za_name);
934         if (namelen > NAME_MAX)
935                 GOTO(out, rc = -EOVERFLOW);
936         strcpy(lde->lde_name, za->za_name);
937         lde->lde_namelen = cpu_to_le16(namelen);
938
939         if (za->za_integer_length != 8 || za->za_num_integers < 3) {
940                 CERROR("%s: unsupported direntry format: %d %d\n",
941                        osd_obj2dev(it->ozi_obj)->od_svname,
942                        za->za_integer_length, (int)za->za_num_integers);
943
944                 GOTO(out, rc = -EIO);
945         }
946
947         rc = -zap_lookup(it->ozi_zc->zc_objset, it->ozi_zc->zc_zapobj,
948                          za->za_name, za->za_integer_length, 3, zde);
949         if (rc)
950                 GOTO(out, rc);
951
952         lde->lde_fid = zde->lzd_fid;
953         lde->lde_attrs = LUDA_FID;
954
955         /* append lustre attributes */
956         osd_it_append_attrs(lde, attr, namelen, zde->lzd_reg.zde_type);
957
958         lde->lde_reclen = cpu_to_le16(lu_dirent_calc_size(namelen, attr));
959
960 out:
961         RETURN(rc);
962 }
963
964 static int osd_dir_it_rec_size(const struct lu_env *env, const struct dt_it *di,
965                                __u32 attr)
966 {
967         struct osd_zap_it   *it = (struct osd_zap_it *)di;
968         zap_attribute_t     *za = &osd_oti_get(env)->oti_za;
969         size_t               namelen = 0;
970         int                  rc;
971         ENTRY;
972
973         if (it->ozi_pos <= 1)
974                 namelen = 1;
975         else if (it->ozi_pos == 2)
976                 namelen = 2;
977
978         if (namelen > 0) {
979                 rc = lu_dirent_calc_size(namelen, attr);
980                 RETURN(rc);
981         }
982
983         rc = -zap_cursor_retrieve(it->ozi_zc, za);
984         if (unlikely(rc != 0))
985                 RETURN(rc);
986
987         if (za->za_integer_length != 8 || za->za_num_integers < 3) {
988                 CERROR("%s: unsupported direntry format: %d %d\n",
989                        osd_obj2dev(it->ozi_obj)->od_svname,
990                        za->za_integer_length, (int)za->za_num_integers);
991                 RETURN(-EIO);
992         }
993
994         namelen = strlen(za->za_name);
995         if (namelen > NAME_MAX)
996                 RETURN(-EOVERFLOW);
997
998         rc = lu_dirent_calc_size(namelen, attr);
999
1000         RETURN(rc);
1001 }
1002
1003 static __u64 osd_dir_it_store(const struct lu_env *env, const struct dt_it *di)
1004 {
1005         struct osd_zap_it *it = (struct osd_zap_it *)di;
1006         __u64              pos;
1007         ENTRY;
1008
1009         if (it->ozi_pos <= 2)
1010                 pos = it->ozi_pos;
1011         else
1012                 pos = osd_zap_cursor_serialize(it->ozi_zc);
1013
1014         RETURN(pos);
1015 }
1016
1017 /*
1018  * return status :
1019  *  rc == 0 -> end of directory.
1020  *  rc >  0 -> ok, proceed.
1021  *  rc <  0 -> error.  ( EOVERFLOW  can be masked.)
1022  */
1023 static int osd_dir_it_load(const struct lu_env *env,
1024                         const struct dt_it *di, __u64 hash)
1025 {
1026         struct osd_zap_it *it = (struct osd_zap_it *)di;
1027         struct osd_object *obj = it->ozi_obj;
1028         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1029         int                rc;
1030         ENTRY;
1031
1032         /* reset the cursor */
1033         zap_cursor_fini(it->ozi_zc);
1034         osd_obj_cursor_init_serialized(it->ozi_zc, obj, hash);
1035
1036         if (hash <= 2) {
1037                 it->ozi_pos = hash;
1038                 rc = +1;
1039         } else {
1040                 it->ozi_pos = 3;
1041                 /* to return whether the end has been reached */
1042                 rc = osd_index_retrieve_skip_dots(it, za);
1043                 if (rc == 0)
1044                         rc = +1;
1045                 else if (rc == -ENOENT)
1046                         rc = 0;
1047         }
1048
1049         RETURN(rc);
1050 }
1051
1052 struct dt_index_operations osd_dir_ops = {
1053         .dio_lookup         = osd_dir_lookup,
1054         .dio_declare_insert = osd_declare_dir_insert,
1055         .dio_insert         = osd_dir_insert,
1056         .dio_declare_delete = osd_declare_dir_delete,
1057         .dio_delete         = osd_dir_delete,
1058         .dio_it     = {
1059                 .init     = osd_dir_it_init,
1060                 .fini     = osd_index_it_fini,
1061                 .get      = osd_dir_it_get,
1062                 .put      = osd_dir_it_put,
1063                 .next     = osd_dir_it_next,
1064                 .key      = osd_dir_it_key,
1065                 .key_size = osd_dir_it_key_size,
1066                 .rec      = osd_dir_it_rec,
1067                 .rec_size = osd_dir_it_rec_size,
1068                 .store    = osd_dir_it_store,
1069                 .load     = osd_dir_it_load
1070         }
1071 };
1072
1073 /*
1074  * Primitives for index files using binary keys.
1075  */
1076
1077 /* key integer_size is 8 */
1078 static int osd_prepare_key_uint64(struct osd_object *o, __u64 *dst,
1079                                   const struct dt_key *src)
1080 {
1081         int size;
1082
1083         LASSERT(dst);
1084         LASSERT(src);
1085
1086         /* align keysize to 64bit */
1087         size = (o->oo_keysize + sizeof(__u64) - 1) / sizeof(__u64);
1088         size *= sizeof(__u64);
1089
1090         LASSERT(size <= MAXNAMELEN);
1091
1092         if (unlikely(size > o->oo_keysize))
1093                 memset(dst + o->oo_keysize, 0, size - o->oo_keysize);
1094         memcpy(dst, (const char *)src, o->oo_keysize);
1095
1096         return (size/sizeof(__u64));
1097 }
1098
1099 static int osd_index_lookup(const struct lu_env *env, struct dt_object *dt,
1100                         struct dt_rec *rec, const struct dt_key *key)
1101 {
1102         struct osd_object *obj = osd_dt_obj(dt);
1103         struct osd_device *osd = osd_obj2dev(obj);
1104         __u64             *k = osd_oti_get(env)->oti_key64;
1105         int                rc;
1106         ENTRY;
1107
1108         rc = osd_prepare_key_uint64(obj, k, key);
1109
1110         rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1111                                 k, rc, obj->oo_recusize, obj->oo_recsize,
1112                                 (void *)rec);
1113         RETURN(rc == 0 ? 1 : rc);
1114 }
1115
1116 static int osd_declare_index_insert(const struct lu_env *env,
1117                                     struct dt_object *dt,
1118                                     const struct dt_rec *rec,
1119                                     const struct dt_key *key,
1120                                     struct thandle *th)
1121 {
1122         struct osd_object  *obj = osd_dt_obj(dt);
1123         struct osd_thandle *oh;
1124         ENTRY;
1125
1126         LASSERT(th != NULL);
1127         oh = container_of0(th, struct osd_thandle, ot_super);
1128
1129         LASSERT(obj->oo_dn);
1130
1131         dmu_tx_hold_bonus(oh->ot_tx, obj->oo_dn->dn_object);
1132
1133         /* do not specify the key as then DMU is trying to look it up
1134          * which is very expensive. usually the layers above lookup
1135          * before insertion */
1136         dmu_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, TRUE, NULL);
1137
1138         RETURN(0);
1139 }
1140
1141 static int osd_index_insert(const struct lu_env *env, struct dt_object *dt,
1142                             const struct dt_rec *rec, const struct dt_key *key,
1143                             struct thandle *th, int ignore_quota)
1144 {
1145         struct osd_object  *obj = osd_dt_obj(dt);
1146         struct osd_device  *osd = osd_obj2dev(obj);
1147         struct osd_thandle *oh;
1148         __u64              *k = osd_oti_get(env)->oti_key64;
1149         int                 rc;
1150         ENTRY;
1151
1152         LASSERT(obj->oo_dn);
1153         LASSERT(dt_object_exists(dt));
1154         LASSERT(osd_invariant(obj));
1155         LASSERT(th != NULL);
1156
1157         oh = container_of0(th, struct osd_thandle, ot_super);
1158
1159         rc = osd_prepare_key_uint64(obj, k, key);
1160
1161         /* Insert (key,oid) into ZAP */
1162         rc = -zap_add_uint64(osd->od_os, obj->oo_dn->dn_object,
1163                              k, rc, obj->oo_recusize, obj->oo_recsize,
1164                              (void *)rec, oh->ot_tx);
1165         RETURN(rc);
1166 }
1167
1168 static int osd_declare_index_delete(const struct lu_env *env,
1169                                     struct dt_object *dt,
1170                                     const struct dt_key *key,
1171                                     struct thandle *th)
1172 {
1173         struct osd_object  *obj = osd_dt_obj(dt);
1174         struct osd_thandle *oh;
1175         ENTRY;
1176
1177         LASSERT(dt_object_exists(dt));
1178         LASSERT(osd_invariant(obj));
1179         LASSERT(th != NULL);
1180         LASSERT(obj->oo_dn);
1181
1182         oh = container_of0(th, struct osd_thandle, ot_super);
1183
1184         /* do not specify the key as then DMU is trying to look it up
1185          * which is very expensive. usually the layers above lookup
1186          * before deletion */
1187         dmu_tx_hold_zap(oh->ot_tx, obj->oo_dn->dn_object, FALSE, NULL);
1188
1189         RETURN(0);
1190 }
1191
1192 static int osd_index_delete(const struct lu_env *env, struct dt_object *dt,
1193                             const struct dt_key *key, struct thandle *th)
1194 {
1195         struct osd_object  *obj = osd_dt_obj(dt);
1196         struct osd_device  *osd = osd_obj2dev(obj);
1197         struct osd_thandle *oh;
1198         __u64              *k = osd_oti_get(env)->oti_key64;
1199         int                 rc;
1200         ENTRY;
1201
1202         LASSERT(obj->oo_dn);
1203         LASSERT(th != NULL);
1204         oh = container_of0(th, struct osd_thandle, ot_super);
1205
1206         rc = osd_prepare_key_uint64(obj, k, key);
1207
1208         /* Remove binary key from the ZAP */
1209         rc = -zap_remove_uint64(osd->od_os, obj->oo_dn->dn_object,
1210                                 k, rc, oh->ot_tx);
1211         RETURN(rc);
1212 }
1213
1214 static int osd_index_it_get(const struct lu_env *env, struct dt_it *di,
1215                             const struct dt_key *key)
1216 {
1217         struct osd_zap_it *it = (struct osd_zap_it *)di;
1218         struct osd_object *obj = it->ozi_obj;
1219         struct osd_device *osd = osd_obj2dev(obj);
1220         ENTRY;
1221
1222         LASSERT(it);
1223         LASSERT(it->ozi_zc);
1224
1225         /*
1226          * XXX: we need a binary version of zap_cursor_move_to_key()
1227          *      to implement this API */
1228         if (*((const __u64 *)key) != 0)
1229                 CERROR("NOT IMPLEMETED YET (move to %#llx)\n",
1230                        *((__u64 *)key));
1231
1232         zap_cursor_fini(it->ozi_zc);
1233         zap_cursor_init(it->ozi_zc, osd->od_os, obj->oo_dn->dn_object);
1234         it->ozi_reset = 1;
1235
1236         RETURN(+1);
1237 }
1238
1239 static int osd_index_it_next(const struct lu_env *env, struct dt_it *di)
1240 {
1241         struct osd_zap_it *it = (struct osd_zap_it *)di;
1242         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1243         int                rc;
1244         ENTRY;
1245
1246         if (it->ozi_reset == 0)
1247                 zap_cursor_advance(it->ozi_zc);
1248         it->ozi_reset = 0;
1249
1250         /*
1251          * According to current API we need to return error if it's last entry.
1252          * zap_cursor_advance() does not return any value. So we need to call
1253          * retrieve to check if there is any record.  We should make
1254          * changes to Iterator API to not return status for this API
1255          */
1256         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1257         if (rc == -ENOENT)
1258                 RETURN(+1);
1259
1260         RETURN((rc));
1261 }
1262
1263 static struct dt_key *osd_index_it_key(const struct lu_env *env,
1264                                        const struct dt_it *di)
1265 {
1266         struct osd_zap_it *it = (struct osd_zap_it *)di;
1267         struct osd_object *obj = it->ozi_obj;
1268         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1269         int                rc = 0;
1270         ENTRY;
1271
1272         it->ozi_reset = 0;
1273         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1274         if (rc)
1275                 RETURN(ERR_PTR(rc));
1276
1277         /* the binary key is stored in the name */
1278         memcpy(&it->ozi_key, za->za_name, obj->oo_keysize);
1279
1280         RETURN((struct dt_key *)&it->ozi_key);
1281 }
1282
1283 static int osd_index_it_key_size(const struct lu_env *env,
1284                                 const struct dt_it *di)
1285 {
1286         struct osd_zap_it *it = (struct osd_zap_it *)di;
1287         struct osd_object *obj = it->ozi_obj;
1288         RETURN(obj->oo_keysize);
1289 }
1290
1291 static int osd_index_it_rec(const struct lu_env *env, const struct dt_it *di,
1292                             struct dt_rec *rec, __u32 attr)
1293 {
1294         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1295         struct osd_zap_it *it = (struct osd_zap_it *)di;
1296         struct osd_object *obj = it->ozi_obj;
1297         struct osd_device *osd = osd_obj2dev(obj);
1298         __u64             *k = osd_oti_get(env)->oti_key64;
1299         int                rc;
1300         ENTRY;
1301
1302         it->ozi_reset = 0;
1303         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1304         if (rc)
1305                 RETURN(rc);
1306
1307         rc = osd_prepare_key_uint64(obj, k, (const struct dt_key *)za->za_name);
1308
1309         rc = -zap_lookup_uint64(osd->od_os, obj->oo_dn->dn_object,
1310                                 k, rc, obj->oo_recusize, obj->oo_recsize,
1311                                 (void *)rec);
1312         RETURN(rc);
1313 }
1314
1315 static __u64 osd_index_it_store(const struct lu_env *env,
1316                                 const struct dt_it *di)
1317 {
1318         struct osd_zap_it *it = (struct osd_zap_it *)di;
1319
1320         it->ozi_reset = 0;
1321         RETURN((__u64)zap_cursor_serialize(it->ozi_zc));
1322 }
1323
1324 static int osd_index_it_load(const struct lu_env *env, const struct dt_it *di,
1325                              __u64 hash)
1326 {
1327         struct osd_zap_it *it = (struct osd_zap_it *)di;
1328         struct osd_object *obj = it->ozi_obj;
1329         struct osd_device *osd = osd_obj2dev(obj);
1330         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1331         int                rc;
1332         ENTRY;
1333
1334         /* reset the cursor */
1335         zap_cursor_fini(it->ozi_zc);
1336         zap_cursor_init_serialized(it->ozi_zc, osd->od_os,
1337                                    obj->oo_dn->dn_object, hash);
1338         it->ozi_reset = 0;
1339
1340         rc = -zap_cursor_retrieve(it->ozi_zc, za);
1341         if (rc == 0)
1342                 RETURN(+1);
1343         else if (rc == -ENOENT)
1344                 RETURN(0);
1345
1346         RETURN(rc);
1347 }
1348
1349 static struct dt_index_operations osd_index_ops = {
1350         .dio_lookup             = osd_index_lookup,
1351         .dio_declare_insert     = osd_declare_index_insert,
1352         .dio_insert             = osd_index_insert,
1353         .dio_declare_delete     = osd_declare_index_delete,
1354         .dio_delete             = osd_index_delete,
1355         .dio_it = {
1356                 .init           = osd_index_it_init,
1357                 .fini           = osd_index_it_fini,
1358                 .get            = osd_index_it_get,
1359                 .put            = osd_index_it_put,
1360                 .next           = osd_index_it_next,
1361                 .key            = osd_index_it_key,
1362                 .key_size       = osd_index_it_key_size,
1363                 .rec            = osd_index_it_rec,
1364                 .store          = osd_index_it_store,
1365                 .load           = osd_index_it_load
1366         }
1367 };
1368
1369 struct osd_metadnode_it {
1370         struct osd_device       *mit_dev;
1371         __u64                    mit_pos;
1372         struct lu_fid            mit_fid;
1373         int                      mit_prefetched;
1374         __u64                    mit_prefetched_dnode;
1375 };
1376
1377 static struct dt_it *osd_zfs_otable_it_init(const struct lu_env *env,
1378                                             struct dt_object *dt, __u32 attr)
1379 {
1380         struct osd_device       *dev   = osd_dev(dt->do_lu.lo_dev);
1381         struct osd_metadnode_it *it;
1382         ENTRY;
1383
1384         OBD_ALLOC_PTR(it);
1385         if (unlikely(it == NULL))
1386                 RETURN(ERR_PTR(-ENOMEM));
1387
1388         it->mit_dev = dev;
1389
1390         /* XXX: dmu_object_next() does NOT find dnodes allocated
1391          *      in the current non-committed txg, so we force txg
1392          *      commit to find all existing dnodes ... */
1393         if (!dev->od_dt_dev.dd_rdonly)
1394                 txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
1395
1396         RETURN((struct dt_it *)it);
1397 }
1398
1399 static void osd_zfs_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1400 {
1401         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1402
1403         OBD_FREE_PTR(it);
1404 }
1405
1406 static int osd_zfs_otable_it_get(const struct lu_env *env,
1407                                  struct dt_it *di, const struct dt_key *key)
1408 {
1409         return 0;
1410 }
1411
1412 static void osd_zfs_otable_it_put(const struct lu_env *env, struct dt_it *di)
1413 {
1414 }
1415
1416 #define OTABLE_PREFETCH         256
1417
1418 static void osd_zfs_otable_prefetch(const struct lu_env *env,
1419                                     struct osd_metadnode_it *it)
1420 {
1421         struct osd_device       *dev = it->mit_dev;
1422         int                      rc;
1423
1424         /* can go negative on the very first access to the iterator
1425          * or if some non-Lustre objects were found */
1426         if (unlikely(it->mit_prefetched < 0))
1427                 it->mit_prefetched = 0;
1428
1429         if (it->mit_prefetched >= (OTABLE_PREFETCH >> 1))
1430                 return;
1431
1432         if (it->mit_prefetched_dnode == 0)
1433                 it->mit_prefetched_dnode = it->mit_pos;
1434
1435         while (it->mit_prefetched < OTABLE_PREFETCH) {
1436                 rc = -dmu_object_next(dev->od_os, &it->mit_prefetched_dnode,
1437                                       B_FALSE, 0);
1438                 if (unlikely(rc != 0))
1439                         break;
1440
1441                 osd_dmu_prefetch(dev->od_os, it->mit_prefetched_dnode,
1442                                  0, 0, 0, ZIO_PRIORITY_ASYNC_READ);
1443
1444                 it->mit_prefetched++;
1445         }
1446 }
1447
1448 static int osd_zfs_otable_it_next(const struct lu_env *env, struct dt_it *di)
1449 {
1450         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1451         struct lustre_mdt_attrs *lma;
1452         struct osd_device       *dev = it->mit_dev;
1453         nvlist_t                *nvbuf = NULL;
1454         uchar_t                 *v;
1455         __u64                    dnode;
1456         int                      rc, s;
1457
1458         memset(&it->mit_fid, 0, sizeof(it->mit_fid));
1459
1460         dnode = it->mit_pos;
1461         do {
1462                 rc = -dmu_object_next(dev->od_os, &it->mit_pos, B_FALSE, 0);
1463                 if (unlikely(rc != 0))
1464                         GOTO(out, rc = 1);
1465                 it->mit_prefetched--;
1466
1467                 /* LMA is required for this to be a Lustre object.
1468                  * If there is no xattr skip it. */
1469                 rc = __osd_xattr_load(dev, it->mit_pos, &nvbuf);
1470                 if (unlikely(rc != 0))
1471                         continue;
1472
1473                 LASSERT(nvbuf != NULL);
1474                 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA, &v, &s);
1475                 if (likely(rc == 0)) {
1476                         /* Lustre object */
1477                         lma = (struct lustre_mdt_attrs *)v;
1478                         lustre_lma_swab(lma);
1479                         it->mit_fid = lma->lma_self_fid;
1480                         nvlist_free(nvbuf);
1481                         break;
1482                 } else {
1483                         /* not a Lustre object, try next one */
1484                         nvlist_free(nvbuf);
1485                 }
1486
1487         } while (1);
1488
1489
1490         /* we aren't prefetching in the above loop because the number of
1491          * non-Lustre objects is very small and we will be repeating very
1492          * rare. in case we want to use this to iterate over non-Lustre
1493          * objects (i.e. when we convert regular ZFS in Lustre) it makes
1494          * sense to initiate prefetching in the loop */
1495
1496         /* 0 - there are more items, +1 - the end */
1497         if (likely(rc == 0))
1498                 osd_zfs_otable_prefetch(env, it);
1499
1500         CDEBUG(D_OTHER, "advance: %llu -> %llu "DFID": %d\n", dnode,
1501                it->mit_pos, PFID(&it->mit_fid), rc);
1502
1503 out:
1504         return rc;
1505 }
1506
1507 static struct dt_key *osd_zfs_otable_it_key(const struct lu_env *env,
1508                                             const struct dt_it *di)
1509 {
1510         return NULL;
1511 }
1512
1513 static int osd_zfs_otable_it_key_size(const struct lu_env *env,
1514                                       const struct dt_it *di)
1515 {
1516         return sizeof(__u64);
1517 }
1518
1519 static int osd_zfs_otable_it_rec(const struct lu_env *env,
1520                                  const struct dt_it *di,
1521                                  struct dt_rec *rec, __u32 attr)
1522 {
1523         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1524         struct lu_fid *fid = (struct lu_fid *)rec;
1525         ENTRY;
1526
1527         *fid = it->mit_fid;
1528
1529         RETURN(0);
1530 }
1531
1532
1533 static __u64 osd_zfs_otable_it_store(const struct lu_env *env,
1534                                      const struct dt_it *di)
1535 {
1536         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1537
1538         return it->mit_pos;
1539 }
1540
1541 static int osd_zfs_otable_it_load(const struct lu_env *env,
1542                                   const struct dt_it *di, __u64 hash)
1543 {
1544         struct osd_metadnode_it *it  = (struct osd_metadnode_it *)di;
1545
1546         it->mit_pos = hash;
1547         it->mit_prefetched = 0;
1548         it->mit_prefetched_dnode = 0;
1549
1550         return osd_zfs_otable_it_next(env, (struct dt_it *)di);
1551 }
1552
1553 static int osd_zfs_otable_it_key_rec(const struct lu_env *env,
1554                                      const struct dt_it *di, void *key_rec)
1555 {
1556         return 0;
1557 }
1558
1559 const struct dt_index_operations osd_zfs_otable_ops = {
1560         .dio_it = {
1561                 .init     = osd_zfs_otable_it_init,
1562                 .fini     = osd_zfs_otable_it_fini,
1563                 .get      = osd_zfs_otable_it_get,
1564                 .put      = osd_zfs_otable_it_put,
1565                 .next     = osd_zfs_otable_it_next,
1566                 .key      = osd_zfs_otable_it_key,
1567                 .key_size = osd_zfs_otable_it_key_size,
1568                 .rec      = osd_zfs_otable_it_rec,
1569                 .store    = osd_zfs_otable_it_store,
1570                 .load     = osd_zfs_otable_it_load,
1571                 .key_rec  = osd_zfs_otable_it_key_rec,
1572         }
1573 };
1574
1575 int osd_index_try(const struct lu_env *env, struct dt_object *dt,
1576                 const struct dt_index_features *feat)
1577 {
1578         struct osd_object *obj = osd_dt_obj(dt);
1579         int rc = 0;
1580         ENTRY;
1581
1582         down_read(&obj->oo_guard);
1583
1584         /*
1585          * XXX: implement support for fixed-size keys sorted with natural
1586          *      numerical way (not using internal hash value)
1587          */
1588         if (feat->dif_flags & DT_IND_RANGE)
1589                 GOTO(out, rc = -ERANGE);
1590
1591         if (unlikely(feat == &dt_otable_features)) {
1592                 dt->do_index_ops = &osd_zfs_otable_ops;
1593                 GOTO(out, rc = 0);
1594         }
1595
1596         LASSERT(!dt_object_exists(dt) || obj->oo_dn != NULL);
1597         if (likely(feat == &dt_directory_features)) {
1598                 if (!dt_object_exists(dt) || osd_object_is_zap(obj->oo_dn))
1599                         dt->do_index_ops = &osd_dir_ops;
1600                 else
1601                         GOTO(out, rc = -ENOTDIR);
1602         } else if (unlikely(feat == &dt_acct_features)) {
1603                 LASSERT(fid_is_acct(lu_object_fid(&dt->do_lu)));
1604                 dt->do_index_ops = &osd_acct_index_ops;
1605         } else if (dt->do_index_ops == NULL) {
1606                 /* For index file, we don't support variable key & record sizes
1607                  * and the key has to be unique */
1608                 if ((feat->dif_flags & ~DT_IND_UPDATE) != 0)
1609                         GOTO(out, rc = -EINVAL);
1610
1611                 if (feat->dif_keysize_max > ZAP_MAXNAMELEN)
1612                         GOTO(out, rc = -E2BIG);
1613                 if (feat->dif_keysize_max != feat->dif_keysize_min)
1614                         GOTO(out, rc = -EINVAL);
1615
1616                 /* As for the record size, it should be a multiple of 8 bytes
1617                  * and smaller than the maximum value length supported by ZAP.
1618                  */
1619                 if (feat->dif_recsize_max > ZAP_MAXVALUELEN)
1620                         GOTO(out, rc = -E2BIG);
1621                 if (feat->dif_recsize_max != feat->dif_recsize_min)
1622                         GOTO(out, rc = -EINVAL);
1623
1624                 obj->oo_keysize = feat->dif_keysize_max;
1625                 obj->oo_recsize = feat->dif_recsize_max;
1626                 obj->oo_recusize = 1;
1627
1628                 /* ZFS prefers to work with array of 64bits */
1629                 if ((obj->oo_recsize & 7) == 0) {
1630                         obj->oo_recsize >>= 3;
1631                         obj->oo_recusize = 8;
1632                 }
1633                 dt->do_index_ops = &osd_index_ops;
1634         }
1635
1636 out:
1637         up_read(&obj->oo_guard);
1638
1639         RETURN(rc);
1640 }