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