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