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