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