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