Whamcloud - gitweb
LU-7311 osd: smp_mb__before_clear_bit deprecated since kernel 3.16
[fs/lustre-release.git] / lustre / ptlrpc / nodemap_storage.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) 2015, Trustees of Indiana University
24  *
25  * Copyright (c) 2014, Intel Corporation.
26  *
27  * Author: Joshua Walgenbach <jjw@iu.edu>
28  * Author: Kit Westneat <cwestnea@iu.edu>
29  *
30  * Implements the storage functionality for the nodemap configuration. Functions
31  * in this file prepare, store, and load nodemap configuration data. Targets
32  * using nodemap services should register a configuration file object. Nodemap
33  * configuration changes that need to persist should call the appropriate
34  * storage function for the data being modified.
35  *
36  * There are several index types as defined in enum nodemap_idx_type:
37  *      NODEMAP_CLUSTER_IDX     stores the data found on the lu_nodemap struct,
38  *                              like root squash and config flags, as well as
39  *                              the name.
40  *      NODEMAP_RANGE_IDX       stores NID range information for a nodemap
41  *      NODEMAP_UIDMAP_IDX      stores a fs/client UID mapping pair
42  *      NODEMAP_GIDMAP_IDX      stores a fs/client GID mapping pair
43  *      NODEMAP_GLOBAL_IDX      stores whether or not nodemaps are active
44  */
45
46 #include <libcfs/libcfs.h>
47 #include <linux/err.h>
48 #include <linux/kernel.h>
49 #include <linux/list.h>
50 #include <linux/mutex.h>
51 #include <linux/string.h>
52 #include <linux/types.h>
53 #include <lnet/types.h>
54 #include <lustre/lustre_idl.h>
55 #include <dt_object.h>
56 #include <lu_object.h>
57 #include <lustre_net.h>
58 #include <lustre_nodemap.h>
59 #include <obd_class.h>
60 #include <obd_support.h>
61 #include "nodemap_internal.h"
62
63 /* list of registered nodemap index files, except MGS */
64 static LIST_HEAD(ncf_list_head);
65 static DEFINE_MUTEX(ncf_list_lock);
66
67 /* MGS index is different than others, others are listeners to MGS idx */
68 static struct nm_config_file *nodemap_mgs_ncf;
69
70 /* lu_nodemap flags */
71 enum nm_flag_shifts {
72         NM_FL_ALLOW_ROOT_ACCESS = 0x1,
73         NM_FL_TRUST_CLIENT_IDS = 0x2,
74         NM_FL_DENY_UNKNOWN = 0x4,
75 };
76
77 static void nodemap_cluster_key_init(struct nodemap_key *nk, unsigned int nm_id)
78 {
79         nk->nk_nodemap_id = cpu_to_le32(nm_idx_set_type(nm_id,
80                                                         NODEMAP_CLUSTER_IDX));
81         nk->nk_unused = 0;
82 }
83
84 static void nodemap_cluster_rec_init(union nodemap_rec *nr,
85                                      const struct lu_nodemap *nodemap)
86 {
87         CLASSERT(sizeof(nr->ncr.ncr_name) == sizeof(nodemap->nm_name));
88
89         strncpy(nr->ncr.ncr_name, nodemap->nm_name, sizeof(nodemap->nm_name));
90         nr->ncr.ncr_squash_uid = cpu_to_le32(nodemap->nm_squash_uid);
91         nr->ncr.ncr_squash_gid = cpu_to_le32(nodemap->nm_squash_gid);
92         nr->ncr.ncr_flags = cpu_to_le32(
93                 (nodemap->nmf_trust_client_ids ?
94                         NM_FL_TRUST_CLIENT_IDS : 0) |
95                 (nodemap->nmf_allow_root_access ?
96                         NM_FL_ALLOW_ROOT_ACCESS : 0) |
97                 (nodemap->nmf_deny_unknown ?
98                         NM_FL_DENY_UNKNOWN : 0));
99 }
100
101 static void nodemap_idmap_key_init(struct nodemap_key *nk, unsigned int nm_id,
102                                    enum nodemap_id_type id_type,
103                                    u32 id_client)
104 {
105         enum nodemap_idx_type idx_type;
106
107         if (id_type == NODEMAP_UID)
108                 idx_type = NODEMAP_UIDMAP_IDX;
109         else
110                 idx_type = NODEMAP_GIDMAP_IDX;
111
112         nk->nk_nodemap_id = cpu_to_le32(nm_idx_set_type(nm_id, idx_type));
113         nk->nk_id_client = cpu_to_le32(id_client);
114 }
115
116 static void nodemap_idmap_rec_init(union nodemap_rec *nr, u32 id_fs)
117 {
118         nr->nir.nir_id_fs = cpu_to_le32(id_fs);
119 }
120
121 static void nodemap_range_key_init(struct nodemap_key *nk, unsigned int nm_id,
122                                    unsigned int rn_id)
123 {
124         nk->nk_nodemap_id = cpu_to_le32(nm_idx_set_type(nm_id,
125                                                         NODEMAP_RANGE_IDX));
126         nk->nk_range_id = cpu_to_le32(rn_id);
127 }
128
129 static void nodemap_range_rec_init(union nodemap_rec *nr,
130                                    const lnet_nid_t nid[2])
131 {
132         nr->nrr.nrr_start_nid = cpu_to_le64(nid[0]);
133         nr->nrr.nrr_end_nid = cpu_to_le64(nid[1]);
134 }
135
136 static void nodemap_global_key_init(struct nodemap_key *nk)
137 {
138         nk->nk_nodemap_id = cpu_to_le32(nm_idx_set_type(0, NODEMAP_GLOBAL_IDX));
139         nk->nk_unused = 0;
140 }
141
142 static void nodemap_global_rec_init(union nodemap_rec *nr, bool active)
143 {
144         nr->ngr.ngr_is_active = active;
145 }
146
147 /* should be called with dt_write lock */
148 static void nodemap_inc_version(const struct lu_env *env,
149                                 struct dt_object *nodemap_idx,
150                                 struct thandle *th)
151 {
152         u64 ver = dt_version_get(env, nodemap_idx);
153         dt_version_set(env, nodemap_idx, ver + 1, th);
154 }
155
156 static int nodemap_idx_insert(const struct lu_env *env,
157                               struct dt_object *idx,
158                               const struct nodemap_key *nk,
159                               const union nodemap_rec *nr)
160 {
161         struct thandle          *th;
162         struct dt_device        *dev = lu2dt_dev(idx->do_lu.lo_dev);
163         int                      rc;
164
165         CLASSERT(sizeof(union nodemap_rec) == 32);
166
167         th = dt_trans_create(env, dev);
168
169         if (IS_ERR(th))
170                 GOTO(out, rc = PTR_ERR(th));
171
172         rc = dt_declare_insert(env, idx,
173                                (const struct dt_rec *)nr,
174                                (const struct dt_key *)nk, th);
175         if (rc != 0)
176                 GOTO(out, rc);
177
178         rc = dt_declare_version_set(env, idx, th);
179         if (rc != 0)
180                 GOTO(out, rc);
181
182         rc = dt_trans_start_local(env, dev, th);
183         if (rc != 0)
184                 GOTO(out, rc);
185
186         dt_write_lock(env, idx, 0);
187
188         rc = dt_insert(env, idx, (const struct dt_rec *)nr,
189                        (const struct dt_key *)nk, th, 1);
190
191         nodemap_inc_version(env, idx, th);
192         dt_write_unlock(env, idx);
193 out:
194         dt_trans_stop(env, dev, th);
195
196         return rc;
197 }
198
199 static int nodemap_idx_update(const struct lu_env *env,
200                               struct dt_object *idx,
201                               const struct nodemap_key *nk,
202                               const union nodemap_rec *nr)
203 {
204         struct thandle          *th;
205         struct dt_device        *dev = lu2dt_dev(idx->do_lu.lo_dev);
206         int                      rc = 0;
207
208         th = dt_trans_create(env, dev);
209
210         if (IS_ERR(th))
211                 GOTO(out, rc = PTR_ERR(th));
212
213         rc = dt_declare_delete(env, idx, (const struct dt_key *)nk, th);
214         if (rc != 0)
215                 GOTO(out, rc);
216
217         rc = dt_declare_insert(env, idx, (const struct dt_rec *)nr,
218                                (const struct dt_key *)nk, th);
219         if (rc != 0)
220                 GOTO(out, rc);
221
222         rc = dt_declare_version_set(env, idx, th);
223         if (rc != 0)
224                 GOTO(out, rc);
225
226         rc = dt_trans_start_local(env, dev, th);
227         if (rc != 0)
228                 GOTO(out, rc);
229
230         dt_write_lock(env, idx, 0);
231
232         rc = dt_delete(env, idx, (const struct dt_key *)nk, th);
233         if (rc != 0)
234                 GOTO(out_lock, rc);
235
236         rc = dt_insert(env, idx, (const struct dt_rec *)nr,
237                        (const struct dt_key *)nk, th, 1);
238         if (rc != 0)
239                 GOTO(out_lock, rc);
240
241         nodemap_inc_version(env, idx, th);
242 out_lock:
243         dt_write_unlock(env, idx);
244 out:
245         dt_trans_stop(env, dev, th);
246
247         return rc;
248 }
249
250 static int nodemap_idx_delete(const struct lu_env *env,
251                               struct dt_object *idx,
252                               const struct nodemap_key *nk,
253                               const union nodemap_rec *unused)
254 {
255         struct thandle          *th;
256         struct dt_device        *dev = lu2dt_dev(idx->do_lu.lo_dev);
257         int                      rc = 0;
258
259         th = dt_trans_create(env, dev);
260
261         if (IS_ERR(th))
262                 GOTO(out, rc = PTR_ERR(th));
263
264         rc = dt_declare_delete(env, idx, (const struct dt_key *)nk, th);
265         if (rc != 0)
266                 GOTO(out, rc);
267
268         rc = dt_declare_version_set(env, idx, th);
269         if (rc != 0)
270                 GOTO(out, rc);
271
272         rc = dt_trans_start_local(env, dev, th);
273         if (rc != 0)
274                 GOTO(out, rc);
275
276         dt_write_lock(env, idx, 0);
277
278         rc = dt_delete(env, idx, (const struct dt_key *)nk, th);
279
280         nodemap_inc_version(env, idx, th);
281
282         dt_write_unlock(env, idx);
283 out:
284         dt_trans_stop(env, dev, th);
285
286         return rc;
287 }
288
289 enum nm_add_update {
290         NM_ADD = 0,
291         NM_UPDATE = 1,
292 };
293
294 static int nodemap_idx_nodemap_add_update(const struct lu_nodemap *nodemap,
295                                           enum nm_add_update update)
296 {
297         struct nodemap_key nk;
298         union nodemap_rec nr;
299         struct lu_env env;
300         int rc = 0;
301
302         ENTRY;
303
304         if (nodemap_mgs_ncf == NULL) {
305                 CERROR("cannot add nodemap config to non-existing MGS.\n");
306                 return -EINVAL;
307         }
308
309         rc = lu_env_init(&env, LCT_LOCAL);
310         if (rc)
311                 RETURN(rc);
312
313         nodemap_cluster_key_init(&nk, nodemap->nm_id);
314         nodemap_cluster_rec_init(&nr, nodemap);
315
316         if (update == NM_UPDATE)
317                 rc = nodemap_idx_update(&env, nodemap_mgs_ncf->ncf_obj,
318                                         &nk, &nr);
319         else
320                 rc = nodemap_idx_insert(&env, nodemap_mgs_ncf->ncf_obj,
321                                         &nk, &nr);
322
323         lu_env_fini(&env);
324
325         RETURN(rc);
326 }
327
328 int nodemap_idx_nodemap_add(const struct lu_nodemap *nodemap)
329 {
330         return nodemap_idx_nodemap_add_update(nodemap, NM_ADD);
331 }
332
333 int nodemap_idx_nodemap_update(const struct lu_nodemap *nodemap)
334 {
335         return nodemap_idx_nodemap_add_update(nodemap, NM_UPDATE);
336 }
337
338 int nodemap_idx_nodemap_del(const struct lu_nodemap *nodemap)
339 {
340         struct rb_root           root;
341         struct lu_idmap         *idmap;
342         struct lu_idmap         *temp;
343         struct lu_nid_range     *range;
344         struct lu_nid_range     *range_temp;
345         struct nodemap_key       nk;
346         struct lu_env            env;
347         int                      rc = 0;
348         int                      rc2 = 0;
349
350         ENTRY;
351
352         if (nodemap_mgs_ncf == NULL) {
353                 CERROR("cannot add nodemap config to non-existing MGS.\n");
354                 return -EINVAL;
355         }
356
357         rc = lu_env_init(&env, LCT_LOCAL);
358         if (rc != 0)
359                 RETURN(rc);
360
361         root = nodemap->nm_fs_to_client_uidmap;
362         nm_rbtree_postorder_for_each_entry_safe(idmap, temp, &root,
363                                                 id_fs_to_client) {
364                 nodemap_idmap_key_init(&nk, nodemap->nm_id, NODEMAP_UID,
365                                        idmap->id_client);
366                 rc2 = nodemap_idx_delete(&env, nodemap_mgs_ncf->ncf_obj,
367                                          &nk, NULL);
368                 if (rc2 < 0)
369                         rc = rc2;
370         }
371
372         root = nodemap->nm_client_to_fs_gidmap;
373         nm_rbtree_postorder_for_each_entry_safe(idmap, temp, &root,
374                                                 id_client_to_fs) {
375                 nodemap_idmap_key_init(&nk, nodemap->nm_id, NODEMAP_GID,
376                                        idmap->id_client);
377                 rc2 = nodemap_idx_delete(&env, nodemap_mgs_ncf->ncf_obj,
378                                          &nk, NULL);
379                 if (rc2 < 0)
380                         rc = rc2;
381         }
382
383         list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
384                                  rn_list) {
385                 nodemap_range_key_init(&nk, nodemap->nm_id, range->rn_id);
386                 rc2 = nodemap_idx_delete(&env, nodemap_mgs_ncf->ncf_obj,
387                                          &nk, NULL);
388                 if (rc2 < 0)
389                         rc = rc2;
390         }
391
392         nodemap_cluster_key_init(&nk, nodemap->nm_id);
393         rc2 = nodemap_idx_delete(&env, nodemap_mgs_ncf->ncf_obj, &nk, NULL);
394         if (rc2 < 0)
395                 rc = rc2;
396
397         lu_env_fini(&env);
398
399         RETURN(rc);
400 }
401
402 int nodemap_idx_range_add(const struct lu_nid_range *range,
403                           const lnet_nid_t nid[2])
404 {
405         struct nodemap_key       nk;
406         union nodemap_rec        nr;
407         struct lu_env            env;
408         int                      rc = 0;
409         ENTRY;
410
411         if (nodemap_mgs_ncf == NULL) {
412                 CERROR("cannot add nodemap config to non-existing MGS.\n");
413                 return -EINVAL;
414         }
415
416         rc = lu_env_init(&env, LCT_LOCAL);
417         if (rc != 0)
418                 RETURN(rc);
419
420         nodemap_range_key_init(&nk, range->rn_nodemap->nm_id, range->rn_id);
421         nodemap_range_rec_init(&nr, nid);
422
423         rc = nodemap_idx_insert(&env, nodemap_mgs_ncf->ncf_obj, &nk, &nr);
424         lu_env_fini(&env);
425
426         RETURN(rc);
427 }
428
429 int nodemap_idx_range_del(const struct lu_nid_range *range)
430 {
431         struct nodemap_key       nk;
432         struct lu_env            env;
433         int                      rc = 0;
434         ENTRY;
435
436         if (nodemap_mgs_ncf == NULL) {
437                 CERROR("cannot add nodemap config to non-existing MGS.\n");
438                 return -EINVAL;
439         }
440
441         rc = lu_env_init(&env, LCT_LOCAL);
442         if (rc != 0)
443                 RETURN(rc);
444
445         nodemap_range_key_init(&nk, range->rn_nodemap->nm_id, range->rn_id);
446
447         rc = nodemap_idx_delete(&env, nodemap_mgs_ncf->ncf_obj, &nk, NULL);
448         lu_env_fini(&env);
449
450         RETURN(rc);
451 }
452
453 int nodemap_idx_idmap_add(const struct lu_nodemap *nodemap,
454                           enum nodemap_id_type id_type,
455                           const u32 map[2])
456 {
457         struct nodemap_key       nk;
458         union nodemap_rec        nr;
459         struct lu_env            env;
460         int                      rc = 0;
461         ENTRY;
462
463         if (nodemap_mgs_ncf == NULL) {
464                 CERROR("cannot add nodemap config to non-existing MGS.\n");
465                 return -EINVAL;
466         }
467
468         rc = lu_env_init(&env, LCT_LOCAL);
469         if (rc != 0)
470                 RETURN(rc);
471
472         nodemap_idmap_key_init(&nk, nodemap->nm_id, id_type, map[0]);
473         nodemap_idmap_rec_init(&nr, map[1]);
474
475         rc = nodemap_idx_insert(&env, nodemap_mgs_ncf->ncf_obj, &nk, &nr);
476         lu_env_fini(&env);
477
478         RETURN(rc);
479 }
480
481 int nodemap_idx_idmap_del(const struct lu_nodemap *nodemap,
482                           enum nodemap_id_type id_type,
483                           const u32 map[2])
484 {
485         struct nodemap_key       nk;
486         struct lu_env            env;
487         int                      rc = 0;
488         ENTRY;
489
490         if (nodemap_mgs_ncf == NULL) {
491                 CERROR("cannot add nodemap config to non-existing MGS.\n");
492                 return -EINVAL;
493         }
494
495         rc = lu_env_init(&env, LCT_LOCAL);
496         if (rc != 0)
497                 RETURN(rc);
498
499         nodemap_idmap_key_init(&nk, nodemap->nm_id, id_type, map[0]);
500
501         rc = nodemap_idx_delete(&env, nodemap_mgs_ncf->ncf_obj, &nk, NULL);
502         lu_env_fini(&env);
503
504         RETURN(rc);
505 }
506
507 static int nodemap_idx_global_add_update(bool value, enum nm_add_update update)
508 {
509         struct nodemap_key       nk;
510         union nodemap_rec        nr;
511         struct lu_env            env;
512         int                      rc = 0;
513         ENTRY;
514
515         if (nodemap_mgs_ncf == NULL) {
516                 CERROR("cannot add nodemap config to non-existing MGS.\n");
517                 return -EINVAL;
518         }
519
520         rc = lu_env_init(&env, LCT_LOCAL);
521         if (rc != 0)
522                 RETURN(rc);
523
524         nodemap_global_key_init(&nk);
525         nodemap_global_rec_init(&nr, value);
526
527         if (update == NM_UPDATE)
528                 rc = nodemap_idx_update(&env, nodemap_mgs_ncf->ncf_obj,
529                                         &nk, &nr);
530         else
531                 rc = nodemap_idx_insert(&env, nodemap_mgs_ncf->ncf_obj,
532                                         &nk, &nr);
533
534         lu_env_fini(&env);
535
536         RETURN(rc);
537 }
538
539 int nodemap_idx_nodemap_activate(bool value)
540 {
541         return nodemap_idx_global_add_update(value, NM_UPDATE);
542 }
543
544 /**
545  * Process a key/rec pair and modify the new configuration.
546  *
547  * \param       config          configuration to update with this key/rec data
548  * \param       key             key of the record that was loaded
549  * \param       rec             record that was loaded
550  * \param       recent_nodemap  last referenced nodemap
551  * \retval      type of record processed, see enum #nodemap_idx_type
552  * \retval      -ENOENT         range or map loaded before nodemap record
553  * \retval      -EINVAL         duplicate nodemap cluster records found with
554  *                              different IDs, or nodemap has invalid name
555  * \retval      -ENOMEM
556  */
557 static int nodemap_process_keyrec(struct nodemap_config *config,
558                                   const struct nodemap_key *key,
559                                   const union nodemap_rec *rec,
560                                   struct lu_nodemap **recent_nodemap)
561 {
562         struct lu_nodemap       *nodemap = NULL;
563         enum nodemap_idx_type    type;
564         enum nodemap_id_type     id_type;
565         u8                       flags;
566         u32                      nodemap_id;
567         lnet_nid_t               nid[2];
568         u32                      map[2];
569         int                      rc;
570
571         CLASSERT(sizeof(union nodemap_rec) == 32);
572
573         nodemap_id = le32_to_cpu(key->nk_nodemap_id);
574         type = nm_idx_get_type(nodemap_id);
575         nodemap_id = nm_idx_set_type(nodemap_id, 0);
576
577         CDEBUG(D_INFO, "found config entry, nm_id %d type %d\n",
578                nodemap_id, type);
579
580         /* find the correct nodemap in the load list */
581         if (type == NODEMAP_RANGE_IDX || type == NODEMAP_UIDMAP_IDX ||
582             type == NODEMAP_GIDMAP_IDX) {
583                 struct lu_nodemap *tmp = NULL;
584
585                 nodemap = *recent_nodemap;
586
587                 if (nodemap == NULL)
588                         GOTO(out, rc = -ENOENT);
589
590                 if (nodemap->nm_id != nodemap_id) {
591                         list_for_each_entry(tmp, &nodemap->nm_list, nm_list)
592                                 if (tmp->nm_id == nodemap_id) {
593                                         nodemap = tmp;
594                                         break;
595                                 }
596
597                         if (nodemap->nm_id != nodemap_id)
598                                 GOTO(out, rc = -ENOENT);
599                 }
600
601                 /* update most recently used nodemap if necessay */
602                 if (nodemap != *recent_nodemap)
603                         *recent_nodemap = nodemap;
604         }
605
606         switch (type) {
607         case NODEMAP_EMPTY_IDX:
608                 if (nodemap_id != 0)
609                         CWARN("Found nodemap config record without type field, "
610                               " nodemap_id=%d. nodemap config file corrupt?\n",
611                               nodemap_id);
612                 break;
613         case NODEMAP_CLUSTER_IDX:
614                 nodemap = cfs_hash_lookup(config->nmc_nodemap_hash,
615                                           rec->ncr.ncr_name);
616                 if (nodemap == NULL) {
617                         if (nodemap_id == LUSTRE_NODEMAP_DEFAULT_ID) {
618                                 nodemap = nodemap_create(rec->ncr.ncr_name,
619                                                          config, 1);
620                                 config->nmc_default_nodemap = nodemap;
621                         } else {
622                                 nodemap = nodemap_create(rec->ncr.ncr_name,
623                                                          config, 0);
624                         }
625                         if (IS_ERR(nodemap))
626                                 GOTO(out, rc = PTR_ERR(nodemap));
627
628                         /* we need to override the local ID with the saved ID */
629                         nodemap->nm_id = nodemap_id;
630                         if (nodemap_id > config->nmc_nodemap_highest_id)
631                                 config->nmc_nodemap_highest_id = nodemap_id;
632
633                 } else if (nodemap->nm_id != nodemap_id) {
634                         nodemap_putref(nodemap);
635                         GOTO(out, rc = -EINVAL);
636                 }
637
638                 nodemap->nm_squash_uid =
639                                 le32_to_cpu(rec->ncr.ncr_squash_uid);
640                 nodemap->nm_squash_gid =
641                                 le32_to_cpu(rec->ncr.ncr_squash_gid);
642
643                 flags = le32_to_cpu(rec->ncr.ncr_flags);
644                 nodemap->nmf_allow_root_access =
645                                         flags & NM_FL_ALLOW_ROOT_ACCESS;
646                 nodemap->nmf_trust_client_ids =
647                                         flags & NM_FL_TRUST_CLIENT_IDS;
648                 nodemap->nmf_deny_unknown =
649                                         flags & NM_FL_DENY_UNKNOWN;
650
651                 if (*recent_nodemap == NULL) {
652                         *recent_nodemap = nodemap;
653                         INIT_LIST_HEAD(&nodemap->nm_list);
654                 } else {
655                         list_add(&nodemap->nm_list,
656                                  &(*recent_nodemap)->nm_list);
657                 }
658                 nodemap_putref(nodemap);
659                 break;
660         case NODEMAP_RANGE_IDX:
661                 nid[0] = le64_to_cpu(rec->nrr.nrr_start_nid);
662                 nid[1] = le64_to_cpu(rec->nrr.nrr_end_nid);
663
664                 rc = nodemap_add_range_helper(config, nodemap, nid,
665                                         le32_to_cpu(key->nk_range_id));
666                 if (rc != 0)
667                         GOTO(out, rc);
668                 break;
669         case NODEMAP_UIDMAP_IDX:
670         case NODEMAP_GIDMAP_IDX:
671                 map[0] = le32_to_cpu(key->nk_id_client);
672                 map[1] = le32_to_cpu(rec->nir.nir_id_fs);
673
674                 if (type == NODEMAP_UIDMAP_IDX)
675                         id_type = NODEMAP_UID;
676                 else
677                         id_type = NODEMAP_GID;
678
679                 rc = nodemap_add_idmap_helper(nodemap, id_type, map);
680                 if (rc != 0)
681                         GOTO(out, rc);
682                 break;
683         case NODEMAP_GLOBAL_IDX:
684                 config->nmc_nodemap_is_active = rec->ngr.ngr_is_active;
685                 break;
686         default:
687                 CERROR("got keyrec pair for unknown type %d\n", type);
688                 break;
689         }
690
691         rc = type;
692
693 out:
694         return rc;
695 }
696
697 static int nodemap_load_entries(const struct lu_env *env,
698                                 struct dt_object *nodemap_idx)
699 {
700         const struct dt_it_ops  *iops;
701         struct dt_it            *it;
702         struct lu_nodemap       *recent_nodemap = NULL;
703         struct nodemap_config   *new_config = NULL;
704         u64                      hash = 0;
705         bool                     activate_nodemap = false;
706         bool                     loaded_global_idx = false;
707         int                      rc = 0;
708
709         ENTRY;
710
711         iops = &nodemap_idx->do_index_ops->dio_it;
712
713         dt_read_lock(env, nodemap_idx, 0);
714         it = iops->init(env, nodemap_idx, 0);
715         if (IS_ERR(it))
716                 GOTO(out, rc = PTR_ERR(it));
717
718         rc = iops->load(env, it, hash);
719         if (rc == 0) {
720                 rc = iops->next(env, it);
721                 if (rc != 0)
722                         GOTO(out_iops, rc = 0);
723         }
724
725         new_config = nodemap_config_alloc();
726         if (IS_ERR(new_config)) {
727                 rc = PTR_ERR(new_config);
728                 new_config = NULL;
729                 GOTO(out_lock, rc);
730         }
731
732         do {
733                 struct nodemap_key *key;
734                 union nodemap_rec rec;
735
736                 key = (struct nodemap_key *)iops->key(env, it);
737                 rc = iops->rec(env, it, (struct dt_rec *)&rec, 0);
738                 if (rc != -ESTALE) {
739                         if (rc != 0)
740                                 GOTO(out_lock, rc);
741                         rc = nodemap_process_keyrec(new_config, key, &rec,
742                                                     &recent_nodemap);
743                         if (rc < 0)
744                                 GOTO(out_lock, rc);
745                         if (rc == NODEMAP_GLOBAL_IDX)
746                                 loaded_global_idx = true;
747                 }
748
749                 do
750                         rc = iops->next(env, it);
751                 while (rc == -ESTALE);
752         } while (rc == 0);
753
754         if (rc > 0)
755                 rc = 0;
756
757 out_lock:
758         if (rc != 0)
759                 nodemap_config_dealloc(new_config);
760         else
761                 /* creating new default needs to be done outside dt read lock */
762                 activate_nodemap = true;
763 out_iops:
764         iops->put(env, it);
765         iops->fini(env, it);
766 out:
767         dt_read_unlock(env, nodemap_idx);
768
769         if (rc != 0)
770                 CWARN("%s: failed to load nodemap configuration: rc = %d\n",
771                       nodemap_idx->do_lu.lo_dev->ld_obd->obd_name, rc);
772
773         if (!activate_nodemap)
774                 RETURN(rc);
775
776         if (new_config->nmc_default_nodemap == NULL) {
777                 /* new MGS won't have a default nm on disk, so create it here */
778                 new_config->nmc_default_nodemap =
779                         nodemap_create(DEFAULT_NODEMAP, new_config, 1);
780                 if (IS_ERR(new_config->nmc_default_nodemap)) {
781                         rc = PTR_ERR(new_config->nmc_default_nodemap);
782                 } else {
783                         rc = nodemap_idx_nodemap_add_update(
784                                         new_config->nmc_default_nodemap,
785                                         NM_ADD);
786                         nodemap_putref(new_config->nmc_default_nodemap);
787                 }
788         }
789
790         /* new nodemap config won't have an active/inactive record */
791         if (rc == 0 && loaded_global_idx == false) {
792                 struct nodemap_key       nk;
793                 union nodemap_rec        nr;
794
795                 nodemap_global_key_init(&nk);
796                 nodemap_global_rec_init(&nr, false);
797                 rc = nodemap_idx_insert(env, nodemap_idx, &nk, &nr);
798         }
799
800         if (rc == 0)
801                 nodemap_config_set_active(new_config);
802         else
803                 nodemap_config_dealloc(new_config);
804
805         RETURN(rc);
806 }
807
808 /* tracks if config still needs to be loaded, either from disk or network */
809 static bool nodemap_config_loaded;
810 static DEFINE_MUTEX(nodemap_config_loaded_lock);
811
812 /**
813  * Ensures that configs loaded over the wire are prioritized over those loaded
814  * from disk.
815  *
816  * \param config        config to set as the active config
817  */
818 void nodemap_config_set_active_mgc(struct nodemap_config *config)
819 {
820         mutex_lock(&nodemap_config_loaded_lock);
821         nodemap_config_set_active(config);
822         nodemap_config_loaded = true;
823         mutex_unlock(&nodemap_config_loaded_lock);
824 }
825 EXPORT_SYMBOL(nodemap_config_set_active_mgc);
826
827 /**
828  * Register a dt_object representing the config index file. This should be
829  * called by targets in order to load the nodemap configuration from disk. The
830  * dt_object should be created with local_index_find_or_create and the index
831  * features should be enabled with do_index_try.
832  *
833  * \param obj   dt_object returned by local_index_find_or_create
834  *
835  * \retval      on success: nm_config_file handle for later deregistration
836  * \retval      -ENOMEM         memory allocation failure
837  * \retval      -ENOENT         error loading nodemap config
838  * \retval      -EINVAL         error loading nodemap config
839  */
840 struct nm_config_file *nm_config_file_register(const struct lu_env *env,
841                                                struct dt_object *obj,
842                                                struct local_oid_storage *los,
843                                                enum nm_config_file_type ncf_type)
844 {
845         struct nm_config_file *ncf;
846         int rc = 0;
847         ENTRY;
848
849         OBD_ALLOC_PTR(ncf);
850         if (ncf == NULL)
851                 RETURN(ERR_PTR(-ENOMEM));
852
853         ncf->ncf_obj = obj;
854         ncf->ncf_los = los;
855
856         if (ncf_type == NCFT_MGS) {
857                 nodemap_mgs_ncf = ncf;
858         } else {
859                 mutex_lock(&ncf_list_lock);
860                 list_add(&ncf->ncf_list, &ncf_list_head);
861                 mutex_unlock(&ncf_list_lock);
862         }
863
864         /* prevent activation of config loaded from MGS until disk is loaded
865          * so disk config is overwritten by MGS config.
866          */
867         mutex_lock(&nodemap_config_loaded_lock);
868         if (ncf_type == NCFT_MGS || !nodemap_config_loaded)
869                 rc = nodemap_load_entries(env, obj);
870         nodemap_config_loaded = true;
871         mutex_unlock(&nodemap_config_loaded_lock);
872
873         if (rc < 0) {
874                 if (ncf_type == NCFT_MGS) {
875                         nodemap_mgs_ncf = NULL;
876                 } else {
877                         mutex_lock(&ncf_list_lock);
878                         list_del(&ncf->ncf_list);
879                         mutex_unlock(&ncf_list_lock);
880                 }
881
882                 OBD_FREE_PTR(ncf);
883                 RETURN(ERR_PTR(rc));
884         }
885
886         RETURN(ncf);
887 }
888 EXPORT_SYMBOL(nm_config_file_register);
889
890 /**
891  * Deregister a nm_config_file. Should be called by targets during cleanup.
892  *
893  * \param ncf   config file to deregister
894  */
895 void nm_config_file_deregister(const struct lu_env *env,
896                                struct nm_config_file *ncf,
897                                enum nm_config_file_type ncf_type)
898 {
899         ENTRY;
900
901         if (ncf->ncf_obj)
902                 lu_object_put(env, &ncf->ncf_obj->do_lu);
903
904         if (ncf_type == NCFT_TGT) {
905                 mutex_lock(&ncf_list_lock);
906                 list_del(&ncf->ncf_list);
907                 mutex_unlock(&ncf_list_lock);
908         } else {
909                 nodemap_mgs_ncf = NULL;
910         }
911         OBD_FREE_PTR(ncf);
912
913         EXIT;
914 }
915 EXPORT_SYMBOL(nm_config_file_deregister);
916
917 int nodemap_process_idx_pages(struct nodemap_config *config, union lu_page *lip,
918                               struct lu_nodemap **recent_nodemap)
919 {
920         struct nodemap_key *key;
921         union nodemap_rec *rec;
922         char *entry;
923         int j;
924         int k;
925         int rc = 0;
926         int size = dt_nodemap_features.dif_keysize_max +
927                    dt_nodemap_features.dif_recsize_max;
928         ENTRY;
929
930         for (j = 0; j < LU_PAGE_COUNT; j++) {
931                 if (lip->lp_idx.lip_magic != LIP_MAGIC)
932                         return -EINVAL;
933
934                 /* get and process keys and records from page */
935                 for (k = 0; k < lip->lp_idx.lip_nr; k++) {
936                         entry = lip->lp_idx.lip_entries + k * size;
937                         key = (struct nodemap_key *)entry;
938
939                         entry += dt_nodemap_features.dif_keysize_max;
940                         rec = (union nodemap_rec *)entry;
941
942                         rc = nodemap_process_keyrec(config, key, rec,
943                                                     recent_nodemap);
944                         if (rc < 0)
945                                 return rc;
946                 }
947                 lip++;
948         }
949
950         EXIT;
951         return 0;
952 }
953 EXPORT_SYMBOL(nodemap_process_idx_pages);
954
955 int nodemap_index_read(struct lu_env *env,
956                        struct nm_config_file *ncf,
957                        struct idx_info *ii,
958                        const struct lu_rdpg *rdpg)
959 {
960         struct dt_object        *nodemap_idx = ncf->ncf_obj;
961         __u64                    version;
962         int                      rc = 0;
963
964         ii->ii_keysize = dt_nodemap_features.dif_keysize_max;
965         ii->ii_recsize = dt_nodemap_features.dif_recsize_max;
966
967         dt_read_lock(env, nodemap_idx, 0);
968         version = dt_version_get(env, nodemap_idx);
969         if (rdpg->rp_hash != 0 && ii->ii_version != version) {
970                 CDEBUG(D_INFO, "nodemap config changed while sending, "
971                                "old "LPU64", new "LPU64"\n",
972                        ii->ii_version,
973                        version);
974                 ii->ii_hash_end = 0;
975         } else {
976                 rc = dt_index_walk(env, nodemap_idx, rdpg, NULL, ii);
977                 CDEBUG(D_INFO, "walked index, hashend %llx\n", ii->ii_hash_end);
978         }
979
980         if (rc >= 0)
981                 ii->ii_version = version;
982
983         dt_read_unlock(env, nodemap_idx);
984         return rc;
985 }
986 EXPORT_SYMBOL(nodemap_index_read);
987
988 /**
989  * Returns the current nodemap configuration to MGC by walking the nodemap
990  * config index and storing it in the response buffer.
991  *
992  * \param       req             incoming MGS_CONFIG_READ request
993  * \retval      0               success
994  * \retval      -EINVAL         malformed request
995  * \retval      -ENOTCONN       client evicted/reconnected already
996  * \retval      -ETIMEDOUT      client timeout or network error
997  * \retval      -ENOMEM
998  */
999 int nodemap_get_config_req(struct obd_device *mgs_obd,
1000                            struct ptlrpc_request *req)
1001 {
1002         struct mgs_config_body *body;
1003         struct mgs_config_res *res;
1004         struct lu_rdpg rdpg;
1005         struct idx_info nodemap_ii;
1006         struct ptlrpc_bulk_desc *desc;
1007         struct l_wait_info lwi;
1008         struct tg_export_data *rqexp_ted = &req->rq_export->exp_target_data;
1009         int i;
1010         int page_count;
1011         int bytes = 0;
1012         int rc = 0;
1013
1014         body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1015         if (!body)
1016                 RETURN(-EINVAL);
1017
1018         if (body->mcb_type != CONFIG_T_NODEMAP)
1019                 RETURN(-EINVAL);
1020
1021         rdpg.rp_count = (body->mcb_units << body->mcb_bits);
1022         rdpg.rp_npages = (rdpg.rp_count + PAGE_CACHE_SIZE - 1) >>
1023                 PAGE_CACHE_SHIFT;
1024         if (rdpg.rp_npages > PTLRPC_MAX_BRW_PAGES)
1025                 RETURN(-EINVAL);
1026
1027         CDEBUG(D_INFO, "reading nodemap log, name '%s', size = %u\n",
1028                body->mcb_name, rdpg.rp_count);
1029
1030         /* allocate pages to store the containers */
1031         OBD_ALLOC(rdpg.rp_pages, sizeof(*rdpg.rp_pages) * rdpg.rp_npages);
1032         if (rdpg.rp_pages == NULL)
1033                 RETURN(-ENOMEM);
1034         for (i = 0; i < rdpg.rp_npages; i++) {
1035                 rdpg.rp_pages[i] = alloc_page(GFP_IOFS);
1036                 if (rdpg.rp_pages[i] == NULL)
1037                         GOTO(out, rc = -ENOMEM);
1038         }
1039
1040         rdpg.rp_hash = body->mcb_offset;
1041         nodemap_ii.ii_magic = IDX_INFO_MAGIC;
1042         nodemap_ii.ii_flags = II_FL_NOHASH;
1043         nodemap_ii.ii_version = rqexp_ted->ted_nodemap_version;
1044
1045         bytes = nodemap_index_read(req->rq_svc_thread->t_env,
1046                                    mgs_obd->u.obt.obt_nodemap_config_file,
1047                                    &nodemap_ii, &rdpg);
1048         if (bytes < 0)
1049                 GOTO(out, rc = bytes);
1050
1051         rqexp_ted->ted_nodemap_version = nodemap_ii.ii_version;
1052
1053         res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1054         if (res == NULL)
1055                 GOTO(out, rc = -EINVAL);
1056         res->mcr_offset = nodemap_ii.ii_hash_end;
1057         res->mcr_size = bytes;
1058
1059         page_count = (bytes + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1060         LASSERT(page_count <= rdpg.rp_count);
1061         desc = ptlrpc_prep_bulk_exp(req, page_count, 1,
1062                                     PTLRPC_BULK_PUT_SOURCE |
1063                                         PTLRPC_BULK_BUF_KIOV,
1064                                     MGS_BULK_PORTAL,
1065                                     &ptlrpc_bulk_kiov_pin_ops);
1066         if (desc == NULL)
1067                 GOTO(out, rc = -ENOMEM);
1068
1069         for (i = 0; i < page_count && bytes > 0; i++) {
1070                 ptlrpc_prep_bulk_page_pin(desc, rdpg.rp_pages[i], 0,
1071                                           min_t(int, bytes, PAGE_CACHE_SIZE));
1072                 bytes -= PAGE_CACHE_SIZE;
1073         }
1074
1075         rc = target_bulk_io(req->rq_export, desc, &lwi);
1076         ptlrpc_free_bulk(desc);
1077
1078 out:
1079         if (rdpg.rp_pages != NULL) {
1080                 for (i = 0; i < rdpg.rp_npages; i++)
1081                         if (rdpg.rp_pages[i] != NULL)
1082                                 __free_page(rdpg.rp_pages[i]);
1083                 OBD_FREE(rdpg.rp_pages,
1084                          rdpg.rp_npages * sizeof(rdpg.rp_pages[0]));
1085         }
1086         return rc;
1087 }
1088 EXPORT_SYMBOL(nodemap_get_config_req);