Whamcloud - gitweb
* Chain granted locks off the export.
[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, 2002 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  *
12  *   This file is part of Lustre, http://www.lustre.org.
13  *
14  *   Lustre is free software; you can redistribute it and/or
15  *   modify it under the terms of version 2 of the GNU General Public
16  *   License as published by the Free Software Foundation.
17  *
18  *   Lustre is distributed in the hope that it will be useful,
19  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *   GNU General Public License for more details.
22  *
23  *   You should have received a copy of the GNU General Public License
24  *   along with Lustre; if not, write to the Free Software
25  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #define EXPORT_SYMTAB
29 #define DEBUG_SUBSYSTEM S_MDS
30
31 #include <linux/module.h>
32 #include <linux/lustre_mds.h>
33 #include <linux/lustre_dlm.h>
34 #include <linux/init.h>
35 #include <linux/obd_class.h>
36
37 extern int mds_get_lovtgts(struct obd_device *obd, int tgt_count,
38                            uuid_t *uuidarray);
39 extern int mds_get_lovdesc(struct obd_device *obd, struct lov_desc *desc);
40 extern int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
41                                 struct ptlrpc_request *req);
42 static int mds_cleanup(struct obd_device * obddev);
43
44 inline struct mds_obd *mds_req2mds(struct ptlrpc_request *req)
45 {
46         return &req->rq_export->exp_obd->u.mds;
47 }
48
49 static int mds_bulk_timeout(void *data)
50 {
51         struct ptlrpc_bulk_desc *desc = data;
52         
53         ENTRY;
54         CERROR("(not yet) starting recovery of client %p\n", desc->b_client);
55         RETURN(1);
56 }
57
58 /* Assumes caller has already pushed into the kernel filesystem context */
59 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
60                         __u64 offset)
61 {
62         int rc = 0;
63         struct mds_obd *mds = mds_req2mds(req);
64         struct ptlrpc_bulk_desc *desc;
65         struct ptlrpc_bulk_page *bulk;
66         struct l_wait_info lwi;
67         char *buf;
68         ENTRY;
69
70         desc = ptlrpc_prep_bulk(req->rq_connection);
71         if (desc == NULL)
72                 GOTO(out, rc = -ENOMEM);
73
74         bulk = ptlrpc_prep_bulk_page(desc);
75         if (bulk == NULL)
76                 GOTO(cleanup_bulk, rc = -ENOMEM);
77
78         OBD_ALLOC(buf, PAGE_SIZE);
79         if (buf == NULL)
80                 GOTO(cleanup_bulk, rc = -ENOMEM);
81
82         rc = mds_fs_readpage(mds, file, buf, PAGE_SIZE, (loff_t *)&offset);
83
84         if (rc != PAGE_SIZE)
85                 GOTO(cleanup_buf, rc = -EIO);
86
87         bulk->b_xid = req->rq_xid;
88         bulk->b_buf = buf;
89         bulk->b_buflen = PAGE_SIZE;
90         desc->b_portal = MDS_BULK_PORTAL;
91
92         rc = ptlrpc_send_bulk(desc);
93         if (rc)
94                 GOTO(cleanup_buf, rc);
95
96         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
97                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
98                        OBD_FAIL_MDS_SENDPAGE, rc);
99                 ptlrpc_abort_bulk(desc);
100                 GOTO(cleanup_buf, rc);
101         }
102
103         lwi = LWI_TIMEOUT(obd_timeout * HZ, mds_bulk_timeout, desc);
104         rc = l_wait_event(desc->b_waitq, desc->b_flags & PTL_BULK_FL_SENT, &lwi);
105         if (rc) {
106                 if (rc != -ETIMEDOUT)
107                         LBUG();
108                 GOTO(cleanup_buf, rc);
109         }
110
111         EXIT;
112  cleanup_buf:
113         OBD_FREE(buf, PAGE_SIZE);
114  cleanup_bulk:
115         ptlrpc_free_bulk(desc);
116  out:
117         return rc;
118 }
119
120 /*
121  * Look up a named entry in a directory, and get an LDLM lock on it.
122  * 'dir' is a inode for which an LDLM lock has already been taken.
123  *
124  * If we do not need an exclusive or write lock on this entry (e.g.
125  * a read lock for attribute lookup only) then we do not hold the
126  * directory on return.  It is up to the caller to know what type
127  * of lock it is getting, and clean up appropriately.
128  */
129 struct dentry *mds_name2locked_dentry(struct obd_device *obd,
130                                       struct dentry *dir, struct vfsmount **mnt,
131                                       char *name, int namelen, int lock_mode,
132                                       struct lustre_handle *lockh,
133                                       int dir_lock_mode)
134 {
135         struct dentry *dchild;
136         int flags, rc;
137         __u64 res_id[3] = {0};
138         ENTRY;
139
140         down(&dir->d_inode->i_sem);
141         dchild = lookup_one_len(name, dir, namelen);
142         if (IS_ERR(dchild)) {
143                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
144                 up(&dir->d_inode->i_sem);
145                 LBUG();
146                 RETURN(dchild);
147         }
148         if (dir_lock_mode != LCK_EX && dir_lock_mode != LCK_PW) {
149                 up(&dir->d_inode->i_sem);
150                 ldlm_lock_decref(lockh, dir_lock_mode);
151         }
152
153         if (lock_mode == 0 || !dchild->d_inode)
154                 RETURN(dchild);
155
156         res_id[0] = dchild->d_inode->i_ino;
157         rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
158                                    res_id, LDLM_PLAIN, NULL, 0, lock_mode,
159                                    &flags, ldlm_completion_ast,
160                                    mds_blocking_ast, NULL, 0, lockh);
161         if (rc != ELDLM_OK) {
162                 l_dput(dchild);
163                 up(&dir->d_inode->i_sem);
164                 RETURN(ERR_PTR(-ENOLCK)); /* XXX translate ldlm code */
165         }
166
167         RETURN(dchild);
168 }
169
170 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
171                                      struct vfsmount **mnt, int lock_mode,
172                                      struct lustre_handle *lockh)
173 {
174         struct mds_obd *mds = &obd->u.mds;
175         struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
176         int flags, rc;
177         __u64 res_id[3] = {0};
178         ENTRY;
179
180         if (IS_ERR(de))
181                 RETURN(de);
182
183         res_id[0] = de->d_inode->i_ino;
184         rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
185                                    res_id, LDLM_PLAIN, NULL, 0, lock_mode,
186                                    &flags, ldlm_completion_ast,
187                                    mds_blocking_ast, NULL, 0, lockh);
188         if (rc != ELDLM_OK) {
189                 l_dput(de);
190                 retval = ERR_PTR(-ENOLCK); /* XXX translate ldlm code */
191         }
192
193         RETURN(retval);
194 }
195
196 /* Look up an entry by inode number. */
197 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
198                               struct vfsmount **mnt)
199 {
200         /* stolen from NFS */
201         struct super_block *sb = mds->mds_sb;
202         unsigned long ino = fid->id;
203         __u32 generation = fid->generation;
204         struct inode *inode;
205         struct list_head *lp;
206         struct dentry *result;
207
208         if (ino == 0)
209                 RETURN(ERR_PTR(-ESTALE));
210
211         inode = iget(sb, ino);
212         if (inode == NULL)
213                 RETURN(ERR_PTR(-ENOMEM));
214
215         CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
216
217         if (is_bad_inode(inode) ||
218             (generation && inode->i_generation != generation)) {
219                 /* we didn't find the right inode.. */
220                 CERROR("bad inode %lu, link: %d ct: %d or version  %u/%u\n",
221                        inode->i_ino, inode->i_nlink,
222                        atomic_read(&inode->i_count), inode->i_generation,
223                        generation);
224                 LBUG();
225                 iput(inode);
226                 RETURN(ERR_PTR(-ESTALE));
227         }
228
229         /* now to find a dentry. If possible, get a well-connected one */
230         if (mnt)
231                 *mnt = mds->mds_vfsmnt;
232         spin_lock(&dcache_lock);
233         list_for_each(lp, &inode->i_dentry) {
234                 result = list_entry(lp, struct dentry, d_alias);
235                 if (!(result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
236                         dget_locked(result);
237                         result->d_vfs_flags |= DCACHE_REFERENCED;
238                         spin_unlock(&dcache_lock);
239                         iput(inode);
240                         if (mnt)
241                                 mntget(*mnt);
242                         return result;
243                 }
244         }
245         spin_unlock(&dcache_lock);
246         result = d_alloc_root(inode);
247         if (result == NULL) {
248                 iput(inode);
249                 return ERR_PTR(-ENOMEM);
250         }
251         if (mnt)
252                 mntget(*mnt);
253         result->d_flags |= DCACHE_NFSD_DISCONNECTED;
254         return result;
255 }
256
257 /* Establish a connection to the MDS.
258  *
259  * This will set up an export structure for the client to hold state data
260  * about that client, like open files, the last operation number it did
261  * on the server, etc.
262  */
263 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
264                        char *cluuid)
265 {
266         struct obd_export *exp;
267         struct mds_client_data *mcd;
268         struct list_head *p;
269         int rc;
270         ENTRY;
271
272         if (!conn || !obd || !cluuid)
273                 RETURN(-EINVAL);
274
275         MOD_INC_USE_COUNT;
276
277         list_for_each(p, &obd->obd_exports) {
278                 exp = list_entry(p, struct obd_export, exp_chain);
279                 mcd = exp->exp_mds_data.med_mcd;
280                 if (!memcmp(cluuid, mcd->mcd_uuid, sizeof(mcd->mcd_uuid))) {
281                         CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
282                                cluuid, exp);
283                         LASSERT(exp->exp_obd == obd);
284
285                         exp->exp_rconnh.addr = conn->addr;
286                         exp->exp_rconnh.cookie = conn->cookie;
287                         conn->addr = (__u64) (unsigned long)exp;
288                         conn->cookie = exp->exp_cookie;
289                         CDEBUG(D_IOCTL,"connect: addr %Lx cookie %Lx\n",
290                                (long long)conn->addr, (long long)conn->cookie);
291                         RETURN(0);
292                 }
293         }
294         rc = class_connect(conn, obd, cluuid);
295         if (rc)
296                 GOTO(out_dec, rc);
297         exp = class_conn2export(conn);
298         LASSERT(exp);
299
300         OBD_ALLOC(mcd, sizeof(*mcd));
301         if (!mcd) {
302                 CERROR("mds: out of memory for client data\n");
303                 GOTO(out_export, rc = -ENOMEM);
304         }
305         memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
306         exp->exp_mds_data.med_mcd = mcd;
307         rc = mds_client_add(&exp->exp_mds_data, -1);
308         if (rc)
309                 GOTO(out_mdc, rc);
310
311         RETURN(0);
312
313 out_mdc:
314         OBD_FREE(mcd, sizeof(*mcd));
315 out_export:
316         class_disconnect(conn);
317 out_dec:
318         MOD_DEC_USE_COUNT;
319
320         return rc;
321 }
322
323 static int mds_disconnect(struct lustre_handle *conn)
324 {
325         struct obd_export *exp;
326         int rc;
327
328         exp = class_conn2export(conn);
329         if (!exp)
330                 RETURN(-EINVAL);
331
332         rc = mds_client_free(&exp->exp_mds_data);
333         if (rc)
334                 CERROR("error freeing client data: rc = %d\n", rc);
335
336         rc = class_disconnect(conn);
337         if (!rc)
338                 MOD_DEC_USE_COUNT;
339
340         return rc;
341 }
342
343 static int mds_getstatus(struct ptlrpc_request *req)
344 {
345         struct mds_obd *mds = mds_req2mds(req);
346         struct mds_body *body;
347         struct mds_export_data *med = &req->rq_export->exp_mds_data;
348         int rc, size = sizeof(*body);
349         ENTRY;
350
351         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
352         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
353                 CERROR("mds: out of memory for message: size=%d\n", size);
354                 req->rq_status = -ENOMEM;
355                 RETURN(0);
356         }
357
358         body = lustre_msg_buf(req->rq_reqmsg, 0);
359         mds_unpack_body(body);
360
361         /* Anything we need to do here with the client's trans no or so? */
362         body = lustre_msg_buf(req->rq_repmsg, 0);
363         memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
364
365         LASSERT(med->med_mcd);
366
367         /* mcd_last_xid is is stored in little endian on the disk and
368            mds_pack_rep_body converts it to network order */
369         req->rq_repmsg->last_xid = le32_to_cpu(med->med_mcd->mcd_last_xid);
370         mds_pack_rep_body(req);
371         RETURN(0);
372 }
373
374 static int mds_getlovinfo(struct ptlrpc_request *req)
375 {
376         struct mds_obd *mds = mds_req2mds(req);
377         struct mds_status_req *streq;
378         struct lov_desc *desc;
379         int tgt_count;
380         int rc, size[2] = {sizeof(*desc)};
381         ENTRY;
382
383         streq = lustre_msg_buf(req->rq_reqmsg, 0);
384         streq->flags = NTOH__u32(streq->flags);
385         streq->repbuf = NTOH__u32(streq->repbuf);
386         size[1] = streq->repbuf;
387
388         rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
389         if (rc) {
390                 CERROR("mds: out of memory for message: size=%d\n", size[1]);
391                 req->rq_status = -ENOMEM;
392                 RETURN(0);
393         }
394
395         desc = lustre_msg_buf(req->rq_repmsg, 0);
396         rc = mds_get_lovdesc(req->rq_obd, desc);
397         if (rc != 0 ) {
398                 CERROR("mds_get_lovdesc error %d", rc);
399                 req->rq_status = rc;
400                 RETURN(0);
401         }
402
403         tgt_count = NTOH__u32(desc->ld_tgt_count);
404         if (tgt_count * sizeof(uuid_t) > streq->repbuf) {
405                 CERROR("too many targets, enlarge client buffers\n");
406                 req->rq_status = -ENOSPC;
407                 RETURN(0);
408         }
409
410         mds->mds_max_mdsize = sizeof(struct lov_mds_md) + 
411                 tgt_count * sizeof(struct lov_object_id);
412         rc = mds_get_lovtgts(req->rq_obd, tgt_count,
413                              lustre_msg_buf(req->rq_repmsg, 1));
414         if (rc) {
415                 CERROR("get_lovtgts error %d\n", rc);
416                 req->rq_status = rc;
417                 RETURN(0);
418         }
419         RETURN(0);
420 }
421
422 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
423                      void *data, __u32 data_len)
424 {
425         int do_ast;
426         ENTRY;
427
428         l_lock(&lock->l_resource->lr_namespace->ns_lock);
429         lock->l_flags |= LDLM_FL_CBPENDING;
430         do_ast = (!lock->l_readers && !lock->l_writers);
431         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
432
433         if (do_ast) {
434                 struct lustre_handle lockh;
435                 int rc;
436
437                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
438                 ldlm_lock2handle(lock, &lockh);
439                 rc = ldlm_cli_cancel(&lockh);
440                 if (rc < 0)
441                         CERROR("ldlm_cli_cancel: %d\n", rc);
442         } else
443                 LDLM_DEBUG(lock, "Lock still has references, will be"
444                            "cancelled later");
445         RETURN(0);
446 }
447
448 static int mds_getattr_internal(struct mds_obd *mds, struct dentry *dentry,
449                                 struct ptlrpc_request *req,
450                                 int request_off, int reply_off)
451 {
452         struct mds_body *request_body, *body;
453         struct inode *inode = dentry->d_inode;
454         int rc;
455         ENTRY;
456
457         if (inode == NULL)
458                 RETURN(-ENOENT);
459
460         /* Did the client request the link name? */
461         request_body = lustre_msg_buf(req->rq_reqmsg, request_off);
462         body = lustre_msg_buf(req->rq_repmsg, reply_off);
463         if ((body->valid & OBD_MD_LINKNAME) && S_ISLNK(inode->i_mode)) {
464                 char *tmp = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
465
466                 rc = inode->i_op->readlink(dentry, tmp, req->rq_repmsg->
467                                            buflens[reply_off + 1]);
468                 if (rc < 0) {
469                         CERROR("readlink failed: %d\n", rc);
470                         RETURN(rc);
471                 }
472
473                 body->valid |= OBD_MD_LINKNAME;
474         }
475
476         mds_pack_inode2fid(&body->fid1, inode);
477         mds_pack_inode2body(body, inode);
478         if (S_ISREG(inode->i_mode)) {
479                 struct lov_mds_md *md;
480
481                 md = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
482                 md->lmd_easize = mds->mds_max_mdsize;
483                 rc = mds_fs_get_md(mds, inode, md);
484
485                 if (rc < 0) {
486                         if (rc == -ENODATA)
487                                 RETURN(0);
488                         CERROR("mds_fs_get_md failed: %d\n", rc);
489                         RETURN(rc);
490                 }
491                 body->valid |= OBD_MD_FLEASIZE;
492         }
493         RETURN(0);
494 }
495
496 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
497 {
498         struct mds_obd *mds = mds_req2mds(req);
499         struct obd_device *obd = req->rq_export->exp_obd;
500         struct obd_run_ctxt saved;
501         struct mds_body *body;
502         struct dentry *de = NULL, *dchild = NULL;
503         struct inode *dir;
504         struct lustre_handle lockh;
505         char *name;
506         int namelen, flags, lock_mode, rc = 0, old_offset = offset;
507         __u64 res_id[3] = {0, 0, 0};
508         ENTRY;
509
510         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
511
512         if (req->rq_reqmsg->bufcount <= offset + 1) {
513                 LBUG();
514                 GOTO(out_pre_de, rc = -EINVAL);
515         }
516
517         body = lustre_msg_buf(req->rq_reqmsg, offset);
518         name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
519         namelen = req->rq_reqmsg->buflens[offset + 1];
520         /* requests were at offset 2, replies go back at 1 */
521         if (offset)
522                 offset = 1;
523
524         push_ctxt(&saved, &mds->mds_ctxt);
525         de = mds_fid2dentry(mds, &body->fid1, NULL);
526         if (IS_ERR(de)) {
527                 LBUG();
528                 GOTO(out_pre_de, rc = -ESTALE);
529         }
530
531         dir = de->d_inode;
532         CDEBUG(D_INODE, "parent ino %ld, name %*s\n", dir->i_ino,namelen,name);
533
534         lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
535         res_id[0] = dir->i_ino;
536
537         rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
538                              NULL, 0, lock_mode, &lockh);
539         if (rc == 0) {
540                 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
541                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
542                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
543                                       &flags, ldlm_completion_ast,
544                                       mds_blocking_ast, NULL, 0, &lockh);
545                 if (rc != ELDLM_OK) {
546                         CERROR("lock enqueue: err: %d\n", rc);
547                         GOTO(out_create_de, rc = -EIO);
548                 }
549         }
550         ldlm_lock_dump((void *)(unsigned long)lockh.addr);
551
552         down(&dir->i_sem);
553         dchild = lookup_one_len(name, de, namelen - 1);
554         if (IS_ERR(dchild)) {
555                 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
556                 up(&dir->i_sem);
557                 LBUG();
558                 GOTO(out_create_dchild, rc = -ESTALE);
559         }
560
561         rc = mds_getattr_internal(mds, dchild, req, old_offset, offset);
562
563         EXIT;
564 out_create_dchild:
565         l_dput(dchild);
566         up(&dir->i_sem);
567         ldlm_lock_decref(&lockh, lock_mode);
568 out_create_de:
569         l_dput(de);
570 out_pre_de:
571         req->rq_status = rc;
572         pop_ctxt(&saved);
573         return 0;
574 }
575
576 static int mds_getattr(int offset, struct ptlrpc_request *req)
577 {
578         struct mds_obd *mds = mds_req2mds(req);
579         struct obd_run_ctxt saved;
580         struct dentry *de;
581         struct inode *inode;
582         struct mds_body *body;
583         int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
584         ENTRY;
585
586         body = lustre_msg_buf(req->rq_reqmsg, offset);
587         push_ctxt(&saved, &mds->mds_ctxt);
588         de = mds_fid2dentry(mds, &body->fid1, NULL);
589         if (IS_ERR(de)) {
590                 req->rq_status = -ENOENT;
591                 GOTO(out_pop, rc = -ENOENT);
592         }
593
594         inode = de->d_inode;
595         if (S_ISREG(body->fid1.f_type)) {
596                 bufcount = 2;
597                 size[1] = mds->mds_max_mdsize;
598         } else if (body->valid & OBD_MD_LINKNAME) {
599                 bufcount = 2;
600                 size[1] = inode->i_size;
601         }
602
603         rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
604                              &req->rq_repmsg);
605         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
606                 CERROR("out of memory or FAIL_MDS_GETATTR_PACK\n");
607                 req->rq_status = rc;
608                 GOTO(out, rc = 0);
609         }
610
611         req->rq_status = mds_getattr_internal(mds, de, req, offset, 0);
612
613 out:
614         l_dput(de);
615 out_pop:
616         pop_ctxt(&saved);
617         RETURN(rc);
618 }
619
620 static int mds_statfs(struct ptlrpc_request *req)
621 {
622         struct mds_obd *mds = mds_req2mds(req);
623         struct obd_statfs *osfs;
624         struct statfs sfs;
625         int rc, size = sizeof(*osfs);
626         ENTRY;
627
628         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
629                              &req->rq_repmsg);
630         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
631                 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
632                 GOTO(out, rc);
633         }
634
635         rc = vfs_statfs(mds->mds_sb, &sfs);
636         if (rc) {
637                 CERROR("mds: statfs failed: rc %d\n", rc);
638                 GOTO(out, rc);
639         }
640         osfs = lustre_msg_buf(req->rq_repmsg, 0);
641         memset(osfs, 0, size);
642         obd_statfs_pack(osfs, &sfs);
643
644 out:
645         req->rq_status = rc;
646         RETURN(0);
647 }
648
649 static int mds_open(struct ptlrpc_request *req)
650 {
651         struct dentry *de;
652         struct mds_body *body;
653         struct file *file;
654         struct vfsmount *mnt;
655         struct mds_obd *mds = mds_req2mds(req);
656         struct mds_export_data *med;
657         __u32 flags;
658         struct list_head *tmp;
659         struct mds_file_data *mfd;
660         int rc, size = sizeof(*body);
661         ENTRY;
662
663         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
664         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
665                 CERROR("mds: out of memory\n");
666                 req->rq_status = -ENOMEM;
667                 RETURN(0);
668         }
669
670         body = lustre_msg_buf(req->rq_reqmsg, 0);
671
672         /* was this animal open already? */
673         /* XXX we should only check on re-open, or do a refcount... */
674         med = &req->rq_export->exp_mds_data;
675         list_for_each(tmp, &med->med_open_head) {
676                 struct mds_file_data *fd;
677                 fd = list_entry(tmp, struct mds_file_data, mfd_list);
678                 if (body->extra == fd->mfd_clientfd &&
679                     body->fid1.id == fd->mfd_file->f_dentry->d_inode->i_ino) {
680                         CERROR("Re opening %Ld\n", body->fid1.id);
681                         RETURN(0);
682                 }
683         }
684
685         OBD_ALLOC(mfd, sizeof(*mfd));
686         if (!mfd) {
687                 CERROR("mds: out of memory\n");
688                 req->rq_status = -ENOMEM;
689                 RETURN(0);
690         }
691
692         de = mds_fid2dentry(mds, &body->fid1, &mnt);
693         if (IS_ERR(de)) {
694                 req->rq_status = -ENOENT;
695                 RETURN(0);
696         }
697
698         /* check if this inode has seen a delayed object creation */
699         if (req->rq_reqmsg->bufcount > 1) {
700                 void *handle;
701                 struct lov_mds_md *md;
702                 struct inode *inode = de->d_inode;
703                 int rc;
704
705                 md = lustre_msg_buf(req->rq_reqmsg, 1);
706
707                 handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
708                 if (!handle) {
709                         req->rq_status = -ENOMEM;
710                         RETURN(0);
711                 }
712
713                 /* XXX error handling */
714                 rc = mds_fs_set_md(mds, inode, handle, md);
715                 if (!rc) {
716                         struct obd_run_ctxt saved;
717                         push_ctxt(&saved, &mds->mds_ctxt);
718                         rc = mds_update_last_rcvd(mds, handle, req);
719                         pop_ctxt(&saved);
720                 } else {
721                         req->rq_status = rc;
722                         RETURN(0);
723                 }
724                 /* FIXME: need to return last_rcvd, last_committed */
725
726                 /* FIXME: keep rc intact */
727                 rc = mds_fs_commit(mds, de->d_inode, handle);
728                 if (rc) {
729                         req->rq_status = rc;
730                         RETURN(0);
731                 }
732         }
733
734         flags = body->flags;
735         file = dentry_open(de, mnt, flags & ~O_DIRECT);
736         if (!file || IS_ERR(file)) {
737                 req->rq_status = -EINVAL;
738                 OBD_FREE(mfd, sizeof(*mfd));
739                 RETURN(0);
740         }
741
742         file->private_data = mfd;
743         mfd->mfd_file = file;
744         mfd->mfd_clientfd = body->extra;
745         list_add(&mfd->mfd_list, &med->med_open_head);
746
747         body = lustre_msg_buf(req->rq_repmsg, 0);
748         /* FIXME: need to have cookies involved here */
749         body->extra = (__u64) (unsigned long)file;
750         RETURN(0);
751 }
752
753 static int mds_close(struct ptlrpc_request *req)
754 {
755         struct dentry *de;
756         struct mds_body *body;
757         struct file *file;
758         struct mds_obd *mds = mds_req2mds(req);
759         struct vfsmount *mnt;
760         struct mds_file_data *mfd;
761         int rc;
762         ENTRY;
763
764         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
765         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
766                 CERROR("mds: out of memory\n");
767                 req->rq_status = -ENOMEM;
768                 RETURN(0);
769         }
770
771         body = lustre_msg_buf(req->rq_reqmsg, 0);
772         de = mds_fid2dentry(mds, &body->fid1, &mnt);
773         if (IS_ERR(de)) {
774                 req->rq_status = -ENOENT;
775                 RETURN(0);
776         }
777
778         /* FIXME: need to have cookies involved here */
779         file = (struct file *)(unsigned long)body->extra;
780         if (!file->f_dentry)
781                 LBUG();
782         mfd = (struct mds_file_data *)file->private_data;
783         list_del(&mfd->mfd_list);
784         OBD_FREE(mfd, sizeof(*mfd));
785
786         req->rq_status = filp_close(file, 0);
787         l_dput(de);
788         mntput(mnt);
789
790         RETURN(0);
791 }
792
793 static int mds_readpage(struct ptlrpc_request *req)
794 {
795         struct mds_obd *mds = mds_req2mds(req);
796         struct vfsmount *mnt;
797         struct dentry *de;
798         struct file *file;
799         struct mds_body *body;
800         struct obd_run_ctxt saved;
801         int rc, size = sizeof(*body);
802         ENTRY;
803
804         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
805         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
806                 CERROR("mds: out of memory\n");
807                 GOTO(out, rc = -ENOMEM);
808         }
809
810         body = lustre_msg_buf(req->rq_reqmsg, 0);
811         push_ctxt(&saved, &mds->mds_ctxt);
812         de = mds_fid2dentry(mds, &body->fid1, &mnt);
813         if (IS_ERR(de))
814                 GOTO(out_pop, rc = PTR_ERR(de));
815
816         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
817
818         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
819         /* note: in case of an error, dentry_open puts dentry */
820         if (IS_ERR(file))
821                 GOTO(out_pop, rc = PTR_ERR(file));
822
823         /* to make this asynchronous make sure that the handling function
824            doesn't send a reply when this function completes. Instead a
825            callback function would send the reply */
826         rc = mds_sendpage(req, file, body->size);
827
828         filp_close(file, 0);
829 out_pop:
830         pop_ctxt(&saved);
831 out:
832         req->rq_status = rc;
833         RETURN(0);
834 }
835
836 int mds_reint(int offset, struct ptlrpc_request *req)
837 {
838         int rc;
839         struct mds_update_record rec;
840
841         rc = mds_update_unpack(req, offset, &rec);
842         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
843                 CERROR("invalid record\n");
844                 req->rq_status = -EINVAL;
845                 RETURN(0);
846         }
847         /* rc will be used to interrupt a for loop over multiple records */
848         rc = mds_reint_rec(&rec, offset, req);
849         return rc;
850 }
851
852 int mds_handle(struct ptlrpc_request *req)
853 {
854         int rc;
855         ENTRY;
856
857         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
858         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
859                 CERROR("lustre_mds: Invalid request\n");
860                 GOTO(out, rc);
861         }
862
863         if (req->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
864                 CERROR("lustre_mds: wrong packet type sent %d\n",
865                        req->rq_reqmsg->type);
866                 GOTO(out, rc = -EINVAL);
867         }
868
869         if (req->rq_reqmsg->opc != MDS_CONNECT && req->rq_export == NULL)
870                 GOTO(out, rc = -ENOTCONN);
871
872         if (strcmp(req->rq_obd->obd_type->typ_name, "mds") != 0)
873                 GOTO(out, rc = -EINVAL);
874
875         switch (req->rq_reqmsg->opc) {
876         case MDS_CONNECT:
877                 CDEBUG(D_INODE, "connect\n");
878                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
879                 rc = target_handle_connect(req);
880                 break;
881
882         case MDS_DISCONNECT:
883                 CDEBUG(D_INODE, "disconnect\n");
884                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
885                 rc = target_handle_disconnect(req);
886                 goto out;
887
888         case MDS_GETSTATUS:
889                 CDEBUG(D_INODE, "getstatus\n");
890                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
891                 rc = mds_getstatus(req);
892                 break;
893
894         case MDS_GETLOVINFO:
895                 CDEBUG(D_INODE, "getlovinfo\n");
896                 rc = mds_getlovinfo(req);
897                 break;
898
899         case MDS_GETATTR:
900                 CDEBUG(D_INODE, "getattr\n");
901                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
902                 rc = mds_getattr(0, req);
903                 break;
904
905         case MDS_STATFS:
906                 CDEBUG(D_INODE, "statfs\n");
907                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
908                 rc = mds_statfs(req);
909                 break;
910
911         case MDS_READPAGE:
912                 CDEBUG(D_INODE, "readpage\n");
913                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
914                 rc = mds_readpage(req);
915
916                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
917                         return 0;
918                 break;
919
920         case MDS_REINT: {
921                 int size = sizeof(struct mds_body);
922                 CDEBUG(D_INODE, "reint\n");
923                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
924
925                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
926                                      &req->rq_repmsg);
927                 if (rc) {
928                         req->rq_status = rc;
929                         break;
930                 }
931                 rc = mds_reint(0, req);
932                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
933                 break;
934                 }
935
936         case MDS_OPEN:
937                 CDEBUG(D_INODE, "open\n");
938                 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
939                 rc = mds_open(req);
940                 break;
941
942         case MDS_CLOSE:
943                 CDEBUG(D_INODE, "close\n");
944                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
945                 rc = mds_close(req);
946                 break;
947
948         case LDLM_ENQUEUE:
949                 CDEBUG(D_INODE, "enqueue\n");
950                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
951                 rc = ldlm_handle_enqueue(req);
952                 if (rc)
953                         break;
954                 RETURN(0);
955
956         case LDLM_CONVERT:
957                 CDEBUG(D_INODE, "convert\n");
958                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
959                 rc = ldlm_handle_convert(req);
960                 if (rc)
961                         break;
962                 RETURN(0);
963
964         case LDLM_CANCEL:
965                 CDEBUG(D_INODE, "cancel\n");
966                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
967                 rc = ldlm_handle_cancel(req);
968                 if (rc)
969                         break;
970                 RETURN(0);
971         case LDLM_BL_CALLBACK:
972         case LDLM_CP_CALLBACK:
973                 CDEBUG(D_INODE, "callback\n");
974                 CERROR("callbacks should not happen on MDS\n");
975                 LBUG();
976                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
977                 break;
978         default:
979                 rc = ptlrpc_error(req->rq_svc, req);
980                 RETURN(rc);
981         }
982
983         EXIT;
984
985         if (!rc) {
986                 struct mds_obd *mds = mds_req2mds(req);
987                 req->rq_repmsg->last_xid = HTON__u64(mds->mds_last_rcvd);
988                 req->rq_repmsg->last_committed =
989                         HTON__u64(mds->mds_last_committed);
990                 CDEBUG(D_INFO, "last_rcvd %Lu, last_committed %Lu, xid %d\n",
991                        (unsigned long long)mds->mds_last_rcvd,
992                        (unsigned long long)mds->mds_last_committed,
993                        cpu_to_le32(req->rq_xid));
994         }
995  out:
996         /* Still not 100% sure whether we should reply with the server
997          * last_rcvd or that of this client.  I'm not sure it even makes
998          * a difference on a per-client basis, because last_rcvd is global
999          * and we are not supposed to allow transactions while in recovery.
1000          */
1001         if (rc) {
1002                 CERROR("mds: processing error %d\n", rc);
1003                 ptlrpc_error(req->rq_svc, req);
1004         } else {
1005                 CDEBUG(D_NET, "sending reply\n");
1006                 ptlrpc_reply(req->rq_svc, req);
1007         }
1008         return 0;
1009 }
1010
1011 /* Update the server data on disk.  This stores the new mount_count and
1012  * also the last_rcvd value to disk.  If we don't have a clean shutdown,
1013  * then the server last_rcvd value may be less than that of the clients.
1014  * This will alert us that we may need to do client recovery.
1015  *
1016  * Assumes we are already in the server filesystem context.
1017  */
1018 static
1019 int mds_update_server_data(struct mds_obd *mds)
1020 {
1021         struct mds_server_data *msd = mds->mds_server_data;
1022         struct file *filp = mds->mds_rcvd_filp;
1023         loff_t off = 0;
1024         int rc;
1025
1026         msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
1027         msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
1028
1029         CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
1030                (unsigned long long)mds->mds_mount_count,
1031                (unsigned long long)mds->mds_last_rcvd);
1032         rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
1033         if (rc != sizeof(*msd)) {
1034                 CERROR("error writing MDS server data: rc = %d\n", rc);
1035                 if (rc > 0)
1036                         RETURN(-EIO);
1037                 RETURN(rc);
1038         }
1039         rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
1040         if (rc)
1041                 CERROR("error flushing MDS server data: rc = %d\n", rc);
1042
1043         return 0;
1044 }
1045
1046 /* Do recovery actions for the MDS */
1047 static int mds_recover(struct obd_device *obddev)
1048 {
1049         struct mds_obd *mds = &obddev->u.mds;
1050         struct obd_run_ctxt saved;
1051         int rc;
1052
1053         /* This happens at the end when recovery is complete */
1054         ++mds->mds_mount_count;
1055         push_ctxt(&saved, &mds->mds_ctxt);
1056         rc = mds_update_server_data(mds);
1057         pop_ctxt(&saved);
1058
1059         return rc;
1060 }
1061
1062 #define MDS_NUM_THREADS 8
1063 /* mount the file system (secretly) */
1064 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
1065 {
1066         int i;
1067         struct obd_ioctl_data* data = buf;
1068         struct mds_obd *mds = &obddev->u.mds;
1069         struct vfsmount *mnt;
1070         int rc = 0;
1071         ENTRY;
1072
1073         MOD_INC_USE_COUNT;
1074 #ifdef CONFIG_DEV_RDONLY
1075         dev_clear_rdonly(2);
1076 #endif
1077         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
1078                 GOTO(err_dec, rc = -EINVAL);
1079
1080         mds->mds_fstype = strdup(data->ioc_inlbuf2);
1081
1082         mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
1083         if (IS_ERR(mnt)) {
1084                 rc = PTR_ERR(mnt);
1085                 CERROR("do_kern_mount failed: rc = %d\n", rc);
1086                 GOTO(err_kfree, rc);
1087         }
1088
1089         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
1090         if (!mds->mds_sb)
1091                 GOTO(err_put, rc = -ENODEV);
1092
1093         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1094         rc = mds_fs_setup(obddev, mnt);
1095         if (rc) {
1096                 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1097                 GOTO(err_put, rc);
1098         }
1099
1100         mds->mds_service = ptlrpc_init_svc(64 * 1024, MDS_REQUEST_PORTAL,
1101                                            MDC_REPLY_PORTAL, "self",mds_handle, 
1102                                            "mds");
1103         if (!mds->mds_service) {
1104                 CERROR("failed to start service\n");
1105                 GOTO(err_fs, rc = -EINVAL);
1106         }
1107
1108         obddev->obd_namespace =
1109                 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1110         if (obddev->obd_namespace == NULL) {
1111                 mds_cleanup(obddev);
1112                 GOTO(err_svc, rc = -ENOMEM);
1113         }
1114
1115         for (i = 0; i < MDS_NUM_THREADS; i++) {
1116                 char name[32];
1117                 sprintf(name, "lustre_MDS_%02d", i);
1118                 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1119                 if (rc) {
1120                         CERROR("cannot start MDS thread #%d: rc %d\n", i, rc);
1121                         GOTO(err_thread, rc);
1122                 }
1123         }
1124
1125         rc = mds_recover(obddev);
1126         if (rc)
1127                 GOTO(err_thread, rc);
1128
1129         RETURN(0);
1130
1131 err_thread:
1132         ptlrpc_stop_all_threads(mds->mds_service);
1133 err_svc:
1134         ptlrpc_unregister_service(mds->mds_service);
1135 err_fs:
1136         mds_fs_cleanup(obddev);
1137 err_put:
1138         unlock_kernel();
1139         mntput(mds->mds_vfsmnt);
1140         mds->mds_sb = 0;
1141         lock_kernel();
1142 err_kfree:
1143         kfree(mds->mds_fstype);
1144 err_dec:
1145         MOD_DEC_USE_COUNT;
1146         return rc;
1147 }
1148
1149 static int mds_cleanup(struct obd_device *obddev)
1150 {
1151         struct super_block *sb;
1152         struct mds_obd *mds = &obddev->u.mds;
1153         struct obd_run_ctxt saved;
1154         ENTRY;
1155
1156         ptlrpc_stop_all_threads(mds->mds_service);
1157         ptlrpc_unregister_service(mds->mds_service);
1158
1159         sb = mds->mds_sb;
1160         if (!mds->mds_sb)
1161                 RETURN(0);
1162
1163         push_ctxt(&saved, &mds->mds_ctxt);
1164         mds_update_server_data(mds);
1165
1166         if (mds->mds_rcvd_filp) {
1167                 int rc = filp_close(mds->mds_rcvd_filp, 0);
1168                 mds->mds_rcvd_filp = NULL;
1169
1170                 if (rc)
1171                         CERROR("last_rcvd file won't close, rc=%d\n", rc);
1172         }
1173         pop_ctxt(&saved);
1174
1175         unlock_kernel();
1176         mntput(mds->mds_vfsmnt);
1177         mds->mds_sb = 0;
1178         kfree(mds->mds_fstype);
1179
1180         ldlm_namespace_free(obddev->obd_namespace);
1181
1182         lock_kernel();
1183 #ifdef CONFIG_DEV_RDONLY
1184         dev_clear_rdonly(2);
1185 #endif
1186         mds_fs_cleanup(obddev);
1187
1188         MOD_DEC_USE_COUNT;
1189         RETURN(0);
1190 }
1191
1192 static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
1193                               ldlm_mode_t mode, void *data)
1194 {
1195         struct ptlrpc_request *req = req_cookie;
1196         int rc = 0;
1197         ENTRY;
1198
1199         if (!req_cookie)
1200                 RETURN(0);
1201
1202         if (req->rq_reqmsg->bufcount > 1) {
1203                 /* an intent needs to be considered */
1204                 struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
1205                 struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
1206                 struct mds_body *mds_rep;
1207                 struct ldlm_reply *rep;
1208                 __u64 new_resid[3] = {0, 0, 0}, old_res;
1209                 int rc, size[3] = {sizeof(struct ldlm_reply),
1210                                                   sizeof(struct mds_body),
1211                                                   mds->mds_max_mdsize};
1212
1213                 it->opc = NTOH__u64(it->opc);
1214
1215                 LDLM_DEBUG(lock, "intent policy, opc: %s",
1216                            ldlm_it2str(it->opc));
1217
1218                 rc = lustre_pack_msg(3, size, NULL, &req->rq_replen,
1219                                      &req->rq_repmsg);
1220                 if (rc) {
1221                         rc = req->rq_status = -ENOMEM;
1222                         RETURN(rc);
1223                 }
1224
1225                 rep = lustre_msg_buf(req->rq_repmsg, 0);
1226                 rep->lock_policy_res1 = 1;
1227
1228                 /* execute policy */
1229                 switch ((long)it->opc) {
1230                 case IT_CREAT|IT_OPEN:
1231                         rc = mds_reint(2, req);
1232                         if (rc || (req->rq_status != 0 &&
1233                                    req->rq_status != -EEXIST)) {
1234                                 rep->lock_policy_res2 = req->rq_status;
1235                                 RETURN(ELDLM_LOCK_ABORTED);
1236                         }
1237                         break;
1238                 case IT_CREAT:
1239                 case IT_LINK:
1240                 case IT_MKDIR:
1241                 case IT_MKNOD:
1242                 case IT_RENAME2:
1243                 case IT_RMDIR:
1244                 case IT_SYMLINK:
1245                 case IT_UNLINK:
1246                         rc = mds_reint(2, req);
1247                         if (rc || (req->rq_status != 0 &&
1248                                    req->rq_status != -EISDIR &&
1249                                    req->rq_status != -ENOTDIR)) {
1250                                 rep->lock_policy_res2 = req->rq_status;
1251                                 RETURN(ELDLM_LOCK_ABORTED);
1252                         }
1253                         break;
1254                 case IT_GETATTR:
1255                 case IT_LOOKUP:
1256                 case IT_OPEN:
1257                 case IT_READDIR:
1258                 case IT_READLINK:
1259                 case IT_RENAME:
1260                 case IT_SETATTR:
1261                         rc = mds_getattr_name(2, req);
1262                         /* FIXME: we need to sit down and decide on who should
1263                          * set req->rq_status, who should return negative and
1264                          * positive return values, and what they all mean. */
1265                         if (rc || req->rq_status != 0) {
1266                                 rep->lock_policy_res2 = req->rq_status;
1267                                 RETURN(ELDLM_LOCK_ABORTED);
1268                         }
1269                         break;
1270                 case IT_READDIR|IT_OPEN:
1271                         LBUG();
1272                         break;
1273                 default:
1274                         CERROR("Unhandled intent\n");
1275                         LBUG();
1276                 }
1277
1278                 if (it->opc == IT_UNLINK || it->opc == IT_RMDIR ||
1279                     it->opc == IT_RENAME || it->opc == IT_RENAME2)
1280                         RETURN(ELDLM_LOCK_ABORTED);
1281
1282                 rep->lock_policy_res2 = req->rq_status;
1283                 mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
1284
1285                 /* If the client is about to open a file that doesn't have an MD
1286                  * stripe record, it's going to need a write lock. */
1287                 if (it->opc & IT_OPEN) {
1288                         struct lov_mds_md *md =
1289                                 lustre_msg_buf(req->rq_repmsg, 2);
1290                         if (md->lmd_easize == 0) {
1291                                 LDLM_DEBUG(lock, "open with no EA; returning PW"
1292                                            " lock");
1293                                 lock->l_req_mode = LCK_PW;
1294                         }
1295                 }
1296
1297                 /* Give the client a lock on the child object, instead of the
1298                  * parent that it requested. */
1299                 new_resid[0] = NTOH__u32(mds_rep->ino);
1300                 if (new_resid[0] == 0)
1301                         LBUG();
1302                 old_res = lock->l_resource->lr_name[0];
1303
1304                 ldlm_lock_change_resource(lock, new_resid);
1305                 if (lock->l_resource == NULL) {
1306                         LBUG();
1307                         RETURN(-ENOMEM);
1308                 }
1309                 LDLM_DEBUG(lock, "intent policy, old res %ld",
1310                            (long)old_res);
1311                 RETURN(ELDLM_LOCK_CHANGED);
1312         } else {
1313                 int size = sizeof(struct ldlm_reply);
1314                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1315                                      &req->rq_repmsg);
1316                 if (rc) {
1317                         LBUG();
1318                         RETURN(-ENOMEM);
1319                 }
1320         }
1321         RETURN(rc);
1322 }
1323
1324
1325 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1326                          int len, void *karg, void *uarg);
1327
1328 /* use obd ops to offer management infrastructure */
1329 static struct obd_ops mds_obd_ops = {
1330         o_connect:     mds_connect,
1331         o_disconnect:  mds_disconnect,
1332         o_setup:       mds_setup,
1333         o_cleanup:     mds_cleanup,
1334         o_iocontrol:   mds_iocontrol
1335 };
1336
1337 static int __init mds_init(void)
1338 {
1339         class_register_type(&mds_obd_ops, LUSTRE_MDS_NAME);
1340         ldlm_register_intent(ldlm_intent_policy);
1341         return 0;
1342 }
1343
1344 static void __exit mds_exit(void)
1345 {
1346         ldlm_unregister_intent();
1347         class_unregister_type(LUSTRE_MDS_NAME);
1348 }
1349
1350 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1351 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1352 MODULE_LICENSE("GPL");
1353
1354 module_init(mds_init);
1355 module_exit(mds_exit);