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