Whamcloud - gitweb
LU-1303 lod: transfer default striping from parent/fs
[fs/lustre-release.git] / lustre / mgs / mgs_llog.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mgs/mgs_llog.c
37  *
38  * Lustre Management Server (mgs) config llog creation
39  *
40  * Author: Nathan Rutman <nathan@clusterfs.com>
41  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
42  * Author: Mikhail Pershin <tappro@whamcloud.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_MGS
46 #define D_MGS D_CONFIG
47
48 #include <obd.h>
49 #include <obd_lov.h>
50 #include <lustre_param.h>
51 #include <lustre_sec.h>
52 #include <lquota.h>
53
54 #include "mgs_internal.h"
55
56 /********************** Class functions ********************/
57
58 int class_dentry_readdir(const struct lu_env *env,
59                          struct mgs_device *mgs, cfs_list_t *list)
60 {
61         struct dt_object    *dir = mgs->mgs_configs_dir;
62         const struct dt_it_ops *iops;
63         struct dt_it        *it;
64         struct mgs_direntry *de;
65         char                *key;
66         int                  rc, key_sz;
67
68         CFS_INIT_LIST_HEAD(list);
69
70         if (!dt_try_as_dir(env, dir))
71                 GOTO(out, rc = -ENOTDIR);
72
73         LASSERT(dir);
74         LASSERT(dir->do_index_ops);
75
76         iops = &dir->do_index_ops->dio_it;
77         it = iops->init(env, dir, LUDA_64BITHASH, BYPASS_CAPA);
78         if (IS_ERR(it))
79                 RETURN(PTR_ERR(it));
80
81         rc = iops->load(env, it, 0);
82         if (rc <= 0)
83                 GOTO(fini, rc = 0);
84
85         /* main cycle */
86         do {
87                 key = (void *)iops->key(env, it);
88                 if (IS_ERR(key)) {
89                         CERROR("%s: key failed when listing %s: rc = %d\n",
90                                mgs->mgs_obd->obd_name, MOUNT_CONFIGS_DIR,
91                                (int) PTR_ERR(key));
92                         goto next;
93                 }
94                 key_sz = iops->key_size(env, it);
95                 LASSERT(key_sz > 0);
96
97                 /* filter out "." and ".." entries */
98                 if (key[0] == '.') {
99                         if (key_sz == 1)
100                                 goto next;
101                         if (key_sz == 2 && key[1] == '.')
102                                 goto next;
103                 }
104
105                 de = mgs_direntry_alloc(key_sz + 1);
106                 if (de == NULL) {
107                         rc = -ENOMEM;
108                         break;
109                 }
110
111                 memcpy(de->name, key, key_sz);
112                 de->name[key_sz] = 0;
113
114                 cfs_list_add(&de->list, list);
115
116 next:
117                 rc = iops->next(env, it);
118         } while (rc == 0);
119         rc = 0;
120
121         iops->put(env, it);
122
123 fini:
124         iops->fini(env, it);
125 out:
126         if (rc)
127                 CERROR("%s: key failed when listing %s: rc = %d\n",
128                        mgs->mgs_obd->obd_name, MOUNT_CONFIGS_DIR, rc);
129         RETURN(rc);
130 }
131
132 /******************** DB functions *********************/
133
134 static inline int name_create(char **newname, char *prefix, char *suffix)
135 {
136         LASSERT(newname);
137         OBD_ALLOC(*newname, strlen(prefix) + strlen(suffix) + 1);
138         if (!*newname)
139                 return -ENOMEM;
140         sprintf(*newname, "%s%s", prefix, suffix);
141         return 0;
142 }
143
144 static inline void name_destroy(char **name)
145 {
146         if (*name)
147                 OBD_FREE(*name, strlen(*name) + 1);
148         *name = NULL;
149 }
150
151 struct mgs_fsdb_handler_data
152 {
153         struct fs_db   *fsdb;
154         __u32           ver;
155 };
156
157 /* from the (client) config log, figure out:
158         1. which ost's/mdt's are configured (by index)
159         2. what the last config step is
160         3. COMPAT_18 osc name
161 */
162 /* It might be better to have a separate db file, instead of parsing the info
163    out of the client log.  This is slow and potentially error-prone. */
164 static int mgs_fsdb_handler(const struct lu_env *env, struct llog_handle *llh,
165                             struct llog_rec_hdr *rec, void *data)
166 {
167         struct mgs_fsdb_handler_data *d = data;
168         struct fs_db *fsdb = d->fsdb;
169         int cfg_len = rec->lrh_len;
170         char *cfg_buf = (char*) (rec + 1);
171         struct lustre_cfg *lcfg;
172         __u32 index;
173         int rc = 0;
174         ENTRY;
175
176         if (rec->lrh_type != OBD_CFG_REC) {
177                 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
178                 RETURN(-EINVAL);
179         }
180
181         rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
182         if (rc) {
183                 CERROR("Insane cfg\n");
184                 RETURN(rc);
185         }
186
187         lcfg = (struct lustre_cfg *)cfg_buf;
188
189         CDEBUG(D_INFO, "cmd %x %s %s\n", lcfg->lcfg_command,
190                lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
191
192         /* Figure out ost indicies */
193         /* lov_modify_tgts add 0:lov1  1:ost1_UUID  2(index):0  3(gen):1 */
194         if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD ||
195             lcfg->lcfg_command == LCFG_LOV_DEL_OBD) {
196                 index = simple_strtoul(lustre_cfg_string(lcfg, 2),
197                                        NULL, 10);
198                 CDEBUG(D_MGS, "OST index for %s is %u (%s)\n",
199                        lustre_cfg_string(lcfg, 1), index,
200                        lustre_cfg_string(lcfg, 2));
201                 cfs_set_bit(index, fsdb->fsdb_ost_index_map);
202         }
203
204         /* Figure out mdt indicies */
205         /* attach   0:MDC_uml1_mdsA_MNT_client  1:mdc  2:1d834_MNT_client_03f */
206         if ((lcfg->lcfg_command == LCFG_ATTACH) &&
207             (strcmp(lustre_cfg_string(lcfg, 1), LUSTRE_MDC_NAME) == 0)) {
208                 rc = server_name2index(lustre_cfg_string(lcfg, 0),
209                                        &index, NULL);
210                 if (rc != LDD_F_SV_TYPE_MDT) {
211                         CWARN("Unparsable MDC name %s, assuming index 0\n",
212                               lustre_cfg_string(lcfg, 0));
213                         index = 0;
214                 }
215                 rc = 0;
216                 CDEBUG(D_MGS, "MDT index is %u\n", index);
217                 cfs_set_bit(index, fsdb->fsdb_mdt_index_map);
218                 fsdb->fsdb_mdt_count ++;
219         }
220
221         /**
222          * figure out the old config. fsdb_gen = 0 means old log
223          * It is obsoleted and not supported anymore
224          */
225         if (fsdb->fsdb_gen == 0) {
226                 CERROR("Old config format is not supported\n");
227                 RETURN(-EINVAL);
228         }
229
230         /*
231          * compat to 1.8, check osc name used by MDT0 to OSTs, bz18548.
232          */
233         if (!cfs_test_bit(FSDB_OSCNAME18, &fsdb->fsdb_flags) &&
234             lcfg->lcfg_command == LCFG_ATTACH &&
235             strcmp(lustre_cfg_string(lcfg, 1), LUSTRE_OSC_NAME) == 0) {
236                 if (OBD_OCD_VERSION_MAJOR(d->ver) == 1 &&
237                     OBD_OCD_VERSION_MINOR(d->ver) <= 8) {
238                         CWARN("MDT using 1.8 OSC name scheme\n");
239                         cfs_set_bit(FSDB_OSCNAME18, &fsdb->fsdb_flags);
240                 }
241         }
242
243         if (lcfg->lcfg_command == LCFG_MARKER) {
244                 struct cfg_marker *marker;
245                 marker = lustre_cfg_buf(lcfg, 1);
246
247                 d->ver = marker->cm_vers;
248
249                 /* Keep track of the latest marker step */
250                 fsdb->fsdb_gen = max(fsdb->fsdb_gen, marker->cm_step);
251         }
252
253         RETURN(rc);
254 }
255
256 /* fsdb->fsdb_mutex is already held  in mgs_find_or_make_fsdb*/
257 static int mgs_get_fsdb_from_llog(const struct lu_env *env,
258                                   struct mgs_device *mgs,
259                                   struct fs_db *fsdb)
260 {
261         char                            *logname;
262         struct llog_handle              *loghandle;
263         struct llog_ctxt                *ctxt;
264         struct mgs_fsdb_handler_data     d = { fsdb, 0 };
265         int rc;
266
267         ENTRY;
268
269         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
270         LASSERT(ctxt != NULL);
271         rc = name_create(&logname, fsdb->fsdb_name, "-client");
272         if (rc)
273                 GOTO(out_put, rc);
274         cfs_mutex_lock(&fsdb->fsdb_mutex);
275         rc = llog_open_create(env, ctxt, &loghandle, NULL, logname);
276         if (rc)
277                 GOTO(out_pop, rc);
278
279         rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
280         if (rc)
281                 GOTO(out_close, rc);
282
283         if (llog_get_size(loghandle) <= 1)
284                 cfs_set_bit(FSDB_LOG_EMPTY, &fsdb->fsdb_flags);
285
286         rc = llog_process(env, loghandle, mgs_fsdb_handler, (void *)&d, NULL);
287         CDEBUG(D_INFO, "get_db = %d\n", rc);
288 out_close:
289         llog_close(env, loghandle);
290 out_pop:
291         cfs_mutex_unlock(&fsdb->fsdb_mutex);
292         name_destroy(&logname);
293 out_put:
294         llog_ctxt_put(ctxt);
295
296         RETURN(rc);
297 }
298
299 static void mgs_free_fsdb_srpc(struct fs_db *fsdb)
300 {
301         struct mgs_tgt_srpc_conf *tgtconf;
302
303         /* free target-specific rules */
304         while (fsdb->fsdb_srpc_tgt) {
305                 tgtconf = fsdb->fsdb_srpc_tgt;
306                 fsdb->fsdb_srpc_tgt = tgtconf->mtsc_next;
307
308                 LASSERT(tgtconf->mtsc_tgt);
309
310                 sptlrpc_rule_set_free(&tgtconf->mtsc_rset);
311                 OBD_FREE(tgtconf->mtsc_tgt, strlen(tgtconf->mtsc_tgt) + 1);
312                 OBD_FREE_PTR(tgtconf);
313         }
314
315         /* free general rules */
316         sptlrpc_rule_set_free(&fsdb->fsdb_srpc_gen);
317 }
318
319 struct fs_db *mgs_find_fsdb(struct mgs_device *mgs, char *fsname)
320 {
321         struct fs_db *fsdb;
322         cfs_list_t *tmp;
323
324         cfs_list_for_each(tmp, &mgs->mgs_fs_db_list) {
325                 fsdb = cfs_list_entry(tmp, struct fs_db, fsdb_list);
326                 if (strcmp(fsdb->fsdb_name, fsname) == 0)
327                         return fsdb;
328         }
329         return NULL;
330 }
331
332 /* caller must hold the mgs->mgs_fs_db_lock */
333 static struct fs_db *mgs_new_fsdb(const struct lu_env *env,
334                                   struct mgs_device *mgs, char *fsname)
335 {
336         struct fs_db *fsdb;
337         int rc;
338         ENTRY;
339
340         if (strlen(fsname) >= sizeof(fsdb->fsdb_name)) {
341                 CERROR("fsname %s is too long\n", fsname);
342                 RETURN(NULL);
343         }
344
345         OBD_ALLOC_PTR(fsdb);
346         if (!fsdb)
347                 RETURN(NULL);
348
349         strcpy(fsdb->fsdb_name, fsname);
350         cfs_mutex_init(&fsdb->fsdb_mutex);
351         cfs_set_bit(FSDB_UDESC, &fsdb->fsdb_flags);
352         fsdb->fsdb_gen = 1;
353
354         if (strcmp(fsname, MGSSELF_NAME) == 0) {
355                 cfs_set_bit(FSDB_MGS_SELF, &fsdb->fsdb_flags);
356         } else {
357                 OBD_ALLOC(fsdb->fsdb_ost_index_map, INDEX_MAP_SIZE);
358                 OBD_ALLOC(fsdb->fsdb_mdt_index_map, INDEX_MAP_SIZE);
359                 if (!fsdb->fsdb_ost_index_map || !fsdb->fsdb_mdt_index_map) {
360                         CERROR("No memory for index maps\n");
361                         GOTO(err, rc = -ENOMEM);
362                 }
363
364                 rc = name_create(&fsdb->fsdb_clilov, fsname, "-clilov");
365                 if (rc)
366                         GOTO(err, rc);
367                 rc = name_create(&fsdb->fsdb_clilmv, fsname, "-clilmv");
368                 if (rc)
369                         GOTO(err, rc);
370
371                 /* initialise data for NID table */
372                 mgs_ir_init_fs(env, mgs, fsdb);
373
374                 lproc_mgs_add_live(mgs, fsdb);
375         }
376
377         cfs_list_add(&fsdb->fsdb_list, &mgs->mgs_fs_db_list);
378
379         RETURN(fsdb);
380 err:
381         if (fsdb->fsdb_ost_index_map)
382                 OBD_FREE(fsdb->fsdb_ost_index_map, INDEX_MAP_SIZE);
383         if (fsdb->fsdb_mdt_index_map)
384                 OBD_FREE(fsdb->fsdb_mdt_index_map, INDEX_MAP_SIZE);
385         name_destroy(&fsdb->fsdb_clilov);
386         name_destroy(&fsdb->fsdb_clilmv);
387         OBD_FREE_PTR(fsdb);
388         RETURN(NULL);
389 }
390
391 static void mgs_free_fsdb(struct mgs_device *mgs, struct fs_db *fsdb)
392 {
393         /* wait for anyone with the sem */
394         cfs_mutex_lock(&fsdb->fsdb_mutex);
395         lproc_mgs_del_live(mgs, fsdb);
396         cfs_list_del(&fsdb->fsdb_list);
397
398         /* deinitialize fsr */
399         mgs_ir_fini_fs(mgs, fsdb);
400
401         if (fsdb->fsdb_ost_index_map)
402                 OBD_FREE(fsdb->fsdb_ost_index_map, INDEX_MAP_SIZE);
403         if (fsdb->fsdb_mdt_index_map)
404                 OBD_FREE(fsdb->fsdb_mdt_index_map, INDEX_MAP_SIZE);
405         name_destroy(&fsdb->fsdb_clilov);
406         name_destroy(&fsdb->fsdb_clilmv);
407         mgs_free_fsdb_srpc(fsdb);
408         cfs_mutex_unlock(&fsdb->fsdb_mutex);
409         OBD_FREE_PTR(fsdb);
410 }
411
412 int mgs_init_fsdb_list(struct mgs_device *mgs)
413 {
414         CFS_INIT_LIST_HEAD(&mgs->mgs_fs_db_list);
415         return 0;
416 }
417
418 int mgs_cleanup_fsdb_list(struct mgs_device *mgs)
419 {
420         struct fs_db *fsdb;
421         cfs_list_t *tmp, *tmp2;
422         cfs_mutex_lock(&mgs->mgs_mutex);
423         cfs_list_for_each_safe(tmp, tmp2, &mgs->mgs_fs_db_list) {
424                 fsdb = cfs_list_entry(tmp, struct fs_db, fsdb_list);
425                 mgs_free_fsdb(mgs, fsdb);
426         }
427         cfs_mutex_unlock(&mgs->mgs_mutex);
428         return 0;
429 }
430
431 int mgs_find_or_make_fsdb(const struct lu_env *env,
432                           struct mgs_device *mgs, char *name,
433                           struct fs_db **dbh)
434 {
435         struct fs_db *fsdb;
436         int rc = 0;
437
438         cfs_mutex_lock(&mgs->mgs_mutex);
439         fsdb = mgs_find_fsdb(mgs, name);
440         if (fsdb) {
441                 cfs_mutex_unlock(&mgs->mgs_mutex);
442                 *dbh = fsdb;
443                 return 0;
444         }
445
446         CDEBUG(D_MGS, "Creating new db\n");
447         fsdb = mgs_new_fsdb(env, mgs, name);
448         cfs_mutex_unlock(&mgs->mgs_mutex);
449         if (!fsdb)
450                 return -ENOMEM;
451
452         if (!cfs_test_bit(FSDB_MGS_SELF, &fsdb->fsdb_flags)) {
453                 /* populate the db from the client llog */
454                 rc = mgs_get_fsdb_from_llog(env, mgs, fsdb);
455                 if (rc) {
456                         CERROR("Can't get db from client log %d\n", rc);
457                         mgs_free_fsdb(mgs, fsdb);
458                         return rc;
459                 }
460         }
461
462         /* populate srpc rules from params llog */
463         rc = mgs_get_fsdb_srpc_from_llog(env, mgs, fsdb);
464         if (rc) {
465                 CERROR("Can't get db from params log %d\n", rc);
466                 mgs_free_fsdb(mgs, fsdb);
467                 return rc;
468         }
469
470         *dbh = fsdb;
471
472         return 0;
473 }
474
475 /* 1 = index in use
476    0 = index unused
477    -1= empty client log */
478 int mgs_check_index(const struct lu_env *env,
479                     struct mgs_device *mgs,
480                     struct mgs_target_info *mti)
481 {
482         struct fs_db *fsdb;
483         void *imap;
484         int rc = 0;
485         ENTRY;
486
487         LASSERT(!(mti->mti_flags & LDD_F_NEED_INDEX));
488
489         rc = mgs_find_or_make_fsdb(env, mgs, mti->mti_fsname, &fsdb);
490         if (rc) {
491                 CERROR("Can't get db for %s\n", mti->mti_fsname);
492                 RETURN(rc);
493         }
494
495         if (cfs_test_bit(FSDB_LOG_EMPTY, &fsdb->fsdb_flags))
496                 RETURN(-1);
497
498         if (mti->mti_flags & LDD_F_SV_TYPE_OST)
499                 imap = fsdb->fsdb_ost_index_map;
500         else if (mti->mti_flags & LDD_F_SV_TYPE_MDT)
501                 imap = fsdb->fsdb_mdt_index_map;
502         else
503                 RETURN(-EINVAL);
504
505         if (cfs_test_bit(mti->mti_stripe_index, imap))
506                 RETURN(1);
507         RETURN(0);
508 }
509
510 static __inline__ int next_index(void *index_map, int map_len)
511 {
512         int i;
513         for (i = 0; i < map_len * 8; i++)
514                  if (!cfs_test_bit(i, index_map)) {
515                          return i;
516                  }
517         CERROR("max index %d exceeded.\n", i);
518         return -1;
519 }
520
521 /* Return codes:
522         0  newly marked as in use
523         <0 err
524         +EALREADY for update of an old index */
525 static int mgs_set_index(const struct lu_env *env,
526                          struct mgs_device *mgs,
527                          struct mgs_target_info *mti)
528 {
529         struct fs_db *fsdb;
530         void *imap;
531         int rc = 0;
532         ENTRY;
533
534         rc = mgs_find_or_make_fsdb(env, mgs, mti->mti_fsname, &fsdb);
535         if (rc) {
536                 CERROR("Can't get db for %s\n", mti->mti_fsname);
537                 RETURN(rc);
538         }
539
540         if (mti->mti_flags & LDD_F_SV_TYPE_OST) {
541                 imap = fsdb->fsdb_ost_index_map;
542         } else if (mti->mti_flags & LDD_F_SV_TYPE_MDT) {
543                 imap = fsdb->fsdb_mdt_index_map;
544                 if (fsdb->fsdb_mdt_count >= MAX_MDT_COUNT) {
545                         LCONSOLE_ERROR_MSG(0x13f, "The max mdt count"
546                                            "is %d\n", (int)MAX_MDT_COUNT);
547                         RETURN(-ERANGE);
548                 }
549         } else {
550                 RETURN(-EINVAL);
551         }
552
553         if (mti->mti_flags & LDD_F_NEED_INDEX) {
554                 rc = next_index(imap, INDEX_MAP_SIZE);
555                 if (rc == -1)
556                         RETURN(-ERANGE);
557                 mti->mti_stripe_index = rc;
558                 if (mti->mti_flags & LDD_F_SV_TYPE_MDT)
559                         fsdb->fsdb_mdt_count ++;
560         }
561
562         if (mti->mti_stripe_index >= INDEX_MAP_SIZE * 8) {
563                 LCONSOLE_ERROR_MSG(0x13f, "Server %s requested index %d, "
564                                    "but the max index is %d.\n",
565                                    mti->mti_svname, mti->mti_stripe_index,
566                                    INDEX_MAP_SIZE * 8);
567                 RETURN(-ERANGE);
568         }
569
570         if (cfs_test_bit(mti->mti_stripe_index, imap)) {
571                 if ((mti->mti_flags & LDD_F_VIRGIN) &&
572                     !(mti->mti_flags & LDD_F_WRITECONF)) {
573                         LCONSOLE_ERROR_MSG(0x140, "Server %s requested index "
574                                            "%d, but that index is already in "
575                                            "use. Use --writeconf to force\n",
576                                            mti->mti_svname,
577                                            mti->mti_stripe_index);
578                         RETURN(-EADDRINUSE);
579                 } else {
580                         CDEBUG(D_MGS, "Server %s updating index %d\n",
581                                mti->mti_svname, mti->mti_stripe_index);
582                         RETURN(EALREADY);
583                 }
584         }
585
586         cfs_set_bit(mti->mti_stripe_index, imap);
587         cfs_clear_bit(FSDB_LOG_EMPTY, &fsdb->fsdb_flags);
588         server_make_name(mti->mti_flags & ~(LDD_F_VIRGIN | LDD_F_WRITECONF),
589                          mti->mti_stripe_index, mti->mti_fsname, mti->mti_svname);
590
591         CDEBUG(D_MGS, "Set index for %s to %d\n", mti->mti_svname,
592                mti->mti_stripe_index);
593
594         RETURN(0);
595 }
596
597 struct mgs_modify_lookup {
598         struct cfg_marker mml_marker;
599         int               mml_modified;
600 };
601
602 static int mgs_modify_handler(const struct lu_env *env,
603                               struct llog_handle *llh,
604                               struct llog_rec_hdr *rec, void *data)
605 {
606         struct mgs_modify_lookup *mml = data;
607         struct cfg_marker *marker;
608         struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
609         int cfg_len = rec->lrh_len - sizeof(struct llog_rec_hdr) -
610                 sizeof(struct llog_rec_tail);
611         int rc;
612         ENTRY;
613
614         if (rec->lrh_type != OBD_CFG_REC) {
615                 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
616                 RETURN(-EINVAL);
617         }
618
619         rc = lustre_cfg_sanity_check(lcfg, cfg_len);
620         if (rc) {
621                 CERROR("Insane cfg\n");
622                 RETURN(rc);
623         }
624
625         /* We only care about markers */
626         if (lcfg->lcfg_command != LCFG_MARKER)
627                 RETURN(0);
628
629         marker = lustre_cfg_buf(lcfg, 1);
630         if ((strcmp(mml->mml_marker.cm_comment, marker->cm_comment) == 0) &&
631             (strcmp(mml->mml_marker.cm_tgtname, marker->cm_tgtname) == 0) &&
632             !(marker->cm_flags & CM_SKIP)) {
633                 /* Found a non-skipped marker match */
634                 CDEBUG(D_MGS, "Changing rec %u marker %d %x->%x: %s %s\n",
635                        rec->lrh_index, marker->cm_step,
636                        marker->cm_flags, mml->mml_marker.cm_flags,
637                        marker->cm_tgtname, marker->cm_comment);
638                 /* Overwrite the old marker llog entry */
639                 marker->cm_flags &= ~CM_EXCLUDE; /* in case we're unexcluding */
640                 marker->cm_flags |= mml->mml_marker.cm_flags;
641                 marker->cm_canceltime = mml->mml_marker.cm_canceltime;
642                 /* Header and tail are added back to lrh_len in
643                    llog_lvfs_write_rec */
644                 rec->lrh_len = cfg_len;
645                 rc = llog_write(env, llh, rec, NULL, 0, (void *)lcfg,
646                                 rec->lrh_index);
647                 if (!rc)
648                          mml->mml_modified++;
649         }
650
651         RETURN(rc);
652 }
653
654 /**
655  * Modify an existing config log record (for CM_SKIP or CM_EXCLUDE)
656  * Return code:
657  * 0 - modified successfully,
658  * 1 - no modification was done
659  * negative - error
660  */
661 static int mgs_modify(const struct lu_env *env, struct mgs_device *mgs,
662                       struct fs_db *fsdb, struct mgs_target_info *mti,
663                       char *logname, char *devname, char *comment, int flags)
664 {
665         struct llog_handle *loghandle;
666         struct llog_ctxt *ctxt;
667         struct mgs_modify_lookup *mml;
668         int rc;
669
670         ENTRY;
671
672         LASSERT(cfs_mutex_is_locked(&fsdb->fsdb_mutex));
673         CDEBUG(D_MGS, "modify %s/%s/%s fl=%x\n", logname, devname, comment,
674                flags);
675
676         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
677         LASSERT(ctxt != NULL);
678         rc = llog_open(env, ctxt, &loghandle, NULL, logname, LLOG_OPEN_EXISTS);
679         if (rc < 0) {
680                 if (rc == -ENOENT)
681                         rc = 0;
682                 GOTO(out_pop, rc);
683         }
684
685         rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
686         if (rc)
687                 GOTO(out_close, rc);
688
689         if (llog_get_size(loghandle) <= 1)
690                 GOTO(out_close, rc = 0);
691
692         OBD_ALLOC_PTR(mml);
693         if (!mml)
694                 GOTO(out_close, rc = -ENOMEM);
695         strcpy(mml->mml_marker.cm_comment, comment);
696         strcpy(mml->mml_marker.cm_tgtname, devname);
697         /* Modify mostly means cancel */
698         mml->mml_marker.cm_flags = flags;
699         mml->mml_marker.cm_canceltime = flags ? cfs_time_current_sec() : 0;
700         mml->mml_modified = 0;
701         rc = llog_process(env, loghandle, mgs_modify_handler, (void *)mml,
702                           NULL);
703         if (!rc && !mml->mml_modified)
704                 rc = 1;
705         OBD_FREE_PTR(mml);
706
707 out_close:
708         llog_close(env, loghandle);
709 out_pop:
710         if (rc < 0)
711                 CERROR("%s: modify %s/%s failed: rc = %d\n",
712                        mgs->mgs_obd->obd_name, mti->mti_svname, comment, rc);
713         llog_ctxt_put(ctxt);
714         RETURN(rc);
715 }
716
717 /******************** config log recording functions *********************/
718
719 static int record_lcfg(const struct lu_env *env, struct llog_handle *llh,
720                        struct lustre_cfg *lcfg)
721 {
722         struct llog_rec_hdr      rec;
723         int                      buflen, rc;
724
725         if (!lcfg || !llh)
726                 return -ENOMEM;
727
728         LASSERT(llh->lgh_ctxt);
729
730         buflen = lustre_cfg_len(lcfg->lcfg_bufcount,
731                                 lcfg->lcfg_buflens);
732         rec.lrh_len = llog_data_len(buflen);
733         rec.lrh_type = OBD_CFG_REC;
734
735         /* idx = -1 means append */
736         rc = llog_write(env, llh, &rec, NULL, 0, (void *)lcfg, -1);
737         if (rc)
738                 CERROR("failed %d\n", rc);
739         return rc;
740 }
741
742 static int record_base(const struct lu_env *env, struct llog_handle *llh,
743                      char *cfgname, lnet_nid_t nid, int cmd,
744                      char *s1, char *s2, char *s3, char *s4)
745 {
746         struct mgs_thread_info *mgi = mgs_env_info(env);
747         struct lustre_cfg     *lcfg;
748         int rc;
749
750         CDEBUG(D_MGS, "lcfg %s %#x %s %s %s %s\n", cfgname,
751                cmd, s1, s2, s3, s4);
752
753         lustre_cfg_bufs_reset(&mgi->mgi_bufs, cfgname);
754         if (s1)
755                 lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 1, s1);
756         if (s2)
757                 lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 2, s2);
758         if (s3)
759                 lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 3, s3);
760         if (s4)
761                 lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 4, s4);
762
763         lcfg = lustre_cfg_new(cmd, &mgi->mgi_bufs);
764         if (!lcfg)
765                 return -ENOMEM;
766         lcfg->lcfg_nid = nid;
767
768         rc = record_lcfg(env, llh, lcfg);
769
770         lustre_cfg_free(lcfg);
771
772         if (rc) {
773                 CERROR("error %d: lcfg %s %#x %s %s %s %s\n", rc, cfgname,
774                        cmd, s1, s2, s3, s4);
775         }
776         return rc;
777 }
778
779
780 static inline int record_add_uuid(const struct lu_env *env,
781                                   struct llog_handle *llh,
782                                   uint64_t nid, char *uuid)
783 {
784         return record_base(env, llh, NULL, nid, LCFG_ADD_UUID, uuid, 0, 0, 0);
785
786 }
787
788 static inline int record_add_conn(const struct lu_env *env,
789                                   struct llog_handle *llh,
790                                   char *devname, char *uuid)
791 {
792         return record_base(env, llh, devname, 0, LCFG_ADD_CONN, uuid, 0, 0, 0);
793 }
794
795 static inline int record_attach(const struct lu_env *env,
796                                 struct llog_handle *llh, char *devname,
797                                 char *type, char *uuid)
798 {
799         return record_base(env, llh,devname, 0, LCFG_ATTACH, type, uuid, 0, 0);
800 }
801
802 static inline int record_setup(const struct lu_env *env,
803                                struct llog_handle *llh, char *devname,
804                                char *s1, char *s2, char *s3, char *s4)
805 {
806         return record_base(env, llh, devname, 0, LCFG_SETUP, s1, s2, s3, s4);
807 }
808
809 static int record_lov_setup(const struct lu_env *env, struct llog_handle *llh,
810                             char *devname, struct lov_desc *desc)
811 {
812         struct mgs_thread_info *mgi = mgs_env_info(env);
813         struct lustre_cfg *lcfg;
814         int rc;
815
816         lustre_cfg_bufs_reset(&mgi->mgi_bufs, devname);
817         lustre_cfg_bufs_set(&mgi->mgi_bufs, 1, desc, sizeof(*desc));
818         lcfg = lustre_cfg_new(LCFG_SETUP, &mgi->mgi_bufs);
819         if (!lcfg)
820                 return -ENOMEM;
821         rc = record_lcfg(env, llh, lcfg);
822
823         lustre_cfg_free(lcfg);
824         return rc;
825 }
826
827 static int record_lmv_setup(const struct lu_env *env, struct llog_handle *llh,
828                             char *devname, struct lmv_desc *desc)
829 {
830         struct mgs_thread_info *mgi = mgs_env_info(env);
831         struct lustre_cfg *lcfg;
832         int rc;
833
834         lustre_cfg_bufs_reset(&mgi->mgi_bufs, devname);
835         lustre_cfg_bufs_set(&mgi->mgi_bufs, 1, desc, sizeof(*desc));
836         lcfg = lustre_cfg_new(LCFG_SETUP, &mgi->mgi_bufs);
837
838         rc = record_lcfg(env, llh, lcfg);
839
840         lustre_cfg_free(lcfg);
841         return rc;
842 }
843
844 static inline int record_mdc_add(const struct lu_env *env,
845                                  struct llog_handle *llh,
846                                  char *logname, char *mdcuuid,
847                                  char *mdtuuid, char *index,
848                                  char *gen)
849 {
850         return record_base(env,llh,logname,0,LCFG_ADD_MDC,
851                            mdtuuid,index,gen,mdcuuid);
852 }
853
854 static inline int record_lov_add(const struct lu_env *env,
855                                  struct llog_handle *llh,
856                                  char *lov_name, char *ost_uuid,
857                                  char *index, char *gen)
858 {
859         return record_base(env,llh,lov_name,0,LCFG_LOV_ADD_OBD,
860                            ost_uuid,index,gen,0);
861 }
862
863 static inline int record_mount_opt(const struct lu_env *env,
864                                    struct llog_handle *llh,
865                                    char *profile, char *lov_name,
866                                    char *mdc_name)
867 {
868         return record_base(env,llh,NULL,0,LCFG_MOUNTOPT,
869                            profile,lov_name,mdc_name,0);
870 }
871
872 static int record_marker(const struct lu_env *env,
873                          struct llog_handle *llh,
874                          struct fs_db *fsdb, __u32 flags,
875                          char *tgtname, char *comment)
876 {
877         struct mgs_thread_info *mgi = mgs_env_info(env);
878         struct lustre_cfg *lcfg;
879         int rc;
880
881         if (flags & CM_START)
882                 fsdb->fsdb_gen++;
883         mgi->mgi_marker.cm_step = fsdb->fsdb_gen;
884         mgi->mgi_marker.cm_flags = flags;
885         mgi->mgi_marker.cm_vers = LUSTRE_VERSION_CODE;
886         strncpy(mgi->mgi_marker.cm_tgtname, tgtname,
887                 sizeof(mgi->mgi_marker.cm_tgtname));
888         strncpy(mgi->mgi_marker.cm_comment, comment,
889                 sizeof(mgi->mgi_marker.cm_comment));
890         mgi->mgi_marker.cm_createtime = cfs_time_current_sec();
891         mgi->mgi_marker.cm_canceltime = 0;
892         lustre_cfg_bufs_reset(&mgi->mgi_bufs, NULL);
893         lustre_cfg_bufs_set(&mgi->mgi_bufs, 1, &mgi->mgi_marker,
894                             sizeof(mgi->mgi_marker));
895         lcfg = lustre_cfg_new(LCFG_MARKER, &mgi->mgi_bufs);
896         if (!lcfg)
897                 return -ENOMEM;
898         rc = record_lcfg(env, llh, lcfg);
899
900         lustre_cfg_free(lcfg);
901         return rc;
902 }
903
904 static int record_start_log(const struct lu_env *env, struct mgs_device *mgs,
905                             struct llog_handle **llh, char *name)
906 {
907         static struct obd_uuid   cfg_uuid = { .uuid = "config_uuid" };
908         struct llog_ctxt        *ctxt;
909         int                      rc = 0;
910
911         if (*llh)
912                 GOTO(out, rc = -EBUSY);
913
914         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
915         if (!ctxt)
916                 GOTO(out, rc = -ENODEV);
917         LASSERT(ctxt->loc_obd == mgs->mgs_obd);
918
919         rc = llog_open_create(env, ctxt, llh, NULL, name);
920         if (rc)
921                 GOTO(out_ctxt, rc);
922         rc = llog_init_handle(env, *llh, LLOG_F_IS_PLAIN, &cfg_uuid);
923         if (rc)
924                 llog_close(env, *llh);
925 out_ctxt:
926         llog_ctxt_put(ctxt);
927 out:
928         if (rc) {
929                 CERROR("%s: can't start log %s: rc = %d\n",
930                        mgs->mgs_obd->obd_name, name, rc);
931                 *llh = NULL;
932         }
933         RETURN(rc);
934 }
935
936 static int record_end_log(const struct lu_env *env, struct llog_handle **llh)
937 {
938         int rc;
939
940         rc = llog_close(env, *llh);
941         *llh = NULL;
942
943         return rc;
944 }
945
946 static int mgs_log_is_empty(const struct lu_env *env,
947                             struct mgs_device *mgs, char *name)
948 {
949         struct llog_handle *llh;
950         struct llog_ctxt *ctxt;
951         int rc = 0;
952
953         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
954         LASSERT(ctxt != NULL);
955         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
956         if (rc < 0) {
957                 if (rc == -ENOENT)
958                         rc = 0;
959                 GOTO(out_ctxt, rc);
960         }
961
962         llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
963         if (rc)
964                 GOTO(out_close, rc);
965         rc = llog_get_size(llh);
966
967 out_close:
968         llog_close(env, llh);
969 out_ctxt:
970         llog_ctxt_put(ctxt);
971         /* header is record 1 */
972         return (rc <= 1);
973 }
974
975 /******************** config "macros" *********************/
976
977 /* write an lcfg directly into a log (with markers) */
978 static int mgs_write_log_direct(const struct lu_env *env,
979                                 struct mgs_device *mgs, struct fs_db *fsdb,
980                                 char *logname, struct lustre_cfg *lcfg,
981                                 char *devname, char *comment)
982 {
983         struct llog_handle *llh = NULL;
984         int rc;
985         ENTRY;
986
987         if (!lcfg)
988                 RETURN(-ENOMEM);
989
990         rc = record_start_log(env, mgs, &llh, logname);
991         if (rc)
992                 RETURN(rc);
993
994         /* FIXME These should be a single journal transaction */
995         rc = record_marker(env, llh, fsdb, CM_START, devname, comment);
996         if (rc)
997                 GOTO(out_end, rc);
998         rc = record_lcfg(env, llh, lcfg);
999         if (rc)
1000                 GOTO(out_end, rc);
1001         rc = record_marker(env, llh, fsdb, CM_END, devname, comment);
1002         if (rc)
1003                 GOTO(out_end, rc);
1004 out_end:
1005         record_end_log(env, &llh);
1006         RETURN(rc);
1007 }
1008
1009 /* write the lcfg in all logs for the given fs */
1010 int mgs_write_log_direct_all(const struct lu_env *env,
1011                              struct mgs_device *mgs,
1012                              struct fs_db *fsdb,
1013                              struct mgs_target_info *mti,
1014                              struct lustre_cfg *lcfg,
1015                              char *devname, char *comment,
1016                              int server_only)
1017 {
1018         cfs_list_t list;
1019         struct mgs_direntry *dirent, *n;
1020         char *fsname = mti->mti_fsname;
1021         char *logname;
1022         int rc = 0, len = strlen(fsname);
1023         ENTRY;
1024
1025         /* We need to set params for any future logs
1026            as well. FIXME Append this file to every new log.
1027            Actually, we should store as params (text), not llogs.  Or
1028            in a database. */
1029         rc = name_create(&logname, fsname, "-params");
1030         if (rc)
1031                 RETURN(rc);
1032         if (mgs_log_is_empty(env, mgs, logname)) {
1033                 struct llog_handle *llh = NULL;
1034                 rc = record_start_log(env, mgs, &llh, logname);
1035                 record_end_log(env, &llh);
1036         }
1037         name_destroy(&logname);
1038         if (rc)
1039                 RETURN(rc);
1040
1041         /* Find all the logs in the CONFIGS directory */
1042         rc = class_dentry_readdir(env, mgs, &list);
1043         if (rc)
1044                 RETURN(rc);
1045
1046         /* Could use fsdb index maps instead of directory listing */
1047         cfs_list_for_each_entry_safe(dirent, n, &list, list) {
1048                 cfs_list_del(&dirent->list);
1049                 /* don't write to sptlrpc rule log */
1050                 if (strstr(dirent->name, "-sptlrpc") != NULL)
1051                         goto next;
1052
1053                 /* caller wants write server logs only */
1054                 if (server_only && strstr(dirent->name, "-client") != NULL)
1055                         goto next;
1056
1057                 if (strncmp(fsname, dirent->name, len) == 0) {
1058                         CDEBUG(D_MGS, "Changing log %s\n", dirent->name);
1059                         /* Erase any old settings of this same parameter */
1060                         rc = mgs_modify(env, mgs, fsdb, mti, dirent->name,
1061                                         devname, comment, CM_SKIP);
1062                         if (rc < 0)
1063                                 CERROR("%s: Can't modify llog %s: rc = %d\n",
1064                                        mgs->mgs_obd->obd_name, dirent->name,rc);
1065                         /* Write the new one */
1066                         if (lcfg) {
1067                                 rc = mgs_write_log_direct(env, mgs, fsdb,
1068                                                           dirent->name,
1069                                                           lcfg, devname,
1070                                                           comment);
1071                                 if (rc)
1072                                         CERROR("%s: writing log %s: rc = %d\n",
1073                                                mgs->mgs_obd->obd_name,
1074                                                dirent->name, rc);
1075                         }
1076                 }
1077 next:
1078                 mgs_direntry_free(dirent);
1079         }
1080
1081         RETURN(rc);
1082 }
1083
1084 static int mgs_write_log_mdc_to_mdt(const struct lu_env *env,
1085                                     struct mgs_device *mgs,
1086                                     struct fs_db *fsdb,
1087                                     struct mgs_target_info *mti,
1088                                     char *logname);
1089 static int mgs_write_log_osc_to_lov(const struct lu_env *env,
1090                                     struct mgs_device *mgs,
1091                                     struct fs_db *fsdb,
1092                                     struct mgs_target_info *mti,
1093                                     char *logname, char *suffix, char *lovname,
1094                                     enum lustre_sec_part sec_part, int flags);
1095 static int name_create_mdt_and_lov(char **logname, char **lovname,
1096                                    struct fs_db *fsdb, int i);
1097
1098 static int mgs_steal_llog_handler(const struct lu_env *env,
1099                                   struct llog_handle *llh,
1100                                   struct llog_rec_hdr *rec, void *data)
1101 {
1102         struct mgs_device *mgs;
1103         struct obd_device *obd;
1104         struct mgs_target_info *mti, *tmti;
1105         struct fs_db *fsdb;
1106         int cfg_len = rec->lrh_len;
1107         char *cfg_buf = (char*) (rec + 1);
1108         struct lustre_cfg *lcfg;
1109         int rc = 0;
1110         struct llog_handle *mdt_llh = NULL;
1111         static int got_an_osc_or_mdc = 0;
1112         /* 0: not found any osc/mdc;
1113            1: found osc;
1114            2: found mdc;
1115         */
1116         static int last_step = -1;
1117
1118         ENTRY;
1119
1120         mti = ((struct temp_comp*)data)->comp_mti;
1121         tmti = ((struct temp_comp*)data)->comp_tmti;
1122         fsdb = ((struct temp_comp*)data)->comp_fsdb;
1123         obd = ((struct temp_comp *)data)->comp_obd;
1124         mgs = lu2mgs_dev(obd->obd_lu_dev);
1125         LASSERT(mgs);
1126
1127         if (rec->lrh_type != OBD_CFG_REC) {
1128                 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
1129                 RETURN(-EINVAL);
1130         }
1131
1132         rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1133         if (rc) {
1134                 CERROR("Insane cfg\n");
1135                 RETURN(rc);
1136         }
1137
1138         lcfg = (struct lustre_cfg *)cfg_buf;
1139
1140         if (lcfg->lcfg_command == LCFG_MARKER) {
1141                 struct cfg_marker *marker;
1142                 marker = lustre_cfg_buf(lcfg, 1);
1143                 if (!strncmp(marker->cm_comment,"add osc",7) &&
1144                     (marker->cm_flags & CM_START)){
1145                         got_an_osc_or_mdc = 1;
1146                         strncpy(tmti->mti_svname, marker->cm_tgtname,
1147                                 sizeof(tmti->mti_svname));
1148                         rc = record_start_log(env, mgs, &mdt_llh,
1149                                               mti->mti_svname);
1150                         if (rc)
1151                                 RETURN(rc);
1152                         rc = record_marker(env, mdt_llh, fsdb, CM_START,
1153                                            mti->mti_svname,"add osc(copied)");
1154                         record_end_log(env, &mdt_llh);
1155                         last_step = marker->cm_step;
1156                         RETURN(rc);
1157                 }
1158                 if (!strncmp(marker->cm_comment,"add osc",7) &&
1159                     (marker->cm_flags & CM_END)){
1160                         LASSERT(last_step == marker->cm_step);
1161                         last_step = -1;
1162                         got_an_osc_or_mdc = 0;
1163                         rc = record_start_log(env, mgs, &mdt_llh,
1164                                               mti->mti_svname);
1165                         if (rc)
1166                                 RETURN(rc);
1167                         rc = record_marker(env, mdt_llh, fsdb, CM_END,
1168                                            mti->mti_svname,"add osc(copied)");
1169                         record_end_log(env, &mdt_llh);
1170                         RETURN(rc);
1171                 }
1172                 if (!strncmp(marker->cm_comment,"add mdc",7) &&
1173                     (marker->cm_flags & CM_START)){
1174                         got_an_osc_or_mdc = 2;
1175                         last_step = marker->cm_step;
1176                         memcpy(tmti->mti_svname, marker->cm_tgtname,
1177                                strlen(marker->cm_tgtname));
1178
1179                         RETURN(rc);
1180                 }
1181                 if (!strncmp(marker->cm_comment,"add mdc",7) &&
1182                     (marker->cm_flags & CM_END)){
1183                         LASSERT(last_step == marker->cm_step);
1184                         last_step = -1;
1185                         got_an_osc_or_mdc = 0;
1186                         RETURN(rc);
1187                 }
1188         }
1189
1190         if (got_an_osc_or_mdc == 0 || last_step < 0)
1191                 RETURN(rc);
1192
1193         if (lcfg->lcfg_command == LCFG_ADD_UUID) {
1194                 uint64_t nodenid;
1195                 nodenid = lcfg->lcfg_nid;
1196
1197                 tmti->mti_nids[tmti->mti_nid_count] = nodenid;
1198                 tmti->mti_nid_count++;
1199
1200                 RETURN(rc);
1201         }
1202
1203         if (lcfg->lcfg_command == LCFG_SETUP) {
1204                 char *target;
1205
1206                 target = lustre_cfg_string(lcfg, 1);
1207                 memcpy(tmti->mti_uuid, target, strlen(target));
1208                 RETURN(rc);
1209         }
1210
1211         /* ignore client side sptlrpc_conf_log */
1212         if (lcfg->lcfg_command == LCFG_SPTLRPC_CONF)
1213                 RETURN(rc);
1214
1215         if (lcfg->lcfg_command == LCFG_ADD_MDC) {
1216                 int index;
1217
1218                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
1219                         RETURN (-EINVAL);
1220
1221                 memcpy(tmti->mti_fsname, mti->mti_fsname,
1222                        strlen(mti->mti_fsname));
1223                 tmti->mti_stripe_index = index;
1224
1225                 rc = mgs_write_log_mdc_to_mdt(env, mgs, fsdb, tmti,
1226                                               mti->mti_svname);
1227                 memset(tmti, 0, sizeof(*tmti));
1228                 RETURN(rc);
1229         }
1230
1231         if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
1232                 int index;
1233                 char mdt_index[9];
1234                 char *logname, *lovname;
1235
1236                 rc = name_create_mdt_and_lov(&logname, &lovname, fsdb,
1237                                              mti->mti_stripe_index);
1238                 if (rc)
1239                         RETURN(rc);
1240                 sprintf(mdt_index, "-MDT%04x", mti->mti_stripe_index);
1241
1242                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1) {
1243                         name_destroy(&logname);
1244                         name_destroy(&lovname);
1245                         RETURN(-EINVAL);
1246                 }
1247
1248                 tmti->mti_stripe_index = index;
1249                 rc = mgs_write_log_osc_to_lov(env, mgs, fsdb, tmti, logname,
1250                                          mdt_index, lovname,
1251                                          LUSTRE_SP_MDT, 0);
1252                 name_destroy(&logname);
1253                 name_destroy(&lovname);
1254                 RETURN(rc);
1255         }
1256         RETURN(rc);
1257 }
1258
1259 /* fsdb->fsdb_mutex is already held  in mgs_write_log_target*/
1260 /* stealed from mgs_get_fsdb_from_llog*/
1261 static int mgs_steal_llog_for_mdt_from_client(const struct lu_env *env,
1262                                               struct mgs_device *mgs,
1263                                               char *client_name,
1264                                               struct temp_comp* comp)
1265 {
1266         struct llog_handle *loghandle;
1267         struct mgs_target_info *tmti;
1268         struct llog_ctxt *ctxt;
1269         int rc;
1270
1271         ENTRY;
1272
1273         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1274         LASSERT(ctxt != NULL);
1275
1276         OBD_ALLOC_PTR(tmti);
1277         if (tmti == NULL)
1278                 GOTO(out_ctxt, rc = -ENOMEM);
1279
1280         comp->comp_tmti = tmti;
1281         comp->comp_obd = mgs->mgs_obd;
1282
1283         rc = llog_open(env, ctxt, &loghandle, NULL, client_name,
1284                        LLOG_OPEN_EXISTS);
1285         if (rc < 0) {
1286                 if (rc == -ENOENT)
1287                         rc = 0;
1288                 GOTO(out_pop, rc);
1289         }
1290
1291         rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
1292         if (rc)
1293                 GOTO(out_close, rc);
1294
1295         rc = llog_process_or_fork(env, loghandle, mgs_steal_llog_handler,
1296                                   (void *)comp, NULL, false);
1297         CDEBUG(D_MGS, "steal llog re = %d\n", rc);
1298 out_close:
1299         llog_close(env, loghandle);
1300 out_pop:
1301         OBD_FREE_PTR(tmti);
1302 out_ctxt:
1303         llog_ctxt_put(ctxt);
1304         RETURN(rc);
1305 }
1306
1307 /* lmv is the second thing for client logs */
1308 /* copied from mgs_write_log_lov. Please refer to that.  */
1309 static int mgs_write_log_lmv(const struct lu_env *env,
1310                              struct mgs_device *mgs,
1311                              struct fs_db *fsdb,
1312                              struct mgs_target_info *mti,
1313                              char *logname, char *lmvname)
1314 {
1315         struct llog_handle *llh = NULL;
1316         struct lmv_desc *lmvdesc;
1317         char *uuid;
1318         int rc = 0;
1319         ENTRY;
1320
1321         CDEBUG(D_MGS, "Writing lmv(%s) log for %s\n", lmvname,logname);
1322
1323         OBD_ALLOC_PTR(lmvdesc);
1324         if (lmvdesc == NULL)
1325                 RETURN(-ENOMEM);
1326         lmvdesc->ld_active_tgt_count = 0;
1327         lmvdesc->ld_tgt_count = 0;
1328         sprintf((char*)lmvdesc->ld_uuid.uuid, "%s_UUID", lmvname);
1329         uuid = (char *)lmvdesc->ld_uuid.uuid;
1330
1331         rc = record_start_log(env, mgs, &llh, logname);
1332         if (rc)
1333                 GOTO(out_free, rc);
1334         rc = record_marker(env, llh, fsdb, CM_START, lmvname, "lmv setup");
1335         if (rc)
1336                 GOTO(out_end, rc);
1337         rc = record_attach(env, llh, lmvname, "lmv", uuid);
1338         if (rc)
1339                 GOTO(out_end, rc);
1340         rc = record_lmv_setup(env, llh, lmvname, lmvdesc);
1341         if (rc)
1342                 GOTO(out_end, rc);
1343         rc = record_marker(env, llh, fsdb, CM_END, lmvname, "lmv setup");
1344         if (rc)
1345                 GOTO(out_end, rc);
1346 out_end:
1347         record_end_log(env, &llh);
1348 out_free:
1349         OBD_FREE_PTR(lmvdesc);
1350         RETURN(rc);
1351 }
1352
1353 /* lov is the first thing in the mdt and client logs */
1354 static int mgs_write_log_lov(const struct lu_env *env, struct mgs_device *mgs,
1355                              struct fs_db *fsdb, struct mgs_target_info *mti,
1356                              char *logname, char *lovname)
1357 {
1358         struct llog_handle *llh = NULL;
1359         struct lov_desc *lovdesc;
1360         char *uuid;
1361         int rc = 0;
1362         ENTRY;
1363
1364         CDEBUG(D_MGS, "Writing lov(%s) log for %s\n", lovname, logname);
1365
1366         /*
1367         #01 L attach   0:lov_mdsA  1:lov  2:71ccb_lov_mdsA_19f961a9e1
1368         #02 L lov_setup 0:lov_mdsA 1:(struct lov_desc)
1369               uuid=lov1_UUID, stripe count=1, size=1048576, offset=0, pattern=0
1370         */
1371
1372         /* FIXME just make lov_setup accept empty desc (put uuid in buf 2) */
1373         OBD_ALLOC_PTR(lovdesc);
1374         if (lovdesc == NULL)
1375                 RETURN(-ENOMEM);
1376         lovdesc->ld_magic = LOV_DESC_MAGIC;
1377         lovdesc->ld_tgt_count = 0;
1378         /* Defaults.  Can be changed later by lcfg config_param */
1379         lovdesc->ld_default_stripe_count = 1;
1380         lovdesc->ld_pattern = LOV_PATTERN_RAID0;
1381         lovdesc->ld_default_stripe_size = 1024 * 1024;
1382         lovdesc->ld_default_stripe_offset = -1;
1383         lovdesc->ld_qos_maxage = QOS_DEFAULT_MAXAGE;
1384         sprintf((char*)lovdesc->ld_uuid.uuid, "%s_UUID", lovname);
1385         /* can these be the same? */
1386         uuid = (char *)lovdesc->ld_uuid.uuid;
1387
1388         /* This should always be the first entry in a log.
1389         rc = mgs_clear_log(obd, logname); */
1390         rc = record_start_log(env, mgs, &llh, logname);
1391         if (rc)
1392                 GOTO(out_free, rc);
1393         /* FIXME these should be a single journal transaction */
1394         rc = record_marker(env, llh, fsdb, CM_START, lovname, "lov setup");
1395         if (rc)
1396                 GOTO(out_end, rc);
1397         rc = record_attach(env, llh, lovname, "lov", uuid);
1398         if (rc)
1399                 GOTO(out_end, rc);
1400         rc = record_lov_setup(env, llh, lovname, lovdesc);
1401         if (rc)
1402                 GOTO(out_end, rc);
1403         rc = record_marker(env, llh, fsdb, CM_END, lovname, "lov setup");
1404         if (rc)
1405                 GOTO(out_end, rc);
1406         EXIT;
1407 out_end:
1408         record_end_log(env, &llh);
1409 out_free:
1410         OBD_FREE_PTR(lovdesc);
1411         return rc;
1412 }
1413
1414 /* add failnids to open log */
1415 static int mgs_write_log_failnids(const struct lu_env *env,
1416                                   struct mgs_target_info *mti,
1417                                   struct llog_handle *llh,
1418                                   char *cliname)
1419 {
1420         char *failnodeuuid = NULL;
1421         char *ptr = mti->mti_params;
1422         lnet_nid_t nid;
1423         int rc = 0;
1424
1425         /*
1426         #03 L add_uuid  nid=uml1@tcp(0x20000c0a80201) nal=90 0:  1:uml1_UUID
1427         #04 L add_uuid  nid=1@elan(0x1000000000001)   nal=90 0:  1:uml1_UUID
1428         #05 L setup    0:OSC_uml1_ost1_mdsA  1:ost1_UUID  2:uml1_UUID
1429         #06 L add_uuid  nid=uml2@tcp(0x20000c0a80202) nal=90 0:  1:uml2_UUID
1430         #0x L add_uuid  nid=2@elan(0x1000000000002)   nal=90 0:  1:uml2_UUID
1431         #07 L add_conn 0:OSC_uml1_ost1_mdsA  1:uml2_UUID
1432         */
1433
1434         /* Pull failnid info out of params string */
1435         while (class_find_param(ptr, PARAM_FAILNODE, &ptr) == 0) {
1436                 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
1437                         if (failnodeuuid == NULL) {
1438                                 /* We don't know the failover node name,
1439                                    so just use the first nid as the uuid */
1440                                 rc = name_create(&failnodeuuid,
1441                                                  libcfs_nid2str(nid), "");
1442                                 if (rc)
1443                                         return rc;
1444                         }
1445                         CDEBUG(D_MGS, "add nid %s for failover uuid %s, "
1446                                "client %s\n", libcfs_nid2str(nid),
1447                                failnodeuuid, cliname);
1448                         rc = record_add_uuid(env, llh, nid, failnodeuuid);
1449                 }
1450                 if (failnodeuuid)
1451                         rc = record_add_conn(env, llh, cliname, failnodeuuid);
1452         }
1453
1454         name_destroy(&failnodeuuid);
1455         return rc;
1456 }
1457
1458 static int mgs_write_log_mdc_to_lmv(const struct lu_env *env,
1459                                     struct mgs_device *mgs,
1460                                     struct fs_db *fsdb,
1461                                     struct mgs_target_info *mti,
1462                                     char *logname, char *lmvname)
1463 {
1464         struct llog_handle *llh = NULL;
1465         char *mdcname = NULL;
1466         char *nodeuuid = NULL;
1467         char *mdcuuid = NULL;
1468         char *lmvuuid = NULL;
1469         char index[6];
1470         int i, rc;
1471         ENTRY;
1472
1473         if (mgs_log_is_empty(env, mgs, logname)) {
1474                 CERROR("log is empty! Logical error\n");
1475                 RETURN(-EINVAL);
1476         }
1477
1478         CDEBUG(D_MGS, "adding mdc for %s to log %s:lmv(%s)\n",
1479                mti->mti_svname, logname, lmvname);
1480
1481         rc = name_create(&nodeuuid, libcfs_nid2str(mti->mti_nids[0]), "");
1482         if (rc)
1483                 RETURN(rc);
1484         rc = name_create(&mdcname, mti->mti_svname, "-mdc");
1485         if (rc)
1486                 GOTO(out_free, rc);
1487         rc = name_create(&mdcuuid, mdcname, "_UUID");
1488         if (rc)
1489                 GOTO(out_free, rc);
1490         rc = name_create(&lmvuuid, lmvname, "_UUID");
1491         if (rc)
1492                 GOTO(out_free, rc);
1493
1494         rc = record_start_log(env, mgs, &llh, logname);
1495         if (rc)
1496                 GOTO(out_free, rc);
1497         rc = record_marker(env, llh, fsdb, CM_START, mti->mti_svname,
1498                            "add mdc");
1499         if (rc)
1500                 GOTO(out_end, rc);
1501         for (i = 0; i < mti->mti_nid_count; i++) {
1502                 CDEBUG(D_MGS, "add nid %s for mdt\n",
1503                        libcfs_nid2str(mti->mti_nids[i]));
1504
1505                 rc = record_add_uuid(env, llh, mti->mti_nids[i], nodeuuid);
1506                 if (rc)
1507                         GOTO(out_end, rc);
1508         }
1509
1510         rc = record_attach(env, llh, mdcname, LUSTRE_MDC_NAME, lmvuuid);
1511         if (rc)
1512                 GOTO(out_end, rc);
1513         rc = record_setup(env, llh, mdcname, mti->mti_uuid, nodeuuid, 0, 0);
1514         if (rc)
1515                 GOTO(out_end, rc);
1516         rc = mgs_write_log_failnids(env, mti, llh, mdcname);
1517         if (rc)
1518                 GOTO(out_end, rc);
1519         snprintf(index, sizeof(index), "%d", mti->mti_stripe_index);
1520         rc = record_mdc_add(env, llh, lmvname, mdcuuid, mti->mti_uuid,
1521                             index, "1");
1522         if (rc)
1523                 GOTO(out_end, rc);
1524         rc = record_marker(env, llh, fsdb, CM_END, mti->mti_svname,
1525                            "add mdc");
1526         if (rc)
1527                 GOTO(out_end, rc);
1528 out_end:
1529         record_end_log(env, &llh);
1530 out_free:
1531         name_destroy(&lmvuuid);
1532         name_destroy(&mdcuuid);
1533         name_destroy(&mdcname);
1534         name_destroy(&nodeuuid);
1535         RETURN(rc);
1536 }
1537
1538 /* add new mdc to already existent MDS */
1539 static int mgs_write_log_mdc_to_mdt(const struct lu_env *env,
1540                                     struct mgs_device *mgs,
1541                                     struct fs_db *fsdb,
1542                                     struct mgs_target_info *mti,
1543                                     char *logname)
1544 {
1545         struct llog_handle *llh = NULL;
1546         char *nodeuuid = NULL;
1547         char *mdcname = NULL;
1548         char *mdcuuid = NULL;
1549         char *mdtuuid = NULL;
1550         int idx = mti->mti_stripe_index;
1551         char index[9];
1552         int i, rc;
1553
1554         ENTRY;
1555         if (mgs_log_is_empty(env, mgs, logname)) {
1556                 CERROR("log is empty! Logical error\n");
1557                 RETURN (-EINVAL);
1558         }
1559
1560         CDEBUG(D_MGS, "adding mdc index %d to %s\n", idx, logname);
1561
1562         rc = name_create(&nodeuuid, libcfs_nid2str(mti->mti_nids[0]), "");
1563         if (rc)
1564                 RETURN(rc);
1565         snprintf(index, sizeof(index), "-mdc%04x", idx);
1566         rc = name_create(&mdcname, logname, index);
1567         if (rc)
1568                 GOTO(out_free, rc);
1569         rc = name_create(&mdcuuid, mdcname, "_UUID");
1570         if (rc)
1571                 GOTO(out_free, rc);
1572         rc = name_create(&mdtuuid, logname, "_UUID");
1573         if (rc)
1574                 GOTO(out_free, rc);
1575
1576         rc = record_start_log(env, mgs, &llh, logname);
1577         if (rc)
1578                 GOTO(out_free, rc);
1579         rc = record_marker(env, llh, fsdb, CM_START, mti->mti_svname, "add mdc");
1580         if (rc)
1581                 GOTO(out_end, rc);
1582         for (i = 0; i < mti->mti_nid_count; i++) {
1583                 CDEBUG(D_MGS, "add nid %s for mdt\n",
1584                        libcfs_nid2str(mti->mti_nids[i]));
1585                 rc = record_add_uuid(env, llh, mti->mti_nids[i], nodeuuid);
1586                 if (rc)
1587                         GOTO(out_end, rc);
1588         }
1589         rc = record_attach(env, llh, mdcname, LUSTRE_MDC_NAME, mdcuuid);
1590         if (rc)
1591                 GOTO(out_end, rc);
1592         rc = record_setup(env, llh, mdcname, mti->mti_uuid, nodeuuid, 0, 0);
1593         if (rc)
1594                 GOTO(out_end, rc);
1595         rc = mgs_write_log_failnids(env, mti, llh, mdcname);
1596         if (rc)
1597                 GOTO(out_end, rc);
1598         snprintf(index, sizeof(index), "%d", idx);
1599
1600         rc = record_mdc_add(env, llh, logname, mdcuuid, mti->mti_uuid,
1601                             index, "1");
1602         if (rc)
1603                 GOTO(out_end, rc);
1604         rc = record_marker(env, llh, fsdb, CM_END, mti->mti_svname, "add mdc");
1605         if (rc)
1606                 GOTO(out_end, rc);
1607 out_end:
1608         record_end_log(env, &llh);
1609 out_free:
1610         name_destroy(&mdtuuid);
1611         name_destroy(&mdcuuid);
1612         name_destroy(&mdcname);
1613         name_destroy(&nodeuuid);
1614         RETURN(rc);
1615 }
1616
1617 static int mgs_write_log_mdt0(const struct lu_env *env,
1618                               struct mgs_device *mgs,
1619                               struct fs_db *fsdb,
1620                               struct mgs_target_info *mti)
1621 {
1622         char *log = mti->mti_svname;
1623         struct llog_handle *llh = NULL;
1624         char *uuid, *lovname;
1625         char mdt_index[6];
1626         char *ptr = mti->mti_params;
1627         int rc = 0, failout = 0;
1628         ENTRY;
1629
1630         OBD_ALLOC(uuid, sizeof(struct obd_uuid));
1631         if (uuid == NULL)
1632                 RETURN(-ENOMEM);
1633
1634         if (class_find_param(ptr, PARAM_FAILMODE, &ptr) == 0)
1635                 failout = (strncmp(ptr, "failout", 7) == 0);
1636
1637         rc = name_create(&lovname, log, "-mdtlov");
1638         if (rc)
1639                 GOTO(out_free, rc);
1640         if (mgs_log_is_empty(env, mgs, log)) {
1641                 rc = mgs_write_log_lov(env, mgs, fsdb, mti, log, lovname);
1642                 if (rc)
1643                         GOTO(out_lod, rc);
1644         }
1645
1646         sprintf(mdt_index, "%d", mti->mti_stripe_index);
1647
1648         rc = record_start_log(env, mgs, &llh, log);
1649         if (rc)
1650                 GOTO(out_lod, rc);
1651
1652         /* add MDT itself */
1653
1654         /* FIXME this whole fn should be a single journal transaction */
1655         sprintf(uuid, "%s_UUID", log);
1656         rc = record_marker(env, llh, fsdb, CM_START, log, "add mdt");
1657         if (rc)
1658                 GOTO(out_lod, rc);
1659         rc = record_attach(env, llh, log, LUSTRE_MDT_NAME, uuid);
1660         if (rc)
1661                 GOTO(out_end, rc);
1662         rc = record_mount_opt(env, llh, log, lovname, NULL);
1663         if (rc)
1664                 GOTO(out_end, rc);
1665         rc = record_setup(env, llh, log, uuid, mdt_index, lovname,
1666                         failout ? "n" : "f");
1667         if (rc)
1668                 GOTO(out_end, rc);
1669         rc = record_marker(env, llh, fsdb, CM_END, log, "add mdt");
1670         if (rc)
1671                 GOTO(out_end, rc);
1672 out_end:
1673         record_end_log(env, &llh);
1674 out_lod:
1675         name_destroy(&lovname);
1676 out_free:
1677         OBD_FREE(uuid, sizeof(struct obd_uuid));
1678         RETURN(rc);
1679 }
1680
1681 static inline int name_create_mdt(char **logname, char *fsname, int i)
1682 {
1683         char mdt_index[9];
1684
1685         sprintf(mdt_index, "-MDT%04x", i);
1686         return name_create(logname, fsname, mdt_index);
1687 }
1688
1689 static int name_create_mdt_and_lov(char **logname, char **lovname,
1690                                     struct fs_db *fsdb, int i)
1691 {
1692         int rc;
1693
1694         rc = name_create_mdt(logname, fsdb->fsdb_name, i);
1695         if (rc)
1696                 return rc;
1697         /* COMPAT_180 */
1698         if (i == 0 && cfs_test_bit(FSDB_OSCNAME18, &fsdb->fsdb_flags))
1699                 rc = name_create(lovname, fsdb->fsdb_name, "-mdtlov");
1700         else
1701                 rc = name_create(lovname, *logname, "-mdtlov");
1702         if (rc) {
1703                 name_destroy(logname);
1704                 *logname = NULL;
1705         }
1706         return rc;
1707 }
1708
1709 static inline int name_create_mdt_osc(char **oscname, char *ostname,
1710                                        struct fs_db *fsdb, int i)
1711 {
1712         char suffix[16];
1713
1714         if (i == 0 && cfs_test_bit(FSDB_OSCNAME18, &fsdb->fsdb_flags))
1715                 sprintf(suffix, "-osc");
1716         else
1717                 sprintf(suffix, "-osc-MDT%04x", i);
1718         return name_create(oscname, ostname, suffix);
1719 }
1720
1721 /* envelope method for all layers log */
1722 static int mgs_write_log_mdt(const struct lu_env *env,
1723                              struct mgs_device *mgs,
1724                              struct fs_db *fsdb,
1725                              struct mgs_target_info *mti)
1726 {
1727         struct mgs_thread_info *mgi = mgs_env_info(env);
1728         struct llog_handle *llh = NULL;
1729         char *cliname;
1730         int rc, i = 0;
1731         ENTRY;
1732
1733         CDEBUG(D_MGS, "writing new mdt %s\n", mti->mti_svname);
1734
1735         if (mti->mti_uuid[0] == '\0') {
1736                 /* Make up our own uuid */
1737                 snprintf(mti->mti_uuid, sizeof(mti->mti_uuid),
1738                          "%s_UUID", mti->mti_svname);
1739         }
1740
1741         /* add mdt */
1742         rc = mgs_write_log_mdt0(env, mgs, fsdb, mti);
1743         if (rc)
1744                 RETURN(rc);
1745         /* Append the mdt info to the client log */
1746         rc = name_create(&cliname, mti->mti_fsname, "-client");
1747         if (rc)
1748                 RETURN(rc);
1749
1750         if (mgs_log_is_empty(env, mgs, cliname)) {
1751                 /* Start client log */
1752                 rc = mgs_write_log_lov(env, mgs, fsdb, mti, cliname,
1753                                        fsdb->fsdb_clilov);
1754                 if (rc)
1755                         GOTO(out_free, rc);
1756                 rc = mgs_write_log_lmv(env, mgs, fsdb, mti, cliname,
1757                                        fsdb->fsdb_clilmv);
1758                 if (rc)
1759                         GOTO(out_free, rc);
1760         }
1761
1762         /*
1763         #09 L add_uuid nid=uml1@tcp(0x20000c0a80201) 0:  1:uml1_UUID
1764         #10 L attach   0:MDC_uml1_mdsA_MNT_client  1:mdc  2:1d834_MNT_client_03f
1765         #11 L setup    0:MDC_uml1_mdsA_MNT_client  1:mdsA_UUID  2:uml1_UUID
1766         #12 L add_uuid nid=uml2@tcp(0x20000c0a80202) 0:  1:uml2_UUID
1767         #13 L add_conn 0:MDC_uml1_mdsA_MNT_client  1:uml2_UUID
1768         #14 L mount_option 0:  1:client  2:lov1  3:MDC_uml1_mdsA_MNT_client
1769         */
1770
1771                 /* copy client info about lov/lmv */
1772                 mgi->mgi_comp.comp_mti = mti;
1773                 mgi->mgi_comp.comp_fsdb = fsdb;
1774
1775                 rc = mgs_steal_llog_for_mdt_from_client(env, mgs, cliname,
1776                                                         &mgi->mgi_comp);
1777                 if (rc)
1778                         GOTO(out_free, rc);
1779                 rc = mgs_write_log_mdc_to_lmv(env, mgs, fsdb, mti, cliname,
1780                                               fsdb->fsdb_clilmv);
1781                 if (rc)
1782                         GOTO(out_free, rc);
1783
1784                 /* add mountopts */
1785                 rc = record_start_log(env, mgs, &llh, cliname);
1786                 if (rc)
1787                         GOTO(out_free, rc);
1788
1789                 rc = record_marker(env, llh, fsdb, CM_START, cliname,
1790                                    "mount opts");
1791                 if (rc)
1792                         GOTO(out_end, rc);
1793                 rc = record_mount_opt(env, llh, cliname, fsdb->fsdb_clilov,
1794                                       fsdb->fsdb_clilmv);
1795                 if (rc)
1796                         GOTO(out_end, rc);
1797                 rc = record_marker(env, llh, fsdb, CM_END, cliname,
1798                                    "mount opts");
1799
1800         if (rc)
1801                 GOTO(out_end, rc);
1802         /* for_all_existing_mdt except current one */
1803         for (i = 0; i < INDEX_MAP_SIZE * 8; i++){
1804                 char *mdtname;
1805                 if (i !=  mti->mti_stripe_index &&
1806                     cfs_test_bit(i,  fsdb->fsdb_mdt_index_map)) {
1807                         rc = name_create_mdt(&mdtname, mti->mti_fsname, i);
1808                         if (rc)
1809                                 GOTO(out_end, rc);
1810                         rc = mgs_write_log_mdc_to_mdt(env, mgs, fsdb, mti, mdtname);
1811                         name_destroy(&mdtname);
1812                         if (rc)
1813                                 GOTO(out_end, rc);
1814                 }
1815         }
1816 out_end:
1817         record_end_log(env, &llh);
1818 out_free:
1819         name_destroy(&cliname);
1820         RETURN(rc);
1821 }
1822
1823 /* Add the ost info to the client/mdt lov */
1824 static int mgs_write_log_osc_to_lov(const struct lu_env *env,
1825                                     struct mgs_device *mgs, struct fs_db *fsdb,
1826                                     struct mgs_target_info *mti,
1827                                     char *logname, char *suffix, char *lovname,
1828                                     enum lustre_sec_part sec_part, int flags)
1829 {
1830         struct llog_handle *llh = NULL;
1831         char *nodeuuid = NULL;
1832         char *oscname = NULL;
1833         char *oscuuid = NULL;
1834         char *lovuuid = NULL;
1835         char *svname = NULL;
1836         char index[6];
1837         int i, rc;
1838
1839         ENTRY;
1840         CDEBUG(D_INFO, "adding osc for %s to log %s\n",
1841                mti->mti_svname, logname);
1842
1843         if (mgs_log_is_empty(env, mgs, logname)) {
1844                 CERROR("log is empty! Logical error\n");
1845                 RETURN (-EINVAL);
1846         }
1847
1848         rc = name_create(&nodeuuid, libcfs_nid2str(mti->mti_nids[0]), "");
1849         if (rc)
1850                 RETURN(rc);
1851         rc = name_create(&svname, mti->mti_svname, "-osc");
1852         if (rc)
1853                 GOTO(out_free, rc);
1854         rc = name_create(&oscname, svname, suffix);
1855         if (rc)
1856                 GOTO(out_free, rc);
1857         rc = name_create(&oscuuid, oscname, "_UUID");
1858         if (rc)
1859                 GOTO(out_free, rc);
1860         rc = name_create(&lovuuid, lovname, "_UUID");
1861         if (rc)
1862                 GOTO(out_free, rc);
1863
1864         /*
1865         #03 L add_uuid nid=uml1@tcp(0x20000c0a80201) 0:  1:uml1_UUID
1866         multihomed (#4)
1867         #04 L add_uuid  nid=1@elan(0x1000000000001)  nal=90 0:  1:uml1_UUID
1868         #04 L attach   0:OSC_uml1_ost1_MNT_client  1:osc  2:89070_lov1_a41dff51a
1869         #05 L setup    0:OSC_uml1_ost1_MNT_client  1:ost1_UUID  2:uml1_UUID
1870         failover (#6,7)
1871         #06 L add_uuid nid=uml2@tcp(0x20000c0a80202) 0:  1:uml2_UUID
1872         #07 L add_conn 0:OSC_uml1_ost1_MNT_client  1:uml2_UUID
1873         #08 L lov_modify_tgts add 0:lov1  1:ost1_UUID  2(index):0  3(gen):1
1874         */
1875
1876         rc = record_start_log(env, mgs, &llh, logname);
1877         if (rc)
1878                 GOTO(out_free, rc);
1879         /* FIXME these should be a single journal transaction */
1880         rc = record_marker(env, llh, fsdb, CM_START | flags, mti->mti_svname,
1881                            "add osc");
1882         if (rc)
1883                 GOTO(out_end, rc);
1884         for (i = 0; i < mti->mti_nid_count; i++) {
1885                 CDEBUG(D_MGS, "add nid %s\n", libcfs_nid2str(mti->mti_nids[i]));
1886                 rc = record_add_uuid(env, llh, mti->mti_nids[i], nodeuuid);
1887                 if (rc)
1888                         GOTO(out_end, rc);
1889         }
1890         rc = record_attach(env, llh, oscname, LUSTRE_OSC_NAME, lovuuid);
1891         if (rc)
1892                 GOTO(out_end, rc);
1893         rc = record_setup(env, llh, oscname, mti->mti_uuid, nodeuuid, 0, 0);
1894         if (rc)
1895                 GOTO(out_end, rc);
1896         rc = mgs_write_log_failnids(env, mti, llh, oscname);
1897         if (rc)
1898                 GOTO(out_end, rc);
1899         snprintf(index, sizeof(index), "%d", mti->mti_stripe_index);
1900         rc = record_lov_add(env, llh, lovname, mti->mti_uuid, index, "1");
1901         if (rc)
1902                 GOTO(out_end, rc);
1903         rc = record_marker(env, llh, fsdb, CM_END | flags, mti->mti_svname,
1904                            "add osc");
1905         if (rc)
1906                 GOTO(out_end, rc);
1907 out_end:
1908         record_end_log(env, &llh);
1909 out_free:
1910         name_destroy(&lovuuid);
1911         name_destroy(&oscuuid);
1912         name_destroy(&oscname);
1913         name_destroy(&svname);
1914         name_destroy(&nodeuuid);
1915         RETURN(rc);
1916 }
1917
1918 static int mgs_write_log_ost(const struct lu_env *env,
1919                              struct mgs_device *mgs, struct fs_db *fsdb,
1920                              struct mgs_target_info *mti)
1921 {
1922         struct llog_handle *llh = NULL;
1923         char *logname, *lovname;
1924         char *ptr = mti->mti_params;
1925         int rc, flags = 0, failout = 0, i;
1926         ENTRY;
1927
1928         CDEBUG(D_MGS, "writing new ost %s\n", mti->mti_svname);
1929
1930         /* The ost startup log */
1931
1932         /* If the ost log already exists, that means that someone reformatted
1933            the ost and it called target_add again. */
1934         if (!mgs_log_is_empty(env, mgs, mti->mti_svname)) {
1935                 LCONSOLE_ERROR_MSG(0x141, "The config log for %s already "
1936                                    "exists, yet the server claims it never "
1937                                    "registered. It may have been reformatted, "
1938                                    "or the index changed. writeconf the MDT to "
1939                                    "regenerate all logs.\n", mti->mti_svname);
1940                 RETURN(-EALREADY);
1941         }
1942
1943         /*
1944         attach obdfilter ost1 ost1_UUID
1945         setup /dev/loop2 ldiskfs f|n errors=remount-ro,user_xattr
1946         */
1947         if (class_find_param(ptr, PARAM_FAILMODE, &ptr) == 0)
1948                 failout = (strncmp(ptr, "failout", 7) == 0);
1949         rc = record_start_log(env, mgs, &llh, mti->mti_svname);
1950         if (rc)
1951                 RETURN(rc);
1952         /* FIXME these should be a single journal transaction */
1953         rc = record_marker(env, llh, fsdb, CM_START, mti->mti_svname,"add ost");
1954         if (rc)
1955                 GOTO(out_end, rc);
1956         if (*mti->mti_uuid == '\0')
1957                 snprintf(mti->mti_uuid, sizeof(mti->mti_uuid),
1958                          "%s_UUID", mti->mti_svname);
1959         rc = record_attach(env, llh, mti->mti_svname,
1960                            "obdfilter"/*LUSTRE_OST_NAME*/, mti->mti_uuid);
1961         if (rc)
1962                 GOTO(out_end, rc);
1963         rc = record_setup(env, llh, mti->mti_svname,
1964                           "dev"/*ignored*/, "type"/*ignored*/,
1965                           failout ? "n" : "f", 0/*options*/);
1966         if (rc)
1967                 GOTO(out_end, rc);
1968         rc = record_marker(env, llh, fsdb, CM_END, mti->mti_svname, "add ost");
1969         if (rc)
1970                 GOTO(out_end, rc);
1971 out_end:
1972         record_end_log(env, &llh);
1973         if (rc)
1974                 RETURN(rc);
1975         /* We also have to update the other logs where this osc is part of
1976            the lov */
1977
1978         if (cfs_test_bit(FSDB_OLDLOG14, &fsdb->fsdb_flags)) {
1979                 /* If we're upgrading, the old mdt log already has our
1980                    entry. Let's do a fake one for fun. */
1981                 /* Note that we can't add any new failnids, since we don't
1982                    know the old osc names. */
1983                 flags = CM_SKIP | CM_UPGRADE146;
1984
1985         } else if ((mti->mti_flags & LDD_F_UPDATE) != LDD_F_UPDATE) {
1986                 /* If the update flag isn't set, don't update client/mdt
1987                    logs. */
1988                 flags |= CM_SKIP;
1989                 LCONSOLE_WARN("Client log for %s was not updated; writeconf "
1990                               "the MDT first to regenerate it.\n",
1991                               mti->mti_svname);
1992         }
1993
1994         /* Add ost to all MDT lov defs */
1995         for (i = 0; i < INDEX_MAP_SIZE * 8; i++){
1996                 if (cfs_test_bit(i, fsdb->fsdb_mdt_index_map)) {
1997                         char mdt_index[9];
1998
1999                         rc = name_create_mdt_and_lov(&logname, &lovname, fsdb,
2000                                                      i);
2001                         if (rc)
2002                                 RETURN(rc);
2003                         sprintf(mdt_index, "-MDT%04x", i);
2004                         rc = mgs_write_log_osc_to_lov(env, mgs, fsdb, mti,
2005                                                       logname, mdt_index,
2006                                                       lovname, LUSTRE_SP_MDT,
2007                                                       flags);
2008                         name_destroy(&logname);
2009                         name_destroy(&lovname);
2010                         if (rc)
2011                                 RETURN(rc);
2012                 }
2013         }
2014
2015         /* Append ost info to the client log */
2016         rc = name_create(&logname, mti->mti_fsname, "-client");
2017         if (rc)
2018                 RETURN(rc);
2019         if (mgs_log_is_empty(env, mgs, logname)) {
2020                 /* Start client log */
2021                 rc = mgs_write_log_lov(env, mgs, fsdb, mti, logname,
2022                                        fsdb->fsdb_clilov);
2023                 if (rc)
2024                         GOTO(out_free, rc);
2025                 rc = mgs_write_log_lmv(env, mgs, fsdb, mti, logname,
2026                                        fsdb->fsdb_clilmv);
2027                 if (rc)
2028                         GOTO(out_free, rc);
2029         }
2030         rc = mgs_write_log_osc_to_lov(env, mgs, fsdb, mti, logname, "",
2031                                       fsdb->fsdb_clilov, LUSTRE_SP_CLI, 0);
2032 out_free:
2033         name_destroy(&logname);
2034         RETURN(rc);
2035 }
2036
2037 static __inline__ int mgs_param_empty(char *ptr)
2038 {
2039         char *tmp;
2040
2041         if ((tmp = strchr(ptr, '=')) && (*(++tmp) == '\0'))
2042                 return 1;
2043         return 0;
2044 }
2045
2046 static int mgs_write_log_failnid_internal(const struct lu_env *env,
2047                                           struct mgs_device *mgs,
2048                                           struct fs_db *fsdb,
2049                                           struct mgs_target_info *mti,
2050                                           char *logname, char *cliname)
2051 {
2052         int rc;
2053         struct llog_handle *llh = NULL;
2054
2055         if (mgs_param_empty(mti->mti_params)) {
2056                 /* Remove _all_ failnids */
2057                 rc = mgs_modify(env, mgs, fsdb, mti, logname,
2058                                 mti->mti_svname, "add failnid", CM_SKIP);
2059                 return rc < 0 ? rc : 0;
2060         }
2061
2062         /* Otherwise failover nids are additive */
2063         rc = record_start_log(env, mgs, &llh, logname);
2064         if (rc)
2065                 return rc;
2066                 /* FIXME this should be a single journal transaction */
2067         rc = record_marker(env, llh, fsdb, CM_START, mti->mti_svname,
2068                            "add failnid");
2069         if (rc)
2070                 goto out_end;
2071         rc = mgs_write_log_failnids(env, mti, llh, cliname);
2072         if (rc)
2073                 goto out_end;
2074         rc = record_marker(env, llh, fsdb, CM_END,
2075                            mti->mti_svname, "add failnid");
2076 out_end:
2077         record_end_log(env, &llh);
2078         return rc;
2079 }
2080
2081
2082 /* Add additional failnids to an existing log.
2083    The mdc/osc must have been added to logs first */
2084 /* tcp nids must be in dotted-quad ascii -
2085    we can't resolve hostnames from the kernel. */
2086 static int mgs_write_log_add_failnid(const struct lu_env *env,
2087                                      struct mgs_device *mgs,
2088                                      struct fs_db *fsdb,
2089                                      struct mgs_target_info *mti)
2090 {
2091         char *logname, *cliname;
2092         int rc;
2093         ENTRY;
2094
2095         /* FIXME we currently can't erase the failnids
2096          * given when a target first registers, since they aren't part of
2097          * an "add uuid" stanza */
2098
2099         /* Verify that we know about this target */
2100         if (mgs_log_is_empty(env, mgs, mti->mti_svname)) {
2101                 LCONSOLE_ERROR_MSG(0x142, "The target %s has not registered "
2102                                    "yet. It must be started before failnids "
2103                                    "can be added.\n", mti->mti_svname);
2104                 RETURN(-ENOENT);
2105         }
2106
2107         /* Create mdc/osc client name (e.g. lustre-OST0001-osc) */
2108         if (mti->mti_flags & LDD_F_SV_TYPE_MDT) {
2109                 rc = name_create(&cliname, mti->mti_svname, "-mdc");
2110         } else if (mti->mti_flags & LDD_F_SV_TYPE_OST) {
2111                 rc = name_create(&cliname, mti->mti_svname, "-osc");
2112         } else {
2113                 RETURN(-EINVAL);
2114         }
2115         if (rc)
2116                 RETURN(rc);
2117         /* Add failover nids to the client log */
2118         rc = name_create(&logname, mti->mti_fsname, "-client");
2119         if (rc) {
2120                 name_destroy(&cliname);
2121                 RETURN(rc);
2122         }
2123         rc = mgs_write_log_failnid_internal(env, mgs, fsdb,mti,logname,cliname);
2124         name_destroy(&logname);
2125         name_destroy(&cliname);
2126         if (rc)
2127                 RETURN(rc);
2128
2129         if (mti->mti_flags & LDD_F_SV_TYPE_OST) {
2130                 /* Add OST failover nids to the MDT logs as well */
2131                 int i;
2132
2133                 for (i = 0; i < INDEX_MAP_SIZE * 8; i++) {
2134                         if (!cfs_test_bit(i, fsdb->fsdb_mdt_index_map))
2135                                 continue;
2136                         rc = name_create_mdt(&logname, mti->mti_fsname, i);
2137                         if (rc)
2138                                 RETURN(rc);
2139                         rc = name_create_mdt_osc(&cliname, mti->mti_svname,
2140                                                  fsdb, i);
2141                         if (rc) {
2142                                 name_destroy(&logname);
2143                                 RETURN(rc);
2144                         }
2145                         rc = mgs_write_log_failnid_internal(env, mgs, fsdb,
2146                                                             mti, logname,
2147                                                             cliname);
2148                         name_destroy(&cliname);
2149                         name_destroy(&logname);
2150                         if (rc)
2151                                 RETURN(rc);
2152                 }
2153         }
2154
2155         RETURN(rc);
2156 }
2157
2158 static int mgs_wlp_lcfg(const struct lu_env *env,
2159                         struct mgs_device *mgs, struct fs_db *fsdb,
2160                         struct mgs_target_info *mti,
2161                         char *logname, struct lustre_cfg_bufs *bufs,
2162                         char *tgtname, char *ptr)
2163 {
2164         char comment[MTI_NAME_MAXLEN];
2165         char *tmp;
2166         struct lustre_cfg *lcfg;
2167         int rc, del;
2168
2169         /* Erase any old settings of this same parameter */
2170         memcpy(comment, ptr, MTI_NAME_MAXLEN);
2171         comment[MTI_NAME_MAXLEN - 1] = 0;
2172         /* But don't try to match the value. */
2173         if ((tmp = strchr(comment, '=')))
2174             *tmp = 0;
2175         /* FIXME we should skip settings that are the same as old values */
2176         rc = mgs_modify(env, mgs, fsdb, mti, logname, tgtname, comment,CM_SKIP);
2177         if (rc < 0)
2178                 return rc;
2179         del = mgs_param_empty(ptr);
2180
2181         LCONSOLE_INFO("%sing parameter %s.%s in log %s\n", del ? "Disabl" : rc ?
2182                       "Sett" : "Modify", tgtname, comment, logname);
2183         if (del)
2184                 return rc;
2185
2186         lustre_cfg_bufs_reset(bufs, tgtname);
2187         lustre_cfg_bufs_set_string(bufs, 1, ptr);
2188         lcfg = lustre_cfg_new(LCFG_PARAM, bufs);
2189         if (!lcfg)
2190                 return -ENOMEM;
2191         rc = mgs_write_log_direct(env, mgs, fsdb, logname,lcfg,tgtname,comment);
2192         lustre_cfg_free(lcfg);
2193         return rc;
2194 }
2195
2196 /* write global variable settings into log */
2197 static int mgs_write_log_sys(const struct lu_env *env,
2198                              struct mgs_device *mgs, struct fs_db *fsdb,
2199                              struct mgs_target_info *mti, char *sys, char *ptr)
2200 {
2201         struct mgs_thread_info *mgi = mgs_env_info(env);
2202         struct lustre_cfg *lcfg;
2203         char *tmp, sep;
2204         int rc, cmd, convert = 1;
2205
2206         if (class_match_param(ptr, PARAM_TIMEOUT, &tmp) == 0) {
2207                 cmd = LCFG_SET_TIMEOUT;
2208         } else if (class_match_param(ptr, PARAM_LDLM_TIMEOUT, &tmp) == 0) {
2209                 cmd = LCFG_SET_LDLM_TIMEOUT;
2210         /* Check for known params here so we can return error to lctl */
2211         } else if ((class_match_param(ptr, PARAM_AT_MIN, &tmp) == 0) ||
2212                 (class_match_param(ptr, PARAM_AT_MAX, &tmp) == 0) ||
2213                 (class_match_param(ptr, PARAM_AT_EXTRA, &tmp) == 0) ||
2214                 (class_match_param(ptr, PARAM_AT_EARLY_MARGIN, &tmp) == 0) ||
2215                 (class_match_param(ptr, PARAM_AT_HISTORY, &tmp) == 0)) {
2216                 cmd = LCFG_PARAM;
2217         } else if (class_match_param(ptr, PARAM_JOBID_VAR, &tmp) == 0) {
2218                 convert = 0; /* Don't convert string value to integer */
2219                 cmd = LCFG_PARAM;
2220         } else {
2221                 return -EINVAL;
2222         }
2223
2224         if (mgs_param_empty(ptr))
2225                 CDEBUG(D_MGS, "global '%s' removed\n", sys);
2226         else
2227                 CDEBUG(D_MGS, "global '%s' val=%s\n", sys, tmp);
2228
2229         lustre_cfg_bufs_reset(&mgi->mgi_bufs, NULL);
2230         lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 1, sys);
2231         if (!convert && *tmp != '\0')
2232                 lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 2, tmp);
2233         lcfg = lustre_cfg_new(cmd, &mgi->mgi_bufs);
2234         lcfg->lcfg_num = convert ? simple_strtoul(tmp, NULL, 0) : 0;
2235         /* truncate the comment to the parameter name */
2236         ptr = tmp - 1;
2237         sep = *ptr;
2238         *ptr = '\0';
2239         /* modify all servers and clients */
2240         rc = mgs_write_log_direct_all(env, mgs, fsdb, mti,
2241                                       *tmp == '\0' ? NULL : lcfg,
2242                                       mti->mti_fsname, sys, 0);
2243         if (rc == 0 && *tmp != '\0') {
2244                 switch (cmd) {
2245                 case LCFG_SET_TIMEOUT:
2246                         if (!obd_timeout_set || lcfg->lcfg_num > obd_timeout)
2247                                 class_process_config(lcfg);
2248                         break;
2249                 case LCFG_SET_LDLM_TIMEOUT:
2250                         if (!ldlm_timeout_set || lcfg->lcfg_num > ldlm_timeout)
2251                                 class_process_config(lcfg);
2252                         break;
2253                 default:
2254                         break;
2255                 }
2256         }
2257         *ptr = sep;
2258         lustre_cfg_free(lcfg);
2259         return rc;
2260 }
2261
2262 /* write quota settings into log */
2263 static int mgs_write_log_quota(const struct lu_env *env, struct mgs_device *mgs,
2264                                struct fs_db *fsdb, struct mgs_target_info *mti,
2265                                char *quota, char *ptr)
2266 {
2267         struct lustre_cfg_bufs bufs;
2268         struct lustre_cfg *lcfg;
2269         char *tmp;
2270         char sep;
2271         int cmd = LCFG_PARAM;
2272         int rc;
2273
2274         /* support only 'meta' and 'data' pools so far */
2275         if (class_match_param(ptr, QUOTA_METAPOOL_NAME, &tmp) != 0 &&
2276             class_match_param(ptr, QUOTA_DATAPOOL_NAME, &tmp) != 0) {
2277                 CERROR("parameter quota.%s isn't supported (only quota.mdt "
2278                        "& quota.ost are)\n", ptr);
2279                 return -EINVAL;
2280         }
2281
2282         if (*tmp == '\0') {
2283                 CDEBUG(D_MGS, "global '%s' removed\n", quota);
2284         } else {
2285                 CDEBUG(D_MGS, "global '%s'\n", quota);
2286
2287                 if (strchr(tmp, 'u') == NULL && strchr(tmp, 'g') == NULL &&
2288                     strcmp(tmp, "none") != 0) {
2289                         CERROR("enable option(%s) isn't supported\n", tmp);
2290                         return -EINVAL;
2291                 }
2292         }
2293
2294         lustre_cfg_bufs_reset(&bufs, NULL);
2295         lustre_cfg_bufs_set_string(&bufs, 1, quota);
2296         lcfg = lustre_cfg_new(cmd, &bufs);
2297         /* truncate the comment to the parameter name */
2298         ptr = tmp - 1;
2299         sep = *ptr;
2300         *ptr = '\0';
2301
2302         /* XXX we duplicated quota enable information in all server
2303          *     config logs, it should be moved to a separate config
2304          *     log once we cleanup the config log for global param. */
2305         /* modify all servers */
2306         rc = mgs_write_log_direct_all(env, mgs, fsdb, mti,
2307                                       *tmp == '\0' ? NULL : lcfg,
2308                                       mti->mti_fsname, quota, 1);
2309         *ptr = sep;
2310         lustre_cfg_free(lcfg);
2311         return rc;
2312 }
2313
2314 static int mgs_srpc_set_param_disk(const struct lu_env *env,
2315                                    struct mgs_device *mgs,
2316                                    struct fs_db *fsdb,
2317                                    struct mgs_target_info *mti,
2318                                    char *param)
2319 {
2320         struct mgs_thread_info *mgi = mgs_env_info(env);
2321         struct llog_handle     *llh = NULL;
2322         char                   *logname;
2323         char                   *comment, *ptr;
2324         struct lustre_cfg      *lcfg;
2325         int                     rc, len;
2326         ENTRY;
2327
2328         /* get comment */
2329         ptr = strchr(param, '=');
2330         LASSERT(ptr);
2331         len = ptr - param;
2332
2333         OBD_ALLOC(comment, len + 1);
2334         if (comment == NULL)
2335                 RETURN(-ENOMEM);
2336         strncpy(comment, param, len);
2337         comment[len] = '\0';
2338
2339         /* prepare lcfg */
2340         lustre_cfg_bufs_reset(&mgi->mgi_bufs, mti->mti_svname);
2341         lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 1, param);
2342         lcfg = lustre_cfg_new(LCFG_SPTLRPC_CONF, &mgi->mgi_bufs);
2343         if (lcfg == NULL)
2344                 GOTO(out_comment, rc = -ENOMEM);
2345
2346         /* construct log name */
2347         rc = name_create(&logname, mti->mti_fsname, "-sptlrpc");
2348         if (rc)
2349                 GOTO(out_lcfg, rc);
2350
2351         if (mgs_log_is_empty(env, mgs, logname)) {
2352                 rc = record_start_log(env, mgs, &llh, logname);
2353                 if (rc)
2354                         GOTO(out, rc);
2355                 record_end_log(env, &llh);
2356         }
2357
2358         /* obsolete old one */
2359         rc = mgs_modify(env, mgs, fsdb, mti, logname, mti->mti_svname,
2360                         comment, CM_SKIP);
2361         if (rc < 0)
2362                 GOTO(out, rc);
2363         /* write the new one */
2364         rc = mgs_write_log_direct(env, mgs, fsdb, logname, lcfg,
2365                                   mti->mti_svname, comment);
2366         if (rc)
2367                 CERROR("err %d writing log %s\n", rc, logname);
2368 out:
2369         name_destroy(&logname);
2370 out_lcfg:
2371         lustre_cfg_free(lcfg);
2372 out_comment:
2373         OBD_FREE(comment, len + 1);
2374         RETURN(rc);
2375 }
2376
2377 static int mgs_srpc_set_param_udesc_mem(struct fs_db *fsdb,
2378                                         char *param)
2379 {
2380         char    *ptr;
2381
2382         /* disable the adjustable udesc parameter for now, i.e. use default
2383          * setting that client always ship udesc to MDT if possible. to enable
2384          * it simply remove the following line */
2385         goto error_out;
2386
2387         ptr = strchr(param, '=');
2388         if (ptr == NULL)
2389                 goto error_out;
2390         *ptr++ = '\0';
2391
2392         if (strcmp(param, PARAM_SRPC_UDESC))
2393                 goto error_out;
2394
2395         if (strcmp(ptr, "yes") == 0) {
2396                 cfs_set_bit(FSDB_UDESC, &fsdb->fsdb_flags);
2397                 CWARN("Enable user descriptor shipping from client to MDT\n");
2398         } else if (strcmp(ptr, "no") == 0) {
2399                 cfs_clear_bit(FSDB_UDESC, &fsdb->fsdb_flags);
2400                 CWARN("Disable user descriptor shipping from client to MDT\n");
2401         } else {
2402                 *(ptr - 1) = '=';
2403                 goto error_out;
2404         }
2405         return 0;
2406
2407 error_out:
2408         CERROR("Invalid param: %s\n", param);
2409         return -EINVAL;
2410 }
2411
2412 static int mgs_srpc_set_param_mem(struct fs_db *fsdb,
2413                                   const char *svname,
2414                                   char *param)
2415 {
2416         struct sptlrpc_rule      rule;
2417         struct sptlrpc_rule_set *rset;
2418         int                      rc;
2419         ENTRY;
2420
2421         if (strncmp(param, PARAM_SRPC, sizeof(PARAM_SRPC) - 1) != 0) {
2422                 CERROR("Invalid sptlrpc parameter: %s\n", param);
2423                 RETURN(-EINVAL);
2424         }
2425
2426         if (strncmp(param, PARAM_SRPC_UDESC,
2427                     sizeof(PARAM_SRPC_UDESC) - 1) == 0) {
2428                 RETURN(mgs_srpc_set_param_udesc_mem(fsdb, param));
2429         }
2430
2431         if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
2432                 CERROR("Invalid sptlrpc flavor parameter: %s\n", param);
2433                 RETURN(-EINVAL);
2434         }
2435
2436         param += sizeof(PARAM_SRPC_FLVR) - 1;
2437
2438         rc = sptlrpc_parse_rule(param, &rule);
2439         if (rc)
2440                 RETURN(rc);
2441
2442         /* mgs rules implies must be mgc->mgs */
2443         if (cfs_test_bit(FSDB_MGS_SELF, &fsdb->fsdb_flags)) {
2444                 if ((rule.sr_from != LUSTRE_SP_MGC &&
2445                      rule.sr_from != LUSTRE_SP_ANY) ||
2446                     (rule.sr_to != LUSTRE_SP_MGS &&
2447                      rule.sr_to != LUSTRE_SP_ANY))
2448                         RETURN(-EINVAL);
2449         }
2450
2451         /* preapre room for this coming rule. svcname format should be:
2452          * - fsname: general rule
2453          * - fsname-tgtname: target-specific rule
2454          */
2455         if (strchr(svname, '-')) {
2456                 struct mgs_tgt_srpc_conf *tgtconf;
2457                 int                       found = 0;
2458
2459                 for (tgtconf = fsdb->fsdb_srpc_tgt; tgtconf != NULL;
2460                      tgtconf = tgtconf->mtsc_next) {
2461                         if (!strcmp(tgtconf->mtsc_tgt, svname)) {
2462                                 found = 1;
2463                                 break;
2464                         }
2465                 }
2466
2467                 if (!found) {
2468                         int name_len;
2469
2470                         OBD_ALLOC_PTR(tgtconf);
2471                         if (tgtconf == NULL)
2472                                 RETURN(-ENOMEM);
2473
2474                         name_len = strlen(svname);
2475
2476                         OBD_ALLOC(tgtconf->mtsc_tgt, name_len + 1);
2477                         if (tgtconf->mtsc_tgt == NULL) {
2478                                 OBD_FREE_PTR(tgtconf);
2479                                 RETURN(-ENOMEM);
2480                         }
2481                         memcpy(tgtconf->mtsc_tgt, svname, name_len);
2482
2483                         tgtconf->mtsc_next = fsdb->fsdb_srpc_tgt;
2484                         fsdb->fsdb_srpc_tgt = tgtconf;
2485                 }
2486
2487                 rset = &tgtconf->mtsc_rset;
2488         } else {
2489                 rset = &fsdb->fsdb_srpc_gen;
2490         }
2491
2492         rc = sptlrpc_rule_set_merge(rset, &rule);
2493
2494         RETURN(rc);
2495 }
2496
2497 static int mgs_srpc_set_param(const struct lu_env *env,
2498                               struct mgs_device *mgs,
2499                               struct fs_db *fsdb,
2500                               struct mgs_target_info *mti,
2501                               char *param)
2502 {
2503         char                   *copy;
2504         int                     rc, copy_size;
2505         ENTRY;
2506
2507 #ifndef HAVE_GSS
2508         RETURN(-EINVAL);
2509 #endif
2510         /* keep a copy of original param, which could be destroied
2511          * during parsing */
2512         copy_size = strlen(param) + 1;
2513         OBD_ALLOC(copy, copy_size);
2514         if (copy == NULL)
2515                 return -ENOMEM;
2516         memcpy(copy, param, copy_size);
2517
2518         rc = mgs_srpc_set_param_mem(fsdb, mti->mti_svname, param);
2519         if (rc)
2520                 goto out_free;
2521
2522         /* previous steps guaranteed the syntax is correct */
2523         rc = mgs_srpc_set_param_disk(env, mgs, fsdb, mti, copy);
2524         if (rc)
2525                 goto out_free;
2526
2527         if (cfs_test_bit(FSDB_MGS_SELF, &fsdb->fsdb_flags)) {
2528                 /*
2529                  * for mgs rules, make them effective immediately.
2530                  */
2531                 LASSERT(fsdb->fsdb_srpc_tgt == NULL);
2532                 sptlrpc_target_update_exp_flavor(mgs->mgs_obd,
2533                                                  &fsdb->fsdb_srpc_gen);
2534         }
2535
2536 out_free:
2537         OBD_FREE(copy, copy_size);
2538         RETURN(rc);
2539 }
2540
2541 struct mgs_srpc_read_data {
2542         struct fs_db   *msrd_fsdb;
2543         int             msrd_skip;
2544 };
2545
2546 static int mgs_srpc_read_handler(const struct lu_env *env,
2547                                  struct llog_handle *llh,
2548                                  struct llog_rec_hdr *rec, void *data)
2549 {
2550         struct mgs_srpc_read_data *msrd = data;
2551         struct cfg_marker         *marker;
2552         struct lustre_cfg         *lcfg = (struct lustre_cfg *)(rec + 1);
2553         char                      *svname, *param;
2554         int                        cfg_len, rc;
2555         ENTRY;
2556
2557         if (rec->lrh_type != OBD_CFG_REC) {
2558                 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
2559                 RETURN(-EINVAL);
2560         }
2561
2562         cfg_len = rec->lrh_len - sizeof(struct llog_rec_hdr) -
2563                   sizeof(struct llog_rec_tail);
2564
2565         rc = lustre_cfg_sanity_check(lcfg, cfg_len);
2566         if (rc) {
2567                 CERROR("Insane cfg\n");
2568                 RETURN(rc);
2569         }
2570
2571         if (lcfg->lcfg_command == LCFG_MARKER) {
2572                 marker = lustre_cfg_buf(lcfg, 1);
2573
2574                 if (marker->cm_flags & CM_START &&
2575                     marker->cm_flags & CM_SKIP)
2576                         msrd->msrd_skip = 1;
2577                 if (marker->cm_flags & CM_END)
2578                         msrd->msrd_skip = 0;
2579
2580                 RETURN(0);
2581         }
2582
2583         if (msrd->msrd_skip)
2584                 RETURN(0);
2585
2586         if (lcfg->lcfg_command != LCFG_SPTLRPC_CONF) {
2587                 CERROR("invalid command (%x)\n", lcfg->lcfg_command);
2588                 RETURN(0);
2589         }
2590
2591         svname = lustre_cfg_string(lcfg, 0);
2592         if (svname == NULL) {
2593                 CERROR("svname is empty\n");
2594                 RETURN(0);
2595         }
2596
2597         param = lustre_cfg_string(lcfg, 1);
2598         if (param == NULL) {
2599                 CERROR("param is empty\n");
2600                 RETURN(0);
2601         }
2602
2603         rc = mgs_srpc_set_param_mem(msrd->msrd_fsdb, svname, param);
2604         if (rc)
2605                 CERROR("read sptlrpc record error (%d): %s\n", rc, param);
2606
2607         RETURN(0);
2608 }
2609
2610 int mgs_get_fsdb_srpc_from_llog(const struct lu_env *env,
2611                                 struct mgs_device *mgs,
2612                                 struct fs_db *fsdb)
2613 {
2614         struct llog_handle        *llh = NULL;
2615         struct llog_ctxt          *ctxt;
2616         char                      *logname;
2617         struct mgs_srpc_read_data  msrd;
2618         int                        rc;
2619         ENTRY;
2620
2621         /* construct log name */
2622         rc = name_create(&logname, fsdb->fsdb_name, "-sptlrpc");
2623         if (rc)
2624                 RETURN(rc);
2625
2626         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
2627         LASSERT(ctxt != NULL);
2628
2629         if (mgs_log_is_empty(env, mgs, logname))
2630                 GOTO(out, rc = 0);
2631
2632         rc = llog_open(env, ctxt, &llh, NULL, logname,
2633                        LLOG_OPEN_EXISTS);
2634         if (rc < 0) {
2635                 if (rc == -ENOENT)
2636                         rc = 0;
2637                 GOTO(out, rc);
2638         }
2639
2640         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
2641         if (rc)
2642                 GOTO(out_close, rc);
2643
2644         if (llog_get_size(llh) <= 1)
2645                 GOTO(out_close, rc = 0);
2646
2647         msrd.msrd_fsdb = fsdb;
2648         msrd.msrd_skip = 0;
2649
2650         rc = llog_process(env, llh, mgs_srpc_read_handler, (void *)&msrd,
2651                           NULL);
2652
2653 out_close:
2654         llog_close(env, llh);
2655 out:
2656         llog_ctxt_put(ctxt);
2657         name_destroy(&logname);
2658
2659         if (rc)
2660                 CERROR("failed to read sptlrpc config database: %d\n", rc);
2661         RETURN(rc);
2662 }
2663
2664 /* Permanent settings of all parameters by writing into the appropriate
2665  * configuration logs.
2666  * A parameter with null value ("<param>='\0'") means to erase it out of
2667  * the logs.
2668  */
2669 static int mgs_write_log_param(const struct lu_env *env,
2670                                struct mgs_device *mgs, struct fs_db *fsdb,
2671                                struct mgs_target_info *mti, char *ptr)
2672 {
2673         struct mgs_thread_info *mgi = mgs_env_info(env);
2674         char *logname;
2675         char *tmp;
2676         int rc = 0, rc2 = 0;
2677         ENTRY;
2678
2679         /* For various parameter settings, we have to figure out which logs
2680            care about them (e.g. both mdt and client for lov settings) */
2681         CDEBUG(D_MGS, "next param '%s'\n", ptr);
2682
2683         /* The params are stored in MOUNT_DATA_FILE and modified via
2684            tunefs.lustre, or set using lctl conf_param */
2685
2686         /* Processed in lustre_start_mgc */
2687         if (class_match_param(ptr, PARAM_MGSNODE, NULL) == 0)
2688                 GOTO(end, rc);
2689
2690         /* Processed in ost/mdt */
2691         if (class_match_param(ptr, PARAM_NETWORK, NULL) == 0)
2692                 GOTO(end, rc);
2693
2694         /* Processed in mgs_write_log_ost */
2695         if (class_match_param(ptr, PARAM_FAILMODE, NULL) == 0) {
2696                 if (mti->mti_flags & LDD_F_PARAM) {
2697                         LCONSOLE_ERROR_MSG(0x169, "%s can only be "
2698                                            "changed with tunefs.lustre"
2699                                            "and --writeconf\n", ptr);
2700                         rc = -EPERM;
2701                 }
2702                 GOTO(end, rc);
2703         }
2704
2705         if (class_match_param(ptr, PARAM_SRPC, NULL) == 0) {
2706                 rc = mgs_srpc_set_param(env, mgs, fsdb, mti, ptr);
2707                 GOTO(end, rc);
2708         }
2709
2710         if (class_match_param(ptr, PARAM_FAILNODE, NULL) == 0) {
2711                 /* Add a failover nidlist */
2712                 rc = 0;
2713                 /* We already processed failovers params for new
2714                    targets in mgs_write_log_target */
2715                 if (mti->mti_flags & LDD_F_PARAM) {
2716                         CDEBUG(D_MGS, "Adding failnode\n");
2717                         rc = mgs_write_log_add_failnid(env, mgs, fsdb, mti);
2718                 }
2719                 GOTO(end, rc);
2720         }
2721
2722         if (class_match_param(ptr, PARAM_SYS, &tmp) == 0) {
2723                 rc = mgs_write_log_sys(env, mgs, fsdb, mti, ptr, tmp);
2724                 GOTO(end, rc);
2725         }
2726
2727         if (class_match_param(ptr, PARAM_QUOTA, &tmp) == 0) {
2728                 rc = mgs_write_log_quota(env, mgs, fsdb, mti, ptr, tmp);
2729                 GOTO(end, rc);
2730         }
2731
2732         if (class_match_param(ptr, PARAM_OSC""PARAM_ACTIVE, &tmp) == 0) {
2733                 /* active=0 means off, anything else means on */
2734                 int flag = (*tmp == '0') ? CM_EXCLUDE : 0;
2735                 int i;
2736
2737                 if (!(mti->mti_flags & LDD_F_SV_TYPE_OST)) {
2738                         LCONSOLE_ERROR_MSG(0x144, "%s: Only OSCs can "
2739                                            "be (de)activated.\n",
2740                                            mti->mti_svname);
2741                         GOTO(end, rc = -EINVAL);
2742                 }
2743                 LCONSOLE_WARN("Permanently %sactivating %s\n",
2744                               flag ? "de": "re", mti->mti_svname);
2745                 /* Modify clilov */
2746                 rc = name_create(&logname, mti->mti_fsname, "-client");
2747                 if (rc)
2748                         GOTO(end, rc);
2749                 rc = mgs_modify(env, mgs, fsdb, mti, logname,
2750                                 mti->mti_svname, "add osc", flag);
2751                 name_destroy(&logname);
2752                 if (rc)
2753                         goto active_err;
2754                 /* Modify mdtlov */
2755                 /* Add to all MDT logs for CMD */
2756                 for (i = 0; i < INDEX_MAP_SIZE * 8; i++) {
2757                         if (!cfs_test_bit(i, fsdb->fsdb_mdt_index_map))
2758                                 continue;
2759                         rc = name_create_mdt(&logname, mti->mti_fsname, i);
2760                         if (rc)
2761                                 GOTO(end, rc);
2762                         rc = mgs_modify(env, mgs, fsdb, mti, logname,
2763                                         mti->mti_svname, "add osc", flag);
2764                         name_destroy(&logname);
2765                         if (rc)
2766                                 goto active_err;
2767                 }
2768         active_err:
2769                 if (rc) {
2770                         LCONSOLE_ERROR_MSG(0x145, "Couldn't find %s in"
2771                                            "log (%d). No permanent "
2772                                            "changes were made to the "
2773                                            "config log.\n",
2774                                            mti->mti_svname, rc);
2775                         if (cfs_test_bit(FSDB_OLDLOG14, &fsdb->fsdb_flags))
2776                                 LCONSOLE_ERROR_MSG(0x146, "This may be"
2777                                                    " because the log"
2778                                                    "is in the old 1.4"
2779                                                    "style. Consider "
2780                                                    " --writeconf to "
2781                                                    "update the logs.\n");
2782                         GOTO(end, rc);
2783                 }
2784                 /* Fall through to osc proc for deactivating live OSC
2785                    on running MDT / clients. */
2786         }
2787         /* Below here, let obd's XXX_process_config methods handle it */
2788
2789         /* All lov. in proc */
2790         if (class_match_param(ptr, PARAM_LOV, NULL) == 0) {
2791                 char *mdtlovname;
2792
2793                 CDEBUG(D_MGS, "lov param %s\n", ptr);
2794                 if (!(mti->mti_flags & LDD_F_SV_TYPE_MDT)) {
2795                         LCONSOLE_ERROR_MSG(0x147, "LOV params must be "
2796                                            "set on the MDT, not %s. "
2797                                            "Ignoring.\n",
2798                                            mti->mti_svname);
2799                         GOTO(end, rc = 0);
2800                 }
2801
2802                 /* Modify mdtlov */
2803                 if (mgs_log_is_empty(env, mgs, mti->mti_svname))
2804                         GOTO(end, rc = -ENODEV);
2805
2806                 rc = name_create_mdt_and_lov(&logname, &mdtlovname, fsdb,
2807                                              mti->mti_stripe_index);
2808                 if (rc)
2809                         GOTO(end, rc);
2810                 rc = mgs_wlp_lcfg(env, mgs, fsdb, mti, mti->mti_svname,
2811                                   &mgi->mgi_bufs, mdtlovname, ptr);
2812                 name_destroy(&logname);
2813                 name_destroy(&mdtlovname);
2814                 if (rc)
2815                         GOTO(end, rc);
2816
2817                 /* Modify clilov */
2818                 rc = name_create(&logname, mti->mti_fsname, "-client");
2819                 if (rc)
2820                         GOTO(end, rc);
2821                 rc = mgs_wlp_lcfg(env, mgs, fsdb, mti, logname, &mgi->mgi_bufs,
2822                                   fsdb->fsdb_clilov, ptr);
2823                 name_destroy(&logname);
2824                 GOTO(end, rc);
2825         }
2826
2827         /* All osc., mdc., llite. params in proc */
2828         if ((class_match_param(ptr, PARAM_OSC, NULL) == 0) ||
2829             (class_match_param(ptr, PARAM_MDC, NULL) == 0) ||
2830             (class_match_param(ptr, PARAM_LLITE, NULL) == 0)) {
2831                 char *cname;
2832
2833                 if (cfs_test_bit(FSDB_OLDLOG14, &fsdb->fsdb_flags)) {
2834                         LCONSOLE_ERROR_MSG(0x148, "Upgraded client logs for %s"
2835                                            " cannot be modified. Consider"
2836                                            " updating the configuration with"
2837                                            " --writeconf\n",
2838                                            mti->mti_svname);
2839                         GOTO(end, rc = -EINVAL);
2840                 }
2841                 if (memcmp(ptr, PARAM_LLITE, strlen(PARAM_LLITE)) == 0) {
2842                         rc = name_create(&cname, mti->mti_fsname, "-client");
2843                         /* Add the client type to match the obdname in
2844                            class_config_llog_handler */
2845                 } else if (mti->mti_flags & LDD_F_SV_TYPE_MDT) {
2846                         rc = name_create(&cname, mti->mti_svname, "-mdc");
2847                 } else if (mti->mti_flags & LDD_F_SV_TYPE_OST) {
2848                         rc = name_create(&cname, mti->mti_svname, "-osc");
2849                 } else {
2850                         GOTO(end, rc = -EINVAL);
2851                 }
2852                 if (rc)
2853                         GOTO(end, rc);
2854
2855                 CDEBUG(D_MGS, "%.3s param %s\n", ptr, ptr + 4);
2856
2857                 /* Modify client */
2858                 rc = name_create(&logname, mti->mti_fsname, "-client");
2859                 if (rc) {
2860                         name_destroy(&cname);
2861                         GOTO(end, rc);
2862                 }
2863                 rc = mgs_wlp_lcfg(env, mgs, fsdb, mti, logname, &mgi->mgi_bufs,
2864                                   cname, ptr);
2865
2866                 /* osc params affect the MDT as well */
2867                 if (!rc && (mti->mti_flags & LDD_F_SV_TYPE_OST)) {
2868                         int i;
2869
2870                         for (i = 0; i < INDEX_MAP_SIZE * 8; i++){
2871                                 if (!cfs_test_bit(i, fsdb->fsdb_mdt_index_map))
2872                                         continue;
2873                                 name_destroy(&cname);
2874                                 rc = name_create_mdt_osc(&cname, mti->mti_svname,
2875                                                          fsdb, i);
2876                                 name_destroy(&logname);
2877                                 if (rc)
2878                                         break;
2879                                 rc = name_create_mdt(&logname,
2880                                                      mti->mti_fsname, i);
2881                                 if (rc)
2882                                         break;
2883                                 if (!mgs_log_is_empty(env, mgs, logname)) {
2884                                         rc = mgs_wlp_lcfg(env, mgs, fsdb,
2885                                                           mti, logname,
2886                                                           &mgi->mgi_bufs,
2887                                                           cname, ptr);
2888                                         if (rc)
2889                                                 break;
2890                                 }
2891                         }
2892                 }
2893                 name_destroy(&logname);
2894                 name_destroy(&cname);
2895                 GOTO(end, rc);
2896         }
2897
2898         /* All mdt. params in proc */
2899         if (class_match_param(ptr, PARAM_MDT, NULL) == 0) {
2900                 int i;
2901                 __u32 idx;
2902
2903                 CDEBUG(D_MGS, "%.3s param %s\n", ptr, ptr + 4);
2904                 if (strncmp(mti->mti_svname, mti->mti_fsname,
2905                             MTI_NAME_MAXLEN) == 0)
2906                         /* device is unspecified completely? */
2907                         rc = LDD_F_SV_TYPE_MDT | LDD_F_SV_ALL;
2908                 else
2909                         rc = server_name2index(mti->mti_svname, &idx, NULL);
2910                 if (rc < 0)
2911                         goto active_err;
2912                 if ((rc & LDD_F_SV_TYPE_MDT) == 0)
2913                         goto active_err;
2914                 if (rc & LDD_F_SV_ALL) {
2915                         for (i = 0; i < INDEX_MAP_SIZE * 8; i++) {
2916                                 if (!cfs_test_bit(i,
2917                                                   fsdb->fsdb_mdt_index_map))
2918                                         continue;
2919                                 rc = name_create_mdt(&logname,
2920                                                 mti->mti_fsname, i);
2921                                 if (rc)
2922                                         goto active_err;
2923                                 rc = mgs_wlp_lcfg(env, mgs, fsdb, mti,
2924                                                   logname, &mgi->mgi_bufs,
2925                                                   logname, ptr);
2926                                 name_destroy(&logname);
2927                                 if (rc)
2928                                         goto active_err;
2929                         }
2930                 } else {
2931                         rc = mgs_wlp_lcfg(env, mgs, fsdb, mti,
2932                                           mti->mti_svname, &mgi->mgi_bufs,
2933                                           mti->mti_svname, ptr);
2934                         if (rc)
2935                                 goto active_err;
2936                 }
2937                 GOTO(end, rc);
2938         }
2939
2940         /* All mdd., ost. params in proc */
2941         if ((class_match_param(ptr, PARAM_MDD, NULL) == 0) ||
2942             (class_match_param(ptr, PARAM_OST, NULL) == 0)) {
2943                 CDEBUG(D_MGS, "%.3s param %s\n", ptr, ptr + 4);
2944                 if (mgs_log_is_empty(env, mgs, mti->mti_svname))
2945                         GOTO(end, rc = -ENODEV);
2946
2947                 rc = mgs_wlp_lcfg(env, mgs, fsdb, mti, mti->mti_svname,
2948                                   &mgi->mgi_bufs, mti->mti_svname, ptr);
2949                 GOTO(end, rc);
2950         }
2951
2952         LCONSOLE_WARN("Ignoring unrecognized param '%s'\n", ptr);
2953         rc2 = -ENOSYS;
2954
2955 end:
2956         if (rc)
2957                 CERROR("err %d on param '%s'\n", rc, ptr);
2958
2959         RETURN(rc ?: rc2);
2960 }
2961
2962 /* Not implementing automatic failover nid addition at this time. */
2963 int mgs_check_failnid(const struct lu_env *env, struct mgs_device *mgs,
2964                       struct mgs_target_info *mti)
2965 {
2966 #if 0
2967         struct fs_db *fsdb;
2968         int rc;
2969         ENTRY;
2970
2971         rc = mgs_find_or_make_fsdb(obd, fsname, &fsdb);
2972         if (rc)
2973                 RETURN(rc);
2974
2975         if (mgs_log_is_empty(obd, mti->mti_svname))
2976                 /* should never happen */
2977                 RETURN(-ENOENT);
2978
2979         CDEBUG(D_MGS, "Checking for new failnids for %s\n", mti->mti_svname);
2980
2981         /* FIXME We can just check mti->params to see if we're already in
2982            the failover list.  Modify mti->params for rewriting back at
2983            server_register_target(). */
2984
2985         cfs_mutex_lock(&fsdb->fsdb_mutex);
2986         rc = mgs_write_log_add_failnid(obd, fsdb, mti);
2987         cfs_mutex_unlock(&fsdb->fsdb_mutex);
2988
2989         RETURN(rc);
2990 #endif
2991         return 0;
2992 }
2993
2994 int mgs_write_log_target(const struct lu_env *env,
2995                          struct mgs_device *mgs,
2996                          struct mgs_target_info *mti,
2997                          struct fs_db *fsdb)
2998 {
2999         int rc = -EINVAL;
3000         char *buf, *params;
3001         ENTRY;
3002
3003         /* set/check the new target index */
3004         rc = mgs_set_index(env, mgs, mti);
3005         if (rc < 0) {
3006                 CERROR("Can't get index (%d)\n", rc);
3007                 RETURN(rc);
3008         }
3009
3010         if (rc == EALREADY) {
3011                 LCONSOLE_WARN("Found index %d for %s, updating log\n",
3012                               mti->mti_stripe_index, mti->mti_svname);
3013                 /* We would like to mark old log sections as invalid
3014                    and add new log sections in the client and mdt logs.
3015                    But if we add new sections, then live clients will
3016                    get repeat setup instructions for already running
3017                    osc's. So don't update the client/mdt logs. */
3018                 mti->mti_flags &= ~LDD_F_UPDATE;
3019         }
3020
3021         cfs_mutex_lock(&fsdb->fsdb_mutex);
3022
3023         if (mti->mti_flags &
3024             (LDD_F_VIRGIN | LDD_F_UPGRADE14 | LDD_F_WRITECONF)) {
3025                 /* Generate a log from scratch */
3026                 if (mti->mti_flags & LDD_F_SV_TYPE_MDT) {
3027                         rc = mgs_write_log_mdt(env, mgs, fsdb, mti);
3028                 } else if (mti->mti_flags & LDD_F_SV_TYPE_OST) {
3029                         rc = mgs_write_log_ost(env, mgs, fsdb, mti);
3030                 } else {
3031                         CERROR("Unknown target type %#x, can't create log for "
3032                                "%s\n", mti->mti_flags, mti->mti_svname);
3033                 }
3034                 if (rc) {
3035                         CERROR("Can't write logs for %s (%d)\n",
3036                                mti->mti_svname, rc);
3037                         GOTO(out_up, rc);
3038                 }
3039         } else {
3040                 /* Just update the params from tunefs in mgs_write_log_params */
3041                 CDEBUG(D_MGS, "Update params for %s\n", mti->mti_svname);
3042                 mti->mti_flags |= LDD_F_PARAM;
3043         }
3044
3045         /* allocate temporary buffer, where class_get_next_param will
3046            make copy of a current  parameter */
3047         OBD_ALLOC(buf, strlen(mti->mti_params) + 1);
3048         if (buf == NULL)
3049                 GOTO(out_up, rc = -ENOMEM);
3050         params = mti->mti_params;
3051         while (params != NULL) {
3052                 rc = class_get_next_param(&params, buf);
3053                 if (rc) {
3054                         if (rc == 1)
3055                                 /* there is no next parameter, that is
3056                                    not an error */
3057                                 rc = 0;
3058                         break;
3059                 }
3060                 CDEBUG(D_MGS, "remaining string: '%s', param: '%s'\n",
3061                        params, buf);
3062                 rc = mgs_write_log_param(env, mgs, fsdb, mti, buf);
3063                 if (rc)
3064                         break;
3065         }
3066
3067         OBD_FREE(buf, strlen(mti->mti_params) + 1);
3068
3069 out_up:
3070         cfs_mutex_unlock(&fsdb->fsdb_mutex);
3071         RETURN(rc);
3072 }
3073
3074 int mgs_erase_log(const struct lu_env *env, struct mgs_device *mgs, char *name)
3075 {
3076         struct llog_ctxt        *ctxt;
3077         int                      rc = 0;
3078
3079         ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
3080         if (ctxt == NULL) {
3081                 CERROR("%s: MGS config context doesn't exist\n",
3082                        mgs->mgs_obd->obd_name);
3083                 rc = -ENODEV;
3084         } else {
3085                 rc = llog_erase(env, ctxt, NULL, name);
3086                 /* llog may not exist */
3087                 if (rc == -ENOENT)
3088                         rc = 0;
3089                 llog_ctxt_put(ctxt);
3090         }
3091
3092         if (rc)
3093                 CERROR("%s: failed to clear log %s: %d\n",
3094                        mgs->mgs_obd->obd_name, name, rc);
3095
3096         return rc;
3097 }
3098
3099 /* erase all logs for the given fs */
3100 int mgs_erase_logs(const struct lu_env *env, struct mgs_device *mgs, char *fsname)
3101 {
3102         struct fs_db *fsdb;
3103         cfs_list_t list;
3104         struct mgs_direntry *dirent, *n;
3105         int rc, len = strlen(fsname);
3106         char *suffix;
3107         ENTRY;
3108
3109         /* Find all the logs in the CONFIGS directory */
3110         rc = class_dentry_readdir(env, mgs, &list);
3111         if (rc)
3112                 RETURN(rc);
3113
3114         cfs_mutex_lock(&mgs->mgs_mutex);
3115
3116         /* Delete the fs db */
3117         fsdb = mgs_find_fsdb(mgs, fsname);
3118         if (fsdb)
3119                 mgs_free_fsdb(mgs, fsdb);
3120
3121         cfs_mutex_unlock(&mgs->mgs_mutex);
3122
3123         cfs_list_for_each_entry_safe(dirent, n, &list, list) {
3124                 cfs_list_del(&dirent->list);
3125                 suffix = strrchr(dirent->name, '-');
3126                 if (suffix != NULL) {
3127                         if ((len == suffix - dirent->name) &&
3128                             (strncmp(fsname, dirent->name, len) == 0)) {
3129                                 CDEBUG(D_MGS, "Removing log %s\n",
3130                                        dirent->name);
3131                                 mgs_erase_log(env, mgs, dirent->name);
3132                         }
3133                 }
3134                 mgs_direntry_free(dirent);
3135         }
3136
3137         RETURN(rc);
3138 }
3139
3140 /* from llog_swab */
3141 static void print_lustre_cfg(struct lustre_cfg *lcfg)
3142 {
3143         int i;
3144         ENTRY;
3145
3146         CDEBUG(D_MGS, "lustre_cfg: %p\n", lcfg);
3147         CDEBUG(D_MGS, "\tlcfg->lcfg_version: %#x\n", lcfg->lcfg_version);
3148
3149         CDEBUG(D_MGS, "\tlcfg->lcfg_command: %#x\n", lcfg->lcfg_command);
3150         CDEBUG(D_MGS, "\tlcfg->lcfg_num: %#x\n", lcfg->lcfg_num);
3151         CDEBUG(D_MGS, "\tlcfg->lcfg_flags: %#x\n", lcfg->lcfg_flags);
3152         CDEBUG(D_MGS, "\tlcfg->lcfg_nid: %s\n", libcfs_nid2str(lcfg->lcfg_nid));
3153
3154         CDEBUG(D_MGS, "\tlcfg->lcfg_bufcount: %d\n", lcfg->lcfg_bufcount);
3155         if (lcfg->lcfg_bufcount < LUSTRE_CFG_MAX_BUFCOUNT)
3156                 for (i = 0; i < lcfg->lcfg_bufcount; i++) {
3157                         CDEBUG(D_MGS, "\tlcfg->lcfg_buflens[%d]: %d %s\n",
3158                                i, lcfg->lcfg_buflens[i],
3159                                lustre_cfg_string(lcfg, i));
3160                 }
3161         EXIT;
3162 }
3163
3164 /* Set a permanent (config log) param for a target or fs
3165  * \param lcfg buf0 may contain the device (testfs-MDT0000) name
3166  *             buf1 contains the single parameter
3167  */
3168 int mgs_setparam(const struct lu_env *env, struct mgs_device *mgs,
3169                  struct lustre_cfg *lcfg, char *fsname)
3170 {
3171         struct fs_db *fsdb;
3172         struct mgs_target_info *mti;
3173         char *devname, *param;
3174         char *ptr, *tmp;
3175         __u32 index;
3176         int rc = 0;
3177         ENTRY;
3178
3179         print_lustre_cfg(lcfg);
3180
3181         /* lustre, lustre-mdtlov, lustre-client, lustre-MDT0000 */
3182         devname = lustre_cfg_string(lcfg, 0);
3183         param = lustre_cfg_string(lcfg, 1);
3184         if (!devname) {
3185                 /* Assume device name embedded in param:
3186                    lustre-OST0000.osc.max_dirty_mb=32 */
3187                 ptr = strchr(param, '.');
3188                 if (ptr) {
3189                         devname = param;
3190                         *ptr = 0;
3191                         param = ptr + 1;
3192                 }
3193         }
3194         if (!devname) {
3195                 LCONSOLE_ERROR_MSG(0x14d, "No target specified: %s\n", param);
3196                 RETURN(-ENOSYS);
3197         }
3198
3199         /* Extract fsname */
3200         ptr = strrchr(devname, '-');
3201         memset(fsname, 0, MTI_NAME_MAXLEN);
3202         if (ptr && (server_name2index(ptr, &index, NULL) >= 0)) {
3203                 /* param related to llite isn't allowed to set by OST or MDT */
3204                 if (strncmp(param, PARAM_LLITE, sizeof(PARAM_LLITE)) == 0)
3205                         RETURN(-EINVAL);
3206
3207                 strncpy(fsname, devname, ptr - devname);
3208         } else {
3209                 /* assume devname is the fsname */
3210                 strncpy(fsname, devname, MTI_NAME_MAXLEN);
3211         }
3212         fsname[MTI_NAME_MAXLEN - 1] = 0;
3213         CDEBUG(D_MGS, "setparam fs='%s' device='%s'\n", fsname, devname);
3214
3215         rc = mgs_find_or_make_fsdb(env, mgs, fsname, &fsdb);
3216         if (rc)
3217                 RETURN(rc);
3218         if (!cfs_test_bit(FSDB_MGS_SELF, &fsdb->fsdb_flags) &&
3219             cfs_test_bit(FSDB_LOG_EMPTY, &fsdb->fsdb_flags)) {
3220                 CERROR("No filesystem targets for %s.  cfg_device from lctl "
3221                        "is '%s'\n", fsname, devname);
3222                 mgs_free_fsdb(mgs, fsdb);
3223                 RETURN(-EINVAL);
3224         }
3225
3226         /* Create a fake mti to hold everything */
3227         OBD_ALLOC_PTR(mti);
3228         if (!mti)
3229                 GOTO(out, rc = -ENOMEM);
3230         strncpy(mti->mti_fsname, fsname, MTI_NAME_MAXLEN);
3231         strncpy(mti->mti_svname, devname, MTI_NAME_MAXLEN);
3232         strncpy(mti->mti_params, param, sizeof(mti->mti_params));
3233         rc = server_name2index(mti->mti_svname, &mti->mti_stripe_index, &tmp);
3234         if (rc < 0)
3235                 /* Not a valid server; may be only fsname */
3236                 rc = 0;
3237         else
3238                 /* Strip -osc or -mdc suffix from svname */
3239                 if (server_make_name(rc, mti->mti_stripe_index, mti->mti_fsname,
3240                                      mti->mti_svname))
3241                         GOTO(out, rc = -EINVAL);
3242
3243         mti->mti_flags = rc | LDD_F_PARAM;
3244
3245         cfs_mutex_lock(&fsdb->fsdb_mutex);
3246         rc = mgs_write_log_param(env, mgs, fsdb, mti, mti->mti_params);
3247         cfs_mutex_unlock(&fsdb->fsdb_mutex);
3248
3249         /*
3250          * Revoke lock so everyone updates.  Should be alright if
3251          * someone was already reading while we were updating the logs,
3252          * so we don't really need to hold the lock while we're
3253          * writing (above).
3254          */
3255         mgs_revoke_lock(mgs, fsdb, CONFIG_T_CONFIG);
3256 out:
3257         OBD_FREE_PTR(mti);
3258         RETURN(rc);
3259 }
3260
3261 static int mgs_write_log_pool(const struct lu_env *env,
3262                               struct mgs_device *mgs, char *logname,
3263                               struct fs_db *fsdb, char *lovname,
3264                               enum lcfg_command_type cmd,
3265                               char *poolname, char *fsname,
3266                               char *ostname, char *comment)
3267 {
3268         struct llog_handle *llh = NULL;
3269         int rc;
3270
3271         rc = record_start_log(env, mgs, &llh, logname);
3272         if (rc)
3273                 return rc;
3274         rc = record_marker(env, llh, fsdb, CM_START, lovname, comment);
3275         if (rc)
3276                 goto out;
3277         rc = record_base(env, llh, lovname, 0, cmd, poolname, fsname, ostname, 0);
3278         if (rc)
3279                 goto out;
3280         rc = record_marker(env, llh, fsdb, CM_END, lovname, comment);
3281 out:
3282         record_end_log(env, &llh);
3283         return rc;
3284 }
3285
3286 int mgs_pool_cmd(const struct lu_env *env, struct mgs_device *mgs,
3287                  enum lcfg_command_type cmd, char *fsname,
3288                  char *poolname, char *ostname)
3289 {
3290         struct fs_db *fsdb;
3291         char *lovname;
3292         char *logname;
3293         char *label = NULL, *canceled_label = NULL;
3294         int label_sz;
3295         struct mgs_target_info *mti = NULL;
3296         int rc, i;
3297         ENTRY;
3298
3299         rc = mgs_find_or_make_fsdb(env, mgs, fsname, &fsdb);
3300         if (rc) {
3301                 CERROR("Can't get db for %s\n", fsname);
3302                 RETURN(rc);
3303         }
3304         if (cfs_test_bit(FSDB_LOG_EMPTY, &fsdb->fsdb_flags)) {
3305                 CERROR("%s is not defined\n", fsname);
3306                 mgs_free_fsdb(mgs, fsdb);
3307                 RETURN(-EINVAL);
3308         }
3309
3310         label_sz = 10 + strlen(fsname) + strlen(poolname);
3311
3312         /* check if ostname match fsname */
3313         if (ostname != NULL) {
3314                 char *ptr;
3315
3316                 ptr = strrchr(ostname, '-');
3317                 if ((ptr == NULL) ||
3318                     (strncmp(fsname, ostname, ptr-ostname) != 0))
3319                         RETURN(-EINVAL);
3320                 label_sz += strlen(ostname);
3321         }
3322
3323         OBD_ALLOC(label, label_sz);
3324         if (label == NULL)
3325                 RETURN(-ENOMEM);
3326
3327         switch(cmd) {
3328         case LCFG_POOL_NEW:
3329                 sprintf(label,
3330                         "new %s.%s", fsname, poolname);
3331                 break;
3332         case LCFG_POOL_ADD:
3333                 sprintf(label,
3334                         "add %s.%s.%s", fsname, poolname, ostname);
3335                 break;
3336         case LCFG_POOL_REM:
3337                 OBD_ALLOC(canceled_label, label_sz);
3338                 if (canceled_label == NULL)
3339                         GOTO(out_label, rc = -ENOMEM);
3340                 sprintf(label,
3341                         "rem %s.%s.%s", fsname, poolname, ostname);
3342                 sprintf(canceled_label,
3343                         "add %s.%s.%s", fsname, poolname, ostname);
3344                 break;
3345         case LCFG_POOL_DEL:
3346                 OBD_ALLOC(canceled_label, label_sz);
3347                 if (canceled_label == NULL)
3348                         GOTO(out_label, rc = -ENOMEM);
3349                 sprintf(label,
3350                         "del %s.%s", fsname, poolname);
3351                 sprintf(canceled_label,
3352                         "new %s.%s", fsname, poolname);
3353                 break;
3354         default:
3355                 break;
3356         }
3357
3358         cfs_mutex_lock(&fsdb->fsdb_mutex);
3359
3360         if (canceled_label != NULL) {
3361                 OBD_ALLOC_PTR(mti);
3362                 if (mti == NULL)
3363                         GOTO(out_cancel, rc = -ENOMEM);
3364         }
3365
3366         /* write pool def to all MDT logs */
3367         for (i = 0; i < INDEX_MAP_SIZE * 8; i++) {
3368                  if (cfs_test_bit(i,  fsdb->fsdb_mdt_index_map)) {
3369                         rc = name_create_mdt_and_lov(&logname, &lovname,
3370                                                      fsdb, i);
3371                         if (rc) {
3372                                 cfs_mutex_unlock(&fsdb->fsdb_mutex);
3373                                 GOTO(out_mti, rc);
3374                         }
3375                         if (canceled_label != NULL) {
3376                                 strcpy(mti->mti_svname, "lov pool");
3377                                 rc = mgs_modify(env, mgs, fsdb, mti, logname,
3378                                                 lovname, canceled_label,
3379                                                 CM_SKIP);
3380                         }
3381
3382                         if (rc >= 0)
3383                                 rc = mgs_write_log_pool(env, mgs, logname,
3384                                                         fsdb, lovname, cmd,
3385                                                         fsname, poolname,
3386                                                         ostname, label);
3387                         name_destroy(&logname);
3388                         name_destroy(&lovname);
3389                         if (rc) {
3390                                 cfs_mutex_unlock(&fsdb->fsdb_mutex);
3391                                 GOTO(out_mti, rc);
3392                         }
3393                 }
3394         }
3395
3396         rc = name_create(&logname, fsname, "-client");
3397         if (rc) {
3398                 cfs_mutex_unlock(&fsdb->fsdb_mutex);
3399                 GOTO(out_mti, rc);
3400         }
3401         if (canceled_label != NULL) {
3402                 rc = mgs_modify(env, mgs, fsdb, mti, logname,
3403                                 fsdb->fsdb_clilov, canceled_label, CM_SKIP);
3404                 if (rc < 0) {
3405                         cfs_mutex_unlock(&fsdb->fsdb_mutex);
3406                         name_destroy(&logname);
3407                         GOTO(out_mti, rc);
3408                 }
3409         }
3410
3411         rc = mgs_write_log_pool(env, mgs, logname, fsdb, fsdb->fsdb_clilov,
3412                                 cmd, fsname, poolname, ostname, label);
3413         cfs_mutex_unlock(&fsdb->fsdb_mutex);
3414         name_destroy(&logname);
3415         /* request for update */
3416         mgs_revoke_lock(mgs, fsdb, CONFIG_T_CONFIG);
3417
3418         EXIT;
3419 out_mti:
3420         if (mti != NULL)
3421                 OBD_FREE_PTR(mti);
3422 out_cancel:
3423         if (canceled_label != NULL)
3424                 OBD_FREE(canceled_label, label_sz);
3425 out_label:
3426         OBD_FREE(label, label_sz);
3427         return rc;
3428 }
3429
3430 #if 0
3431 /******************** unused *********************/
3432 static int mgs_backup_llog(struct obd_device *obd, char* fsname)
3433 {
3434         struct file *filp, *bak_filp;
3435         struct lvfs_run_ctxt saved;
3436         char *logname, *buf;
3437         loff_t soff = 0 , doff = 0;
3438         int count = 4096, len;
3439         int rc = 0;
3440
3441         OBD_ALLOC(logname, PATH_MAX);
3442         if (logname == NULL)
3443                 return -ENOMEM;
3444
3445         OBD_ALLOC(buf, count);
3446         if (!buf)
3447                 GOTO(out , rc = -ENOMEM);
3448
3449         len = snprintf(logname, PATH_MAX, "%s/%s.bak",
3450                        MOUNT_CONFIGS_DIR, fsname);
3451
3452         if (len >= PATH_MAX - 1) {
3453                 GOTO(out, -ENAMETOOLONG);
3454         }
3455
3456         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3457
3458         bak_filp = l_filp_open(logname, O_RDWR|O_CREAT|O_TRUNC, 0660);
3459         if (IS_ERR(bak_filp)) {
3460                 rc = PTR_ERR(bak_filp);
3461                 CERROR("backup logfile open %s: %d\n", logname, rc);
3462                 GOTO(pop, rc);
3463         }
3464         sprintf(logname, "%s/%s", MOUNT_CONFIGS_DIR, fsname);
3465         filp = l_filp_open(logname, O_RDONLY, 0);
3466         if (IS_ERR(filp)) {
3467                 rc = PTR_ERR(filp);
3468                 CERROR("logfile open %s: %d\n", logname, rc);
3469                 GOTO(close1f, rc);
3470         }
3471
3472         while ((rc = lustre_fread(filp, buf, count, &soff)) > 0) {
3473                 rc = lustre_fwrite(bak_filp, buf, count, &doff);
3474                 break;
3475         }
3476
3477         filp_close(filp, 0);
3478 close1f:
3479         filp_close(bak_filp, 0);
3480 pop:
3481         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3482 out:
3483         if (buf)
3484                 OBD_FREE(buf, count);
3485         OBD_FREE(logname, PATH_MAX);
3486         return rc;
3487 }
3488
3489 #endif