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