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