Whamcloud - gitweb
c512293b6da044bac54b31a4e53294e9a1f8257d
[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  *  lustre/mds/handler.c
5  *  Lustre Metadata Server (mds) request handler
6  *
7  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *   Author: Andreas Dilger <adilger@clusterfs.com>
10  *   Author: Phil Schwan <phil@clusterfs.com>
11  *   Author: Mike Shaver <shaver@clusterfs.com>
12  *
13  *   This file is part of Lustre, http://www.lustre.org.
14  *
15  *   Lustre is free software; you can redistribute it and/or
16  *   modify it under the terms of version 2 of the GNU General Public
17  *   License as published by the Free Software Foundation.
18  *
19  *   Lustre is distributed in the hope that it will be useful,
20  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   GNU General Public License for more details.
23  *
24  *   You should have received a copy of the GNU General Public License
25  *   along with Lustre; if not, write to the Free Software
26  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29 #ifndef EXPORT_SYMTAB
30 # define EXPORT_SYMTAB
31 #endif
32 #define DEBUG_SUBSYSTEM S_MDS
33
34 #include <linux/module.h>
35 #include <linux/lustre_mds.h>
36 #include <linux/lustre_dlm.h>
37 #include <linux/init.h>
38 #include <linux/obd_class.h>
39 #include <linux/random.h>
40 #include <linux/fs.h>
41 #include <linux/jbd.h>
42 #include <linux/ext3_fs.h>
43 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
44 # include <linux/smp_lock.h>
45 # include <linux/buffer_head.h>
46 # include <linux/workqueue.h>
47 # include <linux/mount.h>
48 #else
49 # include <linux/locks.h>
50 #endif
51 #include <linux/obd_lov.h>
52 #include <linux/lustre_mds.h>
53 #include <linux/lustre_fsfilt.h>
54 #include <linux/lprocfs_status.h>
55 #include <linux/lustre_commit_confd.h>
56
57 #include "mds_internal.h"
58
59 static int mds_postsetup(struct obd_device *obd);
60 static int mds_cleanup(struct obd_device *obd, int flags);
61
62 static int mds_bulk_timeout(void *data)
63 {
64         struct ptlrpc_bulk_desc *desc = data;
65         struct obd_export *exp = desc->bd_export;
66
67         DEBUG_REQ(D_ERROR, desc->bd_req,"bulk send timed out: evicting %s@%s\n",
68                   exp->exp_client_uuid.uuid,
69                   exp->exp_connection->c_remote_uuid.uuid);
70         ptlrpc_fail_export(exp);
71         ptlrpc_abort_bulk (desc);
72         RETURN(1);
73 }
74
75 /* Assumes caller has already pushed into the kernel filesystem context */
76 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
77                         loff_t offset, int count)
78 {
79         struct ptlrpc_bulk_desc *desc;
80         struct l_wait_info lwi;
81         struct page **pages;
82         int rc = 0, npages, i, tmpcount, tmpsize = 0;
83         ENTRY;
84
85         LASSERT((offset & (PAGE_SIZE - 1)) == 0); /* I'm dubious about this */
86
87         npages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
88         OBD_ALLOC(pages, sizeof(*pages) * npages);
89         if (!pages)
90                 GOTO(out, rc = -ENOMEM);
91
92         desc = ptlrpc_prep_bulk_exp (req, BULK_PUT_SOURCE, MDS_BULK_PORTAL);
93         if (desc == NULL)
94                 GOTO(out_free, rc = -ENOMEM);
95
96         for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
97                 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
98
99                 pages[i] = alloc_pages(GFP_KERNEL, 0);
100                 if (pages[i] == NULL)
101                         GOTO(cleanup_buf, rc = -ENOMEM);
102
103                 rc = ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
104                 if (rc != 0)
105                         GOTO(cleanup_buf, rc);
106         }
107
108         for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
109                 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
110                 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
111                        tmpsize, offset, file->f_dentry->d_inode->i_ino,
112                        file->f_dentry->d_inode->i_size);
113
114                 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
115                                      page_address(pages[i]), tmpsize, &offset);
116
117                 if (rc != tmpsize)
118                         GOTO(cleanup_buf, rc = -EIO);
119         }
120
121         rc = ptlrpc_bulk_put(desc);
122         if (rc)
123                 GOTO(cleanup_buf, rc);
124
125         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
126                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
127                        OBD_FAIL_MDS_SENDPAGE, rc);
128                 ptlrpc_abort_bulk(desc);
129                 GOTO(cleanup_buf, rc);
130         }
131
132         lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, mds_bulk_timeout, desc);
133         rc = l_wait_event(desc->bd_waitq, ptlrpc_bulk_complete (desc), &lwi);
134         if (rc) {
135                 LASSERT (rc == -ETIMEDOUT);
136                 GOTO(cleanup_buf, rc);
137         }
138
139         EXIT;
140  cleanup_buf:
141         for (i = 0; i < npages; i++)
142                 if (pages[i])
143                         __free_pages(pages[i], 0);
144
145         ptlrpc_free_bulk(desc);
146  out_free:
147         OBD_FREE(pages, sizeof(*pages) * npages);
148  out:
149         return rc;
150 }
151
152 /* only valid locked dentries or errors should be returned */
153 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
154                                      struct vfsmount **mnt, int lock_mode,
155                                      struct lustre_handle *lockh,
156                                      char *name, int namelen)
157 {
158         struct mds_obd *mds = &obd->u.mds;
159         struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
160         struct ldlm_res_id res_id = { .name = {0} };
161         int flags = 0, rc;
162         ENTRY;
163
164         if (IS_ERR(de))
165                 RETURN(de);
166
167         res_id.name[0] = de->d_inode->i_ino;
168         res_id.name[1] = de->d_inode->i_generation;
169         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
170                               res_id, LDLM_PLAIN, NULL, 0, lock_mode,
171                               &flags, ldlm_completion_ast,
172                               mds_blocking_ast, NULL, lockh);
173         if (rc != ELDLM_OK) {
174                 l_dput(de);
175                 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
176         }
177
178         RETURN(retval);
179 }
180
181 #ifndef DCACHE_DISCONNECTED
182 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
183 #endif
184
185
186 /* Look up an entry by inode number. */
187 /* this function ONLY returns valid dget'd dentries with an initialized inode
188    or errors */
189 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
190                               struct vfsmount **mnt)
191 {
192         char fid_name[32];
193         unsigned long ino = fid->id;
194         __u32 generation = fid->generation;
195         struct inode *inode;
196         struct dentry *result;
197
198         if (ino == 0)
199                 RETURN(ERR_PTR(-ESTALE));
200
201         snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
202
203         CDEBUG(D_DENTRY, "--> mds_fid2dentry: ino/gen %lu/%u, sb %p\n",
204                ino, generation, mds->mds_sb);
205
206         /* under ext3 this is neither supposed to return bad inodes
207            nor NULL inodes. */
208         result = ll_lookup_one_len(fid_name, mds->mds_fid_de, strlen(fid_name));
209         if (IS_ERR(result))
210                 RETURN(result);
211
212         inode = result->d_inode;
213         if (!inode)
214                 RETURN(ERR_PTR(-ENOENT));
215
216         if (generation && inode->i_generation != generation) {
217                 /* we didn't find the right inode.. */
218                 CERROR("bad inode %lu, link: %lu ct: %d or generation %u/%u\n",
219                        inode->i_ino, (unsigned long)inode->i_nlink,
220                        atomic_read(&inode->i_count), inode->i_generation,
221                        generation);
222                 dput(result);
223                 RETURN(ERR_PTR(-ENOENT));
224         }
225
226         if (mnt) {
227                 *mnt = mds->mds_vfsmnt;
228                 mntget(*mnt);
229         }
230
231         RETURN(result);
232 }
233
234
235 /* Establish a connection to the MDS.
236  *
237  * This will set up an export structure for the client to hold state data
238  * about that client, like open files, the last operation number it did
239  * on the server, etc.
240  */
241 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
242                        struct obd_uuid *cluuid)
243 {
244         struct obd_export *exp;
245         struct mds_export_data *med; /*  */
246         struct mds_client_data *mcd;
247         int rc, abort_recovery;
248         ENTRY;
249
250         if (!conn || !obd || !cluuid)
251                 RETURN(-EINVAL);
252
253         /* Check for aborted recovery. */
254         spin_lock_bh(&obd->obd_processing_task_lock);
255         abort_recovery = obd->obd_abort_recovery;
256         spin_unlock_bh(&obd->obd_processing_task_lock);
257         if (abort_recovery)
258                 target_abort_recovery(obd);
259
260         /* XXX There is a small race between checking the list and adding a
261          * new connection for the same UUID, but the real threat (list
262          * corruption when multiple different clients connect) is solved.
263          *
264          * There is a second race between adding the export to the list,
265          * and filling in the client data below.  Hence skipping the case
266          * of NULL mcd above.  We should already be controlling multiple
267          * connects at the client, and we can't hold the spinlock over
268          * memory allocations without risk of deadlocking.
269          */
270         rc = class_connect(conn, obd, cluuid);
271         if (rc)
272                 RETURN(rc);
273         exp = class_conn2export(conn);
274         LASSERT(exp);
275         med = &exp->exp_mds_data;
276
277         OBD_ALLOC(mcd, sizeof(*mcd));
278         if (!mcd) {
279                 CERROR("mds: out of memory for client data\n");
280                 GOTO(out, rc = -ENOMEM);
281         }
282
283         memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
284         med->med_mcd = mcd;
285
286         rc = mds_client_add(obd, &obd->u.mds, med, -1);
287         if (rc == 0)
288                 EXIT;
289 out:
290         if (rc) {
291                 OBD_FREE(mcd, sizeof(*mcd));
292                 class_disconnect(exp, 0);
293         }
294         class_export_put(exp);
295
296         return rc;
297 }
298
299 static int mds_init_export(struct obd_export *exp) 
300 {
301         struct mds_export_data *med = &exp->exp_mds_data;
302
303         INIT_LIST_HEAD(&med->med_open_head);
304         spin_lock_init(&med->med_open_lock);
305         RETURN(0);
306 }
307
308 static int mds_destroy_export(struct obd_export *export)
309 {
310         struct mds_export_data *med;
311         struct obd_device *obd = export->exp_obd;
312         struct obd_run_ctxt saved;
313         int rc = 0;
314         ENTRY;
315
316         med = &export->exp_mds_data;
317         target_destroy_export(export);
318
319         push_ctxt(&saved, &obd->obd_ctxt, NULL);
320         /* Close any open files (which may also cause orphan unlinking). */
321         spin_lock(&med->med_open_lock);
322         while (!list_empty(&med->med_open_head)) {
323                 struct list_head *tmp = med->med_open_head.next;
324                 struct mds_file_data *mfd =
325                         list_entry(tmp, struct mds_file_data, mfd_list);
326                 BDEVNAME_DECLARE_STORAGE(btmp);
327
328                 /* bug 1579: fix force-closing for 2.5 */
329                 struct dentry *dentry = mfd->mfd_dentry;
330
331                 list_del(&mfd->mfd_list);
332                 spin_unlock(&med->med_open_lock);
333
334                 CERROR("force closing client file handle for %*s (%s:%lu)\n",
335                        dentry->d_name.len, dentry->d_name.name,
336                        ll_bdevname(dentry->d_inode->i_sb, btmp),
337                        dentry->d_inode->i_ino);
338                 rc = mds_mfd_close(NULL, obd, mfd, 
339                                    !(export->exp_flags & OBD_OPT_FAILOVER));
340
341                 if (rc)
342                         CDEBUG(D_INODE, "Error closing file: %d\n", rc);
343                 spin_lock(&med->med_open_lock);
344         }
345         spin_unlock(&med->med_open_lock);
346         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
347
348         mds_client_free(export, !(export->exp_flags & OBD_OPT_FAILOVER));
349
350         RETURN(rc);
351 }
352
353 static int mds_disconnect(struct obd_export *export, int flags)
354 {
355         unsigned long irqflags;
356         int rc;
357         ENTRY;
358
359         ldlm_cancel_locks_for_export(export);
360
361         spin_lock_irqsave(&export->exp_lock, irqflags);
362         export->exp_flags = flags;
363         spin_unlock_irqrestore(&export->exp_lock, irqflags);
364
365         rc = class_disconnect(export, flags);
366         RETURN(rc);
367 }
368
369 static int mds_getstatus(struct ptlrpc_request *req)
370 {
371         struct mds_obd *mds = mds_req2mds(req);
372         struct mds_body *body;
373         int rc, size = sizeof(*body);
374         ENTRY;
375
376         rc = lustre_pack_reply(req, 1, &size, NULL);
377         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
378                 CERROR("mds: out of memory for message: size=%d\n", size);
379                 req->rq_status = -ENOMEM;       /* superfluous? */
380                 RETURN(-ENOMEM);
381         }
382
383         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
384         memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
385
386         /* the last_committed and last_xid fields are filled in for all
387          * replies already - no need to do so here also.
388          */
389         RETURN(0);
390 }
391
392 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
393                      void *data, int flag)
394 {
395         int do_ast;
396         ENTRY;
397
398         if (flag == LDLM_CB_CANCELING) {
399                 /* Don't need to do anything here. */
400                 RETURN(0);
401         }
402
403         /* XXX layering violation!  -phil */
404         l_lock(&lock->l_resource->lr_namespace->ns_lock);
405         /* Get this: if mds_blocking_ast is racing with ldlm_intent_policy,
406          * such that mds_blocking_ast is called just before l_i_p takes the
407          * ns_lock, then by the time we get the lock, we might not be the
408          * correct blocking function anymore.  So check, and return early, if
409          * so. */
410         if (lock->l_blocking_ast != mds_blocking_ast) {
411                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
412                 RETURN(0);
413         }
414
415         lock->l_flags |= LDLM_FL_CBPENDING;
416         do_ast = (!lock->l_readers && !lock->l_writers);
417         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
418
419         if (do_ast) {
420                 struct lustre_handle lockh;
421                 int rc;
422
423                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
424                 ldlm_lock2handle(lock, &lockh);
425                 rc = ldlm_cli_cancel(&lockh);
426                 if (rc < 0)
427                         CERROR("ldlm_cli_cancel: %d\n", rc);
428         } else {
429                 LDLM_DEBUG(lock, "Lock still has references, will be "
430                            "cancelled later");
431         }
432         RETURN(0);
433 }
434
435 /* Call with lock=1 if you want mds_pack_md to take the i_sem.
436  * Call with lock=0 if the caller has already taken the i_sem. */
437 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
438                 struct mds_body *body, struct inode *inode, int lock)
439 {
440         struct mds_obd *mds = &obd->u.mds;
441         void *lmm;
442         int lmm_size;
443         int rc;
444         ENTRY;
445
446         lmm = lustre_msg_buf(msg, offset, 0);
447         if (lmm == NULL) {
448                 /* Some problem with getting eadata when I sized the reply
449                  * buffer... */
450                 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
451                        inode->i_ino);
452                 RETURN(0);
453         }
454         lmm_size = msg->buflens[offset];
455
456         /* I don't really like this, but it is a sanity check on the client
457          * MD request.  However, if the client doesn't know how much space
458          * to reserve for the MD, it shouldn't be bad to have too much space.
459          */
460         if (lmm_size > mds->mds_max_mdsize) {
461                 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
462                        inode->i_ino, lmm_size, mds->mds_max_mdsize);
463                 // RETURN(-EINVAL);
464         }
465
466         if (lock)
467                 down(&inode->i_sem);
468         rc = fsfilt_get_md(obd, inode, lmm, lmm_size);
469         if (lock)
470                 up(&inode->i_sem);
471         if (rc < 0) {
472                 CERROR("Error %d reading eadata for ino %lu\n",
473                        rc, inode->i_ino);
474         } else if (rc > 0) {
475                 lmm_size = rc;
476                 rc = mds_convert_lov_ea(obd, inode, lmm, lmm_size);
477
478                 if (rc > 0)
479                         lmm_size = rc;
480                 body->valid |= OBD_MD_FLEASIZE;
481                 body->eadatasize = lmm_size;
482                 rc = 0;
483         }
484
485         RETURN(rc);
486 }
487
488 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
489                                 struct ptlrpc_request *req,
490                                 struct mds_body *reqbody, int reply_off)
491 {
492         struct mds_body *body;
493         struct inode *inode = dentry->d_inode;
494         int rc = 0;
495         ENTRY;
496
497         if (inode == NULL)
498                 RETURN(-ENOENT);
499
500         body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
501         LASSERT(body != NULL);                 /* caller prepped reply */
502
503         mds_pack_inode2fid(&body->fid1, inode);
504         mds_pack_inode2body(body, inode);
505
506         if (S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE) != 0) {
507                 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1, body,
508                                  inode, 1);
509
510                 /* If we have LOV EA data, the OST holds size, atime, mtime */
511                 if (!(body->valid & OBD_MD_FLEASIZE))
512                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
513                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
514         } else if (S_ISLNK(inode->i_mode) &&
515                    (reqbody->valid & OBD_MD_LINKNAME) != 0) {
516                 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1,0);
517                 int len;
518
519                 LASSERT (symname != NULL);       /* caller prepped reply */
520                 len = req->rq_repmsg->buflens[reply_off + 1];
521
522                 rc = inode->i_op->readlink(dentry, symname, len);
523                 if (rc < 0) {
524                         CERROR("readlink failed: %d\n", rc);
525                 } else if (rc != len - 1) {
526                         CERROR ("Unexpected readlink rc %d: expecting %d\n",
527                                 rc, len - 1);
528                         rc = -EINVAL;
529                 } else {
530                         CDEBUG(D_INODE, "read symlink dest %s\n", symname);
531                         body->valid |= OBD_MD_LINKNAME;
532                         body->eadatasize = rc + 1;
533                         symname[rc] = 0;        /* NULL terminate */
534                         rc = 0;
535                 }
536         }
537
538         RETURN(rc);
539 }
540
541 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct inode *inode,
542                                 int offset)
543 {
544         struct mds_obd *mds = mds_req2mds(req);
545         struct mds_body *body;
546         int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
547         ENTRY;
548
549         body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*body));
550         LASSERT(body != NULL);                 /* checked by caller */
551         LASSERT_REQSWABBED(req, offset);       /* swabbed by caller */
552
553         if (S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) {
554                 int rc;
555                 down(&inode->i_sem);
556                 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0);
557                 up(&inode->i_sem);
558                 CDEBUG(D_INODE, "got %d bytes MD data for inode %lu\n",
559                        rc, inode->i_ino);
560                 if (rc < 0) {
561                         if (rc != -ENODATA)
562                                 CERROR("error getting inode %lu MD: rc = %d\n",
563                                        inode->i_ino, rc);
564                         size[bufcount] = 0;
565                 } else if (rc > mds->mds_max_mdsize) {
566                         size[bufcount] = 0;
567                         CERROR("MD size %d larger than maximum possible %u\n",
568                                rc, mds->mds_max_mdsize);
569                 } else {
570                         size[bufcount] = rc;
571                 }
572                 bufcount++;
573         } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
574                 if (inode->i_size + 1 != body->eadatasize)
575                         CERROR("symlink size: %Lu, reply space: %d\n",
576                                inode->i_size + 1, body->eadatasize);
577                 size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
578                 bufcount++;
579                 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
580                        inode->i_size + 1, body->eadatasize);
581         }
582
583         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
584                 CERROR("failed MDS_GETATTR_PACK test\n");
585                 req->rq_status = -ENOMEM;
586                 GOTO(out, rc = -ENOMEM);
587         }
588
589         rc = lustre_pack_reply(req, bufcount, size, NULL);
590         if (rc) {
591                 CERROR("out of memory\n");
592                 GOTO(out, req->rq_status = rc);
593         }
594
595         EXIT;
596  out:
597         return(rc);
598 }
599
600 static int mds_getattr_name(int offset, struct ptlrpc_request *req,
601                             struct lustre_handle *child_lockh)
602 {
603         struct obd_device *obd = req->rq_export->exp_obd;
604         struct ldlm_reply *rep = NULL;
605         struct obd_run_ctxt saved;
606         struct mds_body *body;
607         struct dentry *dparent = NULL, *dchild = NULL;
608         struct obd_ucred uc;
609         struct lustre_handle parent_lockh;
610         int namesize;
611         int rc = 0, cleanup_phase = 0, resent_req = 0;
612         char *name;
613         ENTRY;
614
615         LASSERT(!strcmp(obd->obd_type->typ_name, "mds"));
616
617         /* Swab now, before anyone looks inside the request */
618
619         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
620                                   lustre_swab_mds_body);
621         if (body == NULL) {
622                 CERROR("Can't swab mds_body\n");
623                 GOTO(cleanup, rc = -EFAULT);
624         }
625
626         LASSERT_REQSWAB(req, offset + 1);
627         name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
628         if (name == NULL) {
629                 CERROR("Can't unpack name\n");
630                 GOTO(cleanup, rc = -EFAULT);
631         }
632         namesize = req->rq_reqmsg->buflens[offset + 1];
633
634         LASSERT (offset == 0 || offset == 2);
635         /* if requests were at offset 2, the getattr reply goes back at 1 */
636         if (offset) { 
637                 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
638                 offset = 1;
639         }
640
641         uc.ouc_fsuid = body->fsuid;
642         uc.ouc_fsgid = body->fsgid;
643         uc.ouc_cap = body->capability;
644         uc.ouc_suppgid1 = body->suppgid;
645         uc.ouc_suppgid2 = -1;
646         push_ctxt(&saved, &obd->obd_ctxt, &uc);
647         cleanup_phase = 1; /* kernel context */
648         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
649
650         /* FIXME: handle raw lookup */
651 #if 0
652         if (body->valid == OBD_MD_FLID) {
653                 struct mds_body *mds_reply;
654                 int size = sizeof(*mds_reply);
655                 ino_t inum;
656                 // The user requested ONLY the inode number, so do a raw lookup
657                 rc = lustre_pack_reply(req, 1, &size, NULL);
658                 if (rc) {
659                         CERROR("out of memory\n");
660                         GOTO(cleanup, rc);
661                 }
662
663                 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
664
665                 mds_reply = lustre_msg_buf(req->rq_repmsg, offset,
666                                            sizeof(*mds_reply));
667                 mds_reply->fid1.id = inum;
668                 mds_reply->valid = OBD_MD_FLID;
669                 GOTO(cleanup, rc);
670         }
671 #endif
672
673         if (child_lockh->cookie != 0) {
674                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
675                 resent_req = 1;
676         }
677
678         if (resent_req == 0) {
679                 rc = mds_get_parent_child_locked(obd, &obd->u.mds, &body->fid1,
680                                                  &parent_lockh, &dparent,
681                                                  LCK_PR, name, namesize,
682                                                  child_lockh, &dchild, LCK_PR);
683                 if (rc)
684                         GOTO(cleanup, rc);
685         } else {
686                 struct ldlm_lock *granted_lock;
687                 struct ll_fid child_fid;
688                 struct ldlm_resource *res;
689                 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
690                 granted_lock = ldlm_handle2lock(child_lockh);
691                 LASSERT(granted_lock);
692
693                 res = granted_lock->l_resource;
694                 child_fid.id = res->lr_name.name[0];
695                 child_fid.generation = res->lr_name.name[1];
696                 dchild = mds_fid2dentry(&obd->u.mds, &child_fid, NULL);
697                 LASSERT(dchild);
698                 LDLM_LOCK_PUT(granted_lock);
699         }
700
701         cleanup_phase = 2; /* dchild, dparent, locks */
702
703         if (dchild->d_inode == NULL) {
704                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
705                 /* in the intent case, the policy clears this error:
706                    the disposition is enough */
707                 GOTO(cleanup, rc = -ENOENT);
708         } else {
709                 intent_set_disposition(rep, DISP_LOOKUP_POS);
710         }
711
712         if (req->rq_repmsg == NULL) {
713                 rc = mds_getattr_pack_msg(req, dchild->d_inode, offset);
714                 if (rc != 0) {
715                         CERROR ("mds_getattr_pack_msg: %d\n", rc);
716                         GOTO (cleanup, rc);
717                 }
718         }
719
720         rc = mds_getattr_internal(obd, dchild, req, body, offset);
721         GOTO(cleanup, rc); /* returns the lock to the client */
722
723  cleanup:
724         switch (cleanup_phase) {
725         case 2:
726                 if (resent_req == 0) {
727                         if (rc && dchild->d_inode)
728                                 ldlm_lock_decref(child_lockh, LCK_PR);
729                         ldlm_lock_decref(&parent_lockh, LCK_PR);
730                         l_dput(dparent);
731                 }
732                 l_dput(dchild);
733         case 1:
734                 pop_ctxt(&saved, &obd->obd_ctxt, &uc);
735         default: ;
736         }
737         return rc;
738 }
739
740 static int mds_getattr(int offset, struct ptlrpc_request *req)
741 {
742         struct mds_obd *mds = mds_req2mds(req);
743         struct obd_device *obd = req->rq_export->exp_obd;
744         struct obd_run_ctxt saved;
745         struct dentry *de;
746         struct mds_body *body;
747         struct obd_ucred uc;
748         int rc = 0;
749         ENTRY;
750
751         body = lustre_swab_reqbuf (req, offset, sizeof (*body),
752                                    lustre_swab_mds_body);
753         if (body == NULL) {
754                 CERROR ("Can't unpack body\n");
755                 RETURN (-EFAULT);
756         }
757
758         uc.ouc_fsuid = body->fsuid;
759         uc.ouc_fsgid = body->fsgid;
760         uc.ouc_cap = body->capability;
761         push_ctxt(&saved, &obd->obd_ctxt, &uc);
762         de = mds_fid2dentry(mds, &body->fid1, NULL);
763         if (IS_ERR(de)) {
764                 rc = req->rq_status = -ENOENT;
765                 GOTO(out_pop, PTR_ERR(de));
766         }
767
768         rc = mds_getattr_pack_msg(req, de->d_inode, offset);
769         if (rc != 0) {
770                 CERROR ("mds_getattr_pack_msg: %d\n", rc);
771                 GOTO (out_pop, rc);
772         }
773
774         req->rq_status = mds_getattr_internal(obd, de, req, body, 0);
775
776         l_dput(de);
777         GOTO(out_pop, rc);
778 out_pop:
779         pop_ctxt(&saved, &obd->obd_ctxt, &uc);
780         return rc;
781 }
782
783
784 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
785                           unsigned long max_age)
786 {
787         int rc;
788
789         spin_lock(&obd->obd_osfs_lock);
790         rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
791         if (rc == 0)
792                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
793         spin_unlock(&obd->obd_osfs_lock);
794
795         return rc;
796 }
797
798 static int mds_statfs(struct ptlrpc_request *req)
799 {
800         struct obd_device *obd = req->rq_export->exp_obd;
801         int rc, size = sizeof(struct obd_statfs);
802         ENTRY;
803
804         rc = lustre_pack_reply(req, 1, &size, NULL);
805         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
806                 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
807                 GOTO(out, rc);
808         }
809
810         /* We call this so that we can cache a bit - 1 jiffie worth */
811         rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
812                             jiffies - HZ);
813         if (rc) {
814                 CERROR("mds_obd_statfs failed: rc %d\n", rc);
815                 GOTO(out, rc);
816         }
817
818         EXIT;
819 out:
820         req->rq_status = rc;
821         return 0;
822 }
823
824 static int mds_sync(struct ptlrpc_request *req)
825 {
826         struct obd_device *obd = req->rq_export->exp_obd;
827         struct mds_obd *mds = &obd->u.mds;
828         struct mds_body *body;
829         int rc, size = sizeof(*body);
830         ENTRY;
831
832         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
833         if (body == NULL)
834                 GOTO(out, rc = -EPROTO);
835
836         rc = lustre_pack_reply(req, 1, &size, NULL);
837         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
838                 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
839                 GOTO(out, rc);
840         }
841
842         if (body->fid1.id == 0) {
843                 /* a fid of zero is taken to mean "sync whole filesystem" */
844                 rc = fsfilt_sync(obd, mds->mds_sb);
845                 if (rc)
846                         GOTO(out, rc);
847         } else {
848                 /* just any file to grab fsync method - "file" arg unused */
849                 struct file *file = mds->mds_rcvd_filp;
850                 struct dentry *de;
851
852                 de = mds_fid2dentry(mds, &body->fid1, NULL);
853                 if (IS_ERR(de))
854                         GOTO(out, rc = PTR_ERR(de));
855
856                 rc = file->f_op->fsync(NULL, de, 1);
857                 l_dput(de);
858                 if (rc)
859                         GOTO(out, rc);
860
861                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
862                 mds_pack_inode2fid(&body->fid1, de->d_inode);
863                 mds_pack_inode2body(body, de->d_inode);
864         }
865 out:
866         req->rq_status = rc;
867         return 0;
868 }
869
870 /* mds_readpage does not take a DLM lock on the inode, because the client must
871  * already have a PR lock.
872  *
873  * If we were to take another one here, a deadlock will result, if another
874  * thread is already waiting for a PW lock. */
875 static int mds_readpage(struct ptlrpc_request *req)
876 {
877         struct obd_device *obd = req->rq_export->exp_obd;
878         struct vfsmount *mnt;
879         struct dentry *de;
880         struct file *file;
881         struct mds_body *body, *repbody;
882         struct obd_run_ctxt saved;
883         int rc, size = sizeof(*repbody);
884         struct obd_ucred uc;
885         ENTRY;
886
887         rc = lustre_pack_reply(req, 1, &size, NULL);
888         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
889                 CERROR("mds: out of memory\n");
890                 GOTO(out, rc = -ENOMEM);
891         }
892
893         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
894         if (body == NULL)
895                 GOTO (out, rc = -EFAULT);
896
897         uc.ouc_fsuid = body->fsuid;
898         uc.ouc_fsgid = body->fsgid;
899         uc.ouc_cap = body->capability;
900         push_ctxt(&saved, &obd->obd_ctxt, &uc);
901         de = mds_fid2dentry(&obd->u.mds, &body->fid1, &mnt);
902         if (IS_ERR(de))
903                 GOTO(out_pop, rc = PTR_ERR(de));
904
905         CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
906
907         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
908         /* note: in case of an error, dentry_open puts dentry */
909         if (IS_ERR(file))
910                 GOTO(out_pop, rc = PTR_ERR(file));
911
912         /* body->size is actually the offset -eeb */
913         if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
914                 CERROR("offset "LPU64" not on a block boundary of %lu\n",
915                        body->size, de->d_inode->i_blksize);
916                 GOTO(out_file, rc = -EFAULT);
917         }
918
919         /* body->nlink is actually the #bytes to read -eeb */
920         if (body->nlink & (de->d_inode->i_blksize - 1)) {
921                 CERROR("size %u is not multiple of blocksize %lu\n",
922                        body->nlink, de->d_inode->i_blksize);
923                 GOTO(out_file, rc = -EFAULT);
924         }
925
926         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
927         repbody->size = file->f_dentry->d_inode->i_size;
928         repbody->valid = OBD_MD_FLSIZE;
929
930         /* to make this asynchronous make sure that the handling function
931            doesn't send a reply when this function completes. Instead a
932            callback function would send the reply */
933         /* body->size is actually the offset -eeb */
934         rc = mds_sendpage(req, file, body->size, body->nlink);
935
936 out_file:
937         filp_close(file, 0);
938 out_pop:
939         pop_ctxt(&saved, &obd->obd_ctxt, &uc);
940 out:
941         req->rq_status = rc;
942         RETURN(0);
943 }
944
945 int mds_reint(struct ptlrpc_request *req, int offset,
946               struct lustre_handle *lockh)
947 {
948         struct mds_update_record *rec; /* 116 bytes on the stack?  no sir! */
949         int rc;
950
951         OBD_ALLOC(rec, sizeof(*rec));
952         if (rec == NULL)
953                 RETURN(-ENOMEM);
954
955         rc = mds_update_unpack(req, offset, rec);
956         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
957                 CERROR("invalid record\n");
958                 GOTO(out, req->rq_status = -EINVAL);
959         }
960         /* rc will be used to interrupt a for loop over multiple records */
961         rc = mds_reint_rec(rec, offset, req, lockh);
962  out:
963         OBD_FREE(rec, sizeof(*rec));
964         return rc;
965 }
966
967 static int mds_filter_recovery_request(struct ptlrpc_request *req,
968                                        struct obd_device *obd, int *process)
969 {
970         switch (req->rq_reqmsg->opc) {
971         case MDS_CONNECT: /* This will never get here, but for completeness. */
972         case OST_CONNECT: /* This will never get here, but for completeness. */
973         case MDS_DISCONNECT:
974         case OST_DISCONNECT:
975                *process = 1;
976                RETURN(0);
977
978         case MDS_CLOSE:
979         case MDS_SYNC: /* used in unmounting */
980         case OBD_PING:
981         case MDS_REINT:
982         case LDLM_ENQUEUE:
983                 *process = target_queue_recovery_request(req, obd);
984                 RETURN(0);
985
986         default:
987                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
988                 *process = 0;
989                 /* XXX what should we set rq_status to here? */
990                 req->rq_status = -EAGAIN;
991                 RETURN(ptlrpc_error(req));
992         }
993 }
994
995 static char *reint_names[] = {
996         [REINT_SETATTR] "setattr",
997         [REINT_CREATE]  "create",
998         [REINT_LINK]    "link",
999         [REINT_UNLINK]  "unlink",
1000         [REINT_RENAME]  "rename",
1001         [REINT_OPEN]    "open",
1002 };
1003
1004 int mds_handle(struct ptlrpc_request *req)
1005 {
1006         int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
1007         int rc = 0;
1008         struct mds_obd *mds = NULL; /* quell gcc overwarning */
1009         struct obd_device *obd = NULL;
1010         ENTRY;
1011
1012         OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
1013
1014         LASSERT(current->journal_info == NULL);
1015         /* XXX identical to OST */
1016         if (req->rq_reqmsg->opc != MDS_CONNECT) {
1017                 struct mds_export_data *med;
1018                 int recovering, abort_recovery;
1019
1020                 if (req->rq_export == NULL) {
1021                         CERROR("lustre_mds: operation %d on unconnected MDS\n",
1022                                req->rq_reqmsg->opc);
1023                         req->rq_status = -ENOTCONN;
1024                         GOTO(out, rc = -ENOTCONN);
1025                 }
1026
1027                 med = &req->rq_export->exp_mds_data;
1028                 obd = req->rq_export->exp_obd;
1029                 mds = &obd->u.mds;
1030
1031                 /* Check for aborted recovery. */
1032                 spin_lock_bh(&obd->obd_processing_task_lock);
1033                 abort_recovery = obd->obd_abort_recovery;
1034                 recovering = obd->obd_recovering;
1035                 spin_unlock_bh(&obd->obd_processing_task_lock);
1036                 if (abort_recovery) {
1037                         target_abort_recovery(obd);
1038                 } else if (recovering) {
1039                         rc = mds_filter_recovery_request(req, obd,
1040                                                          &should_process);
1041                         if (rc || !should_process)
1042                                 RETURN(rc);
1043                 }
1044         }
1045
1046         switch (req->rq_reqmsg->opc) {
1047         case MDS_CONNECT:
1048                 DEBUG_REQ(D_INODE, req, "connect");
1049                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
1050                 rc = target_handle_connect(req, mds_handle);
1051                 if (!rc)
1052                         /* Now that we have an export, set mds. */
1053                         mds = mds_req2mds(req);
1054                 break;
1055
1056         case MDS_DISCONNECT:
1057                 DEBUG_REQ(D_INODE, req, "disconnect");
1058                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
1059                 rc = target_handle_disconnect(req);
1060                 req->rq_status = rc;            /* superfluous? */
1061                 break;
1062
1063         case MDS_GETSTATUS:
1064                 DEBUG_REQ(D_INODE, req, "getstatus");
1065                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1066                 rc = mds_getstatus(req);
1067                 break;
1068
1069         case MDS_GETATTR:
1070                 DEBUG_REQ(D_INODE, req, "getattr");
1071                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1072                 rc = mds_getattr(0, req);
1073                 break;
1074
1075         case MDS_GETATTR_NAME: {
1076                 struct lustre_handle lockh;
1077                 DEBUG_REQ(D_INODE, req, "getattr_name");
1078                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NAME_NET, 0);
1079
1080                 /* If this request gets a reconstructed reply, we won't be
1081                  * acquiring any new locks in mds_getattr_name, so we don't
1082                  * want to cancel.
1083                  */
1084                 lockh.cookie = 0;
1085                 rc = mds_getattr_name(0, req, &lockh);
1086                 /* this non-intent call (from an ioctl) is special */
1087                 req->rq_status = rc;
1088                 if (rc == 0 && lockh.cookie)
1089                         ldlm_lock_decref(&lockh, LCK_PR);
1090                 break;
1091         }
1092         case MDS_STATFS:
1093                 DEBUG_REQ(D_INODE, req, "statfs");
1094                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1095                 rc = mds_statfs(req);
1096                 break;
1097
1098         case MDS_READPAGE:
1099                 DEBUG_REQ(D_INODE, req, "readpage");
1100                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1101                 rc = mds_readpage(req);
1102
1103                 OBD_FAIL_RETURN(OBD_FAIL_MDS_SENDPAGE, 0);
1104
1105                 break;
1106
1107         case MDS_REINT: {
1108                 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*opcp));
1109                 __u32  opc;
1110                 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
1111                                mds->mds_max_cookiesize};
1112                 int bufcount;
1113
1114                 /* NB only peek inside req now; mds_reint() will swab it */
1115                 if (opcp == NULL) {
1116                         CERROR ("Can't inspect opcode\n");
1117                         rc = -EINVAL;
1118                         break;
1119                 }
1120                 opc = *opcp;
1121                 if (lustre_msg_swabbed (req->rq_reqmsg))
1122                         __swab32s(&opc);
1123
1124                 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
1125                           (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
1126                            reint_names[opc] == NULL) ? reint_names[opc] :
1127                                                        "unknown opcode");
1128
1129                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1130
1131                 if (opc == REINT_UNLINK)
1132                         bufcount = 3;
1133                 else if (opc == REINT_OPEN || opc == REINT_RENAME)
1134                         bufcount = 2;
1135                 else
1136                         bufcount = 1;
1137
1138                 rc = lustre_pack_reply(req, bufcount, size, NULL);
1139                 if (rc)
1140                         break;
1141
1142                 rc = mds_reint(req, 0, NULL);
1143                 fail = OBD_FAIL_MDS_REINT_NET_REP;
1144                 break;
1145         }
1146
1147         case MDS_CLOSE:
1148                 DEBUG_REQ(D_INODE, req, "close");
1149                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1150                 rc = mds_close(req);
1151                 break;
1152
1153         case MDS_DONE_WRITING:
1154                 DEBUG_REQ(D_INODE, req, "done_writing");
1155                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
1156                 rc = mds_done_writing(req);
1157                 break;
1158
1159         case MDS_PIN:
1160                 DEBUG_REQ(D_INODE, req, "pin");
1161                 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
1162                 rc = mds_pin(req);
1163                 break;
1164
1165         case MDS_SYNC:
1166                 DEBUG_REQ(D_INODE, req, "sync");
1167                 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
1168                 rc = mds_sync(req);
1169                 break;
1170
1171         case OBD_PING:
1172                 DEBUG_REQ(D_INODE, req, "ping");
1173                 rc = target_handle_ping(req);
1174                 break;
1175
1176         case OBD_LOG_CANCEL:
1177                 CDEBUG(D_INODE, "log cancel\n");
1178                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1179                 rc = -ENOTSUPP; /* la la la */
1180                 break;
1181
1182         case LDLM_ENQUEUE:
1183                 DEBUG_REQ(D_INODE, req, "enqueue");
1184                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1185                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1186                                          ldlm_server_blocking_ast);
1187                 break;
1188         case LDLM_CONVERT:
1189                 DEBUG_REQ(D_INODE, req, "convert");
1190                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1191                 rc = ldlm_handle_convert(req);
1192                 break;
1193         case LDLM_BL_CALLBACK:
1194         case LDLM_CP_CALLBACK:
1195                 DEBUG_REQ(D_INODE, req, "callback");
1196                 CERROR("callbacks should not happen on MDS\n");
1197                 LBUG();
1198                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1199                 break;
1200         case LLOG_ORIGIN_HANDLE_CREATE:
1201                 DEBUG_REQ(D_INODE, req, "llog_init");
1202                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1203                 rc = llog_origin_handle_create(req);
1204                 break;
1205         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1206                 DEBUG_REQ(D_INODE, req, "llog next block");
1207                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1208                 rc = llog_origin_handle_next_block(req);
1209                 break;
1210         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1211                 DEBUG_REQ(D_INODE, req, "llog read header");
1212                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1213                 rc = llog_origin_handle_read_header(req);
1214                 break;
1215         case LLOG_ORIGIN_HANDLE_CLOSE:
1216                 DEBUG_REQ(D_INODE, req, "llog close");
1217                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1218                 rc = llog_origin_handle_close(req);
1219                 break;
1220         case LLOG_CATINFO:
1221                 DEBUG_REQ(D_INODE, req, "llog catinfo");
1222                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1223                 rc = llog_catinfo(req);
1224                 break;
1225         default:
1226                 req->rq_status = -ENOTSUPP;
1227                 rc = ptlrpc_error(req);
1228                 RETURN(rc);
1229         }
1230
1231         LASSERT(current->journal_info == NULL);
1232
1233         EXIT;
1234
1235         /* If we're DISCONNECTing, the mds_export_data is already freed */
1236         if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
1237                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1238                 struct obd_device *obd = list_entry(mds, struct obd_device,
1239                                                     u.mds);
1240                 req->rq_repmsg->last_xid =
1241                         le64_to_cpu(med->med_mcd->mcd_last_xid);
1242
1243                 if (!obd->obd_no_transno) {
1244                         req->rq_repmsg->last_committed =
1245                                 obd->obd_last_committed;
1246                 } else {
1247                         DEBUG_REQ(D_IOCTL, req,
1248                                   "not sending last_committed update");
1249                 }
1250                 CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
1251                        ", xid "LPU64"\n",
1252                        mds->mds_last_transno, obd->obd_last_committed,
1253                        req->rq_xid);
1254         }
1255  out:
1256
1257         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
1258                 if (obd && obd->obd_recovering) {
1259                         DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
1260                         return target_queue_final_reply(req, rc);
1261                 }
1262                 /* Lost a race with recovery; let the error path DTRT. */
1263                 rc = req->rq_status = -ENOTCONN;
1264         }
1265
1266         target_send_reply(req, rc, fail);
1267         return 0;
1268 }
1269
1270 /* Update the server data on disk.  This stores the new mount_count and
1271  * also the last_rcvd value to disk.  If we don't have a clean shutdown,
1272  * then the server last_rcvd value may be less than that of the clients.
1273  * This will alert us that we may need to do client recovery.
1274  *
1275  * Also assumes for mds_last_transno that we are not modifying it (no locking).
1276  */
1277 int mds_update_server_data(struct obd_device *obd, int force_sync)
1278 {
1279         struct mds_obd *mds = &obd->u.mds;
1280         struct mds_server_data *msd = mds->mds_server_data;
1281         struct file *filp = mds->mds_rcvd_filp;
1282         struct obd_run_ctxt saved;
1283         loff_t off = 0;
1284         int rc;
1285         ENTRY;
1286
1287         push_ctxt(&saved, &obd->obd_ctxt, NULL);
1288         msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
1289
1290         CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
1291                mds->mds_mount_count, mds->mds_last_transno);
1292         rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off,force_sync);
1293         if (rc)
1294                 CERROR("error writing MDS server data: rc = %d\n", rc);
1295         pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1296
1297         RETURN(rc);
1298 }
1299
1300
1301 /* mount the file system (secretly) */
1302 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
1303 {
1304         struct lustre_cfg* lcfg = buf;
1305         struct mds_obd *mds = &obd->u.mds;
1306         struct vfsmount *mnt;
1307         int rc = 0;
1308         unsigned long page;
1309         ENTRY;
1310
1311         dev_clear_rdonly(2);
1312
1313         if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
1314                 RETURN(rc = -EINVAL);
1315
1316         obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
1317         if (IS_ERR(obd->obd_fsops))
1318                 RETURN(rc = PTR_ERR(obd->obd_fsops));
1319
1320         if (!(page = __get_free_page(GFP_KERNEL)))
1321                 RETURN(-ENOMEM);
1322
1323         memset((void *)page, 0, PAGE_SIZE);
1324         sprintf((char *)page, "iopen_nopriv");
1325
1326         mnt = do_kern_mount(lcfg->lcfg_inlbuf2, 0,
1327                             lcfg->lcfg_inlbuf1, (void *)page);
1328         free_page(page);
1329         if (IS_ERR(mnt)) {
1330                 rc = PTR_ERR(mnt);
1331                 CERROR("do_kern_mount failed: rc = %d\n", rc);
1332                 GOTO(err_ops, rc);
1333         }
1334
1335         CDEBUG(D_SUPER, "%s: mnt = %p\n", lcfg->lcfg_inlbuf1, mnt);
1336
1337         sema_init(&mds->mds_orphan_recovery_sem, 1);
1338         sema_init(&mds->mds_epoch_sem, 1);
1339         spin_lock_init(&mds->mds_transno_lock);
1340         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1341         mds->mds_max_cookiesize = sizeof(struct llog_cookie);
1342         atomic_set(&mds->mds_open_count, 0);
1343
1344         obd->obd_namespace = ldlm_namespace_new("mds_server",
1345                                                 LDLM_NAMESPACE_SERVER);
1346         if (obd->obd_namespace == NULL) {
1347                 mds_cleanup(obd, 0);
1348                 GOTO(err_put, rc = -ENOMEM);
1349         }
1350
1351         rc = mds_fs_setup(obd, mnt);
1352         if (rc) {
1353                 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1354                 GOTO(err_ns, rc);
1355         }
1356
1357         rc = llog_start_commit_thread();
1358         if (rc < 0)
1359                 GOTO(err_fs, rc);
1360         
1361
1362         if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3) {
1363                 class_uuid_t uuid;
1364
1365                 generate_random_uuid(uuid);
1366                 class_uuid_unparse(uuid, &mds->mds_lov_uuid);
1367
1368                 OBD_ALLOC(mds->mds_profile, lcfg->lcfg_inllen3);
1369                 if (mds->mds_profile == NULL) 
1370                         GOTO(err_fs, rc = -ENOMEM);
1371
1372                 memcpy(mds->mds_profile, lcfg->lcfg_inlbuf3,
1373                        lcfg->lcfg_inllen3);
1374
1375         } 
1376
1377         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1378                            "mds_ldlm_client", &obd->obd_ldlm_client);
1379         obd->obd_replayable = 1;
1380
1381         rc = mds_postsetup(obd);
1382         if (rc)
1383                 GOTO(err_fs, rc);
1384         RETURN(0);
1385
1386 err_fs:
1387         /* No extra cleanup needed for llog_init_commit_thread() */
1388         mds_fs_cleanup(obd, 0);
1389 err_ns:
1390         ldlm_namespace_free(obd->obd_namespace, 0);
1391         obd->obd_namespace = NULL;
1392 err_put:
1393         unlock_kernel();
1394         mntput(mds->mds_vfsmnt);
1395         mds->mds_sb = 0;
1396         lock_kernel();
1397 err_ops:
1398         fsfilt_put_ops(obd->obd_fsops);
1399         return rc;
1400 }
1401
1402 static int mds_postsetup(struct obd_device *obd)
1403 {
1404         struct mds_obd *mds = &obd->u.mds;
1405         int rc = 0;
1406         ENTRY;
1407
1408
1409         rc = llog_setup(obd, LLOG_CONFIG_ORIG_CTXT, obd, 0, NULL,
1410                         &llog_lvfs_ops);
1411         if (rc)
1412                 RETURN(rc);
1413
1414         if (mds->mds_profile) {
1415                 struct obd_run_ctxt saved;
1416                 struct lustre_profile *lprof;
1417                 struct config_llog_instance cfg;
1418
1419                 cfg.cfg_instance = NULL;
1420                 cfg.cfg_uuid = mds->mds_lov_uuid;
1421                 push_ctxt(&saved, &obd->obd_ctxt, NULL);
1422                 rc = class_config_parse_llog(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT), 
1423                                              mds->mds_profile, &cfg);
1424                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1425                 if (rc)
1426                         GOTO(err_llog, rc);
1427
1428                 lprof = class_get_profile(mds->mds_profile);
1429                 if (lprof == NULL) {
1430                         CERROR("No profile found: %s\n", mds->mds_profile);
1431                         GOTO(err_cleanup, rc = -ENOENT);
1432                 }
1433                 rc = mds_lov_connect(obd, lprof->lp_osc);
1434                 if (rc)
1435                         GOTO(err_cleanup, rc);
1436         }
1437
1438         RETURN(rc);
1439
1440 err_cleanup:
1441         mds_lov_clean(obd);
1442 err_llog:
1443         llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
1444         RETURN(rc);
1445 }
1446
1447 static int mds_postrecov(struct obd_device *obd) 
1448
1449 {
1450         int rc, rc2;
1451
1452         LASSERT(!obd->obd_recovering);
1453         LASSERT(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT) != NULL);
1454
1455         rc = llog_connect(llog_get_context(obd, LLOG_UNLINK_ORIG_CTXT),
1456                           obd->u.mds.mds_lov_desc.ld_tgt_count, NULL, NULL);
1457         if (rc != 0) {
1458                 CERROR("faild at llog_origin_connect: %d\n", rc);
1459         }
1460
1461         rc = mds_cleanup_orphans(obd);
1462
1463         rc2 = mds_lov_set_nextid(obd);
1464         if (rc2 == 0)
1465                 rc2 = rc;
1466         RETURN(rc2);
1467 }
1468
1469 int mds_lov_clean(struct obd_device *obd)
1470 {
1471         struct mds_obd *mds = &obd->u.mds;
1472
1473         if (mds->mds_profile) {
1474                 char * cln_prof;
1475                 struct config_llog_instance cfg;
1476                 struct obd_run_ctxt saved;
1477                 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
1478
1479                 OBD_ALLOC(cln_prof, len);
1480                 sprintf(cln_prof, "%s-clean", mds->mds_profile);
1481
1482                 cfg.cfg_instance = NULL;
1483                 cfg.cfg_uuid = mds->mds_lov_uuid;
1484
1485                 push_ctxt(&saved, &obd->obd_ctxt, NULL);
1486                 class_config_parse_llog(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT), 
1487                                         cln_prof, &cfg);
1488                 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
1489
1490                 OBD_FREE(cln_prof, len);
1491                 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
1492                 mds->mds_profile = NULL;
1493         }
1494         RETURN(0);
1495 }
1496
1497 static int mds_precleanup(struct obd_device *obd, int flags)
1498 {
1499         int rc = 0;
1500         ENTRY;
1501
1502         mds_lov_disconnect(obd, flags);
1503         mds_lov_clean(obd);
1504         llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
1505         RETURN(rc);
1506 }
1507
1508 static int mds_cleanup(struct obd_device *obd, int flags)
1509 {
1510         struct mds_obd *mds = &obd->u.mds;
1511         ENTRY;
1512
1513         if (mds->mds_sb == NULL)
1514                 RETURN(0);
1515
1516         mds_update_server_data(obd, 1);
1517         if (mds->mds_lov_objids != NULL) {
1518                 OBD_FREE(mds->mds_lov_objids,
1519                          mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id));
1520         }
1521         mds_fs_cleanup(obd, flags);
1522
1523         unlock_kernel();
1524
1525         /* 2 seems normal on mds, (may_umount() also expects 2
1526           fwiw), but we only see 1 at this point in obdfilter. */
1527         if (atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count) > 2)
1528                 CERROR("%s: mount busy, mnt_count %d != 2\n", obd->obd_name,
1529                        atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count));
1530
1531         mntput(mds->mds_vfsmnt);
1532
1533         mds->mds_sb = 0;
1534
1535         ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
1536
1537         spin_lock_bh(&obd->obd_processing_task_lock);
1538         if (obd->obd_recovering) {
1539                 target_cancel_recovery_timer(obd);
1540                 obd->obd_recovering = 0;
1541         }
1542         spin_unlock_bh(&obd->obd_processing_task_lock);
1543
1544         lock_kernel();
1545         dev_clear_rdonly(2);
1546         fsfilt_put_ops(obd->obd_fsops);
1547
1548         RETURN(0);
1549 }
1550
1551 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
1552                                         struct ldlm_lock *new_lock,
1553                                         struct lustre_handle *lockh)
1554 {
1555         struct obd_export *exp = req->rq_export;
1556         struct obd_device *obd = exp->exp_obd;
1557         struct ldlm_request *dlmreq =
1558                 lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*dlmreq));
1559         struct lustre_handle remote_hdl = dlmreq->lock_handle1;
1560         struct list_head *iter;
1561
1562         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
1563                 return;
1564
1565         l_lock(&obd->obd_namespace->ns_lock);
1566         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
1567                 struct ldlm_lock *lock;
1568                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
1569                 if (lock == new_lock)
1570                         continue;
1571                 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
1572                         lockh->cookie = lock->l_handle.h_cookie;
1573                         DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
1574                                   lockh->cookie);
1575                         l_unlock(&obd->obd_namespace->ns_lock);
1576                         return;
1577                 }
1578         }
1579         l_unlock(&obd->obd_namespace->ns_lock);
1580
1581         /* This remote handle isn't enqueued, so we never received or
1582          * processed this request.  Clear MSG_RESENT, because it can
1583          * be handled like any normal request now. */
1584
1585         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1586         
1587         DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
1588                   remote_hdl.cookie);
1589 }
1590
1591 int intent_disposition(struct ldlm_reply *rep, int flag)
1592 {
1593         if (!rep)
1594                 return 0;
1595         return (rep->lock_policy_res1 & flag);
1596 }
1597
1598 void intent_set_disposition(struct ldlm_reply *rep, int flag)
1599 {
1600         if (!rep)
1601                 return;
1602         rep->lock_policy_res1 |= flag;
1603 }
1604
1605 static int ldlm_intent_policy(struct ldlm_namespace *ns,
1606                               struct ldlm_lock **lockp, void *req_cookie,
1607                               ldlm_mode_t mode, int flags, void *data)
1608 {
1609         struct ptlrpc_request *req = req_cookie;
1610         struct ldlm_lock *lock = *lockp;
1611         int rc;
1612         ENTRY;
1613
1614         if (!req_cookie)
1615                 RETURN(0);
1616
1617         if (req->rq_reqmsg->bufcount > 1) {
1618                 /* an intent needs to be considered */
1619                 struct ldlm_intent *it;
1620                 struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
1621                 struct ldlm_reply *rep;
1622                 struct lustre_handle lockh = { 0 };
1623                 struct ldlm_lock *new_lock;
1624                 int offset = 2, repsize[4] = {sizeof(struct ldlm_reply),
1625                                               sizeof(struct mds_body),
1626                                               mds->mds_max_mdsize,
1627                                               mds->mds_max_cookiesize};
1628
1629                 it = lustre_swab_reqbuf(req, 1, sizeof (*it),
1630                                         lustre_swab_ldlm_intent);
1631                 if (it == NULL) {
1632                         CERROR ("Intent missing\n");
1633                         req->rq_status = -EFAULT;
1634                         RETURN(req->rq_status);
1635                 }
1636
1637                 LDLM_DEBUG(lock, "intent policy, opc: %s",
1638                            ldlm_it2str(it->opc));
1639
1640                 rc = lustre_pack_reply(req, it->opc == IT_UNLINK ? 4 : 3,
1641                                        repsize, NULL);
1642                 if (rc)
1643                         RETURN(req->rq_status = rc);
1644
1645                 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
1646                 intent_set_disposition(rep, DISP_IT_EXECD);
1647
1648                 fixup_handle_for_resent_req(req, lock, &lockh);
1649
1650                 /* execute policy */
1651                 switch ((long)it->opc) {
1652                 case IT_OPEN:
1653                 case IT_CREAT|IT_OPEN:
1654                         /* XXX swab here to assert that an mds_open reint
1655                          * packet is following */
1656                         rep->lock_policy_res2 = mds_reint(req, offset, &lockh);
1657 #if 0
1658                         /* We abort the lock if the lookup was negative and
1659                          * we did not make it to the OPEN portion */
1660                         if (!intent_disposition(rep, DISP_LOOKUP_EXECD))
1661                                 RETURN(ELDLM_LOCK_ABORTED);
1662                         if (intent_disposition(rep, DISP_LOOKUP_NEG) &&
1663                             !intent_disposition(rep, DISP_OPEN_OPEN))
1664 #endif 
1665                                 RETURN(ELDLM_LOCK_ABORTED);
1666                         break;
1667                 case IT_GETATTR:
1668                 case IT_LOOKUP:
1669                 case IT_READDIR:
1670                         rep->lock_policy_res2 = mds_getattr_name(offset, req,
1671                                                                  &lockh);
1672                         /* FIXME: LDLM can set req->rq_status. MDS sets
1673                            policy_res{1,2} with disposition and status.
1674                            - replay: returns 0 & req->status is old status 
1675                            - otherwise: returns req->status */
1676                         if (intent_disposition(rep, DISP_LOOKUP_NEG))
1677                                 rep->lock_policy_res2 = 0;
1678                         if (!intent_disposition(rep, DISP_LOOKUP_POS) || 
1679                             rep->lock_policy_res2)
1680                                 RETURN(ELDLM_LOCK_ABORTED);
1681                         if (req->rq_status != 0) {
1682                                 LBUG();
1683                                 rep->lock_policy_res2 = req->rq_status;
1684                                 RETURN(ELDLM_LOCK_ABORTED);
1685                         }
1686                         break;
1687                 default:
1688                         CERROR("Unhandled intent "LPD64"\n", it->opc);
1689                         LBUG();
1690                 }
1691
1692                 /* By this point, whatever function we called above must have
1693                  * either filled in 'lockh', been an intent replay, or returned
1694                  * an error.  We want to allow replayed RPCs to not get a lock,
1695                  * since we would just drop it below anyways because lock replay
1696                  * is done separately by the client afterwards.  For regular
1697                  * RPCs we want to give the new lock to the client instead of
1698                  * whatever lock it was about to get.
1699                  */
1700                 new_lock = ldlm_handle2lock(&lockh);
1701                 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
1702                         RETURN(0);
1703                 
1704                 LASSERT(new_lock != NULL);
1705
1706                 /* If we've already given this lock to a client once, then we
1707                  * should have no readers or writers.  Otherwise, we should
1708                  * have one reader _or_ writer ref (which will be zeroed below)
1709                  * before returning the lock to a client.
1710                  */
1711                 if (new_lock->l_export == req->rq_export) {
1712                         LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
1713                 } else {
1714                         LASSERT(new_lock->l_export == NULL);
1715                         LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
1716                 }
1717
1718                 *lockp = new_lock;
1719
1720                 if (new_lock->l_export == req->rq_export) {
1721                         /* Already gave this to the client, which means that we
1722                          * reconstructed a reply. */
1723                         LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
1724                                 MSG_RESENT);
1725                         RETURN(ELDLM_LOCK_REPLACED);
1726                 }
1727
1728                 /* Fixup the lock to be given to the client */
1729                 l_lock(&new_lock->l_resource->lr_namespace->ns_lock);
1730                 new_lock->l_readers = 0;
1731                 new_lock->l_writers = 0;
1732
1733                 new_lock->l_export = class_export_get(req->rq_export);
1734                 list_add(&new_lock->l_export_chain,
1735                          &new_lock->l_export->exp_ldlm_data.led_held_locks);
1736
1737                 new_lock->l_blocking_ast = lock->l_blocking_ast;
1738                 new_lock->l_completion_ast = lock->l_completion_ast;
1739
1740                 memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
1741                        sizeof(lock->l_remote_handle));
1742
1743                 new_lock->l_flags &= ~LDLM_FL_LOCAL;
1744
1745                 LDLM_LOCK_PUT(new_lock);
1746                 l_unlock(&new_lock->l_resource->lr_namespace->ns_lock);
1747
1748                 RETURN(ELDLM_LOCK_REPLACED);
1749         } else {
1750                 int size = sizeof(struct ldlm_reply);
1751                 rc = lustre_pack_reply(req, 1, &size, NULL);
1752                 if (rc) {
1753                         LBUG();
1754                         RETURN(-ENOMEM);
1755                 }
1756         }
1757         RETURN(0);
1758 }
1759
1760 int mds_attach(struct obd_device *dev, obd_count len, void *data)
1761 {
1762         struct lprocfs_static_vars lvars;
1763
1764         lprocfs_init_multi_vars(0, &lvars);
1765         return lprocfs_obd_attach(dev, lvars.obd_vars);
1766 }
1767
1768 int mds_detach(struct obd_device *dev)
1769 {
1770         return lprocfs_obd_detach(dev);
1771 }
1772
1773 int mdt_attach(struct obd_device *dev, obd_count len, void *data)
1774 {
1775         struct lprocfs_static_vars lvars;
1776
1777         lprocfs_init_multi_vars(1, &lvars);
1778         return lprocfs_obd_attach(dev, lvars.obd_vars);
1779 }
1780
1781 int mdt_detach(struct obd_device *dev)
1782 {
1783         return lprocfs_obd_detach(dev);
1784 }
1785
1786 static int mdt_setup(struct obd_device *obddev, obd_count len, void *buf)
1787 {
1788         struct mds_obd *mds = &obddev->u.mds;
1789         int rc = 0;
1790         ENTRY;
1791
1792         mds->mds_service = ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
1793                                            MDS_BUFSIZE, MDS_MAXREQSIZE,
1794                                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
1795                                            mds_handle, "mds", 
1796                                            obddev->obd_proc_entry);
1797
1798         if (!mds->mds_service) {
1799                 CERROR("failed to start service\n");
1800                 RETURN(rc = -ENOMEM);
1801         }
1802
1803         rc = ptlrpc_start_n_threads(obddev, mds->mds_service, MDT_NUM_THREADS,
1804                                     "ll_mdt");
1805         if (rc)
1806                 GOTO(err_thread, rc);
1807
1808         mds->mds_setattr_service =
1809                 ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
1810                                 MDS_BUFSIZE, MDS_MAXREQSIZE,
1811                                 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
1812                                 mds_handle, "mds_setattr", 
1813                                 obddev->obd_proc_entry);
1814         if (!mds->mds_setattr_service) {
1815                 CERROR("failed to start getattr service\n");
1816                 GOTO(err_thread, rc = -ENOMEM);
1817         }
1818
1819         rc = ptlrpc_start_n_threads(obddev, mds->mds_setattr_service,
1820                                  MDT_NUM_THREADS, "ll_mdt_attr");
1821         if (rc)
1822                 GOTO(err_thread2, rc);
1823                         
1824         mds->mds_readpage_service =
1825                 ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
1826                                 MDS_BUFSIZE, MDS_MAXREQSIZE,
1827                                 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
1828                                 mds_handle, "mds_readpage", 
1829                                 obddev->obd_proc_entry);
1830         if (!mds->mds_readpage_service) {
1831                 CERROR("failed to start readpage service\n");
1832                 GOTO(err_thread2, rc = -ENOMEM);
1833         }
1834
1835         rc = ptlrpc_start_n_threads(obddev, mds->mds_readpage_service,
1836                                     MDT_NUM_THREADS, "ll_mdt_rdpg");
1837
1838         if (rc) 
1839                 GOTO(err_thread3, rc);
1840
1841         RETURN(0);
1842
1843 err_thread3:
1844         ptlrpc_unregister_service(mds->mds_readpage_service);
1845 err_thread2:
1846         ptlrpc_unregister_service(mds->mds_setattr_service);
1847 err_thread:
1848         ptlrpc_unregister_service(mds->mds_service);
1849         return rc;
1850 }
1851
1852
1853 static int mdt_cleanup(struct obd_device *obddev, int flags)
1854 {
1855         struct mds_obd *mds = &obddev->u.mds;
1856         ENTRY;
1857
1858         ptlrpc_stop_all_threads(mds->mds_readpage_service);
1859         ptlrpc_unregister_service(mds->mds_readpage_service);
1860
1861         ptlrpc_stop_all_threads(mds->mds_setattr_service);
1862         ptlrpc_unregister_service(mds->mds_setattr_service);
1863
1864         ptlrpc_stop_all_threads(mds->mds_service);
1865         ptlrpc_unregister_service(mds->mds_service);
1866
1867         RETURN(0);
1868 }
1869
1870 static struct dentry *mds_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr, void *data)
1871 {
1872         struct obd_device *obd = data;
1873         struct ll_fid fid;
1874         fid.id = id;
1875         fid.generation = gen;
1876         return mds_fid2dentry(&obd->u.mds, &fid, NULL);
1877 }
1878
1879 struct lvfs_callback_ops mds_lvfs_ops = {
1880         l_fid2dentry:     mds_lvfs_fid2dentry,
1881 };
1882
1883 /* use obd ops to offer management infrastructure */
1884 static struct obd_ops mds_obd_ops = {
1885         o_owner:       THIS_MODULE,
1886         o_attach:      mds_attach,
1887         o_detach:      mds_detach,
1888         o_connect:     mds_connect,
1889         o_init_export:  mds_init_export,
1890         o_destroy_export:  mds_destroy_export,
1891         o_disconnect:  mds_disconnect,
1892         o_setup:       mds_setup,
1893         o_precleanup:  mds_precleanup,
1894         o_cleanup:     mds_cleanup,
1895         o_postrecov:   mds_postrecov,
1896         o_statfs:      mds_obd_statfs,
1897         o_iocontrol:   mds_iocontrol,
1898         o_create:      mds_obd_create,
1899         o_destroy:     mds_obd_destroy,
1900         o_llog_init:   mds_llog_init,
1901         o_llog_finish: mds_llog_finish,
1902         o_notify:      mds_notify,
1903 };
1904
1905 static struct obd_ops mdt_obd_ops = {
1906         o_owner:       THIS_MODULE,
1907         o_attach:      mdt_attach,
1908         o_detach:      mdt_detach,
1909         o_setup:       mdt_setup,
1910         o_cleanup:     mdt_cleanup,
1911 };
1912
1913 static int __init mds_init(void)
1914 {
1915         struct lprocfs_static_vars lvars;
1916
1917         lprocfs_init_multi_vars(0, &lvars);
1918         class_register_type(&mds_obd_ops, lvars.module_vars, LUSTRE_MDS_NAME);
1919         lprocfs_init_multi_vars(1, &lvars);
1920         class_register_type(&mdt_obd_ops, lvars.module_vars, LUSTRE_MDT_NAME);
1921         ldlm_register_intent(ldlm_intent_policy);
1922
1923         return 0;
1924 }
1925
1926 static void /*__exit*/ mds_exit(void)
1927 {
1928         ldlm_unregister_intent();
1929         class_unregister_type(LUSTRE_MDS_NAME);
1930         class_unregister_type(LUSTRE_MDT_NAME);
1931 }
1932
1933 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1934 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
1935 MODULE_LICENSE("GPL");
1936
1937 module_init(mds_init);
1938 module_exit(mds_exit);