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