+ if (child == NULL) {
+ lu_object_put(env, luch);
+ CERROR("%s: Unable to get osd_object "DFID"\n",
+ osd_dev(ludev)->od_svname, PFID(fid));
+ child = ERR_PTR(-ENOENT);
+ }
+ } else {
+ LU_OBJECT_DEBUG(D_ERROR, env, luch,
+ "%s: lu_object does not exists "DFID"\n",
+ osd_dev(ludev)->od_svname, PFID(fid));
+ lu_object_put(env, luch);
+ child = ERR_PTR(-ENOENT);
+ }
+
+ return child;
+}
+
+/**
+ * Put the osd object once done with it.
+ *
+ * \param obj osd object that needs to be put
+ */
+static inline void osd_object_put(const struct lu_env *env,
+ struct osd_object *obj)
+{
+ lu_object_put(env, &obj->oo_dt.do_lu);
+}
+
+static int osd_seq_exists(const struct lu_env *env, struct osd_device *osd,
+ u64 seq)
+{
+ struct lu_seq_range *range = &osd_oti_get(env)->oti_seq_range;
+ struct seq_server_site *ss = osd_seq_site(osd);
+ int rc;
+ ENTRY;
+
+ LASSERT(ss != NULL);
+ LASSERT(ss->ss_server_fld != NULL);
+
+ rc = osd_fld_lookup(env, osd, seq, range);
+ if (rc != 0) {
+ if (rc != -ENOENT)
+ CERROR("%s: Can not lookup fld for "LPX64"\n",
+ osd_name(osd), seq);
+ RETURN(0);
+ }
+
+ RETURN(ss->ss_node_id == range->lsr_index);
+}
+
+static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
+ const struct lu_fid *fid)
+{
+ struct seq_server_site *ss = osd_seq_site(osd);
+ ENTRY;
+
+ /* FID seqs not in FLDB, must be local seq */
+ if (unlikely(!fid_seq_in_fldb(fid_seq(fid))))
+ RETURN(0);
+
+ /* If FLD is not being initialized yet, it only happens during the
+ * initialization, likely during mgs initialization, and we assume
+ * this is local FID. */
+ if (ss == NULL || ss->ss_server_fld == NULL)
+ RETURN(0);
+
+ /* Only check the local FLDB here */
+ if (osd_seq_exists(env, osd, fid_seq(fid)))
+ RETURN(0);
+
+ RETURN(1);
+}
+
+/**
+ * Inserts (key, value) pair in \a directory object.
+ *
+ * \param dt osd index object
+ * \param key key for index
+ * \param rec record reference
+ * \param th transaction handler
+ * \param ignore_quota update should not affect quota
+ *
+ * \retval 0 success
+ * \retval -ve failure
+ */
+static int osd_dir_insert(const struct lu_env *env, struct dt_object *dt,
+ const struct dt_rec *rec, const struct dt_key *key,
+ struct thandle *th, int ignore_quota)
+{
+ struct osd_thread_info *oti = osd_oti_get(env);
+ struct osd_object *parent = osd_dt_obj(dt);
+ struct osd_device *osd = osd_obj2dev(parent);
+ struct dt_insert_rec *rec1 = (struct dt_insert_rec *)rec;
+ const struct lu_fid *fid = rec1->rec_fid;
+ struct osd_thandle *oh;
+ struct osd_object *child = NULL;
+ __u32 attr;
+ char *name = (char *)key;
+ int rc;
+ ENTRY;
+
+ LASSERT(parent->oo_db);
+ LASSERT(osd_object_is_zap(parent->oo_db));
+
+ LASSERT(dt_object_exists(dt));
+ LASSERT(osd_invariant(parent));
+
+ LASSERT(th != NULL);
+ oh = container_of0(th, struct osd_thandle, ot_super);
+
+ rc = osd_remote_fid(env, osd, fid);
+ if (rc < 0) {
+ CERROR("%s: Can not find object "DFID": rc = %d\n",
+ osd->od_svname, PFID(fid), rc);
+ RETURN(rc);
+ }
+
+ if (unlikely(rc == 1)) {
+ /* Insert remote entry */
+ memset(&oti->oti_zde.lzd_reg, 0, sizeof(oti->oti_zde.lzd_reg));
+ oti->oti_zde.lzd_reg.zde_type = IFTODT(rec1->rec_type & S_IFMT);
+ } else {
+ /*
+ * To simulate old Orion setups with ./.. stored in the
+ * directories
+ */
+ /* Insert local entry */
+ child = osd_object_find(env, dt, fid);
+ if (IS_ERR(child))
+ RETURN(PTR_ERR(child));
+
+ LASSERT(child->oo_db);
+ if (name[0] == '.') {
+ if (name[1] == 0) {
+ /* do not store ".", instead generate it
+ * during iteration */
+ GOTO(out, rc = 0);
+ } else if (name[1] == '.' && name[2] == 0) {
+ if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
+ struct lu_fid tfid = *fid;
+
+ osd_object_put(env, child);
+ tfid.f_oid--;
+ child = osd_object_find(env, dt, &tfid);
+ if (IS_ERR(child))
+ RETURN(PTR_ERR(child));
+
+ LASSERT(child->oo_db);
+ }
+
+ /* update parent dnode in the child.
+ * later it will be used to generate ".." */
+ rc = osd_object_sa_update(parent,
+ SA_ZPL_PARENT(osd),
+ &child->oo_db->db_object,
+ 8, oh);
+
+ GOTO(out, rc);
+ }
+ }
+ CLASSERT(sizeof(oti->oti_zde.lzd_reg) == 8);
+ CLASSERT(sizeof(oti->oti_zde) % 8 == 0);
+ attr = child->oo_dt.do_lu.lo_header ->loh_attr;
+ oti->oti_zde.lzd_reg.zde_type = IFTODT(attr & S_IFMT);
+ oti->oti_zde.lzd_reg.zde_dnode = child->oo_db->db_object;
+ }
+
+ oti->oti_zde.lzd_fid = *fid;
+ /* Insert (key,oid) into ZAP */
+ rc = -zap_add(osd->od_os, parent->oo_db->db_object,
+ (char *)key, 8, sizeof(oti->oti_zde) / 8,
+ (void *)&oti->oti_zde, oh->ot_tx);
+ if (unlikely(rc == -EEXIST &&
+ name[0] == '.' && name[1] == '.' && name[2] == 0))
+ /* Update (key,oid) in ZAP */
+ rc = -zap_update(osd->od_os, parent->oo_db->db_object,
+ (char *)key, 8, sizeof(oti->oti_zde) / 8,
+ (void *)&oti->oti_zde, oh->ot_tx);
+
+out:
+ if (child != NULL)
+ osd_object_put(env, child);
+
+ RETURN(rc);
+}
+
+static int osd_declare_dir_delete(const struct lu_env *env,
+ struct dt_object *dt,
+ const struct dt_key *key,
+ struct thandle *th)
+{
+ struct osd_object *obj = osd_dt_obj(dt);
+ struct osd_thandle *oh;
+ uint64_t dnode;
+ ENTRY;
+
+ LASSERT(dt_object_exists(dt));
+ LASSERT(osd_invariant(obj));
+
+ LASSERT(th != NULL);
+ oh = container_of0(th, struct osd_thandle, ot_super);
+
+ if (dt_object_exists(dt)) {
+ LASSERT(obj->oo_db);
+ LASSERT(osd_object_is_zap(obj->oo_db));
+ dnode = obj->oo_db->db_object;
+ } else {
+ dnode = DMU_NEW_OBJECT;
+ }
+ dmu_tx_hold_zap(oh->ot_tx, dnode, TRUE, (char *)key);
+
+ RETURN(0);
+}
+
+static int osd_dir_delete(const struct lu_env *env, struct dt_object *dt,
+ const struct dt_key *key, struct thandle *th)
+{
+ struct osd_object *obj = osd_dt_obj(dt);
+ struct osd_device *osd = osd_obj2dev(obj);
+ struct osd_thandle *oh;
+ dmu_buf_t *zap_db = obj->oo_db;
+ char *name = (char *)key;
+ int rc;
+ ENTRY;
+
+ LASSERT(zap_db);
+ LASSERT(osd_object_is_zap(zap_db));
+
+ LASSERT(th != NULL);
+ oh = container_of0(th, struct osd_thandle, ot_super);
+
+ /*
+ * In Orion . and .. were stored in the directory (not generated upon
+ * request as now). we preserve them for backward compatibility
+ */
+ if (name[0] == '.') {
+ if (name[1] == 0) {
+ RETURN(0);
+ } else if (name[1] == '.' && name[2] == 0) {
+ RETURN(0);
+ }
+ }
+
+ /* Remove key from the ZAP */
+ rc = -zap_remove(osd->od_os, zap_db->db_object,
+ (char *) key, oh->ot_tx);
+
+ if (unlikely(rc && rc != -ENOENT))
+ CERROR("%s: zap_remove failed: rc = %d\n", osd->od_svname, rc);
+
+ RETURN(rc);
+}
+
+static struct dt_it *osd_dir_it_init(const struct lu_env *env,
+ struct dt_object *dt,
+ __u32 unused)
+{
+ struct osd_zap_it *it;
+
+ it = (struct osd_zap_it *)osd_index_it_init(env, dt, unused);
+ if (!IS_ERR(it))
+ it->ozi_pos = 0;
+
+ RETURN((struct dt_it *)it);
+}
+
+/**
+ * Move Iterator to record specified by \a key
+ *
+ * \param di osd iterator
+ * \param key key for index
+ *
+ * \retval +ve di points to record with least key not larger than key
+ * \retval 0 di points to exact matched key
+ * \retval -ve failure
+ */
+static int osd_dir_it_get(const struct lu_env *env,
+ struct dt_it *di, const struct dt_key *key)
+{
+ struct osd_zap_it *it = (struct osd_zap_it *)di;
+ struct osd_object *obj = it->ozi_obj;
+ char *name = (char *)key;
+ int rc;
+ ENTRY;
+
+ LASSERT(it);
+ LASSERT(it->ozi_zc);
+
+ /* reset the cursor */
+ zap_cursor_fini(it->ozi_zc);
+ osd_obj_cursor_init_serialized(it->ozi_zc, obj, 0);
+
+ /* XXX: implementation of the API is broken at the moment */
+ LASSERT(((const char *)key)[0] == 0);
+
+ if (name[0] == 0) {
+ it->ozi_pos = 0;
+ RETURN(1);
+ }
+
+ if (name[0] == '.') {
+ if (name[1] == 0) {
+ it->ozi_pos = 1;
+ GOTO(out, rc = 1);
+ } else if (name[1] == '.' && name[2] == 0) {
+ it->ozi_pos = 2;
+ GOTO(out, rc = 1);
+ }
+ }
+
+ /* neither . nor .. - some real record */
+ it->ozi_pos = 3;
+ rc = +1;
+
+out:
+ RETURN(rc);
+}
+
+static void osd_dir_it_put(const struct lu_env *env, struct dt_it *di)
+{
+ /* PBS: do nothing : ref are incremented at retrive and decreamented
+ * next/finish. */
+}
+
+/*
+ * in Orion . and .. were stored in the directory, while ZPL
+ * and current osd-zfs generate them up on request. so, we
+ * need to ignore previously stored . and ..
+ */
+static int osd_index_retrieve_skip_dots(struct osd_zap_it *it,
+ zap_attribute_t *za)
+{
+ int rc, isdot;
+
+ do {
+ rc = -zap_cursor_retrieve(it->ozi_zc, za);
+
+ isdot = 0;
+ if (unlikely(rc == 0 && za->za_name[0] == '.')) {
+ if (za->za_name[1] == 0) {
+ isdot = 1;
+ } else if (za->za_name[1] == '.' &&
+ za->za_name[2] == 0) {
+ isdot = 1;
+ }
+ if (unlikely(isdot))
+ zap_cursor_advance(it->ozi_zc);
+ }
+ } while (unlikely(rc == 0 && isdot));
+
+ return rc;
+}
+
+/**
+ * to load a directory entry at a time and stored it in
+ * iterator's in-memory data structure.
+ *
+ * \param di, struct osd_it_ea, iterator's in memory structure
+ *
+ * \retval +ve, iterator reached to end
+ * \retval 0, iterator not reached to end
+ * \retval -ve, on error
+ */
+static int osd_dir_it_next(const struct lu_env *env, struct dt_it *di)
+{
+ struct osd_zap_it *it = (struct osd_zap_it *)di;
+ zap_attribute_t *za = &osd_oti_get(env)->oti_za;
+ int rc;
+
+ ENTRY;
+
+ /* temp. storage should be enough for any key supported by ZFS */
+ CLASSERT(sizeof(za->za_name) <= sizeof(it->ozi_name));
+
+ /*
+ * the first ->next() moves the cursor to .
+ * the second ->next() moves the cursor to ..
+ * then we get to the real records and have to verify any exist
+ */
+ if (it->ozi_pos <= 2) {
+ it->ozi_pos++;
+ if (it->ozi_pos <=2)
+ RETURN(0);
+
+ } else {
+ zap_cursor_advance(it->ozi_zc);
+ }
+
+ /*
+ * According to current API we need to return error if its last entry.
+ * zap_cursor_advance() does not return any value. So we need to call
+ * retrieve to check if there is any record. We should make
+ * changes to Iterator API to not return status for this API
+ */
+ rc = osd_index_retrieve_skip_dots(it, za);
+
+ if (rc == -ENOENT) /* end of dir */
+ RETURN(+1);
+
+ RETURN(rc);
+}
+
+static struct dt_key *osd_dir_it_key(const struct lu_env *env,
+ const struct dt_it *di)
+{
+ struct osd_zap_it *it = (struct osd_zap_it *)di;
+ zap_attribute_t *za = &osd_oti_get(env)->oti_za;
+ int rc = 0;
+ ENTRY;
+
+ if (it->ozi_pos <= 1) {
+ it->ozi_pos = 1;
+ RETURN((struct dt_key *)".");
+ } else if (it->ozi_pos == 2) {
+ RETURN((struct dt_key *)"..");