Whamcloud - gitweb
remove mount age check
[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/lustre_idl.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd_lov.h>
36 #include <linux/lustre_lib.h>
37 #include <linux/lustre_fsfilt.h>
38
39 #include "mds_internal.h"
40
41 void le_lov_desc_to_cpu (struct lov_desc *ld)
42 {
43         ld->ld_tgt_count = le32_to_cpu (ld->ld_tgt_count);
44         ld->ld_default_stripe_count = le32_to_cpu (ld->ld_default_stripe_count);
45         ld->ld_default_stripe_size = le32_to_cpu (ld->ld_default_stripe_size);
46         ld->ld_pattern = le32_to_cpu (ld->ld_pattern);
47 }
48
49 void cpu_to_le_lov_desc (struct lov_desc *ld)
50 {
51         ld->ld_tgt_count = cpu_to_le32 (ld->ld_tgt_count);
52         ld->ld_default_stripe_count = cpu_to_le32 (ld->ld_default_stripe_count);
53         ld->ld_default_stripe_size = cpu_to_le32 (ld->ld_default_stripe_size);
54         ld->ld_pattern = cpu_to_le32 (ld->ld_pattern);
55 }
56
57 void mds_lov_update_objids(struct obd_device *obd, obd_id *ids)
58 {
59         struct mds_obd *mds = &obd->u.mds;
60         int i;
61         ENTRY;
62
63         lock_kernel();
64         for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
65                 if (ids[i] > (mds->mds_lov_objids)[i])
66                         (mds->mds_lov_objids)[i] = ids[i];
67         unlock_kernel();
68         EXIT;
69 }
70
71 static int mds_lov_read_objids(struct obd_device *obd)
72 {
73         struct mds_obd *mds = &obd->u.mds;
74         obd_id *ids;
75         loff_t off = 0;
76         int i, rc, size = mds->mds_lov_desc.ld_tgt_count * sizeof(*ids);
77         ENTRY;
78
79         if (mds->mds_lov_objids != NULL)
80                 RETURN(0);
81
82         OBD_ALLOC(ids, size);
83         if (ids == NULL)
84                 RETURN(-ENOMEM);
85         mds->mds_lov_objids = ids;
86
87         if (mds->mds_lov_objid_filp->f_dentry->d_inode->i_size == 0)
88                 RETURN(0);
89         rc = fsfilt_read_record(obd, mds->mds_lov_objid_filp, ids, size, &off);
90         if (rc < 0) {
91                 CERROR("Error reading objids %d\n", rc);
92         } else {
93                 mds->mds_lov_objids_valid = 1;
94                 rc = 0;
95         }
96
97         for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
98                 CDEBUG(D_INFO, "read last object "LPU64" for idx %d\n",
99                        mds->mds_lov_objids[i], i);
100
101         RETURN(rc);
102 }
103
104 int mds_lov_write_objids(struct obd_device *obd)
105 {
106         struct mds_obd *mds = &obd->u.mds;
107         loff_t off = 0;
108         int i, rc, size = mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id);
109         ENTRY;
110
111         for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
112                 CDEBUG(D_INFO, "writing last object "LPU64" for idx %d\n",
113                        mds->mds_lov_objids[i], i);
114
115         rc = fsfilt_write_record(obd, mds->mds_lov_objid_filp,
116                                  mds->mds_lov_objids, size, &off, 0);
117         RETURN(rc);
118 }
119
120 static int mds_lov_clearorphans(struct mds_obd *mds, struct obd_uuid *ost_uuid)
121 {
122         int rc;
123         struct obdo oa;
124         struct obd_trans_info oti = {0};
125         struct lov_stripe_md  *empty_ea = NULL;
126         ENTRY;
127
128         LASSERT(mds->mds_lov_objids != NULL);
129
130         /* This create will in fact either create or destroy:  If the OST is
131          * missing objects below this ID, they will be created.  If it finds
132          * objects above this ID, they will be removed. */
133         memset(&oa, 0, sizeof(oa));
134         oa.o_valid = OBD_MD_FLFLAGS;
135         oa.o_flags = OBD_FL_DELORPHAN;
136         if (ost_uuid != NULL) {
137                 memcpy(&oa.o_inline, ost_uuid, sizeof(*ost_uuid));
138                 oa.o_valid |= OBD_MD_FLINLINE;
139         }
140         rc = obd_create(mds->mds_osc_exp, &oa, &empty_ea, &oti);
141
142         RETURN(rc);
143 }
144
145 /* update the LOV-OSC knowledge of the last used object id's */
146 int mds_lov_set_nextid(struct obd_device *obd)
147 {
148         struct mds_obd *mds = &obd->u.mds;
149         int rc;
150         ENTRY;
151
152         LASSERT(!obd->obd_recovering);
153
154         LASSERT(mds->mds_lov_objids != NULL);
155
156         rc = obd_set_info(mds->mds_osc_exp, strlen("next_id"), "next_id",
157                           mds->mds_lov_desc.ld_tgt_count, mds->mds_lov_objids);
158         if (rc < 0)
159                 GOTO(out, rc);
160
161         rc = mds_lov_clearorphans(mds, NULL /* all OSTs */);
162         if (rc < 0)
163                 GOTO(out, rc);
164
165 out:
166         if (rc && mds->mds_lov_objids) {
167                 /* Might as well crash here, until we figure out what to do.
168                  * If we OBD_FREE, we'll just LASSERT the next time through this
169                  * function. */
170                 LBUG();
171                 OBD_FREE(mds->mds_lov_objids,
172                          mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id));
173                 mds->mds_lov_objids = NULL;
174         }
175
176         RETURN(rc);
177 }
178
179 /* tell the LOV-OSC by how much to pre-create */
180 int mds_lov_set_growth(struct mds_obd *mds, int count)
181 {
182         int rc;
183         ENTRY;
184
185         rc = obd_set_info(mds->mds_osc_exp, strlen("growth_count"),
186                           "growth_count", sizeof(count), &count);
187
188         RETURN(rc);
189 }
190
191 int mds_lov_connect(struct obd_device *obd, char * lov_name)
192 {
193         struct mds_obd *mds = &obd->u.mds;
194         struct lustre_handle conn = {0,};
195         int valsize;
196         int rc, i;
197         ENTRY;
198
199         if (IS_ERR(mds->mds_osc_obd))
200                 RETURN(PTR_ERR(mds->mds_osc_obd));
201
202         if (mds->mds_osc_obd)
203                 RETURN(0);
204
205         mds->mds_osc_obd = class_name2obd(lov_name);
206         if (!mds->mds_osc_obd) {
207                 CERROR("MDS cannot locate LOV %s\n",
208                        lov_name);
209                 mds->mds_osc_obd = ERR_PTR(-ENOTCONN);
210                 RETURN(-ENOTCONN);
211         }
212
213         rc = obd_connect(&conn, mds->mds_osc_obd, &obd->obd_uuid);
214         if (rc) {
215                 CERROR("MDS cannot connect to LOV %s (%d)\n",
216                        lov_name, rc);
217                 mds->mds_osc_obd = ERR_PTR(rc);
218                 RETURN(rc);
219         }
220         mds->mds_osc_exp = class_conn2export(&conn);
221
222         rc = obd_register_observer(mds->mds_osc_obd, obd);
223         if (rc) {
224                 CERROR("MDS cannot register as observer of LOV %s (%d)\n",
225                        lov_name, rc);
226                 GOTO(err_discon, rc);
227         }
228
229         valsize = sizeof(mds->mds_lov_desc);
230         rc = obd_get_info(mds->mds_osc_exp, strlen("lovdesc") + 1, "lovdesc", 
231                           &valsize, &mds->mds_lov_desc);
232         if (rc) 
233                 GOTO(err_reg, rc);
234
235         mds->mds_max_mdsize = lov_mds_md_size(mds->mds_lov_desc.ld_tgt_count);
236         mds->mds_max_cookiesize = mds->mds_lov_desc.ld_tgt_count*
237                 sizeof(struct llog_cookie);
238         mds->mds_has_lov_desc = 1;
239         rc = mds_lov_read_objids(obd);
240         if (rc) {
241                 CERROR("cannot read %s: rc = %d\n", "lov_objids", rc);
242                 GOTO(err_reg, rc);
243         } 
244
245 #ifdef ENABLE_ORPHANS
246         rc = llog_cat_initialize(obd, mds->mds_lov_desc.ld_tgt_count);
247         if (rc) {
248                 CERROR("failed to initialize catalog %d\n", rc);
249                 GOTO(err_reg, rc);
250         }
251 #endif
252         /* FIXME before this set info call is made, we must initialize the logging */
253         rc = obd_set_info(mds->mds_osc_exp, strlen("mds_conn"), "mds_conn",
254                           0, NULL);
255         if (rc) 
256                 GOTO(err_reg, rc);
257         
258         /* If we're mounting this code for the first time on an existing FS,
259          * we need to populate the objids array from the real OST values */
260         if (!mds->mds_lov_objids_valid) {
261                 int size = sizeof(obd_id) * mds->mds_lov_desc.ld_tgt_count;
262                 rc = obd_get_info(mds->mds_osc_exp, strlen("last_id"),
263                                   "last_id", &size, mds->mds_lov_objids);
264                 if (!rc) {
265                         for (i = 0; i < mds->mds_lov_desc.ld_tgt_count; i++)
266                                 CWARN("got last object "LPU64" from OST %d\n",
267                                       mds->mds_lov_objids[i], i);
268                         mds->mds_lov_objids_valid = 1;
269                         rc = mds_lov_write_objids(obd);
270                         if (rc)
271                                 CERROR("got last objids from OSTs, but error "
272                                        "writing objids file: %d\n", rc);
273                 }
274         }
275
276         /* I want to see a callback happen when the OBD moves to a
277          * "For General Use" state, and that's when we'll call
278          * set_nextid().  The class driver can help us here, because
279          * it can use the obd_recovering flag to determine when the
280          * the OBD is full available. */
281         if (!obd->obd_recovering) {
282 #ifdef ENABLE_ORPHANS
283                 rc = llog_connect(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT),
284                                   obd->u.mds.mds_lov_desc.ld_tgt_count, NULL, NULL);
285                 if (rc != 0) {
286                         CERROR("faild at llog_origin_connect: %d\n", rc);
287                 }
288 #endif
289                 rc = mds_cleanup_orphans(obd);
290                 if (rc > 0)
291                         CERROR("Cleanup %d orphans while MDS isn't recovering\n", rc);
292
293                 rc = mds_lov_set_nextid(obd);
294                 if (rc)
295                         GOTO(err_llog, rc);
296         }
297         RETURN(rc);
298
299 err_llog:
300 #ifdef ENABLE_ORPHANS
301         /* cleanup all llogging subsystems */
302         rc = obd_llog_finish(obd, mds->mds_lov_desc.ld_tgt_count);
303         if (rc) 
304                 CERROR("failed to cleanup llogging subsystems\n");
305 #endif
306 err_reg:
307         obd_register_observer(mds->mds_osc_obd, NULL);
308 err_discon:
309         obd_disconnect(mds->mds_osc_exp, 0);
310         mds->mds_osc_exp = NULL;
311         mds->mds_osc_obd = ERR_PTR(rc);
312         RETURN(rc);
313 }
314
315 int mds_lov_disconnect(struct obd_device *obd, int flags)
316 {
317         struct mds_obd *mds = &obd->u.mds;
318         int rc = 0;
319         ENTRY;
320
321         if (!IS_ERR(mds->mds_osc_obd) && mds->mds_osc_exp != NULL) {
322 #ifdef ENABLE_ORPHANS
323                 /* cleanup all llogging subsystems */
324                 rc = obd_llog_finish(obd, mds->mds_lov_desc.ld_tgt_count);
325                 if (rc) 
326                         CERROR("failed to cleanup llogging subsystems\n");
327 #endif
328
329                 obd_register_observer(mds->mds_osc_obd, NULL);
330
331                 rc = obd_disconnect(mds->mds_osc_exp, flags);
332                 /* if obd_disconnect fails (probably because the
333                  * export was disconnected by class_disconnect_exports)
334                  * then we just need to drop our ref. */
335                 if (rc != 0)
336                         class_export_put(mds->mds_osc_exp);
337                 mds->mds_osc_exp = NULL;
338                 mds->mds_osc_obd = NULL;
339         }
340
341         RETURN(rc);
342 }
343
344 int mds_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
345                   void *karg, void *uarg)
346 {
347         static struct obd_uuid cfg_uuid = { .uuid = "config_uuid" };
348         struct obd_device *obd = exp->exp_obd;
349         struct mds_obd *mds = &obd->u.mds;
350         struct obd_ioctl_data *data = karg;
351         struct obd_run_ctxt saved;
352         int rc = 0;
353
354         switch (cmd) {
355         case OBD_IOC_RECORD: {
356                 char *name = data->ioc_inlbuf1;
357                 if (mds->mds_cfg_llh)
358                         RETURN(-EBUSY);
359
360                 push_ctxt(&saved, &obd->obd_ctxt, NULL);
361                 rc = llog_create(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT), 
362                                  &mds->mds_cfg_llh, NULL, name);
363                 if (rc == 0)
364                         llog_init_handle(mds->mds_cfg_llh, LLOG_F_IS_PLAIN, 
365                                          &cfg_uuid);
366                 else
367                         mds->mds_cfg_llh = NULL;
368                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
369
370                 RETURN(rc);
371         }
372
373         case OBD_IOC_ENDRECORD: {
374                 if (!mds->mds_cfg_llh)
375                         RETURN(-EBADF);
376
377                 push_ctxt(&saved, &obd->obd_ctxt, NULL);
378                 rc = llog_close(mds->mds_cfg_llh);
379                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
380
381                 mds->mds_cfg_llh = NULL;
382                 RETURN(rc);
383         }
384
385         case OBD_IOC_DORECORD: {
386                 char *cfg_buf;
387                 struct llog_rec_hdr rec;
388                 if (!mds->mds_cfg_llh)
389                         RETURN(-EBADF);
390
391                 rec.lrh_len = llog_data_len(data->ioc_plen1);
392
393                 if (data->ioc_type == LUSTRE_CFG_TYPE) {
394                         rec.lrh_type = OBD_CFG_REC;
395                 } else if (data->ioc_type == PORTALS_CFG_TYPE) {
396                         rec.lrh_type = PTL_CFG_REC;
397                 } else {
398                         CERROR("unknown cfg record type:%d \n", data->ioc_type);
399                         RETURN(-EINVAL);
400                 }
401
402                 OBD_ALLOC(cfg_buf, data->ioc_plen1);
403                 if (cfg_buf == NULL)
404                         RETURN(-EINVAL);
405                 rc = copy_from_user(cfg_buf, data->ioc_pbuf1, data->ioc_plen1);
406                 if (rc) {
407                         OBD_FREE(cfg_buf, data->ioc_plen1);
408                         RETURN(rc);
409                 }
410
411                 push_ctxt(&saved, &obd->obd_ctxt, NULL);
412                 rc = llog_write_rec(mds->mds_cfg_llh, &rec, NULL, 0,
413                                     cfg_buf, -1);
414                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
415
416                 OBD_FREE(cfg_buf, data->ioc_plen1);
417                 RETURN(rc);
418         }
419
420         case OBD_IOC_PARSE: {
421                 struct llog_ctxt *ctxt = 
422                         llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
423                 push_ctxt(&saved, &obd->obd_ctxt, NULL);
424                 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
425                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
426                 if (rc)
427                         RETURN(rc);
428
429                 RETURN(rc);
430         }
431
432         case OBD_IOC_DUMP_LOG: {
433                 struct llog_ctxt *ctxt = 
434                         llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
435                 push_ctxt(&saved, &obd->obd_ctxt, NULL);
436                 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
437                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
438                 if (rc)
439                         RETURN(rc);
440
441                 RETURN(rc);
442         }
443
444         case OBD_IOC_SET_READONLY: {
445                 void *handle;
446                 struct inode *inode = obd->u.mds.mds_sb->s_root->d_inode;
447                 BDEVNAME_DECLARE_STORAGE(tmp);
448                 CERROR("setting device %s read-only\n",
449                        ll_bdevname(obd->u.mds.mds_sb, tmp));
450
451                 handle = fsfilt_start(obd, inode, FSFILT_OP_MKNOD, NULL);
452                 LASSERT(handle);
453                 rc = fsfilt_commit(obd, inode, handle, 1);
454
455                 dev_set_rdonly(ll_sbdev(obd->u.mds.mds_sb), 2);
456                 RETURN(0);
457         }
458
459         case OBD_IOC_CATLOGLIST: {
460                 int count = mds->mds_lov_desc.ld_tgt_count;
461                 rc = llog_catlog_list(obd, count, data);
462                 RETURN(rc);
463
464         }
465         case OBD_IOC_LLOG_CHECK: 
466         case OBD_IOC_LLOG_CANCEL:
467         case OBD_IOC_LLOG_REMOVE: { 
468                 struct llog_ctxt *ctxt = 
469                         llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
470
471 #ifdef ENABLE_ORPHANS
472                 obd_llog_finish(obd, mds->mds_lov_desc.ld_tgt_count);
473 #endif
474                 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
475                 rc = llog_ioctl(ctxt, cmd, data);
476                 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
477       
478 #ifdef ENABLE_ORPHANS                
479                 llog_cat_initialize(obd, mds->mds_lov_desc.ld_tgt_count);
480 #endif
481                 RETURN(rc);
482         }                
483         case OBD_IOC_LLOG_INFO:
484         case OBD_IOC_LLOG_PRINT: {
485                 struct llog_ctxt *ctxt = 
486                         llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
487                 
488                 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
489                 rc = llog_ioctl(ctxt, cmd, data);
490                 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
491                 
492                 RETURN(rc);
493         }
494
495         case OBD_IOC_ABORT_RECOVERY:
496                 CERROR("aborting recovery for device %s\n", obd->obd_name);
497                 target_abort_recovery(obd);
498                 RETURN(0);
499
500         default:
501                 RETURN(-EINVAL);
502         }
503         RETURN(0);
504 }
505
506 int mds_notify(struct obd_device *obd, struct obd_device *watched,
507                int active)
508 {
509         struct obd_uuid *uuid; 
510
511         if (!active)
512                 RETURN(0);
513
514         if (strcmp(watched->obd_type->typ_name, "osc")) {
515                 CERROR("unexpected notification of %s %s!\n",
516                        watched->obd_type->typ_name,
517                        watched->obd_name);
518                 RETURN(-EINVAL);
519         }
520
521         uuid = &watched->u.cli.cl_import->imp_target_uuid;
522         CWARN("MDS %s: %s now active, resetting orphans\n",
523                obd->obd_name, uuid->uuid);
524         RETURN(mds_lov_clearorphans(&obd->u.mds, uuid));
525 }
526
527 /* Convert the on-disk LOV EA structre.
528  * We always try to convert from an old LOV EA format to the common in-memory
529  * (lsm) format (obd_unpackmd() understands the old on-disk (lmm) format) and
530  * then convert back to the new on-disk format and save it back to disk
531  * (obd_packmd() only ever saves to the new on-disk format) so we don't have
532  * to convert it each time this inode is accessed.
533  *
534  * This function is a bit interesting in the error handling.  We can safely
535  * ship the old lmm to the client in case of failure, since it uses the same
536  * obd_unpackmd() code and can do the conversion if the MDS fails for some
537  * reason.  We will not delete the old lmm data until we have written the
538  * new format lmm data in fsfilt_set_md(). */
539 int mds_convert_lov_ea(struct obd_device *obd, struct inode *inode,
540                        struct lov_mds_md *lmm, int lmm_size)
541 {
542         struct lov_stripe_md *lsm = NULL;
543         void *handle;
544         int rc, err;
545         ENTRY;
546
547         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC)
548                 RETURN(0);
549
550         CWARN("converting LOV EA on %lu/%u from V0 to V1\n",
551               inode->i_ino, inode->i_generation);
552         rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, lmm, lmm_size);
553         if (rc < 0)
554                 GOTO(conv_end, rc);
555
556         rc = obd_packmd(obd->u.mds.mds_osc_exp, &lmm, lsm);
557         if (rc < 0)
558                 GOTO(conv_free, rc);
559         lmm_size = rc;
560
561         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
562         if (IS_ERR(handle)) {
563                 rc = PTR_ERR(handle);
564                 GOTO(conv_free, rc);
565         }
566
567         rc = fsfilt_set_md(obd, inode, handle, lmm, lmm_size);
568
569         err = fsfilt_commit(obd, inode, handle, 0);
570         if (!rc)
571                 rc = err ? err : lmm_size;
572         GOTO(conv_free, rc);
573 conv_free:
574         obd_free_memmd(obd->u.mds.mds_osc_exp, &lsm);
575 conv_end:
576         return rc;
577 }