Whamcloud - gitweb
Increase MDS and OST request buffers to 128k each
[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/ext2_fs.h>
26 #include <linux/quotaops.h>
27 #include <asm/unistd.h>
28 #include <asm/uaccess.h>
29
30 #define DEBUG_SUBSYSTEM S_MDS
31
32 #include <linux/obd_support.h>
33 #include <linux/obd_class.h>
34 #include <linux/obd.h>
35 #include <linux/lustre_lib.h>
36 #include <linux/lustre_idl.h>
37 #include <linux/lustre_mds.h>
38 #include <linux/obd_class.h>
39
40 extern struct ptlrpc_request *mds_prep_req(int size, int opcode, int namelen, char *name, int tgtlen, char *tgt);
41
42 static int mds_reint_setattr(struct mds_update_record *rec, struct ptlrpc_request *req)
43 {
44         struct dentry *de;
45         struct inode *inode;
46
47         de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
48         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_SETATTR)) {
49                 req->rq_rephdr->status = -ESTALE;
50                 RETURN(0);
51         }
52
53         inode = de->d_inode;
54         CDEBUG(D_INODE, "ino %ld\n", inode->i_ino);
55
56         /* a _really_ horrible hack to avoid removing the data stored
57            in the block pointers; this data is the object id
58            this will go into an extended attribute at some point.
59         */
60         if ( rec->ur_iattr.ia_valid & ATTR_SIZE ) {
61                 /* ATTR_SIZE would invoke truncate: clear it */
62                 rec->ur_iattr.ia_valid &= ~ATTR_SIZE;
63                 inode->i_size = rec->ur_iattr.ia_size;
64
65                 /* an _even_more_ horrible hack to make this hack work with
66                  * ext3.  This is because ext3 keeps a separate inode size
67                  * until the inode is committed to ensure consistency.  This
68                  * will also go away with the move to EAs.
69                  */
70                 if (!strcmp(inode->i_sb->s_type->name, "ext3"))
71                         inode->u.ext3_i.i_disksize = inode->i_size;
72
73                 /* make sure _something_ gets set - so new inode
74                    goes to disk (probably won't work over XFS */
75                 if (!rec->ur_iattr.ia_valid & ATTR_MODE) {
76                         rec->ur_iattr.ia_valid |= ATTR_MODE;
77                         rec->ur_iattr.ia_mode = inode->i_mode;
78                 }
79         }
80         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE);
81         if ( inode->i_op->setattr ) {
82                 req->rq_rephdr->status =
83                         inode->i_op->setattr(de, &rec->ur_iattr);
84         } else {
85                 req->rq_rephdr->status =
86                         inode_setattr(inode, &rec->ur_iattr);
87         }
88
89         l_dput(de);
90         RETURN(0);
91 }
92
93 /*
94    XXX nasty hack: store the object id in the first two
95    direct block spots
96 */
97 static inline void mds_store_objid(struct inode *inode, __u64 *id)
98 {
99         /* FIXME: it is only by luck that this works on ext3 */
100         memcpy(&inode->u.ext2_i.i_data, id, sizeof(*id));
101 }
102
103
104 static int mds_reint_create(struct mds_update_record *rec,
105                             struct ptlrpc_request *req)
106 {
107         int type = rec->ur_mode & S_IFMT;
108         struct dentry *de = NULL;
109         struct mds_rep *rep = req->rq_rep.mds;
110         struct dentry *dchild = NULL;
111         int rc = 0;
112         ENTRY;
113
114         de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
115         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
116                 LBUG();
117                 GOTO(out_reint_create, (rc = -ESTALE));
118         }
119         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
120
121         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
122         if (IS_ERR(dchild)) {
123                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
124                 LBUG();
125                 GOTO(out_reint_create, (rc = -ESTALE));
126         }
127
128         if (dchild->d_inode) {
129                 CERROR("child exists (dir %ld, name %s)\n",
130                        de->d_inode->i_ino, rec->ur_name);
131                 LBUG();
132                 GOTO(out_reint_create, (rc = -EEXIST));
133         }
134
135         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE);
136
137         switch (type) {
138         case S_IFREG: {
139                 rc = vfs_create(de->d_inode, dchild, rec->ur_mode);
140                 EXIT;
141                 break;
142         }
143         case S_IFDIR: {
144                 rc = vfs_mkdir(de->d_inode, dchild, rec->ur_mode);
145                 EXIT;
146                 break;
147         }
148         case S_IFLNK: {
149                 rc = vfs_symlink(de->d_inode, dchild, rec->ur_tgt);
150                 EXIT;
151                 break;
152         }
153         case S_IFCHR:
154         case S_IFBLK:
155         case S_IFIFO:
156         case S_IFSOCK: {
157                 int rdev = rec->ur_id;
158                 rc = vfs_mknod(de->d_inode, dchild, rec->ur_mode, rdev);
159                 EXIT;
160                 break;
161         }
162         }
163
164         if (!rc) {
165                 if (type == S_IFREG)
166                         mds_store_objid(dchild->d_inode, &rec->ur_id);
167                 dchild->d_inode->i_atime = rec->ur_time;
168                 dchild->d_inode->i_ctime = rec->ur_time;
169                 dchild->d_inode->i_mtime = rec->ur_time;
170                 dchild->d_inode->i_uid = rec->ur_uid;
171                 dchild->d_inode->i_gid = rec->ur_gid;
172                 rep->ino = dchild->d_inode->i_ino;
173         }
174
175 out_reint_create:
176         req->rq_rephdr->status = rc;
177         l_dput(de);
178         l_dput(dchild);
179         RETURN(0);
180 }
181
182 static int mds_reint_unlink(struct mds_update_record *rec,
183                             struct ptlrpc_request *req)
184 {
185         struct dentry *de = NULL;
186         struct dentry *dchild = NULL;
187         int rc = 0;
188         ENTRY;
189
190         de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
191         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK)) {
192                 LBUG();
193                 GOTO(out_unlink, (rc = -ESTALE));
194         }
195         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
196
197         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
198         if (IS_ERR(dchild)) {
199                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
200                 LBUG();
201                 GOTO(out_unlink, (rc = -ESTALE));
202         }
203
204         if (!dchild->d_inode) {
205                 CERROR("child doesn't exist (dir %ld, name %s\n",
206                        de->d_inode->i_ino, rec->ur_name);
207                 LBUG();
208                 GOTO(out_unlink, (rc = -ESTALE));
209         }
210
211         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE);
212
213         switch (dchild->d_inode->i_mode & S_IFMT) {
214         case S_IFDIR:
215                 rc = vfs_rmdir(de->d_inode, dchild);
216                 EXIT;
217                 break;
218         default:
219                 rc = vfs_unlink(de->d_inode, dchild);
220                 EXIT;
221                 break;
222         }
223
224 out_unlink:
225         req->rq_rephdr->status = rc;
226         l_dput(de);
227         l_dput(dchild);
228         RETURN(0);
229 }
230
231 static int mds_reint_link(struct mds_update_record *rec,
232                             struct ptlrpc_request *req)
233 {
234         struct dentry *de_src = NULL;
235         struct dentry *de_tgt_dir = NULL;
236         struct dentry *dchild = NULL;
237         int rc = 0;
238
239         ENTRY;
240         de_src = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
241         if (IS_ERR(de_src) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK)) {
242                 GOTO(out_link, (rc = -ESTALE));
243         }
244
245         de_tgt_dir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid2, NULL);
246         if (IS_ERR(de_tgt_dir)) {
247                 GOTO(out_link, (rc = -ESTALE));
248         }
249
250         dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
251         if (IS_ERR(dchild)) {
252                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
253                 GOTO(out_link, (rc = -ESTALE));
254         }
255
256         if (dchild->d_inode) {
257                 CERROR("child exists (dir %ld, name %s\n",
258                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
259                 GOTO(out_link, (rc = -EEXIST));
260         }
261
262         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE);
263
264         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
265         EXIT;
266
267  out_link:
268         req->rq_rephdr->status = rc;
269         l_dput(de_src);
270         l_dput(de_tgt_dir);
271         l_dput(dchild);
272         return 0;
273 }
274
275
276 static int mds_reint_rename(struct mds_update_record *rec,
277                             struct ptlrpc_request *req)
278 {
279         struct dentry *de_srcdir = NULL;
280         struct dentry *de_tgtdir = NULL;
281         struct dentry *de_old = NULL;
282         struct dentry *de_new = NULL;
283         int rc = 0;
284         ENTRY;
285
286         de_srcdir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
287         if (IS_ERR(de_srcdir)) {
288                 GOTO(out_rename, (rc = -ESTALE));
289         }
290
291         de_tgtdir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid2, NULL);
292         if (IS_ERR(de_tgtdir)) {
293                 GOTO(out_rename, (rc = -ESTALE));
294         }
295
296         de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
297         if (IS_ERR(de_old)) {
298                 CERROR("child lookup error %ld\n", PTR_ERR(de_old));
299                 GOTO(out_rename, (rc = -ESTALE));
300         }
301
302         de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
303         if (IS_ERR(de_new)) {
304                 CERROR("child lookup error %ld\n", PTR_ERR(de_new));
305                 GOTO(out_rename, (rc = -ESTALE));
306         }
307
308         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE);
309
310         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
311         EXIT;
312
313  out_rename:
314         req->rq_rephdr->status = rc;
315         l_dput(de_new);
316         l_dput(de_old);
317         l_dput(de_tgtdir);
318         l_dput(de_srcdir);
319         return 0;
320 }
321
322 typedef int (*mds_reinter)(struct mds_update_record *, struct ptlrpc_request*);
323
324 static mds_reinter  reinters[REINT_MAX+1] = {
325         [REINT_SETATTR]   mds_reint_setattr,
326         [REINT_CREATE]    mds_reint_create,
327         [REINT_UNLINK]    mds_reint_unlink,
328         [REINT_LINK]      mds_reint_link,
329         [REINT_RENAME]    mds_reint_rename
330 };
331
332 int mds_reint_rec(struct mds_update_record *rec, struct ptlrpc_request *req)
333 {
334         int rc;
335
336         if (rec->ur_opcode < 0 || rec->ur_opcode > REINT_MAX) {
337                 CERROR("opcode %d not valid\n", rec->ur_opcode);
338                 rc = req->rq_status = -EINVAL;
339                 RETURN(rc);
340         }
341
342         rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
343                           &req->rq_replen, &req->rq_repbuf);
344         if (rc) {
345                 CERROR("mds: out of memory\n");
346                 rc = req->rq_status = -ENOMEM;
347                 RETURN(rc);
348         }
349         req->rq_rephdr->xid = req->rq_reqhdr->xid;
350
351         rc = reinters[rec->ur_opcode](rec, req);
352         return rc;
353 }