Whamcloud - gitweb
68cf8bd4a81fcd4905a0180ba99e576e8a4660bc
[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/obd_ost.h>
53 #include <linux/lustre_mds.h>
54 #include <linux/lustre_fsfilt.h>
55 #include <linux/lprocfs_status.h>
56 #include <linux/lustre_commit_confd.h>
57
58 #ifdef CONFIG_SNAPFS
59 #include <linux/lustre_smfs.h>
60 #include <linux/lustre_snap.h>
61 #endif
62 #include "mds_internal.h"
63
64 static int mds_intent_policy(struct ldlm_namespace *ns,
65                              struct ldlm_lock **lockp, void *req_cookie,
66                              ldlm_mode_t mode, int flags, void *data);
67 static int mds_postsetup(struct obd_device *obd);
68 static int mds_cleanup(struct obd_device *obd, int flags);
69
70
71 /* Assumes caller has already pushed into the kernel filesystem context */
72 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
73                         loff_t offset, int count)
74 {
75         struct ptlrpc_bulk_desc *desc;
76         struct l_wait_info lwi;
77         struct page **pages;
78         int rc = 0, npages, i, tmpcount, tmpsize = 0;
79         ENTRY;
80
81         LASSERT((offset & (PAGE_SIZE - 1)) == 0); /* I'm dubious about this */
82
83         npages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
84         OBD_ALLOC(pages, sizeof(*pages) * npages);
85         if (!pages)
86                 GOTO(out, rc = -ENOMEM);
87
88         desc = ptlrpc_prep_bulk_exp(req, npages, BULK_PUT_SOURCE,
89                                     MDS_BULK_PORTAL);
90         if (desc == NULL)
91                 GOTO(out_free, rc = -ENOMEM);
92
93         for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
94                 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
95
96                 pages[i] = alloc_pages(GFP_KERNEL, 0);
97                 if (pages[i] == NULL)
98                         GOTO(cleanup_buf, rc = -ENOMEM);
99
100                 ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
101         }
102
103         for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
104                 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
105                 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
106                        tmpsize, offset, file->f_dentry->d_inode->i_ino,
107                        file->f_dentry->d_inode->i_size);
108
109                 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
110                                      kmap(pages[i]), tmpsize, &offset);
111                 kunmap(pages[i]);
112
113                 if (rc != tmpsize)
114                         GOTO(cleanup_buf, rc = -EIO);
115         }
116
117         LASSERT(desc->bd_nob == count);
118
119         rc = ptlrpc_start_bulk_transfer(desc);
120         if (rc)
121                 GOTO(cleanup_buf, rc);
122
123         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
124                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
125                        OBD_FAIL_MDS_SENDPAGE, rc);
126                 GOTO(abort_bulk, rc);
127         }
128
129         lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
130         rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), &lwi);
131         LASSERT (rc == 0 || rc == -ETIMEDOUT);
132
133         if (rc == 0) {
134                 if (desc->bd_success &&
135                     desc->bd_nob_transferred == count)
136                         GOTO(cleanup_buf, rc);
137
138                 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
139         }
140
141         DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
142                   (rc == -ETIMEDOUT) ? "timeout" : "network error",
143                   desc->bd_nob_transferred, count,
144                   req->rq_export->exp_client_uuid.uuid,
145                   req->rq_export->exp_connection->c_remote_uuid.uuid);
146
147         ptlrpc_fail_export(req->rq_export);
148
149         EXIT;
150  abort_bulk:
151         ptlrpc_abort_bulk (desc);
152  cleanup_buf:
153         for (i = 0; i < npages; i++)
154                 if (pages[i])
155                         __free_pages(pages[i], 0);
156
157         ptlrpc_free_bulk(desc);
158  out_free:
159         OBD_FREE(pages, sizeof(*pages) * npages);
160  out:
161         return rc;
162 }
163
164 int mds_lock_mode_for_dir(struct obd_device *obd,
165                           struct dentry *dentry, int mode)
166 {
167         int ret_mode, split;
168
169         /* any dir access needs couple locks:
170          * 1) on part of dir we gonna lookup/modify in
171          * 2) on a whole dir to protect it from concurrent splitting
172          *    and to flush client's cache for readdir()
173          * so, for a given mode and dentry this routine decides what
174          * lock mode to use for lock #2:
175          * 1) if caller's gonna lookup in dir then we need to protect
176          *    dir from being splitted only - LCK_CR
177          * 2) if caller's gonna modify dir then we need to protect
178          *    dir from being splitted and to flush cache - LCK_CW
179          * 3) if caller's gonna modify dir and that dir seems ready
180          *    for splitting then we need to protect it from any
181          *    type of access (lookup/modify/split) - LCK_EX -bzzz */
182
183         split = mds_splitting_expected(obd, dentry);
184         
185         /* it is important to check here only for MDS_NO_SPLITTABLE.
186          * The reason is that MDS_NO_SPLITTABLE means dir is not splittable
187          * in principle and another thread will not split it on the quiet. 
188          * But if we have MDS_NO_SPLIT_EXPECTED, this means, that dir may be 
189          * splitted anytime, but not now (forcurrent thread) and we should 
190          * consider that it can  happen soon and go that branch which can yield 
191          * LCK_EX to protect from possible splitting. */
192         if (split == MDS_NO_SPLITTABLE) {
193                 /* this inode won't be splitted. so we need not to protect from
194                  * just flush client's cache on modification */
195                 ret_mode = 0;
196                 if (mode == LCK_PW)
197                         ret_mode = LCK_CW;
198         } else {
199                 if (mode == LCK_EX)
200                         return LCK_EX;
201                 
202                 if (mode == LCK_PR) {
203                         ret_mode = LCK_CR;
204                 } else if (mode == LCK_PW) {
205                         /* caller gonna modify directory.we use concurrent
206                            write lock here to retract client's cache for readdir */
207                         ret_mode = LCK_CW;
208                         if (split == MDS_EXPECT_SPLIT) {
209                                 /* splitting possible. serialize any access
210                                  * the idea is that first one seen dir is
211                                  * splittable is given exclusive lock and
212                                  * split directory. caller passes lock mode
213                                  * to mds_try_to_split_dir() and splitting
214                                  * would be done with exclusive lock only -bzzz */
215                                 CDEBUG(D_OTHER, "%s: gonna split %u/%u\n",
216                                        obd->obd_name,
217                                        (unsigned) dentry->d_inode->i_ino,
218                                        (unsigned) dentry->d_inode->i_generation);
219                                 ret_mode = LCK_EX;
220                         }
221                 }
222         }
223         return ret_mode;
224 }
225
226 /* only valid locked dentries or errors should be returned */
227 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
228                                      struct vfsmount **mnt, int lock_mode,
229                                      struct lustre_handle *lockh, int *mode,
230                                      char *name, int namelen, __u64 lockpart)
231 {
232         struct mds_obd *mds = &obd->u.mds;
233         struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
234         struct ldlm_res_id res_id = { .name = {0} };
235         int flags = 0, rc;
236         ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
237
238         ENTRY;
239
240         if (IS_ERR(de))
241                 RETURN(de);
242
243         res_id.name[0] = de->d_inode->i_ino;
244         res_id.name[1] = de->d_inode->i_generation;
245         lockh[1].cookie = 0;
246 #ifdef S_PDIROPS
247         if (name && IS_PDIROPS(de->d_inode)) {
248                 ldlm_policy_data_t cpolicy =
249                         { .l_inodebits = { MDS_INODELOCK_UPDATE } };
250                 LASSERT(mode != NULL);
251                 *mode = mds_lock_mode_for_dir(obd, de, lock_mode);
252                 if (*mode) {
253                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
254                                               res_id, LDLM_IBITS,
255                                               &cpolicy, *mode, &flags,
256                                               mds_blocking_ast,
257                                               ldlm_completion_ast, NULL, NULL,
258                                               NULL, 0, NULL, lockh + 1);
259                         if (rc != ELDLM_OK) {
260                                 l_dput(de);
261                                 RETURN(ERR_PTR(-ENOLCK));
262                         }
263                 }
264                 flags = 0;
265
266                 res_id.name[2] = full_name_hash(name, namelen);
267
268                 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
269                        de->d_inode->i_ino, de->d_inode->i_generation,
270                        res_id.name[2]);
271         }
272 #else
273 #warning "No PDIROPS support in the kernel"
274 #endif
275         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
276                               LDLM_IBITS, &policy, lock_mode, &flags,
277                               mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
278                               NULL, 0, NULL, lockh);
279         if (rc != ELDLM_OK) {
280                 l_dput(de);
281                 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
282 #ifdef S_PDIROPS
283                 if (lockh[1].cookie)
284                         ldlm_lock_decref(lockh + 1, LCK_CW);
285 #endif
286         }
287
288         RETURN(retval);
289 }
290
291 #ifndef DCACHE_DISCONNECTED
292 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
293 #endif
294
295
296 /* Look up an entry by inode number. */
297 /* this function ONLY returns valid dget'd dentries with an initialized inode
298    or errors */
299 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
300                               struct vfsmount **mnt)
301 {
302         char fid_name[32];
303         unsigned long ino = fid->id;
304         __u32 generation = fid->generation;
305         struct inode *inode;
306         struct dentry *result;
307
308         if (ino == 0)
309                 RETURN(ERR_PTR(-ESTALE));
310        
311         snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
312
313         CDEBUG(D_DENTRY, "--> mds_fid2dentry: ino/gen %lu/%u, sb %p\n",
314                ino, generation, mds->mds_sb);
315
316         /* under ext3 this is neither supposed to return bad inodes
317            nor NULL inodes. */
318         result = ll_lookup_one_len(fid_name, mds->mds_fid_de, strlen(fid_name));
319         if (IS_ERR(result))
320                 RETURN(result);
321
322         inode = result->d_inode;
323         if (!inode)
324                 RETURN(ERR_PTR(-ENOENT));
325
326         /* here we disabled generation check, as root inode i_generation
327          * of cache mds and real mds are different. */
328         if (inode->i_ino != mds->mds_rootfid.id && generation &&
329                         inode->i_generation != generation) {
330                 /* we didn't find the right inode.. */
331                 CERROR("bad inode %lu, link: %lu ct: %d or generation %u/%u\n",
332                        inode->i_ino, (unsigned long)inode->i_nlink,
333                        atomic_read(&inode->i_count), inode->i_generation,
334                        generation);
335                 dput(result);
336                 RETURN(ERR_PTR(-ENOENT));
337         }
338
339         if (mnt) {
340                 *mnt = mds->mds_vfsmnt;
341                 mntget(*mnt);
342         }
343
344         RETURN(result);
345 }
346
347
348 /* Establish a connection to the MDS.
349  *
350  * This will set up an export structure for the client to hold state data
351  * about that client, like open files, the last operation number it did
352  * on the server, etc.
353  */
354 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
355                        struct obd_uuid *cluuid, unsigned long connect_flags)
356 {
357         struct obd_export *exp;
358         struct mds_export_data *med; /*  */
359         struct mds_client_data *mcd;
360         int rc;
361         ENTRY;
362
363         if (!conn || !obd || !cluuid)
364                 RETURN(-EINVAL);
365
366         /* XXX There is a small race between checking the list and adding a
367          * new connection for the same UUID, but the real threat (list
368          * corruption when multiple different clients connect) is solved.
369          *
370          * There is a second race between adding the export to the list,
371          * and filling in the client data below.  Hence skipping the case
372          * of NULL mcd above.  We should already be controlling multiple
373          * connects at the client, and we can't hold the spinlock over
374          * memory allocations without risk of deadlocking.
375          */
376         rc = class_connect(conn, obd, cluuid);
377         if (rc)
378                 RETURN(rc);
379         exp = class_conn2export(conn);
380         LASSERT(exp);
381         med = &exp->exp_mds_data;
382
383         OBD_ALLOC(mcd, sizeof(*mcd));
384         if (!mcd) {
385                 CERROR("mds: out of memory for client data\n");
386                 GOTO(out, rc = -ENOMEM);
387         }
388
389         memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
390         med->med_mcd = mcd;
391
392         rc = mds_client_add(obd, &obd->u.mds, med, -1);
393         if (rc)
394                 GOTO(out, rc);
395        
396         if (!(connect_flags & OBD_OPT_MDS_CONNECTION)) {
397                 struct mds_obd *mds = &obd->u.mds;
398                 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)) {
399                         atomic_inc(&mds->mds_real_clients);
400                         CDEBUG(D_OTHER,"%s: peer from %s is real client (%d)\n",
401                                obd->obd_name, exp->exp_client_uuid.uuid,
402                                atomic_read(&mds->mds_real_clients));
403                         exp->exp_flags |= OBD_OPT_REAL_CLIENT;
404                 }
405                 if (mds->mds_lmv_name)
406                         rc = mds_lmv_connect(obd, mds->mds_lmv_name);
407         }
408         EXIT;
409 out:
410         if (rc) {
411                 OBD_FREE(mcd, sizeof(*mcd));
412                 class_disconnect(exp, 0);
413         }
414         class_export_put(exp);
415
416         return rc;
417 }
418
419 static int mds_init_export(struct obd_export *exp)
420 {
421         struct mds_export_data *med = &exp->exp_mds_data;
422
423         INIT_LIST_HEAD(&med->med_open_head);
424         spin_lock_init(&med->med_open_lock);
425         RETURN(0);
426 }
427
428 static int mds_destroy_export(struct obd_export *export)
429 {
430         struct mds_export_data *med;
431         struct obd_device *obd = export->exp_obd;
432         struct lvfs_run_ctxt saved;
433         int rc = 0;
434         ENTRY;
435
436         med = &export->exp_mds_data;
437         target_destroy_export(export);
438
439         if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
440                 GOTO(out, 0);
441
442         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
443
444         /* Close any open files (which may also cause orphan unlinking). */
445         spin_lock(&med->med_open_lock);
446         while (!list_empty(&med->med_open_head)) {
447                 struct list_head *tmp = med->med_open_head.next;
448                 struct mds_file_data *mfd =
449                         list_entry(tmp, struct mds_file_data, mfd_list);
450                 BDEVNAME_DECLARE_STORAGE(btmp);
451
452                 /* bug 1579: fix force-closing for 2.5 */
453                 struct dentry *dentry = mfd->mfd_dentry;
454
455                 list_del(&mfd->mfd_list);
456                 spin_unlock(&med->med_open_lock);
457
458                 /* If you change this message, be sure to update
459                  * replay_single:test_46 */
460                 CERROR("force closing client file handle for %*s (%s:%lu)\n",
461                        dentry->d_name.len, dentry->d_name.name,
462                        ll_bdevname(dentry->d_inode->i_sb, btmp),
463                        dentry->d_inode->i_ino);
464                 rc = mds_mfd_close(NULL, 0, obd, mfd,
465                                    !(export->exp_flags & OBD_OPT_FAILOVER));
466
467                 if (rc)
468                         CDEBUG(D_INODE, "Error closing file: %d\n", rc);
469                 spin_lock(&med->med_open_lock);
470         }
471         spin_unlock(&med->med_open_lock);
472         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
473
474 out:
475         mds_client_free(export, !(export->exp_flags & OBD_OPT_FAILOVER));
476
477         RETURN(rc);
478 }
479
480 static int mds_disconnect(struct obd_export *exp, int flags)
481 {
482         struct obd_device *obd;
483         struct mds_obd *mds;
484         unsigned long irqflags;
485         int rc;
486         ENTRY;
487
488         LASSERT(exp);
489         class_export_get(exp);
490
491         obd = class_exp2obd(exp);
492         if (obd == NULL) {
493                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
494                        exp->exp_handle.h_cookie);
495                 RETURN(-EINVAL);
496         }
497         mds = &obd->u.mds;
498
499         if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)
500                         && !atomic_read(&mds->mds_real_clients)) {
501                 /* there was no client at all */
502                 mds_lmv_disconnect(obd, flags);
503         }
504
505         if ((exp->exp_flags & OBD_OPT_REAL_CLIENT)
506                         && atomic_dec_and_test(&mds->mds_real_clients)) {
507                 /* time to drop LMV connections */
508                 CDEBUG(D_OTHER, "%s: last real client %s disconnected.  "
509                        "Disconnnect from LMV now\n",
510                        obd->obd_name, exp->exp_client_uuid.uuid);
511                 mds_lmv_disconnect(obd, flags);
512         }
513
514         spin_lock_irqsave(&exp->exp_lock, irqflags);
515         exp->exp_flags = flags;
516         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
517
518         /* Disconnect early so that clients can't keep using export */
519         rc = class_disconnect(exp, flags);
520         ldlm_cancel_locks_for_export(exp);
521
522         /* complete all outstanding replies */
523         spin_lock_irqsave(&exp->exp_lock, irqflags);
524         while (!list_empty(&exp->exp_outstanding_replies)) {
525                 struct ptlrpc_reply_state *rs =
526                         list_entry(exp->exp_outstanding_replies.next,
527                                    struct ptlrpc_reply_state, rs_exp_list);
528                 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
529
530                 spin_lock(&svc->srv_lock);
531                 list_del_init(&rs->rs_exp_list);
532                 ptlrpc_schedule_difficult_reply(rs);
533                 spin_unlock(&svc->srv_lock);
534         }
535         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
536
537         class_export_put(exp);
538         RETURN(rc);
539 }
540
541 static int mds_getstatus(struct ptlrpc_request *req)
542 {
543         struct mds_obd *mds = mds_req2mds(req);
544         struct mds_body *body;
545         int rc, size = sizeof(*body);
546         ENTRY;
547
548         rc = lustre_pack_reply(req, 1, &size, NULL);
549         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
550                 CERROR("mds: out of memory for message: size=%d\n", size);
551                 req->rq_status = -ENOMEM;       /* superfluous? */
552                 RETURN(-ENOMEM);
553         }
554
555         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
556         memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
557
558         /* the last_committed and last_xid fields are filled in for all
559          * replies already - no need to do so here also.
560          */
561         RETURN(0);
562 }
563
564 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
565                      void *data, int flag)
566 {
567         int do_ast;
568         ENTRY;
569
570         if (flag == LDLM_CB_CANCELING) {
571                 /* Don't need to do anything here. */
572                 RETURN(0);
573         }
574
575         /* XXX layering violation!  -phil */
576         l_lock(&lock->l_resource->lr_namespace->ns_lock);
577         /* Get this: if mds_blocking_ast is racing with mds_intent_policy,
578          * such that mds_blocking_ast is called just before l_i_p takes the
579          * ns_lock, then by the time we get the lock, we might not be the
580          * correct blocking function anymore.  So check, and return early, if
581          * so. */
582         if (lock->l_blocking_ast != mds_blocking_ast) {
583                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
584                 RETURN(0);
585         }
586
587         lock->l_flags |= LDLM_FL_CBPENDING;
588         do_ast = (!lock->l_readers && !lock->l_writers);
589         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
590
591         if (do_ast) {
592                 struct lustre_handle lockh;
593                 int rc;
594
595                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
596                 ldlm_lock2handle(lock, &lockh);
597                 rc = ldlm_cli_cancel(&lockh);
598                 if (rc < 0)
599                         CERROR("ldlm_cli_cancel: %d\n", rc);
600         } else {
601                 LDLM_DEBUG(lock, "Lock still has references, will be "
602                            "cancelled later");
603         }
604         RETURN(0);
605 }
606
607 int mds_get_md(struct obd_device *obd, struct inode *inode, void *md,
608                int *size, int lock)
609 {
610         int rc = 0;
611         int lmm_size;
612
613         if (lock)
614                 down(&inode->i_sem);
615         rc = fsfilt_get_md(obd, inode, md, *size);
616         if (lock)
617                 up(&inode->i_sem);
618
619         if (rc < 0) {
620                 CERROR("Error %d reading eadata for ino %lu\n",
621                        rc, inode->i_ino);
622         } else if (rc > 0) {
623                 lmm_size = rc;
624                 
625                 if (S_ISREG(inode->i_mode))
626                         rc = mds_convert_lov_ea(obd, inode, md, lmm_size);
627                 if (S_ISDIR(inode->i_mode))
628                         rc = mds_convert_mea_ea(obd, inode, md, lmm_size);
629
630                 if (rc == 0) {
631                         *size = lmm_size;
632                         rc = lmm_size;
633                 } else if (rc > 0) {
634                         *size = rc;
635                 }
636         }
637
638         RETURN (rc);
639 }
640
641
642 /* Call with lock=1 if you want mds_pack_md to take the i_sem.
643  * Call with lock=0 if the caller has already taken the i_sem. */
644 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
645                 struct mds_body *body, struct inode *inode, int lock)
646 {
647         struct mds_obd *mds = &obd->u.mds;
648         void *lmm;
649         int lmm_size;
650         int rc;
651         ENTRY;
652
653         lmm = lustre_msg_buf(msg, offset, 0);
654         if (lmm == NULL) {
655                 /* Some problem with getting eadata when I sized the reply
656                  * buffer... */
657                 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
658                        inode->i_ino);
659                 RETURN(0);
660         }
661         lmm_size = msg->buflens[offset];
662
663         /* I don't really like this, but it is a sanity check on the client
664          * MD request.  However, if the client doesn't know how much space
665          * to reserve for the MD, it shouldn't be bad to have too much space.
666          */
667         if (lmm_size > mds->mds_max_mdsize) {
668                 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
669                        inode->i_ino, lmm_size, mds->mds_max_mdsize);
670                 // RETURN(-EINVAL);
671         }
672
673         rc = mds_get_md(obd, inode, lmm, &lmm_size, lock);
674         if (rc > 0) {
675                 if (S_ISDIR(inode->i_mode))
676                         body->valid |= OBD_MD_FLDIREA;
677                 else
678                         body->valid |= OBD_MD_FLEASIZE;
679                 body->eadatasize = lmm_size;
680                 rc = 0;
681         }
682
683         RETURN(rc);
684 }
685
686 void mds_squash_root(struct mds_obd *mds, struct mds_req_sec_desc *rsd,
687                      ptl_nid_t *peernid)
688 {
689         if (!mds->mds_squash_uid ||
690             (rsd->rsd_uid && rsd->rsd_fsuid))
691                 return;
692
693         if (*peernid == mds->mds_nosquash_nid)
694                 return;
695
696         CDEBUG(D_OTHER, "squash req from 0x%llx, (%d:%d/%x)=>(%d:%d/%x)\n",
697                 *peernid, 
698                 rsd->rsd_fsuid, rsd->rsd_fsgid, rsd->rsd_cap,
699                 mds->mds_squash_uid, mds->mds_squash_gid,
700                 (rsd->rsd_cap & ~CAP_FS_MASK));
701
702         rsd->rsd_uid = mds->mds_squash_uid;
703         rsd->rsd_fsuid = mds->mds_squash_uid;
704         rsd->rsd_fsgid = mds->mds_squash_gid;
705         /* XXX should we remove all capabilities? */
706         rsd->rsd_cap &= ~CAP_FS_MASK;
707 }
708
709 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
710                                 struct ptlrpc_request *req,
711                                 struct mds_body *reqbody, int reply_off)
712 {
713         struct mds_body *body;
714         struct inode *inode = dentry->d_inode;
715         int rc = 0;
716         ENTRY;
717
718         if (inode == NULL && !(dentry->d_flags & DCACHE_CROSS_REF))
719                 RETURN(-ENOENT);
720
721         body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
722         LASSERT(body != NULL);                 /* caller prepped reply */
723
724         if (dentry->d_flags & DCACHE_CROSS_REF) {
725                 CDEBUG(D_OTHER, "cross reference: %lu/%lu/%lu\n",
726                        (unsigned long) dentry->d_mdsnum,
727                        (unsigned long) dentry->d_inum,
728                        (unsigned long) dentry->d_generation);
729                 body->valid |= OBD_MD_FLID | OBD_MD_MDS;
730                 body->fid1.id = dentry->d_inum;
731                 body->fid1.mds = dentry->d_mdsnum;
732                 body->fid1.generation = dentry->d_generation;
733                 RETURN(0);
734         }
735         mds_pack_inode2fid(obd, &body->fid1, inode);
736         mds_pack_inode2body(obd, body, inode);
737
738         if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
739             (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
740                 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1, body,
741                                  inode, 1);
742
743                 /* If we have LOV EA data, the OST holds size, atime, mtime */
744                 if (!(body->valid & OBD_MD_FLEASIZE) &&
745                     !(body->valid & OBD_MD_FLDIREA))
746                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
747                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
748         } else if (S_ISLNK(inode->i_mode) &&
749                    (reqbody->valid & OBD_MD_LINKNAME) != 0) {
750                 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1,0);
751                 int len;
752
753                 LASSERT (symname != NULL);       /* caller prepped reply */
754                 len = req->rq_repmsg->buflens[reply_off + 1];
755
756                 rc = inode->i_op->readlink(dentry, symname, len);
757                 if (rc < 0) {
758                         CERROR("readlink failed: %d\n", rc);
759                 } else if (rc != len - 1) {
760                         CERROR ("Unexpected readlink rc %d: expecting %d\n",
761                                 rc, len - 1);
762                         rc = -EINVAL;
763                 } else {
764                         CDEBUG(D_INODE, "read symlink dest %s\n", symname);
765                         body->valid |= OBD_MD_LINKNAME;
766                         body->eadatasize = rc + 1;
767                         symname[rc] = 0;        /* NULL terminate */
768                         rc = 0;
769                 }
770         }
771
772         RETURN(rc);
773 }
774
775 static int mds_getattr_pack_msg_cf(struct ptlrpc_request *req,
776                                    struct dentry *dentry,
777                                    int offset)
778 {
779         int rc = 0, size[1] = {sizeof(struct mds_body)};
780         ENTRY;
781
782         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
783                 CERROR("failed MDS_GETATTR_PACK test\n");
784                 req->rq_status = -ENOMEM;
785                 GOTO(out, rc = -ENOMEM);
786         }
787
788         rc = lustre_pack_reply(req, 1, size, NULL);
789         if (rc) {
790                 CERROR("out of memory\n");
791                 GOTO(out, req->rq_status = rc);
792         }
793
794         EXIT;
795  out:
796         return(rc);
797 }
798
799 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct inode *inode,
800                                 int offset)
801 {
802         struct mds_obd *mds = mds_req2mds(req);
803         struct mds_body *body;
804         int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
805         ENTRY;
806
807         body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*body));
808         LASSERT(body != NULL);                 /* checked by caller */
809         LASSERT_REQSWABBED(req, offset);       /* swabbed by caller */
810
811         if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
812             (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
813                 int rc;
814                 down(&inode->i_sem);
815                 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0);
816                 up(&inode->i_sem);
817                 CDEBUG(D_INODE, "got %d bytes MD data for inode %lu\n",
818                        rc, inode->i_ino);
819                 if (rc < 0) {
820                         if (rc != -ENODATA)
821                                 CERROR("error getting inode %lu MD: rc = %d\n",
822                                        inode->i_ino, rc);
823                         size[bufcount] = 0;
824                 } else if (rc > mds->mds_max_mdsize) {
825                         size[bufcount] = 0;
826                         CERROR("MD size %d larger than maximum possible %u\n",
827                                rc, mds->mds_max_mdsize);
828                 } else {
829                         size[bufcount] = rc;
830                 }
831                 bufcount++;
832         } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
833                 if (inode->i_size + 1 != body->eadatasize)
834                         CERROR("symlink size: %Lu, reply space: %d\n",
835                                inode->i_size + 1, body->eadatasize);
836                 size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
837                 bufcount++;
838                 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
839                        inode->i_size + 1, body->eadatasize);
840         }
841
842         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
843                 CERROR("failed MDS_GETATTR_PACK test\n");
844                 req->rq_status = -ENOMEM;
845                 GOTO(out, rc = -ENOMEM);
846         }
847
848         rc = lustre_pack_reply(req, bufcount, size, NULL);
849         if (rc) {
850                 CERROR("out of memory\n");
851                 GOTO(out, req->rq_status = rc);
852         }
853
854         EXIT;
855  out:
856         return(rc);
857 }
858
859 int mds_check_mds_num(struct obd_device *obd, struct inode* inode,
860                       char *name, int namelen)
861 {
862         struct mea *mea = NULL;
863         int mea_size, rc = 0;
864         ENTRY;
865
866         rc = mds_get_lmv_attr(obd, inode, &mea, &mea_size);
867         if (rc)
868                 RETURN(rc);
869         if (mea != NULL) {
870                 /* dir is already splitted, check if requested filename
871                  * should live at this MDS or at another one */
872                 int i;
873                 i = mea_name2idx(mea, name, namelen - 1);
874                 if (mea->mea_master != mea->mea_fids[i].mds) {
875                         CDEBUG(D_OTHER,
876                                "inapropriate MDS(%d) for %s. should be %d(%d)\n",
877                                mea->mea_master, name, mea->mea_fids[i].mds, i);
878                         rc = -ERESTART;
879                 }
880         }
881
882         if (mea)
883                 OBD_FREE(mea, mea_size);
884         RETURN(rc);
885 }
886
887 static int mds_getattr_name(struct ptlrpc_request *req, int offset,
888                             struct lustre_handle *child_lockh, int child_part)
889 {
890         struct obd_device *obd = req->rq_export->exp_obd;
891         struct mds_obd *mds = &obd->u.mds;
892         struct ldlm_reply *rep = NULL;
893         struct lvfs_run_ctxt saved;
894         struct mds_req_sec_desc *rsd;
895         struct mds_body *body;
896         struct dentry *dparent = NULL, *dchild = NULL;
897         struct lvfs_ucred uc;
898         struct lustre_handle parent_lockh[2];
899         int namesize, update_mode;
900         int rc = 0, cleanup_phase = 0, resent_req = 0, reply_offset;
901         struct clonefs_info *clone_info = NULL;
902         char *name;
903         ENTRY;
904
905         LASSERT(!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME));
906
907         MDS_UPDATE_COUNTER((&obd->u.mds), MDS_GETATTR_NAME_COUNT);
908
909         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
910         if (!rsd) {
911                 CERROR("Can't unpack security desc\n");
912                 RETURN(-EFAULT);
913         }
914         mds_squash_root(mds, rsd, &req->rq_peer.peer_id.nid); 
915
916         /* Swab now, before anyone looks inside the request */
917         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
918                                   lustre_swab_mds_body);
919         if (body == NULL) {
920                 CERROR("Can't swab mds_body\n");
921                 GOTO(cleanup, rc = -EFAULT);
922         }
923
924         LASSERT_REQSWAB(req, offset + 1);
925         name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
926         if (name == NULL) {
927                 CERROR("Can't unpack name\n");
928                 GOTO(cleanup, rc = -EFAULT);
929         }
930         namesize = req->rq_reqmsg->buflens[offset + 1];
931
932         LASSERT (offset == 1 || offset == 3);
933         /* if requests were at offset 3, the getattr reply goes back at 1 */
934         if (offset == 3) {
935                 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
936                 reply_offset = 1;
937         } else {
938                 reply_offset = 0;
939         }
940
941         rc = mds_init_ucred(&uc, rsd);
942         if (rc) {
943                 CERROR("can't init ucred\n");
944                 GOTO(cleanup, rc);
945         }
946
947         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
948         cleanup_phase = 1; /* kernel context */
949         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
950
951         LASSERT(namesize > 0);
952         if (namesize == 1) {
953                 /* we have no dentry here, drop LOOKUP bit */
954                 child_part &= ~MDS_INODELOCK_LOOKUP;
955                 CDEBUG(D_OTHER, "%s: request to retrieve attrs for %lu/%lu\n",
956                        obd->obd_name, (unsigned long) body->fid1.id,
957                        (unsigned long) body->fid1.generation);
958                 dchild = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_PR,
959                                                parent_lockh, &update_mode, 
960                                                NULL, 0, child_part);
961                 if (IS_ERR(dchild)) {
962                         CERROR("can't find inode: %d\n", (int) PTR_ERR(dchild));
963                         GOTO(cleanup, rc = PTR_ERR(dchild));
964                 }
965                 memcpy(child_lockh, parent_lockh, sizeof(parent_lockh[0]));
966 #ifdef S_PDIROPS
967                 if (parent_lockh[1].cookie)
968                         ldlm_lock_decref(parent_lockh + 1, update_mode);
969 #endif
970                 cleanup_phase = 2;
971                 goto fill_inode;
972         }
973         
974 #if HAVE_LOOKUP_RAW
975         /* FIXME: handle raw lookup */
976         if (body->valid == OBD_MD_FLID) {
977                 struct mds_body *mds_reply;
978                 int size = sizeof(*mds_reply);
979                 struct inode *dir;
980                 ino_t inum;
981                 dparent = mds_fid2dentry(mds, &body->fid1, NULL);
982                 if (IS_ERR(dparent)) {
983                         rc = PTR_ERR(dparent);
984                         GOTO(cleanup, rc);
985                 }
986                 // The user requested ONLY the inode number, so do a raw lookup
987                 rc = lustre_pack_reply(req, 1, &size, NULL);
988                 if (rc) {
989                         CERROR("out of memory\n");
990                         l_dput(dparent);
991                         GOTO(cleanup, rc);
992                 }
993                 dir  = dparent->d_inode;
994                 LASSERT(dir->i_op->lookup_raw != NULL);
995                 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
996                 l_dput(dparent);
997                 mds_reply = lustre_msg_buf(req->rq_repmsg, 0,
998                                            sizeof(*mds_reply));
999                 mds_reply->fid1.id = inum;
1000                 mds_reply->valid = OBD_MD_FLID;
1001                 GOTO(cleanup, rc);
1002         }
1003 #endif
1004
1005         if (child_lockh->cookie != 0) {
1006                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1007                 resent_req = 1;
1008         }
1009
1010         if (resent_req == 0) {
1011                 rc = mds_get_parent_child_locked(obd, mds, &body->fid1,
1012                                                  parent_lockh, &dparent,
1013                                                  LCK_PR, MDS_INODELOCK_LOOKUP,
1014                                                  &update_mode, name, namesize,
1015                                                  child_lockh, &dchild, LCK_PR,
1016                                                  child_part, clone_info);
1017                 if (rc)
1018                         GOTO(cleanup, rc);
1019                 
1020                 /* let's make sure this name should leave on this mds node */
1021                 rc = mds_check_mds_num(obd, dparent->d_inode, name, namesize);
1022                 if (rc)
1023                         GOTO(cleanup, rc);
1024         } else {
1025                 struct ldlm_lock *granted_lock;
1026                 struct ll_fid child_fid;
1027                 struct ldlm_resource *res;
1028                 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
1029                 granted_lock = ldlm_handle2lock(child_lockh);
1030                 LASSERT(granted_lock);
1031
1032                 res = granted_lock->l_resource;
1033                 child_fid.id = res->lr_name.name[0];
1034                 child_fid.generation = res->lr_name.name[1];
1035                 dchild = mds_fid2dentry(mds, &child_fid, NULL);
1036                 LASSERT(dchild);
1037                 LDLM_LOCK_PUT(granted_lock);
1038         }
1039
1040         cleanup_phase = 2; /* dchild, dparent, locks */
1041
1042 fill_inode:
1043
1044         if (!DENTRY_VALID(dchild)) {
1045                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
1046                 /* in the intent case, the policy clears this error:
1047                    the disposition is enough */
1048                 rc = -ENOENT;
1049                 GOTO(cleanup, rc);
1050         } else {
1051                 intent_set_disposition(rep, DISP_LOOKUP_POS);
1052         }
1053
1054         if (req->rq_repmsg == NULL) {
1055                 if (dchild->d_flags & DCACHE_CROSS_REF)
1056                         rc = mds_getattr_pack_msg_cf(req, dchild, offset);
1057                 else
1058                         rc = mds_getattr_pack_msg(req, dchild->d_inode, offset);
1059                 if (rc != 0) {
1060                         CERROR ("mds_getattr_pack_msg: %d\n", rc);
1061                         GOTO (cleanup, rc);
1062                 }
1063         }
1064
1065         rc = mds_getattr_internal(obd, dchild, req, body, reply_offset);
1066         GOTO(cleanup, rc); /* returns the lock to the client */
1067
1068  cleanup:
1069         switch (cleanup_phase) {
1070         case 2:
1071                 if (resent_req == 0) {
1072                         if (rc && DENTRY_VALID(dchild))
1073                                 ldlm_lock_decref(child_lockh, LCK_PR);
1074                         if (dparent) {
1075                                 ldlm_lock_decref(parent_lockh, LCK_PR);
1076 #ifdef S_PDIROPS
1077                                 if (parent_lockh[1].cookie != 0)
1078                                         ldlm_lock_decref(parent_lockh + 1,
1079                                                          update_mode);
1080 #endif
1081                         }
1082                         if (dparent)
1083                                 l_dput(dparent);
1084                 }
1085                 l_dput(dchild);
1086         case 1:
1087                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1088                 mds_exit_ucred(&uc);
1089         default: ;
1090         }
1091         return rc;
1092 }
1093
1094 static int mds_getattr(struct ptlrpc_request *req, int offset)
1095 {
1096         struct mds_obd *mds = mds_req2mds(req);
1097         struct obd_device *obd = req->rq_export->exp_obd;
1098         struct lvfs_run_ctxt saved;
1099         struct dentry *de;
1100         struct mds_req_sec_desc *rsd;
1101         struct mds_body *body;
1102         struct lvfs_ucred uc;
1103         int rc = 0;
1104         ENTRY;
1105
1106         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1107         if (!rsd) {
1108                 CERROR("Can't unpack security desc\n");
1109                 RETURN(-EFAULT);
1110         }
1111
1112         body = lustre_swab_reqbuf(req, offset, sizeof (*body),
1113                                   lustre_swab_mds_body);
1114         if (body == NULL) {
1115                 CERROR ("Can't unpack body\n");
1116                 RETURN (-EFAULT);
1117         }
1118
1119         MDS_UPDATE_COUNTER(mds, MDS_GETATTR_COUNT);
1120
1121         rc = mds_init_ucred(&uc, rsd);
1122         if (rc) {
1123                 CERROR("can't init ucred\n");
1124                 RETURN(rc);
1125         }
1126
1127         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1128         de = mds_fid2dentry(mds, &body->fid1, NULL);
1129         if (IS_ERR(de)) {
1130                 rc = req->rq_status = -ENOENT;
1131                 GOTO(out_pop, PTR_ERR(de));
1132         }
1133
1134         rc = mds_getattr_pack_msg(req, de->d_inode, offset);
1135         if (rc != 0) {
1136                 CERROR ("mds_getattr_pack_msg: %d\n", rc);
1137                 GOTO (out_pop, rc);
1138         }
1139
1140         req->rq_status = mds_getattr_internal(obd, de, req, body, 0);
1141
1142         l_dput(de);
1143         GOTO(out_pop, rc);
1144 out_pop:
1145         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1146         mds_exit_ucred(&uc);
1147         return rc;
1148 }
1149
1150 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1151                           unsigned long max_age)
1152 {
1153         int rc;
1154
1155         spin_lock(&obd->obd_osfs_lock);
1156         rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
1157         if (rc == 0)
1158                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1159         spin_unlock(&obd->obd_osfs_lock);
1160
1161         return rc;
1162 }
1163
1164 static int mds_statfs(struct ptlrpc_request *req)
1165 {
1166         struct obd_device *obd = req->rq_export->exp_obd;
1167         int rc, size = sizeof(struct obd_statfs);
1168         ENTRY;
1169
1170         rc = lustre_pack_reply(req, 1, &size, NULL);
1171         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
1172                 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
1173                 GOTO(out, rc);
1174         }
1175
1176         MDS_UPDATE_COUNTER((&obd->u.mds), MDS_STATFS_COUNT);
1177
1178         /* We call this so that we can cache a bit - 1 jiffie worth */
1179         rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
1180                             jiffies - HZ);
1181         if (rc) {
1182                 CERROR("mds_obd_statfs failed: rc %d\n", rc);
1183                 GOTO(out, rc);
1184         }
1185
1186         EXIT;
1187 out:
1188         req->rq_status = rc;
1189         return 0;
1190 }
1191
1192 static int mds_sync(struct ptlrpc_request *req, int offset)
1193 {
1194         struct obd_device *obd = req->rq_export->exp_obd;
1195         struct mds_obd *mds = &obd->u.mds;
1196         struct mds_body *body;
1197         int rc, size = sizeof(*body);
1198         ENTRY;
1199
1200         body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*body));
1201         if (body == NULL)
1202                 GOTO(out, rc = -EPROTO);
1203
1204         rc = lustre_pack_reply(req, 1, &size, NULL);
1205         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
1206                 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
1207                 GOTO(out, rc);
1208         }
1209
1210         if (body->fid1.id == 0) {
1211                 /* a fid of zero is taken to mean "sync whole filesystem" */
1212                 rc = fsfilt_sync(obd, mds->mds_sb);
1213                 if (rc)
1214                         GOTO(out, rc);
1215         } else {
1216                 /* just any file to grab fsync method - "file" arg unused */
1217                 struct file *file = mds->mds_rcvd_filp;
1218                 struct dentry *de;
1219
1220                 de = mds_fid2dentry(mds, &body->fid1, NULL);
1221                 if (IS_ERR(de))
1222                         GOTO(out, rc = PTR_ERR(de));
1223
1224                 rc = file->f_op->fsync(NULL, de, 1);
1225                 l_dput(de);
1226                 if (rc)
1227                         GOTO(out, rc);
1228
1229                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1230                 mds_pack_inode2fid(obd, &body->fid1, de->d_inode);
1231                 mds_pack_inode2body(obd, body, de->d_inode);
1232         }
1233 out:
1234         req->rq_status = rc;
1235         return 0;
1236 }
1237
1238 /* mds_readpage does not take a DLM lock on the inode, because the client must
1239  * already have a PR lock.
1240  *
1241  * If we were to take another one here, a deadlock will result, if another
1242  * thread is already waiting for a PW lock. */
1243 static int mds_readpage(struct ptlrpc_request *req, int offset)
1244 {
1245         struct obd_device *obd = req->rq_export->exp_obd;
1246         struct mds_obd *mds = &obd->u.mds;
1247         struct vfsmount *mnt;
1248         struct dentry *de;
1249         struct file *file;
1250         struct mds_req_sec_desc *rsd;
1251         struct mds_body *body, *repbody;
1252         struct lvfs_run_ctxt saved;
1253         int rc, size = sizeof(*repbody);
1254         struct lvfs_ucred uc;
1255         ENTRY;
1256
1257         rc = lustre_pack_reply(req, 1, &size, NULL);
1258         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
1259                 CERROR("mds: out of memory\n");
1260                 GOTO(out, rc = -ENOMEM);
1261         }
1262
1263         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1264         if (!rsd) {
1265                 CERROR("Can't unpack security desc\n");
1266                 GOTO (out, rc = -EFAULT);
1267         }
1268         mds_squash_root(mds, rsd, &req->rq_peer.peer_id.nid); 
1269
1270         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1271                                   lustre_swab_mds_body);
1272         if (body == NULL) {
1273                 CERROR("Can't unpack body\n");
1274                 GOTO (out, rc = -EFAULT);
1275         }
1276
1277         rc = mds_init_ucred(&uc, rsd);
1278         if (rc) {
1279                 CERROR("can't init ucred\n");
1280                 GOTO(out, rc);
1281         }
1282
1283         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1284         de = mds_fid2dentry(mds, &body->fid1, &mnt);
1285         if (IS_ERR(de))
1286                 GOTO(out_pop, rc = PTR_ERR(de));
1287
1288         CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
1289
1290         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1291         /* note: in case of an error, dentry_open puts dentry */
1292         if (IS_ERR(file))
1293                 GOTO(out_pop, rc = PTR_ERR(file));
1294
1295         /* body->size is actually the offset -eeb */
1296         if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
1297                 CERROR("offset "LPU64" not on a block boundary of %lu\n",
1298                        body->size, de->d_inode->i_blksize);
1299                 GOTO(out_file, rc = -EFAULT);
1300         }
1301
1302         /* body->nlink is actually the #bytes to read -eeb */
1303         if (body->nlink & (de->d_inode->i_blksize - 1)) {
1304                 CERROR("size %u is not multiple of blocksize %lu\n",
1305                        body->nlink, de->d_inode->i_blksize);
1306                 GOTO(out_file, rc = -EFAULT);
1307         }
1308
1309         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
1310         repbody->size = file->f_dentry->d_inode->i_size;
1311         repbody->valid = OBD_MD_FLSIZE;
1312
1313         /* to make this asynchronous make sure that the handling function
1314            doesn't send a reply when this function completes. Instead a
1315            callback function would send the reply */
1316         /* body->size is actually the offset -eeb */
1317         rc = mds_sendpage(req, file, body->size, body->nlink);
1318
1319 out_file:
1320         filp_close(file, 0);
1321 out_pop:
1322         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1323         mds_exit_ucred(&uc);
1324 out:
1325         req->rq_status = rc;
1326         RETURN(0);
1327 }
1328
1329 int mds_reint(struct ptlrpc_request *req, int offset,
1330               struct lustre_handle *lockh)
1331 {
1332         struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
1333         struct mds_update_record *rec; /* 116 bytes on the stack?  no sir! */
1334         struct mds_req_sec_desc *rsd;
1335         int rc;
1336         ENTRY;
1337
1338         OBD_ALLOC(rec, sizeof(*rec));
1339         if (rec == NULL)
1340                 RETURN(-ENOMEM);
1341
1342         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1343         if (!rsd) {
1344                 CERROR("Can't unpack security desc\n");
1345                 GOTO(out, rc = -EFAULT);
1346         }
1347         mds_squash_root(mds, rsd, &req->rq_peer.peer_id.nid); 
1348
1349         rc = mds_update_unpack(req, offset, rec);
1350         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1351                 CERROR("invalid record\n");
1352                 GOTO(out, req->rq_status = -EINVAL);
1353         }
1354
1355         rc = mds_init_ucred(&rec->ur_uc, rsd);
1356         if (rc) {
1357                 CERROR("can't init ucred\n");
1358                 GOTO(out, rc);
1359         }
1360
1361         /* rc will be used to interrupt a for loop over multiple records */
1362         rc = mds_reint_rec(rec, offset, req, lockh);
1363
1364         mds_exit_ucred(&rec->ur_uc);
1365  out:
1366         OBD_FREE(rec, sizeof(*rec));
1367         RETURN(rc);
1368 }
1369
1370 static int mds_filter_recovery_request(struct ptlrpc_request *req,
1371                                        struct obd_device *obd, int *process)
1372 {
1373         switch (req->rq_reqmsg->opc) {
1374         case MDS_CONNECT: /* This will never get here, but for completeness. */
1375         case OST_CONNECT: /* This will never get here, but for completeness. */
1376         case MDS_DISCONNECT:
1377         case OST_DISCONNECT:
1378                *process = 1;
1379                RETURN(0);
1380
1381         case MDS_CLOSE:
1382         case MDS_SYNC: /* used in unmounting */
1383         case OBD_PING:
1384         case MDS_REINT:
1385         case LDLM_ENQUEUE:
1386         case OST_CREATE:
1387                 *process = target_queue_recovery_request(req, obd);
1388                 RETURN(0);
1389
1390         default:
1391                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1392                 *process = 0;
1393                 /* XXX what should we set rq_status to here? */
1394                 req->rq_status = -EAGAIN;
1395                 RETURN(ptlrpc_error(req));
1396         }
1397 }
1398
1399 static char *reint_names[] = {
1400         [REINT_SETATTR] "setattr",
1401         [REINT_CREATE]  "create",
1402         [REINT_LINK]    "link",
1403         [REINT_UNLINK]  "unlink",
1404         [REINT_RENAME]  "rename",
1405         [REINT_OPEN]    "open",
1406 };
1407
1408 #define FILTER_VALID_FLAGS (OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLGENER  |\
1409                             OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ|\
1410                             OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME|\
1411                             OBD_MD_FLID) 
1412
1413 static void reconstruct_create(struct ptlrpc_request *req)
1414 {
1415         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1416         struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
1417         struct mds_client_data *mcd = med->med_mcd;
1418         struct dentry *dentry;
1419         struct ost_body *body;
1420         struct ll_fid fid;
1421         ENTRY;
1422
1423         /* copy rc, transno and disp; steal locks */
1424         mds_req_from_mcd(req, mcd);
1425         if (req->rq_status) {
1426                 EXIT;
1427                 return;
1428         }
1429
1430         fid.mds = 0;
1431         fid.id = mcd->mcd_last_data;
1432         fid.generation = 0;
1433
1434         LASSERT(fid.id != 0);
1435
1436         dentry = mds_fid2dentry(mds, &fid, NULL);
1437         if (IS_ERR(dentry)) {
1438                 CERROR("can't find inode "LPU64"\n", fid.id);
1439                 req->rq_status = PTR_ERR(dentry);
1440                 EXIT;
1441                 return;
1442         }
1443
1444         CWARN("reconstruct reply for x"LPU64" (remote ino) "LPU64" -> %lu/%u\n",
1445               req->rq_xid, fid.id, dentry->d_inode->i_ino,
1446               dentry->d_inode->i_generation);
1447
1448         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
1449         obdo_from_inode(&body->oa, dentry->d_inode, FILTER_VALID_FLAGS);
1450         body->oa.o_id = dentry->d_inode->i_ino;
1451         body->oa.o_generation = dentry->d_inode->i_generation;
1452         body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1453                 
1454         l_dput(dentry);
1455         EXIT;
1456         return;
1457 }
1458
1459 static int mdt_obj_create(struct ptlrpc_request *req)
1460 {
1461         struct obd_device *obd = req->rq_export->exp_obd;
1462         struct mds_obd *mds = &obd->u.mds;
1463         struct ost_body *body, *repbody;
1464         char fidname[LL_FID_NAMELEN];
1465         struct inode *parent_inode;
1466         struct lvfs_run_ctxt saved;
1467         struct dentry *new = NULL;
1468         struct dentry_params dp;
1469         int mealen, flags = 0, rc, size = sizeof(*repbody), cleanup_phase = 0;
1470         struct lvfs_ucred uc;
1471         struct mea *mea;
1472         void *handle = NULL;
1473         unsigned long cr_inum = 0;
1474         ENTRY;
1475        
1476         DEBUG_REQ(D_HA, req, "create remote object");
1477
1478         parent_inode = mds->mds_unnamed_dir->d_inode;
1479
1480         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
1481         if (body == NULL)
1482                 RETURN(-EFAULT);
1483
1484         rc = lustre_pack_reply(req, 1, &size, NULL);
1485         if (rc)
1486                 RETURN(rc);
1487
1488         MDS_CHECK_RESENT(req, reconstruct_create(req));
1489
1490         /* FIXME don't understand what security context we should
1491          * take here. check this -ericm
1492          */
1493         uc.luc_fsuid = body->oa.o_uid;
1494         uc.luc_fsgid = body->oa.o_gid;
1495         uc.luc_uid = body->oa.o_uid;
1496         uc.luc_ghash = mds_get_group_entry(mds, body->oa.o_uid);
1497         uc.luc_ginfo = NULL;
1498         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1499         
1500         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
1501
1502         if (body->oa.o_flags & OBD_FL_RECREATE_OBJS) {
1503                 /* this is re-create request from MDS holding directory name.
1504                  * we have to lookup given ino/generation first. if it exists
1505                  * (good case) then there is nothing to do. if it does not
1506                  * then we have to recreate it */
1507                 struct ll_fid fid;
1508                 fid.id = body->oa.o_id;
1509                 fid.generation = body->oa.o_generation;
1510                 new = mds_fid2dentry(mds, &fid, NULL);
1511                 if (!IS_ERR(new) && new->d_inode) {
1512                         CDEBUG(D_HA, "mkdir() repairing is on its way: %lu/%lu\n",
1513                                (unsigned long) fid.id,
1514                                (unsigned long) fid.generation);
1515                         obdo_from_inode(&repbody->oa, new->d_inode,
1516                                         FILTER_VALID_FLAGS);
1517                         repbody->oa.o_id = new->d_inode->i_ino;
1518                         repbody->oa.o_generation = new->d_inode->i_generation;
1519                         repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1520                         cleanup_phase = 1;
1521                         cr_inum = new->d_inode->i_ino;
1522                         GOTO(cleanup, rc = 0);
1523                 }
1524                 CWARN("hmm. for some reason dir %lu/%lu (or reply) got lost\n",
1525                       (unsigned long) fid.id, (unsigned long) fid.generation);
1526                 LASSERT(new->d_inode == NULL ||
1527                         new->d_inode->i_generation != fid.generation);
1528                 l_dput(new); 
1529         }
1530         
1531         down(&parent_inode->i_sem);
1532         handle = fsfilt_start(obd, parent_inode, FSFILT_OP_MKDIR, NULL);
1533         if (IS_ERR(handle)) {
1534                 up(&parent_inode->i_sem);
1535                 GOTO(cleanup, rc = PTR_ERR(handle));
1536         }
1537         cleanup_phase = 1; /* transaction */
1538
1539 repeat:
1540         rc = sprintf(fidname, "%u.%u", ll_insecure_random_int(), current->pid);
1541         new = lookup_one_len(fidname, mds->mds_unnamed_dir, rc);
1542         if (IS_ERR(new)) {
1543                 CERROR("%s: can't lookup new inode (%s) for mkdir: %d\n",
1544                        obd->obd_name, fidname, (int) PTR_ERR(new));
1545                 fsfilt_commit(obd, mds->mds_sb, new->d_inode, handle, 0);
1546                 up(&parent_inode->i_sem);
1547                 RETURN(PTR_ERR(new));
1548         } else if (new->d_inode) {
1549                 CERROR("%s: name exists. repeat\n", obd->obd_name);
1550                 goto repeat;
1551         }
1552
1553         new->d_fsdata = (void *) &dp;
1554         dp.p_inum = 0;
1555         dp.p_ptr = req;
1556
1557         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1558                 DEBUG_REQ(D_HA, req, "replay create obj %lu/%lu",
1559                           (unsigned long) body->oa.o_id,
1560                           (unsigned long) body->oa.o_generation);
1561                 dp.p_inum = body->oa.o_id;
1562         }
1563         rc = vfs_mkdir(parent_inode, new, body->oa.o_mode);
1564         if (rc == 0) {
1565                 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1566                         LASSERTF(body->oa.o_id == new->d_inode->i_ino, 
1567                                  "BUG 3550: failed to recreate obj "
1568                                  LPU64" -> %lu\n",
1569                                  body->oa.o_id, new->d_inode->i_ino);
1570                         LASSERTF(body->oa.o_generation == 
1571                                  new->d_inode->i_generation,
1572                                  "BUG 3550: failed to recreate obj/gen "
1573                                  LPU64"/%u -> %lu/%u\n",
1574                                  body->oa.o_id, body->oa.o_generation,
1575                                  new->d_inode->i_ino, 
1576                                  new->d_inode->i_generation);
1577                 }
1578                 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1579                         new->d_inode->i_generation = body->oa.o_generation;
1580                         mark_inode_dirty(new->d_inode);
1581                 }
1582
1583                 obdo_from_inode(&repbody->oa, new->d_inode, FILTER_VALID_FLAGS);
1584                 repbody->oa.o_id = new->d_inode->i_ino;
1585                 repbody->oa.o_generation = new->d_inode->i_generation;
1586                 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1587
1588                 rc = fsfilt_del_dir_entry(obd, new);
1589                 up(&parent_inode->i_sem);
1590
1591                 if (rc) {
1592                         CERROR("can't remove name for object: %d\n", rc);
1593                         GOTO(cleanup, rc);
1594                 }
1595                         
1596                 cleanup_phase = 2; /* created directory object */
1597
1598                 cr_inum = new->d_inode->i_ino;
1599                 CDEBUG(D_OTHER, "created dirobj: %lu/%u mode %o\n",
1600                        new->d_inode->i_ino, new->d_inode->i_generation,
1601                        new->d_inode->i_mode);
1602         } else {
1603                 up(&parent_inode->i_sem);
1604                 CERROR("%s: can't create dirobj: %d\n", obd->obd_name, rc);
1605                 GOTO(cleanup, rc);
1606         }
1607
1608         if (body->oa.o_valid & OBD_MD_FLID) {
1609                 /* this is new object for splitted dir. we have to
1610                  * prevent recursive splitting on it -bzzz */
1611                 mealen = obd_size_diskmd(mds->mds_lmv_exp, NULL);
1612                 OBD_ALLOC(mea, mealen);
1613                 if (mea == NULL)
1614                         GOTO(cleanup, rc = -ENOMEM);
1615                 mea->mea_magic = MEA_MAGIC_ALL_CHARS;
1616                 mea->mea_master = 0;
1617                 mea->mea_count = 0;
1618                 down(&new->d_inode->i_sem);
1619                 rc = fsfilt_set_md(obd, new->d_inode, handle, mea, mealen);
1620                 up(&new->d_inode->i_sem);
1621                 OBD_FREE(mea, mealen);
1622                 CDEBUG(D_OTHER, "%s: mark non-splittable %lu/%u - %d\n",
1623                        obd->obd_name, new->d_inode->i_ino,
1624                        new->d_inode->i_generation, flags);
1625         } else if (body->oa.o_easize) {
1626                 /* we pass LCK_EX to split routine to signal that we have
1627                  * exclusive access to the directory. simple because nobody
1628                  * knows it already exists -bzzz */
1629                 mds_try_to_split_dir(obd, new, NULL, body->oa.o_easize, LCK_EX);
1630         }
1631
1632 cleanup:
1633         switch (cleanup_phase) {
1634         case 2: /* object has been created, but we'll may want to replay it later */
1635                 if (rc == 0)
1636                         ptlrpc_require_repack(req);
1637         case 1: /* transaction */
1638                 rc = mds_finish_transno(mds, parent_inode, handle, req, rc, cr_inum);
1639         }
1640
1641         l_dput(new);
1642         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1643         mds_put_group_entry(mds, uc.luc_ghash);
1644         RETURN(rc);
1645 }
1646
1647 static int mdt_get_info(struct ptlrpc_request *req)
1648 {
1649         char *key;
1650         struct obd_export *exp = req->rq_export;
1651         int keylen, rc = 0, size = sizeof(obd_id);
1652         obd_id *reply;
1653         ENTRY;
1654
1655         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1656         if (key == NULL) {
1657                 DEBUG_REQ(D_HA, req, "no get_info key");
1658                 RETURN(-EFAULT);
1659         }
1660         keylen = req->rq_reqmsg->buflens[0];
1661
1662         if (keylen < strlen("mdsize") || memcmp(key, "mdsize", 6) != 0)
1663                 RETURN(-EPROTO);
1664
1665         rc = lustre_pack_reply(req, 1, &size, NULL);
1666         if (rc)
1667                 RETURN(rc);
1668
1669         reply = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply));
1670         rc = obd_get_info(exp, keylen, key, &size, reply);
1671         req->rq_repmsg->status = 0;
1672         RETURN(rc);
1673 }
1674
1675 static int mds_set_info(struct obd_export *exp, __u32 keylen,
1676                         void *key, __u32 vallen, void *val)
1677 {
1678         struct obd_device *obd;
1679         struct mds_obd *mds;
1680         int    rc = 0;
1681         ENTRY;
1682
1683         obd = class_exp2obd(exp);
1684         if (obd == NULL) {
1685                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1686                        exp->exp_handle.h_cookie);
1687                 RETURN(-EINVAL);
1688         }
1689
1690 #define KEY_IS(str) \
1691         (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
1692
1693         mds = &obd->u.mds;
1694         if (KEY_IS("mds_num")) {
1695                 int valsize;
1696                 __u32 group;
1697                 CDEBUG(D_IOCTL, "set mds num %d\n", *(int*)val);
1698                 mds->mds_num = *(int*)val;
1699                 group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
1700                 valsize = sizeof(group);
1701                 /*mds number has been changed, so the corresponding obdfilter exp
1702                  *need to be changed too*/
1703                 rc = obd_set_info(mds->mds_osc_exp, strlen("mds_conn"), "mds_conn",
1704                           valsize, &group);
1705                 RETURN(rc);
1706         }
1707         CDEBUG(D_IOCTL, "invalid key\n");
1708         RETURN(-EINVAL);
1709 }
1710
1711 static int mdt_set_info(struct ptlrpc_request *req)
1712 {
1713         char *key, *val;
1714         struct obd_export *exp = req->rq_export;
1715         int keylen, rc = 0, vallen;
1716         ENTRY;
1717
1718         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1719         if (key == NULL) {
1720                 DEBUG_REQ(D_HA, req, "no set_info key");
1721                 RETURN(-EFAULT);
1722         }
1723         keylen = req->rq_reqmsg->buflens[0];
1724
1725         if (keylen == strlen("mds_num") &&
1726             memcmp(key, "mds_num", keylen) == 0) {
1727                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1728                 if (rc)
1729                         RETURN(rc);
1730                 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
1731
1732                 vallen = req->rq_reqmsg->buflens[1];
1733
1734                 rc = obd_set_info(exp, keylen, key, vallen, val);
1735                 req->rq_repmsg->status = 0;
1736                 RETURN(rc);
1737         } else if (keylen == strlen("client") &&
1738                    memcmp(key, "client", keylen) == 0) {
1739                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1740                 if (rc)
1741                         RETURN(rc);
1742                 rc = obd_set_info(exp, keylen, key, sizeof(obd_id), NULL);
1743                 req->rq_repmsg->status = 0;
1744                 RETURN(rc);
1745         } 
1746         CDEBUG(D_IOCTL, "invalid key\n");
1747         RETURN(-EINVAL);
1748 }
1749
1750 static int mds_msg_check_version(struct lustre_msg *msg)
1751 {
1752         int rc;
1753
1754         switch (msg->opc) {
1755         case MDS_CONNECT:
1756         case MDS_DISCONNECT:
1757         case OBD_PING:
1758                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1759                 if (rc)
1760                         CERROR("bad opc %u version %08x, expecting %08x\n",
1761                                msg->opc, msg->version, LUSTRE_OBD_VERSION);
1762                 break;
1763         case MDS_STATFS:
1764         case MDS_GETSTATUS:
1765         case MDS_GETATTR:
1766         case MDS_GETATTR_NAME:
1767         case MDS_READPAGE:
1768         case MDS_REINT:
1769         case MDS_CLOSE:
1770         case MDS_DONE_WRITING:
1771         case MDS_PIN:
1772         case MDS_SYNC:
1773                 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
1774                 if (rc)
1775                         CERROR("bad opc %u version %08x, expecting %08x\n",
1776                                msg->opc, msg->version, LUSTRE_MDS_VERSION);
1777                 break;
1778         case LDLM_ENQUEUE:
1779         case LDLM_CONVERT:
1780         case LDLM_BL_CALLBACK:
1781         case LDLM_CP_CALLBACK:
1782                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1783                 if (rc)
1784                         CERROR("bad opc %u version %08x, expecting %08x\n",
1785                                msg->opc, msg->version, LUSTRE_DLM_VERSION);
1786                 break;
1787         case OBD_LOG_CANCEL:
1788         case LLOG_ORIGIN_HANDLE_OPEN:
1789         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1790         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
1791         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1792         case LLOG_ORIGIN_HANDLE_CLOSE:
1793         case LLOG_CATINFO:
1794                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1795                 if (rc)
1796                         CERROR("bad opc %u version %08x, expecting %08x\n",
1797                                msg->opc, msg->version, LUSTRE_LOG_VERSION);
1798                 break;
1799         case OST_CREATE:
1800         case OST_WRITE:
1801         case OST_GET_INFO:
1802         case OST_SET_INFO:
1803                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1804                 if (rc)
1805                         CERROR("bad opc %u version %08x, expecting %08x\n",
1806                                msg->opc, msg->version, LUSTRE_OBD_VERSION);
1807                 break;
1808         default:
1809                 CERROR("MDS unknown opcode %d\n", msg->opc);
1810                 rc = -ENOTSUPP;
1811                 break;
1812         }
1813
1814         return rc;
1815 }
1816
1817 int mds_handle(struct ptlrpc_request *req)
1818 {
1819         int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
1820         int rc = 0;
1821         struct mds_obd *mds = NULL; /* quell gcc overwarning */
1822         struct obd_device *obd = NULL;
1823         ENTRY;
1824
1825         OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
1826
1827         rc = mds_msg_check_version(req->rq_reqmsg);
1828         if (rc) {
1829                 CERROR("MDS drop mal-formed request\n");
1830                 RETURN(rc);
1831         }
1832
1833         LASSERT(current->journal_info == NULL);
1834         /* XXX identical to OST */
1835         if (req->rq_reqmsg->opc != MDS_CONNECT) {
1836                 struct mds_export_data *med;
1837                 int recovering;
1838
1839                 if (req->rq_export == NULL) {
1840                         CERROR("lustre_mds: operation %d on unconnected MDS\n",
1841                                req->rq_reqmsg->opc);
1842                         req->rq_status = -ENOTCONN;
1843                         GOTO(out, rc = -ENOTCONN);
1844                 }
1845
1846                 med = &req->rq_export->exp_mds_data;
1847                 obd = req->rq_export->exp_obd;
1848                 mds = &obd->u.mds;
1849
1850                 /* sanity check: if the xid matches, the request must
1851                  * be marked as a resent or replayed */
1852                 if (req->rq_xid == med->med_mcd->mcd_last_xid)
1853                         LASSERTF(lustre_msg_get_flags(req->rq_reqmsg) &
1854                                  (MSG_RESENT | MSG_REPLAY),
1855                                  "rq_xid "LPU64" matches last_xid, "
1856                                  "expected RESENT flag\n",
1857                                  req->rq_xid);
1858                 /* else: note the opposite is not always true; a
1859                  * RESENT req after a failover will usually not match
1860                  * the last_xid, since it was likely never
1861                  * committed. A REPLAYed request will almost never
1862                  * match the last xid, however it could for a
1863                  * committed, but still retained, open. */
1864
1865                 spin_lock_bh(&obd->obd_processing_task_lock);
1866                 recovering = obd->obd_recovering;
1867                 spin_unlock_bh(&obd->obd_processing_task_lock);
1868                 if (recovering) {
1869                         rc = mds_filter_recovery_request(req, obd,
1870                                                          &should_process);
1871                         if (rc || should_process == 0) {
1872                                 RETURN(rc);
1873                         } else if (should_process < 0) {
1874                                 req->rq_status = should_process;
1875                                 rc = ptlrpc_error(req);
1876                                 RETURN(rc);
1877                         }
1878                 }
1879         }
1880
1881         switch (req->rq_reqmsg->opc) {
1882         case MDS_CONNECT:
1883                 DEBUG_REQ(D_INODE, req, "connect");
1884                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
1885                 rc = target_handle_connect(req);
1886                 if (!rc)
1887                         /* Now that we have an export, set mds. */
1888                         mds = mds_req2mds(req);
1889                 break;
1890
1891         case MDS_DISCONNECT:
1892                 DEBUG_REQ(D_INODE, req, "disconnect");
1893                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
1894                 rc = target_handle_disconnect(req);
1895                 req->rq_status = rc;            /* superfluous? */
1896                 break;
1897
1898         case MDS_GETSTATUS:
1899                 DEBUG_REQ(D_INODE, req, "getstatus");
1900                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1901                 rc = mds_getstatus(req);
1902                 break;
1903
1904         case MDS_GETATTR:
1905                 DEBUG_REQ(D_INODE, req, "getattr");
1906                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1907                 rc = mds_getattr(req, MDS_REQ_REC_OFF);
1908                 break;
1909
1910         case MDS_GETATTR_NAME: {
1911                 struct lustre_handle lockh;
1912                 DEBUG_REQ(D_INODE, req, "getattr_name");
1913                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NAME_NET, 0);
1914
1915                 /* If this request gets a reconstructed reply, we won't be
1916                  * acquiring any new locks in mds_getattr_name, so we don't
1917                  * want to cancel.
1918                  */
1919                 lockh.cookie = 0;
1920                 rc = mds_getattr_name(req, MDS_REQ_REC_OFF, &lockh,
1921                                       MDS_INODELOCK_UPDATE);
1922                 /* this non-intent call (from an ioctl) is special */
1923                 req->rq_status = rc;
1924                 if (rc == 0 && lockh.cookie)
1925                         ldlm_lock_decref(&lockh, LCK_PR);
1926                 break;
1927         }
1928         case MDS_STATFS:
1929                 DEBUG_REQ(D_INODE, req, "statfs");
1930                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1931                 rc = mds_statfs(req);
1932                 break;
1933
1934         case MDS_READPAGE:
1935                 DEBUG_REQ(D_INODE, req, "readpage");
1936                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1937                 rc = mds_readpage(req, MDS_REQ_REC_OFF);
1938
1939                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
1940                         if (req->rq_reply_state) {
1941                                 lustre_free_reply_state (req->rq_reply_state);
1942                                 req->rq_reply_state = NULL;
1943                         }
1944                         RETURN(0);
1945                 }
1946
1947                 break;
1948
1949         case MDS_REINT: {
1950                 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
1951                                              sizeof (*opcp));
1952                 __u32  opc;
1953                 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
1954                                mds->mds_max_cookiesize};
1955                 int bufcount;
1956
1957                 /* NB only peek inside req now; mds_reint() will swab it */
1958                 if (opcp == NULL) {
1959                         CERROR ("Can't inspect opcode\n");
1960                         rc = -EINVAL;
1961                         break;
1962                 }
1963                 opc = *opcp;
1964                 if (lustre_msg_swabbed (req->rq_reqmsg))
1965                         __swab32s(&opc);
1966
1967                 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
1968                           (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
1969                            reint_names[opc] == NULL) ? reint_names[opc] :
1970                                                        "unknown opcode");
1971
1972                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1973
1974                 if (opc == REINT_UNLINK || opc == REINT_RENAME)
1975                         bufcount = 3;
1976                 else if (opc == REINT_OPEN)
1977                         bufcount = 2;
1978                 else
1979                         bufcount = 1;
1980
1981                 rc = lustre_pack_reply(req, bufcount, size, NULL);
1982                 if (rc)
1983                         break;
1984
1985                 rc = mds_reint(req, MDS_REQ_REC_OFF, NULL);
1986                 fail = OBD_FAIL_MDS_REINT_NET_REP;
1987                 break;
1988         }
1989
1990         case MDS_CLOSE:
1991                 DEBUG_REQ(D_INODE, req, "close");
1992                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1993                 rc = mds_close(req, MDS_REQ_REC_OFF);
1994                 break;
1995
1996         case MDS_DONE_WRITING:
1997                 DEBUG_REQ(D_INODE, req, "done_writing");
1998                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
1999                 rc = mds_done_writing(req, MDS_REQ_REC_OFF);
2000                 break;
2001
2002         case MDS_PIN:
2003                 DEBUG_REQ(D_INODE, req, "pin");
2004                 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
2005                 rc = mds_pin(req, MDS_REQ_REC_OFF);
2006                 break;
2007
2008         case MDS_SYNC:
2009                 DEBUG_REQ(D_INODE, req, "sync");
2010                 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
2011                 rc = mds_sync(req, MDS_REQ_REC_OFF);
2012                 break;
2013
2014         case OBD_PING:
2015                 DEBUG_REQ(D_INODE, req, "ping");
2016                 rc = target_handle_ping(req);
2017                 break;
2018
2019         case OBD_LOG_CANCEL:
2020                 CDEBUG(D_INODE, "log cancel\n");
2021                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
2022                 rc = -ENOTSUPP; /* la la la */
2023                 break;
2024
2025         case LDLM_ENQUEUE:
2026                 DEBUG_REQ(D_INODE, req, "enqueue");
2027                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
2028                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
2029                                          ldlm_server_blocking_ast, NULL);
2030                 break;
2031         case LDLM_CONVERT:
2032                 DEBUG_REQ(D_INODE, req, "convert");
2033                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
2034                 rc = ldlm_handle_convert(req);
2035                 break;
2036         case LDLM_BL_CALLBACK:
2037         case LDLM_CP_CALLBACK:
2038                 DEBUG_REQ(D_INODE, req, "callback");
2039                 CERROR("callbacks should not happen on MDS\n");
2040                 LBUG();
2041                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
2042                 break;
2043         case LLOG_ORIGIN_HANDLE_OPEN:
2044                 DEBUG_REQ(D_INODE, req, "llog_init");
2045                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2046                 rc = llog_origin_handle_open(req);
2047                 break;
2048         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2049                 DEBUG_REQ(D_INODE, req, "llog next block");
2050                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2051                 rc = llog_origin_handle_next_block(req);
2052                 break;
2053         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2054                 DEBUG_REQ(D_INODE, req, "llog prev block");
2055                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2056                 rc = llog_origin_handle_prev_block(req);
2057                 break;
2058         case LLOG_ORIGIN_HANDLE_READ_HEADER:
2059                 DEBUG_REQ(D_INODE, req, "llog read header");
2060                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2061                 rc = llog_origin_handle_read_header(req);
2062                 break;
2063         case LLOG_ORIGIN_HANDLE_CLOSE:
2064                 DEBUG_REQ(D_INODE, req, "llog close");
2065                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2066                 rc = llog_origin_handle_close(req);
2067                 break;
2068         case OST_CREATE:
2069                 DEBUG_REQ(D_INODE, req, "ost_create");
2070                 rc = mdt_obj_create(req);
2071                 break;
2072         case OST_GET_INFO:
2073                 DEBUG_REQ(D_INODE, req, "get_info");
2074                 rc = mdt_get_info(req);
2075                 break;
2076         case OST_SET_INFO:
2077                 DEBUG_REQ(D_INODE, req, "set_info");
2078                 rc = mdt_set_info(req);
2079                 break;
2080         case OST_WRITE:
2081                 CDEBUG(D_INODE, "write\n");
2082                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
2083                 rc = ost_brw_write(req, NULL);
2084                 LASSERT(current->journal_info == NULL);
2085                 /* mdt_brw sends its own replies */
2086                 RETURN(rc);
2087                 break;
2088         case LLOG_CATINFO:
2089                 DEBUG_REQ(D_INODE, req, "llog catinfo");
2090                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
2091                 rc = llog_catinfo(req);
2092                 break;
2093         default:
2094                 req->rq_status = -ENOTSUPP;
2095                 rc = ptlrpc_error(req);
2096                 RETURN(rc);
2097         }
2098
2099         LASSERT(current->journal_info == NULL);
2100
2101         EXIT;
2102
2103         /* If we're DISCONNECTing, the mds_export_data is already freed */
2104         if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
2105                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
2106                 struct obd_device *obd = list_entry(mds, struct obd_device,
2107                                                     u.mds);
2108                 req->rq_repmsg->last_xid =
2109                         le64_to_cpu(med->med_mcd->mcd_last_xid);
2110
2111                 if (!obd->obd_no_transno) {
2112                         req->rq_repmsg->last_committed =
2113                                 obd->obd_last_committed;
2114                 } else {
2115                         DEBUG_REQ(D_IOCTL, req,
2116                                   "not sending last_committed update");
2117                 }
2118                 CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
2119                        ", xid "LPU64"\n",
2120                        mds->mds_last_transno, obd->obd_last_committed,
2121                        req->rq_xid);
2122         }
2123  out:
2124
2125         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
2126                 if (obd && obd->obd_recovering) {
2127                         DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
2128                         return target_queue_final_reply(req, rc);
2129                 }
2130                 /* Lost a race with recovery; let the error path DTRT. */
2131                 rc = req->rq_status = -ENOTCONN;
2132         }
2133
2134         target_send_reply(req, rc, fail);
2135         return 0;
2136 }
2137
2138 /* Update the server data on disk.  This stores the new mount_count and
2139  * also the last_rcvd value to disk.  If we don't have a clean shutdown,
2140  * then the server last_rcvd value may be less than that of the clients.
2141  * This will alert us that we may need to do client recovery.
2142  *
2143  * Also assumes for mds_last_transno that we are not modifying it (no locking).
2144  */
2145 int mds_update_server_data(struct obd_device *obd, int force_sync)
2146 {
2147         struct mds_obd *mds = &obd->u.mds;
2148         struct mds_server_data *msd = mds->mds_server_data;
2149         struct file *filp = mds->mds_rcvd_filp;
2150         struct lvfs_run_ctxt saved;
2151         loff_t off = 0;
2152         int rc;
2153         ENTRY;
2154
2155         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2156         msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
2157
2158         CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
2159                mds->mds_mount_count, mds->mds_last_transno);
2160         rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off,force_sync);
2161         if (rc)
2162                 CERROR("error writing MDS server data: rc = %d\n", rc);
2163         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2164
2165         RETURN(rc);
2166 }
2167
2168 /* mount the file system (secretly) */
2169 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
2170 {
2171         struct lustre_cfg* lcfg = buf;
2172         struct mds_obd *mds = &obd->u.mds;
2173         char *options = NULL;
2174         struct vfsmount *mnt;
2175         unsigned long page;
2176         int rc = 0;
2177         ENTRY;
2178
2179         dev_clear_rdonly(2);
2180
2181         if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
2182                 RETURN(rc = -EINVAL);
2183
2184         obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
2185         if (IS_ERR(obd->obd_fsops))
2186                 RETURN(rc = PTR_ERR(obd->obd_fsops));
2187
2188         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
2189
2190         page = __get_free_page(GFP_KERNEL);
2191         if (!page)
2192                 RETURN(-ENOMEM);
2193
2194         options = (char *)page;
2195         memset(options, 0, PAGE_SIZE);
2196
2197         /* here we use "iopen_nopriv" hardcoded, because it affects MDS utility
2198          * and the rest of options are passed by mount options. Probably this
2199          * should be moved to somewhere else like startup scripts or lconf. */
2200         sprintf(options, "iopen_nopriv");
2201
2202         if (lcfg->lcfg_inllen4 > 0 && lcfg->lcfg_inlbuf4)
2203                 sprintf(options + strlen(options), ",%s",
2204                         lcfg->lcfg_inlbuf4);
2205
2206         /* we have to know mdsnum before touching underlying fs -bzzz */
2207         sema_init(&mds->mds_lmv_sem, 1);
2208         mds->mds_lmv_connected = 0;
2209         if (lcfg->lcfg_inllen5 > 0 && lcfg->lcfg_inlbuf5 && 
2210             strcmp(lcfg->lcfg_inlbuf5, "dumb")) {
2211                 class_uuid_t uuid;
2212
2213                 CDEBUG(D_OTHER, "MDS: %s is master for %s\n",
2214                        obd->obd_name, lcfg->lcfg_inlbuf5);
2215
2216                 generate_random_uuid(uuid);
2217                 class_uuid_unparse(uuid, &mds->mds_lmv_uuid);
2218
2219                 OBD_ALLOC(mds->mds_lmv_name, lcfg->lcfg_inllen5);
2220                 if (mds->mds_lmv_name == NULL) 
2221                         RETURN(rc = -ENOMEM);
2222
2223                 memcpy(mds->mds_lmv_name, lcfg->lcfg_inlbuf5,
2224                        lcfg->lcfg_inllen5);
2225                 
2226                 rc = mds_lmv_connect(obd, mds->mds_lmv_name);
2227                 if (rc) {
2228                         OBD_FREE(mds->mds_lmv_name, lcfg->lcfg_inllen5);
2229                         GOTO(err_ops, rc);
2230                 }
2231         }
2232         
2233         /* FIXME-WANGDI: this should be reworked when we will use lmv along 
2234          * with cobd, because correct mdsnum is set in mds_lmv_connect(). */
2235         if (lcfg->lcfg_inllen6 > 0 && lcfg->lcfg_inlbuf6 && !mds->mds_lmv_obd &&
2236             strcmp(lcfg->lcfg_inlbuf6, "dumb")) {
2237                 if (!memcmp(lcfg->lcfg_inlbuf6, "master", strlen("master")) &&
2238                     mds->mds_num == 0) {
2239                         mds->mds_num = REAL_MDS_NUMBER;
2240                 } else if (!memcmp(lcfg->lcfg_inlbuf6, "cache", strlen("cache")) &&
2241                            mds->mds_num == 0) {
2242                         mds->mds_num = CACHE_MDS_NUMBER;
2243                 }     
2244         }
2245
2246         mnt = do_kern_mount(lcfg->lcfg_inlbuf2, 0, 
2247                             lcfg->lcfg_inlbuf1, options);
2248
2249         free_page(page);
2250
2251         if (IS_ERR(mnt)) {
2252                 rc = PTR_ERR(mnt);
2253                 CERROR("do_kern_mount failed: rc = %d\n", rc);
2254                 GOTO(err_ops, rc);
2255         }
2256
2257         CDEBUG(D_SUPER, "%s: mnt = %p\n", lcfg->lcfg_inlbuf1, mnt);
2258
2259         sema_init(&mds->mds_orphan_recovery_sem, 1);
2260         sema_init(&mds->mds_epoch_sem, 1);
2261         spin_lock_init(&mds->mds_transno_lock);
2262         mds->mds_max_cookiesize = sizeof(struct llog_cookie);
2263         atomic_set(&mds->mds_real_clients, 0);
2264
2265         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
2266                                                 LDLM_NAMESPACE_SERVER);
2267         if (obd->obd_namespace == NULL) {
2268                 mds_cleanup(obd, 0);
2269                 GOTO(err_put, rc = -ENOMEM);
2270         }
2271         ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
2272
2273         rc = mds_fs_setup(obd, mnt);
2274         if (rc) {
2275                 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
2276                 GOTO(err_ns, rc);
2277         }
2278
2279         rc = llog_start_commit_thread();
2280         if (rc < 0)
2281                 GOTO(err_fs, rc);
2282
2283         /* this check for @dumb string is needed to handle mounting MDS with
2284          * smfs. Read lconf:MDSDEV.write_conf() for more details. */
2285         if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3 &&
2286             strcmp(lcfg->lcfg_inlbuf3, "dumb")) {
2287                 class_uuid_t uuid;
2288
2289                 generate_random_uuid(uuid);
2290                 class_uuid_unparse(uuid, &mds->mds_lov_uuid);
2291
2292                 OBD_ALLOC(mds->mds_profile, lcfg->lcfg_inllen3);
2293                 if (mds->mds_profile == NULL)
2294                         GOTO(err_fs, rc = -ENOMEM);
2295
2296                 memcpy(mds->mds_profile, lcfg->lcfg_inlbuf3,
2297                        lcfg->lcfg_inllen3);
2298         }
2299
2300         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2301                            "mds_ldlm_client", &obd->obd_ldlm_client);
2302         obd->obd_replayable = 1;
2303
2304         mds->mds_counters = lprocfs_alloc_mds_counters();
2305
2306         rc = mds_postsetup(obd);
2307         if (rc)
2308                 GOTO(err_fs, rc);
2309
2310         RETURN(0);
2311
2312 err_fs:
2313         /* No extra cleanup needed for llog_init_commit_thread() */
2314         mds_fs_cleanup(obd, 0);
2315 err_ns:
2316         ldlm_namespace_free(obd->obd_namespace, 0);
2317         obd->obd_namespace = NULL;
2318 err_put:
2319         unlock_kernel();
2320         mntput(mds->mds_vfsmnt);
2321         mds->mds_sb = 0;
2322         lock_kernel();
2323 err_ops:
2324         fsfilt_put_ops(obd->obd_fsops);
2325         return rc;
2326 }
2327
2328 static int mds_postsetup(struct obd_device *obd)
2329 {
2330         struct mds_obd *mds = &obd->u.mds;
2331         int rc = 0;
2332         ENTRY;
2333
2334         rc = obd_llog_setup(obd, &obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT, 
2335                             obd, 0, NULL, &llog_lvfs_ops);
2336         if (rc)
2337                 RETURN(rc);
2338
2339         if (mds->mds_profile) {
2340                 struct llog_ctxt *lgctxt;
2341                 struct lvfs_run_ctxt saved;
2342                 struct lustre_profile *lprof;
2343                 struct config_llog_instance cfg;
2344
2345                 cfg.cfg_instance = NULL;
2346                 cfg.cfg_uuid = mds->mds_lov_uuid;
2347                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2348
2349                 lgctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
2350                 if (!lgctxt) {
2351                         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2352                         GOTO(err_llog, rc = -EINVAL);
2353                 }
2354                 
2355                 rc = class_config_process_llog(lgctxt, mds->mds_profile, &cfg);
2356                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2357
2358                 if (rc)
2359                         GOTO(err_llog, rc);
2360
2361                 lprof = class_get_profile(mds->mds_profile);
2362                 if (lprof == NULL) {
2363                         CERROR("No profile found: %s\n", mds->mds_profile);
2364                         GOTO(err_cleanup, rc = -ENOENT);
2365                 }
2366                 rc = mds_lov_connect(obd, lprof->lp_osc);
2367                 if (rc)
2368                         GOTO(err_cleanup, rc);
2369
2370                 rc = mds_lmv_postsetup(obd);
2371                 if (rc)
2372                         GOTO(err_cleanup, rc);
2373         }
2374
2375         RETURN(rc);
2376
2377 err_cleanup:
2378         mds_lov_clean(obd);
2379 err_llog:
2380         obd_llog_cleanup(llog_get_context(&obd->obd_llogs,
2381                                           LLOG_CONFIG_ORIG_CTXT));
2382         RETURN(rc);
2383 }
2384
2385 int mds_postrecov(struct obd_device *obd)
2386 {
2387         struct mds_obd *mds = &obd->u.mds;
2388         struct llog_ctxt *ctxt;
2389         int rc, item = 0;
2390         ENTRY;
2391
2392         LASSERT(!obd->obd_recovering);
2393         ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
2394         LASSERT(ctxt != NULL);
2395
2396         /* set nextid first, so we are sure it happens */
2397         rc = mds_lov_set_nextid(obd);
2398         if (rc) {
2399                 CERROR("%s: mds_lov_set_nextid failed\n", obd->obd_name);
2400                 GOTO(out, rc);
2401         }
2402
2403         /* clean PENDING dir */
2404         rc = mds_cleanup_orphans(obd);
2405         if (rc < 0)
2406                 GOTO(out, rc);
2407         item = rc;
2408
2409         rc = llog_connect(ctxt, obd->u.mds.mds_lov_desc.ld_tgt_count,
2410                           NULL, NULL, NULL);
2411         if (rc) {
2412                 CERROR("%s: failed at llog_origin_connect: %d\n", 
2413                        obd->obd_name, rc);
2414                 GOTO(out, rc);
2415         }
2416
2417         /* remove the orphaned precreated objects */
2418         rc = mds_lov_clearorphans(mds, NULL /* all OSTs */);
2419         if (rc)
2420                 GOTO(err_llog, rc);
2421
2422 out:
2423         RETURN(rc < 0 ? rc : item);
2424
2425 err_llog:
2426         /* cleanup all llogging subsystems */
2427         rc = obd_llog_finish(obd, &obd->obd_llogs,
2428                              mds->mds_lov_desc.ld_tgt_count);
2429         if (rc)
2430                 CERROR("%s: failed to cleanup llogging subsystems\n",
2431                         obd->obd_name);
2432         goto out;
2433 }
2434
2435 int mds_lov_clean(struct obd_device *obd)
2436 {
2437         struct mds_obd *mds = &obd->u.mds;
2438
2439         if (mds->mds_profile) {
2440                 char * cln_prof;
2441                 struct config_llog_instance cfg;
2442                 struct lvfs_run_ctxt saved;
2443                 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
2444
2445                 OBD_ALLOC(cln_prof, len);
2446                 sprintf(cln_prof, "%s-clean", mds->mds_profile);
2447
2448                 cfg.cfg_instance = NULL;
2449                 cfg.cfg_uuid = mds->mds_lov_uuid;
2450
2451                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2452                 class_config_process_llog(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT),
2453                                           cln_prof, &cfg);
2454                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2455
2456                 OBD_FREE(cln_prof, len);
2457                 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
2458                 mds->mds_profile = NULL;
2459         }
2460         RETURN(0);
2461 }
2462
2463 int mds_lmv_clean(struct obd_device *obd)
2464 {
2465         struct mds_obd *mds = &obd->u.mds;
2466
2467         if (mds->mds_lmv_name) {
2468                 OBD_FREE(mds->mds_lmv_name, strlen(mds->mds_lmv_name) + 1);
2469                 mds->mds_lmv_name = NULL;
2470         }
2471         RETURN(0);
2472 }
2473
2474 static int mds_precleanup(struct obd_device *obd, int flags)
2475 {
2476         int rc = 0;
2477         ENTRY;
2478
2479         mds_lmv_clean(obd);
2480         mds_lov_disconnect(obd, flags);
2481         mds_lov_clean(obd);
2482         obd_llog_cleanup(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT));
2483         RETURN(rc);
2484 }
2485
2486 static int mds_cleanup(struct obd_device *obd, int flags)
2487 {
2488         struct mds_obd *mds = &obd->u.mds;
2489         ENTRY;
2490
2491         if (mds->mds_sb == NULL)
2492                 RETURN(0);
2493
2494         mds_update_server_data(obd, 1);
2495         if (mds->mds_lov_objids != NULL) {
2496                 OBD_FREE(mds->mds_lov_objids,
2497                          mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id));
2498         }
2499         mds_fs_cleanup(obd, flags);
2500
2501         unlock_kernel();
2502
2503         /* 2 seems normal on mds, (may_umount() also expects 2
2504           fwiw), but we only see 1 at this point in obdfilter. */
2505         if (atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count) > 2)
2506                 CERROR("%s: mount busy, mnt_count %d != 2\n", obd->obd_name,
2507                        atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count));
2508
2509         mntput(mds->mds_vfsmnt);
2510
2511         mds->mds_sb = 0;
2512
2513         ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
2514
2515         if (mds->mds_counters) {
2516                 lprocfs_free_mds_counters(mds->mds_counters);
2517         }
2518
2519         spin_lock_bh(&obd->obd_processing_task_lock);
2520         if (obd->obd_recovering) {
2521                 target_cancel_recovery_timer(obd);
2522                 obd->obd_recovering = 0;
2523         }
2524         spin_unlock_bh(&obd->obd_processing_task_lock);
2525
2526         lock_kernel();
2527         dev_clear_rdonly(2);
2528         fsfilt_put_ops(obd->obd_fsops);
2529
2530         RETURN(0);
2531 }
2532
2533 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
2534                                         int offset,
2535                                         struct ldlm_lock *new_lock,
2536                                         struct lustre_handle *lockh)
2537 {
2538         struct obd_export *exp = req->rq_export;
2539         struct obd_device *obd = exp->exp_obd;
2540         struct ldlm_request *dlmreq =
2541                 lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*dlmreq));
2542         struct lustre_handle remote_hdl = dlmreq->lock_handle1;
2543         struct list_head *iter;
2544
2545         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2546                 return;
2547
2548         l_lock(&obd->obd_namespace->ns_lock);
2549         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2550                 struct ldlm_lock *lock;
2551                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2552                 if (lock == new_lock)
2553                         continue;
2554                 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2555                         lockh->cookie = lock->l_handle.h_cookie;
2556                         DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
2557                                   lockh->cookie);
2558                         l_unlock(&obd->obd_namespace->ns_lock);
2559                         return;
2560                 }
2561         }
2562         l_unlock(&obd->obd_namespace->ns_lock);
2563
2564         /* If the xid matches, then we know this is a resent request,
2565          * and allow it. (It's probably an OPEN, for which we don't
2566          * send a lock */
2567         if (req->rq_xid == exp->exp_mds_data.med_mcd->mcd_last_xid)
2568                 return;
2569
2570         /* This remote handle isn't enqueued, so we never received or
2571          * processed this request.  Clear MSG_RESENT, because it can
2572          * be handled like any normal request now. */
2573
2574         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2575
2576         DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
2577                   remote_hdl.cookie);
2578 }
2579
2580 int intent_disposition(struct ldlm_reply *rep, int flag)
2581 {
2582         if (!rep)
2583                 return 0;
2584         return (rep->lock_policy_res1 & flag);
2585 }
2586
2587 void intent_set_disposition(struct ldlm_reply *rep, int flag)
2588 {
2589         if (!rep)
2590                 return;
2591         rep->lock_policy_res1 |= flag;
2592 }
2593
2594 static int mds_intent_policy(struct ldlm_namespace *ns,
2595                              struct ldlm_lock **lockp, void *req_cookie,
2596                              ldlm_mode_t mode, int flags, void *data)
2597 {
2598         struct ptlrpc_request *req = req_cookie;
2599         struct ldlm_lock *lock = *lockp;
2600         struct ldlm_intent *it;
2601         struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
2602         struct ldlm_reply *rep;
2603         struct lustre_handle lockh[2] = {{0}, {0}};
2604         struct ldlm_lock *new_lock;
2605         int getattr_part = MDS_INODELOCK_UPDATE;
2606         int rc, repsize[4] = { sizeof(struct ldlm_reply),
2607                                sizeof(struct mds_body),
2608                                mds->mds_max_mdsize,
2609                                mds->mds_max_cookiesize };
2610         int offset = MDS_REQ_INTENT_REC_OFF; 
2611         ENTRY;
2612
2613         LASSERT(req != NULL);
2614
2615         if (req->rq_reqmsg->bufcount <= MDS_REQ_INTENT_IT_OFF) {
2616                 /* No intent was provided */
2617                 int size = sizeof(struct ldlm_reply);
2618                 rc = lustre_pack_reply(req, 1, &size, NULL);
2619                 LASSERT(rc == 0);
2620                 RETURN(0);
2621         }
2622
2623         it = lustre_swab_reqbuf(req, MDS_REQ_INTENT_IT_OFF, sizeof(*it),
2624                                 lustre_swab_ldlm_intent);
2625         if (it == NULL) {
2626                 CERROR("Intent missing\n");
2627                 RETURN(req->rq_status = -EFAULT);
2628         }
2629
2630         LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
2631
2632         rc = lustre_pack_reply(req, 3, repsize, NULL);
2633         if (rc)
2634                 RETURN(req->rq_status = rc);
2635
2636         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
2637         intent_set_disposition(rep, DISP_IT_EXECD);
2638
2639         fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF,
2640                                     lock, lockh);
2641
2642         /* execute policy */
2643         switch ((long)it->opc) {
2644         case IT_OPEN:
2645         case IT_CREAT|IT_OPEN:
2646                 /* XXX swab here to assert that an mds_open reint
2647                  * packet is following */
2648                 rep->lock_policy_res2 = mds_reint(req, offset, lockh);
2649 #if 0
2650                 /* We abort the lock if the lookup was negative and
2651                  * we did not make it to the OPEN portion */
2652                 if (!intent_disposition(rep, DISP_LOOKUP_EXECD))
2653                         RETURN(ELDLM_LOCK_ABORTED);
2654                 if (intent_disposition(rep, DISP_LOOKUP_NEG) &&
2655                     !intent_disposition(rep, DISP_OPEN_OPEN))
2656 #endif
2657                 /* IT_OPEN may return lock on cross-node dentry
2658                  * that we want to hold during attr retrival -bzzz */
2659                 if (rc != 0 || lockh[0].cookie == 0)
2660                         RETURN(ELDLM_LOCK_ABORTED);
2661                 break;
2662         case IT_LOOKUP:
2663                 getattr_part = MDS_INODELOCK_LOOKUP;
2664         case IT_CHDIR:
2665         case IT_GETATTR:
2666                 getattr_part |= MDS_INODELOCK_LOOKUP;
2667         case IT_READDIR:
2668                 rep->lock_policy_res2 = mds_getattr_name(req, offset, lockh,
2669                                                          getattr_part);
2670                 /* FIXME: LDLM can set req->rq_status. MDS sets
2671                    policy_res{1,2} with disposition and status.
2672                    - replay: returns 0 & req->status is old status
2673                    - otherwise: returns req->status */
2674                 if (intent_disposition(rep, DISP_LOOKUP_NEG))
2675                         rep->lock_policy_res2 = 0;
2676                 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
2677                     rep->lock_policy_res2)
2678                         RETURN(ELDLM_LOCK_ABORTED);
2679                 if (req->rq_status != 0) {
2680                         LBUG();
2681                         rep->lock_policy_res2 = req->rq_status;
2682                         RETURN(ELDLM_LOCK_ABORTED);
2683                 }
2684                 break;
2685         case IT_UNLINK:
2686                 rc = mds_lock_and_check_slave(offset, req, lockh);
2687                 if ((rep->lock_policy_res2 = rc)) {
2688                         if (rc == ENOLCK)
2689                                 rep->lock_policy_res2 = 0;
2690                         RETURN(ELDLM_LOCK_ABORTED);
2691                 }
2692                 break;
2693         default:
2694                 CERROR("Unhandled intent "LPD64"\n", it->opc);
2695                 LBUG();
2696         }
2697
2698         /* By this point, whatever function we called above must have either
2699          * filled in 'lockh', been an intent replay, or returned an error.  We
2700          * want to allow replayed RPCs to not get a lock, since we would just
2701          * drop it below anyways because lock replay is done separately by the
2702          * client afterwards.  For regular RPCs we want to give the new lock to
2703          * the client instead of whatever lock it was about to get. */
2704         new_lock = ldlm_handle2lock(&lockh[0]);
2705         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
2706                 RETURN(0);
2707
2708         LASSERT(new_lock != NULL);
2709
2710         /* If we've already given this lock to a client once, then we should
2711          * have no readers or writers.  Otherwise, we should have one reader
2712          * _or_ writer ref (which will be zeroed below) before returning the
2713          * lock to a client. */
2714         if (new_lock->l_export == req->rq_export) {
2715                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2716         } else {
2717                 LASSERT(new_lock->l_export == NULL);
2718                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2719         }
2720
2721         *lockp = new_lock;
2722
2723         if (new_lock->l_export == req->rq_export) {
2724                 /* Already gave this to the client, which means that we
2725                  * reconstructed a reply. */
2726                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2727                         MSG_RESENT);
2728                 RETURN(ELDLM_LOCK_REPLACED);
2729         }
2730
2731         /* Fixup the lock to be given to the client */
2732         l_lock(&new_lock->l_resource->lr_namespace->ns_lock);
2733         new_lock->l_readers = 0;
2734         new_lock->l_writers = 0;
2735
2736         new_lock->l_export = class_export_get(req->rq_export);
2737         list_add(&new_lock->l_export_chain,
2738                  &new_lock->l_export->exp_ldlm_data.led_held_locks);
2739
2740         new_lock->l_blocking_ast = lock->l_blocking_ast;
2741         new_lock->l_completion_ast = lock->l_completion_ast;
2742
2743         memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
2744                sizeof(lock->l_remote_handle));
2745
2746         new_lock->l_flags &= ~LDLM_FL_LOCAL;
2747
2748         LDLM_LOCK_PUT(new_lock);
2749         l_unlock(&new_lock->l_resource->lr_namespace->ns_lock);
2750
2751         RETURN(ELDLM_LOCK_REPLACED);
2752 }
2753
2754 int mds_attach(struct obd_device *dev, obd_count len, void *data)
2755 {
2756         struct lprocfs_static_vars lvars;
2757
2758         lprocfs_init_multi_vars(0, &lvars);
2759         return lprocfs_obd_attach(dev, lvars.obd_vars);
2760 }
2761
2762 int mds_detach(struct obd_device *dev)
2763 {
2764         return lprocfs_obd_detach(dev);
2765 }
2766
2767 int mdt_attach(struct obd_device *dev, obd_count len, void *data)
2768 {
2769         struct lprocfs_static_vars lvars;
2770
2771         lprocfs_init_multi_vars(1, &lvars);
2772         return lprocfs_obd_attach(dev, lvars.obd_vars);
2773 }
2774
2775 int mdt_detach(struct obd_device *dev)
2776 {
2777         return lprocfs_obd_detach(dev);
2778 }
2779
2780 static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
2781 {
2782         struct mds_obd *mds = &obd->u.mds;
2783         int rc = 0;
2784         ENTRY;
2785
2786         mds->mds_service =
2787                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2788                                 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
2789                                 mds_handle, "mds", obd->obd_proc_entry);
2790
2791         if (!mds->mds_service) {
2792                 CERROR("failed to start service\n");
2793                 RETURN(-ENOMEM);
2794         }
2795
2796         rc = ptlrpc_start_n_threads(obd, mds->mds_service, MDT_NUM_THREADS,
2797                                     "ll_mdt");
2798         if (rc)
2799                 GOTO(err_thread, rc);
2800
2801         mds->mds_setattr_service =
2802                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2803                                 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
2804                                 mds_handle, "mds_setattr",
2805                                 obd->obd_proc_entry);
2806         if (!mds->mds_setattr_service) {
2807                 CERROR("failed to start getattr service\n");
2808                 GOTO(err_thread, rc = -ENOMEM);
2809         }
2810
2811         rc = ptlrpc_start_n_threads(obd, mds->mds_setattr_service,
2812                                     MDT_NUM_THREADS, "ll_mdt_attr");
2813         if (rc)
2814                 GOTO(err_thread2, rc);
2815
2816         mds->mds_readpage_service =
2817                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2818                                 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
2819                                 mds_handle, "mds_readpage",
2820                                 obd->obd_proc_entry);
2821         if (!mds->mds_readpage_service) {
2822                 CERROR("failed to start readpage service\n");
2823                 GOTO(err_thread2, rc = -ENOMEM);
2824         }
2825
2826         rc = ptlrpc_start_n_threads(obd, mds->mds_readpage_service,
2827                                     MDT_NUM_THREADS, "ll_mdt_rdpg");
2828
2829         if (rc)
2830                 GOTO(err_thread3, rc);
2831
2832         RETURN(0);
2833
2834 err_thread3:
2835         ptlrpc_unregister_service(mds->mds_readpage_service);
2836 err_thread2:
2837         ptlrpc_unregister_service(mds->mds_setattr_service);
2838 err_thread:
2839         ptlrpc_unregister_service(mds->mds_service);
2840         return rc;
2841 }
2842
2843 static int mdt_cleanup(struct obd_device *obd, int flags)
2844 {
2845         struct mds_obd *mds = &obd->u.mds;
2846         ENTRY;
2847
2848         ptlrpc_stop_all_threads(mds->mds_readpage_service);
2849         ptlrpc_unregister_service(mds->mds_readpage_service);
2850
2851         ptlrpc_stop_all_threads(mds->mds_setattr_service);
2852         ptlrpc_unregister_service(mds->mds_setattr_service);
2853
2854         ptlrpc_stop_all_threads(mds->mds_service);
2855         ptlrpc_unregister_service(mds->mds_service);
2856
2857         RETURN(0);
2858 }
2859
2860 static struct dentry *mds_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
2861                                           void *data)
2862 {
2863         struct obd_device *obd = data;
2864         struct ll_fid fid;
2865         fid.id = id;
2866         fid.generation = gen;
2867         return mds_fid2dentry(&obd->u.mds, &fid, NULL);
2868 }
2869
2870 static int mds_get_info(struct obd_export *exp, __u32 keylen,
2871                         void *key, __u32 *vallen, void *val)
2872 {
2873         struct obd_device *obd;
2874         struct mds_obd *mds;
2875         ENTRY;
2876
2877         obd = class_exp2obd(exp);
2878         if (obd == NULL) {
2879                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2880                        exp->exp_handle.h_cookie);
2881                 RETURN(-EINVAL);
2882         }
2883
2884         if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
2885                 /*Get log_context handle*/
2886                 unsigned long *llh_handle = val;
2887                 *vallen = sizeof(unsigned long);
2888                 *llh_handle = (unsigned long)obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT];
2889                 RETURN(0);
2890         }
2891         if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
2892                 /*Get log_context handle*/
2893                 unsigned long *sb = val;
2894                 *vallen = sizeof(unsigned long);
2895                 *sb = (unsigned long)obd->u.mds.mds_sb;
2896                 RETURN(0);
2897         }
2898
2899         mds = &obd->u.mds;
2900         if (keylen >= strlen("mdsize") && memcmp(key, "mdsize", keylen) == 0) {
2901                 __u32 *mdsize = val;
2902                 *vallen = sizeof(*mdsize);
2903                 *mdsize = mds->mds_max_mdsize;
2904                 RETURN(0);
2905         }
2906
2907         CDEBUG(D_IOCTL, "invalid key\n");
2908         RETURN(-EINVAL);
2909
2910 }
2911 struct lvfs_callback_ops mds_lvfs_ops = {
2912         l_fid2dentry:     mds_lvfs_fid2dentry,
2913 };
2914
2915 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
2916                 int objcount, struct obd_ioobj *obj,
2917                 int niocount, struct niobuf_remote *nb,
2918                 struct niobuf_local *res,
2919                 struct obd_trans_info *oti);
2920 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
2921                  int objcount, struct obd_ioobj *obj, int niocount,
2922                  struct niobuf_local *res, struct obd_trans_info *oti,
2923                  int rc);
2924
2925 /* use obd ops to offer management infrastructure */
2926 static struct obd_ops mds_obd_ops = {
2927         .o_owner           = THIS_MODULE,
2928         .o_attach          = mds_attach,
2929         .o_detach          = mds_detach,
2930         .o_connect         = mds_connect,
2931         .o_init_export     = mds_init_export,
2932         .o_destroy_export  = mds_destroy_export,
2933         .o_disconnect      = mds_disconnect,
2934         .o_setup           = mds_setup,
2935         .o_precleanup      = mds_precleanup,
2936         .o_cleanup         = mds_cleanup,
2937         .o_postrecov       = mds_postrecov,
2938         .o_statfs          = mds_obd_statfs,
2939         .o_iocontrol       = mds_iocontrol,
2940         .o_create          = mds_obd_create,
2941         .o_destroy         = mds_obd_destroy,
2942         .o_llog_init       = mds_llog_init,
2943         .o_llog_finish     = mds_llog_finish,
2944         .o_notify          = mds_notify,
2945         .o_get_info        = mds_get_info,
2946         .o_set_info        = mds_set_info,
2947         .o_preprw          = mds_preprw, 
2948         .o_commitrw        = mds_commitrw,
2949 };
2950
2951 static struct obd_ops mdt_obd_ops = {
2952         .o_owner           = THIS_MODULE,
2953         .o_attach          = mdt_attach,
2954         .o_detach          = mdt_detach,
2955         .o_setup           = mdt_setup,
2956         .o_cleanup         = mdt_cleanup,
2957         .o_attach          = mdt_attach,
2958         .o_detach          = mdt_detach,
2959 };
2960
2961 static int __init mds_init(void)
2962 {
2963         struct lprocfs_static_vars lvars;
2964
2965         mds_group_hash_init();
2966
2967         lprocfs_init_multi_vars(0, &lvars);
2968         class_register_type(&mds_obd_ops, NULL, lvars.module_vars,
2969                             LUSTRE_MDS_NAME);
2970         lprocfs_init_multi_vars(1, &lvars);
2971         class_register_type(&mdt_obd_ops, NULL, lvars.module_vars,
2972                             LUSTRE_MDT_NAME);
2973
2974         return 0;
2975 }
2976
2977 static void /*__exit*/ mds_exit(void)
2978 {
2979         mds_group_hash_cleanup();
2980
2981         class_unregister_type(LUSTRE_MDS_NAME);
2982         class_unregister_type(LUSTRE_MDT_NAME);
2983 }
2984
2985 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2986 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
2987 MODULE_LICENSE("GPL");
2988
2989 module_init(mds_init);
2990 module_exit(mds_exit);