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