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