Whamcloud - gitweb
Code to update the last_rcvd file within a transaction.
[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 int mds_update_last_rcvd(struct mds_obd *mds, struct ptlrpc_request *req)
40 {
41         /* get from req->rq_connection-> or req->rq_client */
42         struct mds_client_info mci_data, *mci = &mci_data;
43         struct mds_client_data mcd_data, *mcd = &mcd_data;
44         loff_t off;
45         int rc;
46
47         /* Just a placeholder until more gets committed */
48         memset(mcd, 0, sizeof(*mcd));
49         mci->mci_mcd = mcd;
50         mci->mci_off = off = MDS_LR_CLIENT;
51
52         ++mds->mds_last_rcvd;   /* lock this, or make it an LDLM function? */
53         mci->mci_mcd->mcd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
54         mci->mci_mcd->mcd_mount_count = cpu_to_le64(mds->mds_mount_count);
55         rc = lustre_fwrite(mds->mds_rcvd_filp, (char *)mci->mci_mcd,
56                            sizeof(*mci->mci_mcd), &off);
57         CDEBUG(D_INODE, "wrote trans #%Ld for client '%s' at %Ld: rc = %d\n",
58                mds->mds_last_rcvd, mci->mci_mcd->mcd_uuid, mci->mci_off, rc);
59         // store new value and last committed value in req struct
60
61         if (rc == sizeof(mci->mci_mcd))
62                 rc = 0;
63         else if (rc >= 0)
64                 rc = -EIO;
65
66         return rc;
67 }
68
69 static int mds_reint_setattr(struct mds_update_record *rec,
70                              struct ptlrpc_request *req)
71 {
72         struct mds_obd *mds = &req->rq_obd->u.mds;
73         struct dentry *de;
74         void *handle;
75         int rc = 0;
76
77         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
78         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_SETATTR)) {
79                 GOTO(out_setattr, rc = -ESTALE);
80         }
81
82         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
83
84         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE,
85                        de->d_inode->i_sb->s_dev);
86
87         handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
88         if (!handle)
89                 GOTO(out_setattr_de, rc = PTR_ERR(handle));
90         rc = mds_fs_setattr(mds, de, handle, &rec->ur_iattr);
91
92         if (!rc)
93                 rc = mds_update_last_rcvd(mds, req);
94
95         EXIT;
96
97         /* FIXME: keep rc intact */
98         rc = mds_fs_commit(mds, de->d_inode, handle);
99 out_setattr_de:
100         l_dput(de);
101 out_setattr:
102         req->rq_status = rc;
103         return(0);
104 }
105
106 static int mds_reint_create(struct mds_update_record *rec,
107                             struct ptlrpc_request *req)
108 {
109         struct dentry *de = NULL;
110         struct mds_obd *mds = &req->rq_obd->u.mds;
111         struct dentry *dchild = NULL;
112         struct inode *dir;
113         void *handle;
114         int rc = 0, type = rec->ur_mode & S_IFMT;
115         ENTRY;
116
117         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
118         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
119                 LBUG();
120                 GOTO(out_create_de, rc = -ESTALE);
121         }
122         dir = de->d_inode;
123         CDEBUG(D_INODE, "ino %ld\n", dir->i_ino);
124
125         down(&dir->i_sem);
126         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
127         if (IS_ERR(dchild)) {
128                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
129                 up(&dir->i_sem);
130                 LBUG();
131                 GOTO(out_create_dchild, rc = -ESTALE);
132         }
133
134         if (dchild->d_inode) {
135                 CERROR("child exists (dir %ld, name %s)\n",
136                        dir->i_ino, rec->ur_name);
137                 LBUG();
138                 GOTO(out_create_dchild, rc = -EEXIST);
139         }
140
141         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb->s_dev);
142
143         switch (type) {
144         case S_IFREG: {
145                 handle = mds_fs_start(mds, dir, MDS_FSOP_CREATE);
146                 if (!handle)
147                         GOTO(out_create_dchild, PTR_ERR(handle));
148                 rc = vfs_create(dir, dchild, rec->ur_mode);
149                 EXIT;
150                 break;
151         }
152         case S_IFDIR: {
153                 handle = mds_fs_start(mds, dir, MDS_FSOP_MKDIR);
154                 if (!handle)
155                         GOTO(out_create_dchild, PTR_ERR(handle));
156                 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
157                 EXIT;
158                 break;
159         }
160         case S_IFLNK: {
161                 handle = mds_fs_start(mds, dir, MDS_FSOP_SYMLINK);
162                 if (!handle)
163                         GOTO(out_create_dchild, PTR_ERR(handle));
164                 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
165                 EXIT;
166                 break;
167         }
168         case S_IFCHR:
169         case S_IFBLK:
170         case S_IFIFO:
171         case S_IFSOCK: {
172                 int rdev = rec->ur_id;
173                 handle = mds_fs_start(mds, dir, MDS_FSOP_MKNOD);
174                 if (!handle)
175                         GOTO(out_create_dchild, PTR_ERR(handle));
176                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
177                 EXIT;
178                 break;
179         }
180         default:
181                 CERROR("bad file type %d for create of %s\n",type,rec->ur_name);
182                 GOTO(out_create_dchild, rc = -EINVAL);
183         }
184
185         if (rc) {
186                 CERROR("error during create: %d\n", rc);
187                 LBUG();
188                 GOTO(out_create_commit, rc);
189         } else {
190                 struct iattr iattr;
191                 struct inode *inode = dchild->d_inode;
192                 struct mds_body *body;
193
194                 if (type == S_IFREG) {
195                         rc = mds_fs_set_objid(mds, inode, handle, rec->ur_id);
196                         if (rc)
197                                 CERROR("error %d setting objid for %ld\n",
198                                        rc, inode->i_ino);
199                 }
200
201                 iattr.ia_atime = rec->ur_time;
202                 iattr.ia_ctime = rec->ur_time;
203                 iattr.ia_mtime = rec->ur_time;
204                 iattr.ia_uid = rec->ur_uid;
205                 iattr.ia_gid = rec->ur_gid;
206                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
207                         ATTR_MTIME | ATTR_CTIME;
208
209                 rc = mds_fs_setattr(mds, dchild, handle, &iattr);
210                 /* XXX should we abort here in case of error? */
211
212                 body = lustre_msg_buf(req->rq_repmsg, 0);
213                 body->ino = inode->i_ino;
214                 body->generation = inode->i_generation;
215         }
216
217         if (!rc)
218                 rc = mds_update_last_rcvd(mds, req);
219
220 out_create_commit:
221         /* FIXME: keep rc intact */
222         rc = mds_fs_commit(mds, dir, handle);
223 out_create_dchild:
224         l_dput(dchild);
225         up(&dir->i_sem);
226 out_create_de:
227         l_dput(de);
228         req->rq_status = rc;
229         return 0;
230 }
231
232 static int mds_reint_unlink(struct mds_update_record *rec,
233                             struct ptlrpc_request *req)
234 {
235         struct dentry *de = NULL;
236         struct dentry *dchild = NULL;
237         struct mds_obd *mds = &req->rq_obd->u.mds;
238         struct inode *dir, *inode;
239         void *handle;
240         int rc = 0;
241         ENTRY;
242
243         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
244         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK)) {
245                 LBUG();
246                 GOTO(out_unlink, rc = -ESTALE);
247         }
248         dir = de->d_inode;
249         CDEBUG(D_INODE, "ino %ld\n", dir->i_ino);
250
251         down(&dir->i_sem);
252         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
253         if (IS_ERR(dchild)) {
254                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
255                 LBUG();
256                 GOTO(out_unlink_de, rc = -ESTALE);
257         }
258
259         inode = dchild->d_inode;
260         if (!inode) {
261                 CERROR("child doesn't exist (dir %ld, name %s\n",
262                        dir->i_ino, rec->ur_name);
263                 LBUG();
264                 GOTO(out_unlink_dchild, rc = -ESTALE);
265         }
266
267         if (inode->i_ino != rec->ur_fid2->id) {
268                 CERROR("inode and FID ID do not match (%ld != %Ld)\n",
269                        inode->i_ino, rec->ur_fid2->id);
270                 LBUG();
271                 GOTO(out_unlink_dchild, rc = -ESTALE);
272         }
273         if (inode->i_generation != rec->ur_fid2->generation) {
274                 CERROR("inode and FID GENERATION do not match (%d != %d)\n",
275                        inode->i_generation, rec->ur_fid2->generation);
276                 LBUG();
277                 GOTO(out_unlink_dchild, rc = -ESTALE);
278         }
279
280         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dir->i_sb->s_dev);
281
282         switch (dchild->d_inode->i_mode & S_IFMT) {
283         case S_IFDIR:
284                 handle = mds_fs_start(mds, dir, MDS_FSOP_RMDIR);
285                 if (!handle)
286                         GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
287                 rc = vfs_rmdir(dir, dchild);
288                 break;
289         default:
290                 handle = mds_fs_start(mds, dir, MDS_FSOP_UNLINK);
291                 if (!handle)
292                         GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
293                 rc = vfs_unlink(dir, dchild);
294                 break;
295         }
296
297         if (!rc)
298                 rc = mds_update_last_rcvd(mds, req);
299         /* FIXME: keep rc intact */
300         rc = mds_fs_commit(mds, dir, handle);
301
302         EXIT;
303 out_unlink_dchild:
304         l_dput(dchild);
305 out_unlink_de:
306         up(&dir->i_sem);
307         l_dput(de);
308 out_unlink:
309         req->rq_status = rc;
310         return 0;
311 }
312
313 static int mds_reint_link(struct mds_update_record *rec,
314                             struct ptlrpc_request *req)
315 {
316         struct dentry *de_src = NULL;
317         struct dentry *de_tgt_dir = NULL;
318         struct dentry *dchild = NULL;
319         struct mds_obd *mds = &req->rq_obd->u.mds;
320         void *handle;
321         int rc = 0;
322
323         ENTRY;
324         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
325         if (IS_ERR(de_src) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK)) {
326                 GOTO(out_link, rc = -ESTALE);
327         }
328
329         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
330         if (IS_ERR(de_tgt_dir)) {
331                 GOTO(out_link_de_src, rc = -ESTALE);
332         }
333
334         down(&de_tgt_dir->d_inode->i_sem);
335         dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
336         if (IS_ERR(dchild)) {
337                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
338                 GOTO(out_link_de_tgt_dir, rc = -ESTALE);
339         }
340
341         if (dchild->d_inode) {
342                 CERROR("child exists (dir %ld, name %s\n",
343                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
344                 GOTO(out_link_dchild, rc = -EEXIST);
345         }
346
347         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE,
348                        dchild->d_inode->i_sb->s_dev);
349
350         handle = mds_fs_start(mds, de_tgt_dir->d_inode, MDS_FSOP_LINK);
351         if (!handle)
352                 GOTO(out_link_dchild, rc = PTR_ERR(handle));
353
354         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
355
356         if (!rc)
357                 rc = mds_update_last_rcvd(mds, req);
358
359         /* FIXME: keep rc intact */
360         rc = mds_fs_commit(mds, de_tgt_dir->d_inode, handle);
361         EXIT;
362
363 out_link_dchild:
364         l_dput(dchild);
365 out_link_de_tgt_dir:
366         up(&de_tgt_dir->d_inode->i_sem);
367         l_dput(de_tgt_dir);
368 out_link_de_src:
369         l_dput(de_src);
370 out_link:
371         req->rq_status = rc;
372         return 0;
373 }
374
375 static int mds_reint_rename(struct mds_update_record *rec,
376                             struct ptlrpc_request *req)
377 {
378         struct dentry *de_srcdir = NULL;
379         struct dentry *de_tgtdir = NULL;
380         struct dentry *de_old = NULL;
381         struct dentry *de_new = NULL;
382         struct mds_obd *mds = &req->rq_obd->u.mds;
383         void *handle;
384         int rc = 0;
385         ENTRY;
386
387         de_srcdir = mds_fid2dentry(mds, rec->ur_fid1, NULL);
388         if (IS_ERR(de_srcdir)) {
389                 GOTO(out_rename, rc = -ESTALE);
390         }
391
392         de_tgtdir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
393         if (IS_ERR(de_tgtdir)) {
394                 GOTO(out_rename_srcdir, rc = -ESTALE);
395         }
396
397         de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
398         if (IS_ERR(de_old)) {
399                 CERROR("old child lookup error %ld\n", PTR_ERR(de_old));
400                 GOTO(out_rename_tgtdir, rc = -ESTALE);
401         }
402
403         de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
404         if (IS_ERR(de_new)) {
405                 CERROR("new child lookup error %ld\n", PTR_ERR(de_new));
406                 GOTO(out_rename_deold, rc = -ESTALE);
407         }
408
409         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
410                        de_srcdir->d_inode->i_sb->s_dev);
411
412         handle = mds_fs_start(mds, de_tgtdir->d_inode, MDS_FSOP_RENAME);
413         if (!handle)
414                 GOTO(out_rename_denew, rc = PTR_ERR(handle));
415         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
416
417         if (!rc)
418                 rc = mds_update_last_rcvd(mds, req);
419
420         /* FIXME: keep rc intact */
421         rc = mds_fs_commit(mds, de_tgtdir->d_inode, handle);
422         EXIT;
423
424 out_rename_denew:
425         l_dput(de_new);
426 out_rename_deold:
427         l_dput(de_old);
428 out_rename_tgtdir:
429         l_dput(de_tgtdir);
430 out_rename_srcdir:
431         l_dput(de_srcdir);
432 out_rename:
433         req->rq_status = rc;
434         return 0;
435 }
436
437 typedef int (*mds_reinter)(struct mds_update_record *, struct ptlrpc_request*);
438
439 static mds_reinter reinters[REINT_MAX+1] = {
440         [REINT_SETATTR]   mds_reint_setattr,
441         [REINT_CREATE]    mds_reint_create,
442         [REINT_UNLINK]    mds_reint_unlink,
443         [REINT_LINK]      mds_reint_link,
444         [REINT_RENAME]    mds_reint_rename,
445 };
446
447 int mds_reint_rec(struct mds_update_record *rec, struct ptlrpc_request *req)
448 {
449         int rc, size = sizeof(struct mds_body);
450
451         if (rec->ur_opcode < 1 || rec->ur_opcode > REINT_MAX) {
452                 CERROR("opcode %d not valid\n", rec->ur_opcode);
453                 rc = req->rq_status = -EINVAL;
454                 RETURN(rc);
455         }
456
457         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
458         if (rc) {
459                 CERROR("mds: out of memory\n");
460                 rc = req->rq_status = -ENOMEM;
461                 RETURN(rc);
462         }
463
464         rc = reinters[rec->ur_opcode](rec, req);
465         return rc;
466 }