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