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