Whamcloud - gitweb
Don't override MDS return code with transaction close code on error.
[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 obd_run_ctxt saved;
65         struct mds_client_info *mci;
66         loff_t off;
67         int rc;
68
69         mci = mds_uuid_to_mci(mds, req->rq_connection->c_remote_uuid);
70         if (!mci) {
71                 CERROR("unable to locate MDS client data for UUID '%s'\n",
72                        ptlrpc_req_to_uuid(req));
73                 /* This will be a real error once everything is working */
74                 //LBUG();
75                 RETURN(0);
76         }
77
78         off = MDS_LR_CLIENT + mci->mci_off * MDS_LR_SIZE;
79
80         ++mds->mds_last_rcvd;   /* lock this, or make it an LDLM function? */
81         req->rq_repmsg->transno = HTON__u64(mds->mds_last_rcvd);
82         mci->mci_mcd->mcd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
83         mci->mci_mcd->mcd_mount_count = cpu_to_le64(mds->mds_mount_count);
84         mci->mci_mcd->mcd_last_xid = cpu_to_le32(req->rq_reqmsg->xid);
85
86         mds_fs_set_last_rcvd(mds, handle);
87         push_ctxt(&saved, &mds->mds_ctxt);
88         rc = lustre_fwrite(mds->mds_rcvd_filp, (char *)mci->mci_mcd,
89                            sizeof(*mci->mci_mcd), &off);
90         pop_ctxt(&saved);
91         CDEBUG(D_INODE, "wrote trans #%Ld for client '%s' at #%d: rc = %d\n",
92                mds->mds_last_rcvd, mci->mci_mcd->mcd_uuid, mci->mci_off, rc);
93         // store new value and last committed value in req struct
94
95         if (rc == sizeof(*mci->mci_mcd))
96                 rc = 0;
97         else {
98                 CERROR("error writing to last_rcvd file: rc = %d\n", rc);
99                 if (rc >= 0)
100                         rc = -EIO;
101         }
102
103         return rc;
104 }
105
106 static int mds_reint_setattr(struct mds_update_record *rec,
107                              struct ptlrpc_request *req)
108 {
109         struct mds_obd *mds = &req->rq_obd->u.mds;
110         struct dentry *de;
111         void *handle;
112         int rc = 0;
113         int err;
114
115         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
116         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_SETATTR)) {
117                 GOTO(out_setattr, rc = -ESTALE);
118         }
119
120         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
121
122         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE,
123                        de->d_inode->i_sb->s_dev);
124
125         handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
126         if (!handle)
127                 GOTO(out_setattr_de, rc = PTR_ERR(handle));
128         rc = mds_fs_setattr(mds, de, handle, &rec->ur_iattr);
129
130         if (!rc)
131                 rc = mds_update_last_rcvd(mds, handle, req);
132
133         err = mds_fs_commit(mds, de->d_inode, handle);
134         if (err) {
135                 CERROR("error on commit: err = %d\n", err);
136                 if (!rc)
137                         rc = err;
138         }
139         EXIT;
140 out_setattr_de:
141         l_dput(de);
142 out_setattr:
143         req->rq_status = rc;
144         return(0);
145 }
146
147 static int mds_reint_recreate(struct mds_update_record *rec,
148                             struct ptlrpc_request *req)
149 {
150         struct dentry *de = NULL;
151         struct mds_obd *mds = &req->rq_obd->u.mds;
152         struct dentry *dchild = NULL;
153         struct inode *dir;
154         int rc = 0;
155         ENTRY;
156
157         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
158         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
159                 LBUG();
160                 GOTO(out_create_de, rc = -ESTALE);
161         }
162         dir = de->d_inode;
163         CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
164
165         down(&dir->i_sem);
166         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
167         if (IS_ERR(dchild)) {
168                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
169                 up(&dir->i_sem);
170                 LBUG();
171                 GOTO(out_create_dchild, rc = -ESTALE);
172         }
173
174         if (dchild->d_inode) {
175                 struct mds_body *body;
176                 rc = 0;
177                 body = lustre_msg_buf(req->rq_repmsg, 0);
178                 body->ino = dchild->d_inode->i_ino;
179                 body->generation = dchild->d_inode->i_generation;
180         } else { 
181                 CERROR("child doesn't exist (dir %ld, name %s)\n",
182                        dir->i_ino, rec->ur_name);
183                 rc = -ENOENT;
184                 LBUG();
185         }
186
187 out_create_dchild:
188         l_dput(dchild);
189         up(&dir->i_sem);
190 out_create_de:
191         l_dput(de);
192         req->rq_status = rc;
193         return 0;
194 }
195
196 static int mds_reint_create(struct mds_update_record *rec,
197                             struct ptlrpc_request *req)
198 {
199         struct dentry *de = NULL;
200         struct mds_obd *mds = &req->rq_obd->u.mds;
201         struct dentry *dchild = NULL;
202         struct inode *dir;
203         void *handle;
204         int rc = 0, type = rec->ur_mode & S_IFMT;
205         int err;
206         ENTRY;
207
208         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
209         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
210                 LBUG();
211                 GOTO(out_create_de, rc = -ESTALE);
212         }
213         dir = de->d_inode;
214         CDEBUG(D_INODE, "parent ino %ld name %s mode %o\n", 
215                dir->i_ino, rec->ur_name, rec->ur_mode);
216
217         down(&dir->i_sem);
218         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
219         if (IS_ERR(dchild)) {
220                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
221                 up(&dir->i_sem);
222                 LBUG();
223                 GOTO(out_create_dchild, rc = -ESTALE);
224         }
225
226         if (dchild->d_inode) {
227                 CERROR("child exists (dir %ld, name %s, ino %ld)\n",
228                        dir->i_ino, rec->ur_name, dchild->d_inode->i_ino);
229                 LBUG();
230                 GOTO(out_create_dchild, rc = -EEXIST);
231         }
232
233         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb->s_dev);
234
235         switch (type) {
236         case S_IFREG: {
237                 handle = mds_fs_start(mds, dir, MDS_FSOP_CREATE);
238                 if (!handle)
239                         GOTO(out_create_dchild, PTR_ERR(handle));
240                 rc = vfs_create(dir, dchild, rec->ur_mode);
241                 EXIT;
242                 break;
243         }
244         case S_IFDIR: {
245                 handle = mds_fs_start(mds, dir, MDS_FSOP_MKDIR);
246                 if (!handle)
247                         GOTO(out_create_dchild, PTR_ERR(handle));
248                 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
249                 EXIT;
250                 break;
251         }
252         case S_IFLNK: {
253                 handle = mds_fs_start(mds, dir, MDS_FSOP_SYMLINK);
254                 if (!handle)
255                         GOTO(out_create_dchild, PTR_ERR(handle));
256                 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
257                 EXIT;
258                 break;
259         }
260         case S_IFCHR:
261         case S_IFBLK:
262         case S_IFIFO:
263         case S_IFSOCK: {
264                 int rdev = rec->ur_id;
265                 handle = mds_fs_start(mds, dir, MDS_FSOP_MKNOD);
266                 if (!handle)
267                         GOTO(out_create_dchild, PTR_ERR(handle));
268                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
269                 EXIT;
270                 break;
271         }
272         default:
273                 CERROR("bad file type %d for create of %s\n",type,rec->ur_name);
274                 GOTO(out_create_dchild, rc = -EINVAL);
275         }
276
277         if (rc) {
278                 CERROR("error during create: %d\n", rc);
279                 if (rc != -ENOSPC) LBUG();
280                 GOTO(out_create_commit, rc);
281         } else {
282                 struct iattr iattr;
283                 struct inode *inode = dchild->d_inode;
284                 struct mds_body *body;
285
286                 CDEBUG(D_INODE, "created ino %ld\n", dchild->d_inode->i_ino);
287                 if (type == S_IFREG) {
288                         rc = mds_fs_set_objid(mds, inode, handle, rec->ur_id);
289                         if (rc)
290                                 CERROR("error %d setting objid for %ld\n",
291                                        rc, inode->i_ino);
292                 }
293
294                 iattr.ia_atime = rec->ur_time;
295                 iattr.ia_ctime = rec->ur_time;
296                 iattr.ia_mtime = rec->ur_time;
297                 iattr.ia_uid = rec->ur_uid;
298                 iattr.ia_gid = rec->ur_gid;
299                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
300                         ATTR_MTIME | ATTR_CTIME;
301
302                 rc = mds_fs_setattr(mds, dchild, handle, &iattr);
303                 if (rc) {
304                         CERROR("error on setattr: rc = %d\n", rc);
305                         /* XXX should we abort here in case of error? */
306                 }
307
308                 rc = mds_update_last_rcvd(mds, handle, req);
309                 if (rc) {
310                         CERROR("error on update_last_rcvd: rc = %d\n", rc);
311                         /* XXX should we abort here in case of error? */
312                 }
313
314                 body = lustre_msg_buf(req->rq_repmsg, 0);
315                 body->ino = inode->i_ino;
316                 body->generation = inode->i_generation;
317         }
318
319 out_create_commit:
320         err = mds_fs_commit(mds, dir, handle);
321         if (err) {
322                 CERROR("error on commit: err = %d\n", err);
323                 if (!rc)
324                         rc = err;
325         }
326 out_create_dchild:
327         l_dput(dchild);
328         up(&dir->i_sem);
329 out_create_de:
330         l_dput(de);
331         req->rq_status = rc;
332         return 0;
333 }
334
335 static int mds_reint_unlink(struct mds_update_record *rec,
336                             struct ptlrpc_request *req)
337 {
338         struct dentry *de = NULL;
339         struct dentry *dchild = NULL;
340         struct mds_obd *mds = &req->rq_obd->u.mds;
341         struct inode *dir, *inode;
342         void *handle;
343         int rc = 0;
344         int err;
345         ENTRY;
346
347         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
348         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK)) {
349                 LBUG();
350                 GOTO(out_unlink, rc = -ESTALE);
351         }
352         dir = de->d_inode;
353         CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
354
355         down(&dir->i_sem);
356         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
357         if (IS_ERR(dchild)) {
358                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
359                 LBUG();
360                 GOTO(out_unlink_de, rc = -ESTALE);
361         }
362
363         inode = dchild->d_inode;
364         if (!inode) {
365                 CERROR("child doesn't exist (dir %ld, name %s\n",
366                        dir->i_ino, rec->ur_name);
367                 LBUG();
368                 GOTO(out_unlink_dchild, rc = -ESTALE);
369         }
370
371         if (inode->i_ino != rec->ur_fid2->id) {
372                 CERROR("inode and FID ID do not match (%ld != %Ld)\n",
373                        inode->i_ino, rec->ur_fid2->id);
374                 LBUG();
375                 GOTO(out_unlink_dchild, rc = -ESTALE);
376         }
377         if (inode->i_generation != rec->ur_fid2->generation) {
378                 CERROR("inode and FID GENERATION do not match (%d != %d)\n",
379                        inode->i_generation, rec->ur_fid2->generation);
380                 LBUG();
381                 GOTO(out_unlink_dchild, rc = -ESTALE);
382         }
383
384         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dir->i_sb->s_dev);
385
386         switch (dchild->d_inode->i_mode & S_IFMT) {
387         case S_IFDIR:
388                 handle = mds_fs_start(mds, dir, MDS_FSOP_RMDIR);
389                 if (!handle)
390                         GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
391                 rc = vfs_rmdir(dir, dchild);
392                 break;
393         default:
394                 handle = mds_fs_start(mds, dir, MDS_FSOP_UNLINK);
395                 if (!handle)
396                         GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
397                 rc = vfs_unlink(dir, dchild);
398                 break;
399         }
400
401         if (!rc)
402                 rc = mds_update_last_rcvd(mds, handle, req);
403         err = mds_fs_commit(mds, dir, handle);
404         if (err) {
405                 CERROR("error on commit: err = %d\n", err);
406                 if (!rc)
407                         rc = err;
408         }
409
410         EXIT;
411 out_unlink_dchild:
412         l_dput(dchild);
413 out_unlink_de:
414         up(&dir->i_sem);
415         l_dput(de);
416 out_unlink:
417         req->rq_status = rc;
418         return 0;
419 }
420
421 static int mds_reint_link(struct mds_update_record *rec,
422                             struct ptlrpc_request *req)
423 {
424         struct dentry *de_src = NULL;
425         struct dentry *de_tgt_dir = NULL;
426         struct dentry *dchild = NULL;
427         struct mds_obd *mds = &req->rq_obd->u.mds;
428         void *handle;
429         int rc = 0;
430         int err;
431
432         ENTRY;
433         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
434         if (IS_ERR(de_src) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK)) {
435                 GOTO(out_link, rc = -ESTALE);
436         }
437
438         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
439         if (IS_ERR(de_tgt_dir)) {
440                 GOTO(out_link_de_src, rc = -ESTALE);
441         }
442
443         down(&de_tgt_dir->d_inode->i_sem);
444         dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
445         if (IS_ERR(dchild)) {
446                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
447                 GOTO(out_link_de_tgt_dir, rc = -ESTALE);
448         }
449
450         if (dchild->d_inode) {
451                 CERROR("child exists (dir %ld, name %s\n",
452                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
453                 GOTO(out_link_dchild, rc = -EEXIST);
454         }
455
456         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE,
457                        dchild->d_inode->i_sb->s_dev);
458
459         handle = mds_fs_start(mds, de_tgt_dir->d_inode, MDS_FSOP_LINK);
460         if (!handle)
461                 GOTO(out_link_dchild, rc = PTR_ERR(handle));
462
463         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
464
465         if (!rc)
466                 rc = mds_update_last_rcvd(mds, handle, req);
467
468         err = mds_fs_commit(mds, de_tgt_dir->d_inode, handle);
469         if (err) {
470                 CERROR("error on commit: err = %d\n", err);
471                 if (!rc)
472                         rc = err;
473         }
474         EXIT;
475
476 out_link_dchild:
477         l_dput(dchild);
478 out_link_de_tgt_dir:
479         up(&de_tgt_dir->d_inode->i_sem);
480         l_dput(de_tgt_dir);
481 out_link_de_src:
482         l_dput(de_src);
483 out_link:
484         req->rq_status = rc;
485         return 0;
486 }
487
488 static int mds_reint_rename(struct mds_update_record *rec,
489                             struct ptlrpc_request *req)
490 {
491         struct dentry *de_srcdir = NULL;
492         struct dentry *de_tgtdir = NULL;
493         struct dentry *de_old = NULL;
494         struct dentry *de_new = NULL;
495         struct mds_obd *mds = &req->rq_obd->u.mds;
496         void *handle;
497         int rc = 0;
498         int err;
499         ENTRY;
500
501         de_srcdir = mds_fid2dentry(mds, rec->ur_fid1, NULL);
502         if (IS_ERR(de_srcdir)) {
503                 GOTO(out_rename, rc = -ESTALE);
504         }
505
506         de_tgtdir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
507         if (IS_ERR(de_tgtdir)) {
508                 GOTO(out_rename_srcdir, rc = -ESTALE);
509         }
510
511         de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
512         if (IS_ERR(de_old)) {
513                 CERROR("old child lookup error %ld\n", PTR_ERR(de_old));
514                 GOTO(out_rename_tgtdir, rc = -ESTALE);
515         }
516
517         de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
518         if (IS_ERR(de_new)) {
519                 CERROR("new child lookup error %ld\n", PTR_ERR(de_new));
520                 GOTO(out_rename_deold, rc = -ESTALE);
521         }
522
523         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
524                        de_srcdir->d_inode->i_sb->s_dev);
525
526         handle = mds_fs_start(mds, de_tgtdir->d_inode, MDS_FSOP_RENAME);
527         if (!handle)
528                 GOTO(out_rename_denew, rc = PTR_ERR(handle));
529         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
530
531         if (!rc)
532                 rc = mds_update_last_rcvd(mds, handle, req);
533
534         err = mds_fs_commit(mds, de_tgtdir->d_inode, handle);
535         if (err) {
536                 CERROR("error on commit: err = %d\n", err);
537                 if (!rc)
538                         rc = err;
539         }
540         EXIT;
541
542 out_rename_denew:
543         l_dput(de_new);
544 out_rename_deold:
545         l_dput(de_old);
546 out_rename_tgtdir:
547         l_dput(de_tgtdir);
548 out_rename_srcdir:
549         l_dput(de_srcdir);
550 out_rename:
551         req->rq_status = rc;
552         return 0;
553 }
554
555 typedef int (*mds_reinter)(struct mds_update_record *, struct ptlrpc_request*);
556
557 static mds_reinter reinters[REINT_MAX+1] = {
558         [REINT_SETATTR]   mds_reint_setattr,
559         [REINT_CREATE]    mds_reint_create,
560         [REINT_UNLINK]    mds_reint_unlink,
561         [REINT_LINK]      mds_reint_link,
562         [REINT_RENAME]    mds_reint_rename,
563         [REINT_RECREATE]  mds_reint_recreate,
564 };
565
566 int mds_reint_rec(struct mds_update_record *rec, struct ptlrpc_request *req)
567 {
568         int rc, size = sizeof(struct mds_body);
569
570         if (rec->ur_opcode < 1 || rec->ur_opcode > REINT_MAX) {
571                 CERROR("opcode %d not valid\n", rec->ur_opcode);
572                 rc = req->rq_status = -EINVAL;
573                 RETURN(rc);
574         }
575
576         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
577         if (rc) {
578                 CERROR("mds: out of memory\n");
579                 rc = req->rq_status = -ENOMEM;
580                 RETURN(rc);
581         }
582
583         rc = reinters[rec->ur_opcode](rec, req);
584
585         return rc;
586 }