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