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