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