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