Whamcloud - gitweb
mds/handler.c, mds/mds_reint.c: fixup the ext3 MDS code a bit to build on
[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 inode *inode;
45         struct dentry *de;
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         EXIT;
91         return 0;
92 }
93
94 /* 
95    XXX nasty hack: store the object id in the first two
96    direct block spots 
97 */
98 static inline void mds_store_objid(struct inode *inode, __u64 *id)
99 {
100         /* FIXME: it is only by luck that this works on ext3 */
101         memcpy(&inode->u.ext2_i.i_data, id, sizeof(*id));
102 }
103
104
105 static int mds_reint_create(struct mds_update_record *rec, 
106                             struct ptlrpc_request *req)
107 {
108         int type = rec->ur_mode & S_IFMT;
109         struct dentry *de = NULL;
110         struct mds_rep *rep = req->rq_rep.mds;
111         struct dentry *dchild = NULL;
112         int rc;
113         ENTRY;
114
115         de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
116         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
117                 BUG();
118                 GOTO(out_reint_create, (rc = -ESTALE));
119         }
120         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
121
122         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
123         if (IS_ERR(dchild)) {
124                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
125                 BUG();
126                 GOTO(out_reint_create, (rc = -ESTALE));
127         }
128
129         if (dchild->d_inode) {
130                 CERROR("child exists (dir %ld, name %s)\n", 
131                        de->d_inode->i_ino, rec->ur_name);
132                 BUG();
133                 GOTO(out_reint_create, (rc = -EEXIST));
134         }
135
136         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE);
137
138         switch (type) {
139         case S_IFREG: { 
140                 rc = vfs_create(de->d_inode, dchild, rec->ur_mode);
141                 EXIT;
142                 break;
143         }
144         case S_IFDIR: { 
145                 rc = vfs_mkdir(de->d_inode, dchild, rec->ur_mode);
146                 EXIT;
147                 break;
148         } 
149         case S_IFLNK: { 
150                 rc = vfs_symlink(de->d_inode, dchild, rec->ur_tgt);
151                 EXIT;
152                 break;
153         } 
154         case S_IFCHR:
155         case S_IFBLK:
156         case S_IFIFO:
157         case S_IFSOCK: { 
158                 int rdev = rec->ur_id;
159                 rc = vfs_mknod(de->d_inode, dchild, rec->ur_mode, rdev); 
160                 EXIT;
161                 break;
162         }
163         }
164
165         if (!rc) { 
166                 if (type == S_IFREG)
167                         mds_store_objid(dchild->d_inode, &rec->ur_id); 
168                 dchild->d_inode->i_atime = rec->ur_time;
169                 dchild->d_inode->i_ctime = rec->ur_time;
170                 dchild->d_inode->i_mtime = rec->ur_time;
171                 dchild->d_inode->i_uid = rec->ur_uid;
172                 dchild->d_inode->i_gid = rec->ur_gid;
173                 rep->ino = dchild->d_inode->i_ino;
174         }
175
176 out_reint_create:
177         req->rq_rephdr->status = rc;
178         l_dput(de);
179         l_dput(dchild);
180         RETURN(0);
181 }
182
183 static int mds_reint_unlink(struct mds_update_record *rec, 
184                             struct ptlrpc_request *req)
185 {
186         struct dentry *de = NULL;
187         struct dentry *dchild = NULL;
188         int rc = 0;
189         ENTRY;
190
191         de = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
192         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK)) {
193                 BUG();
194                 GOTO(out_unlink, (rc = -ESTALE));
195         }
196         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
197
198         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
199         if (IS_ERR(dchild)) {
200                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
201                 BUG();
202                 GOTO(out_unlink, (rc = -ESTALE));
203         }
204
205         if (!dchild->d_inode) {
206                 CERROR("child doesn't exist (dir %ld, name %s\n", 
207                        de->d_inode->i_ino, rec->ur_name);
208                 BUG();
209                 GOTO(out_unlink, (rc = -ESTALE));
210         }
211
212         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE);
213
214         switch (dchild->d_inode->i_mode & S_IFMT) {
215         case S_IFDIR:
216                 rc = vfs_rmdir(de->d_inode, dchild);
217                 EXIT;
218                 break;
219         default:
220                 rc = vfs_unlink(de->d_inode, dchild);
221                 EXIT;
222                 break;
223         }
224
225 out_unlink:
226         req->rq_rephdr->status = rc;
227         l_dput(de);
228         l_dput(dchild);
229         RETURN(0);
230 }
231
232 static int mds_reint_link(struct mds_update_record *rec, 
233                             struct ptlrpc_request *req)
234 {
235         struct dentry *de_src = NULL;
236         struct dentry *de_tgt_dir = NULL;
237         struct dentry *dchild = NULL;
238         int rc = 0;
239
240         ENTRY;
241         de_src = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
242         if (IS_ERR(de_src) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK)) {
243                 GOTO(out_link, (rc = -ESTALE));
244         }
245
246         de_tgt_dir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid2, NULL);
247         if (IS_ERR(de_tgt_dir)) {
248                 GOTO(out_link, (rc = -ESTALE));
249         }
250
251         dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
252         if (IS_ERR(dchild)) {
253                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
254                 GOTO(out_link, (rc = -ESTALE));
255         }
256
257         if (dchild->d_inode) {
258                 CERROR("child exists (dir %ld, name %s\n", 
259                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
260                 GOTO(out_link, (rc = -EEXIST));
261         }
262
263         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE);
264
265         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild); 
266         EXIT;
267
268  out_link:
269         req->rq_rephdr->status = rc;
270         l_dput(de_src);
271         l_dput(de_tgt_dir); 
272         l_dput(dchild); 
273         return 0;
274 }
275
276
277 static int mds_reint_rename(struct mds_update_record *rec, 
278                             struct ptlrpc_request *req)
279 {
280         struct dentry *de_srcdir = NULL;
281         struct dentry *de_tgtdir = NULL;
282         struct dentry *de_old = NULL; 
283         struct dentry *de_new = NULL; 
284         int rc = 0;
285         ENTRY;
286
287         de_srcdir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid1, NULL);
288         if (IS_ERR(de_srcdir)) {
289                 GOTO(out_rename, (rc = -ESTALE));
290         }
291
292         de_tgtdir = mds_fid2dentry(&req->rq_obd->u.mds, rec->ur_fid2, NULL);
293         if (IS_ERR(de_tgtdir)) {
294                 GOTO(out_rename, (rc = -ESTALE));
295         }
296
297         de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
298         if (IS_ERR(de_old)) {
299                 CERROR("child lookup error %ld\n", PTR_ERR(de_old));
300                 GOTO(out_rename, (rc = -ESTALE));
301         }
302
303         de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
304         if (IS_ERR(de_new)) {
305                 CERROR("child lookup error %ld\n", PTR_ERR(de_new));
306                 GOTO(out_rename, (rc = -ESTALE));
307         }
308
309         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE);
310
311         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
312         EXIT;
313
314  out_rename:
315         req->rq_rephdr->status = rc;
316         l_dput(de_new);
317         l_dput(de_old); 
318         l_dput(de_tgtdir); 
319         l_dput(de_srcdir); 
320         return 0;
321 }
322
323 typedef int (*mds_reinter)(struct mds_update_record *, struct ptlrpc_request*); 
324
325 static mds_reinter  reinters[REINT_MAX+1] = {
326         [REINT_SETATTR]   mds_reint_setattr,
327         [REINT_CREATE]    mds_reint_create,
328         [REINT_UNLINK]    mds_reint_unlink,
329         [REINT_LINK]      mds_reint_link,
330         [REINT_RENAME]    mds_reint_rename
331 };
332
333 int mds_reint_rec(struct mds_update_record *rec, struct ptlrpc_request *req)
334 {
335         int rc; 
336
337         if (rec->ur_opcode < 0 || rec->ur_opcode > REINT_MAX) {
338                 CERROR("opcode %d not valid\n", rec->ur_opcode);
339                 rc = req->rq_status = -EINVAL;
340                 RETURN(rc);
341         }
342
343         rc = mds_pack_rep(NULL, 0, NULL, 0, &req->rq_rephdr, &req->rq_rep, 
344                           &req->rq_replen, &req->rq_repbuf);
345         if (rc) {
346                 CERROR("mds: out of memory\n");
347                 rc = req->rq_status = -ENOMEM;
348                 RETURN(rc);
349         }
350         req->rq_rephdr->xid = req->rq_reqhdr->xid;
351
352         rc = reinters[rec->ur_opcode](rec, req); 
353         return rc;
354
355