Whamcloud - gitweb
b=7148
[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                         GOTO(err_reg, rc);
347                 rc = 0;
348         }
349
350         for (i = 0; i < 2; i++) {
351                 if (!mds->mds_capa_keys[i].k_key)
352                         break;
353                 rc = obd_set_info(mds->mds_dt_exp, strlen("capa_key"),
354                                   "capa_key", sizeof(struct lustre_capa_key),
355                                   mds->mds_capa_keys[i].k_key);
356                 if (rc)
357                         GOTO(err_reg, rc);
358         }
359         RETURN(rc);
360
361 err_reg:
362         obd_register_observer(mds->mds_dt_obd, NULL);
363 err_discon:
364         obd_disconnect(mds->mds_dt_exp, 0);
365         mds->mds_dt_obd = ERR_PTR(rc);
366         mds->mds_dt_exp = NULL;
367         return rc;
368 }
369
370 int mds_dt_disconnect(struct obd_device *obd, int flags)
371 {
372         struct mds_obd *mds = &obd->u.mds;
373         int rc = 0;
374         ENTRY;
375
376         if (!IS_ERR(mds->mds_dt_obd) && mds->mds_dt_exp != NULL) {
377                 /* cleanup all llogging subsystems */
378                 rc = obd_llog_finish(obd, &obd->obd_llogs,
379                                      mds->mds_dt_desc.ld_tgt_count);
380                 if (rc)
381                         CERROR("failed to cleanup llogging subsystems\n");
382
383                 obd_register_observer(mds->mds_dt_obd, NULL);
384
385                 rc = obd_disconnect(mds->mds_dt_exp, flags);
386                 /* if obd_disconnect fails (probably because the
387                  * export was disconnected by class_disconnect_exports)
388                  * then we just need to drop our ref. */
389                 if (rc != 0)
390                         class_export_put(mds->mds_dt_exp);
391                 mds->mds_dt_exp = NULL;
392                 mds->mds_dt_obd = NULL;
393         }
394
395         RETURN(rc);
396 }
397
398 int mds_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
399                   void *karg, void *uarg)
400 {
401         static struct obd_uuid cfg_uuid = { .uuid = "config_uuid" };
402         struct obd_device *obd = exp->exp_obd;
403         struct mds_obd *mds = &obd->u.mds;
404         struct obd_ioctl_data *data = karg;
405         struct lvfs_run_ctxt saved;
406         int rc = 0;
407         ENTRY;
408
409         CDEBUG(D_INFO, "ioctl cmd %x\n", cmd);
410         switch (cmd) {
411         case OBD_IOC_RECORD: {
412                 char *name = data->ioc_inlbuf1;
413                 if (mds->mds_cfg_llh)
414                         RETURN(-EBUSY);
415
416                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
417                 rc = llog_open(llog_get_context(&obd->obd_llogs, 
418                                                 LLOG_CONFIG_ORIG_CTXT),
419                                &mds->mds_cfg_llh, NULL, name,
420                                OBD_LLOG_FL_CREATE);
421                 if (rc == 0)
422                         llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
423                                          &cfg_uuid);
424                 else
425                         mds->mds_cfg_llh = NULL;
426                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
427
428                 RETURN(rc);
429         }
430
431         case OBD_IOC_ENDRECORD: {
432                 if (!mds->mds_cfg_llh)
433                         RETURN(-EBADF);
434
435                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
436                 rc = llog_close(mds->mds_cfg_llh);
437                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
438
439                 mds->mds_cfg_llh = NULL;
440                 RETURN(rc);
441         }
442
443         case OBD_IOC_CLEAR_LOG: {
444                 char *name = data->ioc_inlbuf1;
445                 if (mds->mds_cfg_llh)
446                         RETURN(-EBUSY);
447
448                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
449                 rc = llog_open(llog_get_context(&obd->obd_llogs, 
450                                                 LLOG_CONFIG_ORIG_CTXT),
451                                &mds->mds_cfg_llh, NULL, name,
452                                OBD_LLOG_FL_CREATE);
453                 if (rc == 0) {
454                         llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN,
455                                          NULL);
456
457                         rc = llog_destroy(mds->mds_cfg_llh);
458                         llog_free_handle(mds->mds_cfg_llh);
459                 }
460                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
461
462                 mds->mds_cfg_llh = NULL;
463                 RETURN(rc);
464         }
465
466         case OBD_IOC_DORECORD: {
467                 char *cfg_buf;
468                 struct llog_rec_hdr rec;
469                 if (!mds->mds_cfg_llh)
470                         RETURN(-EBADF);
471
472                 /* XXX - this probably should be a parameter to this ioctl.
473                  * For now, just use llh_max_transno for expediency. */
474                 rec.lrh_len = llog_data_len(data->ioc_plen1);
475
476                 if (data->ioc_type == LUSTRE_CFG_TYPE) {
477                         rec.lrh_type = OBD_CFG_REC;
478                 } else if (data->ioc_type == PORTALS_CFG_TYPE) {
479                         rec.lrh_type = PTL_CFG_REC;
480                 } else {
481                         CERROR("unknown cfg record type:%d \n", data->ioc_type);
482                         RETURN(-EINVAL);
483                 }
484
485                 OBD_ALLOC(cfg_buf, data->ioc_plen1);
486                 if (cfg_buf == NULL)
487                         RETURN(-EINVAL);
488                 rc = copy_from_user(cfg_buf, data->ioc_pbuf1, data->ioc_plen1);
489                 if (rc) {
490                         OBD_FREE(cfg_buf, data->ioc_plen1);
491                         RETURN(rc);
492                 }
493
494                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
495                 rc = llog_write_rec(mds->mds_cfg_llh, &rec, NULL, 0,
496                                     cfg_buf, -1);
497                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
498
499                 OBD_FREE(cfg_buf, data->ioc_plen1);
500                 RETURN(rc);
501         }
502         case OBD_IOC_PARSE: {
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_process_llog(ctxt, data->ioc_inlbuf1, NULL);
507                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
508                 if (rc)
509                         RETURN(rc);
510
511                 RETURN(rc);
512         }
513         case OBD_IOC_DUMP_LOG: {
514                 struct llog_ctxt *ctxt =
515                         llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
516                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
517                 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
518                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
519
520                 RETURN(rc);
521         }
522         case OBD_IOC_SET_READONLY: {
523                 void *handle;
524                 struct inode *inode = obd->u.mds.mds_sb->s_root->d_inode;
525                 BDEVNAME_DECLARE_STORAGE(tmp);
526                 CERROR("setting device %s read-only\n",
527                        ll_bdevname(obd->u.mds.mds_sb, tmp));
528
529                 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
530                 LASSERT(handle);
531                 rc = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 1);
532
533                 ll_set_rdonly(ll_sbdev(obd->u.mds.mds_sb));
534                 RETURN(0);
535         }
536
537         case OBD_IOC_CATLOGLIST: {
538                 int count = mds->mds_dt_desc.ld_tgt_count;
539                 rc = llog_catalog_list(obd, count, data);
540                 RETURN(rc);
541
542         }
543         case OBD_IOC_LLOG_CHECK:
544         case OBD_IOC_LLOG_CANCEL:
545         case OBD_IOC_LLOG_REMOVE: {
546                 struct llog_ctxt *ctxt =
547                         llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
548                 int rc2, valsize;
549                 __u32 group;
550
551                 obd_llog_finish(obd, &obd->obd_llogs,
552                                 mds->mds_dt_desc.ld_tgt_count);
553                 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
554                 rc = llog_ioctl(ctxt, cmd, data);
555                 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
556                 obd_llog_cat_initialize(obd, &obd->obd_llogs, 
557                                         mds->mds_dt_desc.ld_tgt_count,
558                                         CATLIST);
559                 group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
560                 valsize = sizeof(group);
561                 rc2 = obd_set_info(mds->mds_dt_exp, strlen("mds_conn"),
562                                    "mds_conn", valsize, &group);
563                 if (!rc)
564                         rc = rc2;
565                 RETURN(rc);
566         }
567         case OBD_IOC_LLOG_INFO:
568         case OBD_IOC_LLOG_PRINT: {
569                 struct llog_ctxt *ctxt =
570                         llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
571
572                 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
573                 rc = llog_ioctl(ctxt, cmd, data);
574                 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
575
576                 RETURN(rc);
577         }
578
579         case OBD_IOC_ABORT_RECOVERY:
580                 target_stop_recovery_thread(obd);
581                 RETURN(0);
582         case OBD_IOC_ROOT_SQUASH: {
583                 __u32 *p = (__u32 *) data->ioc_inlbuf1;
584
585                 if (data->ioc_inllen1 !=
586                     (sizeof(__u32) * 4 + sizeof(ptl_nid_t)))
587                         RETURN(-EINVAL);
588
589                 if (*p == 0) { /* get */
590                         p += 2;
591                         *p++ = mds->mds_squash_uid;
592                         *p++ = mds->mds_squash_gid;
593                         *((ptl_nid_t*) p) = mds->mds_nosquash_nid;
594                 } else { /* set */
595                         p += 2;
596                         mds->mds_squash_uid = *p++;
597                         mds->mds_squash_gid = *p++;
598                         mds->mds_nosquash_nid = *((ptl_nid_t*) p);
599                         CWARN("MDS: squash root to %d:%d, except nid 0x%llx\n",
600                                mds->mds_squash_uid, mds->mds_squash_gid,
601                                mds->mds_nosquash_nid);
602                 }
603                 RETURN(0);
604         }
605         default:
606                 CDEBUG(D_INFO, "unknown command %x\n", cmd);
607                 RETURN(-EINVAL);
608         }
609         RETURN(0);
610
611 }
612
613 struct mds_dt_sync_info {
614         struct obd_device *mlsi_obd;      /* the mds to sync */
615         struct obd_device *mlsi_watched;  /* new lov target */
616         int                mlsi_index;    /* index into mds_dt_objids */ 
617 };
618
619 int mds_dt_synchronize(void *data)
620 {
621         struct mds_dt_sync_info *mlsi = data;
622         struct llog_ctxt *ctxt;
623         struct obd_device *obd;
624         struct obd_device *watched;
625         struct mds_obd *mds;
626         struct obd_uuid *uuid;
627         obd_id vals[2];
628         unsigned long flags;
629         __u32  vallen, group;
630         int old_count, count, index, i;
631         int rc;
632         char name[32] = "CATLIST";
633
634         lock_kernel();
635
636         ptlrpc_daemonize();
637
638         SIGNAL_MASK_LOCK(current, flags);
639         sigfillset(&current->blocked);
640         RECALC_SIGPENDING;
641         SIGNAL_MASK_UNLOCK(current, flags);
642
643         unlock_kernel();
644
645         obd = mlsi->mlsi_obd;
646         watched = mlsi->mlsi_watched;
647         index = mlsi->mlsi_index;
648
649         LASSERT(obd != NULL);
650         LASSERT(watched != NULL);
651
652         OBD_FREE(mlsi, sizeof(*mlsi));
653
654         mds = &obd->u.mds;
655         down(&mds->mds_orphan_recovery_sem);
656
657         uuid = &watched->u.cli.cl_import->imp_target_uuid;
658         CWARN("MDS %s: %s now active, repairing the connection\n",
659               obd->obd_name, uuid->uuid);
660
661         group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
662         rc = obd_set_info(watched->obd_self_export, strlen("mds_conn"),
663                           "mds_conn", sizeof(group), &group);
664         if (rc)
665                 GOTO(cleanup, rc);
666
667         old_count = mds->mds_dt_desc.ld_tgt_count;
668
669         rc = mds_dt_update_desc(obd, mds->mds_dt_exp);
670         if (rc)
671                 GOTO(cleanup, rc);
672
673         count = mds->mds_dt_desc.ld_tgt_count;
674         LASSERT(count >= old_count);
675
676         vallen = sizeof(vals[1]);
677         rc = obd_get_info(watched->obd_self_export, strlen("last_id"),
678                           "last_id", &vallen, &vals[1]);
679         if (rc)
680                 GOTO(cleanup, rc);
681
682         for (i = 0; i < 2; i++) {
683                 if (!mds->mds_capa_keys[i].k_key)
684                         break;
685                 rc = obd_set_info(mds->mds_dt_exp, strlen("capa_key"),
686                                   "capa_key", sizeof(struct lustre_capa_key),
687                                   mds->mds_capa_keys[i].k_key);
688                 if (rc)
689                         GOTO(cleanup, rc);
690         }
691
692         /* we don't set next id manually, instead OSCs will set them
693          * during own recovery from DELORPHAN reply -bzzz */
694 #if 0
695         vals[0] = index;
696         rc = mds_dt_set_info(obd->obd_self_export, strlen("next_id"),
697                              "next_id", 2, vals);
698         if (rc)
699                 GOTO(cleanup, rc);
700 #endif
701
702         obd_llog_finish(obd, &obd->obd_llogs, old_count);
703         obd_llog_cat_initialize(obd, &obd->obd_llogs, count, name);
704
705         ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
706         LASSERT(ctxt != NULL);
707
708         rc = llog_connect(ctxt, count, NULL, NULL, uuid);
709         if (rc != 0) {
710                 CERROR("%s: failed at llog_origin_connect: %d\n", 
711                        obd->obd_name, rc);
712                 GOTO(cleanup, rc);
713         }
714         
715         CWARN("MDS %s: %s now active, resetting orphans\n",
716               obd->obd_name, uuid->uuid);
717
718         rc = mds_dt_clear_orphans(&obd->u.mds, uuid);
719         if (rc != 0) {
720                 CERROR("%s: failed at mds_dt_clear_orphans(): %d\n", 
721                        obd->obd_name, rc);
722                 GOTO(cleanup, rc);
723         }
724         rc = 0;
725
726         EXIT;
727 cleanup:
728         up(&mds->mds_orphan_recovery_sem);
729         return rc;
730 }
731
732 int mds_dt_start_synchronize(struct obd_device *obd,
733                              struct obd_device *watched, 
734                              void *data)
735 {
736         struct mds_dt_sync_info *mlsi;
737         int rc;
738         
739         ENTRY;
740
741         OBD_ALLOC(mlsi, sizeof(*mlsi));
742         if (mlsi == NULL)
743                 RETURN(-ENOMEM);
744
745         mlsi->mlsi_obd = obd;
746         mlsi->mlsi_watched = watched;
747         mlsi->mlsi_index = (int)data;
748
749         rc = kernel_thread(mds_dt_synchronize, mlsi, CLONE_VM | CLONE_FILES);
750         if (rc < 0)
751                 CERROR("%s: error starting mds_dt_synchronize(): %d\n", 
752                        obd->obd_name, rc);
753         else {
754                 CDEBUG(D_HA, "%s: mds_dt_synchronize() thread: %d\n", 
755                        obd->obd_name, rc);
756                 rc = 0;
757         }
758
759         RETURN(rc);
760 }
761
762 int mds_notify(struct obd_device *obd, struct obd_device *watched,
763                int active, void *data)
764 {
765         int rc = 0;
766         ENTRY;
767
768         if (!active)
769                 RETURN(0);
770
771         if (!strcmp(watched->obd_type->typ_name, OBD_MDC_DEVICENAME))
772                 RETURN(0);
773
774         if (strcmp(watched->obd_type->typ_name, OBD_OSC_DEVICENAME)) {
775                 CERROR("unexpected notification of %s %s!\n",
776                        watched->obd_type->typ_name, watched->obd_name);
777                 RETURN(-EINVAL);
778         }
779
780         if (obd->obd_recovering) {
781                 struct obd_uuid *uuid;
782                 uuid = &watched->u.cli.cl_import->imp_target_uuid;
783
784                 CWARN("MDS %s: in recovery, not resetting orphans on %s\n",
785                       obd->obd_name, uuid->uuid);
786         } else {
787                 rc = mds_dt_start_synchronize(obd, watched, data);
788         }
789         RETURN(rc);
790 }
791
792 int mds_dt_set_info(struct obd_export *exp, obd_count keylen,
793                     void *key, obd_count vallen, void *val)
794 {
795         struct obd_device *obd = class_exp2obd(exp);
796         struct mds_obd *mds = &obd->u.mds;
797         ENTRY;
798
799 #define KEY_IS(str) \
800         (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
801
802         if (KEY_IS("next_id")) {
803                 obd_id *id = (obd_id *)val;
804                 int idx;
805
806                 /* XXX - this really should be vallen != (2 * sizeof(*id)) *
807                  * Just following the precedent set by lov_set_info.       */
808                 if (vallen != 2)
809                         RETURN(-EINVAL);
810
811                 idx = *id;
812                 if ((idx != *id) || (idx >= mds->mds_dt_desc.ld_tgt_count))
813                         RETURN(-EINVAL);
814
815                 CDEBUG(D_CONFIG, "idx: %d id: %llu\n", idx, *(id + 1));
816
817                 mds->mds_dt_objids[idx] = *++id;
818                 CDEBUG(D_CONFIG, "objid: %d: %lld\n", idx, *id);
819                 /* XXX - should we be writing this out here ? */
820                 RETURN(mds_dt_write_objids(obd));
821         }
822
823         RETURN(-EINVAL);
824 }
825
826 int mds_dt_update_config(struct obd_device *obd, int clean)
827 {
828         struct mds_obd *mds = &obd->u.mds;
829         struct lvfs_run_ctxt saved;
830         struct llog_ctxt *ctxt;
831         struct config_llog_instance cfg;
832         char *profile = mds->mds_profile, *name;
833          int rc, version, namelen, value;
834         __u32 valsize;
835         ENTRY;
836
837         if (profile == NULL)
838                 RETURN(0);
839         cfg.cfg_instance = NULL;
840         cfg.cfg_uuid = mds->mds_dt_uuid;
841
842         namelen = strlen(profile) + 20; /* -clean-######### */
843         OBD_ALLOC(name, namelen);
844         if (name == NULL)
845                 RETURN(-ENOMEM);
846         if (clean) {
847                 version = mds->mds_config_version - 1;
848                 sprintf(name, "%s-clean-%d", profile, version);
849         } else {
850                 version = mds->mds_config_version + 1;
851                 sprintf(name, "%s-%d", profile, version);
852         }
853         CWARN("Applying configuration log %s\n", name);
854
855         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
856         ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
857         rc = class_config_process_llog(ctxt, name, &cfg);
858         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
859         CWARN("Finished applying configuration log %s: %d\n", name, rc);
860         if (rc != 0) { 
861                 GOTO(exit, rc);
862         }
863         /* retrieve size of EA */
864         rc = obd_get_info(mds->mds_md_exp, strlen("mdsize"),
865                           "mdsize", &valsize, &value);
866         
867         if (value > mds->mds_max_mdsize)
868                 mds->mds_max_mdsize = value;
869
870         CDEBUG(D_INFO, "mds max md size %d \n", mds->mds_max_mdsize);
871         
872         if (rc == 0)
873                 mds->mds_config_version = version;
874 exit:
875         OBD_FREE(name, namelen);
876         RETURN(rc);
877 }
878
879 /* Convert the on-disk LOV EA structre.
880  * We always try to convert from an old LOV EA format to the common in-memory
881  * (lsm) format (obd_unpackmd() understands the old on-disk (lmm) format) and
882  * then convert back to the new on-disk format and save it back to disk
883  * (obd_packmd() only ever saves to the new on-disk format) so we don't have
884  * to convert it each time this inode is accessed.
885  *
886  * This function is a bit interesting in the error handling.  We can safely
887  * ship the old lmm to the client in case of failure, since it uses the same
888  * obd_unpackmd() code and can do the conversion if the MDS fails for some
889  * reason.  We will not delete the old lmm data until we have written the
890  * new format lmm data in fsfilt_set_md(). */
891 int mds_convert_lov_ea(struct obd_device *obd, struct inode *inode,
892                        struct lov_mds_md *lmm, int lmm_size)
893 {
894         struct lov_stripe_md *lsm = NULL;
895         void *handle;
896         int rc, err;
897         ENTRY;
898
899         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC)
900                 RETURN(0);
901
902         CDEBUG(D_INODE, "converting LOV EA on %lu/%u from V0 to V1\n",      
903                 inode->i_ino, inode->i_generation);
904         rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, lmm, lmm_size);
905         if (rc < 0)
906                 GOTO(conv_end, rc);
907
908         rc = obd_packmd(obd->u.mds.mds_dt_exp, &lmm, lsm);
909         if (rc < 0)
910                 GOTO(conv_free, rc);
911         lmm_size = rc;
912
913         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
914         if (IS_ERR(handle)) {
915                 rc = PTR_ERR(handle);
916                 GOTO(conv_free, rc);
917         }
918
919         rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_LOV);
920         err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
921         if (!rc)
922                 rc = err ? err : lmm_size;
923         GOTO(conv_free, rc);
924 conv_free:
925         obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
926 conv_end:
927         return rc;
928 }
929
930 /* Must be called with i_sem held */
931 int mds_revalidate_lov_ea(struct obd_device *obd, struct dentry *dentry,
932                           struct lustre_msg *msg, int offset)
933 {
934         struct mds_obd *mds = &obd->u.mds;
935         struct obd_export *dt_exp = mds->mds_dt_exp;
936         struct inode *inode = dentry->d_inode;
937         struct lov_mds_md *lmm= NULL;
938         struct lov_stripe_md *lsm = NULL;
939         struct obdo *oa = NULL;
940         struct obd_trans_info oti = {0};
941         struct iattr iattr = { 0 };
942         obd_valid valid = 0;
943         char idname[LL_ID_NAMELEN];
944         int lmm_size = 0, lsm_size = 0, err, rc;
945         void *handle;
946         ENTRY;
947
948         LASSERT(down_trylock(&inode->i_sem) != 0);
949
950         ll_id2str(idname, inode->i_ino, inode->i_generation);
951
952         /* XXX - add way to know if EA is already up to date & return
953          * without doing anything. Easy to do since we get notified of
954          * LOV updates. */
955
956         lmm = lustre_msg_buf(msg, offset, 0);
957         if (lmm == NULL) {
958                 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
959                        inode->i_ino);
960                 RETURN(0);
961         }
962         lmm_size = msg->buflens[offset];
963
964         rc = obd_unpackmd(dt_exp, &lsm, lmm, lmm_size);
965         if (rc < 0)
966                 RETURN(0);
967
968         lsm_size = rc;
969
970         LASSERT(lsm->lsm_magic == LOV_MAGIC);
971
972         oa = obdo_alloc();
973         if (oa == NULL)
974                 GOTO(out_lsm, rc = -ENOMEM);
975         oa->o_mode = S_IFREG | 0600;
976         oa->o_id = inode->i_ino;
977         oa->o_generation = inode->i_generation;
978         oa->o_uid = 0; /* must have 0 uid / gid on OST */
979         oa->o_gid = 0;
980         oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
981
982         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLTYPE |
983                       OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID |
984                       OBD_MD_FLGROUP;
985         valid = OBD_MD_FLTYPE | OBD_MD_FLATIME | OBD_MD_FLMTIME |
986                 OBD_MD_FLCTIME;
987         obdo_from_inode(oa, inode, valid);
988
989         rc = obd_revalidate_md(dt_exp, oa, lsm, &oti);
990         if (rc == 0)
991                 GOTO(out_oa, rc);
992         if (rc < 0) {
993                 CERROR("Error validating LOV EA on %lu/%u: %d\n",
994                        inode->i_ino, inode->i_generation, rc);
995                 GOTO(out_oa, rc);
996         }
997
998         rc = obd_packmd(dt_exp, &lmm, lsm);
999         if (rc < 0)
1000                 GOTO(out_oa, rc);
1001         lmm_size = rc;
1002
1003         DOWN_WRITE_I_ALLOC_SEM(inode);
1004         mds_inode_set_attrs_old(inode);
1005         UP_WRITE_I_ALLOC_SEM(inode);
1006
1007         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
1008         if (IS_ERR(handle)) {
1009                 rc = PTR_ERR(handle);
1010                 GOTO(out_oa, rc);
1011         }
1012
1013         rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_LOV);
1014         if (rc) {
1015                 CERROR("error in fsfilt_set_md(%s): rc %d\n", idname, rc);
1016                 GOTO(commit, rc);
1017         }
1018
1019         /* the EA has changed - let's update i_size/i_blocks */
1020         /* NOTE: that we're calling mds_validate_size() with the i_sem held. */
1021         mds_validate_size(obd, inode, NULL, lsm, &iattr);
1022         if (iattr.ia_valid != 0) {
1023                 rc = fsfilt_setattr(obd, dentry, handle, &iattr, 0);
1024                 if (rc)
1025                         CERROR("error in setattr(%s): rc %d\n", idname, rc);
1026         }
1027 commit:
1028         err = fsfilt_commit(obd, inode->i_sb, inode, handle, 0);
1029         if (!rc)
1030                 rc = err;
1031         EXIT;
1032 out_oa:
1033         obdo_free(oa);
1034 out_lsm:
1035         obd_free_memmd(dt_exp, &lsm);
1036         return rc;
1037 }