Whamcloud - gitweb
- mds failover code
[fs/lustre-release.git] / lustre / mds / mds_reint.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/mds/mds_reint.c
5  *
6  *  Lustre Metadata Server (mds) reintegration routines
7  *
8  *  Copyright (C) 2002  Cluster File Systems, Inc.
9  *  author: Peter Braam <braam@clusterfs.com>
10  *
11  *  This code is issued under the GNU General Public License.
12  *  See the file COPYING in this distribution
13  *
14  */
15
16 // XXX - add transaction sequence numbers
17
18 #define EXPORT_SYMTAB
19
20 #include <linux/version.h>
21 #include <linux/module.h>
22 #include <linux/fs.h>
23 #include <linux/stat.h>
24 #include <linux/locks.h>
25 #include <linux/quotaops.h>
26 #include <asm/unistd.h>
27 #include <asm/uaccess.h>
28
29 #define DEBUG_SUBSYSTEM S_MDS
30
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/obd.h>
34 #include <linux/lustre_lib.h>
35 #include <linux/lustre_idl.h>
36 #include <linux/lustre_mds.h>
37 #include <linux/obd_class.h>
38
39 struct mds_client_info *mds_uuid_to_mci(struct mds_obd *mds, __u8 *uuid)
40 {
41         struct list_head *p;
42
43         if (!uuid)
44                 return NULL;
45
46         list_for_each(p, &mds->mds_client_info) {
47                 struct mds_client_info *mci;
48
49                 mci = list_entry(p, struct mds_client_info, mci_list);
50                 CDEBUG(D_INFO, "checking client UUID '%s'\n",
51                        mci->mci_mcd->mcd_uuid);
52                 if (!strncmp(mci->mci_mcd->mcd_uuid, uuid,
53                              sizeof(mci->mci_mcd->mcd_uuid)))
54                         return mci;
55         }
56         CDEBUG(D_INFO, "no mds client info found for  UUID '%s'\n", uuid);
57         return NULL;
58 }
59
60 int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
61                          struct ptlrpc_request *req)
62 {
63         /* get from req->rq_connection-> or req->rq_client */
64         struct mds_client_info *mci;
65         loff_t off;
66         int rc;
67
68         mci = mds_uuid_to_mci(mds, req->rq_connection->c_remote_uuid);
69         if (!mci) {
70                 CERROR("unable to locate MDS client data for UUID '%s'\n",
71                        ptlrpc_req_to_uuid(req));
72                 /* This will be a real error once everything is working */
73                 //LBUG();
74                 RETURN(0);
75         }
76
77         off = MDS_LR_CLIENT + mci->mci_off * MDS_LR_SIZE;
78
79         ++mds->mds_last_rcvd;   /* lock this, or make it an LDLM function? */
80         req->rq_repmsg->transno = HTON__u64(mds->mds_last_rcvd);
81         mci->mci_mcd->mcd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
82         mci->mci_mcd->mcd_mount_count = cpu_to_le64(mds->mds_mount_count);
83         mci->mci_mcd->mcd_last_xid = cpu_to_le32(req->rq_reqmsg->xid);
84
85         mds_fs_set_last_rcvd(mds, handle);
86         rc = lustre_fwrite(mds->mds_rcvd_filp, (char *)mci->mci_mcd,
87                            sizeof(*mci->mci_mcd), &off);
88         CDEBUG(D_INODE, "wrote trans #%Ld for client '%s' at #%d: rc = %d\n",
89                mds->mds_last_rcvd, mci->mci_mcd->mcd_uuid, mci->mci_off, rc);
90         // store new value and last committed value in req struct
91
92         if (rc == sizeof(mci->mci_mcd))
93                 rc = 0;
94         else if (rc >= 0)
95                 rc = -EIO;
96
97         return rc;
98 }
99
100 static int mds_reint_setattr(struct mds_update_record *rec,
101                              struct ptlrpc_request *req)
102 {
103         struct mds_obd *mds = &req->rq_obd->u.mds;
104         struct dentry *de;
105         void *handle;
106         int rc = 0;
107
108         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
109         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_SETATTR)) {
110                 GOTO(out_setattr, rc = -ESTALE);
111         }
112
113         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
114
115         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE,
116                        de->d_inode->i_sb->s_dev);
117
118         handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
119         if (!handle)
120                 GOTO(out_setattr_de, rc = PTR_ERR(handle));
121         rc = mds_fs_setattr(mds, de, handle, &rec->ur_iattr);
122
123         if (!rc)
124                 rc = mds_update_last_rcvd(mds, handle, req);
125         /* FIXME: need to return last_rcvd, last_committed */
126
127         EXIT;
128
129         /* FIXME: keep rc intact */
130         rc = mds_fs_commit(mds, de->d_inode, handle);
131 out_setattr_de:
132         l_dput(de);
133 out_setattr:
134         req->rq_status = rc;
135         return(0);
136 }
137
138 static int mds_reint_recreate(struct mds_update_record *rec,
139                             struct ptlrpc_request *req)
140 {
141         struct dentry *de = NULL;
142         struct mds_obd *mds = &req->rq_obd->u.mds;
143         struct dentry *dchild = NULL;
144         struct inode *dir;
145         int rc = 0;
146         ENTRY;
147
148         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
149         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
150                 LBUG();
151                 GOTO(out_create_de, rc = -ESTALE);
152         }
153         dir = de->d_inode;
154         CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
155
156         down(&dir->i_sem);
157         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
158         if (IS_ERR(dchild)) {
159                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
160                 up(&dir->i_sem);
161                 LBUG();
162                 GOTO(out_create_dchild, rc = -ESTALE);
163         }
164
165         if (dchild->d_inode) {
166                 struct mds_body *body;
167                 rc = 0;
168                 body = lustre_msg_buf(req->rq_repmsg, 0);
169                 body->ino = dchild->d_inode->i_ino;
170                 body->generation = dchild->d_inode->i_generation;
171         } else { 
172                 CERROR("child doesn't exist (dir %ld, name %s)\n",
173                        dir->i_ino, rec->ur_name);
174                 rc = -ENOENT;
175                 LBUG();
176         }
177
178 out_create_dchild:
179         l_dput(dchild);
180         up(&dir->i_sem);
181 out_create_de:
182         l_dput(de);
183         req->rq_status = rc;
184         return 0;
185 }
186 static int mds_reint_create(struct mds_update_record *rec,
187                             struct ptlrpc_request *req)
188 {
189         struct dentry *de = NULL;
190         struct mds_obd *mds = &req->rq_obd->u.mds;
191         struct dentry *dchild = NULL;
192         struct inode *dir;
193         void *handle;
194         int rc = 0, type = rec->ur_mode & S_IFMT;
195         ENTRY;
196
197         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
198         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
199                 LBUG();
200                 GOTO(out_create_de, rc = -ESTALE);
201         }
202         dir = de->d_inode;
203         CDEBUG(D_INODE, "parent ino %ld name %s mode %o\n", 
204                dir->i_ino, rec->ur_name, rec->ur_mode);
205
206         down(&dir->i_sem);
207         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
208         if (IS_ERR(dchild)) {
209                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
210                 up(&dir->i_sem);
211                 LBUG();
212                 GOTO(out_create_dchild, rc = -ESTALE);
213         }
214
215         if (dchild->d_inode) {
216                 CERROR("child exists (dir %ld, name %s, ino %ld)\n",
217                        dir->i_ino, rec->ur_name, dchild->d_inode->i_ino);
218                 LBUG();
219                 GOTO(out_create_dchild, rc = -EEXIST);
220         }
221
222         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb->s_dev);
223
224         switch (type) {
225         case S_IFREG: {
226                 handle = mds_fs_start(mds, dir, MDS_FSOP_CREATE);
227                 if (!handle)
228                         GOTO(out_create_dchild, PTR_ERR(handle));
229                 rc = vfs_create(dir, dchild, rec->ur_mode);
230                 EXIT;
231                 break;
232         }
233         case S_IFDIR: {
234                 handle = mds_fs_start(mds, dir, MDS_FSOP_MKDIR);
235                 if (!handle)
236                         GOTO(out_create_dchild, PTR_ERR(handle));
237                 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
238                 EXIT;
239                 break;
240         }
241         case S_IFLNK: {
242                 handle = mds_fs_start(mds, dir, MDS_FSOP_SYMLINK);
243                 if (!handle)
244                         GOTO(out_create_dchild, PTR_ERR(handle));
245                 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
246                 EXIT;
247                 break;
248         }
249         case S_IFCHR:
250         case S_IFBLK:
251         case S_IFIFO:
252         case S_IFSOCK: {
253                 int rdev = rec->ur_id;
254                 handle = mds_fs_start(mds, dir, MDS_FSOP_MKNOD);
255                 if (!handle)
256                         GOTO(out_create_dchild, PTR_ERR(handle));
257                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
258                 EXIT;
259                 break;
260         }
261         default:
262                 CERROR("bad file type %d for create of %s\n",type,rec->ur_name);
263                 GOTO(out_create_dchild, rc = -EINVAL);
264         }
265
266         if (rc) {
267                 CERROR("error during create: %d\n", rc);
268                 LBUG();
269                 GOTO(out_create_commit, rc);
270         } else {
271                 struct iattr iattr;
272                 struct inode *inode = dchild->d_inode;
273                 struct mds_body *body;
274
275                 CDEBUG(D_INODE, "created ino %ld\n", dchild->d_inode->i_ino);
276                 if (type == S_IFREG) {
277                         rc = mds_fs_set_objid(mds, inode, handle, rec->ur_id);
278                         if (rc)
279                                 CERROR("error %d setting objid for %ld\n",
280                                        rc, inode->i_ino);
281                 }
282
283                 iattr.ia_atime = rec->ur_time;
284                 iattr.ia_ctime = rec->ur_time;
285                 iattr.ia_mtime = rec->ur_time;
286                 iattr.ia_uid = rec->ur_uid;
287                 iattr.ia_gid = rec->ur_gid;
288                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
289                         ATTR_MTIME | ATTR_CTIME;
290
291                 rc = mds_fs_setattr(mds, dchild, handle, &iattr);
292                 /* XXX should we abort here in case of error? */
293
294                 //if (!rc)
295                 rc = mds_update_last_rcvd(mds, handle, req);
296
297                 body = lustre_msg_buf(req->rq_repmsg, 0);
298                 body->ino = inode->i_ino;
299                 body->generation = inode->i_generation;
300         }
301
302 out_create_commit:
303         /* FIXME: keep rc intact */
304         rc = mds_fs_commit(mds, dir, handle);
305 out_create_dchild:
306         l_dput(dchild);
307         up(&dir->i_sem);
308 out_create_de:
309         l_dput(de);
310         req->rq_status = rc;
311         return 0;
312 }
313
314 static int mds_reint_unlink(struct mds_update_record *rec,
315                             struct ptlrpc_request *req)
316 {
317         struct dentry *de = NULL;
318         struct dentry *dchild = NULL;
319         struct mds_obd *mds = &req->rq_obd->u.mds;
320         struct inode *dir, *inode;
321         void *handle;
322         int rc = 0;
323         ENTRY;
324
325         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
326         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK)) {
327                 LBUG();
328                 GOTO(out_unlink, rc = -ESTALE);
329         }
330         dir = de->d_inode;
331         CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
332
333         down(&dir->i_sem);
334         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
335         if (IS_ERR(dchild)) {
336                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
337                 LBUG();
338                 GOTO(out_unlink_de, rc = -ESTALE);
339         }
340
341         inode = dchild->d_inode;
342         if (!inode) {
343                 CERROR("child doesn't exist (dir %ld, name %s\n",
344                        dir->i_ino, rec->ur_name);
345                 LBUG();
346                 GOTO(out_unlink_dchild, rc = -ESTALE);
347         }
348
349         if (inode->i_ino != rec->ur_fid2->id) {
350                 CERROR("inode and FID ID do not match (%ld != %Ld)\n",
351                        inode->i_ino, rec->ur_fid2->id);
352                 LBUG();
353                 GOTO(out_unlink_dchild, rc = -ESTALE);
354         }
355         if (inode->i_generation != rec->ur_fid2->generation) {
356                 CERROR("inode and FID GENERATION do not match (%d != %d)\n",
357                        inode->i_generation, rec->ur_fid2->generation);
358                 LBUG();
359                 GOTO(out_unlink_dchild, rc = -ESTALE);
360         }
361
362         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dir->i_sb->s_dev);
363
364         switch (dchild->d_inode->i_mode & S_IFMT) {
365         case S_IFDIR:
366                 handle = mds_fs_start(mds, dir, MDS_FSOP_RMDIR);
367                 if (!handle)
368                         GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
369                 rc = vfs_rmdir(dir, dchild);
370                 break;
371         default:
372                 handle = mds_fs_start(mds, dir, MDS_FSOP_UNLINK);
373                 if (!handle)
374                         GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
375                 rc = vfs_unlink(dir, dchild);
376                 break;
377         }
378
379         if (!rc)
380                 rc = mds_update_last_rcvd(mds, handle, req);
381         /* FIXME: need to return last_rcvd, last_committed */
382         /* FIXME: keep rc intact */
383         rc = mds_fs_commit(mds, dir, handle);
384
385         EXIT;
386 out_unlink_dchild:
387         l_dput(dchild);
388 out_unlink_de:
389         up(&dir->i_sem);
390         l_dput(de);
391 out_unlink:
392         req->rq_status = rc;
393         return 0;
394 }
395
396 static int mds_reint_link(struct mds_update_record *rec,
397                             struct ptlrpc_request *req)
398 {
399         struct dentry *de_src = NULL;
400         struct dentry *de_tgt_dir = NULL;
401         struct dentry *dchild = NULL;
402         struct mds_obd *mds = &req->rq_obd->u.mds;
403         void *handle;
404         int rc = 0;
405
406         ENTRY;
407         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
408         if (IS_ERR(de_src) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK)) {
409                 GOTO(out_link, rc = -ESTALE);
410         }
411
412         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
413         if (IS_ERR(de_tgt_dir)) {
414                 GOTO(out_link_de_src, rc = -ESTALE);
415         }
416
417         down(&de_tgt_dir->d_inode->i_sem);
418         dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
419         if (IS_ERR(dchild)) {
420                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
421                 GOTO(out_link_de_tgt_dir, rc = -ESTALE);
422         }
423
424         if (dchild->d_inode) {
425                 CERROR("child exists (dir %ld, name %s\n",
426                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
427                 GOTO(out_link_dchild, rc = -EEXIST);
428         }
429
430         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE,
431                        dchild->d_inode->i_sb->s_dev);
432
433         handle = mds_fs_start(mds, de_tgt_dir->d_inode, MDS_FSOP_LINK);
434         if (!handle)
435                 GOTO(out_link_dchild, rc = PTR_ERR(handle));
436
437         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
438
439         if (!rc)
440                 rc = mds_update_last_rcvd(mds, handle, req);
441
442         /* FIXME: need to return last_rcvd, last_committed */
443         /* FIXME: keep rc intact */
444         rc = mds_fs_commit(mds, de_tgt_dir->d_inode, handle);
445         EXIT;
446
447 out_link_dchild:
448         l_dput(dchild);
449 out_link_de_tgt_dir:
450         up(&de_tgt_dir->d_inode->i_sem);
451         l_dput(de_tgt_dir);
452 out_link_de_src:
453         l_dput(de_src);
454 out_link:
455         req->rq_status = rc;
456         return 0;
457 }
458
459 static int mds_reint_rename(struct mds_update_record *rec,
460                             struct ptlrpc_request *req)
461 {
462         struct dentry *de_srcdir = NULL;
463         struct dentry *de_tgtdir = NULL;
464         struct dentry *de_old = NULL;
465         struct dentry *de_new = NULL;
466         struct mds_obd *mds = &req->rq_obd->u.mds;
467         void *handle;
468         int rc = 0;
469         ENTRY;
470
471         de_srcdir = mds_fid2dentry(mds, rec->ur_fid1, NULL);
472         if (IS_ERR(de_srcdir)) {
473                 GOTO(out_rename, rc = -ESTALE);
474         }
475
476         de_tgtdir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
477         if (IS_ERR(de_tgtdir)) {
478                 GOTO(out_rename_srcdir, rc = -ESTALE);
479         }
480
481         de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
482         if (IS_ERR(de_old)) {
483                 CERROR("old child lookup error %ld\n", PTR_ERR(de_old));
484                 GOTO(out_rename_tgtdir, rc = -ESTALE);
485         }
486
487         de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
488         if (IS_ERR(de_new)) {
489                 CERROR("new child lookup error %ld\n", PTR_ERR(de_new));
490                 GOTO(out_rename_deold, rc = -ESTALE);
491         }
492
493         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
494                        de_srcdir->d_inode->i_sb->s_dev);
495
496         handle = mds_fs_start(mds, de_tgtdir->d_inode, MDS_FSOP_RENAME);
497         if (!handle)
498                 GOTO(out_rename_denew, rc = PTR_ERR(handle));
499         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
500
501         if (!rc)
502                 rc = mds_update_last_rcvd(mds, handle, req);
503
504         /* FIXME: need to return last_rcvd, last_committed */
505         /* FIXME: keep rc intact */
506         rc = mds_fs_commit(mds, de_tgtdir->d_inode, handle);
507         EXIT;
508
509 out_rename_denew:
510         l_dput(de_new);
511 out_rename_deold:
512         l_dput(de_old);
513 out_rename_tgtdir:
514         l_dput(de_tgtdir);
515 out_rename_srcdir:
516         l_dput(de_srcdir);
517 out_rename:
518         req->rq_status = rc;
519         return 0;
520 }
521
522 typedef int (*mds_reinter)(struct mds_update_record *, struct ptlrpc_request*);
523
524 static mds_reinter reinters[REINT_MAX+1] = {
525         [REINT_SETATTR]   mds_reint_setattr,
526         [REINT_CREATE]    mds_reint_create,
527         [REINT_UNLINK]    mds_reint_unlink,
528         [REINT_LINK]      mds_reint_link,
529         [REINT_RENAME]    mds_reint_rename,
530         [REINT_RECREATE]  mds_reint_recreate,
531 };
532
533 int mds_reint_rec(struct mds_update_record *rec, struct ptlrpc_request *req)
534 {
535         int rc, size = sizeof(struct mds_body);
536
537         if (rec->ur_opcode < 1 || rec->ur_opcode > REINT_MAX) {
538                 CERROR("opcode %d not valid\n", rec->ur_opcode);
539                 rc = req->rq_status = -EINVAL;
540                 RETURN(rc);
541         }
542
543         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
544         if (rc) {
545                 CERROR("mds: out of memory\n");
546                 rc = req->rq_status = -ENOMEM;
547                 RETURN(rc);
548         }
549
550         rc = reinters[rec->ur_opcode](rec, req);
551
552         return rc;
553 }