Whamcloud - gitweb
fix regression introduced by capabilities.
[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, pack_off = *offset;
1103         ENTRY;
1104
1105         sizep = lustre_msg_buf(repmsg, pack_off++, 4);
1106         if (!sizep) {
1107                 CERROR("can't locate returned acl size buf\n");
1108                 RETURN(-EPROTO);
1109         }
1110         
1111         if (!inode->i_op->getxattr)
1112                 RETURN(0);
1113
1114         buflen = repmsg->buflens[pack_off];
1115         buf = lustre_msg_buf(repmsg, pack_off++, buflen);
1116         *offset = pack_off;
1117
1118         size = inode->i_op->getxattr(&de, XATTR_NAME_ACL_ACCESS, buf, buflen);
1119         if (size == -ENODATA || size == -EOPNOTSUPP)
1120                 RETURN(0);
1121         if (size < 0)
1122                 RETURN(size);
1123         LASSERT(size);
1124         body->valid |= OBD_MD_FLACL;
1125
1126         *sizep = cpu_to_le32(size);
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         int pack_off = *reply_off;
1136         __u32 lsd_perms;
1137
1138         LASSERT(inode->i_op);
1139         LASSERT(inode->i_op->permission);
1140         LASSERT(req->rq_export->exp_mds_data.med_remote);
1141
1142         perm = (struct mds_remote_perm *)
1143                        lustre_msg_buf(req->rq_repmsg, pack_off++, sizeof(perm));
1144         if (!perm) {
1145                 CERROR("no remote perm buf at offset %d\n", pack_off - 1);
1146                 return -EINVAL;
1147         }
1148
1149         memset(perm, 0, sizeof(*perm));
1150
1151         /* obtain authenticated uid/gid and LSD permissions, which
1152          * might be different from current process context, from LSD
1153          */
1154         lsd = mds_get_lsd(current->uid);
1155         if (!lsd) {
1156                 CWARN("can't LSD of uid %u\n", current->uid);
1157                 RETURN(-EPERM);
1158         }
1159
1160         perm->mrp_auth_uid = lsd->lsd_uid;
1161         perm->mrp_auth_gid = lsd->lsd_gid;
1162
1163         lsd_perms = mds_lsd_get_perms(lsd, 1, 0, req->rq_peer.peer_id.nid);
1164         if (lsd_perms & LSD_PERM_SETUID)
1165                 perm->mrp_allow_setuid = 1;
1166         if (lsd_perms & LSD_PERM_SETGID)
1167                 perm->mrp_allow_setgid = 1;
1168
1169         mds_put_lsd(lsd);
1170
1171         /* permission bits of current user
1172          * XXX this is low efficient, could we do it in one blow?
1173          */
1174         if (inode->i_op->permission(inode, MAY_EXEC, NULL) == 0)
1175                 perm->mrp_perm |= MAY_EXEC;
1176         if (inode->i_op->permission(inode, MAY_WRITE, NULL) == 0)
1177                 perm->mrp_perm |= MAY_WRITE;
1178         if (inode->i_op->permission(inode, MAY_READ, NULL) == 0)
1179                 perm->mrp_perm |= MAY_READ;
1180
1181         body->valid |= (OBD_MD_FLACL | OBD_MD_FLRMTACL);
1182         
1183         *reply_off = pack_off;
1184
1185         RETURN(0);
1186 }
1187
1188 int mds_pack_acl(struct ptlrpc_request *req, int *reply_off,
1189                  struct mds_body *body, struct inode *inode)
1190 {
1191         int rc;
1192
1193         if (!req->rq_export->exp_mds_data.med_remote)
1194                 rc = mds_pack_posix_acl(req->rq_repmsg, reply_off, body, inode);
1195         else
1196                 rc = mds_pack_remote_perm(req, reply_off, body, inode);
1197
1198         return rc;
1199 }
1200
1201 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
1202                                 struct ptlrpc_request *req, int req_off,
1203                                 struct mds_body *reqbody, int reply_off,
1204                                 struct mds_req_sec_desc *rsd)
1205 {
1206         struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1207         struct inode *inode = dentry->d_inode;
1208         struct mds_body *body;
1209         int rc = 0, offset = 0;
1210         ENTRY;
1211
1212         if (inode == NULL && !(dentry->d_flags & DCACHE_CROSS_REF))
1213                 RETURN(-ENOENT);
1214
1215         body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
1216         LASSERT(body != NULL);                 /* caller prepped reply */
1217
1218         if (dentry->d_flags & DCACHE_CROSS_REF) {
1219                 mds_pack_dentry2body(obd, body, dentry,
1220                                      (reqbody->valid & OBD_MD_FID) ? 1 : 0);
1221                 CDEBUG(D_OTHER, "cross reference: "DLID4"\n",
1222                        OLID4(&body->id1));
1223                 RETURN(0);
1224         }
1225         
1226         mds_pack_inode2body(obd, body, inode,
1227                             (reqbody->valid & OBD_MD_FID) ? 1 : 0);
1228
1229         if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
1230             (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
1231             
1232                 /* guessing what kind og attribute do we need. */
1233                 int is_mea = (S_ISDIR(inode->i_mode) && 
1234                     (reqbody->valid & OBD_MD_MEA) != 0);
1235                 
1236                 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1, 
1237                                  body, inode, 1, is_mea);
1238
1239                 /* if we have LOV EA data, the OST holds size, atime, mtime. */
1240                 if (!(body->valid & OBD_MD_FLEASIZE) &&
1241                     !(body->valid & OBD_MD_FLDIREA))
1242                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1243                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
1244         } else if (S_ISLNK(inode->i_mode) &&
1245                    (reqbody->valid & OBD_MD_LINKNAME) != 0) {
1246                 rc = mds_pack_link(dentry, req, body, reply_off);
1247         } else if (reqbody->valid & OBD_MD_FLXATTR) {
1248                 rc = mds_pack_xattr(dentry, req, body, req_off, reply_off);
1249         } else if (reqbody->valid & OBD_MD_FLXATTRLIST) {
1250                 rc = mds_pack_xattr_list(dentry, req, body, reply_off);
1251         }
1252         
1253         offset = reply_off + ((reqbody->valid & OBD_MD_FLEASIZE) ? 2 : 1);
1254         if (reqbody->valid & OBD_MD_FLACL) {
1255                 rc = mds_pack_acl(req, &offset, body, inode);
1256         }                
1257
1258         if (reqbody->valid & OBD_MD_FLKEY) {
1259                 rc = mds_pack_gskey(obd, req->rq_repmsg, &offset, 
1260                                     body, inode);
1261         }
1262         
1263         mds_pack_audit(obd, inode, body);
1264
1265         if (reqbody->valid & OBD_MD_CAPA) {
1266                 struct lustre_capa *req_capa;
1267
1268                 LASSERT(!(reqbody->valid & ~OBD_MD_CAPA));
1269                 LASSERT(S_ISREG(inode->i_mode));
1270
1271                 req_capa = lustre_swab_reqbuf(req, req_off + 1,
1272                                               sizeof(*req_capa),
1273                                               lustre_swab_lustre_capa);
1274                 if (req_capa == NULL) {
1275                         CERROR("Can't unpack capa\n");
1276                         RETURN(-EFAULT);
1277                 }
1278
1279                 offset = reply_off + 1;
1280                 rc = mds_pack_capa(obd, reqbody, req_capa, req->rq_repmsg,
1281                                    &offset, body);
1282         }
1283
1284         if (rc == 0)
1285                 mds_body_do_reverse_map(med, body);
1286
1287         RETURN(rc);
1288 }
1289
1290 static int mds_getattr_pack_msg_cf(struct ptlrpc_request *req,
1291                                    struct dentry *dentry,
1292                                    int offset)
1293 {
1294         int rc = 0, size[1] = {sizeof(struct mds_body)};
1295         ENTRY;
1296
1297         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
1298                 CERROR("failed MDS_GETATTR_PACK test\n");
1299                 req->rq_status = -ENOMEM;
1300                 RETURN(-ENOMEM);
1301         }
1302
1303         rc = lustre_pack_reply(req, 1, size, NULL);
1304         if (rc) {
1305                 CERROR("lustre_pack_reply failed: rc %d\n", rc);
1306                 GOTO(out, req->rq_status = rc);
1307         }
1308
1309         EXIT;
1310 out:
1311         return rc;
1312 }
1313
1314 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct dentry *de,
1315                                 int offset)
1316 {
1317         struct inode *inode = de->d_inode;
1318         struct mds_obd *mds = mds_req2mds(req);
1319         struct mds_body *body;
1320         int rc = 0, size[4] = {sizeof(*body)}, bufcount = 1;
1321         ENTRY;
1322
1323         body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*body));
1324         LASSERT(body != NULL);                 /* checked by caller */
1325         LASSERT_REQSWABBED(req, offset);       /* swabbed by caller */
1326
1327         if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
1328             (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
1329                 int rc;
1330                 
1331                 down(&inode->i_sem);
1332                 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0,
1333                                    ((body->valid & OBD_MD_MEA) ? EA_MEA : EA_LOV));
1334                 up(&inode->i_sem);
1335                 if (rc < 0) {
1336                         if (rc != -ENODATA && rc != -EOPNOTSUPP)
1337                                 CERROR("error getting inode %lu MD: rc = %d\n",
1338                                        inode->i_ino, rc);
1339                         size[bufcount] = 0;
1340                 } else if (rc > mds->mds_max_mdsize) {
1341                         size[bufcount] = 0;
1342                         CERROR("MD size %d larger than maximum possible %u\n",
1343                                rc, mds->mds_max_mdsize);
1344                 } else {
1345                         size[bufcount] = rc;
1346                 }
1347                 bufcount++;
1348         } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
1349                 if (inode->i_size + 1 != body->eadatasize)
1350                         CERROR("symlink size: %Lu, reply space: %d\n",
1351                                inode->i_size + 1, body->eadatasize);
1352                 size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
1353                 bufcount++;
1354                 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
1355                        inode->i_size + 1, body->eadatasize);
1356         } else if ((body->valid & OBD_MD_FLXATTR)) {
1357                 char *ea_name = lustre_msg_string(req->rq_reqmsg, 
1358                                                   offset + 1, 0);
1359                 rc = -EOPNOTSUPP;
1360
1361                 if (!strcmp(ea_name, XATTR_NAME_LUSTRE_ACL)) {
1362                         size[bufcount] = LUSTRE_ACL_SIZE_MAX;
1363                 } else {
1364                         if (inode->i_op && inode->i_op->getxattr)
1365                                 rc = inode->i_op->getxattr(de, ea_name,
1366                                                            NULL, 0);
1367
1368                         if (rc < 0) {
1369                                 if (rc != -ENODATA && rc != -EOPNOTSUPP)
1370                                         CERROR("error get inode %lu EA: %d\n",
1371                                                inode->i_ino, rc);
1372                                 size[bufcount] = 0;
1373                         } else {
1374                                 size[bufcount] = min_t(int,
1375                                                        body->eadatasize, rc);
1376                         }
1377                 }
1378                 bufcount++;
1379         } else if (body->valid & OBD_MD_FLXATTRLIST) {
1380                 rc = -EOPNOTSUPP;
1381                 if (inode->i_op && inode->i_op->getxattr) 
1382                         rc = inode->i_op->listxattr(de, NULL, 0);
1383
1384                 if (rc < 0) {
1385                         if (rc != -ENODATA && rc != -EOPNOTSUPP)
1386                                 CERROR("error getting inode %lu EA: rc = %d\n",
1387                                        inode->i_ino, rc);
1388                         size[bufcount] = 0;
1389                 } else {
1390                         size[bufcount] = min_t(int, body->eadatasize, rc);
1391                 }
1392                 bufcount++;
1393         }
1394         
1395         /* may co-exist with OBD_MD_FLEASIZE */
1396         if (body->valid & OBD_MD_FLACL) {
1397                 if (req->rq_export->exp_mds_data.med_remote) {
1398                         size[bufcount++] = sizeof(struct mds_remote_perm);
1399                 } else {
1400                         size[bufcount++] = sizeof(int);
1401                         size[bufcount++] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
1402                 }
1403         }
1404
1405         if (body->valid & OBD_MD_FLKEY) {
1406                 size[bufcount++] = sizeof(int);
1407                 size[bufcount++] = sizeof(struct crypto_key);
1408         }
1409
1410         if (body->valid & OBD_MD_CAPA)
1411                 size[bufcount++] = sizeof(struct lustre_capa);
1412
1413         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
1414                 CERROR("failed MDS_GETATTR_PACK test\n");
1415                 req->rq_status = -ENOMEM;
1416                 GOTO(out, rc = -ENOMEM);
1417         }
1418
1419         rc = lustre_pack_reply(req, bufcount, size, NULL);
1420         if (rc) {
1421                 CERROR("out of memory\n");
1422                 GOTO(out, req->rq_status = rc);
1423         }
1424
1425         EXIT;
1426  out:
1427         return rc;
1428 }
1429
1430 int mds_check_mds_num(struct obd_device *obd, struct inode *inode,
1431                       char *name, int namelen)
1432 {
1433         struct mea *mea = NULL;
1434         int mea_size, rc = 0;
1435         ENTRY;
1436         
1437         rc = mds_md_get_attr(obd, inode, &mea, &mea_size);
1438         if (rc)
1439                 RETURN(rc);
1440         if (mea != NULL && mea->mea_count) {
1441                 /*
1442                  * dir is already splitted, check if requested filename should
1443                  * live at this MDS or at another one.
1444                  */
1445                 int i = mea_name2idx(mea, name, namelen - 1);
1446                 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
1447                         CDEBUG(D_OTHER,
1448                                "inapropriate MDS(%d) for %s. should be "
1449                                "%lu(%d)\n", mea->mea_master, name, 
1450                                (unsigned long)id_group(&mea->mea_ids[i]), i);
1451                         rc = -ERESTART;
1452                 }
1453         }
1454
1455         if (mea)
1456                 OBD_FREE(mea, mea_size);
1457         RETURN(rc);
1458 }
1459
1460 int mds_getattr_size(struct obd_device *obd, struct dentry *dentry,
1461                      struct ptlrpc_request *req, struct mds_body *body)
1462 {
1463         struct inode *inode = dentry->d_inode;
1464         ENTRY;
1465
1466         LASSERT(body != NULL);
1467
1468         if (dentry->d_inode == NULL || !S_ISREG(inode->i_mode))
1469                 RETURN(0);
1470         
1471         if (obd->obd_recovering) {
1472                 CDEBUG(D_INODE, "size for "DLID4" is unknown yet (recovering)\n",
1473                        OLID4(&body->id1));
1474                 RETURN(0);
1475         }
1476
1477         if (atomic_read(&inode->i_writecount)) {
1478                 /* some one has opened the file for write.
1479                  * mds doesn't know actual size */
1480                 CDEBUG(D_INODE, "MDS doesn't know actual size for "DLID4"\n",
1481                        OLID4(&body->id1));
1482                 RETURN(0);
1483         }
1484         CDEBUG(D_INODE, "MDS returns "LPD64"/"LPD64" for"DLID4"\n",
1485                body->size, body->blocks, OLID4(&body->id1));
1486         body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
1487         RETURN(0);
1488 }
1489
1490 static int mds_getattr_lock(struct ptlrpc_request *req, int offset,
1491                             struct lustre_handle *child_lockh, int child_part)
1492 {
1493         struct obd_device *obd = req->rq_export->exp_obd;
1494         struct mds_obd *mds = &obd->u.mds;
1495         struct ldlm_reply *rep = NULL;
1496         struct lvfs_run_ctxt saved;
1497         struct mds_req_sec_desc *rsd;
1498         struct mds_body *body;
1499         struct dentry *dparent = NULL, *dchild = NULL;
1500         struct lvfs_ucred uc = {NULL, NULL,};
1501         struct lustre_handle parent_lockh[2] = {{0}, {0}};
1502         unsigned int namesize;
1503         int rc = 0, cleanup_phase = 0, resent_req = 0, update_mode, reply_offset;
1504         char *name = NULL;
1505         ENTRY;
1506
1507         LASSERT(!strcmp(obd->obd_type->typ_name, OBD_MDS_DEVICENAME));
1508         MD_COUNTER_INCREMENT(obd, getattr_lock);
1509
1510         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1511         if (!rsd) {
1512                 CERROR("Can't unpack security desc\n");
1513                 RETURN(-EFAULT);
1514         }
1515
1516         /* swab now, before anyone looks inside the request. */
1517         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1518                                   lustre_swab_mds_body);
1519         if (body == NULL) {
1520                 CERROR("Can't swab mds_body\n");
1521                 GOTO(cleanup, rc = -EFAULT);
1522         }
1523
1524         LASSERT_REQSWAB(req, offset + 1);
1525         name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
1526         if (name == NULL) {
1527                 CERROR("Can't unpack name\n");
1528                 GOTO(cleanup, rc = -EFAULT);
1529         }
1530         namesize = req->rq_reqmsg->buflens[offset + 1];
1531
1532         /* namesize less than 2 means we have empty name, probably came from
1533            revalidate by cfid, so no point in having name to be set */
1534         if (namesize <= 1)
1535                 name = NULL;
1536
1537         LASSERT (offset == 1 || offset == 3);
1538         if (offset == 3) {
1539                 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
1540                 reply_offset = 1;
1541         } else {
1542                 reply_offset = 0;
1543         }
1544
1545         rc = mds_init_ucred(&uc, req, rsd);
1546         if (rc) {
1547                 if (child_lockh->cookie == 0)
1548                         mds_audit_auth(req, &uc, AUDIT_STAT, &body->id1, 
1549                                        name, namesize);
1550                 GOTO(cleanup, rc);
1551         }
1552
1553         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1554         cleanup_phase = 1; /* kernel context */
1555         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
1556
1557         LASSERT(namesize > 0);
1558         if (child_lockh->cookie != 0) {
1559                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
1560                 resent_req = 1;
1561         }
1562 #if HAVE_LOOKUP_RAW
1563         if (body->valid == OBD_MD_FLID) {
1564                 struct mds_body *mds_reply;
1565                 int size = sizeof(*mds_reply);
1566                 struct inode *dir;
1567                 ino_t inum;
1568
1569                 dparent = mds_id2dentry(obd, &body->id1, NULL);
1570                 if (IS_ERR(dparent)) {
1571                         rc = PTR_ERR(dparent);
1572                         GOTO(cleanup, rc);
1573                 }
1574                 /*
1575                  * the user requested ONLY the inode number, so do a raw lookup.
1576                  */
1577                 rc = lustre_pack_reply(req, 1, &size, NULL);
1578                 if (rc) {
1579                         CERROR("out of memory\n");
1580                         l_dput(dparent);
1581                         GOTO(cleanup, rc);
1582                 }
1583                 dir  = dparent->d_inode;
1584                 LASSERT(dir->i_op->lookup_raw != NULL);
1585                 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
1586                 l_dput(dparent);
1587                 mds_reply = lustre_msg_buf(req->rq_repmsg, 0,
1588                                            sizeof(*mds_reply));
1589
1590                 id_ino(&mds_reply->id1) = inum;
1591                 mds_reply->valid = OBD_MD_FLID;
1592                 GOTO(cleanup, rc);
1593         }
1594 #endif
1595         if (resent_req == 0) {
1596                 LASSERT(id_fid(&body->id1) != 0);
1597                 if (name) {
1598                         rc = mds_get_parent_child_locked(obd, mds, &body->id1,
1599                                                          parent_lockh, &dparent,
1600                                                          LCK_PR, 
1601                                                          MDS_INODELOCK_UPDATE,
1602                                                          &update_mode, 
1603                                                          name, namesize,
1604                                                          child_lockh, &dchild, 
1605                                                          LCK_PR, child_part);
1606                         if (rc)
1607                                 GOTO(cleanup, rc);
1608                 
1609                         cleanup_phase = 2; /* dchild, dparent, locks */
1610                         
1611                         /*
1612                          * let's make sure this name should leave on this mds
1613                          * node.
1614                          */
1615                         rc = mds_check_mds_num(obd, dparent->d_inode, name, namesize);
1616                         if (rc)
1617                                 GOTO(cleanup, rc);
1618                 } else {
1619                         /* we have no dentry here, drop LOOKUP bit */
1620                         /* FIXME: we need MDS_INODELOCK_LOOKUP or not. */
1621                         child_part &= ~MDS_INODELOCK_LOOKUP;
1622                         CDEBUG(D_OTHER, "%s: retrieve attrs for "DLID4"\n",
1623                                obd->obd_name, OLID4(&body->id1));
1624
1625                         dchild = mds_id2locked_dentry(obd, &body->id1, NULL,
1626                                                       LCK_PR, parent_lockh,
1627                                                       &update_mode, NULL, 0, 
1628                                                       MDS_INODELOCK_UPDATE);
1629                         if (IS_ERR(dchild)) {
1630                                 CERROR("can't find inode with id "DLID4", err = %d\n", 
1631                                        OLID4(&body->id1), (int)PTR_ERR(dchild));
1632                                 GOTO(cleanup, rc = PTR_ERR(dchild));
1633                         }
1634                         memcpy(child_lockh, parent_lockh, sizeof(parent_lockh[0]));
1635                 }
1636         } else {
1637                 struct ldlm_lock *granted_lock;
1638
1639                 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
1640                 granted_lock = ldlm_handle2lock(child_lockh);
1641
1642                 LASSERTF(granted_lock != NULL, LPU64"/%lu lockh "LPX64"\n",
1643                          id_fid(&body->id1), (unsigned long)id_group(&body->id1),
1644                          child_lockh->cookie);
1645
1646                 if (name) {
1647                         /* usual named request */
1648                         dparent = mds_id2dentry(obd, &body->id1, NULL);
1649                         LASSERT(!IS_ERR(dparent));
1650                         dchild = ll_lookup_one_len(name, dparent, namesize - 1);
1651                         LASSERT(!IS_ERR(dchild));
1652                 } else {
1653                         /* client wants to get attr. by id */
1654                         dchild = mds_id2dentry(obd, &body->id1, NULL);
1655                         LASSERT(!IS_ERR(dchild));
1656                 }
1657                 LDLM_LOCK_PUT(granted_lock);
1658         }
1659
1660         cleanup_phase = 2; /* dchild, dparent, locks */
1661
1662         if (!DENTRY_VALID(dchild)) {
1663                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
1664                 /*
1665                  * in the intent case, the policy clears this error: the
1666                  * disposition is enough.
1667                  */
1668                 rc = -ENOENT;
1669                 GOTO(cleanup, rc);
1670         } else {
1671                 intent_set_disposition(rep, DISP_LOOKUP_POS);
1672         }
1673
1674         if (req->rq_repmsg == NULL) {
1675                 if (dchild->d_flags & DCACHE_CROSS_REF)
1676                         rc = mds_getattr_pack_msg_cf(req, dchild, offset);
1677                 else
1678                         rc = mds_getattr_pack_msg(req, dchild, offset);
1679                 if (rc != 0) {
1680                         CERROR ("mds_getattr_pack_msg: %d\n", rc);
1681                         GOTO (cleanup, rc);
1682                 }
1683         }
1684
1685         rc = mds_getattr_internal(obd, dchild, req, offset, body,
1686                                   reply_offset, rsd);
1687         if (rc)
1688                 GOTO(cleanup, rc); /* returns the lock to the client */
1689
1690         /* probably MDS knows actual size? */
1691         body = lustre_msg_buf(req->rq_repmsg, reply_offset, sizeof(*body));
1692         LASSERT(body != NULL);
1693         mds_getattr_size(obd, dchild, req, body);
1694
1695         GOTO(cleanup, rc);
1696
1697  cleanup:
1698         switch (cleanup_phase) {
1699         case 2:
1700                 if (resent_req == 0) {
1701                         if (rc && DENTRY_VALID(dchild))
1702                                 ldlm_lock_decref(child_lockh, LCK_PR);
1703                         if (name)
1704                                 ldlm_lock_decref(parent_lockh, LCK_PR);
1705 #ifdef S_PDIROPS
1706                         if (parent_lockh[1].cookie != 0)
1707                                 ldlm_lock_decref(parent_lockh + 1, update_mode);
1708 #endif
1709                         if (dparent)
1710                                 l_dput(dparent);
1711
1712                         /* audit stuff for getattr */
1713                         if (dchild->d_inode)
1714                                 mds_audit_stat(req, &body->id1, dchild, rc);
1715                 }
1716                 l_dput(dchild);
1717         case 1:
1718                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1719         default:
1720                 mds_exit_ucred(&uc);
1721         }
1722         return rc;
1723 }
1724
1725 static int mds_getattr(struct ptlrpc_request *req, int offset)
1726 {
1727         struct obd_device *obd = req->rq_export->exp_obd;
1728         struct lvfs_run_ctxt saved;
1729         struct dentry *de;
1730         struct mds_req_sec_desc *rsd;
1731         struct mds_body *body;
1732         struct lvfs_ucred uc = {NULL, NULL,};
1733         int rc = 0;
1734         ENTRY;
1735
1736         MD_COUNTER_INCREMENT(obd, getattr);
1737
1738         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1739         if (!rsd) {
1740                 CERROR("Can't unpack security desc\n");
1741                 RETURN(-EFAULT);
1742         }
1743
1744         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1745                                   lustre_swab_mds_body);
1746         if (body == NULL) {
1747                 CERROR ("Can't unpack body\n");
1748                 RETURN (-EFAULT);
1749         }
1750
1751         rc = mds_init_ucred(&uc, req, rsd);
1752         if (rc) {
1753                 mds_exit_ucred(&uc);
1754                 RETURN(rc);
1755         }
1756
1757         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1758         de = mds_id2dentry(obd, &body->id1, NULL);
1759         if (IS_ERR(de)) {
1760                 rc = req->rq_status = PTR_ERR(de);
1761                 GOTO(out_pop, rc);
1762         }
1763
1764         rc = mds_getattr_pack_msg(req, de, offset);
1765         if (rc != 0) {
1766                 CERROR("mds_getattr_pack_msg: %d\n", rc);
1767                 GOTO(out_pop, rc);
1768         }
1769
1770         req->rq_status = mds_getattr_internal(obd, de, req, offset, body,
1771                                               0, rsd);
1772         l_dput(de);
1773
1774         EXIT;
1775 out_pop:
1776         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1777         mds_exit_ucred(&uc);
1778         return rc;
1779 }
1780
1781 static int mds_access_check(struct ptlrpc_request *req, int offset)
1782 {
1783         struct obd_device *obd = req->rq_export->exp_obd;
1784         struct lvfs_run_ctxt saved;
1785         struct dentry *de;
1786         struct mds_req_sec_desc *rsd;
1787         struct mds_body *body;
1788         struct lvfs_ucred uc;
1789         int rep_size[2] = {sizeof(*body),
1790                            sizeof(struct mds_remote_perm)};
1791         int rc = 0, rep_offset;
1792         ENTRY;
1793
1794         if (!req->rq_export->exp_mds_data.med_remote) {
1795                 CERROR("from local client "LPU64"\n", req->rq_peer.peer_id.nid);
1796                 RETURN(-EINVAL);
1797         }
1798
1799         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1800         if (!rsd) {
1801                 CERROR("Can't unpack security desc\n");
1802                 RETURN(-EFAULT);
1803         }
1804
1805         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1806                                   lustre_swab_mds_body);
1807         if (body == NULL) {
1808                 CERROR ("Can't unpack body\n");
1809                 RETURN (-EFAULT);
1810         }
1811
1812         MD_COUNTER_INCREMENT(obd, access_check);
1813
1814         rc = mds_init_ucred(&uc, req, rsd);
1815         if (rc) {
1816                 CERROR("init ucred error: %d\n", rc);
1817                 RETURN(rc);
1818         }
1819         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1820
1821         de = mds_id2dentry(obd, &body->id1, NULL);
1822         if (IS_ERR(de)) {
1823                 CERROR("grab ino "LPU64": err %ld\n",
1824                        body->id1.li_stc.u.e3s.l3s_ino, PTR_ERR(de));
1825                 GOTO(out_pop, rc = PTR_ERR(de));
1826         }
1827
1828         rc = lustre_pack_reply(req, 2, rep_size, NULL);
1829         if (rc) {
1830                 CERROR("pack reply error: %d\n", rc);
1831                 GOTO(out_dput, rc = -EINVAL);
1832         }
1833
1834         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1835         LASSERT(body);
1836
1837         rep_offset = 1;
1838         rc = mds_pack_remote_perm(req, &rep_offset, body, de->d_inode);
1839
1840         EXIT;
1841
1842 out_dput:
1843         l_dput(de);
1844 out_pop:
1845         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1846         mds_exit_ucred(&uc);
1847         return rc;
1848 }
1849
1850 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1851                           unsigned long max_age)
1852 {
1853         int rc;
1854         ENTRY;
1855
1856         spin_lock(&obd->obd_osfs_lock);
1857         rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
1858         if (rc == 0)
1859                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1860         spin_unlock(&obd->obd_osfs_lock);
1861
1862         RETURN(rc);
1863 }
1864
1865 static int mds_statfs(struct ptlrpc_request *req)
1866 {
1867         struct obd_device *obd = req->rq_export->exp_obd;
1868         int rc, size = sizeof(struct obd_statfs);
1869         ENTRY;
1870
1871         /* This will trigger a watchdog timeout */
1872         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
1873                          (MDS_SERVICE_WATCHDOG_TIMEOUT / 1000) + 1);
1874
1875         rc = lustre_pack_reply(req, 1, &size, NULL);
1876         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
1877                 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
1878                 GOTO(out, rc);
1879         }
1880
1881         OBD_COUNTER_INCREMENT(obd, statfs);
1882
1883         /* We call this so that we can cache a bit - 1 jiffie worth */
1884         rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
1885                             jiffies - HZ);
1886         if (rc) {
1887                 CERROR("mds_obd_statfs failed: rc %d\n", rc);
1888                 GOTO(out, rc);
1889         }
1890
1891         EXIT;
1892 out:
1893         req->rq_status = rc;
1894         return rc;
1895 }
1896
1897 static int mds_sync(struct ptlrpc_request *req, int offset)
1898 {
1899         struct obd_device *obd = req->rq_export->exp_obd;
1900         struct mds_obd *mds = &obd->u.mds;
1901         struct mds_body *body;
1902         int rc, size = sizeof(*body);
1903         ENTRY;
1904
1905         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1906                                   lustre_swab_mds_body);
1907         if (body == NULL)
1908                 GOTO(out, rc = -EPROTO);
1909
1910         rc = lustre_pack_reply(req, 1, &size, NULL);
1911         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
1912                 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
1913                 GOTO(out, rc);
1914         }
1915
1916         if (id_ino(&body->id1) == 0) {
1917                 /* an id of zero is taken to mean "sync whole filesystem" */
1918                 rc = fsfilt_sync(obd, mds->mds_sb);
1919                 if (rc)
1920                         GOTO(out, rc);
1921         } else {
1922                 /* just any file to grab fsync method - "file" arg unused */
1923                 struct file *file = mds->mds_rcvd_filp;
1924                 struct mds_body *rep_body;
1925                 struct dentry *de;
1926
1927                 de = mds_id2dentry(obd, &body->id1, NULL);
1928                 if (IS_ERR(de))
1929                         GOTO(out, rc = PTR_ERR(de));
1930
1931                 rc = file->f_op->fsync(NULL, de, 1);
1932                 if (rc)
1933                         GOTO(out, rc);
1934
1935                 rep_body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep_body));
1936                 mds_pack_inode2body(obd, rep_body, de->d_inode,
1937                                     (body->valid & OBD_MD_FID) ? 1 : 0);
1938                 l_dput(de);
1939         }
1940
1941         EXIT;
1942 out:
1943         req->rq_status = rc;
1944         return rc;
1945 }
1946
1947 /* mds_readpage does not take a DLM lock on the inode, because the client must
1948  * already have a PR lock.
1949  *
1950  * If we were to take another one here, a deadlock will result, if another
1951  * thread is already waiting for a PW lock. */
1952 static int mds_readpage(struct ptlrpc_request *req, int offset)
1953 {
1954         struct obd_device *obd = req->rq_export->exp_obd;
1955         struct vfsmount *mnt;
1956         struct dentry *de;
1957         struct file *file;
1958         struct mds_req_sec_desc *rsd;
1959         struct mds_body *body, *repbody;
1960         struct lvfs_run_ctxt saved;
1961         int rc, size = sizeof(*repbody);
1962         struct lvfs_ucred uc = {NULL, NULL,};
1963         ENTRY;
1964
1965         rc = lustre_pack_reply(req, 1, &size, NULL);
1966         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
1967                 CERROR("mds: out of memory\n");
1968                 GOTO(out, rc = -ENOMEM);
1969         }
1970
1971         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
1972         if (!rsd) {
1973                 CERROR("Can't unpack security desc\n");
1974                 GOTO (out, rc = -EFAULT);
1975         }
1976
1977         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1978                                   lustre_swab_mds_body);
1979         if (body == NULL) {
1980                 CERROR("Can't unpack body\n");
1981                 GOTO (out, rc = -EFAULT);
1982         }
1983
1984         rc = mds_init_ucred(&uc, req, rsd);
1985         if (rc) {
1986                 mds_audit_auth(req, &uc, AUDIT_READDIR, &body->id1,
1987                                NULL, 0);
1988                 GOTO(out, rc);
1989         }
1990
1991         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1992         de = mds_id2dentry(obd, &body->id1, &mnt);
1993         if (IS_ERR(de))
1994                 GOTO(out_pop, rc = PTR_ERR(de));
1995
1996         CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
1997
1998         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1999         /* note: in case of an error, dentry_open puts dentry */
2000         if (IS_ERR(file))
2001                 GOTO(out_pop, rc = PTR_ERR(file));
2002
2003         /* body->size is actually the offset -eeb */
2004         if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
2005                 CERROR("offset "LPU64" not on a block boundary of %lu\n",
2006                        body->size, de->d_inode->i_blksize);
2007                 GOTO(out_file, rc = -EFAULT);
2008         }
2009
2010         /* body->nlink is actually the #bytes to read -eeb */
2011         if (body->nlink & (de->d_inode->i_blksize - 1)) {
2012                 CERROR("size %u is not multiple of blocksize %lu\n",
2013                        body->nlink, de->d_inode->i_blksize);
2014                 GOTO(out_file, rc = -EFAULT);
2015         }
2016
2017         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
2018         repbody->size = file->f_dentry->d_inode->i_size;
2019         repbody->valid = OBD_MD_FLSIZE;
2020
2021         /* to make this asynchronous make sure that the handling function
2022            doesn't send a reply when this function completes. Instead a
2023            callback function would send the reply */
2024         /* body->size is actually the offset -eeb */
2025         rc = mds_sendpage(req, file, body->size, body->nlink);
2026
2027         EXIT;
2028 out_file:
2029         filp_close(file, 0);
2030 out_pop:
2031         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2032 out:
2033         mds_exit_ucred(&uc);
2034         req->rq_status = rc;
2035         return 0;
2036 }
2037
2038 int mds_read_md(struct obd_device *obd, struct lustre_id *id, 
2039                 char **data, int *datalen)
2040 {
2041         struct dentry *dentry;
2042         struct mds_obd *mds = &obd->u.mds;
2043         int rc = 0, mea = 0;
2044         char *ea;
2045         ENTRY;
2046
2047         LASSERT(id);
2048         LASSERT(obd);
2049         
2050         dentry = mds_id2dentry(obd, id, NULL);
2051         if (IS_ERR(dentry))
2052                 GOTO(out, rc = PTR_ERR(dentry));
2053
2054         if (!dentry->d_inode) {
2055                 CERROR("Can't find object "DLID4".\n",
2056                        OLID4(id));
2057                 GOTO(out_dentry, rc = -EINVAL);
2058         }
2059         if (S_ISDIR(dentry->d_inode->i_mode)) {
2060                 *datalen = obd_packmd(mds->mds_md_exp, NULL, NULL);
2061                 mea = 1; 
2062         } else {
2063                 *datalen = obd_packmd(mds->mds_dt_exp, NULL, NULL); 
2064                 mea = 0;
2065         }
2066         OBD_ALLOC(ea, *datalen);
2067         if (!ea) {
2068                 *datalen = 0;
2069                 GOTO(out_dentry, rc = PTR_ERR(dentry));
2070         } 
2071         *data = ea;
2072         down(&dentry->d_inode->i_sem);
2073         rc = fsfilt_get_md(obd, dentry->d_inode, *data, *datalen,
2074                            (mea ? EA_MEA : EA_LOV));
2075         up(&dentry->d_inode->i_sem);
2076         
2077         if (rc < 0) 
2078                 CERROR("Error %d reading eadata for ino %lu\n",
2079                         rc, dentry->d_inode->i_ino);
2080 out_dentry:
2081         l_dput(dentry);
2082 out:
2083         RETURN(rc);
2084 }
2085 EXPORT_SYMBOL(mds_read_md);
2086
2087 int mds_reint(struct ptlrpc_request *req, int offset,
2088               struct lustre_handle *lockh)
2089 {
2090         struct mds_update_record *rec;
2091         struct mds_req_sec_desc *rsd;
2092         int rc;
2093         ENTRY;
2094
2095         OBD_ALLOC(rec, sizeof(*rec));
2096         if (rec == NULL)
2097                 RETURN(-ENOMEM);
2098
2099         rsd = lustre_swab_mds_secdesc(req, MDS_REQ_SECDESC_OFF);
2100         if (!rsd) {
2101                 CERROR("Can't unpack security desc\n");
2102                 GOTO(out, rc = -EFAULT);
2103         }
2104
2105         rc = mds_update_unpack(req, offset, rec);
2106         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
2107                 CERROR("invalid record\n");
2108                 GOTO(out, req->rq_status = -EINVAL);
2109         }
2110
2111         rc = mds_init_ucred(&rec->ur_uc, req, rsd);
2112         if (rc) {
2113                 audit_op code = AUDIT_NONE;
2114                 char * au_name = NULL;
2115                 int au_len = 0;
2116                 switch (rec->ur_opcode) {
2117                         case REINT_SETATTR:
2118                                 code = AUDIT_SETATTR;
2119                                 break;
2120                         case REINT_CREATE:
2121                                 code = AUDIT_CREATE;
2122                                 au_name = rec->ur_name;
2123                                 au_len = rec->ur_namelen;
2124                                 break;
2125                         case REINT_LINK:
2126                                 code = AUDIT_LINK;
2127                                 break;
2128                         case REINT_UNLINK:
2129                                 code = AUDIT_UNLINK;
2130                                 break;
2131                         case REINT_RENAME:
2132                                 code = AUDIT_RENAME;
2133                                 break;
2134                         case REINT_OPEN:
2135                                 au_name = rec->ur_name;
2136                                 au_len = rec->ur_namelen;
2137                                 code = AUDIT_OPEN;
2138                                 break;
2139                         default:
2140                                 CERROR("Wrong opcode in reint\n");
2141                                 LBUG();
2142                 }
2143
2144                 mds_audit_auth(req, &rec->ur_uc, code, rec->ur_id1,  
2145                                au_name, au_len);
2146                 GOTO(out, rc);
2147         }
2148
2149         /* rc will be used to interrupt a for loop over multiple records */
2150         rc = mds_reint_rec(rec, offset, req, lockh);
2151
2152         /* audit stuff for OPEN */
2153         if (offset == 3 && rec->ur_opcode == REINT_OPEN)
2154                 mds_audit_open(req, rec, rc);
2155
2156  out:
2157         mds_exit_ucred(&rec->ur_uc);
2158         OBD_FREE(rec, sizeof(*rec));
2159         RETURN(rc);
2160 }
2161
2162 static int mds_filter_recovery_request(struct ptlrpc_request *req,
2163                                        struct obd_device *obd, int *process)
2164 {
2165         switch (req->rq_reqmsg->opc) {
2166         case MDS_CONNECT: /* This will never get here, but for completeness. */
2167         case OST_CONNECT: /* This will never get here, but for completeness. */
2168         case MDS_DISCONNECT:
2169         case OST_DISCONNECT:
2170                *process = 1;
2171                RETURN(0);
2172
2173         case MDS_CLOSE:
2174         case MDS_SYNC: /* used in unmounting */
2175         case OBD_PING:
2176         case MDS_REINT:
2177         case LDLM_ENQUEUE:
2178         case OST_CREATE:
2179                 *process = target_queue_recovery_request(req, obd);
2180                 RETURN(0);
2181
2182         default:
2183                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2184                 *process = 0;
2185                 /* XXX what should we set rq_status to here? */
2186                 req->rq_status = -EAGAIN;
2187                 RETURN(ptlrpc_error(req));
2188         }
2189 }
2190
2191 static char *reint_names[] = {
2192         [REINT_SETATTR] "setattr",
2193         [REINT_CREATE]  "create",
2194         [REINT_LINK]    "link",
2195         [REINT_UNLINK]  "unlink",
2196         [REINT_RENAME]  "rename",
2197         [REINT_OPEN]    "open",
2198 };
2199
2200 #define FILTER_VALID_FLAGS (OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLGENER  | \
2201                             OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ| \
2202                             OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME| \
2203                             OBD_MD_FLID) 
2204
2205 static void reconstruct_create(struct ptlrpc_request *req)
2206 {
2207         struct mds_export_data *med = &req->rq_export->exp_mds_data;
2208         struct mds_client_data *mcd = med->med_mcd;
2209         struct dentry *dentry;
2210         struct ost_body *body;
2211         struct lustre_id id;
2212         int rc;
2213         ENTRY;
2214
2215         /* copy rc, transno and disp; steal locks */
2216         mds_req_from_mcd(req, mcd);
2217         if (req->rq_status) {
2218                 EXIT;
2219                 return;
2220         }
2221
2222         id_gen(&id) = 0;
2223         id_group(&id) = 0;
2224
2225         id_ino(&id) = mcd->mcd_last_data;
2226         LASSERT(id_ino(&id) != 0);
2227
2228         dentry = mds_id2dentry(req2obd(req), &id, NULL);
2229         if (IS_ERR(dentry)) {
2230                 CERROR("can't find inode "LPU64"\n", id_ino(&id));
2231                 req->rq_status = PTR_ERR(dentry);
2232                 EXIT;
2233                 return;
2234         }
2235
2236         CWARN("reconstruct reply for x"LPU64" (remote ino) "LPU64" -> %lu/%u\n",
2237               req->rq_xid, id_ino(&id), dentry->d_inode->i_ino,
2238               dentry->d_inode->i_generation);
2239
2240         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
2241         obdo_from_inode(&body->oa, dentry->d_inode, FILTER_VALID_FLAGS);
2242         body->oa.o_id = dentry->d_inode->i_ino;
2243         body->oa.o_generation = dentry->d_inode->i_generation;
2244         body->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
2245
2246         down(&dentry->d_inode->i_sem);
2247         rc = mds_read_inode_sid(req2obd(req), dentry->d_inode, &id);
2248         up(&dentry->d_inode->i_sem);
2249         if (rc) {
2250                 CERROR("Can't read inode self id, inode %lu, "
2251                        "rc %d\n", dentry->d_inode->i_ino, rc);
2252                 id_fid(&id) = 0;
2253         }
2254
2255         body->oa.o_fid = id_fid(&id);
2256         body->oa.o_mds = id_group(&id);
2257         l_dput(dentry);
2258
2259         EXIT;
2260 }
2261
2262 static int mds_inode_init_acl(struct obd_device *obd, void *handle,
2263                               struct dentry *de, void *xattr, int xattr_size)
2264 {
2265         struct inode *inode = de->d_inode;
2266         struct posix_acl *acl;
2267         mode_t mode;
2268         int rc = 0;
2269
2270         LASSERT(handle);
2271         LASSERT(inode);
2272         LASSERT(xattr);
2273         LASSERT(xattr_size > 0);
2274
2275         if (!inode->i_op->getxattr || !inode->i_op->setxattr) {
2276                 CERROR("backend fs dosen't support xattr\n");
2277                 return -EOPNOTSUPP;
2278         }
2279
2280         /* set default acl */
2281         if (S_ISDIR(inode->i_mode)) {
2282                 rc = inode->i_op->setxattr(de, XATTR_NAME_ACL_DEFAULT,
2283                                            xattr, xattr_size, 0);
2284                 if (rc) {
2285                         CERROR("set default acl err: %d\n", rc);
2286                         return rc;
2287                 }
2288         }
2289
2290         /* set access acl */
2291         acl = posix_acl_from_xattr(xattr, xattr_size);
2292         if (acl == NULL || IS_ERR(acl)) {
2293                 CERROR("insane attr data\n");
2294                 return PTR_ERR(acl);
2295         }
2296
2297         if (posix_acl_valid(acl)) {
2298                 CERROR("default acl not valid: %d\n", rc);
2299                 rc = -EFAULT;
2300                 goto out;
2301         }
2302
2303         mode = inode->i_mode;
2304         rc = posix_acl_create_masq(acl, &mode);
2305         if (rc < 0) {
2306                 CERROR("create masq err %d\n", rc);
2307                 goto out;
2308         }
2309
2310         if (inode->i_mode != mode) {
2311                 struct iattr iattr = { .ia_valid = ATTR_MODE,
2312                                        .ia_mode = mode };
2313                 int rc2;
2314
2315                 rc2 = fsfilt_setattr(obd, de, handle, &iattr, 0);
2316                 if (rc2) {
2317                         CERROR("setattr mode err: %d\n", rc2);
2318                         rc = rc2;
2319                         goto out;
2320                 }
2321         }
2322
2323         if (rc > 0) {
2324                 /* we didn't change acl except mode bits of some
2325                  * entries, so should be fit into original size.
2326                  */
2327                 rc = posix_acl_to_xattr(acl, xattr, xattr_size);
2328                 LASSERT(rc > 0);
2329
2330                 rc = inode->i_op->setxattr(de, XATTR_NAME_ACL_ACCESS,
2331                                            xattr, xattr_size, 0);
2332                 if (rc)
2333                         CERROR("set access acl err: %d\n", rc);
2334         }
2335 out:
2336         posix_acl_release(acl);
2337         return rc;
2338 }
2339
2340 static int mdt_obj_create(struct ptlrpc_request *req)
2341 {
2342         struct obd_device *obd = req->rq_export->exp_obd;
2343         struct mds_obd *mds = &obd->u.mds;
2344         struct ost_body *body, *repbody;
2345         void *acl = NULL;
2346         int acl_size;
2347         char idname[LL_ID_NAMELEN];
2348         int size = sizeof(*repbody);
2349         struct inode *parent_inode;
2350         struct lvfs_run_ctxt saved;
2351         int rc, cleanup_phase = 0;
2352         struct dentry *new = NULL;
2353         struct dentry_params dp;
2354         int mealen, flags = 0;
2355         struct lvfs_ucred uc;
2356         struct lustre_id id;
2357         struct mea *mea;
2358         void *handle = NULL;
2359         unsigned long cr_inum = 0;
2360         __u64 fid = 0;
2361         ENTRY;
2362        
2363         DEBUG_REQ(D_HA, req, "create remote object");
2364         parent_inode = mds->mds_unnamed_dir->d_inode;
2365
2366         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
2367                                   lustre_swab_ost_body);
2368         if (body == NULL)
2369                 RETURN(-EFAULT);
2370
2371         /* acl data is packed transparently, no swab here */
2372         LASSERT(req->rq_reqmsg->bufcount >= 2);
2373         acl_size = req->rq_reqmsg->buflens[1];
2374         if (acl_size) {
2375                 acl = lustre_msg_buf(req->rq_reqmsg, 1, acl_size);
2376                 if (!acl) {
2377                         CERROR("No default acl buf?\n");
2378                         RETURN(-EFAULT);
2379                 }
2380         }
2381
2382         rc = lustre_pack_reply(req, 1, &size, NULL);
2383         if (rc)
2384                 RETURN(rc);
2385
2386         MDS_CHECK_RESENT(req, reconstruct_create(req));
2387
2388         uc.luc_lsd = NULL;
2389         uc.luc_ginfo = NULL;
2390         uc.luc_uid = body->oa.o_uid;
2391         uc.luc_gid = body->oa.o_gid;
2392         uc.luc_fsuid = body->oa.o_uid;
2393         uc.luc_fsgid = body->oa.o_gid;
2394
2395         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2396         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
2397
2398         /* in REPLAY case inum should be given (client or other MDS fills it) */
2399         if (body->oa.o_id && ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2400             (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY))) {
2401                 /*
2402                  * this is re-create request from MDS holding directory name.
2403                  * we have to lookup given ino/gen first. if it exists (good
2404                  * case) then there is nothing to do. if it does not then we
2405                  * have to recreate it.
2406                  */
2407                 id_ino(&id) = body->oa.o_id;
2408                 id_gen(&id) = body->oa.o_generation;
2409  
2410                 new = mds_id2dentry(obd, &id, NULL);
2411                 if (!IS_ERR(new) && new->d_inode) {
2412                         struct lustre_id sid;
2413                                 
2414                         CDEBUG(D_OTHER, "mkdir repairing %lu/%lu\n",
2415                                (unsigned long)id_ino(&id),
2416                                (unsigned long)id_gen(&id));
2417                         
2418                         obdo_from_inode(&repbody->oa, new->d_inode,
2419                                         FILTER_VALID_FLAGS);
2420                         
2421                         repbody->oa.o_id = new->d_inode->i_ino;
2422                         repbody->oa.o_generation = new->d_inode->i_generation;
2423                         repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
2424                         cleanup_phase = 1;
2425
2426                         down(&new->d_inode->i_sem);
2427                         rc = mds_read_inode_sid(obd, new->d_inode, &sid);
2428                         up(&new->d_inode->i_sem);
2429                         if (rc) {
2430                                 CERROR("Can't read inode self id "
2431                                        "inode %lu, rc %d.\n",
2432                                        new->d_inode->i_ino, rc);
2433                                 GOTO(cleanup, rc);
2434                         }
2435
2436                         repbody->oa.o_fid = id_fid(&sid);
2437                         repbody->oa.o_mds = id_group(&sid);
2438                         LASSERT(id_fid(&sid) != 0);
2439
2440                         /* 
2441                          * here we could use fid passed in body->oa.o_fid and
2442                          * thus avoid mds_read_inode_sid().
2443                          */
2444                         cr_inum = new->d_inode->i_ino;
2445                         GOTO(cleanup, rc = 0);
2446                 }
2447         }
2448         
2449         down(&parent_inode->i_sem);
2450         handle = fsfilt_start(obd, parent_inode, FSFILT_OP_MKDIR, NULL);
2451         if (IS_ERR(handle)) {
2452                 up(&parent_inode->i_sem);
2453                 CERROR("fsfilt_start() failed, rc = %d\n",
2454                        (int)PTR_ERR(handle));
2455                 GOTO(cleanup, rc = PTR_ERR(handle));
2456         }
2457         cleanup_phase = 1; /* transaction */
2458
2459 repeat:
2460         rc = sprintf(idname, "%u.%u", ll_insecure_random_int(), current->pid);
2461         new = lookup_one_len(idname, mds->mds_unnamed_dir, rc);
2462         if (IS_ERR(new)) {
2463                 CERROR("%s: can't lookup new inode (%s) for mkdir: %d\n",
2464                        obd->obd_name, idname, (int) PTR_ERR(new));
2465                 fsfilt_commit(obd, mds->mds_sb, new->d_inode, handle, 0);
2466                 up(&parent_inode->i_sem);
2467                 RETURN(PTR_ERR(new));
2468         } else if (new->d_inode) {
2469                 CERROR("%s: name exists. repeat\n", obd->obd_name);
2470                 goto repeat;
2471         }
2472         if ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2473              lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2474                 fid = body->oa.o_fid;
2475         } else { 
2476                 fid = mds_alloc_fid(obd);
2477         }
2478         new->d_fsdata = (void *)&dp;
2479         dp.p_inum = 0;
2480         dp.p_ptr = req;
2481         
2482         dp.p_fid = fid;
2483         dp.p_group = mds->mds_num;
2484
2485         if ((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) ||
2486             (body->oa.o_flags & OBD_FL_RECREATE_OBJS)) {
2487                 LASSERT(body->oa.o_id != 0);
2488                 dp.p_inum = body->oa.o_id;
2489                 DEBUG_REQ(D_HA, req, "replay create obj %lu/%lu",
2490                           (unsigned long)body->oa.o_id,
2491                           (unsigned long)body->oa.o_generation);
2492         }
2493
2494         rc = vfs_mkdir(parent_inode, new, body->oa.o_mode);
2495         if (rc == 0) {
2496                 if (acl) {
2497                         rc = mds_inode_init_acl(obd, handle, new,
2498                                                 acl, acl_size);
2499                         if (rc) {
2500                                 up(&parent_inode->i_sem);
2501                                 GOTO(cleanup, rc);
2502                         }
2503                 }
2504                 if ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2505                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2506                         new->d_inode->i_generation = body->oa.o_generation;
2507                         mark_inode_dirty(new->d_inode);
2508                         
2509                         /*
2510                          * avoiding asserts in cache flush case, as
2511                          * @body->oa.o_id should be zero.
2512                          */
2513                         if (body->oa.o_id) {
2514                                 LASSERTF(body->oa.o_id == new->d_inode->i_ino, 
2515                                          "BUG 3550: failed to recreate obj "
2516                                          LPU64" -> %lu\n", body->oa.o_id,
2517                                          new->d_inode->i_ino);
2518                                 
2519                                 LASSERTF(body->oa.o_generation == 
2520                                          new->d_inode->i_generation,
2521                                          "BUG 3550: failed to recreate obj/gen "
2522                                          LPU64"/%u -> %lu/%u\n", body->oa.o_id,
2523                                          body->oa.o_generation,
2524                                          new->d_inode->i_ino, 
2525                                          new->d_inode->i_generation);
2526                         }
2527                 }
2528                 
2529                 obdo_from_inode(&repbody->oa, new->d_inode, FILTER_VALID_FLAGS);
2530                 repbody->oa.o_id = new->d_inode->i_ino;
2531                 repbody->oa.o_generation = new->d_inode->i_generation;
2532                 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER | OBD_MD_FID;
2533
2534                 if ((body->oa.o_flags & OBD_FL_RECREATE_OBJS) ||
2535                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2536                         LASSERT(body->oa.o_id != 0);
2537                         LASSERT(body->oa.o_fid != 0);
2538                 }
2539                 
2540                 mds_inode2id(obd, &id, new->d_inode, fid);
2541                 mds_update_inode_ids(obd, new->d_inode, handle, &id, 
2542                                      body->oa.o_valid & OBD_MD_FLID ?
2543                                      NULL : obdo_id(&body->oa));
2544
2545                 /* initializing o_fid after it is allocated. */
2546                 repbody->oa.o_fid = id_fid(&id);
2547                 repbody->oa.o_mds = id_group(&id);
2548
2549                 rc = fsfilt_del_dir_entry(obd, new);
2550                 up(&parent_inode->i_sem);
2551                 if (rc) {
2552                         CERROR("can't remove name for object: %d\n", rc);
2553                         GOTO(cleanup, rc);
2554                 }
2555                 
2556                 cleanup_phase = 2; /* created directory object */
2557
2558                 CDEBUG(D_OTHER, "created dirobj: %lu/%lu mode %o\n",
2559                        (unsigned long)new->d_inode->i_ino,
2560                        (unsigned long)new->d_inode->i_generation,
2561                        (unsigned)new->d_inode->i_mode);
2562                 cr_inum = new->d_inode->i_ino;
2563         } else {
2564                 up(&parent_inode->i_sem);
2565                 CERROR("%s: can't create dirobj: %d\n", obd->obd_name, rc);
2566                 GOTO(cleanup, rc);
2567         }
2568
2569         if (body->oa.o_valid & OBD_MD_FLID) {
2570                 /* this is new object for splitted dir. We have to prevent
2571                  * recursive splitting on it -bzzz */
2572                 mealen = obd_size_diskmd(mds->mds_md_exp, NULL);
2573
2574                 OBD_ALLOC(mea, mealen);
2575                 if (mea == NULL)
2576                         GOTO(cleanup, rc = -ENOMEM);
2577
2578                 mea->mea_magic = MEA_MAGIC_ALL_CHARS;
2579                 mea->mea_master = body->oa.o_mds;      /* master mds num */
2580                 mea->mea_count = 0;
2581                 
2582                 obdo2id(&mea->mea_ids[body->oa.o_mds], &body->oa);
2583
2584                 down(&new->d_inode->i_sem);
2585                 rc = fsfilt_set_md(obd, new->d_inode, handle,
2586                                    mea, mealen, EA_MEA);
2587                 up(&new->d_inode->i_sem);
2588                 if (rc)
2589                         CERROR("fsfilt_set_md() failed, "
2590                                "rc = %d\n", rc);
2591
2592                 OBD_FREE(mea, mealen);
2593                 
2594                 CDEBUG(D_OTHER, "%s: mark non-splittable %lu/%u - %d\n",
2595                        obd->obd_name, new->d_inode->i_ino,
2596                        new->d_inode->i_generation, flags);
2597         } else if (body->oa.o_easize) {
2598                 /* we pass LCK_EX to split routine to signal that we have
2599                  * exclusive access to the directory. simple because nobody
2600                  * knows it already exists -bzzz */
2601                 rc = mds_try_to_split_dir(obd, new, NULL,
2602                                           body->oa.o_easize, LCK_EX);
2603                 if (rc < 0) {
2604                         CERROR("Can't split directory %lu, error = %d.\n",
2605                                new->d_inode->i_ino, rc);
2606                 } else {
2607                         rc = 0;
2608                 }
2609         }
2610
2611         EXIT;
2612 cleanup:
2613         if (rc == 0 && (body->oa.o_flags & OBD_FL_REINT)) {
2614                 rc = mds_fidmap_add(obd, &id);
2615                 if (rc < 0) {
2616                         CERROR("can't create fid->ino mapping, "
2617                                "err %d\n", rc);
2618                 } else {
2619                         rc = 0;
2620                 }
2621         }
2622                 
2623         switch (cleanup_phase) {
2624         case 2: /* object has been created, but we'll may want to replay it later */
2625                 if (rc == 0)
2626                         ptlrpc_require_repack(req);
2627         case 1: /* transaction */
2628                 rc = mds_finish_transno(mds, parent_inode, handle,
2629                                         req, rc, cr_inum);
2630         }
2631
2632         l_dput(new);
2633         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
2634         return rc;
2635 }
2636
2637 static int mdt_get_info(struct ptlrpc_request *req)
2638 {
2639         struct obd_export *exp = req->rq_export;
2640         int keylen, rc = 0;
2641         char *key;
2642         ENTRY;
2643
2644         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
2645         if (key == NULL) {
2646                 DEBUG_REQ(D_HA, req, "no get_info key");
2647                 RETURN(-EFAULT);
2648         }
2649         keylen = req->rq_reqmsg->buflens[0];
2650
2651         if ((keylen < strlen("mdsize") || strcmp(key, "mdsize") != 0) &&
2652             (keylen < strlen("mdsnum") || strcmp(key, "mdsnum") != 0) &&
2653             (keylen < strlen("lovdesc") || strcmp(key, "lovdesc") != 0) &&
2654             (keylen < strlen("getext") || strcmp(key, "getext") != 0) &&
2655             (keylen < strlen("rootid") || strcmp(key, "rootid") != 0))
2656                 RETURN(-EPROTO);
2657
2658         if (keylen >= strlen("rootid") && !strcmp(key, "rootid")) {
2659                 struct lustre_id *reply;
2660                 int size = sizeof(*reply);
2661                 
2662                 rc = lustre_pack_reply(req, 1, &size, NULL);
2663                 if (rc)
2664                         RETURN(rc);
2665
2666                 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
2667                 rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
2668         } else if (keylen >= strlen("lovdesc") && !strcmp(key, "lovdesc")) {
2669                 struct lov_desc *reply;
2670                 int size = sizeof(*reply);
2671                 
2672                 rc = lustre_pack_reply(req, 1, &size, NULL);
2673                 if (rc)
2674                         RETURN(rc);
2675
2676                 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
2677                 rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
2678         } else if (keylen >= strlen("getext") && !strcmp(key, "getext")) {
2679                 struct fid_extent *reply;
2680                 int size = sizeof(*reply);
2681                 
2682                 rc = lustre_pack_reply(req, 1, &size, NULL);
2683                 if (rc)
2684                         RETURN(rc);
2685
2686                 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
2687                 rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
2688         } else {
2689                 obd_id *reply;
2690                 int size = sizeof(*reply);
2691                 
2692                 rc = lustre_pack_reply(req, 1, &size, NULL);
2693                 if (rc)
2694                         RETURN(rc);
2695
2696                 reply = lustre_msg_buf(req->rq_repmsg, 0, size);
2697                 rc = obd_get_info(exp, keylen, key, (__u32 *)&size, reply);
2698         }
2699
2700         req->rq_repmsg->status = 0;
2701         RETURN(rc);
2702 }
2703
2704 static int mds_set_info(struct obd_export *exp, __u32 keylen,
2705                         void *key, __u32 vallen, void *val)
2706 {
2707         struct obd_device *obd;
2708         struct mds_obd *mds;
2709         int rc = 0;
2710         ENTRY;
2711
2712         obd = class_exp2obd(exp);
2713         if (obd == NULL) {
2714                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2715                        exp->exp_handle.h_cookie);
2716                 RETURN(-EINVAL);
2717         }
2718
2719         mds = &obd->u.mds;
2720         if (keylen == 5 && memcmp(key, "audit", 5) == 0) {
2721                 rc = mds_set_audit(obd, val);
2722                 RETURN(rc);
2723         } else if (keylen >= strlen("ids") && memcmp(key, "ids", keylen) == 0) {
2724                 struct lustre_id *ids = (struct lustre_id *)val;
2725                 struct dentry *de;
2726                 struct inode *inode;
2727                 void *handle;
2728                 int err;
2729
2730                 de = mds_id2dentry(obd, ids, NULL);
2731                 if (IS_ERR(de)) {
2732                         rc = PTR_ERR(de);
2733                         CERROR("lookup by an id error rc=%d\n ", rc);
2734                         RETURN(rc);
2735                 }
2736                 inode = de->d_inode;
2737                 if (inode == NULL)
2738                         GOTO(out_put, rc = -ENOENT);
2739                 
2740                 down(&inode->i_sem);
2741                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
2742                 if (IS_ERR(handle)) {
2743                         up(&inode->i_sem);
2744                         GOTO(out_put, rc = PTR_ERR(handle));
2745                 }
2746
2747                 rc = mds_update_inode_ids(obd, inode, handle, NULL, ids + 1);
2748                 
2749                 err = fsfilt_commit(obd, mds->mds_sb, inode, handle, 
2750                                     exp->exp_sync);
2751                 if (err) {
2752                         CERROR("error committing transaction: %d\n", err);
2753                         if (!rc) rc = err;
2754                 }
2755                 up(&inode->i_sem);
2756 out_put:
2757                 l_dput(de);
2758                 RETURN(rc);
2759         }
2760
2761         if (keylen >= strlen("crypto_type") &&
2762              memcmp(key, "crypto_type", keylen) == 0) {
2763                 rc = mds_set_crypto_type(obd, val, vallen); 
2764                 RETURN(rc);
2765         }
2766         
2767         if (keylen >= strlen("setext") && !memcmp(key, "setext", keylen)) {
2768                 struct fid_extent *ext = val;
2769
2770                 CDEBUG(D_IOCTL, "set last fid to extent ["LPD64"-"LPD64"]\n",
2771                        ext->fe_start, ext->fe_width);
2772
2773                 /* set lastfid into fid extent start. All next object creates
2774                  * will use that fid. */
2775                 mds_set_last_fid(obd, ext->fe_start);
2776
2777                 /* setting the same extent to OSC to avoid ids intersecting in
2778                  * object ids, as all cache MDSs have the same group 0. */
2779                 rc = obd_set_info(mds->mds_dt_exp, strlen("setext"),
2780                                   "setext", sizeof(*ext), ext);
2781                 if (rc) {
2782                         CERROR("can't set extent ["LPD64"-"LPD64"] to %s, "
2783                                "err %d\n", ext->fe_start, ext->fe_width,
2784                                mds->mds_dt_exp->exp_obd->obd_name, rc);
2785                 }
2786                 RETURN(rc);
2787         }
2788         CDEBUG(D_IOCTL, "invalid key\n");
2789         RETURN(-EINVAL);
2790 }
2791
2792 static int mdt_set_info(struct ptlrpc_request *req)
2793 {
2794         char *key, *val;
2795         struct obd_export *exp = req->rq_export;
2796         int keylen, rc = 0, vallen = 0;
2797         ENTRY;
2798
2799         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
2800         if (key == NULL) {
2801                 DEBUG_REQ(D_HA, req, "no set_info key");
2802                 RETURN(-EFAULT);
2803         }
2804         keylen = req->rq_reqmsg->buflens[0];
2805
2806         if ((keylen == strlen("crypto_type") &&
2807             memcmp(key, "crypto_type", keylen) == 0)) {
2808                 rc = lustre_pack_reply(req, 0, NULL, NULL);
2809                 if (rc)
2810                         RETURN(rc);
2811                 
2812                 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
2813                 vallen = req->rq_reqmsg->buflens[1];
2814
2815                 rc = obd_set_info(exp, keylen, key, vallen, val);
2816                 req->rq_repmsg->status = 0;
2817                 RETURN(rc);
2818         } else if (keylen == 5 && memcmp(key, "audit", 5) == 0) {
2819                 struct audit_attr_msg msg, *p;
2820                 int rc = 0;
2821
2822                 rc = lustre_pack_reply(req, 0, NULL, NULL);
2823                 if (rc)
2824                         RETURN(rc);
2825                 
2826                 p = lustre_swab_reqbuf(req, 1, sizeof(msg),
2827                                        lustre_swab_audit_attr);
2828
2829                 msg = *p;
2830                 CDEBUG(D_INFO, "Get new audit setting 0x%x\n", (__u32)msg.attr);
2831                 rc = obd_set_info(exp, keylen, key, sizeof(msg), &msg);
2832
2833                 req->rq_repmsg->status = rc;
2834                 RETURN(rc);
2835         } else if (keylen == strlen("ids") &&
2836                    memcmp(key, "ids", keylen) == 0) {
2837                 struct lustre_id *id, ids[2];
2838                 
2839                 rc = lustre_pack_reply(req, 0, NULL, NULL);
2840                 if (rc)
2841                         RETURN(rc);
2842                 id = lustre_swab_reqbuf(req, 1, sizeof(struct lustre_id), 
2843                                         lustre_swab_lustre_id);
2844                 ids[0] = *id;
2845                 id = lustre_swab_reqbuf(req, 2, sizeof(struct lustre_id),
2846                                         lustre_swab_lustre_id);
2847                 ids[1] = *id;
2848
2849                 rc = obd_set_info(exp, keylen, key, vallen, ids);
2850                 req->rq_repmsg->status = rc;
2851                 RETURN(rc);
2852         } else if (keylen == strlen("setext") && 
2853                    memcmp(key, "setext", keylen) == 0) {
2854                 rc = lustre_pack_reply(req, 0, NULL, NULL);
2855                 if (rc)
2856                         RETURN(rc);
2857                 
2858                 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
2859                 vallen = req->rq_reqmsg->buflens[1];
2860
2861                 rc = obd_set_info(exp, keylen, key, vallen, val);
2862                 req->rq_repmsg->status = 0;
2863                 RETURN(rc);
2864         }
2865
2866         CDEBUG(D_IOCTL, "invalid key\n");
2867         RETURN(-EINVAL);
2868 }
2869
2870 static void mds_revoke_export_locks(struct obd_export *exp)
2871 {
2872         struct list_head *locklist = &exp->exp_ldlm_data.led_held_locks;
2873         struct list_head  rpc_list;
2874         struct ldlm_lock *lock, *next;
2875         struct ldlm_lock_desc desc;
2876
2877         /* don't do this for local client */
2878         if (!exp->u.eu_mds_data.med_remote)
2879                 return;
2880
2881         /* don't revoke locks during recovery */
2882         if (exp->exp_obd->obd_recovering)
2883                 return;
2884
2885         ENTRY;
2886         INIT_LIST_HEAD(&rpc_list);
2887
2888         spin_lock(&exp->exp_ldlm_data.led_lock);
2889         list_for_each_entry_safe(lock, next, locklist, l_export_chain) {
2890
2891                 lock_res_and_lock(lock);
2892                 if (lock->l_req_mode != lock->l_granted_mode) {
2893                         unlock_res_and_lock(lock);
2894                         continue;
2895                 }
2896
2897                 LASSERT(lock->l_resource);
2898                 if (lock->l_resource->lr_type != LDLM_IBITS &&
2899                     lock->l_resource->lr_type != LDLM_PLAIN) {
2900                         unlock_res_and_lock(lock);
2901                         continue;
2902                 }
2903
2904                 if (lock->l_flags & LDLM_FL_AST_SENT) {
2905                         unlock_res_and_lock(lock);
2906                         continue;
2907                 }
2908
2909                 LASSERT(lock->l_blocking_ast);
2910                 LASSERT(!lock->l_blocking_lock);
2911
2912                 lock->l_flags |= LDLM_FL_AST_SENT;
2913                 unlock_res_and_lock(lock);
2914
2915                 list_move(&lock->l_export_chain, &rpc_list);
2916         }
2917         spin_unlock(&exp->exp_ldlm_data.led_lock);
2918
2919         while (!list_empty(&rpc_list)) {
2920                 lock = list_entry(rpc_list.next, struct ldlm_lock,
2921                                   l_export_chain);
2922                 list_del_init(&lock->l_export_chain);
2923
2924                 /* the desc just pretend to exclusive */
2925                 ldlm_lock2desc(lock, &desc);
2926                 desc.l_req_mode = LCK_EX;
2927                 desc.l_granted_mode = 0;
2928
2929                 lock->l_blocking_ast(lock, &desc, NULL, LDLM_CB_BLOCKING);
2930         }
2931         EXIT;
2932 }
2933
2934 static int mds_msg_check_version(struct lustre_msg *msg)
2935 {
2936         int rc;
2937
2938         switch (msg->opc) {
2939         case MDS_CONNECT:
2940         case MDS_DISCONNECT:
2941         case OBD_PING:
2942                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2943                 if (rc)
2944                         CERROR("bad opc %u version %08x, expecting %08x\n",
2945                                msg->opc, msg->version, LUSTRE_OBD_VERSION);
2946                 break;
2947         case MDS_STATFS:
2948         case MDS_GETSTATUS:
2949         case MDS_GETATTR:
2950         case MDS_GETATTR_LOCK:
2951         case MDS_ACCESS_CHECK:
2952         case MDS_READPAGE:
2953         case MDS_REINT:
2954         case MDS_CLOSE:
2955         case MDS_DONE_WRITING:
2956         case MDS_PIN:
2957         case MDS_SYNC:
2958                 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2959                 if (rc)
2960                         CERROR("bad opc %u version %08x, expecting %08x\n",
2961                                msg->opc, msg->version, LUSTRE_MDS_VERSION);
2962                 break;
2963         case LDLM_ENQUEUE:
2964         case LDLM_CONVERT:
2965         case LDLM_BL_CALLBACK:
2966         case LDLM_CP_CALLBACK:
2967                 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2968                 if (rc)
2969                         CERROR("bad opc %u version %08x, expecting %08x\n",
2970                                msg->opc, msg->version, LUSTRE_DLM_VERSION);
2971                 break;
2972         case OBD_LOG_CANCEL:
2973         case LLOG_ORIGIN_HANDLE_OPEN:
2974         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2975         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2976         case LLOG_ORIGIN_HANDLE_READ_HEADER:
2977         case LLOG_ORIGIN_HANDLE_CLOSE:
2978         case LLOG_CATINFO:
2979                 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2980                 if (rc)
2981                         CERROR("bad opc %u version %08x, expecting %08x\n",
2982                                msg->opc, msg->version, LUSTRE_LOG_VERSION);
2983                 break;
2984         case OST_CREATE:
2985         case OST_WRITE:
2986         case OST_GET_INFO:
2987         case OST_SET_INFO:
2988                 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2989                 if (rc)
2990                         CERROR("bad opc %u version %08x, expecting %08x\n",
2991                                msg->opc, msg->version, LUSTRE_OBD_VERSION);
2992                 break;
2993         case SEC_INIT:
2994         case SEC_INIT_CONTINUE:
2995         case SEC_FINI:
2996                 rc = 0;
2997                 break;
2998         default:
2999                 CERROR("MDS unknown opcode %d\n", msg->opc);
3000                 rc = -ENOTSUPP;
3001                 break;
3002         }
3003
3004         return rc;
3005 }
3006
3007 int mds_handle(struct ptlrpc_request *req)
3008 {
3009         int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
3010         struct obd_device *obd = NULL;
3011         struct mds_obd *mds = NULL; /* quell gcc overwarning */
3012         int rc = 0;
3013         ENTRY;
3014
3015         OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
3016
3017         rc = mds_msg_check_version(req->rq_reqmsg);
3018         if (rc) {
3019                 CERROR("MDS drop mal-formed request\n");
3020                 RETURN(rc);
3021         }
3022
3023         /* Security opc should NOT trigger any recovery events */
3024         if (req->rq_reqmsg->opc == SEC_INIT ||
3025             req->rq_reqmsg->opc == SEC_INIT_CONTINUE) {
3026                 if (req->rq_export) {
3027                         mds_req_add_idmapping(req,
3028                                               &req->rq_export->exp_mds_data);
3029                         mds_revoke_export_locks(req->rq_export);
3030                 }
3031                 GOTO(out, rc = 0);
3032         } else if (req->rq_reqmsg->opc == SEC_FINI) {
3033                 if (req->rq_export) {
3034                         mds_req_del_idmapping(req,
3035                                               &req->rq_export->exp_mds_data);
3036                         mds_revoke_export_locks(req->rq_export);
3037                 }
3038                 GOTO(out, rc = 0);
3039         }
3040
3041         LASSERT(current->journal_info == NULL);
3042         /* XXX identical to OST */
3043         if (req->rq_reqmsg->opc != MDS_CONNECT) {
3044                 struct mds_export_data *med;
3045                 int recovering;
3046
3047                 if (req->rq_export == NULL) {
3048                         CERROR("operation %d on unconnected MDS from %s\n",
3049                                req->rq_reqmsg->opc,
3050                                req->rq_peerstr);
3051                         req->rq_status = -ENOTCONN;
3052                         GOTO(out, rc = -ENOTCONN);
3053                 }
3054
3055                 med = &req->rq_export->exp_mds_data;
3056                 obd = req->rq_export->exp_obd;
3057                 mds = &obd->u.mds;
3058
3059                 /* sanity check: if the xid matches, the request must
3060                  * be marked as a resent or replayed */
3061                 if (req->rq_xid == le64_to_cpu(med->med_mcd->mcd_last_xid) ||
3062                    req->rq_xid == le64_to_cpu(med->med_mcd->mcd_last_close_xid)) {
3063                         LASSERTF(lustre_msg_get_flags(req->rq_reqmsg) &
3064                                  (MSG_RESENT | MSG_REPLAY),
3065                                  "rq_xid "LPU64" matches last_xid, "
3066                                  "expected RESENT flag\n",
3067                                  req->rq_xid);
3068                 }
3069                 /* else: note the opposite is not always true; a
3070                  * RESENT req after a failover will usually not match
3071                  * the last_xid, since it was likely never
3072                  * committed. A REPLAYed request will almost never
3073                  * match the last xid, however it could for a
3074                  * committed, but still retained, open. */
3075
3076                 spin_lock_bh(&obd->obd_processing_task_lock);
3077                 recovering = obd->obd_recovering;
3078                 spin_unlock_bh(&obd->obd_processing_task_lock);
3079                 if (recovering) {
3080                         rc = mds_filter_recovery_request(req, obd,
3081                                                          &should_process);
3082                         if (rc || should_process == 0) {
3083                                 RETURN(rc);
3084                         } else if (should_process < 0) {
3085                                 req->rq_status = should_process;
3086                                 rc = ptlrpc_error(req);
3087                                 RETURN(rc);
3088                         }
3089                 }
3090         }
3091
3092         switch (req->rq_reqmsg->opc) {
3093         case MDS_CONNECT:
3094                 DEBUG_REQ(D_INODE, req, "connect");
3095                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
3096                 rc = target_handle_connect(req);
3097                 if (!rc) {
3098                         struct mds_export_data *med;
3099
3100                         LASSERT(req->rq_export);
3101                         med = &req->rq_export->u.eu_mds_data;
3102                         mds_init_export_data(req, med);
3103                         mds_req_add_idmapping(req, med);
3104
3105                         /* Now that we have an export, set mds. */
3106                         obd = req->rq_export->exp_obd;
3107                         mds = mds_req2mds(req);
3108                 }
3109                 break;
3110
3111         case MDS_DISCONNECT:
3112                 DEBUG_REQ(D_INODE, req, "disconnect");
3113                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
3114                 rc = target_handle_disconnect(req);
3115                 req->rq_status = rc;            /* superfluous? */
3116                 break;
3117
3118         case MDS_GETSTATUS:
3119                 DEBUG_REQ(D_INODE, req, "getstatus");
3120                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
3121                 rc = mds_getstatus(req);
3122                 break;
3123
3124         case MDS_GETATTR:
3125                 DEBUG_REQ(D_INODE, req, "getattr");
3126                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
3127                 rc = mds_getattr(req, MDS_REQ_REC_OFF);
3128                 break;
3129
3130         case MDS_ACCESS_CHECK:
3131                 DEBUG_REQ(D_INODE, req, "access_check");
3132                 OBD_FAIL_RETURN(OBD_FAIL_MDS_ACCESS_CHECK_NET, 0);
3133                 rc = mds_access_check(req, MDS_REQ_REC_OFF);
3134                 break;
3135
3136         case MDS_GETATTR_LOCK: {
3137                 struct lustre_handle lockh;
3138                 DEBUG_REQ(D_INODE, req, "getattr_lock");
3139                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_LOCK_NET, 0);
3140
3141                 /* If this request gets a reconstructed reply, we won't be
3142                  * acquiring any new locks in mds_getattr_lock, so we don't
3143                  * want to cancel.
3144                  */
3145                 lockh.cookie = 0;
3146                 rc = mds_getattr_lock(req, MDS_REQ_REC_OFF, &lockh,
3147                                       MDS_INODELOCK_UPDATE);
3148                 /* this non-intent call (from an ioctl) is special */
3149                 req->rq_status = rc;
3150                 if (rc == 0 && lockh.cookie)
3151                         ldlm_lock_decref(&lockh, LCK_PR);
3152                 break;
3153         }
3154         case MDS_STATFS:
3155                 DEBUG_REQ(D_INODE, req, "statfs");
3156                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
3157                 rc = mds_statfs(req);
3158                 break;
3159
3160         case MDS_READPAGE:
3161                 DEBUG_REQ(D_INODE, req, "readpage");
3162                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
3163                 rc = mds_readpage(req, MDS_REQ_REC_OFF);
3164
3165                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
3166                         if (req->rq_reply_state) {
3167                                 lustre_free_reply_state (req->rq_reply_state);
3168                                 req->rq_reply_state = NULL;
3169                         }
3170                         RETURN(0);
3171                 }
3172
3173                 break;
3174         case MDS_REINT: {
3175                 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_REC_OFF,
3176                                              sizeof (*opcp));
3177                 __u32  opc;
3178                 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
3179                                mds->mds_max_cookiesize};
3180                 int bufcount;
3181
3182                 /* NB only peek inside req now; mds_reint() will swab it */
3183                 if (opcp == NULL) {
3184                         CERROR ("Can't inspect opcode\n");
3185                         rc = -EINVAL;
3186                         break;
3187                 }
3188                 opc = *opcp;
3189                 if (lustre_msg_swabbed (req->rq_reqmsg))
3190                         __swab32s(&opc);
3191
3192                 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
3193                           (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
3194                            reint_names[opc] == NULL) ? reint_names[opc] :
3195                                                        "unknown opcode");
3196
3197                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
3198
3199                 if (opc == REINT_UNLINK || opc == REINT_RENAME)
3200                         bufcount = 3;
3201                 else if (opc == REINT_OPEN)
3202                         bufcount = 2;
3203                 else
3204                         bufcount = 1;
3205
3206                 /* for SETATTR: I have different reply setting for
3207                  * remote setfacl, so delay the reply buffer allocation.
3208                  */
3209                 if (opc != REINT_SETATTR) {
3210                         rc = lustre_pack_reply(req, bufcount, size, NULL);
3211                         if (rc)
3212                                 break;
3213                 }
3214
3215                 rc = mds_reint(req, MDS_REQ_REC_OFF, NULL);
3216                 fail = OBD_FAIL_MDS_REINT_NET_REP;
3217                 break;
3218         }
3219
3220         case MDS_CLOSE:
3221                 DEBUG_REQ(D_INODE, req, "close");
3222                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
3223                 rc = mds_close(req, MDS_REQ_REC_OFF);
3224                 break;
3225
3226         case MDS_DONE_WRITING:
3227                 DEBUG_REQ(D_INODE, req, "done_writing");
3228                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
3229                 rc = mds_done_writing(req, MDS_REQ_REC_OFF);
3230                 break;
3231
3232         case MDS_PIN:
3233                 DEBUG_REQ(D_INODE, req, "pin");
3234                 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
3235                 rc = mds_pin(req, MDS_REQ_REC_OFF);
3236                 break;
3237
3238         case MDS_SYNC:
3239                 DEBUG_REQ(D_INODE, req, "sync");
3240                 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
3241                 rc = mds_sync(req, MDS_REQ_REC_OFF);
3242                 break;
3243         case MDS_PARSE_ID:
3244                 DEBUG_REQ(D_INODE, req, "parseid");
3245                 rc = mds_parse_id(req);
3246                 break;
3247         case OBD_PING:
3248                 DEBUG_REQ(D_INODE, req, "ping");
3249                 rc = target_handle_ping(req);
3250                 break;
3251
3252         case OBD_LOG_CANCEL:
3253                 CDEBUG(D_INODE, "log cancel\n");
3254                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
3255                 rc = -ENOTSUPP; /* la la la */
3256                 break;
3257
3258         case LDLM_ENQUEUE:
3259                 DEBUG_REQ(D_INODE, req, "enqueue");
3260                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
3261                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
3262                                          ldlm_server_blocking_ast, NULL);
3263                 fail = OBD_FAIL_LDLM_REPLY;
3264                 break;
3265         case LDLM_CONVERT:
3266                 DEBUG_REQ(D_INODE, req, "convert");
3267                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
3268                 rc = ldlm_handle_convert(req);
3269                 break;
3270         case LDLM_BL_CALLBACK:
3271         case LDLM_CP_CALLBACK:
3272                 DEBUG_REQ(D_INODE, req, "callback");
3273                 CERROR("callbacks should not happen on MDS\n");
3274                 LBUG();
3275                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
3276                 break;
3277         case LLOG_ORIGIN_HANDLE_OPEN:
3278                 DEBUG_REQ(D_INODE, req, "llog_init");
3279                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
3280                 rc = llog_origin_handle_open(req);
3281                 break;
3282         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3283                 DEBUG_REQ(D_INODE, req, "llog next block");
3284                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
3285                 rc = llog_origin_handle_next_block(req);
3286                 break;
3287         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3288                 DEBUG_REQ(D_INODE, req, "llog prev block");
3289                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
3290                 rc = llog_origin_handle_prev_block(req);
3291                 break;
3292         case LLOG_ORIGIN_HANDLE_READ_HEADER:
3293                 DEBUG_REQ(D_INODE, req, "llog read header");
3294                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
3295                 rc = llog_origin_handle_read_header(req);
3296                 break;
3297         case LLOG_ORIGIN_HANDLE_CLOSE:
3298                 DEBUG_REQ(D_INODE, req, "llog close");
3299                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
3300                 rc = llog_origin_handle_close(req);
3301                 break;
3302         case OST_CREATE:
3303                 DEBUG_REQ(D_INODE, req, "ost_create");
3304                 rc = mdt_obj_create(req);
3305                 break;
3306         case OST_GET_INFO:
3307                 DEBUG_REQ(D_INODE, req, "get_info");
3308                 rc = mdt_get_info(req);
3309                 break;
3310         case OST_SET_INFO:
3311                 DEBUG_REQ(D_INODE, req, "set_info");
3312                 rc = mdt_set_info(req);
3313                 break;
3314         case OST_WRITE:
3315                 CDEBUG(D_INODE, "write\n");
3316                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
3317                 rc = ost_brw_write(req, NULL);
3318                 LASSERT(current->journal_info == NULL);
3319                 /* mdt_brw sends its own replies */
3320                 RETURN(rc);
3321                 break;
3322         case LLOG_CATINFO:
3323                 DEBUG_REQ(D_INODE, req, "llog catinfo");
3324                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
3325                 rc = llog_catinfo(req);
3326                 break;
3327         default:
3328                 req->rq_status = -ENOTSUPP;
3329                 rc = ptlrpc_error(req);
3330                 RETURN(rc);
3331         }
3332
3333         LASSERT(current->journal_info == NULL);
3334
3335         EXIT;
3336
3337         /* If we're DISCONNECTing, the mds_export_data is already freed */
3338         if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
3339                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
3340                 struct obd_device *obd = list_entry(mds, struct obd_device,
3341                                                     u.mds);
3342                 req->rq_repmsg->last_xid =
3343                         le64_to_cpu(med->med_mcd->mcd_last_xid);
3344
3345                 if (!obd->obd_no_transno) {
3346                         req->rq_repmsg->last_committed =
3347                                 obd->obd_last_committed;
3348                 } else {
3349                         DEBUG_REQ(D_IOCTL, req,
3350                                   "not sending last_committed update");
3351                 }
3352                 CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
3353                        ", xid "LPU64"\n",
3354                        mds->mds_last_transno, obd->obd_last_committed,
3355                        req->rq_xid);
3356         }
3357  out:
3358
3359
3360         target_send_reply(req, rc, fail);
3361         return 0;
3362 }
3363
3364 /* Update the server data on disk.  This stores the new mount_count and also the
3365  * last_rcvd value to disk.  If we don't have a clean shutdown, then the server
3366  * last_rcvd value may be less than that of the clients.  This will alert us
3367  * that we may need to do client recovery.
3368  *
3369  * Also assumes for mds_last_transno that we are not modifying it (no locking).
3370  */
3371 int mds_update_server_data(struct obd_device *obd, int force_sync)
3372 {
3373         struct mds_obd *mds = &obd->u.mds;
3374         struct mds_server_data *msd = mds->mds_server_data;
3375         struct file *filp = mds->mds_rcvd_filp;
3376         struct lvfs_run_ctxt saved;
3377         loff_t off = 0;
3378         int rc;
3379         ENTRY;
3380
3381         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3382         msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
3383
3384         CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
3385                mds->mds_mount_count, mds->mds_last_transno);
3386         rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off, force_sync);
3387         if (rc)
3388                 CERROR("error writing MDS server data: rc = %d\n", rc);
3389         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3390
3391         RETURN(rc);
3392 }
3393
3394 /* saves last allocated fid counter to file. */
3395 int mds_update_last_fid(struct obd_device *obd, void *handle,
3396                         int force_sync)
3397 {
3398         struct mds_obd *mds = &obd->u.mds;
3399         struct file *filp = mds->mds_fid_filp;
3400         struct lvfs_run_ctxt saved;
3401         loff_t off = 0;
3402         __u64 last_fid;
3403         int rc = 0;
3404         ENTRY;
3405
3406         spin_lock(&mds->mds_last_fid_lock);
3407         last_fid = mds->mds_last_fid;
3408         spin_unlock(&mds->mds_last_fid_lock);
3409
3410         CDEBUG(D_SUPER, "MDS last_fid is #"LPU64"\n",
3411                last_fid);
3412
3413         if (handle) {
3414                 fsfilt_add_journal_cb(obd, mds->mds_sb, last_fid,
3415                                       handle, mds_commit_last_fid_cb,
3416                                       NULL);
3417         }
3418                 
3419         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3420         rc = fsfilt_write_record(obd, filp, &last_fid, sizeof(last_fid),
3421                                  &off, force_sync);
3422         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3423
3424         if (rc) {
3425                 CERROR("error writing MDS last_fid #"LPU64
3426                        ", err = %d\n", last_fid, rc);
3427                 RETURN(rc);
3428         }
3429                 
3430         CDEBUG(D_SUPER, "wrote fid #"LPU64" at idx "
3431                "%llu: err = %d\n", last_fid, off, rc);
3432
3433         RETURN(rc);
3434 }
3435
3436 void mds_set_last_fid(struct obd_device *obd, __u64 fid)
3437 {
3438         struct mds_obd *mds = &obd->u.mds;
3439
3440         spin_lock(&mds->mds_last_fid_lock);
3441         if (fid > mds->mds_last_fid)
3442                 mds->mds_last_fid = fid;
3443         spin_unlock(&mds->mds_last_fid_lock);
3444 }
3445
3446 void mds_commit_last_transno_cb(struct obd_device *obd,
3447                                 __u64 transno, void *data,
3448                                 int error)
3449 {
3450         obd_transno_commit_cb(obd, transno, error);
3451 }
3452
3453 void mds_commit_last_fid_cb(struct obd_device *obd,
3454                             __u64 fid, void *data,
3455                             int error)
3456 {
3457         if (error) {
3458                 CERROR("%s: fid "LPD64" commit error: %d\n",
3459                        obd->obd_name, fid, error);
3460                 return;
3461         }
3462         
3463         CDEBUG(D_HA, "%s: fid "LPD64" committed\n",
3464                obd->obd_name, fid);
3465 }
3466
3467 __u64 mds_alloc_fid(struct obd_device *obd)
3468 {
3469         struct mds_obd *mds = &obd->u.mds;
3470         __u64 fid;
3471         
3472         spin_lock(&mds->mds_last_fid_lock);
3473         fid = ++mds->mds_last_fid;
3474         spin_unlock(&mds->mds_last_fid_lock);
3475
3476         return fid;
3477 }
3478
3479 /*
3480  * reads inode self id from inode EA. Probably later this should be replaced by
3481  * caching inode self id to avoid raeding it every time it is needed.
3482  */
3483 int mds_read_inode_sid(struct obd_device *obd, struct inode *inode,
3484                        struct lustre_id *id)
3485 {
3486         int rc;
3487         ENTRY;
3488
3489         LASSERT(id != NULL);
3490         LASSERT(obd != NULL);
3491         LASSERT(inode != NULL);
3492
3493         rc = fsfilt_get_md(obd, inode, &id->li_fid,
3494                            sizeof(id->li_fid), EA_SID);
3495         if (rc < 0) {
3496                 CERROR("fsfilt_get_md() failed, "
3497                        "rc = %d\n", rc);
3498                 RETURN(rc);
3499         } else if (!rc) {
3500                 rc = -ENODATA;
3501                 RETURN(rc);
3502         } else {
3503                 rc = 0;
3504         }
3505         id_ino(id) = inode->i_ino;
3506         id_gen(id) = inode->i_generation;
3507         id_type(id) = S_IFMT & inode->i_mode;
3508
3509         RETURN(rc);
3510 }
3511
3512 int mds_read_inode_pid(struct obd_device *obd, struct inode *inode,
3513                        struct lustre_id *id)
3514 {
3515         int rc;
3516         ENTRY;
3517
3518         LASSERT(inode && id);
3519
3520         rc = fsfilt_get_md(obd, inode, id, sizeof(*id), EA_PID);
3521         if (rc < 0)
3522                CERROR("get parent id from EA failed, rc=%d\n", rc);
3523         else if (!rc)
3524                 rc = -ENODATA;
3525         else
3526                 rc = 0;
3527
3528         RETURN(rc);
3529 }
3530
3531 /* updates inode self id in EA. */
3532 int mds_update_inode_ids(struct obd_device *obd, struct inode *inode,
3533                          void *handle, struct lustre_id *id,
3534                          struct lustre_id *pid)
3535 {
3536         int rc = 0;
3537         ENTRY;
3538         
3539         LASSERT(id || pid);
3540         LASSERT(id == NULL || id_fid(id) != 0);
3541         LASSERT(pid == NULL || id_fid(pid) != 0);
3542         LASSERT(obd != NULL);
3543         LASSERT(inode != NULL);
3544         
3545         if (id) {
3546                 mds_set_last_fid(obd, id_fid(id));
3547                 rc = fsfilt_set_md(obd, inode, handle, &id->li_fid,
3548                                    sizeof(id->li_fid), EA_SID);
3549                 LASSERTF(rc == 0, "failed to update fid:  %d\n", rc);
3550         }
3551         if (pid) {
3552                 rc = fsfilt_set_md(obd, inode, handle, pid,
3553                                    sizeof(*pid), EA_PID);
3554                 LASSERTF(rc == 0, "failed to update parent fid:  %d\n", rc);
3555         }
3556
3557         RETURN(rc);
3558 }
3559
3560 /* mount the file system (secretly) */
3561 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
3562 {
3563         struct lustre_cfg* lcfg = buf;
3564         struct mds_obd *mds = &obd->u.mds;
3565         struct lvfs_obd_ctxt *lvfs_ctxt = NULL;
3566         char *options = NULL;
3567         struct vfsmount *mnt;
3568         char ns_name[48];
3569         unsigned long page;
3570         struct crypto_tfm *tfm = NULL;
3571         int rc = 0;
3572         ENTRY;
3573
3574         if (lcfg->lcfg_bufcount < 3)
3575                 RETURN(rc = -EINVAL);
3576
3577         if (LUSTRE_CFG_BUFLEN(lcfg, 1) == 0 || LUSTRE_CFG_BUFLEN(lcfg, 2) == 0)
3578                 RETURN(rc = -EINVAL);
3579
3580         obd->obd_fsops = fsfilt_get_ops(lustre_cfg_string(lcfg, 2));
3581         if (IS_ERR(obd->obd_fsops))
3582                 RETURN(rc = PTR_ERR(obd->obd_fsops));
3583
3584         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
3585
3586         page = get_zeroed_page(GFP_KERNEL);
3587         if (!page)
3588                 RETURN(-ENOMEM);
3589
3590         options = (char *)page;
3591
3592         /*
3593          * here we use "iopen_nopriv" hardcoded, because it affects MDS utility
3594          * and the rest of options are passed by mount options. Probably this
3595          * should be moved to somewhere else like startup scripts or lconf. */
3596         sprintf(options, "iopen_nopriv");
3597         
3598         if (LUSTRE_CFG_BUFLEN(lcfg, 4) > 0 && lustre_cfg_buf(lcfg, 4))
3599                 sprintf(options + strlen(options), ",%s",
3600                         lustre_cfg_string(lcfg, 4));
3601
3602         /* we have to know mdsnum before touching underlying fs -bzzz */
3603         atomic_set(&mds->mds_open_count, 0);
3604         sema_init(&mds->mds_md_sem, 1);
3605         mds->mds_md_connected = 0;
3606         mds->mds_md_name = NULL;
3607
3608         if (LUSTRE_CFG_BUFLEN(lcfg, 5) > 0 && lustre_cfg_buf(lcfg, 5) &&
3609             strncmp(lustre_cfg_string(lcfg, 5), "dumb", LUSTRE_CFG_BUFLEN(lcfg, 5))) {
3610                 class_uuid_t uuid;
3611
3612                 generate_random_uuid(uuid);
3613                 class_uuid_unparse(uuid, &mds->mds_md_uuid);
3614
3615                 OBD_ALLOC(mds->mds_md_name, LUSTRE_CFG_BUFLEN(lcfg, 5));
3616                 if (mds->mds_md_name == NULL) 
3617                         RETURN(rc = -ENOMEM);
3618
3619                 memcpy(mds->mds_md_name, lustre_cfg_buf(lcfg, 5),
3620                        LUSTRE_CFG_BUFLEN(lcfg, 5));
3621                 
3622                 CDEBUG(D_OTHER, "MDS: %s is master for %s\n",
3623                        obd->obd_name, mds->mds_md_name);
3624
3625                 rc = mds_md_connect(obd, mds->mds_md_name);
3626                 if (rc) {
3627                         OBD_FREE(mds->mds_md_name, LUSTRE_CFG_BUFLEN(lcfg, 5));
3628                         GOTO(err_ops, rc);
3629                 }
3630         }
3631
3632         mds->mds_obd_type = MDS_MASTER_OBD;
3633
3634         if (LUSTRE_CFG_BUFLEN(lcfg, 6) > 0 && lustre_cfg_buf(lcfg, 6) &&
3635             strncmp(lustre_cfg_string(lcfg, 6), "dumb", 
3636                     LUSTRE_CFG_BUFLEN(lcfg, 6))) {
3637                 if (!memcmp(lustre_cfg_string(lcfg, 6), "master", 
3638                             strlen("master"))) {
3639                         mds->mds_obd_type = MDS_MASTER_OBD;
3640                 } else if (!memcmp(lustre_cfg_string(lcfg, 6), "cache", 
3641                                    strlen("cache"))) {
3642                         mds->mds_obd_type = MDS_CACHE_OBD;
3643                 }     
3644         }
3645
3646         rc = lvfs_mount_fs(lustre_cfg_string(lcfg, 1), 
3647                            lustre_cfg_string(lcfg, 2),
3648                            options, 0, &lvfs_ctxt);
3649
3650         free_page(page);
3651
3652         if (rc || !lvfs_ctxt) {
3653                 CERROR("lvfs_mount_fs failed: rc = %d\n", rc);
3654                 GOTO(err_ops, rc);
3655         }
3656
3657         mnt = lvfs_ctxt->loc_mnt;
3658         mds->mds_lvfs_ctxt = lvfs_ctxt;
3659         ll_clear_rdonly(ll_sbdev(mnt->mnt_sb));
3660
3661         CDEBUG(D_SUPER, "%s: mnt = %p\n", lustre_cfg_string(lcfg, 1), mnt);
3662
3663         mds->mds_fidext_thumb = 0;
3664         sema_init(&mds->mds_epoch_sem, 1);
3665         atomic_set(&mds->mds_real_clients, 0);
3666         spin_lock_init(&mds->mds_fidext_lock);
3667         spin_lock_init(&mds->mds_fidmap_lock);
3668         spin_lock_init(&mds->mds_transno_lock);
3669         spin_lock_init(&mds->mds_last_fid_lock);
3670         sema_init(&mds->mds_orphan_recovery_sem, 1);
3671         mds->mds_max_cookiesize = sizeof(struct llog_cookie);
3672
3673         sprintf(ns_name, "mds-%s", obd->obd_uuid.uuid);
3674         obd->obd_namespace = ldlm_namespace_new(ns_name, LDLM_NAMESPACE_SERVER);
3675
3676         if (obd->obd_namespace == NULL) {
3677                 mds_cleanup(obd, 0);
3678                 GOTO(err_put, rc = -ENOMEM);
3679         }
3680         ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
3681
3682         tfm = crypto_alloc_tfm(CAPA_HMAC_ALG, 0);
3683         if (!tfm)
3684                 GOTO(err_ns, rc = -ENOSYS);
3685
3686         mds->mds_capa_hmac = tfm;
3687         mds->mds_capa_timeout = CAPA_TIMEOUT;
3688         mds->mds_capa_key_timeout = CAPA_KEY_TIMEOUT;
3689         
3690         rc = mds_fs_setup(obd, mnt);
3691         if (rc) {
3692                 CERROR("%s: MDS filesystem method init failed: rc = %d\n",
3693                        obd->obd_name, rc);
3694                 GOTO(err_capa, rc);
3695         }
3696
3697         rc = llog_start_commit_thread();
3698         if (rc < 0)
3699
3700                 GOTO(err_fs, rc);
3701
3702         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0 && lustre_cfg_buf(lcfg, 3) &&
3703             strncmp(lustre_cfg_string(lcfg, 3), "dumb", 
3704                     LUSTRE_CFG_BUFLEN(lcfg, 3))) {
3705                 class_uuid_t uuid;
3706
3707                 generate_random_uuid(uuid);
3708                 class_uuid_unparse(uuid, &mds->mds_dt_uuid);
3709
3710                 OBD_ALLOC(mds->mds_profile, LUSTRE_CFG_BUFLEN(lcfg, 3));
3711                 if (mds->mds_profile == NULL)
3712                         GOTO(err_fs, rc = -ENOMEM);
3713
3714                 strncpy(mds->mds_profile, lustre_cfg_string(lcfg, 3),
3715                         LUSTRE_CFG_BUFLEN(lcfg, 3));
3716         }
3717
3718         /* 
3719          * setup root dir and files ID dir if lmv already connected, or there is
3720          * not lmv at all.
3721          */
3722         if (mds->mds_md_exp || (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0 && 
3723                                 lustre_cfg_buf(lcfg, 3) &&
3724                                 strncmp(lustre_cfg_string(lcfg, 3), "dumb", 
3725                                         LUSTRE_CFG_BUFLEN(lcfg, 3)))) {
3726                 rc = mds_fs_setup_rootid(obd);
3727                 if (rc)
3728                         GOTO(err_fs, rc);
3729
3730                 rc = mds_fs_setup_virtid(obd);
3731                 if (rc)
3732                         GOTO(err_fs, rc);
3733
3734         }
3735
3736         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3737                            "mds_ldlm_client", &obd->obd_ldlm_client);
3738         obd->obd_replayable = 1;
3739         
3740         mds->mds_crypto_type = NO_CRYPTO;
3741         
3742         rc = mds_postsetup(obd);
3743         if (rc)
3744                 GOTO(err_fs, rc);
3745
3746         RETURN(0);
3747
3748 err_fs:
3749         /* No extra cleanup needed for llog_init_commit_thread() */
3750         mds_fs_cleanup(obd, 0);
3751 err_capa:
3752         crypto_free_tfm(mds->mds_capa_hmac);
3753 err_ns:
3754         ldlm_namespace_free(obd->obd_namespace, 0);
3755         obd->obd_namespace = NULL;
3756 err_put:
3757         unlock_kernel();
3758         lvfs_umount_fs(mds->mds_lvfs_ctxt);
3759         mds->mds_sb = 0;
3760         lock_kernel();
3761 err_ops:
3762         fsfilt_put_ops(obd->obd_fsops);
3763         return rc;
3764 }
3765
3766 static int mds_fs_post_setup(struct obd_device *obd)
3767 {
3768         struct mds_obd *mds = &obd->u.mds;
3769         struct dentry *dentry;
3770         int rc = 0;
3771         ENTRY;
3772        
3773         dentry = mds_id2dentry(obd, &mds->mds_rootid, NULL);
3774         if (IS_ERR(dentry)) {
3775                 CERROR("Can't find ROOT, err = %d\n",
3776                        (int)PTR_ERR(dentry));
3777                 RETURN(PTR_ERR(dentry));
3778         }
3779         rc = fsfilt_post_setup(obd, dentry);
3780         //set id2name function handler
3781         fsfilt_set_info(obd, mds->mds_sb, NULL, 7, "id2name",
3782                         sizeof(mds_audit_id2name), mds_audit_id2name);
3783
3784         l_dput(dentry);
3785         RETURN(rc); 
3786 }
3787
3788 static int mds_postsetup(struct obd_device *obd)
3789 {
3790         struct mds_obd *mds = &obd->u.mds;
3791         int rc = 0;
3792         ENTRY;
3793
3794         rc = obd_llog_setup(obd, &obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT, 
3795                             obd, 0, NULL, &llog_lvfs_ops);
3796         if (rc)
3797                 RETURN(rc);
3798
3799         if (mds->mds_profile) {
3800                 struct llog_ctxt *lgctxt;
3801                 struct lvfs_run_ctxt saved;
3802                 struct lustre_profile *lprof;
3803                 struct config_llog_instance cfg;
3804
3805                 cfg.cfg_instance = NULL;
3806                 cfg.cfg_uuid = mds->mds_dt_uuid;
3807                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3808
3809                 lgctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
3810                 if (!lgctxt) {
3811                         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3812                         GOTO(err_llog, rc = -EINVAL);
3813                 }
3814                 
3815                 rc = class_config_process_llog(lgctxt, mds->mds_profile, &cfg);
3816                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3817
3818                 if (rc)
3819                         GOTO(err_llog, rc);
3820
3821                 lprof = class_get_profile(mds->mds_profile);
3822                 if (lprof == NULL) {
3823                         CERROR("No profile found: %s\n", mds->mds_profile);
3824                         GOTO(err_cleanup, rc = -ENOENT);
3825                 }
3826                 rc = mds_dt_connect(obd, lprof->lp_lov);
3827                 if (rc)
3828                         GOTO(err_cleanup, rc);
3829
3830                 rc = mds_md_postsetup(obd);
3831                 if (rc)
3832                         GOTO(err_cleanup, rc);
3833         }
3834         rc = mds_fs_post_setup(obd);
3835         if (rc)
3836                 CERROR("can not post setup fsfilt\n");        
3837         RETURN(rc);
3838 err_cleanup:
3839         mds_dt_clean(obd);
3840 err_llog:
3841         obd_llog_cleanup(llog_get_context(&obd->obd_llogs,
3842                                           LLOG_CONFIG_ORIG_CTXT));
3843         return rc;
3844 }
3845
3846 int mds_postrecov_common(struct obd_device *obd)
3847 {
3848         struct mds_obd *mds = &obd->u.mds;
3849         struct llog_ctxt *ctxt;
3850         int rc, item = 0, valsize;
3851          __u32 group;
3852         ENTRY;
3853
3854         LASSERT(!obd->obd_recovering);
3855         ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
3856         LASSERT(ctxt != NULL);
3857
3858         /* clean PENDING dir */
3859         rc = mds_cleanup_orphans(obd);
3860         if (rc < 0)
3861                 GOTO(out, rc);
3862         item = rc;
3863
3864         group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
3865         valsize = sizeof(group);
3866         rc = obd_set_info(mds->mds_dt_exp, strlen("mds_conn"),
3867                           "mds_conn", valsize, &group);
3868         if (rc)
3869                 GOTO(out, rc);
3870
3871         rc = llog_connect(ctxt, obd->u.mds.mds_dt_desc.ld_tgt_count,
3872                           NULL, NULL, NULL);
3873         if (rc) {
3874                 CERROR("%s: failed at llog_origin_connect: %d\n", 
3875                        obd->obd_name, rc);
3876                 GOTO(out, rc);
3877         }
3878
3879         /* remove the orphaned precreated objects */
3880         rc = mds_dt_clear_orphans(mds, NULL /* all OSTs */);
3881         if (rc)
3882                 GOTO(err_llog, rc);
3883
3884 out:
3885         RETURN(rc < 0 ? rc : item);
3886
3887 err_llog:
3888         /* cleanup all llogging subsystems */
3889         rc = obd_llog_finish(obd, &obd->obd_llogs,
3890                              mds->mds_dt_desc.ld_tgt_count);
3891         if (rc)
3892                 CERROR("%s: failed to cleanup llogging subsystems\n",
3893                         obd->obd_name);
3894         goto out;
3895 }
3896
3897 int mds_postrecov(struct obd_device *obd)
3898 {
3899         int rc;
3900         ENTRY;
3901         rc = mds_postrecov_common(obd);
3902         if (rc == 0)
3903                 rc = mds_md_reconnect(obd);
3904         RETURN(rc);
3905 }
3906
3907 int mds_dt_clean(struct obd_device *obd)
3908 {
3909         struct mds_obd *mds = &obd->u.mds;
3910         ENTRY;
3911
3912         if (mds->mds_profile) {
3913                 char * cln_prof;
3914                 struct llog_ctxt *llctx;
3915                 struct lvfs_run_ctxt saved;
3916                 struct config_llog_instance cfg;
3917                 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
3918
3919                 OBD_ALLOC(cln_prof, len);
3920                 sprintf(cln_prof, "%s-clean", mds->mds_profile);
3921
3922                 cfg.cfg_instance = NULL;
3923                 cfg.cfg_uuid = mds->mds_dt_uuid;
3924
3925                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3926                 llctx = llog_get_context(&obd->obd_llogs,
3927                                          LLOG_CONFIG_ORIG_CTXT);
3928                 class_config_process_llog(llctx, cln_prof, &cfg);
3929                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
3930
3931                 OBD_FREE(cln_prof, len);
3932                 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
3933                 mds->mds_profile = NULL;
3934         }
3935         RETURN(0);
3936 }
3937
3938 int mds_md_clean(struct obd_device *obd)
3939 {
3940         struct mds_obd *mds = &obd->u.mds;
3941         ENTRY;
3942
3943         if (mds->mds_md_name) {
3944                 OBD_FREE(mds->mds_md_name, strlen(mds->mds_md_name) + 1);
3945                 mds->mds_md_name = NULL;
3946         }
3947         RETURN(0);
3948 }
3949
3950 static int mds_precleanup(struct obd_device *obd, int flags)
3951 {
3952         int rc = 0;
3953         ENTRY;
3954
3955         mds_md_clean(obd);
3956         mds_dt_disconnect(obd, flags);
3957         mds_dt_clean(obd);
3958         obd_llog_cleanup(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT));
3959         RETURN(rc);
3960 }
3961
3962 extern void lgss_svc_cache_purge_all(void);
3963 static int mds_cleanup(struct obd_device *obd, int flags)
3964 {
3965         struct mds_obd *mds = &obd->u.mds;
3966         ENTRY;
3967
3968         if (mds->mds_sb == NULL)
3969                 RETURN(0);
3970
3971         mds_update_server_data(obd, 1);
3972         mds_update_last_fid(obd, NULL, 1);
3973         
3974         if (mds->mds_dt_objids != NULL) {
3975                 int size = mds->mds_dt_desc.ld_tgt_count *
3976                         sizeof(obd_id);
3977                 OBD_FREE(mds->mds_dt_objids, size);
3978         }
3979         mds_fs_cleanup(obd, flags);
3980
3981         unlock_kernel();
3982
3983         /* 2 seems normal on mds, (may_umount() also expects 2
3984           fwiw), but we only see 1 at this point in obdfilter. */
3985         lvfs_umount_fs(mds->mds_lvfs_ctxt);
3986
3987         mds->mds_sb = 0;
3988
3989         ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
3990
3991         spin_lock_bh(&obd->obd_processing_task_lock);
3992         if (obd->obd_recovering) {
3993                 target_cancel_recovery_timer(obd);
3994                 obd->obd_recovering = 0;
3995         }
3996         spin_unlock_bh(&obd->obd_processing_task_lock);
3997
3998         lock_kernel();
3999         fsfilt_put_ops(obd->obd_fsops);
4000
4001 #ifdef ENABLE_GSS
4002         /* XXX */
4003         lgss_svc_cache_purge_all();
4004 #endif
4005
4006         spin_lock(&mds->mds_denylist_lock);
4007         while (!list_empty( &mds->mds_denylist ) ) {
4008                 deny_sec_t *p_deny_sec = list_entry(mds->mds_denylist.next,
4009                                                     deny_sec_t, list);
4010                 list_del(&p_deny_sec->list);
4011                 OBD_FREE(p_deny_sec, sizeof(*p_deny_sec));
4012         }
4013         spin_unlock(&mds->mds_denylist_lock);
4014
4015         mds_capa_keys_cleanup(obd);
4016
4017         if (mds->mds_capa_hmac)
4018                 crypto_free_tfm(mds->mds_capa_hmac);
4019         RETURN(0);
4020 }
4021
4022 static int set_security(const char *value, char **sec)
4023 {
4024         if (!strcmp(value, "null"))
4025                 *sec = "null";
4026         else if (!strcmp(value, "krb5i"))
4027                 *sec = "krb5i";
4028         else if (!strcmp(value, "krb5p"))
4029                 *sec = "krb5p";
4030         else {
4031                 CERROR("Unrecognized security flavor %s\n", value);
4032                 return -EINVAL;
4033         }
4034
4035         return 0;
4036 }
4037
4038 static int mds_process_config(struct obd_device *obd, obd_count len, void *buf)
4039 {
4040         struct lustre_cfg *lcfg = buf;
4041         struct mds_obd *mds = &obd->u.mds;
4042         int rc = 0;
4043         ENTRY;
4044
4045         switch(lcfg->lcfg_command) {
4046         case LCFG_SET_SECURITY: {
4047                 if ((LUSTRE_CFG_BUFLEN(lcfg, 1) == 0) ||
4048                     (LUSTRE_CFG_BUFLEN(lcfg, 2) == 0))
4049                         GOTO(out, rc = -EINVAL);
4050
4051                 if (!strcmp(lustre_cfg_string(lcfg, 1), "mds_sec"))
4052                         rc = set_security(lustre_cfg_string(lcfg, 2),
4053                                           &mds->mds_mds_sec);
4054                 else if (!strcmp(lustre_cfg_string(lcfg, 1), "oss_sec"))
4055                         rc = set_security(lustre_cfg_string(lcfg, 2),
4056                                           &mds->mds_ost_sec);
4057                 else if (!strcmp(lustre_cfg_string(lcfg, 1), "deny_sec")){
4058                         spin_lock(&mds->mds_denylist_lock);
4059                         rc = add_deny_security(lustre_cfg_string(lcfg, 2),
4060                                                &mds->mds_denylist);
4061                         spin_unlock(&mds->mds_denylist_lock);
4062                 } else {
4063                         CERROR("Unrecognized key\n");
4064                         rc = -EINVAL;
4065                 }
4066                 break;
4067         }
4068         default:
4069                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
4070                 GOTO(out, rc = -EINVAL);
4071         }
4072 out:
4073         RETURN(rc);
4074 }
4075
4076 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
4077                                         int offset,
4078                                         struct ldlm_lock *new_lock,
4079                                         struct ldlm_lock **old_lock,
4080                                         struct lustre_handle *lockh)
4081 {
4082         struct obd_export *exp = req->rq_export;
4083         struct obd_device *obd = exp->exp_obd;
4084         struct ldlm_request *dlmreq =
4085                 lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*dlmreq));
4086         struct lustre_handle remote_hdl = dlmreq->lock_handle1;
4087         struct list_head *iter;
4088
4089         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4090                 return;
4091
4092         spin_lock(&obd->obd_namespace->ns_hash_lock);
4093         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
4094                 struct ldlm_lock *lock;
4095                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
4096                 if (lock == new_lock)
4097                         continue;
4098                 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
4099                         lockh->cookie = lock->l_handle.h_cookie;
4100                         LDLM_DEBUG(lock, "restoring lock cookie");
4101                         DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
4102                                   lockh->cookie);
4103                         if (old_lock)
4104                                 *old_lock = LDLM_LOCK_GET(lock);
4105                         spin_unlock(&obd->obd_namespace->ns_hash_lock);
4106                         return;
4107                 }
4108         }
4109         spin_unlock(&obd->obd_namespace->ns_hash_lock);
4110
4111         /* If the xid matches, then we know this is a resent request,
4112          * and allow it. (It's probably an OPEN, for which we don't
4113          * send a lock */
4114         if (req->rq_xid == 
4115             le64_to_cpu(exp->exp_mds_data.med_mcd->mcd_last_xid))
4116                 return;
4117
4118         if (req->rq_xid == 
4119             le64_to_cpu(exp->exp_mds_data.med_mcd->mcd_last_close_xid))
4120                 return;
4121
4122         /* This remote handle isn't enqueued, so we never received or
4123          * processed this request.  Clear MSG_RESENT, because it can
4124          * be handled like any normal request now. */
4125
4126         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4127
4128         DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
4129                   remote_hdl.cookie);
4130 }
4131
4132 int intent_disposition(struct ldlm_reply *rep, int flag)
4133 {
4134         if (!rep)
4135                 return 0;
4136         return (rep->lock_policy_res1 & flag);
4137 }
4138
4139 void intent_set_disposition(struct ldlm_reply *rep, int flag)
4140 {
4141         if (!rep)
4142                 return;
4143         rep->lock_policy_res1 |= flag;
4144 }
4145
4146 static int mds_intent_prepare_reply_buffers(struct ptlrpc_request *req, 
4147                                             struct ldlm_intent *it)
4148 {
4149         struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
4150         int rc, reply_buffers;
4151         int repsize[8] = {sizeof(struct ldlm_reply),
4152                           sizeof(struct mds_body),
4153                           mds->mds_max_mdsize};
4154         ENTRY;       
4155  
4156         reply_buffers = 3;
4157         if (it->opc & ( IT_OPEN | IT_GETATTR | IT_LOOKUP | IT_CHDIR )) {
4158                 /*Ugly here, Actually, we should prepare the reply buffer 
4159                  *after we know whether these stuff exist or not, which should
4160                  * be fixed in future, Now each item is in the fix position,
4161                  * the sequence is lsm, acl, crypto ea, capa.*/
4162                 repsize[reply_buffers++] = sizeof(int);
4163                 /* XXX: mds_remote_perm is stored here too, and for it
4164                  *      the 'size' is ignored */
4165                 repsize[reply_buffers++] = 
4166                                 xattr_acl_size(LL_ACL_MAX_ENTRIES);
4167                 
4168                 repsize[reply_buffers++] = sizeof(int);
4169                 repsize[reply_buffers++] = sizeof(struct crypto_key); 
4170
4171                 /* XXX: if new buffer is to be added, capability reply
4172                  *      buffer should always been reserved. */
4173                 if (it->opc & IT_OPEN)
4174                         repsize[reply_buffers++] = sizeof(struct lustre_capa);
4175         }
4176
4177         rc = lustre_pack_reply(req, reply_buffers, repsize, NULL);
4178
4179         RETURN(rc);
4180 }
4181
4182 static int mds_intent_policy(struct ldlm_namespace *ns,
4183                              struct ldlm_lock **lockp, void *req_cookie,
4184                              ldlm_mode_t mode, int flags, void *data)
4185 {
4186         struct ptlrpc_request *req = req_cookie;
4187         struct ldlm_lock *lock = *lockp;
4188         struct ldlm_intent *it;
4189         struct ldlm_reply *rep;
4190         struct lustre_handle lockh[2] = {{0}, {0}};
4191         struct ldlm_lock *new_lock = NULL;
4192         int getattr_part = MDS_INODELOCK_UPDATE;
4193         int rc;
4194
4195         int offset = MDS_REQ_INTENT_REC_OFF; 
4196         ENTRY;
4197
4198         LASSERT(req != NULL);
4199         MD_COUNTER_INCREMENT(req->rq_export->exp_obd, intent_lock);
4200
4201         if (req->rq_reqmsg->bufcount <= MDS_REQ_INTENT_IT_OFF) {
4202                 /* No intent was provided */
4203                 int size = sizeof(struct ldlm_reply);
4204                 rc = lustre_pack_reply(req, 1, &size, NULL);
4205                 LASSERT(rc == 0);
4206                 RETURN(0);
4207         }
4208
4209         it = lustre_swab_reqbuf(req, MDS_REQ_INTENT_IT_OFF, sizeof(*it),
4210                                 lustre_swab_ldlm_intent);
4211         if (it == NULL) {
4212                 CERROR("Intent missing\n");
4213                 RETURN(req->rq_status = -EFAULT);
4214         }
4215
4216         LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
4217
4218         rc = mds_intent_prepare_reply_buffers(req, it);
4219
4220         if (rc)
4221                 RETURN(req->rq_status = rc);
4222
4223         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
4224         LASSERT(rep != NULL);
4225
4226         intent_set_disposition(rep, DISP_IT_EXECD);
4227
4228         /* execute policy */
4229         switch ((long)it->opc) {
4230         case IT_OPEN:
4231         case IT_CREAT|IT_OPEN:
4232                 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF,
4233                                             lock, NULL, lockh);
4234                 /* XXX swab here to assert that an mds_open reint
4235                  * packet is following */
4236                 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF, 
4237                                             lock, NULL, lockh);
4238                 rep->lock_policy_res2 = mds_reint(req, offset, lockh);
4239                 
4240                 if (rep->lock_policy_res2) {
4241                         /* 
4242                          * mds_open() returns ENOLCK where it should return
4243                          * zero, but it has no lock to return.
4244                          */
4245                         if (rep->lock_policy_res2 == ENOLCK)
4246                                 rep->lock_policy_res2 = 0;
4247
4248                         RETURN(ELDLM_LOCK_ABORTED);
4249                 }
4250                 
4251                 /*
4252                  * IT_OPEN may return lock on cross-node dentry that we want to
4253                  * hold during attr retrival -bzzz
4254                  */
4255                 if (lockh[0].cookie == 0)
4256                         RETURN(ELDLM_LOCK_ABORTED);
4257                 
4258                 break;
4259         case IT_LOOKUP:
4260                 getattr_part = MDS_INODELOCK_LOOKUP;
4261         case IT_CHDIR:
4262         case IT_GETATTR:
4263                 getattr_part |= MDS_INODELOCK_LOOKUP;
4264         case IT_READDIR:
4265                 fixup_handle_for_resent_req(req, MDS_REQ_INTENT_LOCKREQ_OFF, 
4266                                             lock, &new_lock, lockh);
4267                 rep->lock_policy_res2 = mds_getattr_lock(req, offset, lockh,
4268                                                          getattr_part);
4269                 /* FIXME: LDLM can set req->rq_status. MDS sets
4270                    policy_res{1,2} with disposition and status.
4271                    - replay: returns 0 & req->status is old status
4272                    - otherwise: returns req->status */
4273                 if (intent_disposition(rep, DISP_LOOKUP_NEG))
4274                         rep->lock_policy_res2 = 0;
4275                 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
4276                     rep->lock_policy_res2)
4277                         RETURN(ELDLM_LOCK_ABORTED);
4278                 if (req->rq_status != 0) {
4279                         LBUG();
4280                         rep->lock_policy_res2 = req->rq_status;
4281                         RETURN(ELDLM_LOCK_ABORTED);
4282                 }
4283                 break;
4284         case IT_UNLINK:
4285                 rc = mds_lock_and_check_slave(offset, req, lockh);
4286                 if ((rep->lock_policy_res2 = rc)) {
4287                         if (rc == ENOLCK)
4288                                 rep->lock_policy_res2 = 0;
4289                         RETURN(ELDLM_LOCK_ABORTED);
4290                 }
4291                 break;
4292         default:
4293                 CERROR("Unhandled intent "LPD64"\n", it->opc);
4294                 LBUG();
4295         }
4296
4297         /* By this point, whatever function we called above must have either
4298          * filled in 'lockh', been an intent replay, or returned an error.  We
4299          * want to allow replayed RPCs to not get a lock, since we would just
4300          * drop it below anyways because lock replay is done separately by the
4301          * client afterwards.  For regular RPCs we want to give the new lock to
4302          * the client instead of whatever lock it was about to get. */
4303         if (new_lock == NULL)
4304                 new_lock = ldlm_handle2lock(&lockh[0]);
4305         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
4306                 RETURN(0);
4307
4308         LASSERTF(new_lock != NULL, "op "LPX64" lockh "LPX64"\n",
4309                  it->opc, lockh[0].cookie);
4310
4311         /* If we've already given this lock to a client once, then we should
4312          * have no readers or writers.  Otherwise, we should have one reader
4313          * _or_ writer ref (which will be zeroed below) before returning the
4314          * lock to a client. */
4315         if (new_lock->l_export == req->rq_export) {
4316                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4317         } else {
4318                 LASSERT(new_lock->l_export == NULL);
4319                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4320         }
4321
4322         *lockp = new_lock;
4323
4324         if (new_lock->l_export == req->rq_export) {
4325                 /* Already gave this to the client, which means that we
4326                  * reconstructed a reply. */
4327                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4328                         MSG_RESENT);
4329                 RETURN(ELDLM_LOCK_REPLACED);
4330         }
4331
4332         /* Fixup the lock to be given to the client */
4333         lock_res_and_lock(new_lock);
4334         new_lock->l_readers = 0;
4335         new_lock->l_writers = 0;
4336
4337         new_lock->l_export = class_export_get(req->rq_export);
4338
4339         spin_lock(&new_lock->l_export->exp_ldlm_data.led_lock);
4340         list_add(&new_lock->l_export_chain,
4341                  &new_lock->l_export->exp_ldlm_data.led_held_locks);
4342         spin_unlock(&new_lock->l_export->exp_ldlm_data.led_lock);
4343
4344         new_lock->l_blocking_ast = lock->l_blocking_ast;
4345         new_lock->l_completion_ast = lock->l_completion_ast;
4346
4347         memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
4348                sizeof(lock->l_remote_handle));
4349
4350         new_lock->l_flags &= ~LDLM_FL_LOCAL;
4351
4352         unlock_res_and_lock(new_lock);
4353         LDLM_LOCK_PUT(new_lock);
4354
4355         RETURN(ELDLM_LOCK_REPLACED);
4356 }
4357
4358 int mds_attach(struct obd_device *dev, obd_count len, void *data)
4359 {
4360         struct lprocfs_static_vars lvars;
4361         int rc = 0;
4362         struct mds_obd *mds = &dev->u.mds;
4363
4364         spin_lock_init(&mds->mds_denylist_lock);
4365         INIT_LIST_HEAD(&mds->mds_denylist);
4366
4367         lprocfs_init_multi_vars(0, &lvars);
4368
4369         rc = lprocfs_obd_attach(dev, lvars.obd_vars);
4370         if (rc)
4371                 return rc;
4372
4373         return lprocfs_alloc_md_stats(dev, 0);
4374 }
4375
4376 int mds_detach(struct obd_device *dev)
4377 {
4378         lprocfs_free_md_stats(dev);
4379         return lprocfs_obd_detach(dev);
4380 }
4381
4382 int mdt_attach(struct obd_device *dev, obd_count len, void *data)
4383 {
4384         struct lprocfs_static_vars lvars;
4385
4386         lprocfs_init_multi_vars(1, &lvars);
4387         return lprocfs_obd_attach(dev, lvars.obd_vars);
4388 }
4389
4390 int mdt_detach(struct obd_device *dev)
4391 {
4392         return lprocfs_obd_detach(dev);
4393 }
4394
4395 static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
4396 {
4397         struct mds_obd *mds = &obd->u.mds;
4398         int rc = 0;
4399         ENTRY;
4400
4401         mds->mds_service =
4402                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4403                                 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
4404                                 MDS_SERVICE_WATCHDOG_TIMEOUT,
4405                                 mds_handle, "mds", obd->obd_proc_entry);
4406
4407         if (!mds->mds_service) {
4408                 CERROR("failed to start service\n");
4409                 RETURN(-ENOMEM);
4410         }
4411
4412         rc = ptlrpc_start_n_threads(obd, mds->mds_service, MDT_NUM_THREADS,
4413                                     "ll_mdt");
4414         if (rc)
4415                 GOTO(err_thread, rc);
4416
4417         mds->mds_setattr_service =
4418                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4419                                 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
4420                                 MDS_SERVICE_WATCHDOG_TIMEOUT,
4421                                 mds_handle, "mds_setattr",
4422                                 obd->obd_proc_entry);
4423         if (!mds->mds_setattr_service) {
4424                 CERROR("failed to start getattr service\n");
4425                 GOTO(err_thread, rc = -ENOMEM);
4426         }
4427
4428         rc = ptlrpc_start_n_threads(obd, mds->mds_setattr_service,
4429                                     MDT_NUM_THREADS, "ll_mdt_attr");
4430         if (rc)
4431                 GOTO(err_thread2, rc);
4432
4433         mds->mds_readpage_service =
4434                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4435                                 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
4436                                 MDS_SERVICE_WATCHDOG_TIMEOUT,
4437                                 mds_handle, "mds_readpage",
4438                                 obd->obd_proc_entry);
4439         if (!mds->mds_readpage_service) {
4440                 CERROR("failed to start readpage service\n");
4441                 GOTO(err_thread2, rc = -ENOMEM);
4442         }
4443
4444         rc = ptlrpc_start_n_threads(obd, mds->mds_readpage_service,
4445                                     MDT_NUM_THREADS, "ll_mdt_rdpg");
4446
4447         if (rc)
4448                 GOTO(err_thread3, rc);
4449
4450         mds->mds_close_service =
4451                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
4452                                 MDS_CLOSE_PORTAL, MDC_REPLY_PORTAL,
4453                                 MDS_SERVICE_WATCHDOG_TIMEOUT,
4454                                 mds_handle, "mds_close",
4455                                 obd->obd_proc_entry);
4456         if (!mds->mds_close_service) {
4457                 CERROR("failed to start close service\n");
4458                 GOTO(err_thread3, rc = -ENOMEM);
4459         }
4460
4461         rc = ptlrpc_start_n_threads(obd, mds->mds_close_service,
4462                                     MDT_NUM_THREADS, "ll_mdt_clos");
4463
4464         if (rc)
4465                 GOTO(err_thread4, rc);
4466         RETURN(0);
4467
4468 err_thread4:
4469         ptlrpc_unregister_service(mds->mds_close_service);
4470 err_thread3:
4471         ptlrpc_unregister_service(mds->mds_readpage_service);
4472 err_thread2:
4473         ptlrpc_unregister_service(mds->mds_setattr_service);
4474 err_thread:
4475         ptlrpc_unregister_service(mds->mds_service);
4476         return rc;
4477 }
4478
4479 static int mdt_cleanup(struct obd_device *obd, int flags)
4480 {
4481         struct mds_obd *mds = &obd->u.mds;
4482         ENTRY;
4483
4484         ptlrpc_stop_all_threads(mds->mds_close_service);
4485         ptlrpc_unregister_service(mds->mds_close_service);
4486
4487         ptlrpc_stop_all_threads(mds->mds_readpage_service);
4488         ptlrpc_unregister_service(mds->mds_readpage_service);
4489
4490         ptlrpc_stop_all_threads(mds->mds_setattr_service);
4491         ptlrpc_unregister_service(mds->mds_setattr_service);
4492
4493         ptlrpc_stop_all_threads(mds->mds_service);
4494         ptlrpc_unregister_service(mds->mds_service);
4495
4496         RETURN(0);
4497 }
4498
4499 static struct dentry *mds_lvfs_id2dentry(__u64 ino, __u32 gen,
4500                                          __u64 gr, void *data)
4501 {
4502         struct lustre_id id;
4503         struct obd_device *obd = data;
4504         
4505         id_ino(&id) = ino;
4506         id_gen(&id) = gen;
4507         return mds_id2dentry(obd, &id, NULL);
4508 }
4509
4510 static int mds_get_info(struct obd_export *exp, __u32 keylen,
4511                         void *key, __u32 *valsize, void *val)
4512 {
4513         struct obd_device *obd;
4514         struct mds_obd *mds;
4515         int rc = 0;
4516         ENTRY;
4517
4518         obd = class_exp2obd(exp);
4519         mds = &obd->u.mds;
4520         
4521         if (obd == NULL) {
4522                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
4523                        exp->exp_handle.h_cookie);
4524                 RETURN(-EINVAL);
4525         }
4526
4527         if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
4528                 /* get log_context handle. */
4529                 struct llog_ctxt *ctxt;
4530                 unsigned long *llh_handle = val;
4531                 *valsize = sizeof(unsigned long);
4532                 ctxt = llog_get_context(&obd->obd_llogs, LLOG_REINT_ORIG_CTXT);
4533                 if (!ctxt) {
4534                         CERROR("Cannot get REINT llog context\n");
4535                         RETURN(-ENOENT);
4536                 }
4537                 *llh_handle = (unsigned long)ctxt;
4538                 RETURN(0);
4539         }
4540         if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
4541                 /* get log_context handle. */
4542                 unsigned long *sb = val;
4543                 *valsize = sizeof(unsigned long);
4544                 *sb = (unsigned long)obd->u.mds.mds_sb;
4545                 RETURN(0);
4546         }
4547
4548         if (keylen >= strlen("mdsize") && memcmp(key, "mdsize", keylen) == 0) {
4549                 __u32 *mdsize = val;
4550                 *valsize = sizeof(*mdsize);
4551                 *mdsize = mds->mds_max_mdsize;
4552                 RETURN(0);
4553         }
4554
4555         if (keylen >= strlen("mdsnum") && strcmp(key, "mdsnum") == 0) {
4556                 __u32 *mdsnum = val;
4557                 *valsize = sizeof(*mdsnum);
4558                 *mdsnum = mds->mds_num;
4559                 RETURN(0);
4560         }
4561
4562         if (keylen >= strlen("rootid") && strcmp(key, "rootid") == 0) {
4563                 struct lustre_id *rootid = val;
4564                 *valsize = sizeof(*rootid);
4565                 *rootid = mds->mds_rootid;
4566                 RETURN(0);
4567         }
4568         
4569         if (keylen >= strlen("lovdesc") && strcmp(key, "lovdesc") == 0) {
4570                 struct lov_desc *desc = val;
4571                 *valsize = sizeof(*desc);
4572                 *desc = mds->mds_dt_desc;
4573                 RETURN(0);
4574         }
4575         
4576         if (keylen >= strlen("getext") && strcmp(key, "getext") == 0) {
4577                 struct fid_extent *ext = val;
4578                 *valsize = sizeof(*ext);
4579
4580                 spin_lock(&mds->mds_fidext_lock);
4581                 ext->fe_width = MDS_FIDEXT_SIZE;
4582                 ext->fe_start = mds->mds_fidext_thumb + 1;
4583                 mds->mds_fidext_thumb += MDS_FIDEXT_SIZE;
4584                 spin_unlock(&mds->mds_fidext_lock);
4585                 
4586                 RETURN(0);
4587         }
4588         
4589         rc = fsfilt_get_info(obd, mds->mds_sb, NULL, keylen, key, valsize, val);
4590         if (rc)
4591                 CDEBUG(D_IOCTL, "invalid key\n");
4592         
4593         RETURN(rc);
4594 }
4595
4596 struct lvfs_callback_ops mds_lvfs_ops = {
4597         l_id2dentry:     mds_lvfs_id2dentry,
4598 };
4599
4600 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
4601                 int objcount, struct obd_ioobj *obj,
4602                 int niocount, struct niobuf_remote *nb,
4603                 struct niobuf_local *res,
4604                 struct obd_trans_info *oti, struct lustre_capa *capa);
4605
4606 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
4607                  int objcount, struct obd_ioobj *obj, int niocount,
4608                  struct niobuf_local *res, struct obd_trans_info *oti,
4609                  int rc);
4610
4611 /* use obd ops to offer management infrastructure */
4612 static struct obd_ops mds_obd_ops = {
4613         .o_owner           = THIS_MODULE,
4614         .o_attach          = mds_attach,
4615         .o_detach          = mds_detach,
4616         .o_connect         = mds_connect,
4617         .o_connect_post    = mds_connect_post,
4618         .o_init_export     = mds_init_export,
4619         .o_destroy_export  = mds_destroy_export,
4620         .o_disconnect      = mds_disconnect,
4621         .o_setup           = mds_setup,
4622         .o_precleanup      = mds_precleanup,
4623         .o_cleanup         = mds_cleanup,
4624         .o_process_config  = mds_process_config,
4625         .o_postrecov       = mds_postrecov,
4626         .o_statfs          = mds_obd_statfs,
4627         .o_iocontrol       = mds_iocontrol,
4628         .o_create          = mds_obd_create,
4629         .o_destroy         = mds_obd_destroy,
4630         .o_llog_init       = mds_llog_init,
4631         .o_llog_finish     = mds_llog_finish,
4632         .o_notify          = mds_notify,
4633         .o_get_info        = mds_get_info,
4634         .o_set_info        = mds_set_info,
4635         .o_preprw          = mds_preprw, 
4636         .o_commitrw        = mds_commitrw,
4637 };
4638
4639 static struct obd_ops mdt_obd_ops = {
4640         .o_owner           = THIS_MODULE,
4641         .o_attach          = mdt_attach,
4642         .o_detach          = mdt_detach,
4643         .o_setup           = mdt_setup,
4644         .o_cleanup         = mdt_cleanup,
4645 };
4646
4647 static int __init mds_init(void)
4648 {
4649         struct lprocfs_static_vars lvars;
4650         int rc = 0;
4651         ENTRY;
4652
4653         mds_init_lsd_cache();
4654         mds_init_rmtacl_upcall_cache();
4655
4656         lprocfs_init_multi_vars(0, &lvars);
4657         class_register_type(&mds_obd_ops, NULL, lvars.module_vars,
4658                             OBD_MDS_DEVICENAME);
4659         lprocfs_init_multi_vars(1, &lvars);
4660         class_register_type(&mdt_obd_ops, NULL, lvars.module_vars,
4661                             OBD_MDT_DEVICENAME);
4662
4663         rc = mds_capa_key_start_thread();
4664         if (rc) {
4665                 class_unregister_type(OBD_MDT_DEVICENAME);
4666                 class_unregister_type(OBD_MDS_DEVICENAME);
4667                 mds_cleanup_lsd_cache();
4668         }
4669                 
4670         mds_eck_timer.function = mds_capa_key_timer_callback;
4671         mds_eck_timer.data = 0;
4672         init_timer(&mds_eck_timer);
4673
4674         RETURN(rc);
4675 }
4676
4677 static void /*__exit*/ mds_exit(void)
4678 {
4679         mds_capa_key_stop_thread();
4680         mds_cleanup_rmtacl_upcall_cache();
4681         mds_cleanup_lsd_cache();
4682
4683         class_unregister_type(OBD_MDS_DEVICENAME);
4684         class_unregister_type(OBD_MDT_DEVICENAME);
4685 }
4686
4687 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
4688 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
4689 MODULE_LICENSE("GPL");
4690
4691 module_init(mds_init);
4692 module_exit(mds_exit);