Whamcloud - gitweb
f9fd7dab904d48254eb8cf1dcac8790a42770ec1
[fs/lustre-release.git] / lustre / mds / handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/mds/handler.c
35  *
36  * Author: Peter Braam <braam@clusterfs.com>
37  * Author: Andreas Dilger <adilger@clusterfs.com>
38  * Author: Phil Schwan <phil@clusterfs.com>
39  * Author: Mike Shaver <shaver@clusterfs.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_MDS
43
44 #include <lustre_mds.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/fs.h>
48 #include <linux/jbd.h>
49 #include <linux/buffer_head.h>
50 #include <linux/workqueue.h>
51 #include <linux/mount.h>
52
53 #include <lustre_acl.h>
54 #include <obd_class.h>
55 #include <lustre_dlm.h>
56 #include <obd_lov.h>
57 #include <lustre_fsfilt.h>
58 #include <lprocfs_status.h>
59 #include <lustre_disk.h>
60 #include <lustre_param.h>
61
62 #include "mds_internal.h"
63
64 __u32 mds_max_ost_index=0xFFFF;
65 CFS_MODULE_PARM(mds_max_ost_index, "i", int, 0444,
66                 "maximal OST index");
67
68 static int mds_lov_presetup (struct mds_obd *mds, struct lustre_cfg *lcfg)
69 {
70         int rc = 0;
71         ENTRY;
72
73         if (lcfg->lcfg_bufcount >= 4 && LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
74                 class_uuid_t uuid;
75
76                 ll_generate_random_uuid(uuid);
77                 class_uuid_unparse(uuid, &mds->mds_lov_uuid);
78
79                 OBD_ALLOC(mds->mds_profile, LUSTRE_CFG_BUFLEN(lcfg, 3));
80                 if (mds->mds_profile == NULL)
81                         RETURN(-ENOMEM);
82
83                 strncpy(mds->mds_profile, lustre_cfg_string(lcfg, 3),
84                         LUSTRE_CFG_BUFLEN(lcfg, 3));
85         }
86         RETURN(rc);
87 }
88
89 static int mds_lov_clean(struct obd_device *obd)
90 {
91         struct mds_obd *mds = &obd->u.mds;
92         struct obd_device *osc = mds->mds_lov_obd;
93         ENTRY;
94
95         if (mds->mds_profile) {
96                 class_del_profile(mds->mds_profile);
97                 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
98                 mds->mds_profile = NULL;
99         }
100
101         /* There better be a lov */
102         if (!osc)
103                 RETURN(0);
104         if (IS_ERR(osc))
105                 RETURN(PTR_ERR(osc));
106
107         obd_register_observer(osc, NULL);
108
109         /* Give lov our same shutdown flags */
110         osc->obd_force = obd->obd_force;
111         osc->obd_fail = obd->obd_fail;
112
113         /* Cleanup the lov */
114         obd_disconnect(mds->mds_lov_exp);
115         class_manual_cleanup(osc);
116
117         RETURN(0);
118 }
119
120 static int mds_postsetup(struct obd_device *obd)
121 {
122         struct mds_obd *mds = &obd->u.mds;
123         struct llog_ctxt *ctxt;
124         int rc = 0;
125         ENTRY;
126
127         rc = llog_setup(obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, obd, 0, NULL,
128                         &llog_lvfs_ops);
129         if (rc)
130                 RETURN(rc);
131
132         rc = llog_setup(obd, &obd->obd_olg, LLOG_LOVEA_ORIG_CTXT, obd, 0, NULL,
133                         &llog_lvfs_ops);
134         if (rc)
135                 GOTO(err_llog, rc);
136
137         mds_changelog_llog_init(obd, obd);
138
139         if (mds->mds_profile) {
140                 struct lustre_profile *lprof;
141                 /* The profile defines which osc and mdc to connect to, for a
142                    client.  We reuse that here to figure out the name of the
143                    lov to use (and ignore lprof->lp_md).
144                    The profile was set in the config log with
145                    LCFG_MOUNTOPT profilenm oscnm mdcnm */
146                 lprof = class_get_profile(mds->mds_profile);
147                 if (lprof == NULL) {
148                         CERROR("No profile found: %s\n", mds->mds_profile);
149                         GOTO(err_cleanup, rc = -ENOENT);
150                 }
151                 rc = mds_lov_connect(obd, lprof->lp_dt);
152                 if (rc)
153                         GOTO(err_cleanup, rc);
154         }
155
156         RETURN(rc);
157
158 err_cleanup:
159         mds_lov_clean(obd);
160         ctxt = llog_get_context(obd, LLOG_LOVEA_ORIG_CTXT);
161         if (ctxt)
162                 llog_cleanup(ctxt);
163 err_llog:
164         ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
165         if (ctxt)
166                 llog_cleanup(ctxt);
167         return rc;
168 }
169
170 int mds_postrecov(struct obd_device *obd)
171 {
172         int rc = 0;
173         ENTRY;
174
175         if (obd->obd_fail)
176                 RETURN(0);
177
178         LASSERT(!obd->obd_recovering);
179         /* clean PENDING dir */
180 #if 0
181         if (strncmp(obd->obd_name, MDD_OBD_NAME, strlen(MDD_OBD_NAME)))
182                 rc = mds_cleanup_pending(obd);
183                 if (rc < 0)
184                         GOTO(out, rc);
185 #endif
186         /* FIXME Does target_finish_recovery really need this to block? */
187         /* Notify the LOV, which will in turn call mds_notify for each tgt */
188         /* This means that we have to hack obd_notify to think we're obd_set_up
189            during mds_lov_connect. */
190         obd_notify(obd->u.mds.mds_lov_obd, NULL,
191                    obd->obd_async_recov ? OBD_NOTIFY_SYNC_NONBLOCK :
192                    OBD_NOTIFY_SYNC, NULL);
193
194         RETURN(rc);
195 }
196
197 /* We need to be able to stop an mds_lov_synchronize */
198 static int mds_lov_early_clean(struct obd_device *obd)
199 {
200         struct mds_obd *mds = &obd->u.mds;
201         struct obd_device *osc = mds->mds_lov_obd;
202
203         if (!osc || (!obd->obd_force && !obd->obd_fail))
204                 return(0);
205
206         CDEBUG(D_HA, "abort inflight\n");
207         return (obd_precleanup(osc, OBD_CLEANUP_EARLY));
208 }
209
210 static int mds_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
211 {
212         struct mds_obd *mds = &obd->u.mds;
213         struct llog_ctxt *ctxt;
214         int rc = 0;
215         ENTRY;
216
217         switch (stage) {
218         case OBD_CLEANUP_EARLY:
219                 break;
220         case OBD_CLEANUP_EXPORTS:
221                 mds_lov_early_clean(obd);
222                 cfs_down_write(&mds->mds_notify_lock);
223                 mds_lov_disconnect(obd);
224                 mds_lov_clean(obd);
225                 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
226                 if (ctxt)
227                         llog_cleanup(ctxt);
228                 ctxt = llog_get_context(obd, LLOG_LOVEA_ORIG_CTXT);
229                 if (ctxt)
230                         llog_cleanup(ctxt);
231                 rc = obd_llog_finish(obd, 0);
232                 mds->mds_lov_exp = NULL;
233                 cfs_up_write(&mds->mds_notify_lock);
234                 break;
235         }
236         RETURN(rc);
237 }
238
239 /* Look up an entry by inode number. */
240 /* this function ONLY returns valid dget'd dentries with an initialized inode
241    or errors */
242 static struct dentry *mds_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
243                                           void *data)
244 {
245         struct fsfilt_fid  fid;
246         struct obd_device *obd = (struct obd_device *)data;
247
248         if (id == 0)
249                 RETURN(ERR_PTR(-ESTALE));
250
251         fid.ino = id;
252         fid.gen = gen;
253
254         RETURN(fsfilt_fid2dentry(obd, obd->u.mds.mds_obt.obt_vfsmnt, &fid, 0));
255 }
256
257
258 struct lvfs_callback_ops mds_lvfs_ops = {
259         l_fid2dentry:     mds_lvfs_fid2dentry,
260 };
261
262 static int mds_init_ctxt(struct obd_device *obd, struct vfsmount *mnt)
263 {
264         struct mds_obd *mds = &obd->u.mds;
265         int rc;
266
267         mds->mds_obt.obt_vfsmnt = mnt;
268         /* why not mnt->mnt_sb instead of mnt->mnt_root->d_inode->i_sb? */
269         obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
270         obd->u.obt.obt_magic = OBT_MAGIC;
271         rc = fsfilt_setup(obd, obd->u.obt.obt_sb);
272         if (rc)
273                 return rc;
274
275         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
276         obd->obd_lvfs_ctxt.pwdmnt = mnt;
277         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
278         obd->obd_lvfs_ctxt.fs = get_ds();
279         obd->obd_lvfs_ctxt.cb_ops = mds_lvfs_ops;
280         return 0;
281 }
282
283 /*mds still need lov setup here*/
284 static int mds_cmd_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
285 {
286         struct mds_obd *mds = &obd->u.mds;
287         struct lvfs_run_ctxt saved;
288         const char     *dev;
289         struct vfsmount *mnt;
290         struct lustre_sb_info *lsi;
291         struct lustre_mount_info *lmi;
292         struct dentry  *dentry;
293         int rc = 0;
294         ENTRY;
295
296         CDEBUG(D_INFO, "obd %s setup \n", obd->obd_name);
297         if (strncmp(obd->obd_name, MDD_OBD_NAME, strlen(MDD_OBD_NAME)))
298                 RETURN(0);
299
300         if (lcfg->lcfg_bufcount < 5) {
301                 CERROR("invalid arg for setup %s\n", MDD_OBD_NAME);
302                 RETURN(-EINVAL);
303         }
304         dev = lustre_cfg_string(lcfg, 4);
305         lmi = server_get_mount(dev);
306         LASSERT(lmi != NULL);
307
308         lsi = s2lsi(lmi->lmi_sb);
309         mnt = lmi->lmi_mnt;
310         /* FIXME: MDD LOV initialize objects.
311          * we need only lmi here but not get mount
312          * OSD did mount already, so put mount back
313          */
314         cfs_atomic_dec(&lsi->lsi_mounts);
315         mntput(mnt);
316         cfs_init_rwsem(&mds->mds_notify_lock);
317
318         obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
319         rc = mds_init_ctxt(obd, mnt);
320         if (rc)
321                 GOTO(err_putfs, rc);
322
323         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
324         dentry = simple_mkdir(cfs_fs_pwd(current->fs), mnt, "OBJECTS", 0777, 1);
325         if (IS_ERR(dentry)) {
326                 rc = PTR_ERR(dentry);
327                 CERROR("cannot create OBJECTS directory: rc = %d\n", rc);
328                 GOTO(err_pop, rc);
329         }
330         mds->mds_objects_dir = dentry;
331
332         rc = mds_lov_init_objids(obd);
333         if (rc != 0) {
334                CERROR("cannot init lov objid rc = %d\n", rc);
335                GOTO(err_objects, rc );
336         }
337
338         rc = mds_lov_presetup(mds, lcfg);
339         if (rc < 0)
340                 GOTO(err_objects, rc);
341
342         /* Don't wait for mds_postrecov trying to clear orphans */
343         obd->obd_async_recov = 1;
344         rc = mds_postsetup(obd);
345         /* Bug 11557 - allow async abort_recov start
346            FIXME can remove most of this obd_async_recov plumbing
347         obd->obd_async_recov = 0;
348         */
349
350         if (rc)
351                 GOTO(err_objects, rc);
352
353         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
354         RETURN(0);
355
356 err_objects:
357         dput(mds->mds_objects_dir);
358 err_pop:
359         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
360 err_putfs:
361         fsfilt_put_ops(obd->obd_fsops);
362         return rc;
363 }
364
365 static int mds_cmd_cleanup(struct obd_device *obd)
366 {
367         struct mds_obd *mds = &obd->u.mds;
368         struct lvfs_run_ctxt saved;
369         int rc = 0;
370         ENTRY;
371
372         mds->mds_lov_exp = NULL;
373
374         if (obd->obd_fail)
375                 LCONSOLE_WARN("%s: shutting down for failover; client state "
376                               "will be preserved.\n", obd->obd_name);
377
378         if (strncmp(obd->obd_name, MDD_OBD_NAME, strlen(MDD_OBD_NAME)))
379                 RETURN(0);
380
381         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
382
383         mds_lov_destroy_objids(obd);
384
385         if (mds->mds_objects_dir != NULL) {
386                 l_dput(mds->mds_objects_dir);
387                 mds->mds_objects_dir = NULL;
388         }
389
390         ll_vfs_dq_off(obd->u.obt.obt_sb, 0);
391         shrink_dcache_sb(mds->mds_obt.obt_sb);
392         fsfilt_put_ops(obd->obd_fsops);
393
394         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
395         RETURN(rc);
396 }
397
398 #if 0
399 static int mds_cmd_health_check(struct obd_device *obd)
400 {
401         return 0;
402 }
403 #endif
404 static struct obd_ops mds_cmd_obd_ops = {
405         .o_owner           = THIS_MODULE,
406         .o_setup           = mds_cmd_setup,
407         .o_cleanup         = mds_cmd_cleanup,
408         .o_precleanup      = mds_precleanup,
409         .o_create          = mds_obd_create,
410         .o_destroy         = mds_obd_destroy,
411         .o_llog_init       = mds_llog_init,
412         .o_llog_finish     = mds_llog_finish,
413         .o_notify          = mds_notify,
414         .o_postrecov       = mds_postrecov,
415         //   .o_health_check    = mds_cmd_health_check,
416 };
417
418 quota_interface_t *mds_quota_interface_ref;
419 extern quota_interface_t mds_quota_interface;
420
421 static int __init mds_cmd_init(void)
422 {
423         struct lprocfs_static_vars lvars;
424         int rc;
425
426         cfs_request_module("%s", "lquota");
427         mds_quota_interface_ref = PORTAL_SYMBOL_GET(mds_quota_interface);
428         rc = lquota_init(mds_quota_interface_ref);
429         if (rc) {
430                 if (mds_quota_interface_ref)
431                         PORTAL_SYMBOL_PUT(mds_quota_interface);
432                 return rc;
433         }
434         init_obd_quota_ops(mds_quota_interface_ref, &mds_cmd_obd_ops);
435
436         lprocfs_mds_init_vars(&lvars);
437         class_register_type(&mds_cmd_obd_ops, NULL, lvars.module_vars,
438                             LUSTRE_MDS_NAME, NULL);
439
440         return 0;
441 }
442
443 static void /*__exit*/ mds_cmd_exit(void)
444 {
445         lquota_exit(mds_quota_interface_ref);
446         if (mds_quota_interface_ref)
447                 PORTAL_SYMBOL_PUT(mds_quota_interface);
448
449         class_unregister_type(LUSTRE_MDS_NAME);
450 }
451
452 EXPORT_SYMBOL(mds_quota_interface_ref);
453 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
454 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
455 MODULE_LICENSE("GPL");
456
457 module_init(mds_cmd_init);
458 module_exit(mds_cmd_exit);