Whamcloud - gitweb
Land b_hd_capa onto HEAD (20050809_1942)
[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
659         group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
660         rc = obd_set_info(watched->obd_self_export, strlen("mds_conn"),
661                           "mds_conn", sizeof(group), &group);
662         if (rc)
663                 GOTO(cleanup, rc);
664
665         old_count = mds->mds_dt_desc.ld_tgt_count;
666
667         rc = mds_dt_update_desc(obd, mds->mds_dt_exp);
668         if (rc)
669                 GOTO(cleanup, rc);
670
671         count = mds->mds_dt_desc.ld_tgt_count;
672         LASSERT(count >= old_count);
673
674         vallen = sizeof(vals[1]);
675         rc = obd_get_info(watched->obd_self_export, strlen("last_id"),
676                           "last_id", &vallen, &vals[1]);
677         if (rc)
678                 GOTO(cleanup, rc);
679
680         for (i = 0; i < 2; i++) {
681                 if (!mds->mds_capa_keys[i].k_key)
682                         break;
683                 rc = obd_set_info(mds->mds_dt_exp, strlen("capa_key"),
684                                   "capa_key", sizeof(struct lustre_capa_key),
685                                   mds->mds_capa_keys[i].k_key);
686                 if (rc)
687                         GOTO(cleanup, rc);
688         }
689
690         /* we don't set next id manually, instead OSCs will set them
691          * during own recovery from DELORPHAN reply -bzzz */
692 #if 0
693         vals[0] = index;
694         rc = mds_dt_set_info(obd->obd_self_export, strlen("next_id"),
695                              "next_id", 2, vals);
696         if (rc)
697                 GOTO(cleanup, rc);
698 #endif
699
700         obd_llog_finish(obd, &obd->obd_llogs, old_count);
701         obd_llog_cat_initialize(obd, &obd->obd_llogs, count, name);
702
703         ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
704         LASSERT(ctxt != NULL);
705
706         rc = llog_connect(ctxt, count, NULL, NULL, uuid);
707         if (rc != 0) {
708                 CERROR("%s: failed at llog_origin_connect: %d\n", 
709                        obd->obd_name, rc);
710                 GOTO(cleanup, rc);
711         }
712         
713         CWARN("MDS %s: %s now active, resetting orphans\n",
714               obd->obd_name, uuid->uuid);
715
716         rc = mds_dt_clear_orphans(&obd->u.mds, uuid);
717         if (rc != 0) {
718                 CERROR("%s: failed at mds_dt_clear_orphans(): %d\n", 
719                        obd->obd_name, rc);
720                 GOTO(cleanup, rc);
721         }
722         rc = 0;
723
724         EXIT;
725 cleanup:
726         up(&mds->mds_orphan_recovery_sem);
727         return rc;
728 }
729
730 int mds_dt_start_synchronize(struct obd_device *obd,
731                              struct obd_device *watched, 
732                              void *data)
733 {
734         struct mds_dt_sync_info *mlsi;
735         int rc;
736         
737         ENTRY;
738
739         OBD_ALLOC(mlsi, sizeof(*mlsi));
740         if (mlsi == NULL)
741                 RETURN(-ENOMEM);
742
743         mlsi->mlsi_obd = obd;
744         mlsi->mlsi_watched = watched;
745         mlsi->mlsi_index = (int)data;
746
747         rc = kernel_thread(mds_dt_synchronize, mlsi, CLONE_VM | CLONE_FILES);
748         if (rc < 0)
749                 CERROR("%s: error starting mds_dt_synchronize(): %d\n", 
750                        obd->obd_name, rc);
751         else {
752                 CDEBUG(D_HA, "%s: mds_dt_synchronize() thread: %d\n", 
753                        obd->obd_name, rc);
754                 rc = 0;
755         }
756
757         RETURN(rc);
758 }
759
760 int mds_notify(struct obd_device *obd, struct obd_device *watched,
761                int active, void *data)
762 {
763         int rc = 0;
764         ENTRY;
765
766         if (!active)
767                 RETURN(0);
768
769         if (!strcmp(watched->obd_type->typ_name, OBD_MDC_DEVICENAME))
770                 RETURN(0);
771
772         if (strcmp(watched->obd_type->typ_name, OBD_OSC_DEVICENAME)) {
773                 CERROR("unexpected notification of %s %s!\n",
774                        watched->obd_type->typ_name, watched->obd_name);
775                 RETURN(-EINVAL);
776         }
777
778         if (obd->obd_recovering) {
779                 struct obd_uuid *uuid;
780                 uuid = &watched->u.cli.cl_import->imp_target_uuid;
781
782                 CWARN("MDS %s: in recovery, not resetting orphans on %s\n",
783                       obd->obd_name, uuid->uuid);
784         } else {
785                 rc = mds_dt_start_synchronize(obd, watched, data);
786         }
787         RETURN(rc);
788 }
789
790 int mds_dt_set_info(struct obd_export *exp, obd_count keylen,
791                     void *key, obd_count vallen, void *val)
792 {
793         struct obd_device *obd = class_exp2obd(exp);
794         struct mds_obd *mds = &obd->u.mds;
795         ENTRY;
796
797 #define KEY_IS(str) \
798         (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
799
800         if (KEY_IS("next_id")) {
801                 obd_id *id = (obd_id *)val;
802                 int idx;
803
804                 /* XXX - this really should be vallen != (2 * sizeof(*id)) *
805                  * Just following the precedent set by lov_set_info.       */
806                 if (vallen != 2)
807                         RETURN(-EINVAL);
808
809                 idx = *id;
810                 if ((idx != *id) || (idx >= mds->mds_dt_desc.ld_tgt_count))
811                         RETURN(-EINVAL);
812
813                 CDEBUG(D_CONFIG, "idx: %d id: %llu\n", idx, *(id + 1));
814
815                 mds->mds_dt_objids[idx] = *++id;
816                 CDEBUG(D_CONFIG, "objid: %d: %lld\n", idx, *id);
817                 /* XXX - should we be writing this out here ? */
818                 RETURN(mds_dt_write_objids(obd));
819         }
820
821         RETURN(-EINVAL);
822 }
823
824 int mds_dt_update_config(struct obd_device *obd, int clean)
825 {
826         struct mds_obd *mds = &obd->u.mds;
827         struct lvfs_run_ctxt saved;
828         struct config_llog_instance cfg;
829         struct llog_ctxt *ctxt;
830         char *profile = mds->mds_profile, *name;
831         int rc, version, namelen;
832         ENTRY;
833
834         if (profile == NULL)
835                 RETURN(0);
836
837         cfg.cfg_instance = NULL;
838         cfg.cfg_uuid = mds->mds_dt_uuid;
839
840         namelen = strlen(profile) + 20; /* -clean-######### */
841         OBD_ALLOC(name, namelen);
842         if (name == NULL)
843                 RETURN(-ENOMEM);
844
845         if (clean) {
846                 version = mds->mds_config_version - 1;
847                 sprintf(name, "%s-clean-%d", profile, version);
848         } else {
849                 version = mds->mds_config_version + 1;
850                 sprintf(name, "%s-%d", profile, version);
851         }
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         if (rc == 0)
860                 mds->mds_config_version = version;
861         CWARN("Finished applying configuration log %s: %d\n", name, rc);
862
863         OBD_FREE(name, namelen);
864         RETURN(rc);
865 }
866
867 /* Convert the on-disk LOV EA structre.
868  * We always try to convert from an old LOV EA format to the common in-memory
869  * (lsm) format (obd_unpackmd() understands the old on-disk (lmm) format) and
870  * then convert back to the new on-disk format and save it back to disk
871  * (obd_packmd() only ever saves to the new on-disk format) so we don't have
872  * to convert it each time this inode is accessed.
873  *
874  * This function is a bit interesting in the error handling.  We can safely
875  * ship the old lmm to the client in case of failure, since it uses the same
876  * obd_unpackmd() code and can do the conversion if the MDS fails for some
877  * reason.  We will not delete the old lmm data until we have written the
878  * new format lmm data in fsfilt_set_md(). */
879 int mds_convert_lov_ea(struct obd_device *obd, struct inode *inode,
880                        struct lov_mds_md *lmm, int lmm_size)
881 {
882         struct lov_stripe_md *lsm = NULL;
883         void *handle;
884         int rc, err;
885         ENTRY;
886
887         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC)
888                 RETURN(0);
889
890         CDEBUG(D_INODE, "converting LOV EA on %lu/%u from V0 to V1\n",      
891                 inode->i_ino, inode->i_generation);
892         rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, lmm, lmm_size);
893         if (rc < 0)
894                 GOTO(conv_end, rc);
895
896         rc = obd_packmd(obd->u.mds.mds_dt_exp, &lmm, lsm);
897         if (rc < 0)
898                 GOTO(conv_free, rc);
899         lmm_size = rc;
900
901         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
902         if (IS_ERR(handle)) {
903                 rc = PTR_ERR(handle);
904                 GOTO(conv_free, rc);
905         }
906
907         rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_LOV);
908         err = fsfilt_commit(obd, obd->u.mds.mds_sb, inode, handle, 0);
909         if (!rc)
910                 rc = err ? err : lmm_size;
911         GOTO(conv_free, rc);
912 conv_free:
913         obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
914 conv_end:
915         return rc;
916 }
917
918 /* Must be called with i_sem held */
919 int mds_revalidate_lov_ea(struct obd_device *obd, struct inode *inode,
920                           struct lustre_msg *msg, int offset)
921 {
922         struct mds_obd *mds = &obd->u.mds;
923         struct obd_export *dt_exp = mds->mds_dt_exp;
924         struct lov_mds_md *lmm= NULL;
925         struct lov_stripe_md *lsm = NULL;
926         struct obdo *oa = NULL;
927         struct obd_trans_info oti = {0};
928         obd_valid valid = 0;
929         int lmm_size = 0, lsm_size = 0, err, rc;
930         void *handle;
931         ENTRY;
932
933         LASSERT(down_trylock(&inode->i_sem) != 0);
934
935         /* XXX - add way to know if EA is already up to date & return
936          * without doing anything. Easy to do since we get notified of
937          * LOV updates. */
938
939         lmm = lustre_msg_buf(msg, offset, 0);
940         if (lmm == NULL) {
941                 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
942                        inode->i_ino);
943                 RETURN(0);
944         }
945         lmm_size = msg->buflens[offset];
946
947         rc = obd_unpackmd(dt_exp, &lsm, lmm, lmm_size);
948         if (rc < 0)
949                 RETURN(0);
950
951         lsm_size = rc;
952
953         LASSERT(lsm->lsm_magic == LOV_MAGIC);
954
955         oa = obdo_alloc();
956         if (oa == NULL)
957                 GOTO(out_lsm, rc = -ENOMEM);
958         oa->o_mode = S_IFREG | 0600;
959         oa->o_id = inode->i_ino;
960         oa->o_generation = inode->i_generation;
961         oa->o_uid = 0; /* must have 0 uid / gid on OST */
962         oa->o_gid = 0;
963         oa->o_gr = FILTER_GROUP_FIRST_MDS + mds->mds_num;
964
965         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FLTYPE |
966                       OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID |
967                       OBD_MD_FLGROUP;
968         valid = OBD_MD_FLTYPE | OBD_MD_FLATIME | OBD_MD_FLMTIME |
969                 OBD_MD_FLCTIME;
970         obdo_from_inode(oa, inode, valid);
971
972         rc = obd_revalidate_md(dt_exp, oa, lsm, &oti);
973         if (rc == 0)
974                 GOTO(out_oa, rc);
975         if (rc < 0) {
976                 CERROR("Error validating LOV EA on %lu/%u: %d\n",
977                        inode->i_ino, inode->i_generation, rc);
978                 GOTO(out_oa, rc);
979         }
980
981         rc = obd_packmd(dt_exp, &lmm, lsm);
982         if (rc < 0)
983                 GOTO(out_oa, rc);
984         lmm_size = rc;
985
986         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
987         if (IS_ERR(handle)) {
988                 rc = PTR_ERR(handle);
989                 GOTO(out_oa, rc);
990         }
991
992         rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size, EA_LOV);
993         err = fsfilt_commit(obd, inode->i_sb, inode, handle, 0);
994         if (!rc)
995                 rc = err;
996
997         EXIT;
998 out_oa:
999         obdo_free(oa);
1000 out_lsm:
1001         obd_free_memmd(dt_exp, &lsm);
1002         return rc;
1003 }