Whamcloud - gitweb
Commit minor cleanups to reduce size of outstanding changes in my tree.
[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 static int mds_reint_setattr(struct mds_update_record *rec,
40                              struct ptlrpc_request *req)
41 {
42         struct mds_obd *mds = &req->rq_obd->u.mds;
43         struct dentry *de;
44
45         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
46         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_SETATTR)) {
47                 req->rq_status = -ESTALE;
48                 RETURN(0);
49         }
50
51         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
52
53         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE,
54                        de->d_inode->i_sb->s_dev);
55
56         req->rq_status = mds_fs_setattr(mds, de, NULL, &rec->ur_iattr);
57
58         l_dput(de);
59         RETURN(0);
60 }
61
62 static int mds_reint_create(struct mds_update_record *rec,
63                             struct ptlrpc_request *req)
64 {
65         struct dentry *de = NULL;
66         struct mds_obd *mds = &req->rq_obd->u.mds;
67         struct dentry *dchild = NULL;
68         struct inode *dir;
69         void *handle;
70         int rc = 0, type = rec->ur_mode & S_IFMT;
71         ENTRY;
72
73         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
74         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
75                 LBUG();
76                 GOTO(out_create_de, rc = -ESTALE);
77         }
78         dir = de->d_inode;
79         CDEBUG(D_INODE, "ino %ld\n", dir->i_ino);
80
81         down(&dir->i_sem);
82         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
83         if (IS_ERR(dchild)) {
84                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
85                 up(&dir->i_sem);
86                 LBUG();
87                 GOTO(out_create_dchild, rc = -ESTALE);
88         }
89
90         if (dchild->d_inode) {
91                 CERROR("child exists (dir %ld, name %s)\n",
92                        dir->i_ino, rec->ur_name);
93                 LBUG();
94                 GOTO(out_create_dchild, rc = -EEXIST);
95         }
96
97         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb->s_dev);
98
99         switch (type) {
100         case S_IFREG: {
101                 handle = mds_fs_start(mds, dir, MDS_FSOP_CREATE);
102                 if (!handle)
103                         GOTO(out_create_dchild, PTR_ERR(handle));
104                 rc = vfs_create(dir, dchild, rec->ur_mode);
105                 EXIT;
106                 break;
107         }
108         case S_IFDIR: {
109                 handle = mds_fs_start(mds, dir, MDS_FSOP_MKDIR);
110                 if (!handle)
111                         GOTO(out_create_dchild, PTR_ERR(handle));
112                 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
113                 EXIT;
114                 break;
115         }
116         case S_IFLNK: {
117                 handle = mds_fs_start(mds, dir, MDS_FSOP_SYMLINK);
118                 if (!handle)
119                         GOTO(out_create_dchild, PTR_ERR(handle));
120                 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
121                 EXIT;
122                 break;
123         }
124         case S_IFCHR:
125         case S_IFBLK:
126         case S_IFIFO:
127         case S_IFSOCK: {
128                 int rdev = rec->ur_id;
129                 handle = mds_fs_start(mds, dir, MDS_FSOP_MKNOD);
130                 if (!handle)
131                         GOTO(out_create_dchild, PTR_ERR(handle));
132                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
133                 EXIT;
134                 break;
135         }
136         default:
137                 CERROR("bad file type %d for create of %s\n",type,rec->ur_name);
138                 GOTO(out_create_dchild, rc = -EINVAL);
139         }
140
141         if (rc) {
142                 CERROR("error during create: %d\n", rc);
143                 LBUG();
144                 GOTO(out_create_commit, rc);
145         } else {
146                 struct iattr iattr;
147                 struct inode *inode = dchild->d_inode;
148                 struct mds_body *body;
149
150                 if (type == S_IFREG) {
151                         rc = mds_fs_set_objid(mds, inode, handle, rec->ur_id);
152                         if (rc)
153                                 CERROR("error %d setting objid for %ld\n",
154                                        rc, inode->i_ino);
155                 }
156
157                 iattr.ia_atime = rec->ur_time;
158                 iattr.ia_ctime = rec->ur_time;
159                 iattr.ia_mtime = rec->ur_time;
160                 iattr.ia_uid = rec->ur_uid;
161                 iattr.ia_gid = rec->ur_gid;
162                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
163                         ATTR_MTIME | ATTR_CTIME;
164
165                 rc = mds_fs_setattr(mds, dchild, handle, &iattr);
166                 /* XXX should we abort here in case of error? */
167
168                 body = lustre_msg_buf(req->rq_repmsg, 0);
169                 body->ino = inode->i_ino;
170                 body->generation = inode->i_generation;
171         }
172
173 out_create_commit:
174         /* FIXME: keep rc intact */
175         rc = mds_fs_commit(mds, dir, handle);
176 out_create_dchild:
177         l_dput(dchild);
178         up(&dir->i_sem);
179 out_create_de:
180         l_dput(de);
181         req->rq_status = rc;
182         return 0;
183 }
184
185 static int mds_reint_unlink(struct mds_update_record *rec,
186                             struct ptlrpc_request *req)
187 {
188         struct dentry *de = NULL;
189         struct dentry *dchild = NULL;
190         struct mds_obd *mds = &req->rq_obd->u.mds;
191         struct inode *dir, *inode;
192         int rc = 0;
193         ENTRY;
194
195         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
196         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK)) {
197                 LBUG();
198                 GOTO(out_unlink, rc = -ESTALE);
199         }
200         dir = de->d_inode;
201         CDEBUG(D_INODE, "ino %ld\n", dir->i_ino);
202
203         down(&dir->i_sem);
204         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
205         if (IS_ERR(dchild)) {
206                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
207                 LBUG();
208                 GOTO(out_unlink_de, rc = -ESTALE);
209         }
210
211         inode = dchild->d_inode;
212         if (!inode) {
213                 CERROR("child doesn't exist (dir %ld, name %s\n",
214                        dir->i_ino, rec->ur_name);
215                 LBUG();
216                 GOTO(out_unlink_dchild, rc = -ESTALE);
217         }
218
219         if (inode->i_ino != rec->ur_fid2->id) {
220                 CERROR("inode and FID ID do not match (%ld != %Ld)\n",
221                        inode->i_ino, rec->ur_fid2->id);
222                 LBUG();
223                 GOTO(out_unlink_dchild, rc = -ESTALE);
224         }
225         if (inode->i_generation != rec->ur_fid2->generation) {
226                 CERROR("inode and FID GENERATION do not match (%d != %d)\n",
227                        inode->i_generation, rec->ur_fid2->generation);
228                 LBUG();
229                 GOTO(out_unlink_dchild, rc = -ESTALE);
230         }
231
232         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dir->i_sb->s_dev);
233
234         switch (dchild->d_inode->i_mode & S_IFMT) {
235         case S_IFDIR:
236                 rc = vfs_rmdir(dir, dchild);
237                 EXIT;
238                 break;
239         default:
240                 rc = vfs_unlink(dir, dchild);
241                 EXIT;
242                 break;
243         }
244
245         EXIT;
246 out_unlink_dchild:
247         l_dput(dchild);
248 out_unlink_de:
249         up(&dir->i_sem);
250         l_dput(de);
251 out_unlink:
252         req->rq_status = rc;
253         return 0;
254 }
255
256 static int mds_reint_link(struct mds_update_record *rec,
257                             struct ptlrpc_request *req)
258 {
259         struct dentry *de_src = NULL;
260         struct dentry *de_tgt_dir = NULL;
261         struct dentry *dchild = NULL;
262         struct mds_obd *mds = &req->rq_obd->u.mds;
263         int rc = 0;
264
265         ENTRY;
266         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
267         if (IS_ERR(de_src) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK)) {
268                 GOTO(out_link, rc = -ESTALE);
269         }
270
271         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
272         if (IS_ERR(de_tgt_dir)) {
273                 GOTO(out_link_de_src, rc = -ESTALE);
274         }
275
276         down(&de_tgt_dir->d_inode->i_sem);
277         dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
278         if (IS_ERR(dchild)) {
279                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
280                 GOTO(out_link_de_tgt_dir, rc = -ESTALE);
281         }
282
283         if (dchild->d_inode) {
284                 CERROR("child exists (dir %ld, name %s\n",
285                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
286                 GOTO(out_link_dchild, rc = -EEXIST);
287         }
288
289         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE,
290                        dchild->d_inode->i_sb->s_dev);
291
292         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
293         EXIT;
294
295 out_link_dchild:
296         l_dput(dchild);
297 out_link_de_tgt_dir:
298         up(&de_tgt_dir->d_inode->i_sem);
299         l_dput(de_tgt_dir);
300 out_link_de_src:
301         l_dput(de_src);
302 out_link:
303         req->rq_status = rc;
304         return 0;
305 }
306
307 static int mds_reint_rename(struct mds_update_record *rec,
308                             struct ptlrpc_request *req)
309 {
310         struct dentry *de_srcdir = NULL;
311         struct dentry *de_tgtdir = NULL;
312         struct dentry *de_old = NULL;
313         struct dentry *de_new = NULL;
314         struct mds_obd *mds = &req->rq_obd->u.mds;
315         int rc = 0;
316         ENTRY;
317
318         de_srcdir = mds_fid2dentry(mds, rec->ur_fid1, NULL);
319         if (IS_ERR(de_srcdir)) {
320                 GOTO(out_rename, rc = -ESTALE);
321         }
322
323         de_tgtdir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
324         if (IS_ERR(de_tgtdir)) {
325                 GOTO(out_rename_srcdir, rc = -ESTALE);
326         }
327
328         de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
329         if (IS_ERR(de_old)) {
330                 CERROR("old child lookup error %ld\n", PTR_ERR(de_old));
331                 GOTO(out_rename_tgtdir, rc = -ESTALE);
332         }
333
334         de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
335         if (IS_ERR(de_new)) {
336                 CERROR("new child lookup error %ld\n", PTR_ERR(de_new));
337                 GOTO(out_rename_deold, rc = -ESTALE);
338         }
339
340         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
341                        de_srcdir->d_inode->i_sb->s_dev);
342
343         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
344         EXIT;
345
346         l_dput(de_new);
347 out_rename_deold:
348         l_dput(de_old);
349 out_rename_tgtdir:
350         l_dput(de_tgtdir);
351 out_rename_srcdir:
352         l_dput(de_srcdir);
353 out_rename:
354         req->rq_status = rc;
355         return 0;
356 }
357
358 typedef int (*mds_reinter)(struct mds_update_record *, struct ptlrpc_request*);
359
360 static mds_reinter reinters[REINT_MAX+1] = {
361         [REINT_SETATTR]   mds_reint_setattr,
362         [REINT_CREATE]    mds_reint_create,
363         [REINT_UNLINK]    mds_reint_unlink,
364         [REINT_LINK]      mds_reint_link,
365         [REINT_RENAME]    mds_reint_rename,
366 };
367
368 int mds_reint_rec(struct mds_update_record *rec, struct ptlrpc_request *req)
369 {
370         int rc, size = sizeof(struct mds_body);
371
372         if (rec->ur_opcode < 1 || rec->ur_opcode > REINT_MAX) {
373                 CERROR("opcode %d not valid\n", rec->ur_opcode);
374                 rc = req->rq_status = -EINVAL;
375                 RETURN(rc);
376         }
377
378         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
379         if (rc) {
380                 CERROR("mds: out of memory\n");
381                 rc = req->rq_status = -ENOMEM;
382                 RETURN(rc);
383         }
384
385         rc = reinters[rec->ur_opcode](rec, req);
386         return rc;
387 }