Whamcloud - gitweb
merge b_md onto HEAD. as best as I can remember:
[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 #include <linux/random.h>
37 #include <linux/locks.h>
38 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
39 #include <linux/buffer_head.h>
40 #endif
41 #include <linux/obd_lov.h>
42 #include <linux/lprocfs_status.h>
43
44 static kmem_cache_t *mds_file_cache;
45
46 extern int mds_get_lovtgts(struct mds_obd *obd, int tgt_count,
47                            obd_uuid_t *uuidarray);
48 extern int mds_get_lovdesc(struct mds_obd  *obd, struct lov_desc *desc);
49 extern int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
50                                 struct ptlrpc_request *req);
51 static int mds_cleanup(struct obd_device * obddev);
52
53 extern struct lprocfs_vars status_var_nm_1[];
54 extern struct lprocfs_vars status_class_var[];
55
56 inline struct mds_obd *mds_req2mds(struct ptlrpc_request *req)
57 {
58         return &req->rq_export->exp_obd->u.mds;
59 }
60
61 static int mds_bulk_timeout(void *data)
62 {
63         struct ptlrpc_bulk_desc *desc = data;
64
65         ENTRY;
66         CERROR("(not yet) starting recovery of client %p\n", desc->bd_client);
67         RETURN(1);
68 }
69
70 /* Assumes caller has already pushed into the kernel filesystem context */
71 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
72                         __u64 offset)
73 {
74         int rc = 0;
75         struct mds_obd *mds = mds_req2mds(req);
76         struct ptlrpc_bulk_desc *desc;
77         struct ptlrpc_bulk_page *bulk;
78         struct l_wait_info lwi;
79         char *buf;
80         ENTRY;
81
82         desc = ptlrpc_prep_bulk(req->rq_connection);
83         if (desc == NULL)
84                 GOTO(out, rc = -ENOMEM);
85
86         bulk = ptlrpc_prep_bulk_page(desc);
87         if (bulk == NULL)
88                 GOTO(cleanup_bulk, rc = -ENOMEM);
89
90         OBD_ALLOC(buf, PAGE_SIZE);
91         if (buf == NULL)
92                 GOTO(cleanup_bulk, rc = -ENOMEM);
93
94         rc = mds_fs_readpage(mds, file, buf, PAGE_SIZE, (loff_t *)&offset);
95
96         if (rc != PAGE_SIZE)
97                 GOTO(cleanup_buf, rc = -EIO);
98
99         bulk->bp_xid = req->rq_xid;
100         bulk->bp_buf = buf;
101         bulk->bp_buflen = PAGE_SIZE;
102         desc->bd_portal = MDS_BULK_PORTAL;
103
104         rc = ptlrpc_send_bulk(desc);
105         if (rc)
106                 GOTO(cleanup_buf, rc);
107
108         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
109                 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
110                        OBD_FAIL_MDS_SENDPAGE, rc);
111                 ptlrpc_abort_bulk(desc);
112                 GOTO(cleanup_buf, rc);
113         }
114
115         lwi = LWI_TIMEOUT(obd_timeout * HZ, mds_bulk_timeout, desc);
116         rc = l_wait_event(desc->bd_waitq, desc->bd_flags & PTL_BULK_FL_SENT, &lwi);
117         if (rc) {
118                 if (rc != -ETIMEDOUT)
119                         LBUG();
120                 GOTO(cleanup_buf, rc);
121         }
122
123         EXIT;
124  cleanup_buf:
125         OBD_FREE(buf, PAGE_SIZE);
126  cleanup_bulk:
127         ptlrpc_free_bulk(desc);
128  out:
129         return rc;
130 }
131
132 /*
133  * Look up a named entry in a directory, and get an LDLM lock on it.
134  * 'dir' is a inode for which an LDLM lock has already been taken.
135  *
136  * If we do not need an exclusive or write lock on this entry (e.g.
137  * a read lock for attribute lookup only) then we do not hold the
138  * directory on return.  It is up to the caller to know what type
139  * of lock it is getting, and clean up appropriately.
140  */
141 struct dentry *mds_name2locked_dentry(struct obd_device *obd,
142                                       struct dentry *dir, struct vfsmount **mnt,
143                                       char *name, int namelen, int lock_mode,
144                                       struct lustre_handle *lockh,
145                                       int dir_lock_mode)
146 {
147         struct dentry *dchild;
148         int flags = 0, rc;
149         __u64 res_id[3] = {0};
150         ENTRY;
151
152         down(&dir->d_inode->i_sem);
153         dchild = lookup_one_len(name, dir, namelen);
154         if (IS_ERR(dchild)) {
155                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
156                 up(&dir->d_inode->i_sem);
157                 LBUG();
158                 RETURN(dchild);
159         }
160         if (dir_lock_mode != LCK_EX && dir_lock_mode != LCK_PW) {
161                 up(&dir->d_inode->i_sem);
162                 ldlm_lock_decref(lockh, dir_lock_mode);
163         }
164
165         if (lock_mode == 0 || !dchild->d_inode)
166                 RETURN(dchild);
167
168         res_id[0] = dchild->d_inode->i_ino;
169         res_id[1] = dchild->d_inode->i_generation;
170         rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
171                                    res_id, LDLM_PLAIN, NULL, 0, lock_mode,
172                                    &flags, ldlm_completion_ast,
173                                    mds_blocking_ast, NULL, 0, lockh);
174         if (rc != ELDLM_OK) {
175                 l_dput(dchild);
176                 up(&dir->d_inode->i_sem);
177                 RETURN(ERR_PTR(-ENOLCK)); /* XXX translate ldlm code */
178         }
179
180         RETURN(dchild);
181 }
182
183 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
184                                      struct vfsmount **mnt, int lock_mode,
185                                      struct lustre_handle *lockh)
186 {
187         struct mds_obd *mds = &obd->u.mds;
188         struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
189         int flags = 0, rc;
190         __u64 res_id[3] = {0};
191         ENTRY;
192
193         if (IS_ERR(de))
194                 RETURN(de);
195
196         res_id[0] = de->d_inode->i_ino;
197         res_id[1] = de->d_inode->i_generation;
198         rc = ldlm_match_or_enqueue(NULL, NULL, obd->obd_namespace, NULL,
199                                    res_id, LDLM_PLAIN, NULL, 0, lock_mode,
200                                    &flags, ldlm_completion_ast,
201                                    mds_blocking_ast, NULL, 0, lockh);
202         if (rc != ELDLM_OK) {
203                 l_dput(de);
204                 retval = ERR_PTR(-ENOLCK); /* XXX translate ldlm code */
205         }
206
207         RETURN(retval);
208 }
209
210 #ifndef DCACHE_DISCONNECTED
211 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
212 #endif
213
214 /* Look up an entry by inode number. */
215 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
216                               struct vfsmount **mnt)
217 {
218         /* stolen from NFS */
219         struct super_block *sb = mds->mds_sb;
220         unsigned long ino = fid->id;
221         __u32 generation = fid->generation;
222         struct inode *inode;
223         struct list_head *lp;
224         struct dentry *result;
225
226         if (ino == 0)
227                 RETURN(ERR_PTR(-ESTALE));
228
229         inode = iget(sb, ino);
230         if (inode == NULL)
231                 RETURN(ERR_PTR(-ENOMEM));
232
233         CDEBUG(D_DENTRY, "--> mds_fid2dentry: sb %p\n", inode->i_sb);
234
235         if (is_bad_inode(inode) ||
236             (generation && inode->i_generation != generation)) {
237                 /* we didn't find the right inode.. */
238                 CERROR("bad inode %lu, link: %d ct: %d or version  %u/%u\n",
239                        inode->i_ino, inode->i_nlink,
240                        atomic_read(&inode->i_count), inode->i_generation,
241                        generation);
242                 iput(inode);
243                 RETURN(ERR_PTR(-ENOENT));
244         }
245
246         /* now to find a dentry. If possible, get a well-connected one */
247         if (mnt)
248                 *mnt = mds->mds_vfsmnt;
249         spin_lock(&dcache_lock);
250         list_for_each(lp, &inode->i_dentry) {
251                 result = list_entry(lp, struct dentry, d_alias);
252                 if (!(result->d_flags & DCACHE_DISCONNECTED)) {
253                         dget_locked(result);
254                         result->d_vfs_flags |= DCACHE_REFERENCED;
255                         spin_unlock(&dcache_lock);
256                         iput(inode);
257                         if (mnt)
258                                 mntget(*mnt);
259                         return result;
260                 }
261         }
262         spin_unlock(&dcache_lock);
263         result = d_alloc_root(inode);
264         if (result == NULL) {
265                 iput(inode);
266                 return ERR_PTR(-ENOMEM);
267         }
268         if (mnt)
269                 mntget(*mnt);
270         result->d_flags |= DCACHE_DISCONNECTED;
271         return result;
272 }
273
274 /* Establish a connection to the MDS.
275  *
276  * This will set up an export structure for the client to hold state data
277  * about that client, like open files, the last operation number it did
278  * on the server, etc.
279  */
280 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
281                        obd_uuid_t cluuid, struct recovd_obd *recovd,
282                        ptlrpc_recovery_cb_t recover)
283 {
284         struct obd_export *exp;
285         struct mds_export_data *med;
286         struct mds_client_data *mcd;
287         struct list_head *p;
288         int rc;
289         ENTRY;
290
291         if (!conn || !obd || !cluuid)
292                 RETURN(-EINVAL);
293
294         MOD_INC_USE_COUNT;
295
296         spin_lock(&obd->obd_dev_lock);
297         list_for_each(p, &obd->obd_exports) {
298                 exp = list_entry(p, struct obd_export, exp_obd_chain);
299                 mcd = exp->exp_mds_data.med_mcd;
300                 if (!mcd) {
301                         CERROR("FYI: NULL mcd - simultaneous connects\n");
302                         continue;
303                 }
304                 if (!memcmp(cluuid, mcd->mcd_uuid, sizeof(mcd->mcd_uuid))) {
305                         LASSERT(exp->exp_obd == obd);
306
307                         if (!list_empty(&exp->exp_conn_chain)) {
308                                 CERROR("existing uuid/export, list not empty!\n");
309                                 spin_unlock(&obd->obd_dev_lock);
310                                 MOD_DEC_USE_COUNT;
311                                 RETURN(-EALREADY);
312                         }
313                         conn->addr = (__u64) (unsigned long)exp;
314                         conn->cookie = exp->exp_cookie;
315                         spin_unlock(&obd->obd_dev_lock);
316                         CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
317                                cluuid, exp);
318                         CDEBUG(D_IOCTL,"connect: addr %Lx cookie %Lx\n",
319                                (long long)conn->addr, (long long)conn->cookie);
320                         MOD_DEC_USE_COUNT;
321                         RETURN(0);
322                 }
323         }
324         spin_unlock(&obd->obd_dev_lock);
325         /* XXX There is a small race between checking the list and adding a
326          * new connection for the same UUID, but the real threat (list
327          * corruption when multiple different clients connect) is solved.
328          *
329          * There is a second race between adding the export to the list,
330          * and filling in the client data below.  Hence skipping the case
331          * of NULL mcd above.  We should already be controlling multiple
332          * connects at the client, and we can't hold the spinlock over
333          * memory allocations without risk of deadlocking.
334          */
335         rc = class_connect(conn, obd, cluuid);
336         if (rc)
337                 GOTO(out_dec, rc);
338         exp = class_conn2export(conn);
339         LASSERT(exp);
340         med = &exp->exp_mds_data;
341
342         OBD_ALLOC(mcd, sizeof(*mcd));
343         if (!mcd) {
344                 CERROR("mds: out of memory for client data\n");
345                 GOTO(out_export, rc = -ENOMEM);
346         }
347
348         memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
349         med->med_mcd = mcd;
350
351         INIT_LIST_HEAD(&med->med_open_head);
352         spin_lock_init(&med->med_open_lock);
353
354         rc = mds_client_add(med, -1);
355         if (rc)
356                 GOTO(out_mcd, rc);
357
358         RETURN(0);
359
360 out_mcd:
361         OBD_FREE(mcd, sizeof(*mcd));
362 out_export:
363         class_disconnect(conn);
364 out_dec:
365         MOD_DEC_USE_COUNT;
366
367         return rc;
368 }
369
370 /* Call with med->med_open_lock held, please. */
371 inline int mds_close_mfd(struct mds_file_data *mfd, struct mds_export_data *med)
372 {
373         struct file *file = mfd->mfd_file;
374         LASSERT(file->private_data == mfd);
375
376         list_del(&mfd->mfd_list);
377         mfd->mfd_servercookie = DEAD_HANDLE_MAGIC;
378         kmem_cache_free(mds_file_cache, mfd);
379
380         return filp_close(file, 0);
381 }
382
383 static int mds_disconnect(struct lustre_handle *conn)
384 {
385         struct obd_export *export = class_conn2export(conn);
386         struct list_head *tmp, *n;
387         struct mds_export_data *med = &export->exp_mds_data;
388         int rc;
389         ENTRY;
390
391         /*
392          * Close any open files.
393          */
394         spin_lock(&med->med_open_lock);
395         list_for_each_safe(tmp, n, &med->med_open_head) {
396                 struct mds_file_data *mfd =
397                         list_entry(tmp, struct mds_file_data, mfd_list);
398                 rc = mds_close_mfd(mfd, med);
399                 if (rc) {
400                         /* XXX better diagnostics, with file path and stuff */
401                         CDEBUG(D_INODE, "Error %d closing mfd %p\n", rc, mfd);
402                 }
403         }
404         spin_unlock(&med->med_open_lock);
405
406         ldlm_cancel_locks_for_export(export);
407         mds_client_free(export);
408
409         rc = class_disconnect(conn);
410         if (!rc)
411                 MOD_DEC_USE_COUNT;
412
413         RETURN(rc);
414 }
415
416 /*
417  * XXX This is NOT guaranteed to flush all transactions to disk (even though
418  *     it is equivalent to calling sync()) because it only _starts_ the flush
419  *     and does not wait for completion.  It's better than nothing though.
420  *     What we really want is a mild form of fsync_dev_lockfs(), but it is
421  *     non-standard, or enabling do_sync_supers in ext3, just for this call.
422  */
423 static void mds_fsync_super(struct super_block *sb)
424 {
425         lock_kernel();
426         lock_super(sb);
427         if (sb->s_dirt && sb->s_op && sb->s_op->write_super)
428                 sb->s_op->write_super(sb);
429         unlock_super(sb);
430         unlock_kernel();
431 }
432
433 static int mds_getstatus(struct ptlrpc_request *req)
434 {
435         struct mds_obd *mds = mds_req2mds(req);
436         struct mds_body *body;
437         int rc, size = sizeof(*body);
438         ENTRY;
439
440         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
441         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
442                 CERROR("mds: out of memory for message: size=%d\n", size);
443                 req->rq_status = -ENOMEM;
444                 RETURN(0);
445         }
446
447         /* Flush any outstanding transactions to disk so the client will
448          * get the latest last_committed value and can drop their local
449          * requests if they have any.  This would be fsync_super() if it
450          * was exported.
451          */
452         mds_fsync_super(mds->mds_sb);
453
454         body = lustre_msg_buf(req->rq_repmsg, 0);
455         memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
456
457         /* the last_committed and last_xid fields are filled in for all
458          * replies already - no need to do so here also.
459          */
460         RETURN(0);
461 }
462
463 static int mds_getlovinfo(struct ptlrpc_request *req)
464 {
465         struct mds_obd *mds = mds_req2mds(req);
466         struct mds_status_req *streq;
467         struct lov_desc *desc;
468         int tgt_count;
469         int rc, size[2] = {sizeof(*desc)};
470         ENTRY;
471
472         streq = lustre_msg_buf(req->rq_reqmsg, 0);
473         streq->flags = NTOH__u32(streq->flags);
474         streq->repbuf = NTOH__u32(streq->repbuf);
475         size[1] = streq->repbuf;
476
477         rc = lustre_pack_msg(2, size, NULL, &req->rq_replen, &req->rq_repmsg);
478         if (rc) {
479                 CERROR("mds: out of memory for message: size=%d\n", size[1]);
480                 req->rq_status = -ENOMEM;
481                 RETURN(0);
482         }
483
484         desc = lustre_msg_buf(req->rq_repmsg, 0);
485         rc = mds_get_lovdesc(mds, desc);
486         if (rc) {
487                 CERROR("mds_get_lovdesc error %d", rc);
488                 req->rq_status = rc;
489                 RETURN(0);
490         }
491
492         tgt_count = le32_to_cpu(desc->ld_tgt_count);
493         if (tgt_count * sizeof(obd_uuid_t) > streq->repbuf) {
494                 CERROR("too many targets, enlarge client buffers\n");
495                 req->rq_status = -ENOSPC;
496                 RETURN(0);
497         }
498
499         /* XXX the MDS should not really know about this */
500         mds->mds_max_mdsize = lov_mds_md_size(tgt_count);
501         rc = mds_get_lovtgts(mds, tgt_count,
502                              lustre_msg_buf(req->rq_repmsg, 1));
503         if (rc) {
504                 CERROR("get_lovtgts error %d\n", rc);
505                 req->rq_status = rc;
506                 RETURN(0);
507         }
508         RETURN(0);
509 }
510
511 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
512                      void *data, __u32 data_len, int flag)
513 {
514         int do_ast;
515         ENTRY;
516
517         if (flag == LDLM_CB_CANCELING) {
518                 /* Don't need to do anything here. */
519                 RETURN(0);
520         }
521
522         /* XXX layering violation!  -phil */
523         l_lock(&lock->l_resource->lr_namespace->ns_lock);
524         lock->l_flags |= LDLM_FL_CBPENDING;
525         do_ast = (!lock->l_readers && !lock->l_writers);
526         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
527
528         if (do_ast) {
529                 struct lustre_handle lockh;
530                 int rc;
531
532                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
533                 ldlm_lock2handle(lock, &lockh);
534                 rc = ldlm_cli_cancel(&lockh);
535                 if (rc < 0)
536                         CERROR("ldlm_cli_cancel: %d\n", rc);
537         } else
538                 LDLM_DEBUG(lock, "Lock still has references, will be"
539                            "cancelled later");
540         RETURN(0);
541 }
542
543 int mds_pack_md(struct mds_obd *mds, struct ptlrpc_request *req,
544                 int offset, struct mds_body *body, struct inode *inode)
545 {
546         struct lov_mds_md *lmm;
547         int lmm_size = req->rq_repmsg->buflens[offset];
548         int rc;
549
550         if (lmm_size == 0) {
551                 CDEBUG(D_INFO, "no space reserved for inode %u MD\n", inode->i_ino);
552                 RETURN(0);
553         }
554
555         lmm = lustre_msg_buf(req->rq_repmsg, offset);
556
557         /* I don't really like this, but it is a sanity check on the client
558          * MD request.  However, if the client doesn't know how much space
559          * to reserve for the MD, this shouldn't be fatal either...
560          */
561         if (lmm_size > mds->mds_max_mdsize) {
562                 CERROR("Reading MD for inode %u of %d bytes > max %d\n",
563                        inode->i_ino, lmm_size, mds->mds_max_mdsize);
564                 // RETURN(-EINVAL);
565         }
566
567         /* We don't need to store the reply size, because this buffer is
568          * discarded right after unpacking, and the LOV can figure out the
569          * size itself from the ost count.
570          */
571         if ((rc = mds_fs_get_md(mds, inode, lmm, lmm_size)) < 0) {
572                 CDEBUG(D_INFO, "No md for ino %u: rc = %d\n", inode->i_ino, rc);
573         } else if (rc > 0) {
574                 body->valid |= OBD_MD_FLEASIZE;
575                 rc = 0;
576         }
577
578         return rc;
579 }
580
581 static int mds_getattr_internal(struct mds_obd *mds, struct dentry *dentry,
582                                 struct ptlrpc_request *req,
583                                 struct mds_body *reqbody, int reply_off)
584 {
585         struct mds_body *body;
586         struct inode *inode = dentry->d_inode;
587         int rc = 0;
588         ENTRY;
589
590         if (inode == NULL)
591                 RETURN(-ENOENT);
592
593         body = lustre_msg_buf(req->rq_repmsg, reply_off);
594
595         mds_pack_inode2fid(&body->fid1, inode);
596         mds_pack_inode2body(body, inode);
597
598         if (S_ISREG(inode->i_mode)) {
599                 rc = mds_pack_md(mds, req, reply_off + 1, body, inode);
600         } else if (S_ISLNK(inode->i_mode) && reqbody->valid & OBD_MD_LINKNAME) {
601                 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1);
602                 int len = req->rq_repmsg->buflens[reply_off + 1];
603
604                 rc = inode->i_op->readlink(dentry, symname, len);
605                 if (rc < 0) {
606                         CERROR("readlink failed: %d\n", rc);
607                 } else {
608                         CDEBUG(D_INODE, "read symlink dest %s\n", symname);
609                         body->valid |= OBD_MD_LINKNAME;
610                 }
611         }
612         RETURN(rc);
613 }
614
615 static int mds_getattr_name(int offset, struct ptlrpc_request *req)
616 {
617         struct mds_obd *mds = mds_req2mds(req);
618         struct obd_device *obd = req->rq_export->exp_obd;
619         struct obd_run_ctxt saved;
620         struct mds_body *body;
621         struct dentry *de = NULL, *dchild = NULL;
622         struct inode *dir;
623         struct lustre_handle lockh;
624         char *name;
625         int namelen, flags = 0, lock_mode, rc = 0;
626         struct obd_ucred uc;
627         __u64 res_id[3] = {0, 0, 0};
628         ENTRY;
629
630         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
631
632         if (req->rq_reqmsg->bufcount <= offset + 1) {
633                 LBUG();
634                 GOTO(out_pre_de, rc = -EINVAL);
635         }
636
637         body = lustre_msg_buf(req->rq_reqmsg, offset);
638         name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
639         namelen = req->rq_reqmsg->buflens[offset + 1];
640         /* requests were at offset 2, replies go back at 1 */
641         if (offset)
642                 offset = 1;
643
644         uc.ouc_fsuid = body->fsuid;
645         uc.ouc_fsgid = body->fsgid;
646         uc.ouc_cap = body->capability;
647         push_ctxt(&saved, &mds->mds_ctxt, &uc);
648         de = mds_fid2dentry(mds, &body->fid1, NULL);
649         if (IS_ERR(de)) {
650                 GOTO(out_pre_de, rc = -ENOENT);
651         }
652
653         dir = de->d_inode;
654         CDEBUG(D_INODE, "parent ino %ld, name %*s\n", dir->i_ino,namelen,name);
655
656         lock_mode = LCK_PR;
657         res_id[0] = dir->i_ino;
658         res_id[1] = dir->i_generation;
659
660         rc = ldlm_lock_match(obd->obd_namespace, res_id, LDLM_PLAIN,
661                              NULL, 0, lock_mode, &lockh);
662         if (rc == 0) {
663                 LDLM_DEBUG_NOLOCK("enqueue res "LPU64, res_id[0]);
664                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
665                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
666                                       &flags, ldlm_completion_ast,
667                                       mds_blocking_ast, NULL, 0, &lockh);
668                 if (rc != ELDLM_OK) {
669                         CERROR("lock enqueue: err: %d\n", rc);
670                         GOTO(out_create_de, rc = -EIO);
671                 }
672         }
673         ldlm_lock_dump((void *)(unsigned long)lockh.addr);
674
675         down(&dir->i_sem);
676         dchild = lookup_one_len(name, de, namelen - 1);
677         if (IS_ERR(dchild)) {
678                 CDEBUG(D_INODE, "child lookup error %ld\n", PTR_ERR(dchild));
679                 up(&dir->i_sem);
680                 GOTO(out_create_dchild, rc = PTR_ERR(dchild));
681         }
682
683         rc = mds_getattr_internal(mds, dchild, req, body, offset);
684
685         EXIT;
686 out_create_dchild:
687         l_dput(dchild);
688         up(&dir->i_sem);
689         ldlm_lock_decref(&lockh, lock_mode);
690 out_create_de:
691         l_dput(de);
692 out_pre_de:
693         req->rq_status = rc;
694         pop_ctxt(&saved);
695         return 0;
696 }
697
698 static int mds_getattr(int offset, struct ptlrpc_request *req)
699 {
700         struct mds_obd *mds = mds_req2mds(req);
701         struct obd_run_ctxt saved;
702         struct dentry *de;
703         struct inode *inode;
704         struct mds_body *body;
705         struct obd_ucred uc;
706         int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
707         ENTRY;
708
709         body = lustre_msg_buf(req->rq_reqmsg, offset);
710         uc.ouc_fsuid = body->fsuid;
711         uc.ouc_fsgid = body->fsgid;
712         uc.ouc_cap = body->capability;
713         push_ctxt(&saved, &mds->mds_ctxt, &uc);
714         de = mds_fid2dentry(mds, &body->fid1, NULL);
715         if (IS_ERR(de)) {
716                 rc = req->rq_status = -ENOENT;
717                 GOTO(out_pop, PTR_ERR(de));
718         }
719
720         inode = de->d_inode;
721         if (S_ISREG(body->fid1.f_type)) {
722                 int rc = mds_fs_get_md(mds, inode, NULL, 0);
723                 CDEBUG(D_INODE, "got %d bytes MD data for inode %u\n",
724                        rc, inode->i_ino);
725                 if (rc < 0) {
726                         if (rc != -ENODATA)
727                                 CERROR("error getting inode %u MD: rc = %d\n",
728                                        inode->i_ino, rc);
729                         size[bufcount] = 0;
730                 } else if (rc > mds->mds_max_mdsize) {
731                         size[bufcount] = 0;
732                         CERROR("MD size %d larger than maximum possible %u\n",
733                                rc, mds->mds_max_mdsize);
734                 } else
735                         size[bufcount] = rc;
736                 bufcount++;
737         } else if (body->valid & OBD_MD_LINKNAME) {
738                 size[bufcount] = MIN(inode->i_size + 1, body->size);
739                 bufcount++;
740                 CDEBUG(D_INODE, "symlink size: %d, reply space: %d\n",
741                        inode->i_size + 1, body->size);
742         }
743
744         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
745                 CERROR("failed MDS_GETATTR_PACK test\n");
746                 req->rq_status = -ENOMEM;
747                 GOTO(out, rc = -ENOMEM);
748         }
749
750         rc = lustre_pack_msg(bufcount, size, NULL, &req->rq_replen,
751                              &req->rq_repmsg);
752         if (rc) {
753                 CERROR("out of memoryK\n");
754                 req->rq_status = rc;
755                 GOTO(out, rc);
756         }
757
758         req->rq_status = mds_getattr_internal(mds, de, req, body, 0);
759
760 out:
761         l_dput(de);
762 out_pop:
763         pop_ctxt(&saved);
764         RETURN(rc);
765 }
766
767 static int mds_statfs(struct ptlrpc_request *req)
768 {
769         struct mds_obd *mds = mds_req2mds(req);
770         struct obd_statfs *osfs;
771         struct statfs sfs;
772         int rc, size = sizeof(*osfs);
773         ENTRY;
774
775         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
776         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
777                 CERROR("mds: statfs lustre_pack_msg failed: rc = %d\n", rc);
778                 GOTO(out, rc);
779         }
780
781         rc = mds_fs_statfs(mds, &sfs);
782         if (rc) {
783                 CERROR("mds: statfs failed: rc %d\n", rc);
784                 GOTO(out, rc);
785         }
786         osfs = lustre_msg_buf(req->rq_repmsg, 0);
787         memset(osfs, 0, size);
788         statfs_pack(osfs, &sfs);
789         obd_statfs_pack(osfs, osfs);
790
791 out:
792         req->rq_status = rc;
793         RETURN(0);
794 }
795
796 static struct mds_file_data *mds_handle2mfd(struct lustre_handle *handle)
797 {
798         struct mds_file_data *mfd = NULL;
799
800         if (!handle || !handle->addr)
801                 RETURN(NULL);
802
803         mfd = (struct mds_file_data *)(unsigned long)(handle->addr);
804         if (!kmem_cache_validate(mds_file_cache, mfd))
805                 RETURN(NULL);
806
807         if (mfd->mfd_servercookie != handle->cookie)
808                 RETURN(NULL);
809
810         return mfd;
811 }
812
813 static int mds_store_md(struct mds_obd *mds, struct ptlrpc_request *req,
814                         int offset, struct mds_body *body, struct inode *inode)
815 {
816         struct lov_mds_md *lmm = lustre_msg_buf(req->rq_reqmsg, offset);
817         int lmm_size = req->rq_reqmsg->buflens[offset];
818         struct obd_run_ctxt saved;
819         struct obd_ucred uc;
820         void *handle;
821         int rc, rc2;
822         ENTRY;
823
824         /* I don't really like this, but it is a sanity check on the client
825          * MD request.
826          */
827         if (lmm_size > mds->mds_max_mdsize) {
828                 CERROR("Saving MD for inode %u of %d bytes > max %d\n",
829                        inode->i_ino, lmm_size, mds->mds_max_mdsize);
830                 //RETURN(-EINVAL);
831         }
832
833         CDEBUG(D_INODE, "storing %d bytes MD for inode %u\n",
834                lmm_size, inode->i_ino);
835         uc.ouc_fsuid = body->fsuid;
836         uc.ouc_fsgid = body->fsgid;
837         uc.ouc_cap = body->capability;
838         push_ctxt(&saved, &mds->mds_ctxt, &uc);
839         handle = mds_fs_start(mds, inode, MDS_FSOP_SETATTR);
840         if (!handle)
841                 GOTO(out_ea, rc = -ENOMEM);
842
843         rc = mds_fs_set_md(mds, inode, handle, lmm, lmm_size);
844         if (!rc)
845                 rc = mds_update_last_rcvd(mds, handle, req);
846
847         rc2 = mds_fs_commit(mds, inode, handle);
848         if (rc2 && !rc)
849                 rc = rc2;
850 out_ea:
851         pop_ctxt(&saved);
852
853         RETURN(rc);
854 }
855
856 static int mds_open(struct ptlrpc_request *req)
857 {
858         struct mds_obd *mds = mds_req2mds(req);
859         struct mds_body *body;
860         struct mds_export_data *med;
861         struct mds_file_data *mfd;
862         struct dentry *de;
863         struct file *file;
864         struct vfsmount *mnt;
865         __u32 flags;
866         struct list_head *tmp;
867         int rc, size = sizeof(*body);
868         ENTRY;
869
870         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OPEN_PACK)) {
871                 CERROR("test case OBD_FAIL_MDS_OPEN_PACK\n");
872                 req->rq_status = -ENOMEM;
873                 RETURN(-ENOMEM);
874         }
875
876         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
877         if (rc) {
878                 CERROR("mds: pack error: rc = %d\n", rc);
879                 req->rq_status = rc;
880                 RETURN(rc);
881         }
882
883         body = lustre_msg_buf(req->rq_reqmsg, 0);
884
885         /* was this animal open already and the client lost the reply? */
886         /* XXX need some way to detect a reopen, to avoid locked list walks */
887         med = &req->rq_export->exp_mds_data;
888         spin_lock(&med->med_open_lock);
889         list_for_each(tmp, &med->med_open_head) {
890                 mfd = list_entry(tmp, typeof(*mfd), mfd_list);
891                 if (!memcmp(&mfd->mfd_clienthandle, &body->handle,
892                             sizeof(mfd->mfd_clienthandle)) &&
893                     body->fid1.id == mfd->mfd_file->f_dentry->d_inode->i_ino) {
894                         de = mfd->mfd_file->f_dentry;
895                         spin_unlock(&med->med_open_lock);
896                         CERROR("Re opening "LPD64"\n", body->fid1.id);
897                         GOTO(out_pack, rc = 0);
898                 }
899         }
900         spin_unlock(&med->med_open_lock);
901
902         mfd = kmem_cache_alloc(mds_file_cache, GFP_KERNEL);
903         if (!mfd) {
904                 CERROR("mds: out of memory\n");
905                 req->rq_status = -ENOMEM;
906                 RETURN(0);
907         }
908
909         de = mds_fid2dentry(mds, &body->fid1, &mnt);
910         if (IS_ERR(de))
911                 GOTO(out_free, rc = PTR_ERR(de));
912
913         /* check if this inode has seen a delayed object creation */
914         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MDS_OPEN_HAS_EA) {
915                 rc = mds_store_md(mds, req, 1, body, de->d_inode);
916                 if (rc) {
917                         l_dput(de);
918                         mntput(mnt);
919                         GOTO(out_free, rc);
920                 }
921         }
922
923         flags = body->flags;
924         /* dentry_open does a dput(de) and mntput(mnt) on error */
925         file = dentry_open(de, mnt, flags & ~O_DIRECT);
926         if (IS_ERR(file)) {
927                 rc = PTR_ERR(file);
928                 GOTO(out_free, 0);
929         }
930
931         file->private_data = mfd;
932         mfd->mfd_file = file;
933         memcpy(&mfd->mfd_clienthandle, &body->handle, sizeof(body->handle));
934         get_random_bytes(&mfd->mfd_servercookie, sizeof(mfd->mfd_servercookie));
935         spin_lock(&med->med_open_lock);
936         list_add(&mfd->mfd_list, &med->med_open_head);
937         spin_unlock(&med->med_open_lock);
938
939 out_pack:
940         body = lustre_msg_buf(req->rq_repmsg, 0);
941         mds_pack_inode2fid(&body->fid1, de->d_inode);
942         mds_pack_inode2body(body, de->d_inode);
943         body->handle.addr = (__u64)(unsigned long)mfd;
944         body->handle.cookie = mfd->mfd_servercookie;
945         CDEBUG(D_INODE, "llite file "LPX64": addr %p, cookie "LPX64"\n",
946                mfd->mfd_clienthandle.addr, mfd, mfd->mfd_servercookie);
947         RETURN(0);
948
949 out_free:
950         mfd->mfd_servercookie = DEAD_HANDLE_MAGIC;
951         kmem_cache_free(mds_file_cache, mfd);
952         req->rq_status = rc;
953         RETURN(0);
954 }
955
956 static int mds_close(struct ptlrpc_request *req)
957 {
958         struct mds_export_data *med = &req->rq_export->exp_mds_data;
959         struct mds_body *body;
960         struct mds_file_data *mfd;
961         int rc;
962         ENTRY;
963
964         body = lustre_msg_buf(req->rq_reqmsg, 0);
965
966         mfd = mds_handle2mfd(&body->handle);
967         if (!mfd) {
968                 CERROR("no handle for file close "LPD64
969                        ": addr "LPX64", cookie "LPX64"\n",
970                        body->fid1.id, body->handle.addr, body->handle.cookie);
971                 RETURN(-ESTALE);
972         }
973
974         spin_lock(&med->med_open_lock);
975         req->rq_status = mds_close_mfd(mfd, med);
976         spin_unlock(&med->med_open_lock);
977
978         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_CLOSE_PACK)) {
979                 CERROR("test case OBD_FAIL_MDS_CLOSE_PACK\n");
980                 req->rq_status = -ENOMEM;
981                 RETURN(-ENOMEM);
982         }
983
984         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
985         if (rc) {
986                 CERROR("mds: lustre_pack_msg: rc = %d\n", rc);
987                 req->rq_status = rc;
988         }
989
990         RETURN(0);
991 }
992
993 static int mds_readpage(struct ptlrpc_request *req)
994 {
995         struct mds_obd *mds = mds_req2mds(req);
996         struct vfsmount *mnt;
997         struct dentry *de;
998         struct file *file;
999         struct mds_body *body, *repbody;
1000         struct obd_run_ctxt saved;
1001         int rc, size = sizeof(*body);
1002         struct obd_ucred uc;
1003         ENTRY;
1004
1005         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
1006         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
1007                 CERROR("mds: out of memory\n");
1008                 GOTO(out, rc = -ENOMEM);
1009         }
1010
1011         body = lustre_msg_buf(req->rq_reqmsg, 0);
1012         uc.ouc_fsuid = body->fsuid;
1013         uc.ouc_fsgid = body->fsgid;
1014         uc.ouc_cap = body->capability;
1015         push_ctxt(&saved, &mds->mds_ctxt, &uc);
1016         de = mds_fid2dentry(mds, &body->fid1, &mnt);
1017         if (IS_ERR(de))
1018                 GOTO(out_pop, rc = PTR_ERR(de));
1019
1020         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
1021
1022         file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1023         /* note: in case of an error, dentry_open puts dentry */
1024         if (IS_ERR(file))
1025                 GOTO(out_pop, rc = PTR_ERR(file));
1026
1027         repbody = lustre_msg_buf(req->rq_repmsg, 0);
1028         repbody->size = file->f_dentry->d_inode->i_size;
1029         repbody->valid = OBD_MD_FLSIZE;
1030
1031         /* to make this asynchronous make sure that the handling function
1032            doesn't send a reply when this function completes. Instead a
1033            callback function would send the reply */
1034         /* note: in case of an error, dentry_open puts dentry */
1035         rc = mds_sendpage(req, file, body->size);
1036
1037         filp_close(file, 0);
1038 out_pop:
1039         pop_ctxt(&saved);
1040 out:
1041         req->rq_status = rc;
1042         RETURN(0);
1043 }
1044
1045 int mds_reint(struct ptlrpc_request *req, int offset)
1046 {
1047         int rc;
1048         struct mds_update_record rec;
1049
1050         rc = mds_update_unpack(req, offset, &rec);
1051         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1052                 CERROR("invalid record\n");
1053                 req->rq_status = -EINVAL;
1054                 RETURN(0);
1055         }
1056         /* rc will be used to interrupt a for loop over multiple records */
1057         rc = mds_reint_rec(&rec, offset, req);
1058         return rc;
1059 }
1060
1061 int mds_handle(struct ptlrpc_request *req)
1062 {
1063         int rc;
1064         ENTRY;
1065
1066         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
1067         if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_HANDLE_UNPACK)) {
1068                 CERROR("lustre_mds: Invalid request\n");
1069                 GOTO(out, rc);
1070         }
1071
1072         if (req->rq_reqmsg->opc != MDS_CONNECT && req->rq_export == NULL)
1073                 GOTO(out, rc = -ENOTCONN);
1074
1075         LASSERT(!strcmp(req->rq_obd->obd_type->typ_name, LUSTRE_MDT_NAME));
1076
1077         switch (req->rq_reqmsg->opc) {
1078         case MDS_CONNECT:
1079                 CDEBUG(D_INODE, "connect\n");
1080                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
1081                 rc = target_handle_connect(req);
1082                 break;
1083
1084         case MDS_DISCONNECT:
1085                 CDEBUG(D_INODE, "disconnect\n");
1086                 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
1087                 rc = target_handle_disconnect(req);
1088                 goto out;
1089
1090         case MDS_GETSTATUS:
1091                 CDEBUG(D_INODE, "getstatus\n");
1092                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1093                 rc = mds_getstatus(req);
1094                 break;
1095
1096         case MDS_GETLOVINFO:
1097                 CDEBUG(D_INODE, "getlovinfo\n");
1098                 rc = mds_getlovinfo(req);
1099                 break;
1100
1101         case MDS_GETATTR:
1102                 CDEBUG(D_INODE, "getattr\n");
1103                 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1104                 rc = mds_getattr(0, req);
1105                 break;
1106
1107         case MDS_STATFS:
1108                 CDEBUG(D_INODE, "statfs\n");
1109                 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1110                 rc = mds_statfs(req);
1111                 break;
1112
1113         case MDS_READPAGE:
1114                 CDEBUG(D_INODE, "readpage\n");
1115                 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1116                 rc = mds_readpage(req);
1117
1118                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1119                         return 0;
1120                 break;
1121
1122         case MDS_REINT: {
1123                 int size = sizeof(struct mds_body);
1124                 CDEBUG(D_INODE, "reint\n");
1125                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1126
1127                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1128                                      &req->rq_repmsg);
1129                 if (rc) {
1130                         req->rq_status = rc;
1131                         break;
1132                 }
1133                 rc = mds_reint(req, 0);
1134                 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET_REP, 0);
1135                 break;
1136                 }
1137
1138         case MDS_OPEN:
1139                 CDEBUG(D_INODE, "open\n");
1140                 OBD_FAIL_RETURN(OBD_FAIL_MDS_OPEN_NET, 0);
1141                 rc = mds_open(req);
1142                 break;
1143
1144         case MDS_CLOSE:
1145                 CDEBUG(D_INODE, "close\n");
1146                 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1147                 rc = mds_close(req);
1148                 break;
1149
1150         case LDLM_ENQUEUE:
1151                 CDEBUG(D_INODE, "enqueue\n");
1152                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1153                 rc = ldlm_handle_enqueue(req);
1154                 break;
1155         case LDLM_CONVERT:
1156                 CDEBUG(D_INODE, "convert\n");
1157                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1158                 rc = ldlm_handle_convert(req);
1159                 break;
1160         case LDLM_BL_CALLBACK:
1161         case LDLM_CP_CALLBACK:
1162                 CDEBUG(D_INODE, "callback\n");
1163                 CERROR("callbacks should not happen on MDS\n");
1164                 LBUG();
1165                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1166                 break;
1167         default:
1168                 rc = ptlrpc_error(req->rq_svc, req);
1169                 RETURN(rc);
1170         }
1171
1172         EXIT;
1173
1174         if (!rc) {
1175                 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1176                 struct mds_obd *mds = mds_req2mds(req);
1177
1178                 req->rq_repmsg->last_xid =
1179                         HTON__u64(le64_to_cpu(med->med_mcd->mcd_last_xid));
1180                 req->rq_repmsg->last_committed =
1181                         HTON__u64(mds->mds_last_committed);
1182                 CDEBUG(D_INFO, "last_rcvd ~%Lu, last_committed %Lu, xid %d\n",
1183                        (unsigned long long)mds->mds_last_rcvd,
1184                        (unsigned long long)mds->mds_last_committed,
1185                        cpu_to_le32(req->rq_xid));
1186         }
1187  out:
1188         if (rc) {
1189                 CERROR("mds: processing error (opcode %d): %d\n",
1190                        req->rq_reqmsg->opc, rc);
1191                 ptlrpc_error(req->rq_svc, req);
1192         } else {
1193                 CDEBUG(D_NET, "sending reply\n");
1194                 ptlrpc_reply(req->rq_svc, req);
1195         }
1196         return 0;
1197 }
1198
1199 /* Update the server data on disk.  This stores the new mount_count and
1200  * also the last_rcvd value to disk.  If we don't have a clean shutdown,
1201  * then the server last_rcvd value may be less than that of the clients.
1202  * This will alert us that we may need to do client recovery.
1203  *
1204  * Assumes we are already in the server filesystem context.
1205  *
1206  * Also assumes for mds_last_rcvd that we are not modifying it (no locking).
1207  */
1208 static
1209 int mds_update_server_data(struct mds_obd *mds)
1210 {
1211         struct mds_server_data *msd = mds->mds_server_data;
1212         struct file *filp = mds->mds_rcvd_filp;
1213         loff_t off = 0;
1214         int rc;
1215
1216         msd->msd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
1217         msd->msd_mount_count = cpu_to_le64(mds->mds_mount_count);
1218
1219         CDEBUG(D_SUPER, "MDS mount_count is %Lu, last_rcvd is %Lu\n",
1220                (unsigned long long)mds->mds_mount_count,
1221                (unsigned long long)mds->mds_last_rcvd);
1222         rc = lustre_fwrite(filp, (char *)msd, sizeof(*msd), &off);
1223         if (rc != sizeof(*msd)) {
1224                 CERROR("error writing MDS server data: rc = %d\n", rc);
1225                 if (rc > 0)
1226                         RETURN(-EIO);
1227                 RETURN(rc);
1228         }
1229 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1230         rc = fsync_dev(filp->f_dentry->d_inode->i_rdev);
1231 #else
1232         rc = file_fsync(filp,  filp->f_dentry, 1);
1233 #endif
1234         if (rc)
1235                 CERROR("error flushing MDS server data: rc = %d\n", rc);
1236
1237         return 0;
1238 }
1239
1240 /* Do recovery actions for the MDS */
1241 static int mds_recover(struct obd_device *obddev)
1242 {
1243         struct mds_obd *mds = &obddev->u.mds;
1244         struct obd_run_ctxt saved;
1245         int rc;
1246
1247         /* This happens at the end when recovery is complete */
1248         ++mds->mds_mount_count;
1249         push_ctxt(&saved, &mds->mds_ctxt, NULL);
1250         rc = mds_update_server_data(mds);
1251         pop_ctxt(&saved);
1252
1253         return rc;
1254 }
1255
1256 /* mount the file system (secretly) */
1257 static int mds_setup(struct obd_device *obddev, obd_count len, void *buf)
1258 {
1259         struct obd_ioctl_data* data = buf;
1260         struct mds_obd *mds = &obddev->u.mds;
1261         struct vfsmount *mnt;
1262         int rc = 0;
1263         ENTRY;
1264
1265         MOD_INC_USE_COUNT;
1266 #ifdef CONFIG_DEV_RDONLY
1267         dev_clear_rdonly(2);
1268 #endif
1269         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2)
1270                 GOTO(err_dec, rc = -EINVAL);
1271
1272         mds->mds_fstype = strdup(data->ioc_inlbuf2);
1273
1274         mnt = do_kern_mount(mds->mds_fstype, 0, data->ioc_inlbuf1, NULL);
1275         if (IS_ERR(mnt)) {
1276                 rc = PTR_ERR(mnt);
1277                 CERROR("do_kern_mount failed: rc = %d\n", rc);
1278                 GOTO(err_kfree, rc);
1279         }
1280
1281         CERROR("%s: mnt is %p\n", data->ioc_inlbuf1, mnt);
1282         mds->mds_sb = mnt->mnt_root->d_inode->i_sb;
1283         if (!mds->mds_sb)
1284                 GOTO(err_put, rc = -ENODEV);
1285
1286         spin_lock_init(&mds->mds_last_lock);
1287         mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1288         rc = mds_fs_setup(obddev, mnt);
1289         if (rc) {
1290                 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
1291                 GOTO(err_put, rc);
1292         }
1293
1294         obddev->obd_namespace =
1295                 ldlm_namespace_new("mds_server", LDLM_NAMESPACE_SERVER);
1296         if (obddev->obd_namespace == NULL) {
1297                 mds_cleanup(obddev);
1298                 GOTO(err_fs, rc = -ENOMEM);
1299         }
1300
1301
1302         rc = mds_recover(obddev);
1303         if (rc)
1304                 GOTO(err_fs, rc);
1305
1306         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1307                            "mds_ldlm_client", &obddev->obd_ldlm_client);
1308
1309         RETURN(0);
1310
1311 err_fs:
1312         mds_fs_cleanup(obddev);
1313 err_put:
1314         unlock_kernel();
1315         mntput(mds->mds_vfsmnt);
1316         mds->mds_sb = 0;
1317         lock_kernel();
1318 err_kfree:
1319         kfree(mds->mds_fstype);
1320 err_dec:
1321         MOD_DEC_USE_COUNT;
1322         RETURN(rc);
1323 }
1324
1325 static int mds_cleanup(struct obd_device *obddev)
1326 {
1327         struct super_block *sb;
1328         struct mds_obd *mds = &obddev->u.mds;
1329         struct obd_run_ctxt saved;
1330         ENTRY;
1331
1332         sb = mds->mds_sb;
1333         if (!mds->mds_sb)
1334                 RETURN(0);
1335
1336         push_ctxt(&saved, &mds->mds_ctxt, NULL);
1337         mds_update_server_data(mds);
1338
1339         if (mds->mds_rcvd_filp) {
1340                 int rc = filp_close(mds->mds_rcvd_filp, 0);
1341                 mds->mds_rcvd_filp = NULL;
1342
1343                 if (rc)
1344                         CERROR("last_rcvd file won't close, rc=%d\n", rc);
1345         }
1346         pop_ctxt(&saved);
1347
1348         unlock_kernel();
1349         mntput(mds->mds_vfsmnt);
1350         mds->mds_sb = 0;
1351         kfree(mds->mds_fstype);
1352
1353         ldlm_namespace_free(obddev->obd_namespace);
1354
1355         lock_kernel();
1356 #ifdef CONFIG_DEV_RDONLY
1357         dev_clear_rdonly(2);
1358 #endif
1359         mds_fs_cleanup(obddev);
1360
1361         MOD_DEC_USE_COUNT;
1362         RETURN(0);
1363 }
1364
1365 static int ldlm_intent_policy(struct ldlm_lock *lock, void *req_cookie,
1366                               ldlm_mode_t mode, int flags, void *data)
1367 {
1368         struct ptlrpc_request *req = req_cookie;
1369         int rc = 0;
1370         ENTRY;
1371
1372         if (!req_cookie)
1373                 RETURN(0);
1374
1375         if (req->rq_reqmsg->bufcount > 1) {
1376                 /* an intent needs to be considered */
1377                 struct ldlm_intent *it = lustre_msg_buf(req->rq_reqmsg, 1);
1378                 struct mds_obd *mds= &req->rq_export->exp_obd->u.mds;
1379                 struct mds_body *mds_rep;
1380                 struct ldlm_reply *rep;
1381                 __u64 new_resid[3] = {0, 0, 0}, old_res;
1382                 int rc, size[3] = {sizeof(struct ldlm_reply),
1383                                                   sizeof(struct mds_body),
1384                                                   mds->mds_max_mdsize};
1385
1386                 it->opc = NTOH__u64(it->opc);
1387
1388                 LDLM_DEBUG(lock, "intent policy, opc: %s",
1389                            ldlm_it2str(it->opc));
1390
1391                 rc = lustre_pack_msg(3, size, NULL, &req->rq_replen,
1392                                      &req->rq_repmsg);
1393                 if (rc) {
1394                         rc = req->rq_status = -ENOMEM;
1395                         RETURN(rc);
1396                 }
1397
1398                 rep = lustre_msg_buf(req->rq_repmsg, 0);
1399                 rep->lock_policy_res1 = 1;
1400
1401                 /* execute policy */
1402                 switch ((long)it->opc) {
1403                 case IT_CREAT|IT_OPEN:
1404                         rc = mds_reint(req, 2);
1405                         if (rc || (req->rq_status != 0 &&
1406                                    req->rq_status != -EEXIST)) {
1407                                 rep->lock_policy_res2 = req->rq_status;
1408                                 RETURN(ELDLM_LOCK_ABORTED);
1409                         }
1410                         break;
1411                 case IT_CREAT:
1412                 case IT_MKDIR:
1413                 case IT_MKNOD:
1414                 case IT_RENAME2:
1415                 case IT_LINK2:
1416                 case IT_RMDIR:
1417                 case IT_SYMLINK:
1418                 case IT_UNLINK:
1419                         rc = mds_reint(req, 2);
1420                         if (rc || (req->rq_status != 0 &&
1421                                    req->rq_status != -EISDIR &&
1422                                    req->rq_status != -ENOTDIR)) {
1423                                 rep->lock_policy_res2 = req->rq_status;
1424                                 RETURN(ELDLM_LOCK_ABORTED);
1425                         }
1426                         break;
1427                 case IT_GETATTR:
1428                 case IT_LOOKUP:
1429                 case IT_OPEN:
1430                 case IT_READDIR:
1431                 case IT_READLINK:
1432                 case IT_RENAME:
1433                 case IT_LINK:
1434                 case IT_SETATTR:
1435                         rc = mds_getattr_name(2, req);
1436                         /* FIXME: we need to sit down and decide on who should
1437                          * set req->rq_status, who should return negative and
1438                          * positive return values, and what they all mean. */
1439                         if (rc || req->rq_status != 0) {
1440                                 rep->lock_policy_res2 = req->rq_status;
1441                                 RETURN(ELDLM_LOCK_ABORTED);
1442                         }
1443                         break;
1444                 case IT_READDIR|IT_OPEN:
1445                         LBUG();
1446                         break;
1447                 default:
1448                         CERROR("Unhandled intent "LPD64"\n", it->opc);
1449                         LBUG();
1450                 }
1451
1452                 /* We don't bother returning a lock to the client for a file
1453                  * or directory we are removing.
1454                  *
1455                  * As for link and rename, there is no reason for the client
1456                  * to get a lock on the target at this point.  If they are
1457                  * going to modify the file/directory later they will get a
1458                  * lock at that time.
1459                  */
1460                 if (it->opc & (IT_UNLINK | IT_RMDIR | IT_LINK | IT_LINK2 |
1461                                IT_RENAME | IT_RENAME2))
1462                         RETURN(ELDLM_LOCK_ABORTED);
1463
1464                 rep->lock_policy_res2 = req->rq_status;
1465                 mds_rep = lustre_msg_buf(req->rq_repmsg, 1);
1466
1467                 /* If the client is about to open a file that doesn't have an MD
1468                  * stripe record, it's going to need a write lock. */
1469                 if (it->opc & IT_OPEN &&
1470                     !(lustre_msg_get_op_flags(req->rq_reqmsg)&MDS_OPEN_HAS_EA)){
1471                         LDLM_DEBUG(lock, "open with no EA; returning PW lock");
1472                         lock->l_req_mode = LCK_PW;
1473                 }
1474
1475                 if (flags & LDLM_FL_INTENT_ONLY) {
1476                         LDLM_DEBUG(lock, "INTENT_ONLY, aborting lock");
1477                         RETURN(ELDLM_LOCK_ABORTED);
1478                 }
1479                 /* Give the client a lock on the child object, instead of the
1480                  * parent that it requested. */
1481                 new_resid[0] = NTOH__u32(mds_rep->ino);
1482                 new_resid[1] = NTOH__u32(mds_rep->generation);
1483                 if (new_resid[0] == 0)
1484                         LBUG();
1485                 old_res = lock->l_resource->lr_name[0];
1486
1487                 ldlm_lock_change_resource(lock, new_resid);
1488                 if (lock->l_resource == NULL) {
1489                         LBUG();
1490                         RETURN(-ENOMEM);
1491                 }
1492                 LDLM_DEBUG(lock, "intent policy, old res %ld",
1493                            (long)old_res);
1494                 RETURN(ELDLM_LOCK_CHANGED);
1495         } else {
1496                 int size = sizeof(struct ldlm_reply);
1497                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
1498                                      &req->rq_repmsg);
1499                 if (rc) {
1500                         LBUG();
1501                         RETURN(-ENOMEM);
1502                 }
1503         }
1504         RETURN(rc);
1505 }
1506
1507 int mds_attach(struct obd_device *dev, obd_count len, void *data)
1508 {
1509         return lprocfs_reg_obd(dev, status_var_nm_1, dev);
1510 }
1511
1512 int mds_detach(struct obd_device *dev)
1513 {
1514         return lprocfs_dereg_obd(dev);
1515 }
1516
1517 static int mdt_setup(struct obd_device *obddev, obd_count len, void *buf)
1518 {
1519         int i;
1520         //        struct obd_ioctl_data* data = buf;
1521         struct mds_obd *mds = &obddev->u.mds;
1522         int rc = 0;
1523         ENTRY;
1524
1525         MOD_INC_USE_COUNT;
1526
1527         mds->mds_service = ptlrpc_init_svc(MDS_NEVENTS, MDS_NBUFS,
1528                                            MDS_BUFSIZE, MDS_MAXREQSIZE,
1529                                            MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
1530                                            "self", mds_handle, "mds");
1531         if (!mds->mds_service) {
1532                 CERROR("failed to start service\n");
1533                 GOTO(err_dec, rc = -EINVAL);
1534         }
1535
1536         for (i = 0; i < MDT_NUM_THREADS; i++) {
1537                 char name[32];
1538                 sprintf(name, "ll_mdt_%02d", i);
1539                 rc = ptlrpc_start_thread(obddev, mds->mds_service, name);
1540                 if (rc) {
1541                         CERROR("cannot start MDT thread #%d: rc %d\n", i, rc);
1542                         GOTO(err_thread, rc);
1543                 }
1544         }
1545
1546         RETURN(0);
1547
1548 err_thread:
1549         ptlrpc_stop_all_threads(mds->mds_service);
1550         ptlrpc_unregister_service(mds->mds_service);
1551 err_dec:
1552         MOD_DEC_USE_COUNT;
1553         RETURN(rc);
1554 }
1555
1556
1557 static int mdt_cleanup(struct obd_device *obddev)
1558 {
1559         struct mds_obd *mds = &obddev->u.mds;
1560         ENTRY;
1561
1562         ptlrpc_stop_all_threads(mds->mds_service);
1563         ptlrpc_unregister_service(mds->mds_service);
1564
1565         MOD_DEC_USE_COUNT;
1566         RETURN(0);
1567 }
1568
1569 extern int mds_iocontrol(long cmd, struct lustre_handle *conn,
1570                          int len, void *karg, void *uarg);
1571
1572 /* use obd ops to offer management infrastructure */
1573 static struct obd_ops mds_obd_ops = {
1574         o_attach:      mds_attach,
1575         o_detach:      mds_detach,
1576         o_connect:     mds_connect,
1577         o_disconnect:  mds_disconnect,
1578         o_setup:       mds_setup,
1579         o_cleanup:     mds_cleanup,
1580         o_iocontrol:   mds_iocontrol
1581 };
1582
1583 static struct obd_ops mdt_obd_ops = {
1584         o_setup:       mdt_setup,
1585         o_cleanup:     mdt_cleanup,
1586 };
1587
1588
1589 static int __init mds_init(void)
1590 {
1591
1592         mds_file_cache = kmem_cache_create("ll_mds_file_data",
1593                                            sizeof(struct mds_file_data),
1594                                            0, 0, NULL, NULL);
1595         if (mds_file_cache == NULL)
1596                 return -ENOMEM;
1597
1598         class_register_type(&mds_obd_ops, status_class_var, LUSTRE_MDS_NAME);
1599         class_register_type(&mdt_obd_ops, 0, LUSTRE_MDT_NAME);
1600         ldlm_register_intent(ldlm_intent_policy);
1601         return 0;
1602
1603 }
1604
1605 static void __exit mds_exit(void)
1606 {
1607
1608
1609         ldlm_unregister_intent();
1610         class_unregister_type(LUSTRE_MDS_NAME);
1611         class_unregister_type(LUSTRE_MDT_NAME);
1612         if (kmem_cache_destroy(mds_file_cache))
1613                 CERROR("couldn't free MDS file cache\n");
1614
1615 }
1616
1617 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
1618 MODULE_DESCRIPTION("Lustre Metadata Server (MDS) v0.01");
1619 MODULE_LICENSE("GPL");
1620
1621 module_init(mds_init);
1622 module_exit(mds_exit);