Whamcloud - gitweb
- additional debug messages for #46
[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/ext3_fs.h>
43 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
44 # include <linux/smp_lock.h>
45 # include <linux/buffer_head.h>
46 # include <linux/workqueue.h>
47 # include <linux/mount.h>
48 #else
49 # include <linux/locks.h>
50 #endif
51 #include <linux/obd_lov.h>
52 #include <linux/lustre_mds.h>
53 #include <linux/lustre_fsfilt.h>
54 #include <linux/lprocfs_status.h>
55 #include <linux/lustre_commit_confd.h>
56
57 #include "mds_internal.h"
58
59 static int mds_intent_policy(struct ldlm_namespace *ns,
60                              struct ldlm_lock **lockp, void *req_cookie,
61                              ldlm_mode_t mode, int flags, void *data);
62 static int mds_postsetup(struct obd_device *obd);
63 static int mds_cleanup(struct obd_device *obd, int flags);
64
65 /* Assumes caller has already pushed into the kernel filesystem context */
66 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
67                         loff_t offset, int count)
68 {
69         struct ptlrpc_bulk_desc *desc;
70         struct l_wait_info lwi;
71         struct page **pages;
72         int rc = 0, npages, i, tmpcount, tmpsize = 0;
73         ENTRY;
74
75         LASSERT((offset & (PAGE_SIZE - 1)) == 0); /* I'm dubious about this */
76
77         npages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
78         OBD_ALLOC(pages, sizeof(*pages) * npages);
79         if (!pages)
80                 GOTO(out, rc = -ENOMEM);
81
82         desc = ptlrpc_prep_bulk_exp(req, npages, BULK_PUT_SOURCE,
83                                     MDS_BULK_PORTAL);
84         if (desc == NULL)
85                 GOTO(out_free, rc = -ENOMEM);
86
87         for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
88                 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
89
90                 pages[i] = alloc_pages(GFP_KERNEL, 0);
91                 if (pages[i] == NULL)
92                         GOTO(cleanup_buf, rc = -ENOMEM);
93
94                 ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
95         }
96
97         for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
98                 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
99                 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
100                        tmpsize, offset, file->f_dentry->d_inode->i_ino,
101                        file->f_dentry->d_inode->i_size);
102
103                 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
104                                      kmap(pages[i]), tmpsize, &offset);
105                 kunmap(pages[i]);
106
107                 if (rc != tmpsize)
108                         GOTO(cleanup_buf, rc = -EIO);
109         }
110
111         LASSERT(desc->bd_nob == count);
112
113         rc = ptlrpc_start_bulk_transfer(desc);
114         if (rc)
115                 GOTO(cleanup_buf, rc);
116
117         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
118                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
119                        OBD_FAIL_MDS_SENDPAGE, rc);
120                 GOTO(abort_bulk, rc);
121         }
122
123         lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
124         rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), &lwi);
125         LASSERT (rc == 0 || rc == -ETIMEDOUT);
126
127         if (rc == 0) {
128                 if (desc->bd_success &&
129                     desc->bd_nob_transferred == count)
130                         GOTO(cleanup_buf, rc);
131
132                 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
133         }
134
135         DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
136                   (rc == -ETIMEDOUT) ? "timeout" : "network error",
137                   desc->bd_nob_transferred, count,
138                   req->rq_export->exp_client_uuid.uuid,
139                   req->rq_export->exp_connection->c_remote_uuid.uuid);
140
141         ptlrpc_fail_export(req->rq_export);
142
143         EXIT;
144  abort_bulk:
145         ptlrpc_abort_bulk (desc);
146  cleanup_buf:
147         for (i = 0; i < npages; i++)
148                 if (pages[i])
149                         __free_pages(pages[i], 0);
150
151         ptlrpc_free_bulk(desc);
152  out_free:
153         OBD_FREE(pages, sizeof(*pages) * npages);
154  out:
155         return rc;
156 }
157
158 int mds_lock_mode_for_dir(struct obd_device *obd,
159                               struct dentry *dentry, int mode)
160 {
161         int ret_mode, split;
162
163         /* any dir access needs couple locks:
164          * 1) on part of dir we gonna lookup/modify in
165          * 2) on a whole dir to protect it from concurrent splitting
166          *    and to flush client's cache for readdir()
167          * so, for a given mode and dentry this routine decides what
168          * lock mode to use for lock #2:
169          * 1) if caller's gonna lookup in dir then we need to protect
170          *    dir from being splitted only - LCK_CR
171          * 2) if caller's gonna modify dir then we need to protect
172          *    dir from being splitted and to flush cache - LCK_CW
173          * 3) if caller's gonna modify dir and that dir seems ready
174          *    for splitting then we need to protect it from any
175          *    type of access (lookup/modify/split) - LCK_EX -bzzz */
176
177         split = mds_splitting_expected(obd, dentry);
178         if (split == MDS_NO_SPLITTABLE) {
179                 /* this inode won't be splitted. so we need not to protect from
180                  * just flush client's cache on modification */
181                 ret_mode = 0;
182                 if (mode == LCK_PW)
183                         ret_mode = LCK_CW;
184         } else {
185                 if (mode == LCK_PR) {
186                         ret_mode = LCK_CR;
187                 } else if (mode == LCK_PW) {
188                         /* caller gonna modify directory.we use concurrent
189                            write lock here to retract client's cache for readdir */
190                         ret_mode = LCK_CW;
191                         if (split == MDS_EXPECT_SPLIT) {
192                                 /* splitting possible. serialize any access */
193                                 CDEBUG(D_OTHER, "%s: gonna split %u/%u\n",
194                                        obd->obd_name,
195                                        (unsigned) dentry->d_inode->i_ino,
196                                        (unsigned) dentry->d_inode->i_generation);
197                                 ret_mode = LCK_EX;
198                         }
199                 } else {
200                         CWARN("unexpected lock mode %d\n", mode);
201                         ret_mode = LCK_EX;
202                 }
203         }
204         return ret_mode;
205 }
206
207 /* only valid locked dentries or errors should be returned */
208 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
209                                      struct vfsmount **mnt, int lock_mode,
210                                      struct lustre_handle *lockh, int *mode,
211                                      char *name, int namelen, __u64 lockpart)
212 {
213         struct mds_obd *mds = &obd->u.mds;
214         struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
215         struct ldlm_res_id res_id = { .name = {0} };
216         int flags = 0, rc;
217         ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
218
219         ENTRY;
220
221         if (IS_ERR(de))
222                 RETURN(de);
223
224         res_id.name[0] = de->d_inode->i_ino;
225         res_id.name[1] = de->d_inode->i_generation;
226         lockh[1].cookie = 0;
227 #ifdef S_PDIROPS
228         if (name && IS_PDIROPS(de->d_inode)) {
229                 ldlm_policy_data_t cpolicy =
230                         { .l_inodebits = { MDS_INODELOCK_UPDATE } };
231                 LASSERT(mode != NULL);
232                 *mode = mds_lock_mode_for_dir(obd, de, lock_mode);
233                 if (*mode) {
234                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
235                                               res_id, LDLM_IBITS,
236                                               &cpolicy, *mode, &flags,
237                                               mds_blocking_ast,
238                                               ldlm_completion_ast, NULL, NULL,
239                                               NULL, 0, NULL, lockh + 1);
240                         if (rc != ELDLM_OK) {
241                                 l_dput(de);
242                                 RETURN(ERR_PTR(-ENOLCK));
243                         }
244                 }
245                 flags = 0;
246
247                 res_id.name[2] = full_name_hash(name, namelen);
248
249                 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
250                        de->d_inode->i_ino, de->d_inode->i_generation,
251                        res_id.name[2]);
252         }
253 #else
254 #warning "No PDIROPS support in the kernel"
255 #endif
256         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
257                               LDLM_IBITS, &policy, lock_mode, &flags,
258                               mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
259                               NULL, 0, NULL, lockh);
260         if (rc != ELDLM_OK) {
261                 l_dput(de);
262                 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
263 #ifdef S_PDIROPS
264                 if (lockh[1].cookie)
265                         ldlm_lock_decref(lockh + 1, LCK_CW);
266 #endif
267         }
268
269         RETURN(retval);
270 }
271
272 #ifndef DCACHE_DISCONNECTED
273 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
274 #endif
275
276
277 /* Look up an entry by inode number. */
278 /* this function ONLY returns valid dget'd dentries with an initialized inode
279    or errors */
280 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
281                               struct vfsmount **mnt)
282 {
283         char fid_name[32];
284         unsigned long ino = fid->id;
285         __u32 generation = fid->generation;
286         struct inode *inode;
287         struct dentry *result;
288
289         if (ino == 0)
290                 RETURN(ERR_PTR(-ESTALE));
291
292         snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
293
294         CDEBUG(D_DENTRY, "--> mds_fid2dentry: ino/gen %lu/%u, sb %p\n",
295                ino, generation, mds->mds_sb);
296
297         /* under ext3 this is neither supposed to return bad inodes
298            nor NULL inodes. */
299         result = ll_lookup_one_len(fid_name, mds->mds_fid_de, strlen(fid_name));
300         if (IS_ERR(result))
301                 RETURN(result);
302
303         inode = result->d_inode;
304         if (!inode)
305                 RETURN(ERR_PTR(-ENOENT));
306
307 #if 0
308         /* here we disabled generation check, as root inode i_generation
309          * of cache mds and real mds are different. */
310         if (generation && inode->i_generation != generation) {
311                 /* we didn't find the right inode.. */
312                 CERROR("bad inode %lu, link: %lu ct: %d or generation %u/%u\n",
313                        inode->i_ino, (unsigned long)inode->i_nlink,
314                        atomic_read(&inode->i_count), inode->i_generation,
315                        generation);
316                 dput(result);
317                 RETURN(ERR_PTR(-ENOENT));
318         }
319 #endif
320
321         if (mnt) {
322                 *mnt = mds->mds_vfsmnt;
323                 mntget(*mnt);
324         }
325
326         RETURN(result);
327 }
328
329
330 /* Establish a connection to the MDS.
331  *
332  * This will set up an export structure for the client to hold state data
333  * about that client, like open files, the last operation number it did
334  * on the server, etc.
335  */
336 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
337                        struct obd_uuid *cluuid)
338 {
339         struct obd_export *exp;
340         struct mds_export_data *med; /*  */
341         struct mds_client_data *mcd;
342         int rc, abort_recovery;
343         ENTRY;
344
345         if (!conn || !obd || !cluuid)
346                 RETURN(-EINVAL);
347
348         /* Check for aborted recovery. */
349         spin_lock_bh(&obd->obd_processing_task_lock);
350         abort_recovery = obd->obd_abort_recovery;
351         spin_unlock_bh(&obd->obd_processing_task_lock);
352         if (abort_recovery)
353                 target_abort_recovery(obd);
354
355         /* XXX There is a small race between checking the list and adding a
356          * new connection for the same UUID, but the real threat (list
357          * corruption when multiple different clients connect) is solved.
358          *
359          * There is a second race between adding the export to the list,
360          * and filling in the client data below.  Hence skipping the case
361          * of NULL mcd above.  We should already be controlling multiple
362          * connects at the client, and we can't hold the spinlock over
363          * memory allocations without risk of deadlocking.
364          */
365         rc = class_connect(conn, obd, cluuid);
366         if (rc)
367                 RETURN(rc);
368         exp = class_conn2export(conn);
369         LASSERT(exp);
370         med = &exp->exp_mds_data;
371
372         OBD_ALLOC(mcd, sizeof(*mcd));
373         if (!mcd) {
374                 CERROR("mds: out of memory for client data\n");
375                 GOTO(out, rc = -ENOMEM);
376         }
377
378         memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
379         med->med_mcd = mcd;
380
381         rc = mds_client_add(obd, &obd->u.mds, med, -1);
382         if (rc == 0)
383                 EXIT;
384 out:
385         if (rc) {
386                 OBD_FREE(mcd, sizeof(*mcd));
387                 class_disconnect(exp, 0);
388         }
389         class_export_put(exp);
390
391         return rc;
392 }
393
394 static int mds_init_export(struct obd_export *exp)
395 {
396         struct mds_export_data *med = &exp->exp_mds_data;
397
398         INIT_LIST_HEAD(&med->med_open_head);
399         spin_lock_init(&med->med_open_lock);
400         RETURN(0);
401 }
402
403 static int mds_destroy_export(struct obd_export *export)
404 {
405         struct mds_export_data *med;
406         struct obd_device *obd = export->exp_obd;
407         struct lvfs_run_ctxt saved;
408         int rc = 0;
409         ENTRY;
410
411         med = &export->exp_mds_data;
412         target_destroy_export(export);
413
414         if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
415                 GOTO(out, 0);
416
417         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
418
419         /* Close any open files (which may also cause orphan unlinking). */
420         spin_lock(&med->med_open_lock);
421         while (!list_empty(&med->med_open_head)) {
422                 struct list_head *tmp = med->med_open_head.next;
423                 struct mds_file_data *mfd =
424                         list_entry(tmp, struct mds_file_data, mfd_list);
425                 BDEVNAME_DECLARE_STORAGE(btmp);
426
427                 /* bug 1579: fix force-closing for 2.5 */
428                 struct dentry *dentry = mfd->mfd_dentry;
429
430                 list_del(&mfd->mfd_list);
431                 spin_unlock(&med->med_open_lock);
432
433                 /* If you change this message, be sure to update
434                  * replay_single:test_46 */
435                 CERROR("force closing client file handle for %*s (%s:%lu)\n",
436                        dentry->d_name.len, dentry->d_name.name,
437                        ll_bdevname(dentry->d_inode->i_sb, btmp),
438                        dentry->d_inode->i_ino);
439                 rc = mds_mfd_close(NULL, obd, mfd,
440                                    !(export->exp_flags & OBD_OPT_FAILOVER));
441
442                 if (rc)
443                         CDEBUG(D_INODE, "Error closing file: %d\n", rc);
444                 spin_lock(&med->med_open_lock);
445         }
446         spin_unlock(&med->med_open_lock);
447         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
448
449 out:
450         mds_client_free(export, !(export->exp_flags & OBD_OPT_FAILOVER));
451
452         RETURN(rc);
453 }
454
455 static int mds_disconnect(struct obd_export *exp, int flags)
456 {
457         struct obd_device *obd;
458         struct mds_obd *mds;
459         unsigned long irqflags;
460         int rc;
461         ENTRY;
462
463         LASSERT(exp);
464         class_export_get(exp);
465
466         obd = class_exp2obd(exp);
467         if (obd == NULL) {
468                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
469                        exp->exp_handle.h_cookie);
470                 RETURN(-EINVAL);
471         }
472         mds = &obd->u.mds;
473
474         if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)
475                         && !atomic_read(&mds->mds_real_clients)) {
476                 /* there was no client at all */
477                 mds_lmv_disconnect(obd, flags);
478         }
479
480         if ((exp->exp_flags & OBD_OPT_REAL_CLIENT)
481                         && atomic_dec_and_test(&mds->mds_real_clients)) {
482                 /* time to drop LMV connections */
483                 CDEBUG(D_OTHER, "%s: last real client %s disconnected.  "
484                        "Disconnnect from LMV now\n",
485                        obd->obd_name, exp->exp_client_uuid.uuid);
486                 mds_lmv_disconnect(obd, flags);
487         }
488
489         spin_lock_irqsave(&exp->exp_lock, irqflags);
490         exp->exp_flags = flags;
491         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
492
493         /* Disconnect early so that clients can't keep using export */
494         rc = class_disconnect(exp, flags);
495         ldlm_cancel_locks_for_export(exp);
496
497         /* complete all outstanding replies */
498         spin_lock_irqsave(&exp->exp_lock, irqflags);
499         while (!list_empty(&exp->exp_outstanding_replies)) {
500                 struct ptlrpc_reply_state *rs =
501                         list_entry(exp->exp_outstanding_replies.next,
502                                    struct ptlrpc_reply_state, rs_exp_list);
503                 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
504
505                 spin_lock(&svc->srv_lock);
506                 list_del_init(&rs->rs_exp_list);
507                 ptlrpc_schedule_difficult_reply(rs);
508                 spin_unlock(&svc->srv_lock);
509         }
510         spin_unlock_irqrestore(&exp->exp_lock, irqflags);
511
512         class_export_put(exp);
513         RETURN(rc);
514 }
515
516 static int mds_getstatus(struct ptlrpc_request *req)
517 {
518         struct mds_obd *mds = mds_req2mds(req);
519         struct mds_body *body;
520         int rc, size = sizeof(*body);
521         ENTRY;
522
523         rc = lustre_pack_reply(req, 1, &size, NULL);
524         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
525                 CERROR("mds: out of memory for message: size=%d\n", size);
526                 req->rq_status = -ENOMEM;       /* superfluous? */
527                 RETURN(-ENOMEM);
528         }
529
530         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
531         memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
532
533         /* the last_committed and last_xid fields are filled in for all
534          * replies already - no need to do so here also.
535          */
536         RETURN(0);
537 }
538
539 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
540                      void *data, int flag)
541 {
542         int do_ast;
543         ENTRY;
544
545         if (flag == LDLM_CB_CANCELING) {
546                 /* Don't need to do anything here. */
547                 RETURN(0);
548         }
549
550         /* XXX layering violation!  -phil */
551         l_lock(&lock->l_resource->lr_namespace->ns_lock);
552         /* Get this: if mds_blocking_ast is racing with mds_intent_policy,
553          * such that mds_blocking_ast is called just before l_i_p takes the
554          * ns_lock, then by the time we get the lock, we might not be the
555          * correct blocking function anymore.  So check, and return early, if
556          * so. */
557         if (lock->l_blocking_ast != mds_blocking_ast) {
558                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
559                 RETURN(0);
560         }
561
562         lock->l_flags |= LDLM_FL_CBPENDING;
563         do_ast = (!lock->l_readers && !lock->l_writers);
564         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
565
566         if (do_ast) {
567                 struct lustre_handle lockh;
568                 int rc;
569
570                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
571                 ldlm_lock2handle(lock, &lockh);
572                 rc = ldlm_cli_cancel(&lockh);
573                 if (rc < 0)
574                         CERROR("ldlm_cli_cancel: %d\n", rc);
575         } else {
576                 LDLM_DEBUG(lock, "Lock still has references, will be "
577                            "cancelled later");
578         }
579         RETURN(0);
580 }
581
582 int mds_get_md(struct obd_device *obd, struct inode *inode, void *md,
583                int *size, int lock)
584 {
585         int rc = 0;
586         int lmm_size;
587
588         if (lock)
589                 down(&inode->i_sem);
590         rc = fsfilt_get_md(obd, inode, md, *size);
591         if (lock)
592                 up(&inode->i_sem);
593
594         if (rc < 0) {
595                 CERROR("Error %d reading eadata for ino %lu\n",
596                        rc, inode->i_ino);
597         } else if (rc > 0) {
598                 lmm_size = rc;
599                 
600                 if (S_ISREG(inode->i_mode))
601                         rc = mds_convert_lov_ea(obd, inode, md, lmm_size);
602
603                 if (rc == 0) {
604                         *size = lmm_size;
605                         rc = lmm_size;
606                 } else if (rc > 0) {
607                         *size = rc;
608                 }
609         }
610
611         RETURN (rc);
612 }
613
614
615 /* Call with lock=1 if you want mds_pack_md to take the i_sem.
616  * Call with lock=0 if the caller has already taken the i_sem. */
617 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
618                 struct mds_body *body, struct inode *inode, int lock)
619 {
620         struct mds_obd *mds = &obd->u.mds;
621         void *lmm;
622         int lmm_size;
623         int rc;
624         ENTRY;
625
626         lmm = lustre_msg_buf(msg, offset, 0);
627         if (lmm == NULL) {
628                 /* Some problem with getting eadata when I sized the reply
629                  * buffer... */
630                 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
631                        inode->i_ino);
632                 RETURN(0);
633         }
634         lmm_size = msg->buflens[offset];
635
636         /* I don't really like this, but it is a sanity check on the client
637          * MD request.  However, if the client doesn't know how much space
638          * to reserve for the MD, it shouldn't be bad to have too much space.
639          */
640         if (lmm_size > mds->mds_max_mdsize) {
641                 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
642                        inode->i_ino, lmm_size, mds->mds_max_mdsize);
643                 // RETURN(-EINVAL);
644         }
645
646         rc = mds_get_md(obd, inode, lmm, &lmm_size, lock);
647         if (rc > 0) {
648                 if (S_ISDIR(inode->i_mode))
649                         body->valid |= OBD_MD_FLDIREA;
650                 else
651                         body->valid |= OBD_MD_FLEASIZE;
652                 body->eadatasize = lmm_size;
653                 rc = 0;
654         }
655
656         RETURN(rc);
657 }
658
659 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
660                                 struct ptlrpc_request *req,
661                                 struct mds_body *reqbody, int reply_off)
662 {
663         struct mds_body *body;
664         struct inode *inode = dentry->d_inode;
665         int rc = 0;
666         ENTRY;
667
668         if (inode == NULL && !(dentry->d_flags & DCACHE_CROSS_REF))
669                 RETURN(-ENOENT);
670
671         body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
672         LASSERT(body != NULL);                 /* caller prepped reply */
673
674         if (dentry->d_flags & DCACHE_CROSS_REF) {
675                 CDEBUG(D_OTHER, "cross reference: %lu/%lu/%lu\n",
676                        (unsigned long) dentry->d_mdsnum,
677                        (unsigned long) dentry->d_inum,
678                        (unsigned long) dentry->d_generation);
679                 body->valid |= OBD_MD_FLID | OBD_MD_MDS;
680                 body->fid1.id = dentry->d_inum;
681                 body->fid1.mds = dentry->d_mdsnum;
682                 body->fid1.generation = dentry->d_generation;
683                 RETURN(0);
684         }
685         mds_pack_inode2fid(obd, &body->fid1, inode);
686         mds_pack_inode2body(obd, body, inode);
687
688         if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
689             (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
690                 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1, body,
691                                  inode, 1);
692
693                 /* If we have LOV EA data, the OST holds size, atime, mtime */
694                 if (!(body->valid & OBD_MD_FLEASIZE) &&
695                     !(body->valid & OBD_MD_FLDIREA))
696                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
697                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
698         } else if (S_ISLNK(inode->i_mode) &&
699                    (reqbody->valid & OBD_MD_LINKNAME) != 0) {
700                 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1,0);
701                 int len;
702
703                 LASSERT (symname != NULL);       /* caller prepped reply */
704                 len = req->rq_repmsg->buflens[reply_off + 1];
705
706                 rc = inode->i_op->readlink(dentry, symname, len);
707                 if (rc < 0) {
708                         CERROR("readlink failed: %d\n", rc);
709                 } else if (rc != len - 1) {
710                         CERROR ("Unexpected readlink rc %d: expecting %d\n",
711                                 rc, len - 1);
712                         rc = -EINVAL;
713                 } else {
714                         CDEBUG(D_INODE, "read symlink dest %s\n", symname);
715                         body->valid |= OBD_MD_LINKNAME;
716                         body->eadatasize = rc + 1;
717                         symname[rc] = 0;        /* NULL terminate */
718                         rc = 0;
719                 }
720         }
721
722         RETURN(rc);
723 }
724
725 static int mds_getattr_pack_msg_cf(struct ptlrpc_request *req,
726                                         struct dentry *dentry,
727                                         int offset)
728 {
729         int rc = 0, size[1] = {sizeof(struct mds_body)};
730         ENTRY;
731
732         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
733                 CERROR("failed MDS_GETATTR_PACK test\n");
734                 req->rq_status = -ENOMEM;
735                 GOTO(out, rc = -ENOMEM);
736         }
737
738         rc = lustre_pack_reply(req, 1, size, NULL);
739         if (rc) {
740                 CERROR("out of memory\n");
741                 GOTO(out, req->rq_status = rc);
742         }
743
744         EXIT;
745  out:
746         return(rc);
747 }
748
749 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct inode *inode,
750                                 int offset)
751 {
752         struct mds_obd *mds = mds_req2mds(req);
753         struct mds_body *body;
754         int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
755         ENTRY;
756
757         body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*body));
758         LASSERT(body != NULL);                 /* checked by caller */
759         LASSERT_REQSWABBED(req, offset);       /* swabbed by caller */
760
761         if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
762             (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
763                 int rc;
764                 down(&inode->i_sem);
765                 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0);
766                 up(&inode->i_sem);
767                 CDEBUG(D_INODE, "got %d bytes MD data for inode %lu\n",
768                        rc, inode->i_ino);
769                 if (rc < 0) {
770                         if (rc != -ENODATA)
771                                 CERROR("error getting inode %lu MD: rc = %d\n",
772                                        inode->i_ino, rc);
773                         size[bufcount] = 0;
774                 } else if (rc > mds->mds_max_mdsize) {
775                         size[bufcount] = 0;
776                         CERROR("MD size %d larger than maximum possible %u\n",
777                                rc, mds->mds_max_mdsize);
778                 } else {
779                         size[bufcount] = rc;
780                 }
781                 bufcount++;
782         } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
783                 if (inode->i_size + 1 != body->eadatasize)
784                         CERROR("symlink size: %Lu, reply space: %d\n",
785                                inode->i_size + 1, body->eadatasize);
786                 size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
787                 bufcount++;
788                 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
789                        inode->i_size + 1, body->eadatasize);
790         }
791
792         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
793                 CERROR("failed MDS_GETATTR_PACK test\n");
794                 req->rq_status = -ENOMEM;
795                 GOTO(out, rc = -ENOMEM);
796         }
797
798         rc = lustre_pack_reply(req, bufcount, size, NULL);
799         if (rc) {
800                 CERROR("out of memory\n");
801                 GOTO(out, req->rq_status = rc);
802         }
803
804         EXIT;
805  out:
806         return(rc);
807 }
808
809 int mds_check_mds_num(struct obd_device *obd, struct inode* inode,
810                       char *name, int namelen)
811 {
812         struct mea *mea = NULL;
813         int mea_size, rc = 0;
814         ENTRY;
815                                                                                                                                                                                                      
816         rc = mds_get_lmv_attr(obd, inode, &mea, &mea_size);
817         if (rc)
818                 RETURN(rc);
819         if (mea != NULL) {
820                 /* dir is already splitted, check if requested filename
821                  * should live at this MDS or at another one */
822                 int i;
823                 i = mea_name2idx(mea, name, namelen - 1);
824                 if (mea->mea_master != i) {
825                         CERROR("inapropriate MDS(%d) for %s. should be %d\n",
826                                 mea->mea_master, name, i);
827                         rc = -ERESTART;
828                 }
829         }
830                                                                                                                                                                                                      
831         if (mea)
832                 OBD_FREE(mea, mea_size);
833         RETURN(rc);
834 }
835
836 static int mds_getattr_name(int offset, struct ptlrpc_request *req,
837                             struct lustre_handle *child_lockh, int child_part)
838 {
839         struct obd_device *obd = req->rq_export->exp_obd;
840         struct ldlm_reply *rep = NULL;
841         struct lvfs_run_ctxt saved;
842         struct mds_body *body;
843         struct dentry *dparent = NULL, *dchild = NULL;
844         struct lvfs_ucred uc;
845         struct lustre_handle parent_lockh[2];
846         int namesize, update_mode;
847         int rc = 0, cleanup_phase = 0, resent_req = 0;
848         char *name;
849         ENTRY;
850
851         LASSERT(!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME));
852
853         /* Swab now, before anyone looks inside the request */
854
855         body = lustre_swab_reqbuf(req, offset, sizeof(*body),
856                                   lustre_swab_mds_body);
857         if (body == NULL) {
858                 CERROR("Can't swab mds_body\n");
859                 GOTO(cleanup, rc = -EFAULT);
860         }
861
862         LASSERT_REQSWAB(req, offset + 1);
863         name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
864         if (name == NULL) {
865                 CERROR("Can't unpack name\n");
866                 GOTO(cleanup, rc = -EFAULT);
867         }
868         namesize = req->rq_reqmsg->buflens[offset + 1];
869
870         LASSERT (offset == 0 || offset == 2);
871         /* if requests were at offset 2, the getattr reply goes back at 1 */
872         if (offset) {
873                 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
874                 offset = 1;
875         }
876
877         uc.luc_fsuid = body->fsuid;
878         uc.luc_fsgid = body->fsgid;
879         uc.luc_cap = body->capability;
880         uc.luc_suppgid1 = body->suppgid;
881         uc.luc_suppgid2 = -1;
882         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
883         cleanup_phase = 1; /* kernel context */
884         intent_set_disposition(rep, DISP_LOOKUP_EXECD);
885
886         LASSERT(namesize > 0);
887         if (namesize == 1) {
888                 /* we have no dentry here, drop LOOKUP bit */
889                 child_part &= ~MDS_INODELOCK_LOOKUP;
890                 CDEBUG(D_OTHER, "%s: request to retrieve attrs for %lu/%lu\n",
891                        obd->obd_name, (unsigned long) body->fid1.id,
892                        (unsigned long) body->fid1.generation);
893                 dchild = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_PR,
894                                                parent_lockh, &update_mode, 
895                                                NULL, 0, child_part);
896                 if (IS_ERR(dchild)) {
897                         CERROR("can't find inode: %d\n", (int) PTR_ERR(dchild));
898                         GOTO(cleanup, rc = PTR_ERR(dchild));
899                 }
900                 memcpy(child_lockh, parent_lockh, sizeof(parent_lockh[0]));
901 #ifdef S_PDIROPS
902                 if (parent_lockh[1].cookie)
903                         ldlm_lock_decref(parent_lockh + 1, update_mode);
904 #endif
905                 cleanup_phase = 2;
906                 goto fill_inode;
907         }
908         
909         /* FIXME: handle raw lookup */
910 #if 0
911         if (body->valid == OBD_MD_FLID) {
912                 struct mds_body *mds_reply;
913                 int size = sizeof(*mds_reply);
914                 ino_t inum;
915                 // The user requested ONLY the inode number, so do a raw lookup
916                 rc = lustre_pack_reply(req, 1, &size, NULL);
917                 if (rc) {
918                         CERROR("out of memory\n");
919                         GOTO(cleanup, rc);
920                 }
921
922                 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
923
924                 mds_reply = lustre_msg_buf(req->rq_repmsg, offset,
925                                            sizeof(*mds_reply));
926                 mds_reply->fid1.id = inum;
927                 mds_reply->valid = OBD_MD_FLID;
928                 GOTO(cleanup, rc);
929         }
930 #endif
931
932         if (child_lockh->cookie != 0) {
933                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
934                 resent_req = 1;
935         }
936
937         if (resent_req == 0) {
938                 rc = mds_get_parent_child_locked(obd, &obd->u.mds, &body->fid1,
939                                                  parent_lockh, &dparent,
940                                                  LCK_PR, MDS_INODELOCK_LOOKUP,
941                                                  &update_mode, name, namesize,
942                                                  child_lockh, &dchild, LCK_PR,
943                                                  child_part);
944                 if (rc)
945                         GOTO(cleanup, rc);
946         } else {
947                 struct ldlm_lock *granted_lock;
948                 struct ll_fid child_fid;
949                 struct ldlm_resource *res;
950                 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
951                 granted_lock = ldlm_handle2lock(child_lockh);
952                 LASSERT(granted_lock);
953
954                 res = granted_lock->l_resource;
955                 child_fid.id = res->lr_name.name[0];
956                 child_fid.generation = res->lr_name.name[1];
957                 dchild = mds_fid2dentry(&obd->u.mds, &child_fid, NULL);
958                 LASSERT(dchild);
959                 LDLM_LOCK_PUT(granted_lock);
960         }
961
962         cleanup_phase = 2; /* dchild, dparent, locks */
963
964         /* let's make sure this name should leave on this mds node */
965         rc = mds_check_mds_num(obd, dparent->d_inode, name, namesize);
966         if (rc)
967                 GOTO(cleanup, rc);
968
969 fill_inode:
970
971         if (!DENTRY_VALID(dchild)) {
972                 intent_set_disposition(rep, DISP_LOOKUP_NEG);
973                 /* in the intent case, the policy clears this error:
974                    the disposition is enough */
975                 rc = -ENOENT;
976                 GOTO(cleanup, rc);
977         } else {
978                 intent_set_disposition(rep, DISP_LOOKUP_POS);
979         }
980
981         if (req->rq_repmsg == NULL) {
982                 if (dchild->d_flags & DCACHE_CROSS_REF)
983                         rc = mds_getattr_pack_msg_cf(req, dchild, offset);
984                 else
985                         rc = mds_getattr_pack_msg(req, dchild->d_inode, offset);
986                 if (rc != 0) {
987                         CERROR ("mds_getattr_pack_msg: %d\n", rc);
988                         GOTO (cleanup, rc);
989                 }
990         }
991
992         rc = mds_getattr_internal(obd, dchild, req, body, offset);
993         GOTO(cleanup, rc); /* returns the lock to the client */
994
995  cleanup:
996         switch (cleanup_phase) {
997         case 2:
998                 if (resent_req == 0) {
999                         if (rc && DENTRY_VALID(dchild))
1000                                 ldlm_lock_decref(child_lockh, LCK_PR);
1001                         if (dparent) {
1002                                 ldlm_lock_decref(parent_lockh, LCK_PR);
1003 #ifdef S_PDIROPS
1004                                 if (parent_lockh[1].cookie != 0)
1005                                         ldlm_lock_decref(parent_lockh + 1,
1006                                                          update_mode);
1007 #endif
1008                         }
1009                         if (dparent)
1010                                 l_dput(dparent);
1011                 }
1012                 l_dput(dchild);
1013         case 1:
1014                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1015         default: ;
1016         }
1017         return rc;
1018 }
1019
1020 static int mds_getattr(int offset, struct ptlrpc_request *req)
1021 {
1022         struct mds_obd *mds = mds_req2mds(req);
1023         struct obd_device *obd = req->rq_export->exp_obd;
1024         struct lvfs_run_ctxt saved;
1025         struct dentry *de;
1026         struct mds_body *body;
1027         struct lvfs_ucred uc;
1028         int rc = 0;
1029         ENTRY;
1030
1031         body = lustre_swab_reqbuf (req, offset, sizeof (*body),
1032                                    lustre_swab_mds_body);
1033         if (body == NULL) {
1034                 CERROR ("Can't unpack body\n");
1035                 RETURN (-EFAULT);
1036         }
1037
1038         uc.luc_fsuid = body->fsuid;
1039         uc.luc_fsgid = body->fsgid;
1040         uc.luc_cap = body->capability;
1041         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1042         de = mds_fid2dentry(mds, &body->fid1, NULL);
1043         if (IS_ERR(de)) {
1044                 rc = req->rq_status = -ENOENT;
1045                 GOTO(out_pop, PTR_ERR(de));
1046         }
1047
1048         rc = mds_getattr_pack_msg(req, de->d_inode, offset);
1049         if (rc != 0) {
1050                 CERROR ("mds_getattr_pack_msg: %d\n", rc);
1051                 GOTO (out_pop, rc);
1052         }
1053
1054         req->rq_status = mds_getattr_internal(obd, de, req, body, 0);
1055
1056         l_dput(de);
1057         GOTO(out_pop, rc);
1058 out_pop:
1059         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1060         return rc;
1061 }
1062
1063
1064 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1065                           unsigned long max_age)
1066 {
1067         int rc;
1068
1069         spin_lock(&obd->obd_osfs_lock);
1070         rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
1071         if (rc == 0)
1072                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1073         spin_unlock(&obd->obd_osfs_lock);
1074
1075         return rc;
1076 }
1077
1078 static int mds_statfs(struct ptlrpc_request *req)
1079 {
1080         struct obd_device *obd = req->rq_export->exp_obd;
1081         int rc, size = sizeof(struct obd_statfs);
1082         ENTRY;
1083
1084         rc = lustre_pack_reply(req, 1, &size, NULL);
1085         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
1086                 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
1087                 GOTO(out, rc);
1088         }
1089
1090         /* We call this so that we can cache a bit - 1 jiffie worth */
1091         rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
1092                             jiffies - HZ);
1093         if (rc) {
1094                 CERROR("mds_obd_statfs failed: rc %d\n", rc);
1095                 GOTO(out, rc);
1096         }
1097
1098         EXIT;
1099 out:
1100         req->rq_status = rc;
1101         return 0;
1102 }
1103
1104 static int mds_sync(struct ptlrpc_request *req)
1105 {
1106         struct obd_device *obd = req->rq_export->exp_obd;
1107         struct mds_obd *mds = &obd->u.mds;
1108         struct mds_body *body;
1109         int rc, size = sizeof(*body);
1110         ENTRY;
1111
1112         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1113         if (body == NULL)
1114                 GOTO(out, rc = -EPROTO);
1115
1116         rc = lustre_pack_reply(req, 1, &size, NULL);
1117         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
1118                 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
1119                 GOTO(out, rc);
1120         }
1121
1122         if (body->fid1.id == 0) {
1123                 /* a fid of zero is taken to mean "sync whole filesystem" */
1124                 rc = fsfilt_sync(obd, mds->mds_sb);
1125                 if (rc)
1126                         GOTO(out, rc);
1127         } else {
1128                 /* just any file to grab fsync method - "file" arg unused */
1129                 struct file *file = mds->mds_rcvd_filp;
1130                 struct dentry *de;
1131
1132                 de = mds_fid2dentry(mds, &body->fid1, NULL);
1133                 if (IS_ERR(de))
1134                         GOTO(out, rc = PTR_ERR(de));
1135
1136                 rc = file->f_op->fsync(NULL, de, 1);
1137                 l_dput(de);
1138                 if (rc)
1139                         GOTO(out, rc);
1140
1141                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1142                 mds_pack_inode2fid(obd, &body->fid1, de->d_inode);
1143                 mds_pack_inode2body(obd, body, de->d_inode);
1144         }
1145 out:
1146         req->rq_status = rc;
1147         return 0;
1148 }
1149
1150 /* mds_readpage does not take a DLM lock on the inode, because the client must
1151  * already have a PR lock.
1152  *
1153  * If we were to take another one here, a deadlock will result, if another
1154  * thread is already waiting for a PW lock. */
1155 static int mds_readpage(struct ptlrpc_request *req)
1156 {
1157         struct obd_device *obd = req->rq_export->exp_obd;
1158         struct vfsmount *mnt;
1159         struct dentry *de;
1160         struct file *file;
1161         struct mds_body *body, *repbody;
1162         struct lvfs_run_ctxt saved;
1163         int rc, size = sizeof(*repbody);
1164         struct lvfs_ucred uc;
1165         ENTRY;
1166
1167         rc = lustre_pack_reply(req, 1, &size, NULL);
1168         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
1169                 CERROR("mds: out of memory\n");
1170                 GOTO(out, rc = -ENOMEM);
1171         }
1172
1173         body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
1174         if (body == NULL)
1175                 GOTO (out, rc = -EFAULT);
1176
1177         uc.luc_fsuid = body->fsuid;
1178         uc.luc_fsgid = body->fsgid;
1179         uc.luc_cap = body->capability;
1180         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1181         de = mds_fid2dentry(&obd->u.mds, &body->fid1, &mnt);
1182         if (IS_ERR(de))
1183                 GOTO(out_pop, rc = PTR_ERR(de));
1184
1185         CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
1186
1187         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1188         /* note: in case of an error, dentry_open puts dentry */
1189         if (IS_ERR(file))
1190                 GOTO(out_pop, rc = PTR_ERR(file));
1191
1192         /* body->size is actually the offset -eeb */
1193         if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
1194                 CERROR("offset "LPU64" not on a block boundary of %lu\n",
1195                        body->size, de->d_inode->i_blksize);
1196                 GOTO(out_file, rc = -EFAULT);
1197         }
1198
1199         /* body->nlink is actually the #bytes to read -eeb */
1200         if (body->nlink & (de->d_inode->i_blksize - 1)) {
1201                 CERROR("size %u is not multiple of blocksize %lu\n",
1202                        body->nlink, de->d_inode->i_blksize);
1203                 GOTO(out_file, rc = -EFAULT);
1204         }
1205
1206         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
1207         repbody->size = file->f_dentry->d_inode->i_size;
1208         repbody->valid = OBD_MD_FLSIZE;
1209
1210         /* to make this asynchronous make sure that the handling function
1211            doesn't send a reply when this function completes. Instead a
1212            callback function would send the reply */
1213         /* body->size is actually the offset -eeb */
1214         rc = mds_sendpage(req, file, body->size, body->nlink);
1215
1216 out_file:
1217         filp_close(file, 0);
1218 out_pop:
1219         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1220 out:
1221         req->rq_status = rc;
1222         RETURN(0);
1223 }
1224
1225 int mds_reint(struct ptlrpc_request *req, int offset,
1226               struct lustre_handle *lockh)
1227 {
1228         struct mds_update_record *rec; /* 116 bytes on the stack?  no sir! */
1229         int rc;
1230         ENTRY;
1231
1232         OBD_ALLOC(rec, sizeof(*rec));
1233         if (rec == NULL)
1234                 RETURN(-ENOMEM);
1235
1236         rc = mds_update_unpack(req, offset, rec);
1237         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1238                 CERROR("invalid record\n");
1239                 GOTO(out, req->rq_status = -EINVAL);
1240         }
1241         /* rc will be used to interrupt a for loop over multiple records */
1242         rc = mds_reint_rec(rec, offset, req, lockh);
1243  out:
1244         OBD_FREE(rec, sizeof(*rec));
1245         RETURN(rc);
1246 }
1247
1248 static int mds_filter_recovery_request(struct ptlrpc_request *req,
1249                                        struct obd_device *obd, int *process)
1250 {
1251         switch (req->rq_reqmsg->opc) {
1252         case MDS_CONNECT: /* This will never get here, but for completeness. */
1253         case OST_CONNECT: /* This will never get here, but for completeness. */
1254         case MDS_DISCONNECT:
1255         case OST_DISCONNECT:
1256                *process = 1;
1257                RETURN(0);
1258
1259         case MDS_CLOSE:
1260         case MDS_SYNC: /* used in unmounting */
1261         case OBD_PING:
1262         case MDS_REINT:
1263         case LDLM_ENQUEUE:
1264         case OST_CREATE:
1265                 *process = target_queue_recovery_request(req, obd);
1266                 RETURN(0);
1267
1268         default:
1269                 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1270                 *process = 0;
1271                 /* XXX what should we set rq_status to here? */
1272                 req->rq_status = -EAGAIN;
1273                 RETURN(ptlrpc_error(req));
1274         }
1275 }
1276
1277 static char *reint_names[] = {
1278         [REINT_SETATTR] "setattr",
1279         [REINT_CREATE]  "create",
1280         [REINT_LINK]    "link",
1281         [REINT_UNLINK]  "unlink",
1282         [REINT_RENAME]  "rename",
1283         [REINT_OPEN]    "open",
1284 };
1285
1286 #define FILTER_VALID_FLAGS (OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLGENER  |\
1287                             OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ|\
1288                             OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME|\
1289                             OBD_MD_FLID) 
1290
1291 static void reconstruct_create(struct ptlrpc_request *req)
1292 {
1293         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1294         struct mds_client_data *mcd = med->med_mcd;
1295         struct ost_body *body;
1296
1297         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1298
1299         /* copy rc, transno and disp; steal locks */
1300         mds_req_from_mcd(req, mcd);
1301         CERROR("reconstruct reply for x"LPU64"\n", req->rq_xid);
1302 }
1303
1304 static int mdt_obj_create(struct ptlrpc_request *req)
1305 {
1306         struct obd_device *obd = req->rq_export->exp_obd;
1307         struct ldlm_res_id res_id = { .name = {0} };
1308         struct mds_obd *mds = &obd->u.mds;
1309         struct ost_body *body, *repbody;
1310         int rc, size = sizeof(*repbody);
1311         char fidname[LL_FID_NAMELEN];
1312         struct inode *parent_inode;
1313         struct lustre_handle lockh;
1314         struct lvfs_run_ctxt saved;
1315         ldlm_policy_data_t policy;
1316         struct dentry *new = NULL;
1317         struct dentry_params dp;
1318         int mealen, flags = 0;
1319         unsigned int tmpname;
1320         struct lvfs_ucred uc;
1321         struct mea *mea;
1322         void *handle;
1323         ENTRY;
1324        
1325         DEBUG_REQ(D_HA, req, "create remote object");
1326
1327         parent_inode = mds->mds_objects_dir->d_inode;
1328
1329         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
1330                                   lustre_swab_ost_body);
1331         if (body == NULL)
1332                 RETURN(-EFAULT);
1333
1334         MDS_CHECK_RESENT(req, reconstruct_create(req));
1335
1336         uc.luc_fsuid = body->oa.o_uid;
1337         uc.luc_fsgid = body->oa.o_gid;
1338
1339         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1340         
1341         rc = lustre_pack_reply(req, 1, &size, NULL);
1342         if (rc)
1343                 RETURN(rc);
1344
1345         repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
1346
1347         if (body->oa.o_flags & OBD_FL_RECREATE_OBJS) {
1348                 /* this is re-create request from MDS holding directory name.
1349                  * we have to lookup given ino/generation first. if it exists
1350                  * (good case) then there is nothing to do. if it does not
1351                  * then we have to recreate it */
1352                 struct ll_fid fid;
1353                 fid.id = body->oa.o_id;
1354                 fid.generation = body->oa.o_generation;
1355                 new = mds_fid2dentry(mds, &fid, NULL);
1356                 if (!IS_ERR(new) && new->d_inode) {
1357                         CWARN("mkdir() repairing is on its way: %lu/%lu\n",
1358                               (unsigned long) fid.id,
1359                               (unsigned long) fid.generation);
1360                         obdo_from_inode(&repbody->oa, new->d_inode,
1361                                         FILTER_VALID_FLAGS);
1362                         repbody->oa.o_id = new->d_inode->i_ino;
1363                         repbody->oa.o_generation = new->d_inode->i_generation;
1364                         repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1365                         GOTO(cleanup2, rc = 0);
1366                 }
1367                 CWARN("hmm. for some reason dir %lu/%lu (or reply) got lost\n",
1368                       (unsigned long) fid.id, (unsigned long) fid.generation);
1369                 LASSERT(new->d_inode == NULL ||
1370                         new->d_inode->i_generation != fid.generation);
1371                 l_dput(new); 
1372         }
1373         
1374         down(&parent_inode->i_sem);
1375         handle = fsfilt_start(obd, parent_inode, FSFILT_OP_MKDIR, NULL);
1376         LASSERT(!IS_ERR(handle));
1377
1378 repeat:
1379         tmpname = ll_insecure_random_int();
1380         rc = sprintf(fidname, "%u", tmpname);
1381         new = lookup_one_len(fidname, mds->mds_objects_dir, rc);
1382         if (IS_ERR(new)) {
1383                 CERROR("%s: can't lookup new inode (%s) for mkdir: %d\n",
1384                        obd->obd_name, fidname, (int) PTR_ERR(new));
1385                 fsfilt_commit(obd, mds->mds_sb, new->d_inode, handle, 0);
1386                 up(&parent_inode->i_sem);
1387                 RETURN(PTR_ERR(new));
1388         } else if (new->d_inode) {
1389                 CERROR("%s: name exists. repeat\n", obd->obd_name);
1390                 goto repeat;
1391         }
1392
1393         new->d_fsdata = (void *) &dp;
1394         dp.p_inum = 0;
1395         dp.p_ptr = req;
1396
1397         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1398                 DEBUG_REQ(D_HA, req, "replay create obj %lu/%lu",
1399                           (unsigned long) body->oa.o_id,
1400                           (unsigned long) body->oa.o_generation);
1401                 dp.p_inum = body->oa.o_id;
1402                 dp.p_generation = body->oa.o_generation;
1403         }
1404         rc = vfs_mkdir(parent_inode, new, body->oa.o_mode);
1405         if (rc == 0) {
1406                 obdo_from_inode(&repbody->oa, new->d_inode, FILTER_VALID_FLAGS);
1407                 repbody->oa.o_id = new->d_inode->i_ino;
1408                 repbody->oa.o_generation = new->d_inode->i_generation;
1409                 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1410
1411                 rc = fsfilt_del_dir_entry(obd, new);
1412                 up(&parent_inode->i_sem);
1413
1414                 if (rc) {
1415                         CERROR("can't remove name for object: %d\n", rc);
1416                         GOTO(cleanup, rc);
1417                 }
1418                         
1419                 /* this lock should be taken to serialize MDS modifications
1420                  * in failure case */
1421                 res_id.name[0] = new->d_inode->i_ino;
1422                 res_id.name[1] = new->d_inode->i_generation;
1423                 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1424                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1425                                 res_id, LDLM_IBITS, &policy,
1426                                 LCK_EX, &flags, mds_blocking_ast,
1427                                 ldlm_completion_ast, NULL, NULL,
1428                                 NULL, 0, NULL, &lockh);
1429                 if (rc != ELDLM_OK)
1430                         GOTO(cleanup, rc);
1431
1432                 CDEBUG(D_OTHER, "created dirobj: %lu/%lu mode %o\n",
1433                                 (unsigned long) new->d_inode->i_ino,
1434                                 (unsigned long) new->d_inode->i_generation,
1435                                 (unsigned) new->d_inode->i_mode);
1436         } else {
1437                 up(&parent_inode->i_sem);
1438                 CERROR("%s: can't create dirobj: %d\n", obd->obd_name, rc);
1439         }
1440
1441         if (rc == 0 && body->oa.o_valid & OBD_MD_FLID) {
1442                 /* this is new object for splitted dir. we have to
1443                  * prevent recursive splitting on it -bzzz */
1444                 mealen = obd_size_diskmd(mds->mds_lmv_exp, NULL);
1445                 OBD_ALLOC(mea, mealen);
1446                 if (mea == NULL)
1447                         GOTO(cleanup, rc = -ENOMEM);
1448                 mea->mea_count = 0;
1449                 down(&new->d_inode->i_sem);
1450                 rc = fsfilt_set_md(obd, new->d_inode, handle, mea, mealen);
1451                 up(&new->d_inode->i_sem);
1452                 OBD_FREE(mea, mealen);
1453         } else if (rc == 0 && body->oa.o_easize) {
1454                 flags = mds_try_to_split_dir(obd, new, NULL, body->oa.o_easize);
1455                 CERROR("%s: splitted %lu/%u - %d\n", obd->obd_name,
1456                        new->d_inode->i_ino, new->d_inode->i_generation, flags);
1457         }
1458
1459 cleanup:
1460         rc = mds_finish_transno(mds, parent_inode, handle, req, rc, 0);
1461         if (rc == 0)
1462                 ptlrpc_save_lock(req, &lockh, LCK_EX);
1463         else
1464                 ldlm_lock_decref(&lockh, LCK_EX);
1465 cleanup2:
1466         l_dput(new);
1467         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1468         RETURN(rc);
1469 }
1470
1471 static int mdt_get_info(struct ptlrpc_request *req)
1472 {
1473         char *key;
1474         struct obd_export *exp = req->rq_export;
1475         int keylen, rc = 0, size = sizeof(obd_id);
1476         obd_id *reply;
1477         ENTRY;
1478
1479         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1480         if (key == NULL) {
1481                 DEBUG_REQ(D_HA, req, "no get_info key");
1482                 RETURN(-EFAULT);
1483         }
1484         keylen = req->rq_reqmsg->buflens[0];
1485
1486         if (keylen < strlen("mdsize") || memcmp(key, "mdsize", 6) != 0)
1487                 RETURN(-EPROTO);
1488
1489         rc = lustre_pack_reply(req, 1, &size, NULL);
1490         if (rc)
1491                 RETURN(rc);
1492
1493         reply = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply));
1494         rc = obd_get_info(exp, keylen, key, &size, reply);
1495         req->rq_repmsg->status = 0;
1496         RETURN(rc);
1497 }
1498
1499 static int mds_set_info(struct obd_export *exp, __u32 keylen,
1500                         void *key, __u32 vallen, void *val)
1501 {
1502         struct obd_device *obd;
1503         struct mds_obd *mds;
1504         int    rc = 0;
1505         ENTRY;
1506
1507         obd = class_exp2obd(exp);
1508         if (obd == NULL) {
1509                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1510                        exp->exp_handle.h_cookie);
1511                 RETURN(-EINVAL);
1512         }
1513
1514 #define KEY_IS(str) \
1515         (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
1516
1517         mds = &obd->u.mds;
1518         if (KEY_IS("mds_num")) {
1519                 int valsize;
1520                 __u32 group;
1521                 CDEBUG(D_IOCTL, "set mds num %d\n", *(int*)val);
1522                 mds->mds_num = *(int*)val;
1523                 group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
1524                 valsize = sizeof(group);
1525                 /*mds number has been changed, so the corresponding obdfilter exp
1526                  *need to be changed too*/
1527                 rc = obd_set_info(mds->mds_osc_exp, strlen("mds_conn"), "mds_conn",
1528                           valsize, &group);
1529                 RETURN(rc);
1530         } else if (KEY_IS("client")) {
1531                 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)) {
1532                         atomic_inc(&mds->mds_real_clients);
1533                         CDEBUG(D_OTHER,"%s: peer from %s is real client (%d)\n",
1534                                obd->obd_name, exp->exp_client_uuid.uuid,
1535                                atomic_read(&mds->mds_real_clients));
1536                         exp->exp_flags |= OBD_OPT_REAL_CLIENT;
1537                 }
1538                 if (mds->mds_lmv_name) {
1539                         rc = mds_lmv_connect(obd, mds->mds_lmv_name);
1540                         LASSERT(rc == 0);
1541                 }
1542                 RETURN(0);
1543         }
1544         CDEBUG(D_IOCTL, "invalid key\n");
1545         RETURN(-EINVAL);
1546 }
1547
1548 static int mdt_set_info(struct ptlrpc_request *req)
1549 {
1550         char *key, *val;
1551         struct obd_export *exp = req->rq_export;
1552         int keylen, rc = 0, vallen;
1553         ENTRY;
1554
1555         key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1556         if (key == NULL) {
1557                 DEBUG_REQ(D_HA, req, "no set_info key");
1558                 RETURN(-EFAULT);
1559         }
1560         keylen = req->rq_reqmsg->buflens[0];
1561
1562         if (keylen == strlen("mds_num") &&
1563             memcmp(key, "mds_num", keylen) == 0) {
1564                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1565                 if (rc)
1566                         RETURN(rc);
1567                 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
1568
1569                 vallen = req->rq_reqmsg->buflens[1];
1570
1571                 rc = obd_set_info(exp, keylen, key, vallen, val);
1572                 req->rq_repmsg->status = 0;
1573                 RETURN(rc);
1574         } else if (keylen == strlen("client") &&
1575                    memcmp(key, "client", keylen) == 0) {
1576                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1577                 if (rc)
1578                         RETURN(rc);
1579                 rc = obd_set_info(exp, keylen, key, sizeof(obd_id), NULL);
1580                 req->rq_repmsg->status = 0;
1581                 RETURN(rc);
1582         } 
1583         CDEBUG(D_IOCTL, "invalid key\n");
1584         RETURN(-EINVAL);
1585 }
1586
1587 extern int ost_brw_write(struct ptlrpc_request *, struct obd_trans_info *);
1588 int mds_handle(struct ptlrpc_request *req)
1589 {
1590         int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
1591         int rc = 0;
1592         struct mds_obd *mds = NULL; /* quell gcc overwarning */
1593         struct obd_device *obd = NULL;
1594         ENTRY;
1595
1596         OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
1597
1598         LASSERT(current->journal_info == NULL);
1599         /* XXX identical to OST */
1600         if (req->rq_reqmsg->opc != MDS_CONNECT) {
1601                 struct mds_export_data *med;
1602                 int recovering, abort_recovery;
1603
1604                 if (req->rq_export == NULL) {
1605                         CERROR("lustre_mds: operation %d on unconnected MDS\n",
1606                                req->rq_reqmsg->opc);
1607                         req->rq_status = -ENOTCONN;
1608                         GOTO(out, rc = -ENOTCONN);
1609                 }
1610
1611                 med = &req->rq_export->exp_mds_data;
1612                 obd = req->rq_export->exp_obd;
1613                 mds = &obd->u.mds;
1614
1615                 /* sanity check: if the xid matches, the request must
1616                  * be marked as a resent or replayed */
1617                 if (req->rq_xid == med->med_mcd->mcd_last_xid)
1618                         LASSERTF(lustre_msg_get_flags(req->rq_reqmsg) &
1619                                  (MSG_RESENT | MSG_REPLAY),
1620                                  "rq_xid "LPU64" matches last_xid, "
1621                                  "expected RESENT flag\n",
1622                                  req->rq_xid);
1623                 /* else: note the opposite is not always true; a
1624                  * RESENT req after a failover will usually not match
1625                  * the last_xid, since it was likely never
1626                  * committed. A REPLAYed request will almost never
1627                  * match the last xid, however it could for a
1628                  * committed, but still retained, open. */
1629
1630                 /* Check for aborted recovery. */
1631                 spin_lock_bh(&obd->obd_processing_task_lock);
1632                 abort_recovery = obd->obd_abort_recovery;
1633                 recovering = obd->obd_recovering;
1634                 spin_unlock_bh(&obd->obd_processing_task_lock);
1635                 if (abort_recovery) {
1636                         target_abort_recovery(obd);
1637                 } else if (recovering) {
1638                         rc = mds_filter_recovery_request(req, obd,
1639                                                          &should_process);
1640                         if (rc || !should_process)
1641                                 RETURN(rc);
1642                 }
1643         }
1644
1645         switch (req->rq_reqmsg->opc) {
1646         case MDS_CONNECT:
1647                 DEBUG_REQ(D_INODE, req, "connect");
1648                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
1649                 rc = target_handle_connect(req, mds_handle);
1650                 if (!rc)
1651                         /* Now that we have an export, set mds. */
1652                         mds = mds_req2mds(req);
1653                 break;
1654
1655         case MDS_DISCONNECT:
1656                 DEBUG_REQ(D_INODE, req, "disconnect");
1657                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
1658                 rc = target_handle_disconnect(req);
1659                 req->rq_status = rc;            /* superfluous? */
1660                 break;
1661
1662         case MDS_GETSTATUS:
1663                 DEBUG_REQ(D_INODE, req, "getstatus");
1664                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1665                 rc = mds_getstatus(req);
1666                 break;
1667
1668         case MDS_GETATTR:
1669                 DEBUG_REQ(D_INODE, req, "getattr");
1670                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1671                 rc = mds_getattr(0, req);
1672                 break;
1673
1674         case MDS_GETATTR_NAME: {
1675                 struct lustre_handle lockh;
1676                 DEBUG_REQ(D_INODE, req, "getattr_name");
1677                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NAME_NET, 0);
1678
1679                 /* If this request gets a reconstructed reply, we won't be
1680                  * acquiring any new locks in mds_getattr_name, so we don't
1681                  * want to cancel.
1682                  */
1683                 lockh.cookie = 0;
1684                 rc = mds_getattr_name(0, req, &lockh, MDS_INODELOCK_UPDATE);
1685                 /* this non-intent call (from an ioctl) is special */
1686                 req->rq_status = rc;
1687                 if (rc == 0 && lockh.cookie)
1688                         ldlm_lock_decref(&lockh, LCK_PR);
1689                 break;
1690         }
1691         case MDS_STATFS:
1692                 DEBUG_REQ(D_INODE, req, "statfs");
1693                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1694                 rc = mds_statfs(req);
1695                 break;
1696
1697         case MDS_READPAGE:
1698                 DEBUG_REQ(D_INODE, req, "readpage");
1699                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1700                 rc = mds_readpage(req);
1701
1702                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
1703                         if (req->rq_reply_state) {
1704                                 lustre_free_reply_state (req->rq_reply_state);
1705                                 req->rq_reply_state = NULL;
1706                         }
1707                         RETURN(0);
1708                 }
1709
1710                 break;
1711
1712         case MDS_REINT: {
1713                 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*opcp));
1714                 __u32  opc;
1715                 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
1716                                mds->mds_max_cookiesize};
1717                 int bufcount;
1718
1719                 /* NB only peek inside req now; mds_reint() will swab it */
1720                 if (opcp == NULL) {
1721                         CERROR ("Can't inspect opcode\n");
1722                         rc = -EINVAL;
1723                         break;
1724                 }
1725                 opc = *opcp;
1726                 if (lustre_msg_swabbed (req->rq_reqmsg))
1727                         __swab32s(&opc);
1728
1729                 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
1730                           (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
1731                            reint_names[opc] == NULL) ? reint_names[opc] :
1732                                                        "unknown opcode");
1733
1734                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1735
1736                 if (opc == REINT_UNLINK)
1737                         bufcount = 3;
1738                 else if (opc == REINT_OPEN || opc == REINT_RENAME)
1739                         bufcount = 2;
1740                 else
1741                         bufcount = 1;
1742
1743                 rc = lustre_pack_reply(req, bufcount, size, NULL);
1744                 if (rc)
1745                         break;
1746
1747                 rc = mds_reint(req, 0, NULL);
1748                 fail = OBD_FAIL_MDS_REINT_NET_REP;
1749                 break;
1750         }
1751
1752         case MDS_CLOSE:
1753                 DEBUG_REQ(D_INODE, req, "close");
1754                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1755                 rc = mds_close(req);
1756                 break;
1757
1758         case MDS_DONE_WRITING:
1759                 DEBUG_REQ(D_INODE, req, "done_writing");
1760                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
1761                 rc = mds_done_writing(req);
1762                 break;
1763
1764         case MDS_PIN:
1765                 DEBUG_REQ(D_INODE, req, "pin");
1766                 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
1767                 rc = mds_pin(req);
1768                 break;
1769
1770         case MDS_SYNC:
1771                 DEBUG_REQ(D_INODE, req, "sync");
1772                 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
1773                 rc = mds_sync(req);
1774                 break;
1775
1776         case OBD_PING:
1777                 DEBUG_REQ(D_INODE, req, "ping");
1778                 rc = target_handle_ping(req);
1779                 break;
1780
1781         case OBD_LOG_CANCEL:
1782                 CDEBUG(D_INODE, "log cancel\n");
1783                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1784                 rc = -ENOTSUPP; /* la la la */
1785                 break;
1786
1787         case LDLM_ENQUEUE:
1788                 DEBUG_REQ(D_INODE, req, "enqueue");
1789                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1790                 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1791                                          ldlm_server_blocking_ast, NULL);
1792                 break;
1793         case LDLM_CONVERT:
1794                 DEBUG_REQ(D_INODE, req, "convert");
1795                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1796                 rc = ldlm_handle_convert(req);
1797                 break;
1798         case LDLM_BL_CALLBACK:
1799         case LDLM_CP_CALLBACK:
1800                 DEBUG_REQ(D_INODE, req, "callback");
1801                 CERROR("callbacks should not happen on MDS\n");
1802                 LBUG();
1803                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1804                 break;
1805         case LLOG_ORIGIN_HANDLE_OPEN:
1806                 DEBUG_REQ(D_INODE, req, "llog_init");
1807                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1808                 rc = llog_origin_handle_open(req);
1809                 break;
1810         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1811                 DEBUG_REQ(D_INODE, req, "llog next block");
1812                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1813                 rc = llog_origin_handle_next_block(req);
1814                 break;
1815         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
1816                 DEBUG_REQ(D_INODE, req, "llog prev block");
1817                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1818                 rc = llog_origin_handle_prev_block(req);
1819                 break;
1820         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1821                 DEBUG_REQ(D_INODE, req, "llog read header");
1822                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1823                 rc = llog_origin_handle_read_header(req);
1824                 break;
1825         case LLOG_ORIGIN_HANDLE_CLOSE:
1826                 DEBUG_REQ(D_INODE, req, "llog close");
1827                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1828                 rc = llog_origin_handle_close(req);
1829                 break;
1830         case OST_CREATE:
1831                 DEBUG_REQ(D_INODE, req, "ost_create");
1832                 rc = mdt_obj_create(req);
1833                 break;
1834         case OST_GET_INFO:
1835                 DEBUG_REQ(D_INODE, req, "get_info");
1836                 rc = mdt_get_info(req);
1837                 break;
1838         case OST_SET_INFO:
1839                 DEBUG_REQ(D_INODE, req, "set_info");
1840                 rc = mdt_set_info(req);
1841                 break;
1842         case OST_WRITE:
1843                 CDEBUG(D_INODE, "write\n");
1844                 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1845                 rc = ost_brw_write(req, NULL);
1846                 LASSERT(current->journal_info == NULL);
1847                 /* mdt_brw sends its own replies */
1848                 RETURN(rc);
1849                 break;
1850         case LLOG_CATINFO:
1851                 DEBUG_REQ(D_INODE, req, "llog catinfo");
1852                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1853                 rc = llog_catinfo(req);
1854                 break;
1855         default:
1856                 req->rq_status = -ENOTSUPP;
1857                 rc = ptlrpc_error(req);
1858                 RETURN(rc);
1859         }
1860
1861         LASSERT(current->journal_info == NULL);
1862
1863         EXIT;
1864
1865         /* If we're DISCONNECTing, the mds_export_data is already freed */
1866         if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
1867                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1868                 struct obd_device *obd = list_entry(mds, struct obd_device,
1869                                                     u.mds);
1870                 req->rq_repmsg->last_xid =
1871                         le64_to_cpu(med->med_mcd->mcd_last_xid);
1872
1873                 if (!obd->obd_no_transno) {
1874                         req->rq_repmsg->last_committed =
1875                                 obd->obd_last_committed;
1876                 } else {
1877                         DEBUG_REQ(D_IOCTL, req,
1878                                   "not sending last_committed update");
1879                 }
1880                 CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
1881                        ", xid "LPU64"\n",
1882                        mds->mds_last_transno, obd->obd_last_committed,
1883                        req->rq_xid);
1884         }
1885  out:
1886
1887         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
1888                 if (obd && obd->obd_recovering) {
1889                         DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
1890                         return target_queue_final_reply(req, rc);
1891                 }
1892                 /* Lost a race with recovery; let the error path DTRT. */
1893                 rc = req->rq_status = -ENOTCONN;
1894         }
1895
1896         target_send_reply(req, rc, fail);
1897         return 0;
1898 }
1899
1900 /* Update the server data on disk.  This stores the new mount_count and
1901  * also the last_rcvd value to disk.  If we don't have a clean shutdown,
1902  * then the server last_rcvd value may be less than that of the clients.
1903  * This will alert us that we may need to do client recovery.
1904  *
1905  * Also assumes for mds_last_transno that we are not modifying it (no locking).
1906  */
1907 int mds_update_server_data(struct obd_device *obd, int force_sync)
1908 {
1909         struct mds_obd *mds = &obd->u.mds;
1910         struct mds_server_data *msd = mds->mds_server_data;
1911         struct file *filp = mds->mds_rcvd_filp;
1912         struct lvfs_run_ctxt saved;
1913         loff_t off = 0;
1914         int rc;
1915         ENTRY;
1916
1917         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1918         msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
1919
1920         CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
1921                mds->mds_mount_count, mds->mds_last_transno);
1922         rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off,force_sync);
1923         if (rc)
1924                 CERROR("error writing MDS server data: rc = %d\n", rc);
1925         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1926
1927         RETURN(rc);
1928 }
1929
1930 /* mount the file system (secretly) */
1931 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
1932 {
1933         struct lustre_cfg* lcfg = buf;
1934         struct mds_obd *mds = &obd->u.mds;
1935         char *options = NULL;
1936         struct vfsmount *mnt;
1937         unsigned long page;
1938         int rc = 0;
1939         ENTRY;
1940
1941         dev_clear_rdonly(2);
1942
1943         if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
1944                 RETURN(rc = -EINVAL);
1945
1946         obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
1947         if (IS_ERR(obd->obd_fsops))
1948                 RETURN(rc = PTR_ERR(obd->obd_fsops));
1949
1950         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1951
1952         page = __get_free_page(GFP_KERNEL);
1953         if (!page)
1954                 RETURN(-ENOMEM);
1955
1956         options = (char *)page;
1957         memset(options, 0, PAGE_SIZE);
1958
1959         /* here we use "iopen_nopriv" hardcoded, because it affects MDS utility
1960          * and the rest of options are passed by mount options. Probably this
1961          * should be moved to somewhere else like startup scripts or lconf. */
1962         sprintf(options, "iopen_nopriv");
1963
1964         if (lcfg->lcfg_inllen4 > 0 && lcfg->lcfg_inlbuf4)
1965                 sprintf(options + strlen(options), ",%s",
1966                         lcfg->lcfg_inlbuf4);
1967
1968         /* we have to know mdsnum before touching underlying fs -bzzz */
1969         if (lcfg->lcfg_inllen5 > 0 && lcfg->lcfg_inlbuf5 && 
1970             strcmp(lcfg->lcfg_inlbuf5, "dumb")) {
1971                 class_uuid_t uuid;
1972
1973                 CDEBUG(D_OTHER, "MDS: %s is master for %s\n",
1974                        obd->obd_name, lcfg->lcfg_inlbuf5);
1975
1976                 generate_random_uuid(uuid);
1977                 class_uuid_unparse(uuid, &mds->mds_lmv_uuid);
1978
1979                 OBD_ALLOC(mds->mds_lmv_name, lcfg->lcfg_inllen5);
1980                 if (mds->mds_lmv_name == NULL) 
1981                         RETURN(rc = -ENOMEM);
1982
1983                 memcpy(mds->mds_lmv_name, lcfg->lcfg_inlbuf5,
1984                        lcfg->lcfg_inllen5);
1985                 
1986                 rc = mds_lmv_connect(obd, mds->mds_lmv_name);
1987                 if (rc) {
1988                         OBD_FREE(mds->mds_lmv_name, lcfg->lcfg_inllen5);
1989                         GOTO(err_ops, rc);
1990                 }
1991         }
1992         
1993         /* FIXME-WANGDI: this should be reworked when we will use lmv along 
1994          * with cobd, because correct mdsnum is set in mds_lmv_connect(). */
1995         if (lcfg->lcfg_inllen6 > 0 && lcfg->lcfg_inlbuf6 && !mds->mds_lmv_obd &&
1996             strcmp(lcfg->lcfg_inlbuf6, "dumb")) {
1997                 if (!memcmp(lcfg->lcfg_inlbuf6, "master", strlen("master")) &&
1998                     mds->mds_num == 0) {
1999                         mds->mds_num = REAL_MDS_NUMBER;
2000                 } else if (!memcmp(lcfg->lcfg_inlbuf6, "cache", strlen("cache")) &&
2001                            mds->mds_num == 0) {
2002                         mds->mds_num = CACHE_MDS_NUMBER;
2003                 }     
2004         }
2005
2006         mnt = do_kern_mount(lcfg->lcfg_inlbuf2, 0, 
2007                             lcfg->lcfg_inlbuf1, options);
2008
2009         free_page(page);
2010
2011         if (IS_ERR(mnt)) {
2012                 rc = PTR_ERR(mnt);
2013                 CERROR("do_kern_mount failed: rc = %d\n", rc);
2014                 GOTO(err_ops, rc);
2015         }
2016
2017         CDEBUG(D_SUPER, "%s: mnt = %p\n", lcfg->lcfg_inlbuf1, mnt);
2018
2019         sema_init(&mds->mds_orphan_recovery_sem, 1);
2020         sema_init(&mds->mds_epoch_sem, 1);
2021         spin_lock_init(&mds->mds_transno_lock);
2022         mds->mds_max_cookiesize = sizeof(struct llog_cookie);
2023         atomic_set(&mds->mds_open_count, 0);
2024         atomic_set(&mds->mds_real_clients, 0);
2025
2026         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
2027                                                 LDLM_NAMESPACE_SERVER);
2028         if (obd->obd_namespace == NULL) {
2029                 mds_cleanup(obd, 0);
2030                 GOTO(err_put, rc = -ENOMEM);
2031         }
2032         ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
2033
2034         rc = mds_fs_setup(obd, mnt);
2035         if (rc) {
2036                 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
2037                 GOTO(err_ns, rc);
2038         }
2039
2040         rc = llog_start_commit_thread();
2041         if (rc < 0)
2042                 GOTO(err_fs, rc);
2043
2044         /* this check for @dumb string is needed to handle mounting MDS with
2045          * smfs. Read lconf:MDSDEV.write_conf() for more details. */
2046         if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3 &&
2047             strcmp(lcfg->lcfg_inlbuf3, "dumb")) {
2048                 class_uuid_t uuid;
2049
2050                 generate_random_uuid(uuid);
2051                 class_uuid_unparse(uuid, &mds->mds_lov_uuid);
2052
2053                 OBD_ALLOC(mds->mds_profile, lcfg->lcfg_inllen3);
2054                 if (mds->mds_profile == NULL)
2055                         GOTO(err_fs, rc = -ENOMEM);
2056
2057                 memcpy(mds->mds_profile, lcfg->lcfg_inlbuf3,
2058                        lcfg->lcfg_inllen3);
2059         }
2060
2061         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2062                            "mds_ldlm_client", &obd->obd_ldlm_client);
2063         obd->obd_replayable = 1;
2064
2065         rc = mds_postsetup(obd);
2066         if (rc)
2067                 GOTO(err_fs, rc);
2068
2069         RETURN(0);
2070
2071 err_fs:
2072         /* No extra cleanup needed for llog_init_commit_thread() */
2073         mds_fs_cleanup(obd, 0);
2074 err_ns:
2075         ldlm_namespace_free(obd->obd_namespace, 0);
2076         obd->obd_namespace = NULL;
2077 err_put:
2078         unlock_kernel();
2079         mntput(mds->mds_vfsmnt);
2080         mds->mds_sb = 0;
2081         lock_kernel();
2082 err_ops:
2083         fsfilt_put_ops(obd->obd_fsops);
2084         return rc;
2085 }
2086
2087 static int mds_postsetup(struct obd_device *obd)
2088 {
2089         struct mds_obd *mds = &obd->u.mds;
2090         int rc = 0;
2091         ENTRY;
2092
2093         rc = obd_llog_setup(obd, &obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT, 
2094                             obd, 0, NULL, &llog_lvfs_ops);
2095         if (rc)
2096                 RETURN(rc);
2097
2098         if (mds->mds_profile) {
2099                 struct llog_ctxt *lgctxt;
2100                 struct lvfs_run_ctxt saved;
2101                 struct lustre_profile *lprof;
2102                 struct config_llog_instance cfg;
2103
2104                 cfg.cfg_instance = NULL;
2105                 cfg.cfg_uuid = mds->mds_lov_uuid;
2106                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2107
2108                 lgctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
2109                 if (!lgctxt) {
2110                         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2111                         GOTO(err_llog, rc = -EINVAL);
2112                 }
2113                 
2114                 rc = class_config_process_llog(lgctxt, mds->mds_profile, &cfg);
2115                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2116
2117                 if (rc)
2118                         GOTO(err_llog, rc);
2119
2120                 lprof = class_get_profile(mds->mds_profile);
2121                 if (lprof == NULL) {
2122                         CERROR("No profile found: %s\n", mds->mds_profile);
2123                         GOTO(err_cleanup, rc = -ENOENT);
2124                 }
2125                 rc = mds_lov_connect(obd, lprof->lp_osc);
2126                 if (rc)
2127                         GOTO(err_cleanup, rc);
2128
2129                 rc = mds_lmv_postsetup(obd);
2130                 if (rc)
2131                         GOTO(err_cleanup, rc);
2132         }
2133
2134         RETURN(rc);
2135
2136 err_cleanup:
2137         mds_lov_clean(obd);
2138 err_llog:
2139         obd_llog_cleanup(llog_get_context(&obd->obd_llogs,
2140                                           LLOG_CONFIG_ORIG_CTXT));
2141         RETURN(rc);
2142 }
2143
2144 int mds_postrecov(struct obd_device *obd)
2145 {
2146         struct mds_obd *mds = &obd->u.mds;
2147         struct llog_ctxt *ctxt;
2148         int rc, item = 0;
2149         ENTRY;
2150
2151         LASSERT(!obd->obd_recovering);
2152         ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
2153         LASSERT(ctxt != NULL);
2154
2155         /* set nextid first, so we are sure it happens */
2156         rc = mds_lov_set_nextid(obd);
2157         if (rc) {
2158                 CERROR("%s: mds_lov_set_nextid failed\n", obd->obd_name);
2159                 GOTO(out, rc);
2160         }
2161
2162         /* clean PENDING dir */
2163         rc = mds_cleanup_orphans(obd);
2164         if (rc < 0)
2165                 GOTO(out, rc);
2166         item = rc;
2167
2168         rc = llog_connect(ctxt, obd->u.mds.mds_lov_desc.ld_tgt_count,
2169                           NULL, NULL, NULL);
2170         if (rc) {
2171                 CERROR("%s: failed at llog_origin_connect: %d\n", 
2172                        obd->obd_name, rc);
2173                 GOTO(out, rc);
2174         }
2175
2176         /* remove the orphaned precreated objects */
2177         rc = mds_lov_clearorphans(mds, NULL /* all OSTs */);
2178         if (rc)
2179                 GOTO(err_llog, rc);
2180
2181 out:
2182         RETURN(rc < 0 ? rc : item);
2183
2184 err_llog:
2185         /* cleanup all llogging subsystems */
2186         rc = obd_llog_finish(obd, &obd->obd_llogs,
2187                              mds->mds_lov_desc.ld_tgt_count);
2188         if (rc)
2189                 CERROR("%s: failed to cleanup llogging subsystems\n",
2190                         obd->obd_name);
2191         goto out;
2192 }
2193
2194 int mds_lov_clean(struct obd_device *obd)
2195 {
2196         struct mds_obd *mds = &obd->u.mds;
2197
2198         if (mds->mds_profile) {
2199                 char * cln_prof;
2200                 struct config_llog_instance cfg;
2201                 struct lvfs_run_ctxt saved;
2202                 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
2203
2204                 OBD_ALLOC(cln_prof, len);
2205                 sprintf(cln_prof, "%s-clean", mds->mds_profile);
2206
2207                 cfg.cfg_instance = NULL;
2208                 cfg.cfg_uuid = mds->mds_lov_uuid;
2209
2210                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2211                 class_config_process_llog(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT),
2212                                           cln_prof, &cfg);
2213                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2214
2215                 OBD_FREE(cln_prof, len);
2216                 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
2217                 mds->mds_profile = NULL;
2218         }
2219         RETURN(0);
2220 }
2221
2222 int mds_lmv_clean(struct obd_device *obd)
2223 {
2224         struct mds_obd *mds = &obd->u.mds;
2225
2226         if (mds->mds_lmv_name) {
2227                 OBD_FREE(mds->mds_lmv_name, strlen(mds->mds_lmv_name) + 1);
2228                 mds->mds_lmv_name = NULL;
2229         }
2230         RETURN(0);
2231 }
2232
2233 static int mds_precleanup(struct obd_device *obd, int flags)
2234 {
2235         int rc = 0;
2236         ENTRY;
2237
2238         mds_lmv_clean(obd);
2239         mds_lov_disconnect(obd, flags);
2240         mds_lov_clean(obd);
2241         obd_llog_cleanup(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT));
2242         RETURN(rc);
2243 }
2244
2245 static int mds_cleanup(struct obd_device *obd, int flags)
2246 {
2247         struct mds_obd *mds = &obd->u.mds;
2248         ENTRY;
2249
2250         if (mds->mds_sb == NULL)
2251                 RETURN(0);
2252
2253         mds_update_server_data(obd, 1);
2254         if (mds->mds_lov_objids != NULL) {
2255                 OBD_FREE(mds->mds_lov_objids,
2256                          mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id));
2257         }
2258         mds_fs_cleanup(obd, flags);
2259
2260         unlock_kernel();
2261
2262         /* 2 seems normal on mds, (may_umount() also expects 2
2263           fwiw), but we only see 1 at this point in obdfilter. */
2264         if (atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count) > 2)
2265                 CERROR("%s: mount busy, mnt_count %d != 2\n", obd->obd_name,
2266                        atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count));
2267
2268         mntput(mds->mds_vfsmnt);
2269
2270         mds->mds_sb = 0;
2271
2272         ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
2273
2274         spin_lock_bh(&obd->obd_processing_task_lock);
2275         if (obd->obd_recovering) {
2276                 target_cancel_recovery_timer(obd);
2277                 obd->obd_recovering = 0;
2278         }
2279         spin_unlock_bh(&obd->obd_processing_task_lock);
2280
2281         lock_kernel();
2282         dev_clear_rdonly(2);
2283         fsfilt_put_ops(obd->obd_fsops);
2284
2285         RETURN(0);
2286 }
2287
2288 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
2289                                         struct ldlm_lock *new_lock,
2290                                         struct lustre_handle *lockh)
2291 {
2292         struct obd_export *exp = req->rq_export;
2293         struct obd_device *obd = exp->exp_obd;
2294         struct ldlm_request *dlmreq =
2295                 lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*dlmreq));
2296         struct lustre_handle remote_hdl = dlmreq->lock_handle1;
2297         struct list_head *iter;
2298
2299         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2300                 return;
2301
2302         l_lock(&obd->obd_namespace->ns_lock);
2303         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2304                 struct ldlm_lock *lock;
2305                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2306                 if (lock == new_lock)
2307                         continue;
2308                 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2309                         lockh->cookie = lock->l_handle.h_cookie;
2310                         DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
2311                                   lockh->cookie);
2312                         l_unlock(&obd->obd_namespace->ns_lock);
2313                         return;
2314                 }
2315         }
2316         l_unlock(&obd->obd_namespace->ns_lock);
2317
2318         /* If the xid matches, then we know this is a resent request,
2319          * and allow it. (It's probably an OPEN, for which we don't
2320          * send a lock */
2321         if (req->rq_xid == exp->exp_mds_data.med_mcd->mcd_last_xid)
2322                 return;
2323
2324         /* This remote handle isn't enqueued, so we never received or
2325          * processed this request.  Clear MSG_RESENT, because it can
2326          * be handled like any normal request now. */
2327
2328         lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2329
2330         DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
2331                   remote_hdl.cookie);
2332 }
2333
2334 int intent_disposition(struct ldlm_reply *rep, int flag)
2335 {
2336         if (!rep)
2337                 return 0;
2338         return (rep->lock_policy_res1 & flag);
2339 }
2340
2341 void intent_set_disposition(struct ldlm_reply *rep, int flag)
2342 {
2343         if (!rep)
2344                 return;
2345         rep->lock_policy_res1 |= flag;
2346 }
2347
2348 static int mds_intent_policy(struct ldlm_namespace *ns,
2349                              struct ldlm_lock **lockp, void *req_cookie,
2350                              ldlm_mode_t mode, int flags, void *data)
2351 {
2352         struct ptlrpc_request *req = req_cookie;
2353         struct ldlm_lock *lock = *lockp;
2354         struct ldlm_intent *it;
2355         struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
2356         struct ldlm_reply *rep;
2357         struct lustre_handle lockh = { 0 };
2358         struct ldlm_lock *new_lock;
2359         int getattr_part = MDS_INODELOCK_UPDATE;
2360         int rc, offset = 2, repsize[4] = {sizeof(struct ldlm_reply),
2361                                           sizeof(struct mds_body),
2362                                           mds->mds_max_mdsize,
2363                                           mds->mds_max_cookiesize};
2364         ENTRY;
2365
2366         LASSERT(req != NULL);
2367
2368         if (req->rq_reqmsg->bufcount <= 1) {
2369                 /* No intent was provided */
2370                 int size = sizeof(struct ldlm_reply);
2371                 rc = lustre_pack_reply(req, 1, &size, NULL);
2372                 LASSERT(rc == 0);
2373                 RETURN(0);
2374         }
2375
2376         it = lustre_swab_reqbuf(req, 1, sizeof(*it), lustre_swab_ldlm_intent);
2377         if (it == NULL) {
2378                 CERROR("Intent missing\n");
2379                 RETURN(req->rq_status = -EFAULT);
2380         }
2381
2382         LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
2383
2384         rc = lustre_pack_reply(req, 3, repsize, NULL);
2385         if (rc)
2386                 RETURN(req->rq_status = rc);
2387
2388         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
2389         intent_set_disposition(rep, DISP_IT_EXECD);
2390
2391         fixup_handle_for_resent_req(req, lock, &lockh);
2392
2393         /* execute policy */
2394         switch ((long)it->opc) {
2395         case IT_OPEN:
2396         case IT_CREAT|IT_OPEN:
2397                 /* XXX swab here to assert that an mds_open reint
2398                  * packet is following */
2399                 rep->lock_policy_res2 = mds_reint(req, offset, &lockh);
2400 #if 0
2401                 /* We abort the lock if the lookup was negative and
2402                  * we did not make it to the OPEN portion */
2403                 if (!intent_disposition(rep, DISP_LOOKUP_EXECD))
2404                         RETURN(ELDLM_LOCK_ABORTED);
2405                 if (intent_disposition(rep, DISP_LOOKUP_NEG) &&
2406                     !intent_disposition(rep, DISP_OPEN_OPEN))
2407 #endif
2408                 /* IT_OPEN may return lock on cross-node dentry
2409                  * that we want to hold during attr retrival -bzzz */
2410                 if (rc != 0 || lockh.cookie == 0)
2411                         RETURN(ELDLM_LOCK_ABORTED);
2412                 break;
2413         case IT_LOOKUP:
2414                 getattr_part = MDS_INODELOCK_LOOKUP;
2415         case IT_CHDIR:
2416         case IT_GETATTR:
2417                 getattr_part |= MDS_INODELOCK_LOOKUP;
2418         case IT_READDIR:
2419                 rep->lock_policy_res2 = mds_getattr_name(offset, req, &lockh,
2420                                                          getattr_part);
2421                 /* FIXME: LDLM can set req->rq_status. MDS sets
2422                    policy_res{1,2} with disposition and status.
2423                    - replay: returns 0 & req->status is old status
2424                    - otherwise: returns req->status */
2425                 if (intent_disposition(rep, DISP_LOOKUP_NEG))
2426                         rep->lock_policy_res2 = 0;
2427                 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
2428                     rep->lock_policy_res2)
2429                         RETURN(ELDLM_LOCK_ABORTED);
2430                 if (req->rq_status != 0) {
2431                         LBUG();
2432                         rep->lock_policy_res2 = req->rq_status;
2433                         RETURN(ELDLM_LOCK_ABORTED);
2434                 }
2435                 break;
2436         case IT_UNLINK:
2437                 rc = mds_lock_and_check_slave(offset, req, &lockh);
2438                 if ((rep->lock_policy_res2 = rc)) {
2439                         if (rc == ENOLCK)
2440                                 rep->lock_policy_res2 = 0;
2441                         RETURN(ELDLM_LOCK_ABORTED);
2442                 }
2443                 break;
2444         default:
2445                 CERROR("Unhandled intent "LPD64"\n", it->opc);
2446                 LBUG();
2447         }
2448
2449         /* By this point, whatever function we called above must have either
2450          * filled in 'lockh', been an intent replay, or returned an error.  We
2451          * want to allow replayed RPCs to not get a lock, since we would just
2452          * drop it below anyways because lock replay is done separately by the
2453          * client afterwards.  For regular RPCs we want to give the new lock to
2454          * the client instead of whatever lock it was about to get. */
2455         new_lock = ldlm_handle2lock(&lockh);
2456         if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
2457                 RETURN(0);
2458
2459         LASSERT(new_lock != NULL);
2460
2461         /* If we've already given this lock to a client once, then we should
2462          * have no readers or writers.  Otherwise, we should have one reader
2463          * _or_ writer ref (which will be zeroed below) before returning the
2464          * lock to a client. */
2465         if (new_lock->l_export == req->rq_export) {
2466                 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2467         } else {
2468                 LASSERT(new_lock->l_export == NULL);
2469                 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2470         }
2471
2472         *lockp = new_lock;
2473
2474         if (new_lock->l_export == req->rq_export) {
2475                 /* Already gave this to the client, which means that we
2476                  * reconstructed a reply. */
2477                 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2478                         MSG_RESENT);
2479                 RETURN(ELDLM_LOCK_REPLACED);
2480         }
2481
2482         /* Fixup the lock to be given to the client */
2483         l_lock(&new_lock->l_resource->lr_namespace->ns_lock);
2484         new_lock->l_readers = 0;
2485         new_lock->l_writers = 0;
2486
2487         new_lock->l_export = class_export_get(req->rq_export);
2488         list_add(&new_lock->l_export_chain,
2489                  &new_lock->l_export->exp_ldlm_data.led_held_locks);
2490
2491         new_lock->l_blocking_ast = lock->l_blocking_ast;
2492         new_lock->l_completion_ast = lock->l_completion_ast;
2493
2494         memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
2495                sizeof(lock->l_remote_handle));
2496
2497         new_lock->l_flags &= ~LDLM_FL_LOCAL;
2498
2499         LDLM_LOCK_PUT(new_lock);
2500         l_unlock(&new_lock->l_resource->lr_namespace->ns_lock);
2501
2502         RETURN(ELDLM_LOCK_REPLACED);
2503 }
2504
2505 int mds_attach(struct obd_device *dev, obd_count len, void *data)
2506 {
2507         struct lprocfs_static_vars lvars;
2508
2509         lprocfs_init_multi_vars(0, &lvars);
2510         return lprocfs_obd_attach(dev, lvars.obd_vars);
2511 }
2512
2513 int mds_detach(struct obd_device *dev)
2514 {
2515         return lprocfs_obd_detach(dev);
2516 }
2517
2518 int mdt_attach(struct obd_device *dev, obd_count len, void *data)
2519 {
2520         struct lprocfs_static_vars lvars;
2521
2522         lprocfs_init_multi_vars(1, &lvars);
2523         return lprocfs_obd_attach(dev, lvars.obd_vars);
2524 }
2525
2526 int mdt_detach(struct obd_device *dev)
2527 {
2528         return lprocfs_obd_detach(dev);
2529 }
2530
2531 static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
2532 {
2533         struct mds_obd *mds = &obd->u.mds;
2534         int rc = 0;
2535         ENTRY;
2536
2537         mds->mds_service =
2538                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2539                                 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
2540                                 mds_handle, "mds", obd->obd_proc_entry);
2541
2542         if (!mds->mds_service) {
2543                 CERROR("failed to start service\n");
2544                 RETURN(-ENOMEM);
2545         }
2546
2547         rc = ptlrpc_start_n_threads(obd, mds->mds_service, MDT_NUM_THREADS,
2548                                     "ll_mdt");
2549         if (rc)
2550                 GOTO(err_thread, rc);
2551
2552         mds->mds_setattr_service =
2553                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2554                                 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
2555                                 mds_handle, "mds_setattr",
2556                                 obd->obd_proc_entry);
2557         if (!mds->mds_setattr_service) {
2558                 CERROR("failed to start getattr service\n");
2559                 GOTO(err_thread, rc = -ENOMEM);
2560         }
2561
2562         rc = ptlrpc_start_n_threads(obd, mds->mds_setattr_service,
2563                                     MDT_NUM_THREADS, "ll_mdt_attr");
2564         if (rc)
2565                 GOTO(err_thread2, rc);
2566
2567         mds->mds_readpage_service =
2568                 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2569                                 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
2570                                 mds_handle, "mds_readpage",
2571                                 obd->obd_proc_entry);
2572         if (!mds->mds_readpage_service) {
2573                 CERROR("failed to start readpage service\n");
2574                 GOTO(err_thread2, rc = -ENOMEM);
2575         }
2576
2577         rc = ptlrpc_start_n_threads(obd, mds->mds_readpage_service,
2578                                     MDT_NUM_THREADS, "ll_mdt_rdpg");
2579
2580         if (rc)
2581                 GOTO(err_thread3, rc);
2582
2583         RETURN(0);
2584
2585 err_thread3:
2586         ptlrpc_unregister_service(mds->mds_readpage_service);
2587 err_thread2:
2588         ptlrpc_unregister_service(mds->mds_setattr_service);
2589 err_thread:
2590         ptlrpc_unregister_service(mds->mds_service);
2591         return rc;
2592 }
2593
2594 static int mdt_cleanup(struct obd_device *obd, int flags)
2595 {
2596         struct mds_obd *mds = &obd->u.mds;
2597         ENTRY;
2598
2599         ptlrpc_stop_all_threads(mds->mds_readpage_service);
2600         ptlrpc_unregister_service(mds->mds_readpage_service);
2601
2602         ptlrpc_stop_all_threads(mds->mds_setattr_service);
2603         ptlrpc_unregister_service(mds->mds_setattr_service);
2604
2605         ptlrpc_stop_all_threads(mds->mds_service);
2606         ptlrpc_unregister_service(mds->mds_service);
2607
2608         RETURN(0);
2609 }
2610
2611 static struct dentry *mds_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
2612                                           void *data)
2613 {
2614         struct obd_device *obd = data;
2615         struct ll_fid fid;
2616         fid.id = id;
2617         fid.generation = gen;
2618         return mds_fid2dentry(&obd->u.mds, &fid, NULL);
2619 }
2620
2621 static int mds_get_info(struct obd_export *exp, __u32 keylen,
2622                         void *key, __u32 *vallen, void *val)
2623 {
2624         struct obd_device *obd;
2625         struct mds_obd *mds;
2626         ENTRY;
2627
2628         obd = class_exp2obd(exp);
2629         if (obd == NULL) {
2630                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2631                        exp->exp_handle.h_cookie);
2632                 RETURN(-EINVAL);
2633         }
2634
2635         if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
2636                 /*Get log_context handle*/
2637                 unsigned long *llh_handle = val;
2638                 *vallen = sizeof(unsigned long);
2639                 *llh_handle = (unsigned long)obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT];
2640                 RETURN(0);
2641         }
2642         if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
2643                 /*Get log_context handle*/
2644                 unsigned long *sb = val;
2645                 *vallen = sizeof(unsigned long);
2646                 *sb = (unsigned long)obd->u.mds.mds_sb;
2647                 RETURN(0);
2648         }
2649
2650         mds = &obd->u.mds;
2651         keylen == strlen("mdsize");
2652         if (keylen && memcmp(key, "mdsize", keylen) == 0) {
2653                 __u32 *mdsize = val;
2654                 *vallen = sizeof(*mdsize);
2655                 *mdsize = mds->mds_max_mdsize;
2656                 RETURN(0);
2657         }
2658
2659         CDEBUG(D_IOCTL, "invalid key\n");
2660         RETURN(-EINVAL);
2661
2662 }
2663 struct lvfs_callback_ops mds_lvfs_ops = {
2664         l_fid2dentry:     mds_lvfs_fid2dentry,
2665 };
2666
2667 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
2668                 int objcount, struct obd_ioobj *obj,
2669                 int niocount, struct niobuf_remote *nb,
2670                 struct niobuf_local *res,
2671                 struct obd_trans_info *oti);
2672 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
2673                  int objcount, struct obd_ioobj *obj, int niocount,
2674                  struct niobuf_local *res, struct obd_trans_info *oti,
2675                  int rc);
2676
2677 /* use obd ops to offer management infrastructure */
2678 static struct obd_ops mds_obd_ops = {
2679         .o_owner           = THIS_MODULE,
2680         .o_attach          = mds_attach,
2681         .o_detach          = mds_detach,
2682         .o_connect         = mds_connect,
2683         .o_init_export     = mds_init_export,
2684         .o_destroy_export  = mds_destroy_export,
2685         .o_disconnect      = mds_disconnect,
2686         .o_setup           = mds_setup,
2687         .o_precleanup      = mds_precleanup,
2688         .o_cleanup         = mds_cleanup,
2689         .o_postrecov       = mds_postrecov,
2690         .o_statfs          = mds_obd_statfs,
2691         .o_iocontrol       = mds_iocontrol,
2692         .o_create          = mds_obd_create,
2693         .o_destroy         = mds_obd_destroy,
2694         .o_llog_init       = mds_llog_init,
2695         .o_llog_finish     = mds_llog_finish,
2696         .o_notify          = mds_notify,
2697         .o_get_info        = mds_get_info,
2698         .o_set_info        = mds_set_info,
2699         .o_preprw          = mds_preprw, 
2700         .o_commitrw        = mds_commitrw,
2701 };
2702
2703 static struct obd_ops mdt_obd_ops = {
2704         .o_owner           = THIS_MODULE,
2705         .o_attach          = mdt_attach,
2706         .o_detach          = mdt_detach,
2707         .o_setup           = mdt_setup,
2708         .o_cleanup         = mdt_cleanup,
2709         .o_attach          = mdt_attach,
2710         .o_detach          = mdt_detach,
2711 };
2712
2713 static int __init mds_init(void)
2714 {
2715         struct lprocfs_static_vars lvars;
2716
2717         lprocfs_init_multi_vars(0, &lvars);
2718         class_register_type(&mds_obd_ops, NULL, lvars.module_vars,
2719                             LUSTRE_MDS_NAME);
2720         lprocfs_init_multi_vars(1, &lvars);
2721         class_register_type(&mdt_obd_ops, NULL, lvars.module_vars,
2722                             LUSTRE_MDT_NAME);
2723
2724         return 0;
2725 }
2726
2727 static void /*__exit*/ mds_exit(void)
2728 {
2729         class_unregister_type(LUSTRE_MDS_NAME);
2730         class_unregister_type(LUSTRE_MDT_NAME);
2731 }
2732
2733 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2734 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
2735 MODULE_LICENSE("GPL");
2736
2737 module_init(mds_init);
2738 module_exit(mds_exit);