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