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