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