Whamcloud - gitweb
02927107e91f0292d2d287a6463852066a3c0919
[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/namei.h>
43 #include <linux/ext3_fs.h>
44 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
45 # include <linux/smp_lock.h>
46 # include <linux/buffer_head.h>
47 # include <linux/workqueue.h>
48 # include <linux/mount.h>
49 #else
50 # include <linux/locks.h>
51 #endif
52 #include <linux/obd_lov.h>
53 #include <linux/obd_ost.h>
54 #include <linux/lustre_mds.h>
55 #include <linux/lustre_fsfilt.h>
56 #include <linux/lprocfs_status.h>
57 #include <linux/lustre_commit_confd.h>
58 #include <linux/lustre_acl.h>
59 #include <linux/lustre_sec.h>
60 #include <linux/lustre_gs.h>
61 #include "mds_internal.h"
62
63 static int mds_intent_policy(struct ldlm_namespace *ns,
64                              struct ldlm_lock **lockp, void *req_cookie,
65                              ldlm_mode_t mode, int flags, void *data);
66 static int mds_postsetup(struct obd_device *obd);
67 static int mds_cleanup(struct obd_device *obd, int flags);
68
69
70 /* Assumes caller has already pushed into the kernel filesystem context */
71 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
72                         loff_t offset, int count)
73 {
74         struct ptlrpc_bulk_desc *desc;
75         struct l_wait_info lwi;
76         struct page **pages;
77         int rc = 0, npages, i, tmpcount, tmpsize = 0;
78         ENTRY;
79
80         LASSERT((offset & (PAGE_SIZE - 1)) == 0); /* I'm dubious about this */
81
82         npages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
83         OBD_ALLOC(pages, sizeof(*pages) * npages);
84         if (!pages)
85                 GOTO(out, rc = -ENOMEM);
86
87         desc = ptlrpc_prep_bulk_exp(req, npages, BULK_PUT_SOURCE,
88                                     MDS_BULK_PORTAL);
89         if (desc == NULL)
90                 GOTO(out_free, rc = -ENOMEM);
91
92         for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
93                 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
94
95                 pages[i] = alloc_pages(GFP_KERNEL, 0);
96                 if (pages[i] == NULL)
97                         GOTO(cleanup_buf, rc = -ENOMEM);
98
99                 ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
100         }
101
102         for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
103                 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
104                 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
105                        tmpsize, offset, file->f_dentry->d_inode->i_ino,
106                        file->f_dentry->d_inode->i_size);
107
108                 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
109                                      kmap(pages[i]), tmpsize, &offset);
110                 kunmap(pages[i]);
111
112                 if (rc != tmpsize)
113                         GOTO(cleanup_buf, rc = -EIO);
114         }
115
116         LASSERT(desc->bd_nob == count);
117
118         rc = ptlrpc_start_bulk_transfer(desc);
119         if (rc)
120                 GOTO(cleanup_buf, rc);
121
122         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
123                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
124                        OBD_FAIL_MDS_SENDPAGE, rc = -EIO);
125                 GOTO(abort_bulk, rc);
126         }
127
128         lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
129         rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), &lwi);
130         LASSERT (rc == 0 || rc == -ETIMEDOUT);
131
132         if (rc == 0) {
133                 if (desc->bd_success &&
134                     desc->bd_nob_transferred == count)
135                         GOTO(cleanup_buf, rc);
136
137                 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
138         }
139
140         DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
141                   (rc == -ETIMEDOUT) ? "timeout" : "network error",
142                   desc->bd_nob_transferred, count,
143                   req->rq_export->exp_client_uuid.uuid,
144                   req->rq_export->exp_connection->c_remote_uuid.uuid);
145
146         ptlrpc_fail_export(req->rq_export);
147
148         EXIT;
149  abort_bulk:
150         ptlrpc_abort_bulk (desc);
151  cleanup_buf:
152         for (i = 0; i < npages; i++)
153                 if (pages[i])
154                         __free_pages(pages[i], 0);
155
156         ptlrpc_free_bulk(desc);
157  out_free:
158         OBD_FREE(pages, sizeof(*pages) * npages);
159  out:
160         return rc;
161 }
162
163 extern char *ldlm_lockname[];
164
165 int mds_lock_mode_for_dir(struct obd_device *obd,
166                           struct dentry *dentry, int mode)
167 {
168         int ret_mode = 0, split;
169
170         /* any dir access needs couple locks:
171          * 1) on part of dir we gonna lookup/modify in
172          * 2) on a whole dir to protect it from concurrent splitting
173          *    and to flush client's cache for readdir()
174          * so, for a given mode and dentry this routine decides what
175          * lock mode to use for lock #2:
176          * 1) if caller's gonna lookup in dir then we need to protect
177          *    dir from being splitted only - LCK_CR
178          * 2) if caller's gonna modify dir then we need to protect
179          *    dir from being splitted and to flush cache - LCK_CW
180          * 3) if caller's gonna modify dir and that dir seems ready
181          *    for splitting then we need to protect it from any
182          *    type of access (lookup/modify/split) - LCK_EX -bzzz */
183
184         split = mds_splitting_expected(obd, dentry);
185         
186         /*
187          * it is important to check here only for MDS_NO_SPLITTABLE. The reason
188          * is that MDS_NO_SPLITTABLE means dir is not splittable in principle
189          * and another thread will not split it on the quiet. But if we have
190          * MDS_NO_SPLIT_EXPECTED, this means, that dir may be splitted anytime,
191          * but not now (for current thread) and we should consider that it can
192          * happen soon and go that branch which can yield LCK_EX to protect from
193          * possible splitting.
194          */
195         if (split == MDS_NO_SPLITTABLE) {
196                 /*
197                  * this inode won't be splitted. so we need not to protect from
198                  * just flush client's cache on modification.
199                  */
200                 if (mode == LCK_PW)
201                         ret_mode = LCK_CW;
202                 else
203                         ret_mode = 0;
204         } else {
205                 if (mode == LCK_EX) {
206                         ret_mode = LCK_EX;
207                 } else if (mode == LCK_PR) {
208                         ret_mode = LCK_CR;
209                 } else if (mode == LCK_PW) {
210                         /*
211                          * caller gonna modify directory. We use concurrent
212                          * write lock here to retract client's cache for
213                          * readdir.
214                          */
215                         if (split == MDS_EXPECT_SPLIT) {
216                                 /*
217                                  * splitting possible. serialize any access the
218                                  * idea is that first one seen dir is splittable
219                                  * is given exclusive lock and split
220                                  * directory. caller passes lock mode to
221                                  * mds_try_to_split_dir() and splitting would be
222                                  * done with exclusive lock only -bzzz.
223                                  */
224                                 CDEBUG(D_OTHER, "%s: gonna split %lu/%lu\n",
225                                        obd->obd_name,
226                                        (unsigned long)dentry->d_inode->i_ino,
227                                        (unsigned long)dentry->d_inode->i_generation);
228                                 ret_mode = LCK_EX;
229                         } else {
230                                 ret_mode = LCK_CW;
231                         }
232                 }
233         }
234
235         return ret_mode;        
236 }
237
238 /* only valid locked dentries or errors should be returned */
239 struct dentry *mds_id2locked_dentry(struct obd_device *obd, struct lustre_id *id,
240                                     struct vfsmount **mnt, int lock_mode,
241                                     struct lustre_handle *lockh, int *mode,
242                                     char *name, int namelen, __u64 lockpart)
243 {
244         struct dentry *de = mds_id2dentry(obd, id, mnt), *retval = de;
245         ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
246         struct ldlm_res_id res_id = { .name = {0} };
247         int flags = LDLM_FL_ATOMIC_CB, rc;
248         ENTRY;
249
250         if (IS_ERR(de))
251                 RETURN(de);
252
253         lockh[1].cookie = 0;
254         res_id.name[0] = id_fid(id);
255         res_id.name[1] = id_group(id);
256         
257 #ifdef S_PDIROPS
258         if (name && IS_PDIROPS(de->d_inode)) {
259                 ldlm_policy_data_t cpolicy =
260                         { .l_inodebits = { MDS_INODELOCK_UPDATE } };
261                 LASSERT(mode != NULL);
262                 *mode = mds_lock_mode_for_dir(obd, de, lock_mode);
263                 if (*mode) {
264                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
265                                               res_id, LDLM_IBITS,
266                                               &cpolicy, *mode, &flags,
267                                               mds_blocking_ast,
268                                               ldlm_completion_ast, NULL, NULL,
269                                               NULL, 0, NULL, lockh + 1);
270                         if (rc != ELDLM_OK) {
271                                 l_dput(de);
272                                 RETURN(ERR_PTR(-ENOLCK));
273                         }
274                 }
275                 flags = LDLM_FL_ATOMIC_CB;
276
277                 res_id.name[2] = full_name_hash((unsigned char *)name, namelen);
278
279                 CDEBUG(D_INFO, "take lock on "DLID4":"LPX64"\n",
280                        OLID4(id), res_id.name[2]);
281         }
282 #else
283 #warning "No PDIROPS support in the kernel"
284 #endif
285         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
286                               LDLM_IBITS, &policy, lock_mode, &flags,
287                               mds_blocking_ast, ldlm_completion_ast,
288                               NULL, NULL, NULL, 0, NULL, lockh);
289         if (rc != ELDLM_OK) {
290                 l_dput(de);
291                 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
292 #ifdef S_PDIROPS
293                 if (lockh[1].cookie)
294                         ldlm_lock_decref(lockh + 1, *mode);
295 #endif
296         } else if (de->d_inode && de->d_inode->i_nlink == 0) {
297                 /* as sometimes we lookup inode by ino/generation through
298                    iopen mechanism, it's possible to find already unlinked
299                    inode with nlink == 0. let's interpretate the case as
300                    ENOENT -bzzz */
301                 CWARN("found already unlinked inode %lu/%u\n",
302                       de->d_inode->i_ino, de->d_inode->i_generation);
303                 l_dput(de);
304                 retval = ERR_PTR(-ENOENT);
305                 ldlm_lock_decref(lockh, lock_mode);
306 #ifdef S_PDIROPS
307                 if (lockh[1].cookie)
308                         ldlm_lock_decref(lockh + 1, *mode);
309 #endif
310         }
311
312         RETURN(retval);
313 }
314
315 #ifndef DCACHE_DISCONNECTED
316 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
317 #endif
318
319 /* Look up an entry by inode number. This function ONLY returns valid dget'd
320  * dentries with an initialized inode or errors */
321 struct dentry *mds_id2dentry(struct obd_device *obd, struct lustre_id *id,
322                              struct vfsmount **mnt)
323 {
324         struct mds_obd *mds = &obd->u.mds;
325         struct dentry *result;
326         struct inode *inode;
327         unsigned long ino = 0;
328         __u32 generation = 0;
329         char idname[32];
330
331         if (!id_ino(id) && id_fid(id)) {
332                 struct lustre_id *lid;
333                 
334                 /* if this is reint case we should use fidmap for resolving
335                  * correct local store cookie. */
336                 lid = mds_fidmap_lookup(obd, id);
337                 if (!lid)
338                         RETURN(ERR_PTR(-ENOENT));
339
340                 ino = (unsigned long)id_ino(lid);
341                 generation = (__u32)id_gen(lid);
342
343                 CDEBUG(D_DENTRY, "fidmap resolved "DLID4"->"DLID4"\n",
344                        OLID4(id), OLID4(lid));
345
346         } else if (id_ino(id)) {
347                 ino = (unsigned long)id_ino(id);
348                 generation = (__u32)id_gen(id);
349         } else {
350                 CERROR("invalid id for lookup "
351                        DLID4"\n", OLID4(id));
352                 LBUG();
353         }
354
355         if (ino == 0)
356                 RETURN(ERR_PTR(-ESTALE));
357
358         snprintf(idname, sizeof(idname), "0x%lx", ino);
359
360         CDEBUG(D_DENTRY, "--> mds_id2dentry: ino/gen %lu/%u, sb %p\n",
361                ino, generation, mds->mds_sb);
362
363         /* under ext3 this is neither supposed to return bad inodes nor NULL
364          * inodes. */
365         result = ll_lookup_one_len(idname, mds->mds_id_de, 
366                                    strlen(idname));
367         if (IS_ERR(result))
368                 RETURN(result);
369
370         inode = result->d_inode;
371         if (!inode)
372                 RETURN(ERR_PTR(-ENOENT));
373
374         if (is_bad_inode(inode)) {
375                 CERROR("bad inode returned %lu/%u\n",
376                        inode->i_ino, inode->i_generation);
377                 dput(result);
378                 RETURN(ERR_PTR(-ENOENT));
379         }
380
381         /* here we disabled generation check, as root inode i_generation
382          * of cache mds and real mds are different. */
383         if (id_fid(id) != id_fid(&mds->mds_rootid) && generation != 0 &&
384             inode->i_generation != generation) {
385                 /* we didn't find the right inode.. */
386                 if (id_group(id) != mds->mds_num) {
387                         CERROR("bad inode %lu found, link: %lu, ct: %d, generation "
388                                "%u != %u, mds %u != %u, request to wrong MDS?\n",
389                                inode->i_ino, (unsigned long)inode->i_nlink,
390                                atomic_read(&inode->i_count), inode->i_generation,
391                                generation, mds->mds_num, (unsigned)id_group(id));
392                 } else {
393                         CERROR("bad inode %lu found, link: %lu, ct: %d, generation "
394                                "%u != %u, inode is recreated while request handled?\n",
395                                inode->i_ino, (unsigned long)inode->i_nlink,
396                                atomic_read(&inode->i_count), inode->i_generation,
397                                generation);
398                 }
399                 dput(result);
400                 RETURN(ERR_PTR(-ENOENT));
401         }
402
403         if (mnt) {
404                 *mnt = mds->mds_vfsmnt;
405                 mntget(*mnt);
406         }
407
408         RETURN(result);
409 }
410
411 static
412 int mds_req_add_idmapping(struct ptlrpc_request *req,
413                           struct mds_export_data *med)
414 {
415         struct mds_req_sec_desc *rsd;
416         struct lustre_sec_desc  *lsd;
417         int rc;
418
419         if (!med->med_remote)
420                 return 0;
421
422         /* maybe we should do it more completely: invalidate the gss ctxt? */
423         if (req->rq_mapped_uid == MDS_IDMAP_NOTFOUND) {
424                 CWARN("didn't find mapped uid\n");
425                 return -EPERM;
426         }
427
428         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
429         if (!rsd) {
430                 CERROR("Can't unpack security desc\n");
431                 return -EPROTO;
432         }
433
434         lsd = mds_get_lsd(req->rq_mapped_uid);
435         if (!lsd) {
436                 CERROR("can't get LSD(%u), no mapping added\n",
437                        req->rq_mapped_uid);
438                 return -EPERM;
439         }
440
441         rc = mds_idmap_add(med->med_idmap, rsd->rsd_uid, lsd->lsd_uid,
442                            rsd->rsd_gid, lsd->lsd_gid);
443         mds_put_lsd(lsd);
444         return rc;
445 }
446
447 static
448 int mds_req_del_idmapping(struct ptlrpc_request *req,
449                           struct mds_export_data *med)
450 {
451         struct mds_req_sec_desc *rsd;
452         struct lustre_sec_desc  *lsd;
453         int rc;
454
455         if (!med->med_remote)
456                 return 0;
457
458         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
459         if (!rsd) {
460                 CERROR("Can't unpack security desc\n");
461                 return -EPROTO;
462         }
463
464         LASSERT(req->rq_mapped_uid != -1);
465         lsd = mds_get_lsd(req->rq_mapped_uid);
466         if (!lsd) {
467                 CERROR("can't get LSD(%u), no idmapping deleted\n",
468                        req->rq_mapped_uid);
469                 return -EPERM;
470         }
471
472         rc = mds_idmap_del(med->med_idmap, rsd->rsd_uid, lsd->lsd_uid,
473                            rsd->rsd_gid, lsd->lsd_gid);
474         mds_put_lsd(lsd);
475         return rc;
476 }
477
478 static int mds_init_export_data(struct ptlrpc_request *req,
479                                 struct mds_export_data *med)
480 {
481         struct obd_connect_data *data, *reply;
482         int ask_remote, ask_local;
483         ENTRY;
484
485         data = lustre_msg_buf(req->rq_reqmsg, 5, sizeof(*data));
486         reply = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*data));
487         LASSERT(data && reply);
488
489         if (med->med_initialized) {
490                 CDEBUG(D_SEC, "med already initialized, reconnect?\n");
491                 goto reply;
492         }
493
494         ask_remote = data->ocd_connect_flags & OBD_CONNECT_REMOTE;
495         ask_local = data->ocd_connect_flags & OBD_CONNECT_LOCAL;
496
497         /* currently the policy is simple: satisfy client as possible
498          * as we can.
499          */
500         if (req->rq_auth_uid == -1) {
501                 if (ask_remote)
502                         CWARN("null sec is used, force to be local\n");
503                 med->med_remote = 0;
504         } else {
505                 if (ask_remote) {
506                         if (!req->rq_remote_realm)
507                                 CWARN("local realm asked to be remote\n");
508                         med->med_remote = 1;
509                 } else if (ask_local) {
510                         if (req->rq_remote_realm)
511                                 CWARN("remote realm asked to be local\n");
512                         med->med_remote = 0;
513                 } else
514                         med->med_remote = (req->rq_remote_realm != 0);
515         }
516
517         med->med_nllu = data->ocd_nllu[0];
518         med->med_nllg = data->ocd_nllu[1];
519
520         med->med_initialized = 1;
521 reply:
522         reply->ocd_connect_flags &= ~(OBD_CONNECT_REMOTE | OBD_CONNECT_LOCAL);
523         if (med->med_remote) {
524                 if (!med->med_idmap)
525                         med->med_idmap = mds_idmap_alloc();
526
527                 if (!med->med_idmap)
528                         CERROR("Failed to alloc idmap, following request from "
529                                "this client will be refused\n");
530
531                 reply->ocd_connect_flags |= OBD_CONNECT_REMOTE;
532                 CDEBUG(D_SEC, "set client as remote\n");
533         } else {
534                 reply->ocd_connect_flags |= OBD_CONNECT_LOCAL;
535                 CDEBUG(D_SEC, "set client as local\n");
536         }
537
538         RETURN(0);
539 }
540
541 static void mds_free_export_data(struct mds_export_data *med)
542 {
543         if (!med->med_idmap)
544                 return;
545
546         LASSERT(med->med_remote);
547         mds_idmap_free(med->med_idmap);
548         med->med_idmap = NULL;
549 }
550
551 /* Establish a connection to the MDS.
552  *
553  * This will set up an export structure for the client to hold state data about
554  * that client, like open files, the last operation number it did on the server,
555  * etc.
556  */
557 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
558                        struct obd_uuid *cluuid, struct obd_connect_data *data,
559                        unsigned long flags)
560 {
561         struct mds_export_data *med;
562         struct mds_client_data *mcd;
563         struct obd_export *exp;
564         int rc;
565         ENTRY;
566
567         if (!conn || !obd || !cluuid)
568                 RETURN(-EINVAL);
569
570         /* XXX There is a small race between checking the list and adding a new
571          * connection for the same UUID, but the real threat (list corruption
572          * when multiple different clients connect) is solved.
573          *
574          * There is a second race between adding the export to the list, and
575          * filling in the client data below.  Hence skipping the case of NULL
576          * mcd above.  We should already be controlling multiple connects at the
577          * client, and we can't hold the spinlock over memory allocations
578          * without risk of deadlocking.
579          */
580         rc = class_connect(conn, obd, cluuid);
581         if (rc)
582                 RETURN(rc);
583         exp = class_conn2export(conn);
584         
585         LASSERT(exp != NULL);
586         med = &exp->exp_mds_data;
587
588         OBD_ALLOC(mcd, sizeof(*mcd));
589         if (!mcd) {
590                 CERROR("%s: out of memory for client data.\n",
591                         obd->obd_name);
592                 GOTO(out, rc = -ENOMEM);
593         }
594
595         memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
596         med->med_mcd = mcd;
597
598         rc = mds_client_add(obd, &obd->u.mds, med, -1);
599         if (rc)
600                 GOTO(out, rc);
601        
602         EXIT;
603 out:
604         if (rc) {
605                 if (mcd)
606                         OBD_FREE(mcd, sizeof(*mcd));
607                 class_disconnect(exp, 0);
608         } else {
609                 class_export_put(exp);
610         }
611         return rc;
612 }
613
614 static int mds_connect_post(struct obd_export *exp, unsigned initial,
615                             unsigned long flags)
616 {
617         struct obd_device *obd = exp->exp_obd;
618         struct mds_obd *mds = &obd->u.mds;
619         struct mds_export_data *med;
620         struct mds_client_data *mcd;
621         int rc = 0;
622         ENTRY;
623
624         med = &exp->exp_mds_data;
625         mcd = med->med_mcd;
626
627         if (initial) {
628                 /* some one reconnect initially, we have to reset
629                  * data existing export can have. bug 6102 */
630                 if (mcd->mcd_last_xid != 0)
631                         CDEBUG(D_HA, "initial reconnect to existing export\n");
632                 mcd->mcd_last_transno = 0;
633                 mcd->mcd_last_xid = 0;
634                 mcd->mcd_last_close_xid = 0;
635                 mcd->mcd_last_result = 0;
636                 mcd->mcd_last_data = 0;
637         }
638
639         if (!(flags & OBD_OPT_MDS_CONNECTION)) {
640                 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)) {
641                         atomic_inc(&mds->mds_real_clients);
642                         CDEBUG(D_OTHER,"%s: peer from %s is real client (%d)\n",
643                                obd->obd_name, exp->exp_client_uuid.uuid,
644                                atomic_read(&mds->mds_real_clients));
645                         exp->exp_flags |= OBD_OPT_REAL_CLIENT;
646                 }
647                 if (mds->mds_md_name)
648                         rc = mds_md_connect(obd, mds->mds_md_name);
649         }
650         RETURN(rc);
651 }
652
653 static int mds_init_export(struct obd_export *exp)
654 {
655         struct mds_export_data *med = &exp->exp_mds_data;
656
657         INIT_LIST_HEAD(&med->med_open_head);
658         spin_lock_init(&med->med_open_lock);
659         return 0;
660 }
661
662 static int mds_destroy_export(struct obd_export *export)
663 {
664         struct obd_device *obd = export->exp_obd;
665         struct mds_export_data *med = &export->exp_mds_data;
666         struct lvfs_run_ctxt saved;
667         int rc = 0;
668         ENTRY;
669
670         mds_free_export_data(med);
671         target_destroy_export(export);
672
673         if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
674                 GOTO(out, 0);
675
676         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
677
678         /* Close any open files (which may also cause orphan unlinking). */
679         spin_lock(&med->med_open_lock);
680         while (!list_empty(&med->med_open_head)) {
681                 struct list_head *tmp = med->med_open_head.next;
682                 struct mds_file_data *mfd =
683                         list_entry(tmp, struct mds_file_data, mfd_list);
684                 struct lustre_id sid;
685                 
686                 BDEVNAME_DECLARE_STORAGE(btmp);
687
688                 /* bug 1579: fix force-closing for 2.5 */
689                 struct dentry *dentry = mfd->mfd_dentry;
690
691                 list_del(&mfd->mfd_list);
692                 spin_unlock(&med->med_open_lock);
693
694                 down(&dentry->d_inode->i_sem);
695                 rc = mds_read_inode_sid(obd, dentry->d_inode, &sid);
696                 up(&dentry->d_inode->i_sem);
697                 if (rc) {
698                         CERROR("Can't read inode self id, inode %lu, "
699                                "rc %d\n", dentry->d_inode->i_ino, rc);
700                         memset(&sid, 0, sizeof(sid));
701                 }
702
703                 /* If you change this message, be sure to update
704                  * replay_single:test_46 */
705                 CERROR("force closing client file handle for %.*s (%s:"
706                        DLID4")\n", dentry->d_name.len, dentry->d_name.name,
707                        ll_bdevname(dentry->d_inode->i_sb, btmp),
708                        OLID4(&sid));
709                 
710                 /* child inode->i_alloc_sem protects orphan_dec_test and
711                  * is_orphan race, mds_mfd_close drops it */
712                 DOWN_WRITE_I_ALLOC_SEM(dentry->d_inode);
713                 rc = mds_mfd_close(NULL, 0, obd, mfd,
714                                    !(export->exp_flags & OBD_OPT_FAILOVER));
715                 if (rc)
716                         CDEBUG(D_INODE, "Error closing file: %d\n", rc);
717                 spin_lock(&med->med_open_lock);
718         }
719         spin_unlock(&med->med_open_lock);
720         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
721
722         EXIT;
723 out:
724         mds_client_free(export, !(export->exp_flags & OBD_OPT_FAILOVER));
725         return rc;
726 }
727
728 static int mds_disconnect(struct obd_export *exp, unsigned long flags)
729 {
730         unsigned long irqflags;
731         struct obd_device *obd;
732         struct mds_obd *mds;
733         int rc;
734         ENTRY;
735
736         LASSERT(exp != NULL);
737         obd = class_exp2obd(exp);
738         if (obd == NULL) {
739                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
740                        exp->exp_handle.h_cookie);
741                 RETURN(-EINVAL);
742         }
743         mds = &obd->u.mds;
744
745         /*
746          * suppress any inter-mds requests durring disconnecting lmv if this is
747          * detected --force mode. This is needed to avoid endless recovery.
748          */
749         if (atomic_read(&mds->mds_real_clients) > 0 &&
750             !(exp->exp_flags & OBD_OPT_REAL_CLIENT))
751                 flags |= OBD_OPT_FORCE;
752                                                                                               
753         if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)
754             && !atomic_read(&mds->mds_real_clients)) {
755                 /* there was no client at all */
756                 mds_md_disconnect(obd, flags);
757         }
758
759         if ((exp->exp_flags & OBD_OPT_REAL_CLIENT)
760             && atomic_dec_and_test(&mds->mds_real_clients)) {
761                 /* time to drop LMV connections */
762                 CDEBUG(D_OTHER, "%s: last real client %s disconnected.  "
763                        "Disconnnect from LMV now\n",
764                        obd->obd_name, exp->exp_client_uuid.uuid);
765                 mds_md_disconnect(obd, flags);
766         }
767
768         spin_lock_irqsave(&exp->exp_lock, irqflags);
769         exp->exp_flags = flags;
770         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
771
772         /* disconnect early so that clients can't keep using export */
773         rc = class_disconnect(exp, flags);
774         ldlm_cancel_locks_for_export(exp);
775
776         /* complete all outstanding replies */
777         spin_lock_irqsave(&exp->exp_lock, irqflags);
778         while (!list_empty(&exp->exp_outstanding_replies)) {
779                 struct ptlrpc_reply_state *rs =
780                         list_entry(exp->exp_outstanding_replies.next,
781                                    struct ptlrpc_reply_state, rs_exp_list);
782                 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
783
784                 spin_lock(&svc->srv_lock);
785                 list_del_init(&rs->rs_exp_list);
786                 ptlrpc_schedule_difficult_reply(rs);
787                 spin_unlock(&svc->srv_lock);
788         }
789         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
790         RETURN(rc);
791 }
792
793 static int mds_getstatus(struct ptlrpc_request *req)
794 {
795         struct mds_obd *mds = mds_req2mds(req);
796         struct mds_body *body;
797         int rc, size;
798         ENTRY;
799
800         size = sizeof(*body);
801         
802         rc = lustre_pack_reply(req, 1, &size, NULL);
803         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
804                 CERROR("mds: out of memory for message: size=%d\n", size);
805                 req->rq_status = -ENOMEM;       /* superfluous? */
806                 RETURN(-ENOMEM);
807         }
808
809         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
810         body->valid |= OBD_MD_FID;
811         
812         memcpy(&body->id1, &mds->mds_rootid, sizeof(body->id1));
813
814         /*
815          * the last_committed and last_xid fields are filled in for all replies
816          * already - no need to do so here also.
817          */
818         RETURN(0);
819 }
820
821 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
822                      void *data, int flag)
823 {
824         int do_ast;
825         ENTRY;
826
827         if (flag == LDLM_CB_CANCELING) {
828                 /* Don't need to do anything here. */
829                 RETURN(0);
830         }
831
832         /* XXX layering violation!  -phil */
833         lock_res_and_lock(lock);
834         
835         /*
836          * get this: if mds_blocking_ast is racing with mds_intent_policy, such
837          * that mds_blocking_ast is called just before l_i_p takes the ns_lock,
838          * then by the time we get the lock, we might not be the correct
839          * blocking function anymore.  So check, and return early, if so.
840          */
841         if (lock->l_blocking_ast != mds_blocking_ast) {
842                 unlock_res_and_lock(lock);
843                 RETURN(0);
844         }
845
846         lock->l_flags |= LDLM_FL_CBPENDING;
847         do_ast = (!lock->l_readers && !lock->l_writers);
848         unlock_res_and_lock(lock);
849
850         if (do_ast) {
851                 struct lustre_handle lockh;
852                 int rc;
853
854                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
855                 ldlm_lock2handle(lock, &lockh);
856                 rc = ldlm_cli_cancel(&lockh);
857                 if (rc < 0)
858                         CERROR("ldlm_cli_cancel: %d\n", rc);
859         } else {
860                 LDLM_DEBUG(lock, "Lock still has references, will be "
861                            "cancelled later");
862         }
863         RETURN(0);
864 }
865
866 static int mds_convert_md(struct obd_device *obd, struct inode *inode,
867                           void *md, int size, int mea)
868 {
869         int rc = size;
870         
871         if (S_ISREG(inode->i_mode)) {
872                 rc = mds_convert_lov_ea(obd, inode, md, size);
873         } else if (S_ISDIR(inode->i_mode)) {
874                 if (mea) {
875                         rc = mds_convert_mea_ea(obd, inode, md, size);
876                 } else {
877                         rc = mds_convert_lov_ea(obd, inode, md, size);
878                 }
879                 if (rc == -EINVAL) {
880                         CERROR("Invalid EA format (nor LOV or MEA) "
881                                "is detected. Inode %lu/%u\n",
882                                inode->i_ino, inode->i_generation);
883                 }
884         }
885         return rc;
886 }
887
888 int mds_get_md(struct obd_device *obd, struct inode *inode,
889                void *md, int *size, int lock, int mea)
890 {
891         int lmm_size;
892         int rc = 0;
893         ENTRY;
894
895         if (lock)
896                 down(&inode->i_sem);
897
898         rc = fsfilt_get_md(obd, inode, md, *size,
899                            (mea ? EA_MEA : EA_LOV));
900         if (rc < 0) {
901                 CERROR("Error %d reading eadata for ino %lu\n",
902                        rc, inode->i_ino);
903         } else if (rc > 0) {
904                 lmm_size = rc;
905                 rc = mds_convert_md(obd, inode, md,
906                                     lmm_size, mea);
907                 if (rc == 0) {
908                         *size = lmm_size;
909                         rc = lmm_size;
910                 } else if (rc > 0) {
911                         *size = rc;
912                 }
913         }
914         if (lock)
915                 up(&inode->i_sem);
916
917         RETURN(rc);
918 }
919
920 /* Call with lock=1 if you want mds_pack_md to take the i_sem.
921  * Call with lock=0 if the caller has already taken the i_sem. */
922 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
923                 struct mds_body *body, struct inode *inode, int lock, int mea)
924 {
925         struct mds_obd *mds = &obd->u.mds;
926         int rc, lmm_size;
927         void *lmm;
928         ENTRY;
929
930         lmm = lustre_msg_buf(msg, offset, 0);
931         if (lmm == NULL) {
932                 /* Some problem with getting eadata when I sized the reply
933                  * buffer... */
934                 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
935                        inode->i_ino);
936                 RETURN(0);
937         }
938         lmm_size = msg->buflens[offset];
939
940         /* I don't really like this, but it is a sanity check on the client
941          * MD request.  However, if the client doesn't know how much space
942          * to reserve for the MD, it shouldn't be bad to have too much space.
943          */
944         if (lmm_size > mds->mds_max_mdsize) {
945                 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
946                        inode->i_ino, lmm_size, mds->mds_max_mdsize);
947                 // RETURN(-EINVAL);
948         }
949
950         rc = mds_get_md(obd, inode, lmm, &lmm_size, lock, mea);
951         if (rc > 0) {
952                 body->valid |= S_ISDIR(inode->i_mode) ?
953                         OBD_MD_FLDIREA : OBD_MD_FLEASIZE;
954                 
955                 if (mea)
956                         body->valid |= OBD_MD_MEA;
957                 
958                 body->eadatasize = lmm_size;
959                 rc = 0;
960         }
961
962         RETURN(rc);
963 }
964
965 int mds_pack_link(struct dentry *dentry, struct ptlrpc_request *req,
966                   struct mds_body *repbody, int reply_off)
967 {
968         struct inode *inode = dentry->d_inode;
969         char *symname;
970         int len, rc;
971         ENTRY;
972
973         symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1,0);
974         LASSERT(symname != NULL);
975         len = req->rq_repmsg->buflens[reply_off + 1];
976         
977         rc = inode->i_op->readlink(dentry, symname, len);
978         if (rc < 0) {
979                 CERROR("readlink failed: %d\n", rc);
980         } else if (rc != len - 1) {
981                 CERROR ("Unexpected readlink rc %d: expecting %d\n",
982                         rc, len - 1);
983                 rc = -EINVAL;
984         } else {
985                 CDEBUG(D_INODE, "read symlink dest %s\n", symname);
986                 repbody->valid |= OBD_MD_LINKNAME;
987                 repbody->eadatasize = rc + 1;
988                 symname[rc] = 0;        /* NULL terminate */
989                 rc = 0;
990         }
991
992         RETURN(rc);
993 }
994
995 int mds_pack_xattr(struct dentry *dentry, struct ptlrpc_request *req,
996                    struct mds_body *repbody, int req_off, int reply_off)
997 {
998         struct inode *inode = dentry->d_inode;
999         char *ea_name;
1000         void *value = NULL;
1001         int len, rc;
1002         ENTRY;
1003
1004         ea_name = lustre_msg_string(req->rq_reqmsg, req_off + 1, 0);
1005         len = req->rq_repmsg->buflens[reply_off + 1];
1006         if (len != 0)
1007                 value = lustre_msg_buf(req->rq_repmsg, reply_off + 1, len);
1008
1009         rc = -EOPNOTSUPP;
1010
1011         if (!strcmp(ea_name, XATTR_NAME_LUSTRE_ACL)) {
1012                 struct rmtacl_upcall_desc desc;
1013
1014                 if (len != LUSTRE_ACL_SIZE_MAX || !value) {
1015                         CERROR("no reply buffer prepared\n");
1016                         RETURN(-EFAULT);
1017                 }
1018
1019                 memset(&desc, 0, sizeof(desc));
1020                 desc.get = 1;
1021                 desc.cmd = lustre_msg_string(req->rq_reqmsg, req_off + 2, 0);
1022                 desc.cmdlen =  req->rq_reqmsg->buflens[req_off + 2];
1023                 desc.res = (char *) value;
1024                 desc.reslen = LUSTRE_ACL_SIZE_MAX;
1025
1026                 mds_do_remote_acl_upcall(&desc);
1027
1028                 if (desc.upcall_status)
1029                         RETURN(desc.upcall_status);
1030
1031                 if (desc.reslen > LUSTRE_ACL_SIZE_MAX) {
1032                         CERROR("downcall claim reslen %u\n", desc.reslen);
1033                         RETURN(-EINVAL);
1034                 }
1035                 /* like remote setfacl, steal "flags" in mds_body as the
1036                  * exececution status
1037                  */
1038                 repbody->flags = desc.status;
1039                 repbody->valid |= OBD_MD_FLXATTR;
1040                 repbody->eadatasize = desc.reslen;
1041
1042                 RETURN(0);
1043         }
1044
1045         if (inode->i_op && inode->i_op->getxattr)
1046                 rc = inode->i_op->getxattr(dentry, ea_name, value, len);
1047
1048         if (rc < 0) {
1049                 if (rc != -ENODATA && rc != -EOPNOTSUPP)
1050                         CERROR("getxattr failed: %d", rc);
1051         } else {
1052                 repbody->valid |= OBD_MD_FLXATTR;
1053                 repbody->eadatasize = rc;
1054                 rc = 0;
1055         }
1056
1057         RETURN(rc);
1058 }
1059
1060 int mds_pack_xattr_list(struct dentry *dentry, struct ptlrpc_request *req,
1061                         struct mds_body *repbody, int reply_off)
1062 {
1063         struct inode *inode = dentry->d_inode;        
1064         void *value = NULL;
1065         int len, rc;
1066         ENTRY;
1067
1068         len = req->rq_repmsg->buflens[reply_off + 1];
1069         if (len != 0)
1070                 value = lustre_msg_buf(req->rq_repmsg, reply_off + 1, len);
1071
1072         rc = -EOPNOTSUPP;
1073         if (inode->i_op && inode->i_op->getxattr) 
1074                 rc = inode->i_op->listxattr(dentry, value, len);
1075
1076         if (rc < 0) {
1077                 CERROR("listxattr failed: %d", rc);
1078         } else {
1079                 repbody->valid |= OBD_MD_FLXATTRLIST;
1080                 repbody->eadatasize = rc;
1081                 rc = 0;
1082         }
1083         RETURN(rc);
1084 }
1085
1086 static
1087 int mds_pack_posix_acl(struct lustre_msg *repmsg, int offset,
1088                        struct mds_body *body, struct inode *inode)
1089 {
1090         struct dentry de = { .d_inode = inode };
1091         __u32 buflen, *sizep;
1092         void *buf;
1093         int size;
1094         ENTRY;
1095
1096         if (!inode->i_op->getxattr)
1097                 RETURN(0);
1098
1099         sizep = lustre_msg_buf(repmsg, offset, 4);
1100         if (!sizep) {
1101                 CERROR("can't locate returned acl size buf\n");
1102                 RETURN(-EPROTO);
1103         }
1104
1105         buflen = repmsg->buflens[offset + 1];
1106         buf = lustre_msg_buf(repmsg, offset + 1, buflen);
1107
1108         size = inode->i_op->getxattr(&de, XATTR_NAME_ACL_ACCESS, buf, buflen);
1109         if (size == -ENODATA || size == -EOPNOTSUPP)
1110                 RETURN(0);
1111         if (size < 0)
1112                 RETURN(size);
1113         LASSERT(size);
1114
1115         *sizep = cpu_to_le32(size);
1116         body->valid |= OBD_MD_FLACL;
1117
1118         RETURN(0);
1119 }
1120
1121 int mds_pack_remote_perm(struct ptlrpc_request *req, int reply_off,
1122                          struct mds_body *body, struct inode *inode)
1123 {
1124         struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1125         struct lustre_sec_desc *lsd;
1126         struct mds_remote_perm *perm;
1127         __u32 lsd_perms;
1128
1129         LASSERT(inode->i_op);
1130         LASSERT(inode->i_op->permission);
1131         LASSERT(req->rq_export->exp_mds_data.med_remote);
1132
1133         perm = (struct mds_remote_perm *)
1134                        lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(perm));
1135         if (!perm) {
1136                 CERROR("no remote perm buf at offset %d\n", reply_off);
1137                 return -EINVAL;
1138         }
1139
1140         memset(perm, 0, sizeof(*perm));
1141
1142         /* obtain authenticated uid/gid and LSD permissions, which
1143          * might be different from current process context, from LSD
1144          */
1145         lsd = mds_get_lsd(current->uid);
1146         if (!lsd) {
1147                 CWARN("can't LSD of uid %u\n", current->uid);
1148                 RETURN(-EPERM);
1149         }
1150
1151         perm->mrp_auth_uid = lsd->lsd_uid;
1152         perm->mrp_auth_gid = lsd->lsd_gid;
1153
1154         lsd_perms = mds_lsd_get_perms(lsd, 1, 0, req->rq_peer.peer_id.nid);
1155         if (lsd_perms & LSD_PERM_SETUID)
1156                 perm->mrp_allow_setuid = 1;
1157         if (lsd_perms & LSD_PERM_SETGID)
1158                 perm->mrp_allow_setgid = 1;
1159
1160         mds_put_lsd(lsd);
1161
1162         if (mds_remote_perm_do_reverse_map(med, perm))
1163                 RETURN(-EPERM);
1164
1165         /* permission bits of current user
1166          * XXX this is low efficient, could we do it in one blow?
1167          */
1168         if (inode->i_op->permission(inode, MAY_EXEC, NULL) == 0)
1169                 perm->mrp_perm |= MAY_EXEC;
1170         if (inode->i_op->permission(inode, MAY_WRITE, NULL) == 0)
1171                 perm->mrp_perm |= MAY_WRITE;
1172         if (inode->i_op->permission(inode, MAY_READ, NULL) == 0)
1173                 perm->mrp_perm |= MAY_READ;
1174
1175         body->valid |= (OBD_MD_FLACL | OBD_MD_FLRMTACL);
1176
1177         RETURN(0);
1178 }
1179
1180 int mds_pack_acl(struct ptlrpc_request *req, int reply_off,
1181                  struct mds_body *body, struct inode *inode)
1182 {
1183         int rc;
1184
1185         if (!req->rq_export->exp_mds_data.med_remote)
1186                 rc = mds_pack_posix_acl(req->rq_repmsg, reply_off, body, inode);
1187         else
1188                 rc = mds_pack_remote_perm(req, reply_off + 1, body, inode);
1189
1190         return rc;
1191 }
1192
1193 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
1194                                 struct ptlrpc_request *req, int req_off,
1195                                 struct mds_body *reqbody, int reply_off,
1196                                 struct mds_req_sec_desc *rsd)
1197 {
1198         struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1199         struct inode *inode = dentry->d_inode;
1200         struct mds_body *body;
1201         int rc = 0, offset = 0;
1202         ENTRY;
1203
1204         if (inode == NULL && !(dentry->d_flags & DCACHE_CROSS_REF))
1205                 RETURN(-ENOENT);
1206
1207         body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
1208         LASSERT(body != NULL);                 /* caller prepped reply */
1209
1210         if (dentry->d_flags & DCACHE_CROSS_REF) {
1211                 mds_pack_dentry2body(obd, body, dentry,
1212                                      (reqbody->valid & OBD_MD_FID) ? 1 : 0);
1213                 CDEBUG(D_OTHER, "cross reference: "DLID4"\n",
1214                        OLID4(&body->id1));
1215                 RETURN(0);
1216         }
1217         
1218         mds_pack_inode2body(obd, body, inode,
1219                             (reqbody->valid & OBD_MD_FID) ? 1 : 0);
1220
1221         if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
1222             (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
1223             
1224                 /* guessing what kind og attribute do we need. */
1225                 int is_mea = (S_ISDIR(inode->i_mode) && 
1226                     (reqbody->valid & OBD_MD_MEA) != 0);
1227                 
1228                 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1, 
1229                                  body, inode, 1, is_mea);
1230
1231                 /* if we have LOV EA data, the OST holds size, atime, mtime. */
1232                 if (!(body->valid & OBD_MD_FLEASIZE) &&
1233                     !(body->valid & OBD_MD_FLDIREA))
1234                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1235                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
1236                 
1237         } else if (S_ISLNK(inode->i_mode) &&
1238                    (reqbody->valid & OBD_MD_LINKNAME) != 0) {
1239                 rc = mds_pack_link(dentry, req, body, reply_off);
1240         } else if (reqbody->valid & OBD_MD_FLXATTR) {
1241                 rc = mds_pack_xattr(dentry, req, body, req_off, reply_off);
1242         } else if (reqbody->valid & OBD_MD_FLXATTRLIST) {
1243                 rc = mds_pack_xattr_list(dentry, req, body, reply_off);
1244         }
1245         
1246         offset = reply_off + ((reqbody->valid & OBD_MD_FLEASIZE) ? 2 : 1);
1247         if (reqbody->valid & OBD_MD_FLACL) {
1248                 rc = mds_pack_acl(req, offset, body, inode);
1249                 offset += 2;
1250         }                
1251
1252         if (reqbody->valid & OBD_MD_FLKEY) {
1253                 rc = mds_pack_gskey(obd, req->rq_repmsg, &offset, 
1254                                     body, inode);
1255         }
1256         
1257         mds_pack_audit(obd, inode, body);
1258
1259         if (reqbody->valid & OBD_MD_CAPA) {
1260                 struct lustre_capa *req_capa;
1261
1262                 LASSERT(!(reqbody->valid & ~OBD_MD_CAPA));
1263                 LASSERT(S_ISREG(inode->i_mode));
1264
1265                 req_capa = lustre_swab_reqbuf(req, req_off + 1,
1266                                               sizeof(*req_capa),
1267                                               lustre_swab_lustre_capa);
1268                 if (req_capa == NULL) {
1269                         CERROR("Can't unpack capa\n");
1270                         RETURN(-EFAULT);
1271                 }
1272
1273                 offset = reply_off + 1;
1274                 rc = mds_pack_capa(obd, med, reqbody, req_capa, req,
1275                                    &offset, body);
1276         }
1277
1278         if (rc == 0)
1279                 mds_body_do_reverse_map(med, body);
1280
1281         RETURN(rc);
1282 }
1283
1284 static int mds_getattr_pack_msg_cf(struct ptlrpc_request *req,
1285                                    struct dentry *dentry,
1286                                    int offset)
1287 {
1288         int rc = 0, size[1] = {sizeof(struct mds_body)};
1289         ENTRY;
1290
1291         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
1292                 CERROR("failed MDS_GETATTR_PACK test\n");
1293                 req->rq_status = -ENOMEM;
1294                 RETURN(-ENOMEM);
1295         }
1296
1297         rc = lustre_pack_reply(req, 1, size, NULL);
1298         if (rc) {
1299                 CERROR("lustre_pack_reply failed: rc %d\n", rc);
1300                 GOTO(out, req->rq_status = rc);
1301         }
1302
1303         EXIT;
1304 out:
1305         return rc;
1306 }
1307
1308 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct dentry *de,
1309                                 int offset)
1310 {
1311         struct inode *inode = de->d_inode;
1312         struct mds_obd *mds = mds_req2mds(req);
1313         struct mds_body *body;
1314         int rc = 0, size[4] = {sizeof(*body)}, bufcount = 1;
1315         ENTRY;
1316
1317         body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*body));
1318         LASSERT(body != NULL);                 /* checked by caller */
1319         LASSERT_REQSWABBED(req, offset);       /* swabbed by caller */
1320
1321         if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
1322             (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
1323                 int rc;
1324                 
1325                 down(&inode->i_sem);
1326                 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0,
1327                                    ((body->valid & OBD_MD_MEA) ? EA_MEA : EA_LOV));
1328                 up(&inode->i_sem);
1329                 if (rc < 0) {
1330                         if (rc != -ENODATA && rc != -EOPNOTSUPP)
1331                                 CERROR("error getting inode %lu MD: rc = %d\n",
1332                                        inode->i_ino, rc);
1333                         size[bufcount] = 0;
1334                 } else if (rc > mds->mds_max_mdsize) {
1335                         size[bufcount] = 0;
1336                         CERROR("MD size %d larger than maximum possible %u\n",
1337                                rc, mds->mds_max_mdsize);
1338                 } else {
1339                         size[bufcount] = rc;
1340                 }
1341                 bufcount++;
1342         } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
1343                 if (inode->i_size + 1 != body->eadatasize)
1344                         CERROR("symlink size: %Lu, reply space: %d\n",
1345                                inode->i_size + 1, body->eadatasize);
1346                 size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
1347                 bufcount++;
1348                 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
1349                        inode->i_size + 1, body->eadatasize);
1350         } else if ((body->valid & OBD_MD_FLXATTR)) {
1351                 char *ea_name = lustre_msg_string(req->rq_reqmsg, 
1352                                                   offset + 1, 0);
1353                 rc = -EOPNOTSUPP;
1354
1355                 if (!strcmp(ea_name, XATTR_NAME_LUSTRE_ACL)) {
1356                         size[bufcount] = LUSTRE_ACL_SIZE_MAX;
1357                 } else {
1358                         if (inode->i_op && inode->i_op->getxattr)
1359                                 rc = inode->i_op->getxattr(de, ea_name,
1360                                                            NULL, 0);
1361
1362                         if (rc < 0) {
1363                                 if (rc != -ENODATA && rc != -EOPNOTSUPP)
1364                                         CERROR("error get inode %lu EA: %d\n",
1365                                                inode->i_ino, rc);
1366                                 size[bufcount] = 0;
1367                         } else {
1368                                 size[bufcount] = min_t(int,
1369                                                        body->eadatasize, rc);
1370                         }
1371                 }
1372                 bufcount++;
1373         } else if (body->valid & OBD_MD_FLXATTRLIST) {
1374                 rc = -EOPNOTSUPP;
1375                 if (inode->i_op && inode->i_op->getxattr) 
1376                         rc = inode->i_op->listxattr(de, NULL, 0);
1377
1378                 if (rc < 0) {
1379                         if (rc != -ENODATA && rc != -EOPNOTSUPP)
1380                                 CERROR("error getting inode %lu EA: rc = %d\n",
1381                                        inode->i_ino, rc);
1382                         size[bufcount] = 0;
1383                 } else {
1384                         size[bufcount] = min_t(int, body->eadatasize, rc);
1385                 }
1386                 bufcount++;
1387         }
1388         
1389         /* may co-exist with OBD_MD_FLEASIZE */
1390         if (body->valid & OBD_MD_FLACL) {
1391                 if (req->rq_export->exp_mds_data.med_remote) {
1392                         size[bufcount++] = sizeof(int);
1393                         size[bufcount++] = sizeof(struct mds_remote_perm);
1394                 } else {
1395                         size[bufcount++] = sizeof(int);
1396                         size[bufcount++] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
1397                 }
1398         }
1399
1400         if (body->valid & OBD_MD_FLKEY) {
1401                 size[bufcount++] = sizeof(int);
1402                 size[bufcount++] = sizeof(struct crypto_key);
1403         }
1404
1405         if (body->valid & OBD_MD_CAPA)
1406                 size[bufcount++] = sizeof(struct lustre_capa);
1407
1408         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
1409                 CERROR("failed MDS_GETATTR_PACK test\n");
1410                 req->rq_status = -ENOMEM;
1411                 GOTO(out, rc = -ENOMEM);
1412         }
1413
1414         rc = lustre_pack_reply(req, bufcount, size, NULL);
1415         if (rc) {
1416                 CERROR("out of memory\n");
1417                 GOTO(out, req->rq_status = rc);
1418         }
1419
1420         EXIT;
1421  out:
1422         return rc;
1423 }
1424
1425 int mds_check_mds_num(struct obd_device *obd, struct inode *inode,
1426                       char *name, int namelen)
1427 {
1428         struct mea *mea = NULL;
1429         int mea_size, rc = 0;
1430         ENTRY;
1431         
1432         rc = mds_md_get_attr(obd, inode, &mea, &mea_size);
1433         if (rc)
1434                 RETURN(rc);
1435         if (mea != NULL && mea->mea_count) {
1436                 /*
1437                  * dir is already splitted, check if requested filename should
1438                  * live at this MDS or at another one.
1439                  */
1440                 int i = mea_name2idx(mea, name, namelen - 1);
1441                 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
1442                         CDEBUG(D_OTHER,
1443                                "inapropriate MDS(%d) for %s. should be "
1444                                "%lu(%d)\n", mea->mea_master, name, 
1445                                (unsigned long)id_group(&mea->mea_ids[i]), i);
1446                         rc = -ERESTART;
1447                 }
1448         }
1449
1450         if (mea)
1451                 OBD_FREE(mea, mea_size);
1452         RETURN(rc);
1453 }
1454
1455 int mds_getattr_size(struct obd_device *obd, struct dentry *dentry,
1456                      struct ptlrpc_request *req, struct mds_body *body)
1457 {
1458         struct inode *inode = dentry->d_inode;
1459         struct mds_obd *mds = &obd->u.mds;
1460         ENTRY;
1461
1462         LASSERT(body != NULL);
1463
1464         if (dentry->d_inode == NULL || !S_ISREG(inode->i_mode))
1465                 RETURN(0);
1466         
1467         /* XXX: quite a ugly hack, need to check old code
1468          * drop FLSIZE/FLBLOCKS prior any checking to */
1469         body->valid &= ~(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS|OBD_MD_FLMTIME);
1470         
1471         if (obd->obd_recovering) {
1472                 CDEBUG(D_INODE, "size for "DLID4" is unknown yet (recovering)\n",
1473                        OLID4(&body->id1));
1474                 RETURN(0);
1475         }
1476
1477        if (mds->mds_config_generation)
1478                return 0;
1479
1480         if (atomic_read(&inode->i_writecount)) {
1481                 /* some one has opened the file for write.
1482                  * mds doesn't know actual size */
1483                 CDEBUG(D_INODE, "MDS doesn't know actual size for "DLID4"\n",
1484                        OLID4(&body->id1));
1485                 RETURN(0);
1486         }
1487         CDEBUG(D_INODE, "MDS returns "LPD64"/"LPD64" for"DLID4"\n",
1488                body->size, body->blocks, OLID4(&body->id1));
1489         body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLMTIME;
1490         RETURN(0);
1491 }
1492
1493 static int mds_getattr_lock(struct ptlrpc_request *req, int offset,
1494                             struct lustre_handle *child_lockh, int child_part)
1495 {
1496         struct obd_device *obd = req->rq_export->exp_obd;
1497         struct mds_obd *mds = &obd->u.mds;
1498         struct ldlm_reply *rep = NULL;
1499         struct lvfs_run_ctxt saved;
1500         struct mds_req_sec_desc *rsd;
1501         struct mds_body *body;
1502         struct dentry *dparent = NULL, *dchild = NULL;
1503         struct lvfs_ucred uc = {NULL, NULL,};
1504         struct lustre_handle parent_lockh[2] = {{0}, {0}};
1505         unsigned int namesize = 0;
1506         int rc = 0, cleanup_phase = 0, resent_req = 0, update_mode, reply_offset;
1507         char *name = NULL;
1508         ENTRY;
1509
1510         LASSERT(!strcmp(obd->obd_type->typ_name, OBD_MDS_DEVICENAME));
1511         MD_COUNTER_INCREMENT(obd, getattr_lock);
1512
1513         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1514         if (!rsd) {
1515                 CERROR("Can't unpack security desc\n");
1516                 RETURN(-EFAULT);
1517         }
1518
1519         /* swab now, before anyone looks inside the request. */
1520         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1521                                   lustre_swab_mds_body);
1522         if (body == NULL) {
1523                 CERROR("Can't swab mds_body\n");
1524                 GOTO(cleanup, rc = -EFAULT);
1525         }
1526
1527         LASSERT_REQSWAB(req, offset + 1);
1528         name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
1529         if (name == NULL) {
1530                 CERROR("Can't unpack name\n");
1531                 GOTO(cleanup, rc = -EFAULT);
1532         }
1533         namesize = req->rq_reqmsg->buflens[offset + 1];
1534
1535         /* namesize less than 2 means we have empty name, probably came from
1536            revalidate by cfid, so no point in having name to be set */
1537         if (namesize <= 1)
1538                 name = NULL;
1539
1540         LASSERT (offset == 1 || offset == 3);
1541         if (offset == 3) {
1542                 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
1543                 reply_offset = 1;
1544         } else {
1545                 reply_offset = 0;
1546         }
1547
1548         rc = mds_init_ucred(&uc, req, rsd);
1549         if (rc) {
1550                 if (child_lockh->cookie == 0)
1551                         mds_audit_auth(req, &uc, AUDIT_STAT, &body->id1, 
1552                                        name, namesize - 1);
1553                 GOTO(cleanup, rc);
1554         }
1555
1556         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1557         cleanup_phase = 1; /* kernel context */
1558         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
1559
1560         LASSERT(namesize > 0);
1561         if (child_lockh->cookie != 0) {
1562                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1563                 resent_req = 1;
1564         }
1565 #if HAVE_LOOKUP_RAW
1566         if (body->valid == OBD_MD_FLID) {
1567                 struct mds_body *mds_reply;
1568                 int size = sizeof(*mds_reply);
1569                 struct inode *dir;
1570                 ino_t inum;
1571
1572                 dparent = mds_id2dentry(obd, &body->id1, NULL);
1573                 if (IS_ERR(dparent)) {
1574                         rc = PTR_ERR(dparent);
1575                         GOTO(cleanup, rc);
1576                 }
1577                 /*
1578                  * the user requested ONLY the inode number, so do a raw lookup.
1579                  */
1580                 rc = lustre_pack_reply(req, 1, &size, NULL);
1581                 if (rc) {
1582                         CERROR("out of memory\n");
1583                         l_dput(dparent);
1584                         GOTO(cleanup, rc);
1585                 }
1586                 dir  = dparent->d_inode;
1587                 LASSERT(dir->i_op->lookup_raw != NULL);
1588                 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
1589                 l_dput(dparent);
1590                 mds_reply = lustre_msg_buf(req->rq_repmsg, 0,
1591                                            sizeof(*mds_reply));
1592
1593                 id_ino(&mds_reply->id1) = inum;
1594                 mds_reply->valid = OBD_MD_FLID;
1595                 GOTO(cleanup, rc);
1596         }
1597 #endif
1598         if (resent_req == 0) {
1599                 LASSERT(id_fid(&body->id1) != 0);
1600                 if (name) {
1601                         rc = mds_get_parent_child_locked(obd, mds, &body->id1,
1602                                                          parent_lockh, &dparent,
1603                                                          LCK_PR, 
1604                                                          MDS_INODELOCK_UPDATE,
1605                                                          &update_mode, 
1606                                                          name, namesize,
1607                                                          child_lockh, &dchild, 
1608                                                          LCK_PR, child_part);
1609                         if (rc)
1610                                 GOTO(cleanup, rc);
1611                 
1612                         cleanup_phase = 2; /* dchild, dparent, locks */
1613                         
1614                         /*
1615                          * let's make sure this name should leave on this mds
1616                          * node.
1617                          */
1618                         rc = mds_check_mds_num(obd, dparent->d_inode, name, namesize);
1619                         if (rc)
1620                                 GOTO(cleanup, rc);
1621                 } else {
1622                         /* we have no dentry here, drop LOOKUP bit */
1623                         /* FIXME: we need MDS_INODELOCK_LOOKUP or not. */
1624                         child_part &= ~MDS_INODELOCK_LOOKUP;
1625                         CDEBUG(D_OTHER, "%s: retrieve attrs for "DLID4"\n",
1626                                obd->obd_name, OLID4(&body->id1));
1627
1628                         dchild = mds_id2locked_dentry(obd, &body->id1, NULL,
1629                                                       LCK_PR, parent_lockh,
1630                                                       &update_mode, NULL, 0, 
1631                                                       MDS_INODELOCK_UPDATE);
1632                         if (IS_ERR(dchild)) {
1633                                 CERROR("can't find inode with id "DLID4", err = %d\n", 
1634                                        OLID4(&body->id1), (int)PTR_ERR(dchild));
1635                                 rc = PTR_ERR(dchild);
1636                                 dchild = NULL;
1637                                 GOTO(cleanup, rc);
1638                         }
1639                         memcpy(child_lockh, parent_lockh, sizeof(parent_lockh[0]));
1640                 }
1641         } else {
1642                 struct ldlm_lock *granted_lock;
1643
1644                 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
1645                 granted_lock = ldlm_handle2lock(child_lockh);
1646
1647                 LASSERTF(granted_lock != NULL, LPU64"/%lu lockh "LPX64"\n",
1648                          id_fid(&body->id1), (unsigned long)id_group(&body->id1),
1649                          child_lockh->cookie);
1650
1651                 if (name) {
1652                         /* usual named request */
1653                         dparent = mds_id2dentry(obd, &body->id1, NULL);
1654                         LASSERT(!IS_ERR(dparent));
1655                         dchild = ll_lookup_one_len(name, dparent, namesize - 1);
1656                         LASSERT(!IS_ERR(dchild));
1657                 } else {
1658                         /* client wants to get attr. by id */
1659                         dchild = mds_id2dentry(obd, &body->id1, NULL);
1660                         LASSERT(!IS_ERR(dchild));
1661                 }
1662                 LDLM_LOCK_PUT(granted_lock);
1663         }
1664
1665         cleanup_phase = 2; /* dchild, dparent, locks */
1666
1667         if (!DENTRY_VALID(dchild)) {
1668                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
1669                 /*
1670                  * in the intent case, the policy clears this error: the
1671                  * disposition is enough.
1672                  */
1673                 rc = -ENOENT;
1674                 GOTO(cleanup, rc);
1675         } else {
1676                 intent_set_disposition(rep, DISP_LOOKUP_POS);
1677         }
1678
1679         if (req->rq_repmsg == NULL) {
1680                 if (dchild->d_flags & DCACHE_CROSS_REF)
1681                         rc = mds_getattr_pack_msg_cf(req, dchild, offset);
1682                 else
1683                         rc = mds_getattr_pack_msg(req, dchild, offset);
1684                 if (rc != 0) {
1685                         CERROR ("mds_getattr_pack_msg: %d\n", rc);
1686                         GOTO (cleanup, rc);
1687                 }
1688         }
1689
1690         rc = mds_getattr_internal(obd, dchild, req, offset, body,
1691                                   reply_offset, rsd);
1692         if (rc)
1693                 GOTO(cleanup, rc); /* returns the lock to the client */
1694
1695         /* probably MDS knows actual size? */
1696         body = lustre_msg_buf(req->rq_repmsg, reply_offset, sizeof(*body));
1697         LASSERT(body != NULL);
1698         mds_getattr_size(obd, dchild, req, body);
1699
1700         GOTO(cleanup, rc);
1701
1702  cleanup:
1703         switch (cleanup_phase) {
1704         case 2:
1705                 if (resent_req == 0) {
1706                         mds_audit(req, dchild, name, namesize - 1,
1707                                   AUDIT_STAT, rc);
1708                         if (rc && DENTRY_VALID(dchild))
1709                                 ldlm_lock_decref(child_lockh, LCK_PR);
1710                         if (name)
1711                                 ldlm_lock_decref(parent_lockh, LCK_PR);
1712 #ifdef S_PDIROPS
1713                         if (parent_lockh[1].cookie != 0)
1714                                 ldlm_lock_decref(parent_lockh + 1, update_mode);
1715 #endif
1716                 }
1717                 if (dparent)
1718                         l_dput(dparent);
1719
1720                 l_dput(dchild);
1721         case 1:
1722                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1723         default:
1724                 mds_exit_ucred(&uc);
1725         }
1726         return rc;
1727 }
1728
1729 static int mds_getattr(struct ptlrpc_request *req, int offset)
1730 {
1731         struct obd_device *obd = req->rq_export->exp_obd;
1732         struct lvfs_run_ctxt saved;
1733         struct dentry *de;
1734         struct mds_req_sec_desc *rsd;
1735         struct mds_body *body;
1736         struct lvfs_ucred uc = {NULL, NULL,};
1737         int rc = 0;
1738         ENTRY;
1739
1740         MD_COUNTER_INCREMENT(obd, getattr);
1741
1742         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1743         if (!rsd) {
1744                 CERROR("Can't unpack security desc\n");
1745                 RETURN(-EFAULT);
1746         }
1747
1748         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1749                                   lustre_swab_mds_body);
1750         if (body == NULL) {
1751                 CERROR ("Can't unpack body\n");
1752                 RETURN (-EFAULT);
1753         }
1754
1755         rc = mds_init_ucred(&uc, req, rsd);
1756         if (rc) {
1757                 mds_exit_ucred(&uc);
1758                 RETURN(rc);
1759         }
1760
1761         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1762         de = mds_id2dentry(obd, &body->id1, NULL);
1763         if (IS_ERR(de)) {
1764                 rc = PTR_ERR(de);
1765                 GOTO(out_pop, rc);
1766         }
1767
1768         rc = mds_getattr_pack_msg(req, de, offset);
1769         if (rc != 0) {
1770                 CERROR("mds_getattr_pack_msg: %d\n", rc);
1771                 l_dput(de);
1772                 GOTO(out_pop, rc);
1773         }
1774
1775         rc = mds_getattr_internal(obd, de, req, offset, body, 0, rsd);
1776         l_dput(de);
1777
1778         EXIT;
1779 out_pop:
1780         req->rq_status = rc;
1781         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1782         if (req->rq_reply_state == NULL)
1783                 lustre_pack_reply(req, 0, NULL, NULL);
1784         mds_exit_ucred(&uc);
1785         return 0;
1786 }
1787
1788 static int mds_access_check(struct ptlrpc_request *req, int offset)
1789 {
1790         struct obd_device *obd = req->rq_export->exp_obd;
1791         struct lvfs_run_ctxt saved;
1792         struct dentry *de;
1793         struct mds_req_sec_desc *rsd;
1794         struct mds_body *body;
1795         struct lvfs_ucred uc;
1796         int rep_size[2] = {sizeof(*body),
1797                            sizeof(struct mds_remote_perm)};
1798         int rc = 0;
1799         ENTRY;
1800
1801         if (!req->rq_export->exp_mds_data.med_remote) {
1802                 CERROR("from local client "LPU64"\n", req->rq_peer.peer_id.nid);
1803                 RETURN(-EINVAL);
1804         }
1805
1806         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1807         if (!rsd) {
1808                 CERROR("Can't unpack security desc\n");
1809                 RETURN(-EFAULT);
1810         }
1811
1812         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1813                                   lustre_swab_mds_body);
1814         if (body == NULL) {
1815                 CERROR ("Can't unpack body\n");
1816                 RETURN (-EFAULT);
1817         }
1818
1819         MD_COUNTER_INCREMENT(obd, access_check);
1820
1821         rc = mds_init_ucred(&uc, req, rsd);
1822         if (rc) {
1823                 CERROR("init ucred error: %d\n", rc);
1824                 RETURN(rc);
1825         }
1826         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1827
1828         de = mds_id2dentry(obd, &body->id1, NULL);
1829         if (IS_ERR(de)) {
1830                 CERROR("grab ino "LPU64": err %ld\n",
1831                        body->id1.li_stc.u.e3s.l3s_ino, PTR_ERR(de));
1832                 GOTO(out_pop, rc = PTR_ERR(de));
1833         }
1834
1835         rc = lustre_pack_reply(req, 2, rep_size, NULL);
1836         if (rc) {
1837                 CERROR("pack reply error: %d\n", rc);
1838                 GOTO(out_dput, rc = -EINVAL);
1839         }
1840
1841         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1842         LASSERT(body);
1843
1844         rc = mds_pack_remote_perm(req, 1, body, de->d_inode);
1845
1846         EXIT;
1847
1848 out_dput:
1849         l_dput(de);
1850 out_pop:
1851         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1852         mds_exit_ucred(&uc);
1853         return rc;
1854 }
1855
1856 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1857                           unsigned long max_age)
1858 {
1859         int rc;
1860         ENTRY;
1861
1862         spin_lock(&obd->obd_osfs_lock);
1863         rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
1864         if (rc == 0)
1865                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1866         spin_unlock(&obd->obd_osfs_lock);
1867
1868         RETURN(rc);
1869 }
1870
1871 static int mds_statfs(struct ptlrpc_request *req)
1872 {
1873         struct obd_device *obd = req->rq_export->exp_obd;
1874         int rc, size = sizeof(struct obd_statfs);
1875         ENTRY;
1876
1877         /* This will trigger a watchdog timeout */
1878         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
1879                          (MDS_SERVICE_WATCHDOG_TIMEOUT / 1000) + 1);
1880
1881         rc = lustre_pack_reply(req, 1, &size, NULL);
1882         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
1883                 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
1884                 GOTO(out, rc);
1885         }
1886
1887         OBD_COUNTER_INCREMENT(obd, statfs);
1888
1889         /* We call this so that we can cache a bit - 1 jiffie worth */
1890         rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
1891                             jiffies - HZ);
1892         if (rc) {
1893                 CERROR("mds_obd_statfs failed: rc %d\n", rc);
1894                 GOTO(out, rc);
1895         }
1896
1897         EXIT;
1898 out:
1899         req->rq_status = rc;
1900         return rc;
1901 }
1902
1903 static int mds_sync(struct ptlrpc_request *req, int offset)
1904 {
1905         struct obd_device *obd = req->rq_export->exp_obd;
1906         struct mds_obd *mds = &obd->u.mds;
1907         struct mds_body *body;
1908         int rc, size = sizeof(*body);
1909         ENTRY;
1910
1911         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1912                                   lustre_swab_mds_body);
1913         if (body == NULL)
1914                 GOTO(out, rc = -EPROTO);
1915
1916         rc = lustre_pack_reply(req, 1, &size, NULL);
1917         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
1918                 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
1919                 GOTO(out, rc);
1920         }
1921
1922         if (id_ino(&body->id1) == 0) {
1923                 /* an id of zero is taken to mean "sync whole filesystem" */
1924                 rc = fsfilt_sync(obd, mds->mds_sb);
1925                 if (rc)
1926                         GOTO(out, rc);
1927         } else {
1928                 /* just any file to grab fsync method - "file" arg unused */
1929                 struct file *file = mds->mds_rcvd_filp;
1930                 struct mds_body *rep_body;
1931                 struct dentry *de;
1932
1933                 de = mds_id2dentry(obd, &body->id1, NULL);
1934                 if (IS_ERR(de))
1935                         GOTO(out, rc = PTR_ERR(de));
1936
1937                 rc = file->f_op->fsync(NULL, de, 1);
1938                 if (rc)
1939                         GOTO(out, rc);
1940
1941                 rep_body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep_body));
1942                 mds_pack_inode2body(obd, rep_body, de->d_inode,
1943                                     (body->valid & OBD_MD_FID) ? 1 : 0);
1944                 l_dput(de);
1945         }
1946
1947         EXIT;
1948 out:
1949         req->rq_status = rc;
1950         return rc;
1951 }
1952
1953 /* mds_readpage does not take a DLM lock on the inode, because the client must
1954  * already have a PR lock.
1955  *
1956  * If we were to take another one here, a deadlock will result, if another
1957  * thread is already waiting for a PW lock. */
1958 static int mds_readpage(struct ptlrpc_request *req, int offset)
1959 {
1960         struct obd_device *obd = req->rq_export->exp_obd;
1961         struct vfsmount *mnt;
1962         struct dentry *de;
1963         struct file *file;
1964         struct mds_req_sec_desc *rsd;
1965         struct mds_body *body, *repbody;
1966         struct lvfs_run_ctxt saved;
1967         int rc, size = sizeof(*repbody);
1968         struct lvfs_ucred uc = {NULL, NULL,};
1969         ENTRY;
1970
1971         rc = lustre_pack_reply(req, 1, &size, NULL);
1972         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
1973                 CERROR("mds: out of memory\n");
1974                 GOTO(out, rc = -ENOMEM);
1975         }
1976
1977         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1978         if (!rsd) {
1979                 CERROR("Can't unpack security desc\n");
1980                 GOTO (out, rc = -EFAULT);
1981         }
1982
1983         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1984                                   lustre_swab_mds_body);
1985         if (body == NULL) {
1986                 CERROR("Can't unpack body\n");
1987                 GOTO (out, rc = -EFAULT);
1988         }
1989
1990         rc = mds_init_ucred(&uc, req, rsd);
1991         if (rc) {
1992                 mds_audit_auth(req, &uc, AUDIT_READDIR, &body->id1,
1993                                NULL, 0);
1994                 GOTO(out, rc);
1995         }
1996
1997         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1998         de = mds_id2dentry(obd, &body->id1, &mnt);
1999         if (IS_ERR(de))
2000                 GOTO(out_pop, rc = PTR_ERR(de));
2001
2002         CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
2003
2004         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
2005         /* note: in case of an error, dentry_open puts dentry */
2006         if (IS_ERR(file))
2007                 GOTO(out_pop, rc = PTR_ERR(file));
2008
2009         /* body->size is actually the offset -eeb */
2010         if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
2011                 CERROR("offset "LPU64" not on a block boundary of %lu\n",
2012                        body->size, de->d_inode->i_blksize);
2013                 GOTO(out_file, rc = -EFAULT);
2014         }
2015
2016         /* body->nlink is actually the #bytes to read -eeb */
2017         if (body->nlink & (de->d_inode->i_blksize - 1)) {
2018                 CERROR("size %u is not multiple of blocksize %lu\n",
2019                        body->nlink, de->d_inode->i_blksize);
2020                 GOTO(out_file, rc = -EFAULT);
2021         }
2022
2023         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
2024         repbody->size = file->f_dentry->d_inode->i_size;
2025         repbody->valid = OBD_MD_FLSIZE;
2026
2027         /* to make this asynchronous make sure that the handling function
2028            doesn't send a reply when this function completes. Instead a
2029            callback function would send the reply */
2030         /* body->size is actually the offset -eeb */
2031         rc = mds_sendpage(req, file, body->size, body->nlink);
2032
2033         EXIT;
2034 out_file:
2035         filp_close(file, 0);
2036 out_pop:
2037         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2038 out:
2039         mds_exit_ucred(&uc);
2040         req->rq_status = rc;
2041         return 0;
2042 }
2043
2044 int mds_read_md(struct obd_device *obd, struct lustre_id *id, 
2045                 char **data, int *datalen)
2046 {
2047         struct dentry *dentry;
2048         struct mds_obd *mds = &obd->u.mds;
2049         int rc = 0, mea = 0;
2050         char *ea;
2051         ENTRY;
2052
2053         LASSERT(id);
2054         LASSERT(obd);
2055         
2056         dentry = mds_id2dentry(obd, id, NULL);
2057         if (IS_ERR(dentry))
2058                 GOTO(out, rc = PTR_ERR(dentry));
2059
2060         if (!dentry->d_inode) {
2061                 CERROR("Can't find object "DLID4".\n",
2062                        OLID4(id));
2063                 GOTO(out_dentry, rc = -EINVAL);
2064         }
2065         if (S_ISDIR(dentry->d_inode->i_mode)) {
2066                 *datalen = obd_packmd(mds->mds_md_exp, NULL, NULL);
2067                 mea = 1; 
2068         } else {
2069                 *datalen = obd_packmd(mds->mds_dt_exp, NULL, NULL); 
2070                 mea = 0;
2071         }
2072         OBD_ALLOC(ea, *datalen);
2073         if (!ea) {
2074                 *datalen = 0;
2075                 GOTO(out_dentry, rc = PTR_ERR(dentry));
2076         } 
2077         *data = ea;
2078         down(&dentry->d_inode->i_sem);
2079         rc = fsfilt_get_md(obd, dentry->d_inode, *data, *datalen,
2080                            (mea ? EA_MEA : EA_LOV));
2081         up(&dentry->d_inode->i_sem);
2082         
2083         if (rc < 0) 
2084                 CERROR("Error %d reading eadata for ino %lu\n",
2085                         rc, dentry->d_inode->i_ino);
2086 out_dentry:
2087         l_dput(dentry);
2088 out:
2089         RETURN(rc);
2090 }
2091 EXPORT_SYMBOL(mds_read_md);
2092
2093 int mds_reint(struct ptlrpc_request *req, int offset,
2094               struct lustre_handle *lockh)
2095 {
2096         struct mds_update_record *rec;
2097         struct mds_req_sec_desc *rsd;
2098         int rc;
2099         ENTRY;
2100
2101         OBD_ALLOC(rec, sizeof(*rec));
2102         if (rec == NULL)
2103                 RETURN(-ENOMEM);
2104
2105         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
2106         if (!rsd) {
2107                 CERROR("Can't unpack security desc\n");
2108                 GOTO(out, rc = -EFAULT);
2109         }
2110
2111         rc = mds_update_unpack(req, offset, rec);
2112         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
2113                 CERROR("invalid record\n");
2114                 GOTO(out, req->rq_status = -EINVAL);
2115         }
2116
2117         rc = mds_init_ucred(&rec->ur_uc, req, rsd);
2118         if (rc) {
2119                 mds_audit_reint(req, rec);
2120                 GOTO(out, rc);
2121         }
2122
2123         /* rc will be used to interrupt a for loop over multiple records */
2124         rc = mds_reint_rec(rec, offset, req, lockh);
2125
2126 out:
2127         mds_exit_ucred(&rec->ur_uc);
2128         OBD_FREE(rec, sizeof(*rec));
2129         RETURN(rc);
2130 }
2131
2132 static int mds_filter_recovery_request(struct ptlrpc_request *req,
2133                                        struct obd_device *obd, int *process)
2134 {
2135         switch (req->rq_reqmsg->opc) {
2136         case MDS_CONNECT: /* This will never get here, but for completeness. */
2137         case OST_CONNECT: /* This will never get here, but for completeness. */
2138         case MDS_DISCONNECT:
2139         case OST_DISCONNECT:
2140                *process = 1;
2141                RETURN(0);
2142
2143         case MDS_CLOSE:
2144         case MDS_SYNC: /* used in unmounting */
2145         case OBD_PING:
2146         case MDS_REINT:
2147         case LDLM_ENQUEUE:
2148         case OST_CREATE:
2149                 *process = target_queue_recovery_request(req, obd);
2150                 RETURN(0);
2151
2152         default:
2153                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2154                 *process = 0;
2155                 /* XXX what should we set rq_status to here? */
2156                 req->rq_status = -EAGAIN;
2157                 RETURN(ptlrpc_error(req));
2158         }
2159 }
2160
2161 static char *reint_names[] = {
2162         [REINT_SETATTR] "setattr",
2163         [REINT_CREATE]  "create",
2164         [REINT_LINK]    "link",
2165         [REINT_UNLINK]  "unlink",
2166         [REINT_RENAME]  "rename",
2167         [REINT_OPEN]    "open",
2168 };
2169
2170 #define FILTER_VALID_FLAGS (OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLGENER  | \
2171                             OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ| \
2172                             OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME| \
2173                             OBD_MD_FLID) 
2174
2175 static void reconstruct_create(struct ptlrpc_request *req)
2176 {
2177         struct mds_export_data *med = &req->rq_export->exp_mds_data;
2178         struct mds_client_data *mcd = med->med_mcd;
2179         struct dentry *dentry;
2180         struct ost_body *body;
2181         struct lustre_id id;
2182         int rc;
2183         ENTRY;
2184
2185         /* copy rc, transno and disp; steal locks */
2186         mds_req_from_mcd(req, mcd);
2187         if (req->rq_status) {
2188                 EXIT;
2189                 return;
2190         }
2191
2192         id_gen(&id) = 0;
2193         id_group(&id) = 0;
2194
2195         id_ino(&id) = mcd->mcd_last_data;
2196         LASSERT(id_ino(&id) != 0);
2197
2198         dentry = mds_id2dentry(req2obd(req), &id, NULL);
2199         if (IS_ERR(dentry)) {
2200                 CERROR("can't find inode "LPU64"\n", id_ino(&id));
2201                 req->rq_status = PTR_ERR(dentry);
2202                 EXIT;
2203                 return;
2204         }
2205
2206         CWARN("reconstruct reply for x"LPU64" (remote ino) "LPU64" -> %lu/%u\n",
2207               req->rq_xid, id_ino(&id), dentry->d_inode->i_ino,
2208               dentry->d_inode->i_generation);
2209
2210         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
2211         obdo_from_inode(&body->oa, dentry->d_inode, FILTER_VALID_FLAGS);
2212         body->oa.o_id = dentry->d_inode->i_ino;
2213         body->oa.o_generation = dentry->d_inode->i_generation;
2214         body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
2215
2216         down(&dentry->d_inode->i_sem);
2217         rc = mds_read_inode_sid(req2obd(req), dentry->d_inode, &id);
2218         up(&dentry->d_inode->i_sem);
2219         if (rc) {
2220                 CERROR("Can't read inode self id, inode %lu, "
2221                        "rc %d\n", dentry->d_inode->i_ino, rc);
2222                 id_fid(&id) = 0;
2223         }
2224
2225         body->oa.o_fid = id_fid(&id);
2226         body->oa.o_mds = id_group(&id);
2227         l_dput(dentry);
2228
2229         EXIT;
2230 }
2231
2232 static int mds_inode_init_acl(struct obd_device *obd, void *handle,
2233                               struct dentry *de, void *xattr, int xattr_size)
2234 {
2235         struct inode *inode = de->d_inode;
2236         struct posix_acl *acl;
2237         mode_t mode;
2238         int rc = 0;
2239
2240         LASSERT(handle);
2241         LASSERT(inode);
2242         LASSERT(xattr);
2243         LASSERT(xattr_size > 0);
2244
2245         if (!inode->i_op->getxattr || !inode->i_op->setxattr) {
2246                 CERROR("backend fs dosen't support xattr\n");
2247                 return -EOPNOTSUPP;
2248         }
2249
2250         /* set default acl */
2251         if (S_ISDIR(inode->i_mode)) {
2252                 rc = inode->i_op->setxattr(de, XATTR_NAME_ACL_DEFAULT,
2253                                            xattr, xattr_size, 0);
2254                 if (rc) {
2255                         CERROR("set default acl err: %d\n", rc);
2256                         return rc;
2257                 }
2258         }
2259
2260         /* set access acl */
2261         acl = posix_acl_from_xattr(xattr, xattr_size);
2262         if (acl == NULL || IS_ERR(acl)) {
2263                 CERROR("insane attr data\n");
2264                 return PTR_ERR(acl);
2265         }
2266
2267         if (posix_acl_valid(acl)) {
2268                 CERROR("default acl not valid: %d\n", rc);
2269                 rc = -EFAULT;
2270                 goto out;
2271         }
2272
2273         mode = inode->i_mode;
2274         rc = posix_acl_create_masq(acl, &mode);
2275         if (rc < 0) {
2276                 CERROR("create masq err %d\n", rc);
2277                 goto out;
2278         }
2279
2280         if (inode->i_mode != mode) {
2281                 struct iattr iattr = { .ia_valid = ATTR_MODE,
2282                                        .ia_mode = mode };
2283                 int rc2;
2284
2285                 rc2 = fsfilt_setattr(obd, de, handle, &iattr, 0);
2286                 if (rc2) {
2287                         CERROR("setattr mode err: %d\n", rc2);
2288                         rc = rc2;
2289                         goto out;
2290                 }
2291         }
2292
2293         if (rc > 0) {
2294                 /* we didn't change acl except mode bits of some
2295                  * entries, so should be fit into original size.
2296                  */
2297                 rc = posix_acl_to_xattr(acl, xattr, xattr_size);
2298                 LASSERT(rc > 0);
2299
2300                 rc = inode->i_op->setxattr(de, XATTR_NAME_ACL_ACCESS,
2301                                            xattr, xattr_size, 0);
2302                 if (rc)
2303                         CERROR("set access acl err: %d\n", rc);
2304         }
2305 out:
2306         posix_acl_release(acl);
2307         return rc;
2308 }
2309
2310 static int mdt_obj_create(struct ptlrpc_request *req)
2311 {
2312         struct obd_device *obd = req->rq_export->exp_obd;
2313         struct mds_obd *mds = &obd->u.mds;
2314         struct ost_body *body, *repbody;
2315         void *acl = NULL;
2316         int acl_size;
2317         char idname[LL_ID_NAMELEN];
2318         int size = sizeof(*repbody);
2319         struct inode *parent_inode;
2320         struct lvfs_run_ctxt saved;
2321         int rc, cleanup_phase = 0;
2322         struct dentry *new = NULL;
2323         struct dentry_params dp;
2324         int mealen, flags = 0;
2325         struct lvfs_ucred uc;
2326         struct lustre_id id;
2327         struct mea *mea;
2328         void *handle = NULL;
2329         unsigned long cr_inum = 0;
2330         __u64 fid = 0;
2331         ENTRY;
2332        
2333         DEBUG_REQ(D_HA, req, "create remote object");
2334         parent_inode = mds->mds_unnamed_dir->d_inode;
2335
2336         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
2337                                   lustre_swab_ost_body);
2338         if (body == NULL)
2339                 RETURN(-EFAULT);
2340
2341         /* acl data is packed transparently, no swab here */
2342         LASSERT(req->rq_reqmsg->bufcount >= 2);
2343         acl_size = req->rq_reqmsg->buflens[1];
2344         if (acl_size) {
2345                 acl = lustre_msg_buf(req->rq_reqmsg, 1, acl_size);
2346                 if (!acl) {
2347                         CERROR("No default acl buf?\n");
2348                         RETURN(-EFAULT);
2349                 }
2350         }
2351
2352         rc = lustre_pack_reply(req, 1, &size, NULL);
2353         if (rc)
2354                 RETURN(rc);
2355
2356         MDS_CHECK_RESENT(req, reconstruct_create(req));
2357
2358         uc.luc_lsd = NULL;
2359         uc.luc_ginfo = NULL;
2360         uc.luc_uid = body->oa.o_uid;
2361         uc.luc_gid = body->oa.o_gid;
2362         uc.luc_fsuid = body->oa.o_uid;
2363         uc.luc_fsgid = body->oa.o_gid;
2364
2365         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2366         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
2367
2368         /* in REPLAY case inum should be given (client or other MDS fills it) */
2369         if (body->oa.o_id && ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2370             (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))) {
2371                 /*
2372                  * this is re-create request from MDS holding directory name.
2373                  * we have to lookup given ino/gen first. if it exists (good
2374                  * case) then there is nothing to do. if it does not then we
2375                  * have to recreate it.
2376                  */
2377                 id_ino(&id) = body->oa.o_id;
2378                 id_gen(&id) = body->oa.o_generation;
2379  
2380                 new = mds_id2dentry(obd, &id, NULL);
2381                 if (!IS_ERR(new) && new->d_inode) {
2382                         struct lustre_id sid;
2383                                 
2384                         CDEBUG(D_OTHER, "mkdir repairing %lu/%lu\n",
2385                                (unsigned long)id_ino(&id),
2386                                (unsigned long)id_gen(&id));
2387                         
2388                         obdo_from_inode(&repbody->oa, new->d_inode,
2389                                         FILTER_VALID_FLAGS);
2390                         
2391                         repbody->oa.o_id = new->d_inode->i_ino;
2392                         repbody->oa.o_generation = new->d_inode->i_generation;
2393                         repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
2394                         cleanup_phase = 1;
2395
2396                         down(&new->d_inode->i_sem);
2397                         rc = mds_read_inode_sid(obd, new->d_inode, &sid);
2398                         up(&new->d_inode->i_sem);
2399                         if (rc) {
2400                                 CERROR("Can't read inode self id "
2401                                        "inode %lu, rc %d.\n",
2402                                        new->d_inode->i_ino, rc);
2403                                 GOTO(cleanup, rc);
2404                         }
2405
2406                         repbody->oa.o_fid = id_fid(&sid);
2407                         repbody->oa.o_mds = id_group(&sid);
2408                         LASSERT(id_fid(&sid) != 0);
2409
2410                         /* 
2411                          * here we could use fid passed in body->oa.o_fid and
2412                          * thus avoid mds_read_inode_sid().
2413                          */
2414                         cr_inum = new->d_inode->i_ino;
2415                         GOTO(cleanup, rc = 0);
2416                 }
2417         }
2418         
2419         down(&parent_inode->i_sem);
2420         handle = fsfilt_start(obd, parent_inode, FSFILT_OP_MKDIR, NULL);
2421         if (IS_ERR(handle)) {
2422                 up(&parent_inode->i_sem);
2423                 CERROR("fsfilt_start() failed, rc = %d\n",
2424                        (int)PTR_ERR(handle));
2425                 GOTO(cleanup, rc = PTR_ERR(handle));
2426         }
2427         cleanup_phase = 1; /* transaction */
2428
2429 repeat:
2430         rc = sprintf(idname, "%u.%u", ll_insecure_random_int(), current->pid);
2431         new = lookup_one_len(idname, mds->mds_unnamed_dir, rc);
2432         if (IS_ERR(new)) {
2433                 CERROR("%s: can't lookup new inode (%s) for mkdir: %d\n",
2434                        obd->obd_name, idname, (int) PTR_ERR(new));
2435                 fsfilt_commit(obd, mds->mds_sb, new->d_inode, handle, 0);
2436                 up(&parent_inode->i_sem);
2437                 RETURN(PTR_ERR(new));
2438         } else if (new->d_inode) {
2439                 CERROR("%s: name exists. repeat\n", obd->obd_name);
2440                 goto repeat;
2441         }
2442         if ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2443              lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2444                 fid = body->oa.o_fid;
2445         } else { 
2446                 fid = mds_alloc_fid(obd);
2447         }
2448         new->d_fsdata = (void *)&dp;
2449         dp.p_inum = 0;
2450         dp.p_ptr = req;
2451         
2452         dp.p_fid = fid;
2453         dp.p_group = mds->mds_num;
2454
2455         if ((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) ||
2456             (body->oa.o_flags & OBD_FL_RECREATE_OBJS)) {
2457                 LASSERT(body->oa.o_id != 0);
2458                 dp.p_inum = body->oa.o_id;
2459                 DEBUG_REQ(D_HA, req, "replay create obj %lu/%lu",
2460                           (unsigned long)body->oa.o_id,
2461                           (unsigned long)body->oa.o_generation);
2462         }
2463
2464         rc = vfs_mkdir(parent_inode, new, body->oa.o_mode);
2465         if (rc == 0) {
2466                 if (acl) {
2467                         rc = mds_inode_init_acl(obd, handle, new,
2468                                                 acl, acl_size);
2469                         if (rc) {
2470                                 up(&parent_inode->i_sem);
2471                                 GOTO(cleanup, rc);
2472                         }
2473                 }
2474                 if ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2475                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2476                         new->d_inode->i_generation = body->oa.o_generation;
2477                         mark_inode_dirty(new->d_inode);
2478                         
2479                         /*
2480                          * avoiding asserts in cache flush case, as
2481                          * @body->oa.o_id should be zero.
2482                          */
2483                         if (body->oa.o_id) {
2484                                 LASSERTF(body->oa.o_id == new->d_inode->i_ino, 
2485                                          "BUG 3550: failed to recreate obj "
2486                                          LPU64" -> %lu\n", body->oa.o_id,
2487                                          new->d_inode->i_ino);
2488                                 
2489                                 LASSERTF(body->oa.o_generation == 
2490                                          new->d_inode->i_generation,
2491                                          "BUG 3550: failed to recreate obj/gen "
2492                                          LPU64"/%u -> %lu/%u\n", body->oa.o_id,
2493                                          body->oa.o_generation,
2494                                          new->d_inode->i_ino, 
2495                                          new->d_inode->i_generation);
2496                         }
2497                 }
2498                 
2499                 obdo_from_inode(&repbody->oa, new->d_inode, FILTER_VALID_FLAGS);
2500                 repbody->oa.o_id = new->d_inode->i_ino;
2501                 repbody->oa.o_generation = new->d_inode->i_generation;
2502                 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FID;
2503
2504                 if ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2505                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2506                         LASSERT(body->oa.o_id != 0);
2507                         LASSERT(body->oa.o_fid != 0);
2508                 }
2509                 
2510                 mds_inode2id(obd, &id, new->d_inode, fid);
2511                 mds_update_inode_ids(obd, new->d_inode, handle, &id, 
2512                                      body->oa.o_valid & OBD_MD_FLID ?
2513                                      NULL : obdo_id(&body->oa));
2514
2515                 /* initializing o_fid after it is allocated. */
2516                 repbody->oa.o_fid = id_fid(&id);
2517                 repbody->oa.o_mds = id_group(&id);
2518
2519                 rc = fsfilt_del_dir_entry(obd, new);
2520                 up(&parent_inode->i_sem);
2521                 if (rc) {
2522                         CERROR("can't remove name for object: %d\n", rc);
2523                         GOTO(cleanup, rc);
2524                 }
2525                 
2526                 cleanup_phase = 2; /* created directory object */
2527
2528                 CDEBUG(D_OTHER, "created dirobj: %lu/%lu mode %o\n",
2529                        (unsigned long)new->d_inode->i_ino,
2530                        (unsigned long)new->d_inode->i_generation,
2531                        (unsigned)new->d_inode->i_mode);
2532                 cr_inum = new->d_inode->i_ino;
2533         } else {
2534                 up(&parent_inode->i_sem);
2535                 CERROR("%s: can't create dirobj: %d\n", obd->obd_name, rc);
2536                 GOTO(cleanup, rc);
2537         }
2538
2539         if (body->oa.o_valid & OBD_MD_FLID) {
2540                 /* this is new object for splitted dir. We have to prevent
2541                  * recursive splitting on it -bzzz */
2542                 mealen = obd_size_diskmd(mds->mds_md_exp, NULL);
2543
2544                 OBD_ALLOC(mea, mealen);
2545                 if (mea == NULL)
2546                         GOTO(cleanup, rc = -ENOMEM);
2547
2548                 mea->mea_magic = MEA_MAGIC_ALL_CHARS;
2549                 mea->mea_master = body->oa.o_mds;      /* master mds num */
2550                 mea->mea_count = 0;
2551                 
2552                 obdo2id(&mea->mea_ids[body->oa.o_mds], &body->oa);
2553
2554                 down(&new->d_inode->i_sem);
2555                 rc = fsfilt_set_md(obd, new->d_inode, handle,
2556                                    mea, mealen, EA_MEA);
2557                 up(&new->d_inode->i_sem);
2558                 if (rc)
2559                         CERROR("fsfilt_set_md() failed, "
2560                                "rc = %d\n", rc);
2561
2562                 OBD_FREE(mea, mealen);
2563                 
2564                 CDEBUG(D_OTHER, "%s: mark non-splittable %lu/%u - %d\n",
2565                        obd->obd_name, new->d_inode->i_ino,
2566                        new->d_inode->i_generation, flags);
2567         } else if (body->oa.o_easize) {
2568                 /* we pass LCK_EX to split routine to signal that we have
2569                  * exclusive access to the directory. simple because nobody
2570                  * knows it already exists -bzzz */
2571                 rc = mds_try_to_split_dir(obd, new, NULL,
2572                                           body->oa.o_easize, LCK_EX);
2573                 if (rc < 0) {
2574                         CERROR("Can't split directory %lu, error = %d.\n",
2575                                new->d_inode->i_ino, rc);
2576                 } else {
2577                         rc = 0;
2578                 }
2579         }
2580
2581         EXIT;
2582 cleanup:
2583         if (rc == 0 && (body->oa.o_flags & OBD_FL_REINT)) {
2584                 rc = mds_fidmap_add(obd, &id);
2585                 if (rc < 0) {
2586                         CERROR("can't create fid->ino mapping, "
2587                                "err %d\n", rc);
2588                 } else {
2589                         rc = 0;
2590                 }
2591         }
2592                 
2593         switch (cleanup_phase) {
2594         case 2: /* object has been created, but we'll may want to replay it later */
2595                 if (rc == 0)
2596                         ptlrpc_require_repack(req);
2597         case 1: /* transaction */
2598                 rc = mds_finish_transno(mds, parent_inode, handle,
2599                                         req, rc, cr_inum);
2600         }
2601
2602         l_dput(new);
2603         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2604         return rc;
2605 }
2606
2607 static int mdt_get_info(struct ptlrpc_request *req)
2608 {
2609         struct obd_export *exp = req->rq_export;
2610         int keylen, rc = 0;
2611         char *key;
2612         ENTRY;
2613
2614         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
2615         if (key == NULL) {
2616                 DEBUG_REQ(D_HA, req, "no get_info key");
2617                 RETURN(-EFAULT);
2618         }
2619         keylen = req->rq_reqmsg->buflens[0];
2620
2621         if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
2622             (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
2623             (keylen < strlen("lovdesc") || strcmp(key, "lovdesc") != 0) &&
2624             (keylen < strlen("getext") || strcmp(key, "getext") != 0) &&
2625             (keylen < strlen("rootid") || strcmp(key, "rootid") != 0))
2626                 RETURN(-EPROTO);
2627
2628         if (keylen >= strlen("rootid") && !strcmp(key, "rootid")) {
2629                 struct lustre_id *reply;
2630                 int size = sizeof(*reply);
2631                 
2632                 rc = lustre_pack_reply(req, 1, &size, NULL);
2633                 if (rc)
2634                         RETURN(rc);
2635
2636                 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
2637                 rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
2638         } else if (keylen >= strlen("lovdesc") && !strcmp(key, "lovdesc")) {
2639                 struct lov_desc *reply;
2640                 int size = sizeof(*reply);
2641                 
2642                 rc = lustre_pack_reply(req, 1, &size, NULL);
2643                 if (rc)
2644                         RETURN(rc);
2645
2646                 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
2647                 rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
2648         } else if (keylen >= strlen("getext") && !strcmp(key, "getext")) {
2649                 struct fid_extent *reply;
2650                 int size = sizeof(*reply);
2651                 
2652                 rc = lustre_pack_reply(req, 1, &size, NULL);
2653                 if (rc)
2654                         RETURN(rc);
2655
2656                 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
2657                 rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
2658         } else {
2659                 obd_id *reply;
2660                 int size = sizeof(*reply);
2661                 
2662                 rc = lustre_pack_reply(req, 1, &size, NULL);
2663                 if (rc)
2664                         RETURN(rc);
2665
2666                 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
2667                 rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
2668         }
2669
2670         req->rq_repmsg->status = 0;
2671         RETURN(rc);
2672 }
2673
2674 static int mds_set_info(struct obd_export *exp, __u32 keylen,
2675                         void *key, __u32 vallen, void *val)
2676 {
2677         struct obd_device *obd;
2678         struct mds_obd *mds;
2679         int rc = 0;
2680         ENTRY;
2681
2682         obd = class_exp2obd(exp);
2683         if (obd == NULL) {
2684                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2685                        exp->exp_handle.h_cookie);
2686                 RETURN(-EINVAL);
2687         }
2688
2689         mds = &obd->u.mds;
2690         if (keylen == 5 && memcmp(key, "audit", 5) == 0) {
2691                 rc = mds_set_audit(obd, val);
2692                 RETURN(rc);
2693         } else if (keylen >= strlen("ids") && memcmp(key, "ids", keylen) == 0) {
2694                 struct lustre_id *ids = (struct lustre_id *)val;
2695                 struct dentry *de;
2696                 struct inode *inode;
2697                 void *handle;
2698                 int err;
2699
2700                 de = mds_id2dentry(obd, ids, NULL);
2701                 if (IS_ERR(de)) {
2702                         rc = PTR_ERR(de);
2703                         CERROR("lookup by an id error rc=%d\n ", rc);
2704                         RETURN(rc);
2705                 }
2706                 inode = de->d_inode;
2707                 if (inode == NULL)
2708                         GOTO(out_put, rc = -ENOENT);
2709                 
2710                 down(&inode->i_sem);
2711                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
2712                 if (IS_ERR(handle)) {
2713                         up(&inode->i_sem);
2714                         GOTO(out_put, rc = PTR_ERR(handle));
2715                 }
2716
2717                 rc = mds_update_inode_ids(obd, inode, handle, NULL, ids + 1);
2718                 
2719                 err = fsfilt_commit(obd, mds->mds_sb, inode, handle, 
2720                                     exp->exp_sync);
2721                 if (err) {
2722                         CERROR("error committing transaction: %d\n", err);
2723                         if (!rc) rc = err;
2724                 }
2725                 up(&inode->i_sem);
2726 out_put:
2727                 l_dput(de);
2728                 RETURN(rc);
2729         }
2730
2731         if (keylen >= strlen("crypto_type") &&
2732              memcmp(key, "crypto_type", keylen) == 0) {
2733                 rc = mds_set_crypto_type(obd, val, vallen); 
2734                 RETURN(rc);
2735         }
2736         
2737         if (keylen >= strlen("setext") && !memcmp(key, "setext", keylen)) {
2738                 struct fid_extent *ext = val;
2739
2740                 CDEBUG(D_IOCTL, "set last fid to extent ["LPD64"-"LPD64"]\n",
2741                        ext->fe_start, ext->fe_width);
2742
2743                 /* set lastfid into fid extent start. All next object creates
2744                  * will use that fid. */
2745                 mds_set_last_fid(obd, ext->fe_start);
2746
2747                 /* setting the same extent to OSC to avoid ids intersecting in
2748                  * object ids, as all cache MDSs have the same group 0. */
2749                 rc = obd_set_info(mds->mds_dt_exp, strlen("setext"),
2750                                   "setext", sizeof(*ext), ext);
2751                 if (rc) {
2752                         CERROR("can't set extent ["LPD64"-"LPD64"] to %s, "
2753                                "err %d\n", ext->fe_start, ext->fe_width,
2754                                mds->mds_dt_exp->exp_obd->obd_name, rc);
2755                 }
2756                 RETURN(rc);
2757         }
2758         CDEBUG(D_IOCTL, "invalid key\n");
2759         RETURN(-EINVAL);
2760 }
2761
2762 static int mdt_set_info(struct ptlrpc_request *req)
2763 {
2764         char *key, *val;
2765         struct obd_export *exp = req->rq_export;
2766         int keylen, rc = 0, vallen = 0;
2767         ENTRY;
2768
2769         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
2770         if (key == NULL) {
2771                 DEBUG_REQ(D_HA, req, "no set_info key");
2772                 RETURN(-EFAULT);
2773         }
2774         keylen = req->rq_reqmsg->buflens[0];
2775
2776         if ((keylen == strlen("crypto_type") &&
2777             memcmp(key, "crypto_type", keylen) == 0)) {
2778                 rc = lustre_pack_reply(req, 0, NULL, NULL);
2779                 if (rc)
2780                         RETURN(rc);
2781                 
2782                 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
2783                 vallen = req->rq_reqmsg->buflens[1];
2784
2785                 rc = obd_set_info(exp, keylen, key, vallen, val);
2786                 req->rq_repmsg->status = 0;
2787                 RETURN(rc);
2788         } else if (keylen == 5 && memcmp(key, "audit", 5) == 0) {
2789                 struct audit_attr_msg msg, *p;
2790                 int rc = 0;
2791
2792                 rc = lustre_pack_reply(req, 0, NULL, NULL);
2793                 if (rc)
2794                         RETURN(rc);
2795                 
2796                 p = lustre_swab_reqbuf(req, 1, sizeof(msg),
2797                                        lustre_swab_audit_attr);
2798
2799                 msg = *p;
2800                 //CDEBUG(D_INFO, "Get new audit setting 0x%x\n", (__u32)msg.attr);
2801                 rc = obd_set_info(exp, keylen, key, sizeof(msg), &msg);
2802
2803                 req->rq_repmsg->status = rc;
2804                 RETURN(rc);
2805         } else if (keylen == strlen("ids") &&
2806                    memcmp(key, "ids", keylen) == 0) {
2807                 struct lustre_id *id, ids[2];
2808                 
2809                 rc = lustre_pack_reply(req, 0, NULL, NULL);
2810                 if (rc)
2811                         RETURN(rc);
2812                 id = lustre_swab_reqbuf(req, 1, sizeof(struct lustre_id), 
2813                                         lustre_swab_lustre_id);
2814                 ids[0] = *id;
2815                 id = lustre_swab_reqbuf(req, 2, sizeof(struct lustre_id),
2816                                         lustre_swab_lustre_id);
2817                 ids[1] = *id;
2818
2819                 rc = obd_set_info(exp, keylen, key, vallen, ids);
2820                 req->rq_repmsg->status = rc;
2821                 RETURN(rc);
2822         } else if (keylen == strlen("setext") && 
2823                    memcmp(key, "setext", keylen) == 0) {
2824                 rc = lustre_pack_reply(req, 0, NULL, NULL);
2825                 if (rc)
2826                         RETURN(rc);
2827                 
2828                 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
2829                 vallen = req->rq_reqmsg->buflens[1];
2830
2831                 rc = obd_set_info(exp, keylen, key, vallen, val);
2832                 req->rq_repmsg->status = 0;
2833                 RETURN(rc);
2834         }
2835
2836         CDEBUG(D_IOCTL, "invalid key\n");
2837         RETURN(-EINVAL);
2838 }
2839
2840 static void mds_revoke_export_locks(struct obd_export *exp)
2841 {
2842         struct list_head *locklist = &exp->exp_ldlm_data.led_held_locks;
2843         struct list_head  rpc_list;
2844         struct ldlm_lock *lock, *next;
2845         struct ldlm_lock_desc desc;
2846
2847         /* don't do this for local client */
2848         if (!exp->u.eu_mds_data.med_remote)
2849                 return;
2850
2851         /* don't revoke locks during recovery */
2852         if (exp->exp_obd->obd_recovering)
2853                 return;
2854
2855         ENTRY;
2856         INIT_LIST_HEAD(&rpc_list);
2857
2858         spin_lock(&exp->exp_ldlm_data.led_lock);
2859         list_for_each_entry_safe(lock, next, locklist, l_export_chain) {
2860
2861                 lock_res_and_lock(lock);
2862                 if (lock->l_req_mode != lock->l_granted_mode) {
2863                         unlock_res_and_lock(lock);
2864                         continue;
2865                 }
2866
2867                 LASSERT(lock->l_resource);
2868                 if (lock->l_resource->lr_type != LDLM_IBITS &&
2869                     lock->l_resource->lr_type != LDLM_PLAIN) {
2870                         unlock_res_and_lock(lock);
2871                         continue;
2872                 }
2873
2874                 if (lock->l_flags & LDLM_FL_AST_SENT) {
2875                         unlock_res_and_lock(lock);
2876                         continue;
2877                 }
2878
2879                 LASSERT(lock->l_blocking_ast);
2880                 LASSERT(!lock->l_blocking_lock);
2881
2882                 lock->l_flags |= LDLM_FL_AST_SENT;
2883                 unlock_res_and_lock(lock);
2884
2885                 list_move(&lock->l_export_chain, &rpc_list);
2886         }
2887         spin_unlock(&exp->exp_ldlm_data.led_lock);
2888
2889         while (!list_empty(&rpc_list)) {
2890                 lock = list_entry(rpc_list.next, struct ldlm_lock,
2891                                   l_export_chain);
2892                 list_del_init(&lock->l_export_chain);
2893
2894                 /* the desc just pretend to exclusive */
2895                 ldlm_lock2desc(lock, &desc);
2896                 desc.l_req_mode = LCK_EX;
2897                 desc.l_granted_mode = 0;
2898
2899                 lock->l_blocking_ast(lock, &desc, NULL, LDLM_CB_BLOCKING);
2900         }
2901         EXIT;
2902 }
2903
2904 static int mds_msg_check_version(struct lustre_msg *msg)
2905 {
2906         int rc;
2907
2908         switch (msg->opc) {
2909         case MDS_CONNECT:
2910         case MDS_DISCONNECT:
2911         case OBD_PING:
2912                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2913                 if (rc)
2914                         CERROR("bad opc %u version %08x, expecting %08x\n",
2915                                msg->opc, msg->version, LUSTRE_OBD_VERSION);
2916                 break;
2917         case MDS_STATFS:
2918         case MDS_GETSTATUS:
2919         case MDS_GETATTR:
2920         case MDS_GETATTR_LOCK:
2921         case MDS_ACCESS_CHECK:
2922         case MDS_READPAGE:
2923         case MDS_REINT:
2924         case MDS_CLOSE:
2925         case MDS_DONE_WRITING:
2926         case MDS_PIN:
2927         case MDS_SYNC:
2928         case MDS_PARSE_ID:
2929                 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2930                 if (rc)
2931                         CERROR("bad opc %u version %08x, expecting %08x\n",
2932                                msg->opc, msg->version, LUSTRE_MDS_VERSION);
2933                 break;
2934         case LDLM_ENQUEUE:
2935         case LDLM_CONVERT:
2936         case LDLM_BL_CALLBACK:
2937         case LDLM_CP_CALLBACK:
2938         case LDLM_FLK_DEADLOCK_CHK:
2939                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2940                 if (rc)
2941                         CERROR("bad opc %u version %08x, expecting %08x\n",
2942                                msg->opc, msg->version, LUSTRE_DLM_VERSION);
2943                 break;
2944         case OBD_LOG_CANCEL:
2945         case LLOG_ORIGIN_HANDLE_OPEN:
2946         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2947         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2948         case LLOG_ORIGIN_HANDLE_READ_HEADER:
2949         case LLOG_ORIGIN_HANDLE_CLOSE:
2950         case LLOG_CATINFO:
2951                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2952                 if (rc)
2953                         CERROR("bad opc %u version %08x, expecting %08x\n",
2954                                msg->opc, msg->version, LUSTRE_LOG_VERSION);
2955                 break;
2956         case OST_CREATE:
2957         case OST_WRITE:
2958         case OST_GET_INFO:
2959         case OST_SET_INFO:
2960                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2961                 if (rc)
2962                         CERROR("bad opc %u version %08x, expecting %08x\n",
2963                                msg->opc, msg->version, LUSTRE_OBD_VERSION);
2964                 break;
2965         case SEC_INIT:
2966         case SEC_INIT_CONTINUE:
2967         case SEC_FINI:
2968                 rc = 0;
2969                 break;
2970         default:
2971                 CERROR("MDS unknown opcode %d\n", msg->opc);
2972                 rc = -ENOTSUPP;
2973                 break;
2974         }
2975
2976         return rc;
2977 }
2978
2979 int mds_handle(struct ptlrpc_request *req)
2980 {
2981         int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
2982         struct obd_device *obd = NULL;
2983         struct mds_obd *mds = NULL; /* quell gcc overwarning */
2984         int rc = 0;
2985         ENTRY;
2986
2987         OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
2988
2989         rc = mds_msg_check_version(req->rq_reqmsg);
2990         if (rc) {
2991                 CERROR("MDS drop mal-formed request\n");
2992                 RETURN(rc);
2993         }
2994
2995         /* Security opc should NOT trigger any recovery events */
2996         if (req->rq_reqmsg->opc == SEC_INIT ||
2997             req->rq_reqmsg->opc == SEC_INIT_CONTINUE) {
2998                 if (req->rq_export) {
2999                         mds_req_add_idmapping(req,
3000                                               &req->rq_export->exp_mds_data);
3001                         mds_revoke_export_locks(req->rq_export);
3002                 }
3003                 GOTO(out, rc = 0);
3004         } else if (req->rq_reqmsg->opc == SEC_FINI) {
3005                 if (req->rq_export) {
3006                         mds_req_del_idmapping(req,
3007                                               &req->rq_export->exp_mds_data);
3008                         mds_revoke_export_locks(req->rq_export);
3009                 }
3010                 GOTO(out, rc = 0);
3011         }
3012
3013         LASSERT(current->journal_info == NULL);
3014         /* XXX identical to OST */
3015         if (req->rq_reqmsg->opc != MDS_CONNECT) {
3016                 struct mds_export_data *med;
3017                 int recovering;
3018
3019                 if (req->rq_export == NULL) {
3020                         CERROR("operation %d on unconnected MDS from %s\n",
3021                                req->rq_reqmsg->opc,
3022                                req->rq_peerstr);
3023                         req->rq_status = -ENOTCONN;
3024                         GOTO(out, rc = -ENOTCONN);
3025                 }
3026
3027                 med = &req->rq_export->exp_mds_data;
3028                 obd = req->rq_export->exp_obd;
3029                 mds = &obd->u.mds;
3030
3031                 /* sanity check: if the xid matches, the request must
3032                  * be marked as a resent or replayed */
3033                 if (req->rq_xid == le64_to_cpu(med->med_mcd->mcd_last_xid) ||
3034                    req->rq_xid == le64_to_cpu(med->med_mcd->mcd_last_close_xid)) {
3035                         LASSERTF(lustre_msg_get_flags(req->rq_reqmsg) &
3036                                  (MSG_RESENT | MSG_REPLAY),
3037                                  "rq_xid "LPU64" matches last_xid, "
3038                                  "expected RESENT flag\n",
3039                                  req->rq_xid);
3040                 }
3041                 /* else: note the opposite is not always true; a
3042                  * RESENT req after a failover will usually not match
3043                  * the last_xid, since it was likely never
3044                  * committed. A REPLAYed request will almost never
3045                  * match the last xid, however it could for a
3046                  * committed, but still retained, open. */
3047
3048                 spin_lock_bh(&obd->obd_processing_task_lock);
3049                 recovering = obd->obd_recovering;
3050                 spin_unlock_bh(&obd->obd_processing_task_lock);
3051                 if (recovering) {
3052                         rc = mds_filter_recovery_request(req, obd,
3053                                                          &should_process);
3054                         if (rc || should_process == 0) {
3055                                 RETURN(rc);
3056                         } else if (should_process < 0) {
3057                                 req->rq_status = should_process;
3058                                 rc = ptlrpc_error(req);
3059                                 RETURN(rc);
3060                         }
3061                 }
3062         }
3063
3064         switch (req->rq_reqmsg->opc) {
3065         case MDS_CONNECT:
3066                 DEBUG_REQ(D_INODE, req, "connect");
3067                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
3068                 rc = target_handle_connect(req);
3069                 if (!rc) {
3070                         struct mds_export_data *med;
3071
3072                         LASSERT(req->rq_export);
3073                         med = &req->rq_export->u.eu_mds_data;
3074                         mds_init_export_data(req, med);
3075                         mds_req_add_idmapping(req, med);
3076
3077                         /* Now that we have an export, set mds. */
3078                         obd = req->rq_export->exp_obd;
3079                         mds = mds_req2mds(req);
3080                 }
3081                 break;
3082
3083         case MDS_DISCONNECT:
3084                 DEBUG_REQ(D_INODE, req, "disconnect");
3085                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
3086                 rc = target_handle_disconnect(req);
3087                 req->rq_status = rc;            /* superfluous? */
3088                 break;
3089
3090         case MDS_GETSTATUS:
3091                 DEBUG_REQ(D_INODE, req, "getstatus");
3092                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
3093                 rc = mds_getstatus(req);
3094                 break;
3095
3096         case MDS_GETATTR:
3097                 DEBUG_REQ(D_INODE, req, "getattr");
3098                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
3099                 rc = mds_getattr(req, MDS_REQ_REC_OFF);
3100                 break;
3101
3102         case MDS_ACCESS_CHECK:
3103                 DEBUG_REQ(D_INODE, req, "access_check");
3104                 OBD_FAIL_RETURN(OBD_FAIL_MDS_ACCESS_CHECK_NET, 0);
3105                 rc = mds_access_check(req, MDS_REQ_REC_OFF);
3106                 break;
3107
3108         case MDS_GETATTR_LOCK: {
3109                 struct lustre_handle lockh;
3110                 DEBUG_REQ(D_INODE, req, "getattr_lock");
3111                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_LOCK_NET, 0);
3112
3113                 /* If this request gets a reconstructed reply, we won't be
3114                  * acquiring any new locks in mds_getattr_lock, so we don't
3115                  * want to cancel.
3116                  */
3117                 lockh.cookie = 0;
3118                 rc = mds_getattr_lock(req, MDS_REQ_REC_OFF, &lockh,
3119                                       MDS_INODELOCK_UPDATE);
3120                 /* this non-intent call (from an ioctl) is special */
3121                 req->rq_status = rc;
3122                 if (rc == 0 && lockh.cookie)
3123                         ldlm_lock_decref(&lockh, LCK_PR);
3124                 break;
3125         }
3126         case MDS_STATFS:
3127                 DEBUG_REQ(D_INODE, req, "statfs");
3128                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
3129                 rc = mds_statfs(req);
3130                 break;
3131
3132         case MDS_READPAGE:
3133                 DEBUG_REQ(D_INODE, req, "readpage");
3134                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
3135                 rc = mds_readpage(req, MDS_REQ_REC_OFF);
3136
3137                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
3138                         if (req->rq_reply_state) {
3139                                 lustre_free_reply_state (req->rq_reply_state);
3140                                 req->rq_reply_state = NULL;
3141                         }
3142                         RETURN(0);
3143                 }
3144
3145                 break;
3146         case MDS_REINT: {
3147                 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
3148                                              sizeof (*opcp));
3149                 __u32  opc;
3150                 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
3151                                mds->mds_max_cookiesize};
3152                 int bufcount;
3153
3154                 /* NB only peek inside req now; mds_reint() will swab it */
3155                 if (opcp == NULL) {
3156                         CERROR ("Can't inspect opcode\n");
3157                         rc = -EINVAL;
3158                         break;
3159                 }
3160                 opc = *opcp;
3161                 if (lustre_msg_swabbed (req->rq_reqmsg))
3162                         __swab32s(&opc);
3163
3164                 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
3165                           (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
3166                            reint_names[opc] == NULL) ? reint_names[opc] :
3167                                                        "unknown opcode");
3168
3169                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
3170
3171                 if (opc == REINT_UNLINK || opc == REINT_RENAME)
3172                         bufcount = 3;
3173                 else if (opc == REINT_OPEN)
3174                         bufcount = 2;
3175                 else
3176                         bufcount = 1;
3177
3178                 /* for SETATTR: I have different reply setting for
3179                  * remote setfacl, so delay the reply buffer allocation.
3180                  */
3181                 if (opc != REINT_SETATTR) {
3182                         rc = lustre_pack_reply(req, bufcount, size, NULL);
3183                         if (rc)
3184                                 break;
3185                 }
3186
3187                 rc = mds_reint(req, MDS_REQ_REC_OFF, NULL);
3188                 fail = OBD_FAIL_MDS_REINT_NET_REP;
3189                 break;
3190         }
3191
3192         case MDS_CLOSE:
3193                 DEBUG_REQ(D_INODE, req, "close");
3194                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
3195                 rc = mds_close(req, MDS_REQ_REC_OFF);
3196                 break;
3197
3198         case MDS_DONE_WRITING:
3199                 DEBUG_REQ(D_INODE, req, "done_writing");
3200                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
3201                 rc = mds_done_writing(req, MDS_REQ_REC_OFF);
3202                 break;
3203
3204         case MDS_PIN:
3205                 DEBUG_REQ(D_INODE, req, "pin");
3206                 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
3207                 rc = mds_pin(req, MDS_REQ_REC_OFF);
3208                 break;
3209
3210         case MDS_SYNC:
3211                 DEBUG_REQ(D_INODE, req, "sync");
3212                 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
3213                 rc = mds_sync(req, MDS_REQ_REC_OFF);
3214                 break;
3215         case MDS_PARSE_ID:
3216                 DEBUG_REQ(D_INODE, req, "parseid");
3217                 rc = mds_parse_id(req);
3218                 break;
3219         case OBD_PING:
3220                 DEBUG_REQ(D_INODE, req, "ping");
3221                 rc = target_handle_ping(req);
3222                 break;
3223
3224         case OBD_LOG_CANCEL:
3225                 CDEBUG(D_INODE, "log cancel\n");
3226                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
3227                 rc = -ENOTSUPP; /* la la la */
3228                 break;
3229
3230         case LDLM_ENQUEUE:
3231                 DEBUG_REQ(D_INODE, req, "enqueue");
3232                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
3233                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
3234                                          ldlm_server_blocking_ast, NULL);
3235                 fail = OBD_FAIL_LDLM_REPLY;
3236                 break;
3237         case LDLM_CONVERT:
3238                 DEBUG_REQ(D_INODE, req, "convert");
3239                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
3240                 rc = ldlm_handle_convert(req);
3241                 break;
3242         case LDLM_BL_CALLBACK:
3243         case LDLM_CP_CALLBACK:
3244                 DEBUG_REQ(D_INODE, req, "callback");
3245                 CERROR("callbacks should not happen on MDS\n");
3246                 LBUG();
3247                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
3248                 break;
3249         case LDLM_FLK_DEADLOCK_CHK:
3250                 DEBUG_REQ(D_INODE, req, "flock deadlock check");
3251                 rc = ldlm_handle_flock_deadlock_check(req);
3252                 break;
3253         case LLOG_ORIGIN_HANDLE_OPEN:
3254                 DEBUG_REQ(D_INODE, req, "llog_init");
3255                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
3256                 rc = llog_origin_handle_open(req);
3257                 break;
3258         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3259                 DEBUG_REQ(D_INODE, req, "llog next block");
3260                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
3261                 rc = llog_origin_handle_next_block(req);
3262                 break;
3263         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3264                 DEBUG_REQ(D_INODE, req, "llog prev block");
3265                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
3266                 rc = llog_origin_handle_prev_block(req);
3267                 break;
3268         case LLOG_ORIGIN_HANDLE_READ_HEADER:
3269                 DEBUG_REQ(D_INODE, req, "llog read header");
3270                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
3271                 rc = llog_origin_handle_read_header(req);
3272                 break;
3273         case LLOG_ORIGIN_HANDLE_CLOSE:
3274                 DEBUG_REQ(D_INODE, req, "llog close");
3275                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
3276                 rc = llog_origin_handle_close(req);
3277                 break;
3278         case OST_CREATE:
3279                 DEBUG_REQ(D_INODE, req, "ost_create");
3280                 rc = mdt_obj_create(req);
3281                 break;
3282         case OST_GET_INFO:
3283                 DEBUG_REQ(D_INODE, req, "get_info");
3284                 rc = mdt_get_info(req);
3285                 break;
3286         case OST_SET_INFO:
3287                 DEBUG_REQ(D_INODE, req, "set_info");
3288                 rc = mdt_set_info(req);
3289                 break;
3290         case OST_WRITE:
3291                 CDEBUG(D_INODE, "write\n");
3292                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
3293                 rc = ost_brw_write(req, NULL);
3294                 LASSERT(current->journal_info == NULL);
3295                 /* mdt_brw sends its own replies */
3296                 RETURN(rc);
3297                 break;
3298         case LLOG_CATINFO:
3299                 DEBUG_REQ(D_INODE, req, "llog catinfo");
3300                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
3301                 rc = llog_catinfo(req);
3302                 break;
3303         default:
3304                 req->rq_status = -ENOTSUPP;
3305                 rc = ptlrpc_error(req);
3306                 RETURN(rc);
3307         }
3308
3309         LASSERT(current->journal_info == NULL);
3310
3311         /* If we're DISCONNECTing, the mds_export_data is already freed */
3312         if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
3313                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
3314                 struct obd_device *obd = list_entry(mds, struct obd_device,
3315                                                     u.mds);
3316                 req->rq_repmsg->last_xid =
3317                         le64_to_cpu(med->med_mcd->mcd_last_xid);
3318
3319                 if (!obd->obd_no_transno) {
3320                         req->rq_repmsg->last_committed =
3321                                 obd->obd_last_committed;
3322                 } else {
3323                         DEBUG_REQ(D_IOCTL, req,
3324                                   "not sending last_committed update");
3325                 }
3326                 CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
3327                        ", xid "LPU64"\n",
3328                        mds->mds_last_transno, obd->obd_last_committed,
3329                        req->rq_xid);
3330         }
3331  out:
3332
3333
3334         target_send_reply(req, rc, fail);
3335         RETURN(0);
3336 }
3337
3338 /* Update the server data on disk.  This stores the new mount_count and also the
3339  * last_rcvd value to disk.  If we don't have a clean shutdown, then the server
3340  * last_rcvd value may be less than that of the clients.  This will alert us
3341  * that we may need to do client recovery.
3342  *
3343  * Also assumes for mds_last_transno that we are not modifying it (no locking).
3344  */
3345 int mds_update_server_data(struct obd_device *obd, int force_sync)
3346 {
3347         struct mds_obd *mds = &obd->u.mds;
3348         struct mds_server_data *msd = mds->mds_server_data;
3349         struct file *filp = mds->mds_rcvd_filp;
3350         struct lvfs_run_ctxt saved;
3351         loff_t off = 0;
3352         int rc;
3353         ENTRY;
3354
3355         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3356         msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
3357
3358         CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
3359                mds->mds_mount_count, mds->mds_last_transno);
3360         rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off, force_sync);
3361         if (rc)
3362                 CERROR("error writing MDS server data: rc = %d\n", rc);
3363         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3364
3365         RETURN(rc);
3366 }
3367
3368 /* saves last allocated fid counter to file. */
3369 int mds_update_last_fid(struct obd_device *obd, void *handle,
3370                         int force_sync)
3371 {
3372         struct mds_obd *mds = &obd->u.mds;
3373         struct file *filp = mds->mds_fid_filp;
3374         struct lvfs_run_ctxt saved;
3375         loff_t off = 0;
3376         __u64 last_fid;
3377         int rc = 0;
3378         ENTRY;
3379
3380         spin_lock(&mds->mds_last_fid_lock);
3381         last_fid = mds->mds_last_fid;
3382         spin_unlock(&mds->mds_last_fid_lock);
3383
3384         CDEBUG(D_SUPER, "MDS last_fid is #"LPU64"\n",
3385                last_fid);
3386
3387         if (handle) {
3388                 fsfilt_add_journal_cb(obd, mds->mds_sb, last_fid,
3389                                       handle, mds_commit_last_fid_cb,
3390                                       NULL);
3391         }
3392                 
3393         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3394         rc = fsfilt_write_record(obd, filp, &last_fid, sizeof(last_fid),
3395                                  &off, force_sync);
3396         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3397
3398         if (rc) {
3399                 CERROR("error writing MDS last_fid #"LPU64
3400                        ", err = %d\n", last_fid, rc);
3401                 RETURN(rc);
3402         }
3403                 
3404         CDEBUG(D_SUPER, "wrote fid #"LPU64" at idx "
3405                "%llu: err = %d\n", last_fid, off, rc);
3406
3407         RETURN(rc);
3408 }
3409
3410 void mds_set_last_fid(struct obd_device *obd, __u64 fid)
3411 {
3412         struct mds_obd *mds = &obd->u.mds;
3413
3414         spin_lock(&mds->mds_last_fid_lock);
3415         if (fid > mds->mds_last_fid)
3416                 mds->mds_last_fid = fid;
3417         spin_unlock(&mds->mds_last_fid_lock);
3418 }
3419
3420 void mds_commit_last_transno_cb(struct obd_device *obd,
3421                                 __u64 transno, void *data,
3422                                 int error)
3423 {
3424         obd_transno_commit_cb(obd, transno, error);
3425 }
3426
3427 void mds_commit_last_fid_cb(struct obd_device *obd,
3428                             __u64 fid, void *data,
3429                             int error)
3430 {
3431         if (error) {
3432                 CERROR("%s: fid "LPD64" commit error: %d\n",
3433                        obd->obd_name, fid, error);
3434                 return;
3435         }
3436         
3437         CDEBUG(D_HA, "%s: fid "LPD64" committed\n",
3438                obd->obd_name, fid);
3439 }
3440
3441 __u64 mds_alloc_fid(struct obd_device *obd)
3442 {
3443         struct mds_obd *mds = &obd->u.mds;
3444         __u64 fid;
3445         
3446         spin_lock(&mds->mds_last_fid_lock);
3447         fid = ++mds->mds_last_fid;
3448         spin_unlock(&mds->mds_last_fid_lock);
3449
3450         return fid;
3451 }
3452
3453 /*
3454  * reads inode self id from inode EA. Probably later this should be replaced by
3455  * caching inode self id to avoid raeding it every time it is needed.
3456  */
3457 int mds_read_inode_sid(struct obd_device *obd, struct inode *inode,
3458                        struct lustre_id *id)
3459 {
3460         int rc;
3461         ENTRY;
3462
3463         LASSERT(id != NULL);
3464         LASSERT(obd != NULL);
3465         LASSERT(inode != NULL);
3466
3467         rc = fsfilt_get_md(obd, inode, &id->li_fid,
3468                            sizeof(id->li_fid), EA_SID);
3469         if (rc < 0) {
3470                 CERROR("fsfilt_get_md() failed, "
3471                        "rc = %d\n", rc);
3472                 RETURN(rc);
3473         } else if (!rc) {
3474                 rc = -ENODATA;
3475                 RETURN(rc);
3476         } else {
3477                 rc = 0;
3478         }
3479         id_ino(id) = inode->i_ino;
3480         id_gen(id) = inode->i_generation;
3481         id_type(id) = S_IFMT & inode->i_mode;
3482
3483         RETURN(rc);
3484 }
3485
3486 int mds_read_inode_pid(struct obd_device *obd, struct inode *inode,
3487                        struct lustre_id *id)
3488 {
3489         int rc;
3490         ENTRY;
3491
3492         LASSERT(inode && id);
3493
3494         rc = fsfilt_get_md(obd, inode, id, sizeof(*id), EA_PID);
3495         if (rc < 0)
3496                CERROR("get parent id from EA failed, rc=%d\n", rc);
3497         else if (!rc)
3498                 rc = -ENODATA;
3499         else
3500                 rc = 0;
3501
3502         RETURN(rc);
3503 }
3504
3505 /* updates inode self id in EA. */
3506 int mds_update_inode_ids(struct obd_device *obd, struct inode *inode,
3507                          void *handle, struct lustre_id *id,
3508                          struct lustre_id *pid)
3509 {
3510         int rc = 0;
3511         ENTRY;
3512         
3513         LASSERT(id || pid);
3514         LASSERT(id == NULL || id_fid(id) != 0);
3515         LASSERT(pid == NULL || id_fid(pid) != 0);
3516         LASSERT(obd != NULL);
3517         LASSERT(inode != NULL);
3518         
3519         if (id) {
3520                 mds_set_last_fid(obd, id_fid(id));
3521                 rc = fsfilt_set_md(obd, inode, handle, &id->li_fid,
3522                                    sizeof(id->li_fid), EA_SID);
3523                 LASSERTF(rc == 0, "failed to update fid:  %d\n", rc);
3524         }
3525         if (pid) {
3526                 rc = fsfilt_set_md(obd, inode, handle, pid,
3527                                    sizeof(*pid), EA_PID);
3528                 LASSERTF(rc == 0, "failed to update parent fid:  %d\n", rc);
3529         }
3530
3531         RETURN(rc);
3532 }
3533
3534 /* mount the file system (secretly) */
3535 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
3536 {
3537         struct lustre_cfg* lcfg = buf;
3538         struct mds_obd *mds = &obd->u.mds;
3539         struct lvfs_obd_ctxt *lvfs_ctxt = NULL;
3540         char *options = NULL;
3541         struct vfsmount *mnt;
3542         char ns_name[48];
3543         unsigned long page;
3544         int rc = 0;
3545         ENTRY;
3546
3547         if (lcfg->lcfg_bufcount < 3)
3548                 RETURN(rc = -EINVAL);
3549
3550         if (LUSTRE_CFG_BUFLEN(lcfg, 1) == 0 || LUSTRE_CFG_BUFLEN(lcfg, 2) == 0)
3551                 RETURN(rc = -EINVAL);
3552
3553         obd->obd_fsops = fsfilt_get_ops(lustre_cfg_string(lcfg, 2));
3554         if (IS_ERR(obd->obd_fsops))
3555                 RETURN(rc = PTR_ERR(obd->obd_fsops));
3556
3557         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
3558
3559         page = get_zeroed_page(GFP_KERNEL);
3560         if (!page)
3561                 RETURN(-ENOMEM);
3562
3563         options = (char *)page;
3564
3565         /*
3566          * here we use "iopen_nopriv" hardcoded, because it affects MDS utility
3567          * and the rest of options are passed by mount options. Probably this
3568          * should be moved to somewhere else like startup scripts or lconf. */
3569         sprintf(options, "iopen_nopriv");
3570         
3571         if (LUSTRE_CFG_BUFLEN(lcfg, 4) > 0 && lustre_cfg_buf(lcfg, 4))
3572                 sprintf(options + strlen(options), ",%s",
3573                         lustre_cfg_string(lcfg, 4));
3574
3575         /* we have to know mdsnum before touching underlying fs -bzzz */
3576         atomic_set(&mds->mds_open_count, 0);
3577         sema_init(&mds->mds_md_sem, 1);
3578         mds->mds_md_connected = 0;
3579         mds->mds_md_name = NULL;
3580
3581         if (LUSTRE_CFG_BUFLEN(lcfg, 5) > 0 && lustre_cfg_buf(lcfg, 5) &&
3582             strncmp(lustre_cfg_string(lcfg, 5), "dumb", LUSTRE_CFG_BUFLEN(lcfg, 5))) {
3583                 class_uuid_t uuid;
3584
3585                 generate_random_uuid(uuid);
3586                 class_uuid_unparse(uuid, &mds->mds_md_uuid);
3587
3588                 OBD_ALLOC(mds->mds_md_name, LUSTRE_CFG_BUFLEN(lcfg, 5));
3589                 if (mds->mds_md_name == NULL) 
3590                         RETURN(rc = -ENOMEM);
3591
3592                 memcpy(mds->mds_md_name, lustre_cfg_buf(lcfg, 5),
3593                        LUSTRE_CFG_BUFLEN(lcfg, 5));
3594                 
3595                 CDEBUG(D_OTHER, "MDS: %s is master for %s\n",
3596                        obd->obd_name, mds->mds_md_name);
3597
3598                 rc = mds_md_connect(obd, mds->mds_md_name);
3599                 if (rc) {
3600                         OBD_FREE(mds->mds_md_name, LUSTRE_CFG_BUFLEN(lcfg, 5));
3601                         GOTO(err_ops, rc);
3602                 }
3603         }
3604
3605         mds->mds_obd_type = MDS_MASTER_OBD;
3606
3607         if (LUSTRE_CFG_BUFLEN(lcfg, 6) > 0 && lustre_cfg_buf(lcfg, 6) &&
3608             strncmp(lustre_cfg_string(lcfg, 6), "dumb", 
3609                     LUSTRE_CFG_BUFLEN(lcfg, 6))) {
3610                 if (!memcmp(lustre_cfg_string(lcfg, 6), "master", 
3611                             strlen("master"))) {
3612                         mds->mds_obd_type = MDS_MASTER_OBD;
3613                 } else if (!memcmp(lustre_cfg_string(lcfg, 6), "cache", 
3614                                    strlen("cache"))) {
3615                         mds->mds_obd_type = MDS_CACHE_OBD;
3616                 }     
3617         }
3618
3619         rc = lvfs_mount_fs(lustre_cfg_string(lcfg, 1), 
3620                            lustre_cfg_string(lcfg, 2),
3621                            options, 0, &lvfs_ctxt);
3622
3623         free_page(page);
3624
3625         if (rc || !lvfs_ctxt) {
3626                 CERROR("lvfs_mount_fs failed: rc = %d\n", rc);
3627                 GOTO(err_ops, rc);
3628         }
3629
3630         mnt = lvfs_ctxt->loc_mnt;
3631         mds->mds_lvfs_ctxt = lvfs_ctxt;
3632         ll_clear_rdonly(ll_sbdev(mnt->mnt_sb));
3633
3634         CDEBUG(D_SUPER, "%s: mnt = %p\n", lustre_cfg_string(lcfg, 1), mnt);
3635
3636         mds->mds_fidext_thumb = 0;
3637         sema_init(&mds->mds_epoch_sem, 1);
3638         atomic_set(&mds->mds_real_clients, 0);
3639         spin_lock_init(&mds->mds_fidext_lock);
3640         spin_lock_init(&mds->mds_fidmap_lock);
3641         spin_lock_init(&mds->mds_transno_lock);
3642         spin_lock_init(&mds->mds_last_fid_lock);
3643         sema_init(&mds->mds_orphan_recovery_sem, 1);
3644         mds->mds_max_cookiesize = sizeof(struct llog_cookie);
3645
3646         sprintf(ns_name, "mds-%s", obd->obd_uuid.uuid);
3647         obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER);
3648
3649         if (obd->obd_namespace == NULL) {
3650                 mds_cleanup(obd, 0);
3651                 GOTO(err_put, rc = -ENOMEM);
3652         }
3653         ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
3654
3655         mds->mds_capa_timeout = CAPA_TIMEOUT;
3656         mds->mds_capa_key_timeout = CAPA_KEY_TIMEOUT;
3657         
3658         rc = mds_fs_setup(obd, mnt);
3659         if (rc) {
3660                 CERROR("%s: MDS filesystem method init failed: rc = %d\n",
3661                        obd->obd_name, rc);
3662                 GOTO(err_ns, rc);
3663         }
3664
3665         rc = llog_start_commit_thread();
3666         if (rc < 0)
3667
3668                 GOTO(err_fs, rc);
3669
3670         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0 && lustre_cfg_buf(lcfg, 3) &&
3671             strncmp(lustre_cfg_string(lcfg, 3), "dumb", 
3672                     LUSTRE_CFG_BUFLEN(lcfg, 3))) {
3673                 class_uuid_t uuid;
3674
3675                 generate_random_uuid(uuid);
3676                 class_uuid_unparse(uuid, &mds->mds_dt_uuid);
3677
3678                 OBD_ALLOC(mds->mds_profile, LUSTRE_CFG_BUFLEN(lcfg, 3));
3679                 if (mds->mds_profile == NULL)
3680                         GOTO(err_fs, rc = -ENOMEM);
3681
3682                 strncpy(mds->mds_profile, lustre_cfg_string(lcfg, 3),
3683                         LUSTRE_CFG_BUFLEN(lcfg, 3));
3684         }
3685
3686         /* 
3687          * setup root dir and files ID dir if lmv already connected, or there is
3688          * not lmv at all.
3689          */
3690         if (mds->mds_md_exp || (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0 && 
3691                                 lustre_cfg_buf(lcfg, 3) &&
3692                                 strncmp(lustre_cfg_string(lcfg, 3), "dumb", 
3693                                         LUSTRE_CFG_BUFLEN(lcfg, 3)))) {
3694                 rc = mds_fs_setup_rootid(obd);
3695                 if (rc)
3696                         GOTO(err_fs, rc);
3697
3698                 rc = mds_fs_setup_virtid(obd);
3699                 if (rc)
3700                         GOTO(err_fs, rc);
3701
3702         }
3703
3704         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3705                            "mds_ldlm_client", &obd->obd_ldlm_client);
3706         obd->obd_replayable = 1;
3707         
3708         mds->mds_crypto_type = NO_CRYPTO;
3709         
3710         rc = mds_postsetup(obd);
3711         if (rc)
3712                 GOTO(err_fs, rc);
3713
3714         RETURN(0);
3715
3716 err_fs:
3717         /* No extra cleanup needed for llog_init_commit_thread() */
3718         mds_fs_cleanup(obd, 0);
3719 err_ns:
3720         ldlm_namespace_free(obd->obd_namespace, 0);
3721         obd->obd_namespace = NULL;
3722 err_put:
3723         unlock_kernel();
3724         lvfs_umount_fs(mds->mds_lvfs_ctxt);
3725         mds->mds_sb = 0;
3726         lock_kernel();
3727 err_ops:
3728         fsfilt_put_ops(obd->obd_fsops);
3729         return rc;
3730 }
3731
3732 static int mds_fs_post_setup(struct obd_device *obd)
3733 {
3734         struct mds_obd *mds = &obd->u.mds;
3735         struct dentry *dentry;
3736         int rc = 0;
3737         ENTRY;
3738        
3739         dentry = mds_id2dentry(obd, &mds->mds_rootid, NULL);
3740         if (IS_ERR(dentry)) {
3741                 CERROR("Can't find ROOT, err = %d\n",
3742                        (int)PTR_ERR(dentry));
3743                 RETURN(PTR_ERR(dentry));
3744         }
3745         rc = fsfilt_post_setup(obd, dentry);
3746         //set id2name function handler
3747         fsfilt_set_info(obd, mds->mds_sb, NULL, 7, "id2name",
3748                         sizeof(mds_audit_id2name), mds_audit_id2name);
3749
3750         l_dput(dentry);
3751         RETURN(rc); 
3752 }
3753
3754 static int mds_postsetup(struct obd_device *obd)
3755 {
3756         struct mds_obd *mds = &obd->u.mds;
3757         int rc = 0;
3758         ENTRY;
3759
3760         rc = obd_llog_setup(obd, &obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT, 
3761                             obd, 0, NULL, &llog_lvfs_ops);
3762         if (rc)
3763                 RETURN(rc);
3764
3765         if (mds->mds_profile) {
3766                 struct llog_ctxt *lgctxt;
3767                 struct lvfs_run_ctxt saved;
3768                 struct lustre_profile *lprof;
3769                 struct config_llog_instance cfg;
3770
3771                 cfg.cfg_instance = NULL;
3772                 cfg.cfg_uuid = mds->mds_dt_uuid;
3773                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3774
3775                 lgctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
3776                 if (!lgctxt) {
3777                         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3778                         GOTO(err_llog, rc = -EINVAL);
3779                 }
3780                 
3781                 rc = class_config_process_llog(lgctxt, mds->mds_profile, &cfg);
3782                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3783
3784                 if (rc)
3785                         GOTO(err_llog, rc);
3786
3787                 lprof = class_get_profile(mds->mds_profile);
3788                 if (lprof == NULL) {
3789                         CERROR("No profile found: %s\n", mds->mds_profile);
3790                         GOTO(err_cleanup, rc = -ENOENT);
3791                 }
3792                 rc = mds_dt_connect(obd, lprof->lp_lov);
3793                 if (rc)
3794                         GOTO(err_cleanup, rc);
3795
3796                 rc = mds_md_postsetup(obd);
3797                 if (rc)
3798                         GOTO(err_cleanup, rc);
3799         }
3800         rc = mds_fs_post_setup(obd);
3801         if (rc)
3802                 CERROR("can not post setup fsfilt\n");        
3803         RETURN(rc);
3804 err_cleanup:
3805         mds_dt_clean(obd);
3806 err_llog:
3807         obd_llog_cleanup(llog_get_context(&obd->obd_llogs,
3808                                           LLOG_CONFIG_ORIG_CTXT));
3809         return rc;
3810 }
3811
3812 int mds_postrecov_common(struct obd_device *obd)
3813 {
3814         struct mds_obd *mds = &obd->u.mds;
3815         struct llog_ctxt *ctxt;
3816         int rc, item = 0, valsize;
3817         struct timeval tstart, now;
3818          __u32 group;
3819         ENTRY;
3820
3821         CDEBUG(D_ERROR, "%s: post-recovery\n", obd->obd_name);
3822         LASSERT(!obd->obd_recovering);
3823         ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
3824         LASSERT(ctxt != NULL);
3825
3826         /* clean PENDING dir */
3827         do_gettimeofday(&tstart);
3828         rc = mds_cleanup_orphans(obd);
3829         do_gettimeofday(&now);
3830         CDEBUG(D_ERROR, "%s: cleaned %d MDS orphans in %us\n",
3831                obd->obd_name, rc, (unsigned) (now.tv_sec - tstart.tv_sec));
3832         if (rc < 0)
3833                 GOTO(out, rc);
3834         item = rc;
3835
3836         group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
3837         valsize = sizeof(group);
3838         rc = obd_set_info(mds->mds_dt_exp, strlen("mds_conn"),
3839                           "mds_conn", valsize, &group);
3840         if (rc) {
3841                 CERROR("%s: failed to obd_set_info(): %d\n", 
3842                        obd->obd_name, rc);
3843                 GOTO(out, rc);
3844         }
3845
3846         rc = llog_connect(ctxt, obd->u.mds.mds_dt_desc.ld_tgt_count,
3847                           NULL, NULL, NULL);
3848         if (rc) {
3849                 CERROR("%s: failed at llog_origin_connect: %d\n", 
3850                        obd->obd_name, rc);
3851                 GOTO(out, rc);
3852         }
3853
3854         /* remove the orphaned precreated objects */
3855         do_gettimeofday(&tstart);
3856         rc = mds_dt_clear_orphans(mds, NULL /* all OSTs */);
3857         do_gettimeofday(&now);
3858         CDEBUG(D_ERROR, "%s: cleaned OSS orphans in %us: %d\n",
3859                obd->obd_name, (unsigned) (now.tv_sec - tstart.tv_sec), rc);
3860         if (rc) {
3861                 CERROR("%s: can't clear OSS orphans: %d\n", 
3862                        obd->obd_name, rc);
3863                 GOTO(err_llog, rc);
3864         }
3865
3866 out:
3867         RETURN(rc < 0 ? rc : item);
3868
3869 err_llog:
3870         /* cleanup all llogging subsystems */
3871         rc = obd_llog_finish(obd, &obd->obd_llogs,
3872                              mds->mds_dt_desc.ld_tgt_count);
3873         if (rc)
3874                 CERROR("%s: failed to cleanup llogging subsystems\n",
3875                         obd->obd_name);
3876         goto out;
3877 }
3878
3879 int mds_postrecov(struct obd_device *obd)
3880 {
3881         int rc;
3882         ENTRY;
3883         rc = mds_postrecov_common(obd);
3884         if (rc >= 0)
3885                 rc = mds_md_reconnect(obd);
3886         RETURN(rc);
3887 }
3888
3889 int mds_dt_clean(struct obd_device *obd)
3890 {
3891         struct mds_obd *mds = &obd->u.mds;
3892         ENTRY;
3893
3894         if (mds->mds_profile) {
3895                 char * cln_prof;
3896                 struct llog_ctxt *llctx;
3897                 struct lvfs_run_ctxt saved;
3898                 struct config_llog_instance cfg;
3899                 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
3900
3901                 OBD_ALLOC(cln_prof, len);
3902                 if (!cln_prof) {
3903                         CERROR("can't allocate memory, processing cleanup "
3904                                "profile is skipped\n");
3905                         goto out;
3906                 }
3907                 
3908                 sprintf(cln_prof, "%s-clean", mds->mds_profile);
3909
3910                 cfg.cfg_instance = NULL;
3911                 cfg.cfg_uuid = mds->mds_dt_uuid;
3912
3913                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3914                 llctx = llog_get_context(&obd->obd_llogs,
3915                                          LLOG_CONFIG_ORIG_CTXT);
3916                 class_config_process_llog(llctx, cln_prof, &cfg);
3917                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3918
3919                 OBD_FREE(cln_prof, len);
3920         out:
3921                 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
3922                 mds->mds_profile = NULL;
3923         }
3924         RETURN(0);
3925 }
3926
3927 int mds_md_clean(struct obd_device *obd)
3928 {
3929         struct mds_obd *mds = &obd->u.mds;
3930         ENTRY;
3931
3932         if (mds->mds_md_name) {
3933                 OBD_FREE(mds->mds_md_name, strlen(mds->mds_md_name) + 1);
3934                 mds->mds_md_name = NULL;
3935         }
3936         RETURN(0);
3937 }
3938
3939 static int mds_precleanup(struct obd_device *obd, int flags)
3940 {
3941         int rc = 0;
3942         ENTRY;
3943
3944         mds_md_clean(obd);
3945         mds_dt_disconnect(obd, flags);
3946         mds_dt_clean(obd);
3947         obd_llog_cleanup(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT));
3948         RETURN(rc);
3949 }
3950
3951 extern void lgss_svc_cache_purge_all(void);
3952 static int mds_cleanup(struct obd_device *obd, int flags)
3953 {
3954         struct mds_obd *mds = &obd->u.mds;
3955         ENTRY;
3956
3957         if (mds->mds_sb == NULL)
3958                 RETURN(0);
3959
3960         mds_update_server_data(obd, 1);
3961         mds_update_last_fid(obd, NULL, 1);
3962         
3963         if (mds->mds_dt_objids != NULL) {
3964                 int size = mds->mds_dt_desc.ld_tgt_count *
3965                         sizeof(obd_id);
3966                 OBD_FREE(mds->mds_dt_objids, size);
3967         }
3968         mds_fs_cleanup(obd, flags);
3969
3970         unlock_kernel();
3971
3972         /* 2 seems normal on mds, (may_umount() also expects 2
3973           fwiw), but we only see 1 at this point in obdfilter. */
3974         lvfs_umount_fs(mds->mds_lvfs_ctxt);
3975
3976         mds->mds_sb = 0;
3977
3978         ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
3979
3980         spin_lock_bh(&obd->obd_processing_task_lock);
3981         if (obd->obd_recovering) {
3982                 target_cancel_recovery_timer(obd);
3983                 obd->obd_recovering = 0;
3984         }
3985         spin_unlock_bh(&obd->obd_processing_task_lock);
3986
3987         lock_kernel();
3988         fsfilt_put_ops(obd->obd_fsops);
3989
3990 #ifdef ENABLE_GSS
3991         /* XXX */
3992         lgss_svc_cache_purge_all();
3993 #endif
3994
3995         spin_lock(&mds->mds_denylist_lock);
3996         while (!list_empty( &mds->mds_denylist ) ) {
3997                 deny_sec_t *p_deny_sec = list_entry(mds->mds_denylist.next,
3998                                                     deny_sec_t, list);
3999                 list_del(&p_deny_sec->list);
4000                 OBD_FREE(p_deny_sec, sizeof(*p_deny_sec));
4001         }
4002         spin_unlock(&mds->mds_denylist_lock);
4003
4004         mds_capa_keys_cleanup(obd);
4005
4006         RETURN(0);
4007 }
4008
4009 static int set_security(const char *value, char **sec)
4010 {
4011         if (!strcmp(value, "null"))
4012                 *sec = "null";
4013         else if (!strcmp(value, "krb5i"))
4014                 *sec = "krb5i";
4015         else if (!strcmp(value, "krb5p"))
4016                 *sec = "krb5p";
4017         else {
4018                 CERROR("Unrecognized security flavor %s\n", value);
4019                 return -EINVAL;
4020         }
4021
4022         return 0;
4023 }
4024
4025 static int mds_process_config(struct obd_device *obd, obd_count len, void *buf)
4026 {
4027         struct lustre_cfg *lcfg = buf;
4028         struct mds_obd *mds = &obd->u.mds;
4029         int rc = 0;
4030         ENTRY;
4031
4032         switch(lcfg->lcfg_command) {
4033         case LCFG_SET_SECURITY: {
4034                 if ((LUSTRE_CFG_BUFLEN(lcfg, 1) == 0) ||
4035                     (LUSTRE_CFG_BUFLEN(lcfg, 2) == 0))
4036                         GOTO(out, rc = -EINVAL);
4037
4038                 if (!strcmp(lustre_cfg_string(lcfg, 1), "mds_sec"))
4039                         rc = set_security(lustre_cfg_string(lcfg, 2),
4040                                           &mds->mds_mds_sec);
4041                 else if (!strcmp(lustre_cfg_string(lcfg, 1), "oss_sec"))
4042                         rc = set_security(lustre_cfg_string(lcfg, 2),
4043                                           &mds->mds_ost_sec);
4044                 else if (!strcmp(lustre_cfg_string(lcfg, 1), "deny_sec")){
4045                         spin_lock(&mds->mds_denylist_lock);
4046                         rc = add_deny_security(lustre_cfg_string(lcfg, 2),
4047                                                &mds->mds_denylist);
4048                         spin_unlock(&mds->mds_denylist_lock);
4049                 } else {
4050                         CERROR("Unrecognized key\n");
4051                         rc = -EINVAL;
4052                 }
4053                 break;
4054         }
4055         default:
4056                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
4057                 GOTO(out, rc = -EINVAL);
4058         }
4059 out:
4060         RETURN(rc);
4061 }
4062
4063 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
4064                                         int offset,
4065                                         struct ldlm_lock *new_lock,
4066                                         struct ldlm_lock **old_lock,
4067                                         struct lustre_handle *lockh)
4068 {
4069         struct obd_export *exp = req->rq_export;
4070         struct obd_device *obd = exp->exp_obd;
4071         struct ldlm_request *dlmreq =
4072                 lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*dlmreq));
4073         struct lustre_handle remote_hdl = dlmreq->lock_handle1;
4074         struct list_head *iter;
4075
4076         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4077                 return;
4078
4079         spin_lock(&obd->obd_namespace->ns_hash_lock);
4080         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
4081                 struct ldlm_lock *lock;
4082                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
4083                 if (lock == new_lock)
4084                         continue;
4085                 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
4086                         lockh->cookie = lock->l_handle.h_cookie;
4087                         LDLM_DEBUG(lock, "restoring lock cookie");
4088                         DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
4089                                   lockh->cookie);
4090                         if (old_lock)
4091                                 *old_lock = LDLM_LOCK_GET(lock);
4092                         spin_unlock(&obd->obd_namespace->ns_hash_lock);
4093                         return;
4094                 }
4095         }
4096         spin_unlock(&obd->obd_namespace->ns_hash_lock);
4097
4098         /* If the xid matches, then we know this is a resent request,
4099          * and allow it. (It's probably an OPEN, for which we don't
4100          * send a lock */
4101         if (req->rq_xid == 
4102             le64_to_cpu(exp->exp_mds_data.med_mcd->mcd_last_xid))
4103                 return;
4104
4105         if (req->rq_xid == 
4106             le64_to_cpu(exp->exp_mds_data.med_mcd->mcd_last_close_xid))
4107                 return;
4108
4109         /* This remote handle isn't enqueued, so we never received or
4110          * processed this request.  Clear MSG_RESENT, because it can
4111          * be handled like any normal request now. */
4112
4113         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4114
4115         DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
4116                   remote_hdl.cookie);
4117 }
4118
4119 int intent_disposition(struct ldlm_reply *rep, int flag)
4120 {
4121         if (!rep)
4122                 return 0;
4123         return (rep->lock_policy_res1 & flag);
4124 }
4125
4126 void intent_set_disposition(struct ldlm_reply *rep, int flag)
4127 {
4128         if (!rep)
4129                 return;
4130         rep->lock_policy_res1 |= flag;
4131 }
4132
4133 static int mds_intent_prepare_reply_buffers(struct ptlrpc_request *req, 
4134                                             struct ldlm_intent *it)
4135 {
4136         struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
4137         struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
4138         int rc, reply_buffers;
4139         int repsize[8] = {sizeof(struct ldlm_reply),
4140                           sizeof(struct mds_body),
4141                           mds->mds_max_mdsize};
4142         ENTRY;       
4143  
4144         reply_buffers = 3;
4145         if (it->opc & ( IT_OPEN | IT_GETATTR | IT_LOOKUP | IT_CHDIR )) {
4146                 /*Ugly here, Actually, we should prepare the reply buffer 
4147                  *after we know whether these stuff exist or not, which should
4148                  * be fixed in future, Now each item is in the fix position,
4149                  * the sequence is lsm, acl, crypto ea, capa.*/
4150                 repsize[reply_buffers++] = sizeof(int);
4151                 if (med->med_remote)
4152                         repsize[reply_buffers++] =
4153                                 sizeof(struct mds_remote_perm);
4154                 else
4155                         repsize[reply_buffers++] = 
4156                                 xattr_acl_size(LL_ACL_MAX_ENTRIES);
4157                 
4158                 repsize[reply_buffers++] = sizeof(int);
4159                 repsize[reply_buffers++] = sizeof(struct crypto_key); 
4160
4161                 /* XXX: if new buffer is to be added, capability reply
4162                  *      buffer should always been reserved. */
4163                 if (it->opc & IT_OPEN)
4164                         repsize[reply_buffers++] = sizeof(struct lustre_capa);
4165         }
4166
4167         rc = lustre_pack_reply(req, reply_buffers, repsize, NULL);
4168
4169         RETURN(rc);
4170 }
4171
4172 static int mds_intent_policy(struct ldlm_namespace *ns,
4173                              struct ldlm_lock **lockp, void *req_cookie,
4174                              ldlm_mode_t mode, int flags, void *data)
4175 {
4176         struct ptlrpc_request *req = req_cookie;
4177         struct ldlm_lock *lock = *lockp;
4178         struct ldlm_intent *it;
4179         struct ldlm_reply *rep;
4180         struct lustre_handle lockh[2] = {{0}, {0}};
4181         struct ldlm_lock *new_lock = NULL;
4182         int getattr_part = MDS_INODELOCK_UPDATE;
4183         int rc;
4184
4185         int offset = MDS_REQ_INTENT_REC_OFF; 
4186         ENTRY;
4187
4188         LASSERT(req != NULL);
4189         MD_COUNTER_INCREMENT(req->rq_export->exp_obd, intent_lock);
4190
4191         if (req->rq_reqmsg->bufcount <= MDS_REQ_INTENT_IT_OFF) {
4192                 /* No intent was provided */
4193                 int size = sizeof(struct ldlm_reply);
4194                 rc = lustre_pack_reply(req, 1, &size, NULL);
4195                 LASSERT(rc == 0);
4196                 RETURN(0);
4197         }
4198
4199         it = lustre_swab_reqbuf(req, MDS_REQ_INTENT_IT_OFF, sizeof(*it),
4200                                 lustre_swab_ldlm_intent);
4201         if (it == NULL) {
4202                 CERROR("Intent missing\n");
4203                 RETURN(req->rq_status = -EFAULT);
4204         }
4205
4206         LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
4207
4208         rc = mds_intent_prepare_reply_buffers(req, it);
4209
4210         if (rc)
4211                 RETURN(req->rq_status = rc);
4212
4213         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
4214         LASSERT(rep != NULL);
4215
4216         intent_set_disposition(rep, DISP_IT_EXECD);
4217
4218         /* execute policy */
4219         switch ((long)it->opc) {
4220         case IT_OPEN:
4221         case IT_CREAT|IT_OPEN:
4222                 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF,
4223                                             lock, NULL, lockh);
4224                 /* XXX swab here to assert that an mds_open reint
4225                  * packet is following */
4226                 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF, 
4227                                             lock, NULL, lockh);
4228                 rep->lock_policy_res2 = mds_reint(req, offset, lockh);
4229                 
4230                 if (rep->lock_policy_res2) {
4231                         /* 
4232                          * mds_open() returns ENOLCK where it should return
4233                          * zero, but it has no lock to return.
4234                          */
4235                         if (rep->lock_policy_res2 == ENOLCK)
4236                                 rep->lock_policy_res2 = 0;
4237
4238                         RETURN(ELDLM_LOCK_ABORTED);
4239                 }
4240                 
4241                 /*
4242                  * IT_OPEN may return lock on cross-node dentry that we want to
4243                  * hold during attr retrival -bzzz
4244                  */
4245                 if (lockh[0].cookie == 0)
4246                         RETURN(ELDLM_LOCK_ABORTED);
4247                 
4248                 break;
4249         case IT_LOOKUP:
4250                 getattr_part = MDS_INODELOCK_LOOKUP;
4251         case IT_CHDIR:
4252         case IT_GETATTR:
4253                 getattr_part |= MDS_INODELOCK_LOOKUP;
4254         case IT_READDIR:
4255                 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF, 
4256                                             lock, &new_lock, lockh);
4257                 rep->lock_policy_res2 = mds_getattr_lock(req, offset, lockh,
4258                                                          getattr_part);
4259                 /* FIXME: LDLM can set req->rq_status. MDS sets
4260                    policy_res{1,2} with disposition and status.
4261                    - replay: returns 0 & req->status is old status
4262                    - otherwise: returns req->status */
4263                 if (intent_disposition(rep, DISP_LOOKUP_NEG))
4264                         rep->lock_policy_res2 = 0;
4265                 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
4266                     rep->lock_policy_res2)
4267                         RETURN(ELDLM_LOCK_ABORTED);
4268                 if (req->rq_status != 0) {
4269                         LBUG();
4270                         rep->lock_policy_res2 = req->rq_status;
4271                         RETURN(ELDLM_LOCK_ABORTED);
4272                 }
4273                 break;
4274         case IT_UNLINK:
4275                 rc = mds_lock_and_check_slave(offset, req, lockh);
4276                 if ((rep->lock_policy_res2 = rc)) {
4277                         if (rc == ENOLCK)
4278                                 rep->lock_policy_res2 = 0;
4279                         RETURN(ELDLM_LOCK_ABORTED);
4280                 }
4281                 break;
4282         default:
4283                 CERROR("Unhandled intent "LPD64"\n", it->opc);
4284                 LBUG();
4285         }
4286
4287         /* By this point, whatever function we called above must have either
4288          * filled in 'lockh', been an intent replay, or returned an error.  We
4289          * want to allow replayed RPCs to not get a lock, since we would just
4290          * drop it below anyways because lock replay is done separately by the
4291          * client afterwards.  For regular RPCs we want to give the new lock to
4292          * the client instead of whatever lock it was about to get. */
4293         if (new_lock == NULL)
4294                 new_lock = ldlm_handle2lock(&lockh[0]);
4295         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
4296                 RETURN(0);
4297
4298         LASSERTF(new_lock != NULL, "op "LPX64" lockh "LPX64"\n",
4299                  it->opc, lockh[0].cookie);
4300
4301         /* If we've already given this lock to a client once, then we should
4302          * have no readers or writers.  Otherwise, we should have one reader
4303          * _or_ writer ref (which will be zeroed below) before returning the
4304          * lock to a client. */
4305         if (new_lock->l_export == req->rq_export) {
4306                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4307         } else {
4308                 LASSERT(new_lock->l_export == NULL);
4309                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4310         }
4311
4312         *lockp = new_lock;
4313
4314         if (new_lock->l_export == req->rq_export) {
4315                 /* Already gave this to the client, which means that we
4316                  * reconstructed a reply. */
4317                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4318                         MSG_RESENT);
4319                 RETURN(ELDLM_LOCK_REPLACED);
4320         }
4321
4322         /* Fixup the lock to be given to the client */
4323         lock_res_and_lock(new_lock);
4324         new_lock->l_readers = 0;
4325         new_lock->l_writers = 0;
4326
4327         new_lock->l_export = class_export_get(req->rq_export);
4328
4329         spin_lock(&new_lock->l_export->exp_ldlm_data.led_lock);
4330         list_add(&new_lock->l_export_chain,
4331                  &new_lock->l_export->exp_ldlm_data.led_held_locks);
4332         spin_unlock(&new_lock->l_export->exp_ldlm_data.led_lock);
4333
4334         new_lock->l_blocking_ast = lock->l_blocking_ast;
4335         new_lock->l_completion_ast = lock->l_completion_ast;
4336
4337         memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
4338                sizeof(lock->l_remote_handle));
4339
4340         new_lock->l_flags &= ~LDLM_FL_LOCAL;
4341
4342         unlock_res_and_lock(new_lock);
4343         LDLM_LOCK_PUT(new_lock);
4344
4345         RETURN(ELDLM_LOCK_REPLACED);
4346 }
4347
4348 int mds_attach(struct obd_device *dev, obd_count len, void *data)
4349 {
4350         struct lprocfs_static_vars lvars;
4351         int rc = 0;
4352         struct mds_obd *mds = &dev->u.mds;
4353
4354         spin_lock_init(&mds->mds_denylist_lock);
4355         INIT_LIST_HEAD(&mds->mds_denylist);
4356
4357         lprocfs_init_multi_vars(0, &lvars);
4358
4359         rc = lprocfs_obd_attach(dev, lvars.obd_vars);
4360         if (rc)
4361                 return rc;
4362
4363         return lprocfs_alloc_md_stats(dev, 0);
4364 }
4365
4366 int mds_detach(struct obd_device *dev)
4367 {
4368         lprocfs_free_md_stats(dev);
4369         return lprocfs_obd_detach(dev);
4370 }
4371
4372 int mdt_attach(struct obd_device *dev, obd_count len, void *data)
4373 {
4374         struct lprocfs_static_vars lvars;
4375
4376         lprocfs_init_multi_vars(1, &lvars);
4377         return lprocfs_obd_attach(dev, lvars.obd_vars);
4378 }
4379
4380 int mdt_detach(struct obd_device *dev)
4381 {
4382         return lprocfs_obd_detach(dev);
4383 }
4384
4385 static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
4386 {
4387         struct mds_obd *mds = &obd->u.mds;
4388         int rc = 0;
4389         ENTRY;
4390
4391         mds->mds_service =
4392                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4393                                 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
4394                                 MDS_SERVICE_WATCHDOG_TIMEOUT,
4395                                 mds_handle, "mds", obd->obd_proc_entry);
4396
4397         if (!mds->mds_service) {
4398                 CERROR("failed to start service\n");
4399                 RETURN(-ENOMEM);
4400         }
4401
4402         rc = ptlrpc_start_n_threads(obd, mds->mds_service, MDT_NUM_THREADS,
4403                                     "ll_mdt");
4404         if (rc)
4405                 GOTO(err_thread, rc);
4406
4407         mds->mds_setattr_service =
4408                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4409                                 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
4410                                 MDS_SERVICE_WATCHDOG_TIMEOUT,
4411                                 mds_handle, "mds_setattr",
4412                                 obd->obd_proc_entry);
4413         if (!mds->mds_setattr_service) {
4414                 CERROR("failed to start getattr service\n");
4415                 GOTO(err_thread, rc = -ENOMEM);
4416         }
4417
4418         rc = ptlrpc_start_n_threads(obd, mds->mds_setattr_service,
4419                                     MDT_NUM_THREADS, "ll_mdt_attr");
4420         if (rc)
4421                 GOTO(err_thread2, rc);
4422
4423         mds->mds_readpage_service =
4424                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4425                                 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
4426                                 MDS_SERVICE_WATCHDOG_TIMEOUT,
4427                                 mds_handle, "mds_readpage",
4428                                 obd->obd_proc_entry);
4429         if (!mds->mds_readpage_service) {
4430                 CERROR("failed to start readpage service\n");
4431                 GOTO(err_thread2, rc = -ENOMEM);
4432         }
4433
4434         rc = ptlrpc_start_n_threads(obd, mds->mds_readpage_service,
4435                                     MDT_NUM_THREADS, "ll_mdt_rdpg");
4436
4437         if (rc)
4438                 GOTO(err_thread3, rc);
4439
4440         mds->mds_close_service =
4441                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4442                                 MDS_CLOSE_PORTAL, MDC_REPLY_PORTAL,
4443                                 MDS_SERVICE_WATCHDOG_TIMEOUT,
4444                                 mds_handle, "mds_close",
4445                                 obd->obd_proc_entry);
4446         if (!mds->mds_close_service) {
4447                 CERROR("failed to start close service\n");
4448                 GOTO(err_thread3, rc = -ENOMEM);
4449         }
4450
4451         rc = ptlrpc_start_n_threads(obd, mds->mds_close_service,
4452                                     MDT_NUM_THREADS, "ll_mdt_clos");
4453
4454         if (rc)
4455                 GOTO(err_thread4, rc);
4456         RETURN(0);
4457
4458 err_thread4:
4459         ptlrpc_unregister_service(mds->mds_close_service);
4460 err_thread3:
4461         ptlrpc_unregister_service(mds->mds_readpage_service);
4462 err_thread2:
4463         ptlrpc_unregister_service(mds->mds_setattr_service);
4464 err_thread:
4465         ptlrpc_unregister_service(mds->mds_service);
4466         return rc;
4467 }
4468
4469 static int mdt_cleanup(struct obd_device *obd, int flags)
4470 {
4471         struct mds_obd *mds = &obd->u.mds;
4472         ENTRY;
4473
4474         ptlrpc_stop_all_threads(mds->mds_close_service);
4475         ptlrpc_unregister_service(mds->mds_close_service);
4476
4477         ptlrpc_stop_all_threads(mds->mds_readpage_service);
4478         ptlrpc_unregister_service(mds->mds_readpage_service);
4479
4480         ptlrpc_stop_all_threads(mds->mds_setattr_service);
4481         ptlrpc_unregister_service(mds->mds_setattr_service);
4482
4483         ptlrpc_stop_all_threads(mds->mds_service);
4484         ptlrpc_unregister_service(mds->mds_service);
4485
4486         RETURN(0);
4487 }
4488
4489 static struct dentry *mds_lvfs_id2dentry(__u64 ino, __u32 gen,
4490                                          __u64 gr, void *data)
4491 {
4492         struct lustre_id id;
4493         struct obd_device *obd = data;
4494         
4495         id_ino(&id) = ino;
4496         id_gen(&id) = gen;
4497         return mds_id2dentry(obd, &id, NULL);
4498 }
4499
4500 static int mds_get_info(struct obd_export *exp, __u32 keylen,
4501                         void *key, __u32 *valsize, void *val)
4502 {
4503         struct obd_device *obd;
4504         struct mds_obd *mds;
4505         int rc = 0;
4506         ENTRY;
4507
4508         obd = class_exp2obd(exp);
4509         mds = &obd->u.mds;
4510         
4511         if (obd == NULL) {
4512                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
4513                        exp->exp_handle.h_cookie);
4514                 RETURN(-EINVAL);
4515         }
4516
4517         if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
4518                 /* get log_context handle. */
4519                 struct llog_ctxt *ctxt;
4520                 unsigned long *llh_handle = val;
4521                 *valsize = sizeof(unsigned long);
4522                 ctxt = llog_get_context(&obd->obd_llogs, LLOG_REINT_ORIG_CTXT);
4523                 if (!ctxt) {
4524                         CERROR("Cannot get REINT llog context\n");
4525                         RETURN(-ENOENT);
4526                 }
4527                 *llh_handle = (unsigned long)ctxt;
4528                 RETURN(0);
4529         }
4530         if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
4531                 /* get log_context handle. */
4532                 unsigned long *sb = val;
4533                 *valsize = sizeof(unsigned long);
4534                 *sb = (unsigned long)obd->u.mds.mds_sb;
4535                 RETURN(0);
4536         }
4537
4538         if (keylen >= strlen("mdsize") && memcmp(key, "mdsize", keylen) == 0) {
4539                 __u32 *mdsize = val;
4540                 *valsize = sizeof(*mdsize);
4541                 *mdsize = mds->mds_max_mdsize;
4542                 RETURN(0);
4543         }
4544
4545         if (keylen >= strlen("mdsnum") && strcmp(key, "mdsnum") == 0) {
4546                 __u32 *mdsnum = val;
4547                 *valsize = sizeof(*mdsnum);
4548                 *mdsnum = mds->mds_num;
4549                 RETURN(0);
4550         }
4551
4552         if (keylen >= strlen("rootid") && strcmp(key, "rootid") == 0) {
4553                 struct lustre_id *rootid = val;
4554                 *valsize = sizeof(*rootid);
4555                 *rootid = mds->mds_rootid;
4556                 RETURN(0);
4557         }
4558         
4559         if (keylen >= strlen("lovdesc") && strcmp(key, "lovdesc") == 0) {
4560                 struct lov_desc *desc = val;
4561                 *valsize = sizeof(*desc);
4562                 *desc = mds->mds_dt_desc;
4563                 RETURN(0);
4564         }
4565         
4566         if (keylen >= strlen("getext") && strcmp(key, "getext") == 0) {
4567                 struct fid_extent *ext = val;
4568                 *valsize = sizeof(*ext);
4569
4570                 spin_lock(&mds->mds_fidext_lock);
4571                 ext->fe_width = MDS_FIDEXT_SIZE;
4572                 ext->fe_start = mds->mds_fidext_thumb + 1;
4573                 mds->mds_fidext_thumb += MDS_FIDEXT_SIZE;
4574                 spin_unlock(&mds->mds_fidext_lock);
4575                 
4576                 RETURN(0);
4577         }
4578         
4579         rc = fsfilt_get_info(obd, mds->mds_sb, NULL, keylen, key, valsize, val);
4580         if (rc)
4581                 CDEBUG(D_IOCTL, "invalid key\n");
4582         
4583         RETURN(rc);
4584 }
4585
4586 struct lvfs_callback_ops mds_lvfs_ops = {
4587         l_id2dentry:     mds_lvfs_id2dentry,
4588 };
4589
4590 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
4591                 int objcount, struct obd_ioobj *obj,
4592                 int niocount, struct niobuf_remote *nb,
4593                 struct niobuf_local *res,
4594                 struct obd_trans_info *oti, struct lustre_capa *capa);
4595
4596 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
4597                  int objcount, struct obd_ioobj *obj, int niocount,
4598                  struct niobuf_local *res, struct obd_trans_info *oti,
4599                  int rc);
4600
4601 /* use obd ops to offer management infrastructure */
4602 static struct obd_ops mds_obd_ops = {
4603         .o_owner           = THIS_MODULE,
4604         .o_attach          = mds_attach,
4605         .o_detach          = mds_detach,
4606         .o_connect         = mds_connect,
4607         .o_connect_post    = mds_connect_post,
4608         .o_init_export     = mds_init_export,
4609         .o_destroy_export  = mds_destroy_export,
4610         .o_disconnect      = mds_disconnect,
4611         .o_setup           = mds_setup,
4612         .o_precleanup      = mds_precleanup,
4613         .o_cleanup         = mds_cleanup,
4614         .o_process_config  = mds_process_config,
4615         .o_postrecov       = mds_postrecov,
4616         .o_statfs          = mds_obd_statfs,
4617         .o_iocontrol       = mds_iocontrol,
4618         .o_create          = mds_obd_create,
4619         .o_destroy         = mds_obd_destroy,
4620         .o_llog_init       = mds_llog_init,
4621         .o_llog_finish     = mds_llog_finish,
4622         .o_notify          = mds_notify,
4623         .o_get_info        = mds_get_info,
4624         .o_set_info        = mds_set_info,
4625         .o_preprw          = mds_preprw, 
4626         .o_commitrw        = mds_commitrw,
4627 };
4628
4629 static struct obd_ops mdt_obd_ops = {
4630         .o_owner           = THIS_MODULE,
4631         .o_attach          = mdt_attach,
4632         .o_detach          = mdt_detach,
4633         .o_setup           = mdt_setup,
4634         .o_cleanup         = mdt_cleanup,
4635 };
4636
4637 static int __init mds_init(void)
4638 {
4639         struct lprocfs_static_vars lvars;
4640         int rc = 0;
4641         ENTRY;
4642
4643         mds_init_lsd_cache();
4644         mds_init_rmtacl_upcall_cache();
4645
4646         lprocfs_init_multi_vars(0, &lvars);
4647         class_register_type(&mds_obd_ops, NULL, lvars.module_vars,
4648                             OBD_MDS_DEVICENAME);
4649         lprocfs_init_multi_vars(1, &lvars);
4650         class_register_type(&mdt_obd_ops, NULL, lvars.module_vars,
4651                             OBD_MDT_DEVICENAME);
4652
4653         rc = mds_capa_key_start_thread();
4654         if (rc) {
4655                 class_unregister_type(OBD_MDT_DEVICENAME);
4656                 class_unregister_type(OBD_MDS_DEVICENAME);
4657                 mds_cleanup_lsd_cache();
4658         }
4659                 
4660         mds_eck_timer.function = mds_capa_key_timer_callback;
4661         mds_eck_timer.data = 0;
4662         init_timer(&mds_eck_timer);
4663
4664         RETURN(rc);
4665 }
4666
4667 static void /*__exit*/ mds_exit(void)
4668 {
4669         mds_capa_key_stop_thread();
4670         mds_cleanup_rmtacl_upcall_cache();
4671         mds_cleanup_lsd_cache();
4672
4673         class_unregister_type(OBD_MDS_DEVICENAME);
4674         class_unregister_type(OBD_MDT_DEVICENAME);
4675 }
4676
4677 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
4678 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
4679 MODULE_LICENSE("GPL");
4680
4681 module_init(mds_init);
4682 module_exit(mds_exit);