Whamcloud - gitweb
- Added a queue length and waitqueue to the client structure in an
[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                 rep->generation = dchild->d_inode->i_generation;
174         } else {
175                 CERROR("error during create: %d\n", rc);
176                 LBUG();
177         }
178
179 out_reint_create:
180         req->rq_rephdr->status = rc;
181         l_dput(de);
182         l_dput(dchild);
183         RETURN(0);
184 }
185
186 static int mds_reint_unlink(struct mds_update_record *rec,
187                             struct ptlrpc_request *req)
188 {
189         struct dentry *de = NULL;
190         struct dentry *dchild = NULL;
191         int rc = 0;
192         ENTRY;
193
194         de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
195         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK)) {
196                 LBUG();
197                 GOTO(out_unlink, (rc = -ESTALE));
198         }
199         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
200
201         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
202         if (IS_ERR(dchild)) {
203                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
204                 LBUG();
205                 GOTO(out_unlink, (rc = -ESTALE));
206         }
207
208         if (!dchild->d_inode) {
209                 CERROR("child doesn't exist (dir %ld, name %s\n",
210                        de->d_inode->i_ino, rec->ur_name);
211                 LBUG();
212                 GOTO(out_unlink, (rc = -ESTALE));
213         }
214
215         if (dchild->d_inode->i_ino != rec->ur_fid2->id)
216                 LBUG();
217         if (dchild->d_inode->i_generation != rec->ur_fid2->generation)
218                 LBUG();
219
220         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE);
221
222         switch (dchild->d_inode->i_mode & S_IFMT) {
223         case S_IFDIR:
224                 rc = vfs_rmdir(de->d_inode, dchild);
225                 EXIT;
226                 break;
227         default:
228                 rc = vfs_unlink(de->d_inode, dchild);
229                 EXIT;
230                 break;
231         }
232
233 out_unlink:
234         req->rq_rephdr->status = rc;
235         l_dput(de);
236         l_dput(dchild);
237         RETURN(0);
238 }
239
240 static int mds_reint_link(struct mds_update_record *rec,
241                             struct ptlrpc_request *req)
242 {
243         struct dentry *de_src = NULL;
244         struct dentry *de_tgt_dir = NULL;
245         struct dentry *dchild = NULL;
246         int rc = 0;
247
248         ENTRY;
249         de_src = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
250         if (IS_ERR(de_src) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK)) {
251                 GOTO(out_link, (rc = -ESTALE));
252         }
253
254         de_tgt_dir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid2, NULL);
255         if (IS_ERR(de_tgt_dir)) {
256                 GOTO(out_link, (rc = -ESTALE));
257         }
258
259         dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
260         if (IS_ERR(dchild)) {
261                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
262                 GOTO(out_link, (rc = -ESTALE));
263         }
264
265         if (dchild->d_inode) {
266                 CERROR("child exists (dir %ld, name %s\n",
267                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
268                 GOTO(out_link, (rc = -EEXIST));
269         }
270
271         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE);
272
273         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
274         EXIT;
275
276  out_link:
277         req->rq_rephdr->status = rc;
278         l_dput(de_src);
279         l_dput(de_tgt_dir);
280         l_dput(dchild);
281         return 0;
282 }
283
284
285 static int mds_reint_rename(struct mds_update_record *rec,
286                             struct ptlrpc_request *req)
287 {
288         struct dentry *de_srcdir = NULL;
289         struct dentry *de_tgtdir = NULL;
290         struct dentry *de_old = NULL;
291         struct dentry *de_new = NULL;
292         int rc = 0;
293         ENTRY;
294
295         de_srcdir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
296         if (IS_ERR(de_srcdir)) {
297                 GOTO(out_rename, (rc = -ESTALE));
298         }
299
300         de_tgtdir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid2, NULL);
301         if (IS_ERR(de_tgtdir)) {
302                 GOTO(out_rename, (rc = -ESTALE));
303         }
304
305         de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
306         if (IS_ERR(de_old)) {
307                 CERROR("child lookup error %ld\n", PTR_ERR(de_old));
308                 GOTO(out_rename, (rc = -ESTALE));
309         }
310
311         de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
312         if (IS_ERR(de_new)) {
313                 CERROR("child lookup error %ld\n", PTR_ERR(de_new));
314                 GOTO(out_rename, (rc = -ESTALE));
315         }
316
317         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE);
318
319         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
320         EXIT;
321
322  out_rename:
323         req->rq_rephdr->status = rc;
324         l_dput(de_new);
325         l_dput(de_old);
326         l_dput(de_tgtdir);
327         l_dput(de_srcdir);
328         return 0;
329 }
330
331 typedef int (*mds_reinter)(struct mds_update_record *, struct ptlrpc_request*);
332
333 static mds_reinter  reinters[REINT_MAX+1] = {
334         [REINT_SETATTR]   mds_reint_setattr,
335         [REINT_CREATE]    mds_reint_create,
336         [REINT_UNLINK]    mds_reint_unlink,
337         [REINT_LINK]      mds_reint_link,
338         [REINT_RENAME]    mds_reint_rename
339 };
340
341 int mds_reint_rec(struct mds_update_record *rec, struct ptlrpc_request *req)
342 {
343         int rc;
344
345         if (rec->ur_opcode < 0 || rec->ur_opcode > REINT_MAX) {
346                 CERROR("opcode %d not valid\n", rec->ur_opcode);
347                 rc = req->rq_status = -EINVAL;
348                 RETURN(rc);
349         }
350
351         rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep,
352                           &req->rq_replen, &req->rq_repbuf);
353         if (rc) {
354                 CERROR("mds: out of memory\n");
355                 rc = req->rq_status = -ENOMEM;
356                 RETURN(rc);
357         }
358         req->rq_rephdr->xid = req->rq_reqhdr->xid;
359
360         rc = reinters[rec->ur_opcode](rec, req);
361         return rc;
362 }