Whamcloud - gitweb
- cleanups in cmobd and others:
[fs/lustre-release.git] / lustre / mds / mds_lov.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mds/mds_lov.c
5  *  Lustre Metadata Server (mds) handling of striped file data
6  *
7  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #ifndef EXPORT_SYMTAB
27 # define EXPORT_SYMTAB
28 #endif
29 #define DEBUG_SUBSYSTEM S_MDS
30
31 #include <linux/module.h>
32 #include <linux/lustre_mds.h>
33 #include <linux/obd_ost.h>
34 #include <linux/lustre_idl.h>
35 #include <linux/obd_class.h>
36 #include <linux/obd_lov.h>
37 #include <linux/lustre_lib.h>
38 #include <linux/lustre_fsfilt.h>
39 #include <linux/lustre_sec.h>
40
41 #include "mds_internal.h"
42
43 void le_lov_desc_to_cpu (struct lov_desc *ld)
44 {
45         ld->ld_tgt_count = le32_to_cpu (ld->ld_tgt_count);
46         ld->ld_default_stripe_count = le32_to_cpu (ld->ld_default_stripe_count);
47         ld->ld_default_stripe_size = le32_to_cpu (ld->ld_default_stripe_size);
48         ld->ld_pattern = le32_to_cpu (ld->ld_pattern);
49 }
50
51 void cpu_to_le_lov_desc (struct lov_desc *ld)
52 {
53         ld->ld_tgt_count = cpu_to_le32 (ld->ld_tgt_count);
54         ld->ld_default_stripe_count = cpu_to_le32 (ld->ld_default_stripe_count);
55         ld->ld_default_stripe_size = cpu_to_le32 (ld->ld_default_stripe_size);
56         ld->ld_pattern = cpu_to_le32 (ld->ld_pattern);
57 }
58
59 void mds_dt_save_objids(struct obd_device *obd, obd_id *ids)
60 {
61         struct mds_obd *mds = &obd->u.mds;
62         int i;
63         ENTRY;
64
65         spin_lock(&mds->mds_dt_lock);
66         for (i = 0; i < mds->mds_dt_desc.ld_tgt_count; i++)
67                 ids[i] = mds->mds_dt_objids[i];
68         spin_unlock(&mds->mds_dt_lock);
69         EXIT;
70 }
71
72 void mds_dt_update_objids(struct obd_device *obd, obd_id *ids)
73 {
74         struct mds_obd *mds = &obd->u.mds;
75         int i;
76         ENTRY;
77
78         spin_lock(&mds->mds_dt_lock);
79         for (i = 0; i < mds->mds_dt_desc.ld_tgt_count; i++)
80                 if (ids[i] > mds->mds_dt_objids[i])
81                         mds->mds_dt_objids[i] = ids[i];
82         spin_unlock(&mds->mds_dt_lock);
83         EXIT;
84 }
85
86 static int mds_dt_read_objids(struct obd_device *obd)
87 {
88         struct mds_obd *mds = &obd->u.mds;
89         int i, rc, size;
90         loff_t off = 0;
91         obd_id *ids;
92         ENTRY;
93
94         if (mds->mds_dt_objids != NULL)
95                 RETURN(0);
96
97         size = mds->mds_dt_desc.ld_tgt_count * sizeof(*ids);
98         OBD_ALLOC(ids, size);
99         if (ids == NULL)
100                 RETURN(-ENOMEM);
101         mds->mds_dt_objids = ids;
102
103         if (mds->mds_dt_objid_filp->f_dentry->d_inode->i_size == 0)
104                 RETURN(0);
105         
106         rc = fsfilt_read_record(obd, mds->mds_dt_objid_filp, ids, size, &off);
107         if (rc < 0) {
108                 CERROR("error reading objids %d\n", rc);
109         } else {
110                 mds->mds_dt_objids_valid = 1;
111                 rc = 0;
112         }
113
114         for (i = 0; i < mds->mds_dt_desc.ld_tgt_count; i++) {
115                 CDEBUG(D_INFO, "read last object "LPU64
116                        " for idx %d\n", mds->mds_dt_objids[i], i);
117         }
118
119         RETURN(rc);
120 }
121
122 int mds_dt_write_objids(struct obd_device *obd)
123 {
124         struct mds_obd *mds = &obd->u.mds;
125         int i, rc, size;
126         loff_t off = 0;
127         ENTRY;
128
129         for (i = 0; i < mds->mds_dt_desc.ld_tgt_count; i++)
130                 CDEBUG(D_INFO, "writing last object "LPU64" for idx %d\n",
131                        mds->mds_dt_objids[i], i);
132
133         size = mds->mds_dt_desc.ld_tgt_count * sizeof(obd_id);
134         rc = fsfilt_write_record(obd, mds->mds_dt_objid_filp,
135                                  mds->mds_dt_objids, size, &off, 0);
136         RETURN(rc);
137 }
138
139 int mds_dt_clear_orphans(struct mds_obd *mds, struct obd_uuid *ost_uuid)
140 {
141         struct lov_stripe_md *empty_ea = NULL;
142         struct obd_trans_info oti = { 0 };
143         struct obdo *oa = NULL;
144         int rc;
145         ENTRY;
146
147         LASSERT(mds->mds_dt_objids != NULL);
148
149         /*
150          * this create will in fact either create or destroy: If the OST is
151          * missing objects below this ID, they will be created.  If it finds
152          * objects above this ID, they will be removed.
153          */
154         oa = obdo_alloc();
155         if (oa == NULL)
156                 RETURN(-ENOMEM);
157         
158         memset(oa, 0, sizeof(*oa));
159
160         oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
161         oa->o_valid = OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
162         oa->o_flags = OBD_FL_DELORPHAN;
163         
164         if (ost_uuid != NULL) {
165                 memcpy(&oa->o_inline, ost_uuid,
166                        sizeof(*ost_uuid));
167                 oa->o_valid |= OBD_MD_FLINLINE;
168         }
169
170         /* 
171          * passing current objids for letting data layer know last objids MDS
172          * knows about and do appropriate. --umka
173          */
174         oti.oti_objid = mds->mds_dt_objids;
175         
176         rc = obd_create(mds->mds_dt_exp, oa,
177                         NULL, 0, &empty_ea, &oti);
178         
179         obdo_free(oa);
180         RETURN(rc);
181 }
182
183 /* tell the LOV-OSC by how much to pre-create */
184 int mds_dt_set_growth(struct mds_obd *mds, int count)
185 {
186         int rc;
187         ENTRY;
188
189         rc = obd_set_info(mds->mds_dt_exp, strlen("growth_count"),
190                           "growth_count", sizeof(count), &count);
191
192         RETURN(rc);
193 }
194
195 static int mds_dt_update_desc(struct obd_device *obd, struct obd_export *lov)
196 {
197         struct mds_obd *mds = &obd->u.mds;
198         __u32 valsize = sizeof(mds->mds_dt_desc);
199         int old_count, rc = 0, i;
200         ENTRY;
201
202         old_count = mds->mds_dt_desc.ld_tgt_count;
203         
204         rc = obd_get_info(lov, strlen("lovdesc") + 1, "lovdesc",
205                           &valsize, &mds->mds_dt_desc);
206         if (rc)
207                 RETURN(rc);
208
209         /* The size of the LOV target table may have increased. */
210         if (old_count >= mds->mds_dt_desc.ld_tgt_count) {
211                 obd_id *ids;
212                 int     size;
213
214                 size = mds->mds_dt_desc.ld_tgt_count * sizeof(*ids);
215                 OBD_ALLOC(ids, size);
216                 if (ids == NULL)
217                         RETURN(-ENOMEM);
218
219                 memset(ids, 0, size);
220
221                 if (mds->mds_dt_objids != NULL) {
222                         int oldsize = old_count * sizeof(*ids);
223
224                         memcpy(ids, mds->mds_dt_objids, oldsize);
225                         OBD_FREE(mds->mds_dt_objids, oldsize);
226                 }
227                 mds->mds_dt_objids = ids;
228         }
229
230         i = lov_mds_md_size(mds->mds_dt_desc.ld_tgt_count);
231         if (i > mds->mds_max_mdsize)
232                 mds->mds_max_mdsize = i;
233         mds->mds_max_cookiesize = mds->mds_dt_desc.ld_tgt_count *
234                                   sizeof(struct llog_cookie);
235         mds->mds_has_dt_desc = 1;
236         RETURN(0);
237 }
238
239 int mds_dt_connect(struct obd_device *obd, char *lov_name)
240 {
241         struct mds_obd *mds = &obd->u.mds;
242         struct lustre_handle conn = { 0 };
243         unsigned long sec_flags = PTLRPC_SEC_FL_MDS;
244         int i, rc = 0;
245         ENTRY;
246
247         if (IS_ERR(mds->mds_dt_obd))
248                 RETURN(PTR_ERR(mds->mds_dt_obd));
249
250         if (mds->mds_dt_obd)
251                 RETURN(0);
252
253         spin_lock_init(&mds->mds_dt_lock);
254         mds->mds_dt_obd = class_name2obd(lov_name);
255         if (!mds->mds_dt_obd) {
256                 CERROR("MDS cannot locate LOV %s\n", lov_name);
257                 mds->mds_dt_obd = ERR_PTR(-ENOTCONN);
258                 RETURN(-ENOTCONN);
259         }
260
261         if (mds->mds_ost_sec) {
262                 rc = obd_set_info(mds->mds_dt_obd->obd_self_export,
263                                   strlen("sec"), "sec",
264                                   strlen(mds->mds_ost_sec), mds->mds_ost_sec);
265                 if (rc) {
266                         mds->mds_dt_obd = ERR_PTR(rc);
267                         RETURN(rc);
268                 }
269         }
270
271         rc = obd_set_info(mds->mds_dt_obd->obd_self_export,
272                           strlen("sec_flags"), "sec_flags",
273                           sizeof(sec_flags), &sec_flags);
274         if (rc) {
275                 mds->mds_dt_obd = ERR_PTR(rc);
276                 RETURN(rc);
277         }
278
279         CDEBUG(D_HA, "obd: %s osc: %s lov_name: %s\n",
280                obd->obd_name, mds->mds_dt_obd->obd_name, lov_name);
281
282         rc = obd_connect(&conn, mds->mds_dt_obd, &obd->obd_uuid, NULL,
283                          mds->mds_num + FILTER_GROUP_FIRST_MDS);
284         if (rc) {
285                 CERROR("MDS cannot connect to LOV %s (%d)\n", lov_name, rc);
286                 mds->mds_dt_obd = ERR_PTR(rc);
287                 RETURN(rc);
288         }
289         mds->mds_dt_exp = class_conn2export(&conn);
290
291         rc = obd_register_observer(mds->mds_dt_obd, obd);
292         if (rc) {
293                 CERROR("MDS cannot register as observer of LOV %s (%d)\n",
294                        lov_name, rc);
295                 GOTO(err_discon, rc);
296         }
297
298         rc = mds_dt_update_desc(obd, mds->mds_dt_exp);
299         if (rc)
300                 GOTO(err_reg, rc);
301
302         rc = mds_dt_read_objids(obd);
303         if (rc) {
304                 CERROR("cannot read %s: rc = %d\n", "lov_objids", rc);
305                 GOTO(err_reg, rc);
306         }
307
308         rc = obd_llog_cat_initialize(obd, &obd->obd_llogs, 
309                                      mds->mds_dt_desc.ld_tgt_count, CATLIST);
310         if (rc) {
311                 CERROR("failed to initialize catalog %d\n", rc);
312                 GOTO(err_reg, rc);
313         }
314
315         /*
316          * If we're mounting this code for the first time on an existing FS, we
317          * need to populate the objids array from the real OST values.
318          */
319         if (!mds->mds_dt_objids_valid) {
320                 __u32 size = sizeof(obd_id) * mds->mds_dt_desc.ld_tgt_count;
321                 
322                 rc = obd_get_info(mds->mds_dt_exp, strlen("last_id"),
323                                   "last_id", &size, mds->mds_dt_objids);
324                 if (!rc) {
325                         for (i = 0; i < mds->mds_dt_desc.ld_tgt_count; i++)
326                                 CWARN("got last object "LPU64" from OST %d\n",
327                                       mds->mds_dt_objids[i], i);
328                         mds->mds_dt_objids_valid = 1;
329                         rc = mds_dt_write_objids(obd);
330                         if (rc)
331                                 CERROR("got last objids from OSTs, but error "
332                                        "writing objids file: %d\n", rc);
333                 }
334         }
335
336         /*
337          * I want to see a callback happen when the OBD moves to a "For General
338          * Use" state, and that's when we'll call set_nextid(). The class driver
339          * can help us here, because it can use the obd_recovering flag to
340          * determine when the the OBD is full available.
341          */
342         if (!obd->obd_recovering) {
343                 CDEBUG(D_OTHER, "call mds_postrecov_common()\n");
344                 rc = mds_postrecov_common(obd);
345                 if (rc > 0) 
346                         rc = 0;
347         }
348         RETURN(rc);
349
350 err_reg:
351         obd_register_observer(mds->mds_dt_obd, NULL);
352 err_discon:
353         obd_disconnect(mds->mds_dt_exp, 0);
354         mds->mds_dt_obd = ERR_PTR(rc);
355         mds->mds_dt_exp = NULL;
356         return rc;
357 }
358
359 int mds_dt_disconnect(struct obd_device *obd, int flags)
360 {
361         struct mds_obd *mds = &obd->u.mds;
362         int rc = 0;
363         ENTRY;
364
365         if (!IS_ERR(mds->mds_dt_obd) && mds->mds_dt_exp != NULL) {
366                 /* cleanup all llogging subsystems */
367                 rc = obd_llog_finish(obd, &obd->obd_llogs,
368                                      mds->mds_dt_desc.ld_tgt_count);
369                 if (rc)
370                         CERROR("failed to cleanup llogging subsystems\n");
371
372                 obd_register_observer(mds->mds_dt_obd, NULL);
373
374                 rc = obd_disconnect(mds->mds_dt_exp, flags);
375                 /* if obd_disconnect fails (probably because the
376                  * export was disconnected by class_disconnect_exports)
377                  * then we just need to drop our ref. */
378                 if (rc != 0)
379                         class_export_put(mds->mds_dt_exp);
380                 mds->mds_dt_exp = NULL;
381                 mds->mds_dt_obd = NULL;
382         }
383
384         RETURN(rc);
385 }
386
387 int mds_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
388                   void *karg, void *uarg)
389 {
390         static struct obd_uuid cfg_uuid = { .uuid = "config_uuid" };
391         struct obd_device *obd = exp->exp_obd;
392         struct mds_obd *mds = &obd->u.mds;
393         struct obd_ioctl_data *data = karg;
394         struct lvfs_run_ctxt saved;
395         int rc = 0;
396         ENTRY;
397
398         CDEBUG(D_INFO, "ioctl cmd %x\n", cmd);
399         switch (cmd) {
400         case OBD_IOC_RECORD: {
401                 char *name = data->ioc_inlbuf1;
402                 if (mds->mds_cfg_llh)
403                         RETURN(-EBUSY);
404
405                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
406                 rc = llog_open(llog_get_context(&obd->obd_llogs, 
407                                                 LLOG_CONFIG_ORIG_CTXT),
408                                &mds->mds_cfg_llh, NULL, name,
409                                OBD_LLOG_FL_CREATE);
410                 if (rc == 0)
411                         llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
412                                          &cfg_uuid);
413                 else
414                         mds->mds_cfg_llh = NULL;
415                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
416
417                 RETURN(rc);
418         }
419
420         case OBD_IOC_ENDRECORD: {
421                 if (!mds->mds_cfg_llh)
422                         RETURN(-EBADF);
423
424                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
425                 rc = llog_close(mds->mds_cfg_llh);
426                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
427
428                 mds->mds_cfg_llh = NULL;
429                 RETURN(rc);
430         }
431
432         case OBD_IOC_CLEAR_LOG: {
433                 char *name = data->ioc_inlbuf1;
434                 if (mds->mds_cfg_llh)
435                         RETURN(-EBUSY);
436
437                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
438                 rc = llog_open(llog_get_context(&obd->obd_llogs, 
439                                                 LLOG_CONFIG_ORIG_CTXT),
440                                &mds->mds_cfg_llh, NULL, name,
441                                OBD_LLOG_FL_CREATE);
442                 if (rc == 0) {
443                         llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
444                                          NULL);
445
446                         rc = llog_destroy(mds->mds_cfg_llh);
447                         llog_free_handle(mds->mds_cfg_llh);
448                 }
449                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
450
451                 mds->mds_cfg_llh = NULL;
452                 RETURN(rc);
453         }
454
455         case OBD_IOC_DORECORD: {
456                 char *cfg_buf;
457                 struct llog_rec_hdr rec;
458                 if (!mds->mds_cfg_llh)
459                         RETURN(-EBADF);
460
461                 /* XXX - this probably should be a parameter to this ioctl.
462                  * For now, just use llh_max_transno for expediency. */
463                 rec.lrh_len = llog_data_len(data->ioc_plen1);
464
465                 if (data->ioc_type == LUSTRE_CFG_TYPE) {
466                         rec.lrh_type = OBD_CFG_REC;
467                 } else if (data->ioc_type == PORTALS_CFG_TYPE) {
468                         rec.lrh_type = PTL_CFG_REC;
469                 } else {
470                         CERROR("unknown cfg record type:%d \n", data->ioc_type);
471                         RETURN(-EINVAL);
472                 }
473
474                 OBD_ALLOC(cfg_buf, data->ioc_plen1);
475                 if (cfg_buf == NULL)
476                         RETURN(-EINVAL);
477                 rc = copy_from_user(cfg_buf, data->ioc_pbuf1, data->ioc_plen1);
478                 if (rc) {
479                         OBD_FREE(cfg_buf, data->ioc_plen1);
480                         RETURN(rc);
481                 }
482
483                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
484                 rc = llog_write_rec(mds->mds_cfg_llh, &rec, NULL, 0,
485                                     cfg_buf, -1);
486                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
487
488                 OBD_FREE(cfg_buf, data->ioc_plen1);
489                 RETURN(rc);
490         }
491         case OBD_IOC_PARSE: {
492                 struct llog_ctxt *ctxt =
493                         llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
494                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
495                 rc = class_config_process_llog(ctxt, data->ioc_inlbuf1, NULL);
496                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
497                 if (rc)
498                         RETURN(rc);
499
500                 RETURN(rc);
501         }
502         case OBD_IOC_DUMP_LOG: {
503                 struct llog_ctxt *ctxt =
504                         llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
505                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
506                 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
507                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
508
509                 RETURN(rc);
510         }
511         case OBD_IOC_SET_READONLY: {
512                 void *handle;
513                 struct inode *inode = obd->u.mds.mds_sb->s_root->d_inode;
514                 BDEVNAME_DECLARE_STORAGE(tmp);
515                 CERROR("setting device %s read-only\n",
516                        ll_bdevname(obd->u.mds.mds_sb, tmp));
517
518                 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
519                 LASSERT(handle);
520                 rc = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 1);
521
522                 ll_set_rdonly(ll_sbdev(obd->u.mds.mds_sb));
523                 RETURN(0);
524         }
525
526         case OBD_IOC_CATLOGLIST: {
527                 int count = mds->mds_dt_desc.ld_tgt_count;
528                 rc = llog_catalog_list(obd, count, data);
529                 RETURN(rc);
530
531         }
532         case OBD_IOC_LLOG_CHECK:
533         case OBD_IOC_LLOG_CANCEL:
534         case OBD_IOC_LLOG_REMOVE: {
535                 struct llog_ctxt *ctxt =
536                         llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
537                 int rc2, valsize;
538                 __u32 group;
539
540                 obd_llog_finish(obd, &obd->obd_llogs,
541                                 mds->mds_dt_desc.ld_tgt_count);
542                 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
543                 rc = llog_ioctl(ctxt, cmd, data);
544                 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
545                 obd_llog_cat_initialize(obd, &obd->obd_llogs, 
546                                         mds->mds_dt_desc.ld_tgt_count,
547                                         CATLIST);
548                 group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
549                 valsize = sizeof(group);
550                 rc2 = obd_set_info(mds->mds_dt_exp, strlen("mds_conn"),
551                                    "mds_conn", valsize, &group);
552                 if (!rc)
553                         rc = rc2;
554                 RETURN(rc);
555         }
556         case OBD_IOC_LLOG_INFO:
557         case OBD_IOC_LLOG_PRINT: {
558                 struct llog_ctxt *ctxt =
559                         llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
560
561                 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
562                 rc = llog_ioctl(ctxt, cmd, data);
563                 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
564
565                 RETURN(rc);
566         }
567
568         case OBD_IOC_ABORT_RECOVERY:
569                 target_stop_recovery_thread(obd);
570                 RETURN(0);
571         case OBD_IOC_ROOT_SQUASH: {
572                 __u32 *p = (__u32 *) data->ioc_inlbuf1;
573
574                 if (data->ioc_inllen1 !=
575                     (sizeof(__u32) * 4 + sizeof(ptl_nid_t)))
576                         RETURN(-EINVAL);
577
578                 if (*p == 0) { /* get */
579                         p += 2;
580                         *p++ = mds->mds_squash_uid;
581                         *p++ = mds->mds_squash_gid;
582                         *((ptl_nid_t*) p) = mds->mds_nosquash_nid;
583                 } else { /* set */
584                         p += 2;
585                         mds->mds_squash_uid = *p++;
586                         mds->mds_squash_gid = *p++;
587                         mds->mds_nosquash_nid = *((ptl_nid_t*) p);
588                         CWARN("MDS: squash root to %d:%d, except nid 0x%llx\n",
589                                mds->mds_squash_uid, mds->mds_squash_gid,
590                                mds->mds_nosquash_nid);
591                 }
592                 RETURN(0);
593         }
594         default:
595                 CDEBUG(D_INFO, "unknown command %x\n", cmd);
596                 RETURN(-EINVAL);
597         }
598         RETURN(0);
599
600 }
601
602 struct mds_dt_sync_info {
603         struct obd_device *mlsi_obd;      /* the mds to sync */
604         struct obd_device *mlsi_watched;  /* new lov target */
605         int                mlsi_index;    /* index into mds_dt_objids */ 
606 };
607
608 int mds_dt_synchronize(void *data)
609 {
610         struct mds_dt_sync_info *mlsi = data;
611         struct llog_ctxt *ctxt;
612         struct obd_device *obd;
613         struct obd_device *watched;
614         struct mds_obd *mds;
615         struct obd_uuid *uuid;
616         obd_id vals[2];
617         unsigned long flags;
618         __u32  vallen;
619         __u32  group;
620         int old_count;
621         int count;
622         int index;
623         int rc;
624         char name[32] = "CATLIST";
625
626         lock_kernel();
627
628         ptlrpc_daemonize();
629
630         SIGNAL_MASK_LOCK(current, flags);
631         sigfillset(&current->blocked);
632         RECALC_SIGPENDING;
633         SIGNAL_MASK_UNLOCK(current, flags);
634
635         unlock_kernel();
636
637         obd = mlsi->mlsi_obd;
638         watched = mlsi->mlsi_watched;
639         index = mlsi->mlsi_index;
640
641         LASSERT(obd != NULL);
642         LASSERT(watched != NULL);
643
644         OBD_FREE(mlsi, sizeof(*mlsi));
645
646         mds = &obd->u.mds;
647         down(&mds->mds_orphan_recovery_sem);
648
649         uuid = &watched->u.cli.cl_import->imp_target_uuid;
650
651         group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
652         rc = obd_set_info(watched->obd_self_export, strlen("mds_conn"),
653                           "mds_conn", sizeof(group), &group);
654         if (rc)
655                 GOTO(cleanup, rc);
656
657         old_count = mds->mds_dt_desc.ld_tgt_count;
658
659         rc = mds_dt_update_desc(obd, mds->mds_dt_exp);
660         if (rc)
661                 GOTO(cleanup, rc);
662
663         count = mds->mds_dt_desc.ld_tgt_count;
664         LASSERT(count >= old_count);
665
666         vallen = sizeof(vals[1]);
667         rc = obd_get_info(watched->obd_self_export, strlen("last_id"),
668                           "last_id", &vallen, &vals[1]);
669         if (rc)
670                 GOTO(cleanup, rc);
671
672         /* we don't set next id manually, instead OSCs will set them
673          * during own recovery from DELORPHAN reply -bzzz */
674 #if 0
675         vals[0] = index;
676         rc = mds_dt_set_info(obd->obd_self_export, strlen("next_id"),
677                              "next_id", 2, vals);
678         if (rc)
679                 GOTO(cleanup, rc);
680 #endif
681
682         obd_llog_finish(obd, &obd->obd_llogs, old_count);
683         obd_llog_cat_initialize(obd, &obd->obd_llogs, count, name);
684
685         ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
686         LASSERT(ctxt != NULL);
687
688         rc = llog_connect(ctxt, count, NULL, NULL, uuid);
689         if (rc != 0) {
690                 CERROR("%s: failed at llog_origin_connect: %d\n", 
691                        obd->obd_name, rc);
692                 GOTO(cleanup, rc);
693         }
694         
695         CWARN("MDS %s: %s now active, resetting orphans\n",
696               obd->obd_name, uuid->uuid);
697
698         rc = mds_dt_clear_orphans(&obd->u.mds, uuid);
699         if (rc != 0) {
700                 CERROR("%s: failed at mds_dt_clear_orphans(): %d\n", 
701                        obd->obd_name, rc);
702                 GOTO(cleanup, rc);
703         }
704         rc = 0;
705
706         EXIT;
707 cleanup:
708         up(&mds->mds_orphan_recovery_sem);
709         return rc;
710 }
711
712 int mds_dt_start_synchronize(struct obd_device *obd,
713                              struct obd_device *watched, 
714                              void *data)
715 {
716         struct mds_dt_sync_info *mlsi;
717         int rc;
718         
719         ENTRY;
720
721         OBD_ALLOC(mlsi, sizeof(*mlsi));
722         if (mlsi == NULL)
723                 RETURN(-ENOMEM);
724
725         mlsi->mlsi_obd = obd;
726         mlsi->mlsi_watched = watched;
727         mlsi->mlsi_index = (int)data;
728
729         rc = kernel_thread(mds_dt_synchronize, mlsi, CLONE_VM | CLONE_FILES);
730         if (rc < 0)
731                 CERROR("%s: error starting mds_dt_synchronize(): %d\n", 
732                        obd->obd_name, rc);
733         else {
734                 CDEBUG(D_HA, "%s: mds_dt_synchronize() thread: %d\n", 
735                        obd->obd_name, rc);
736                 rc = 0;
737         }
738
739         RETURN(rc);
740 }
741
742 int mds_notify(struct obd_device *obd, struct obd_device *watched,
743                int active, void *data)
744 {
745         int rc = 0;
746         ENTRY;
747
748         if (!active)
749                 RETURN(0);
750
751         if (!strcmp(watched->obd_type->typ_name, OBD_MDC_DEVICENAME))
752                 RETURN(0);
753
754         if (strcmp(watched->obd_type->typ_name, OBD_OSC_DEVICENAME)) {
755                 CERROR("unexpected notification of %s %s!\n",
756                        watched->obd_type->typ_name, watched->obd_name);
757                 RETURN(-EINVAL);
758         }
759
760         if (obd->obd_recovering) {
761                 struct obd_uuid *uuid;
762                 uuid = &watched->u.cli.cl_import->imp_target_uuid;
763
764                 CWARN("MDS %s: in recovery, not resetting orphans on %s\n",
765                       obd->obd_name, uuid->uuid);
766         } else {
767                 rc = mds_dt_start_synchronize(obd, watched, data);
768         }
769         RETURN(rc);
770 }
771
772 int mds_dt_set_info(struct obd_export *exp, obd_count keylen,
773                     void *key, obd_count vallen, void *val)
774 {
775         struct obd_device *obd = class_exp2obd(exp);
776         struct mds_obd *mds = &obd->u.mds;
777         ENTRY;
778
779 #define KEY_IS(str) \
780         (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
781
782         if (KEY_IS("next_id")) {
783                 obd_id *id = (obd_id *)val;
784                 int idx;
785
786                 /* XXX - this really should be vallen != (2 * sizeof(*id)) *
787                  * Just following the precedent set by lov_set_info.       */
788                 if (vallen != 2)
789                         RETURN(-EINVAL);
790
791                 idx = *id;
792                 if ((idx != *id) || (idx >= mds->mds_dt_desc.ld_tgt_count))
793                         RETURN(-EINVAL);
794
795                 CDEBUG(D_CONFIG, "idx: %d id: %llu\n", idx, *(id + 1));
796
797                 mds->mds_dt_objids[idx] = *++id;
798                 CDEBUG(D_CONFIG, "objid: %d: %lld\n", idx, *id);
799                 /* XXX - should we be writing this out here ? */
800                 RETURN(mds_dt_write_objids(obd));
801         }
802
803         RETURN(-EINVAL);
804 }
805
806 int mds_dt_update_config(struct obd_device *obd, int clean)
807 {
808         struct mds_obd *mds = &obd->u.mds;
809         struct lvfs_run_ctxt saved;
810         struct config_llog_instance cfg;
811         struct llog_ctxt *ctxt;
812         char *profile = mds->mds_profile, *name;
813         int rc, version, namelen;
814         ENTRY;
815
816         if (profile == NULL)
817                 RETURN(0);
818
819         cfg.cfg_instance = NULL;
820         cfg.cfg_uuid = mds->mds_dt_uuid;
821
822         namelen = strlen(profile) + 20; /* -clean-######### */
823         OBD_ALLOC(name, namelen);
824         if (name == NULL)
825                 RETURN(-ENOMEM);
826
827         if (clean) {
828                 version = mds->mds_config_version - 1;
829                 sprintf(name, "%s-clean-%d", profile, version);
830         } else {
831                 version = mds->mds_config_version + 1;
832                 sprintf(name, "%s-%d", profile, version);
833         }
834
835         CWARN("Applying configuration log %s\n", name);
836
837         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
838         ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
839         rc = class_config_process_llog(ctxt, name, &cfg);
840         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
841         if (rc == 0)
842                 mds->mds_config_version = version;
843         CWARN("Finished applying configuration log %s: %d\n", name, rc);
844
845         OBD_FREE(name, namelen);
846         RETURN(rc);
847 }
848
849 /* Convert the on-disk LOV EA structre.
850  * We always try to convert from an old LOV EA format to the common in-memory
851  * (lsm) format (obd_unpackmd() understands the old on-disk (lmm) format) and
852  * then convert back to the new on-disk format and save it back to disk
853  * (obd_packmd() only ever saves to the new on-disk format) so we don't have
854  * to convert it each time this inode is accessed.
855  *
856  * This function is a bit interesting in the error handling.  We can safely
857  * ship the old lmm to the client in case of failure, since it uses the same
858  * obd_unpackmd() code and can do the conversion if the MDS fails for some
859  * reason.  We will not delete the old lmm data until we have written the
860  * new format lmm data in fsfilt_set_md(). */
861 int mds_convert_lov_ea(struct obd_device *obd, struct inode *inode,
862                        struct lov_mds_md *lmm, int lmm_size)
863 {
864         struct lov_stripe_md *lsm = NULL;
865         void *handle;
866         int rc, err;
867         ENTRY;
868
869         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC)
870                 RETURN(0);
871
872         CDEBUG(D_INODE, "converting LOV EA on %lu/%u from V0 to V1\n",      
873                 inode->i_ino, inode->i_generation);
874         rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, lmm, lmm_size);
875         if (rc < 0)
876                 GOTO(conv_end, rc);
877
878         rc = obd_packmd(obd->u.mds.mds_dt_exp, &lmm, lsm);
879         if (rc < 0)
880                 GOTO(conv_free, rc);
881         lmm_size = rc;
882
883         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
884         if (IS_ERR(handle)) {
885                 rc = PTR_ERR(handle);
886                 GOTO(conv_free, rc);
887         }
888
889         rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_LOV);
890         err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
891         if (!rc)
892                 rc = err ? err : lmm_size;
893         GOTO(conv_free, rc);
894 conv_free:
895         obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
896 conv_end:
897         return rc;
898 }
899
900 /* Must be called with i_sem held */
901 int mds_revalidate_lov_ea(struct obd_device *obd, struct inode *inode,
902                           struct lustre_msg *msg, int offset)
903 {
904         struct mds_obd *mds = &obd->u.mds;
905         struct obd_export *dt_exp = mds->mds_dt_exp;
906         struct lov_mds_md *lmm= NULL;
907         struct lov_stripe_md *lsm = NULL;
908         struct obdo *oa = NULL;
909         struct obd_trans_info oti = {0};
910         obd_valid valid = 0;
911         int lmm_size = 0, lsm_size = 0, err, rc;
912         void *handle;
913         ENTRY;
914
915         LASSERT(down_trylock(&inode->i_sem) != 0);
916
917         /* XXX - add way to know if EA is already up to date & return
918          * without doing anything. Easy to do since we get notified of
919          * LOV updates. */
920
921         lmm = lustre_msg_buf(msg, offset, 0);
922         if (lmm == NULL) {
923                 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
924                        inode->i_ino);
925                 RETURN(0);
926         }
927         lmm_size = msg->buflens[offset];
928
929         rc = obd_unpackmd(dt_exp, &lsm, lmm, lmm_size);
930         if (rc < 0)
931                 RETURN(0);
932
933         lsm_size = rc;
934
935         LASSERT(lsm->lsm_magic == LOV_MAGIC);
936
937         oa = obdo_alloc();
938         if (oa == NULL)
939                 GOTO(out_lsm, rc = -ENOMEM);
940         oa->o_mode = S_IFREG | 0600;
941         oa->o_id = inode->i_ino;
942         oa->o_generation = inode->i_generation;
943         oa->o_uid = 0; /* must have 0 uid / gid on OST */
944         oa->o_gid = 0;
945         oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
946
947         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLTYPE |
948                       OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID |
949                       OBD_MD_FLGROUP;
950         valid = OBD_MD_FLTYPE | OBD_MD_FLATIME | OBD_MD_FLMTIME |
951                 OBD_MD_FLCTIME;
952         obdo_from_inode(oa, inode, valid);
953
954         rc = obd_revalidate_md(dt_exp, oa, lsm, &oti);
955         if (rc == 0)
956                 GOTO(out_oa, rc);
957         if (rc < 0) {
958                 CERROR("Error validating LOV EA on %lu/%u: %d\n",
959                        inode->i_ino, inode->i_generation, rc);
960                 GOTO(out_oa, rc);
961         }
962
963         rc = obd_packmd(dt_exp, &lmm, lsm);
964         if (rc < 0)
965                 GOTO(out_oa, rc);
966         lmm_size = rc;
967
968         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
969         if (IS_ERR(handle)) {
970                 rc = PTR_ERR(handle);
971                 GOTO(out_oa, rc);
972         }
973
974         rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_LOV);
975         err = fsfilt_commit(obd, inode->i_sb, inode, handle, 0);
976         if (!rc)
977                 rc = err;
978
979         EXIT;
980 out_oa:
981         obdo_free(oa);
982 out_lsm:
983         obd_free_memmd(dt_exp, &lsm);
984         return rc;
985 }