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