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