Whamcloud - gitweb
LU-10850 fld: handle empty ranges in fld_index_init()
[fs/lustre-release.git] / lustre / fld / fld_index.c
index 4226438..7399206 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -27,7 +23,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2014, Intel Corporation.
+ * Copyright (c) 2011, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -251,7 +247,7 @@ int fld_index_lookup(const struct lu_env *env, struct lu_server_fld *fld,
                        rc = -ENOENT;
         }
 
-        CDEBUG(D_INFO, "%s: lookup seq = "LPX64" range : "DRANGE" rc = %d\n",
+       CDEBUG(D_INFO, "%s: lookup seq = %#llx range : "DRANGE" rc = %d\n",
                fld->lsf_name, seq, PRANGE(range), rc);
 
         RETURN(rc);
@@ -275,12 +271,16 @@ int fld_insert_entry(const struct lu_env *env,
                     const struct lu_seq_range *range)
 {
        struct thandle *th;
+       struct dt_device *dt = lu2dt_dev(fld->lsf_obj->do_lu.lo_dev);
        int rc;
        ENTRY;
 
        LASSERT(mutex_is_locked(&fld->lsf_lock));
 
-       th = dt_trans_create(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev));
+       if (dt->dd_rdonly)
+               RETURN(0);
+
+       th = dt_trans_create(env, dt);
        if (IS_ERR(th))
                RETURN(PTR_ERR(th));
 
@@ -291,8 +291,7 @@ int fld_insert_entry(const struct lu_env *env,
                GOTO(out, rc);
        }
 
-       rc = dt_trans_start_local(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev),
-                                 th);
+       rc = dt_trans_start_local(env, dt, th);
        if (rc)
                GOTO(out, rc);
 
@@ -300,7 +299,7 @@ int fld_insert_entry(const struct lu_env *env,
        if (rc == -EEXIST)
                rc = 0;
 out:
-       dt_trans_stop(env, lu2dt_dev(fld->lsf_obj->do_lu.lo_dev), th);
+       dt_trans_stop(env, dt, th);
        RETURN(rc);
 }
 EXPORT_SYMBOL(fld_insert_entry);
@@ -336,6 +335,7 @@ int fld_index_init(const struct lu_env *env, struct lu_server_fld *fld,
        const struct dt_it_ops  *iops;
        int                     rc;
        __u32                   index;
+       int range_count = 0;
        ENTRY;
 
        info = lu_context_key_get(&env->le_ctx, &fld_thread_key);
@@ -361,7 +361,7 @@ int fld_index_init(const struct lu_env *env, struct lu_server_fld *fld,
 
        LASSERT(dt_obj != NULL);
        if (!dt_object_exists(dt_obj)) {
-               lu_object_put(env, &dt_obj->do_lu);
+               dt_object_put(env, dt_obj);
                dt_obj = dt_find_or_create(env, dt, &fid, &dof, attr);
                fld->lsf_new = 1;
                if (IS_ERR(dt_obj)) {
@@ -389,26 +389,39 @@ int fld_index_init(const struct lu_env *env, struct lu_server_fld *fld,
                GOTO(out, rc = PTR_ERR(it));
 
        rc = iops->load(env, it, 0);
+       if (rc > 0)
+               rc = 0;
+       else if (rc == 0)
+               rc = iops->next(env, it);
+
        if (rc < 0)
                GOTO(out_it_fini, rc);
 
-       if (rc > 0) {
-               /* Load FLD entry into server cache */
-               do {
-                       rc = iops->rec(env, it, (struct dt_rec *)range, 0);
-                       if (rc != 0)
-                               GOTO(out_it_put, rc);
-                       LASSERT(range != NULL);
-                       range_be_to_cpu(range, range);
+       while (rc == 0) {
+               rc = iops->rec(env, it, (struct dt_rec *)range, 0);
+               if (rc != 0)
+                       GOTO(out_it_put, rc);
+
+               range_be_to_cpu(range, range);
+
+               /* Newly created ldiskfs IAM indexes may include a
+                * zeroed-out key and record. Ignore it here. */
+               if (range->lsr_start < range->lsr_end) {
                        rc = fld_cache_insert(fld->lsf_cache, range);
                        if (rc != 0)
                                GOTO(out_it_put, rc);
-                       rc = iops->next(env, it);
-               } while (rc == 0);
-       } else {
-               fld->lsf_new = 1;
+
+                       range_count++;
+               }
+
+               rc = iops->next(env, it);
+               if (rc < 0)
+                       GOTO(out_it_fini, rc);
        }
 
+       if (range_count == 0)
+               fld->lsf_new = 1;
+
        rc = fld_name_to_index(fld->lsf_name, &index);
        if (rc < 0)
                GOTO(out_it_put, rc);
@@ -437,7 +450,7 @@ out:
 
        if (rc < 0) {
                if (dt_obj != NULL)
-                       lu_object_put(env, &dt_obj->do_lu);
+                       dt_object_put(env, dt_obj);
                fld->lsf_obj = NULL;
        }
        RETURN(rc);
@@ -448,7 +461,7 @@ void fld_index_fini(const struct lu_env *env, struct lu_server_fld *fld)
        ENTRY;
        if (fld->lsf_obj != NULL) {
                if (!IS_ERR(fld->lsf_obj))
-                       lu_object_put(env, &fld->lsf_obj->do_lu);
+                       dt_object_put(env, fld->lsf_obj);
                fld->lsf_obj = NULL;
        }
        EXIT;