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