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