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