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