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