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