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