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