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