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