Whamcloud - gitweb
- Added match_or_enqueue helper function
[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 #define DEBUG_SUBSYSTEM S_MDS
20
21 #include <linux/obd_support.h>
22 #include <linux/obd_class.h>
23 #include <linux/obd.h>
24 #include <linux/lustre_lib.h>
25 #include <linux/lustre_idl.h>
26 #include <linux/lustre_mds.h>
27 #include <linux/lustre_dlm.h>
28 #include <linux/obd_class.h>
29
30 extern inline struct mds_obd *mds_req2mds(struct ptlrpc_request *req);
31
32 struct mds_client_info *mds_uuid_to_mci(struct mds_obd *mds, __u8 *uuid)
33 {
34         struct list_head *p;
35
36         if (!uuid)
37                 return NULL;
38
39         list_for_each(p, &mds->mds_client_info) {
40                 struct mds_client_info *mci;
41
42                 mci = list_entry(p, struct mds_client_info, mci_list);
43                 CDEBUG(D_INFO, "checking client UUID '%s'\n",
44                        mci->mci_mcd->mcd_uuid);
45                 if (!strncmp(mci->mci_mcd->mcd_uuid, uuid,
46                              sizeof(mci->mci_mcd->mcd_uuid)))
47                         return mci;
48         }
49         CDEBUG(D_INFO, "no mds client info found for  UUID '%s'\n", uuid);
50         return NULL;
51 }
52
53 /* Assumes caller has already pushed us into the kernel context. */
54 int mds_update_last_rcvd(struct mds_obd *mds, void *handle,
55                          struct ptlrpc_request *req)
56 {
57         /* get from req->rq_connection-> or req->rq_client */
58         struct mds_client_info *mci;
59         loff_t off;
60         int rc;
61
62         mci = mds_uuid_to_mci(mds, req->rq_connection->c_remote_uuid);
63         if (!mci) {
64                 CERROR("unable to locate MDS client data for UUID '%s'\n",
65                        ptlrpc_req_to_uuid(req));
66                 /* This will be a real error once everything is working */
67                 //LBUG();
68                 RETURN(0);
69         }
70
71         off = MDS_LR_CLIENT + mci->mci_off * MDS_LR_SIZE;
72
73         ++mds->mds_last_rcvd;   /* lock this, or make it an LDLM function? */
74         req->rq_repmsg->transno = HTON__u64(mds->mds_last_rcvd);
75         mci->mci_mcd->mcd_last_rcvd = cpu_to_le64(mds->mds_last_rcvd);
76         mci->mci_mcd->mcd_mount_count = cpu_to_le64(mds->mds_mount_count);
77         mci->mci_mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
78
79         mds_fs_set_last_rcvd(mds, handle);
80         rc = lustre_fwrite(mds->mds_rcvd_filp, (char *)mci->mci_mcd,
81                            sizeof(*mci->mci_mcd), &off);
82         CDEBUG(D_INODE, "wrote trans #%Ld for client '%s' at #%d: rc = %d\n",
83                mds->mds_last_rcvd, mci->mci_mcd->mcd_uuid, mci->mci_off, rc);
84         // store new value and last committed value in req struct
85
86         if (rc == sizeof(*mci->mci_mcd))
87                 rc = 0;
88         else {
89                 CERROR("error writing to last_rcvd file: rc = %d\n", rc);
90                 if (rc >= 0)
91                         rc = -EIO;
92         }
93
94         return rc;
95 }
96
97 /* In the write-back case, the client holds a lock on a subtree.
98  * In the intent case, the client holds a lock on the child inode.
99  * In the pathname case, the client (may) hold a lock on the child inode. */
100 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
101                              struct ptlrpc_request *req)
102 {
103         struct mds_obd *mds = mds_req2mds(req);
104         struct dentry *de;
105         void *handle;
106         struct lustre_handle child_lockh;
107         int rc = 0, err;
108
109         if (req->rq_reqmsg->bufcount > offset + 1) {
110                 struct dentry *dir;
111                 struct lustre_handle dir_lockh;
112                 char *name;
113                 int namelen;
114                 /* a name was supplied by the client; fid1 is the directory */
115
116                 name = lustre_msg_buf(req->rq_reqmsg, offset + 1);
117                 namelen = req->rq_reqmsg->buflens[offset + 1] - 1;
118                 dir = mds_fid2locked_dentry(mds, rec->ur_fid1, NULL, LCK_PR,
119                                             &dir_lockh);
120                 if (!dir || IS_ERR(dir)) {
121                         l_dput(dir);
122                         LBUG();
123                         GOTO(out_setattr, rc = -ESTALE);
124                 }
125
126                 de = mds_name2locked_dentry(mds, dir, NULL, name, namelen,
127                                             0, &child_lockh);
128                 l_dput(dir);
129                 if (!de || IS_ERR(de)) {
130                         LBUG();
131                         GOTO(out_setattr_de, rc = -ESTALE);
132                 }
133         } else {
134                 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
135                 if (!de || IS_ERR(de)) {
136                         LBUG();
137                         GOTO(out_setattr_de, rc = -ESTALE);
138                 }
139         }
140         CDEBUG(D_INODE, "ino %ld\n", de->d_inode->i_ino);
141
142         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE,
143                        de->d_inode->i_sb->s_dev);
144
145         lock_kernel();
146         handle = mds_fs_start(mds, de->d_inode, MDS_FSOP_SETATTR);
147         if (!handle)
148                 GOTO(out_unlock, rc = PTR_ERR(handle));
149         rc = mds_fs_setattr(mds, de, handle, &rec->ur_iattr);
150
151         if (!rc)
152                 rc = mds_update_last_rcvd(mds, handle, req);
153
154         err = mds_fs_commit(mds, de->d_inode, handle);
155         if (err) {
156                 CERROR("error on commit: err = %d\n", err);
157                 if (!rc)
158                         rc = err;
159         }
160
161         EXIT;
162  out_unlock:
163         unlock_kernel();
164  out_setattr_de:
165         l_dput(de);
166  out_setattr:
167         req->rq_status = rc;
168         return(0);
169 }
170
171 static int mds_reint_recreate(struct mds_update_record *rec, int offset,
172                             struct ptlrpc_request *req)
173 {
174         struct dentry *de = NULL;
175         struct mds_obd *mds = mds_req2mds(req);
176         struct dentry *dchild = NULL;
177         struct inode *dir;
178         int rc = 0;
179         ENTRY;
180
181         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
182         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
183                 LBUG();
184                 GOTO(out_create_de, rc = -ESTALE);
185         }
186         dir = de->d_inode;
187         CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
188
189         down(&dir->i_sem);
190         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
191         if (IS_ERR(dchild)) {
192                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
193                 up(&dir->i_sem);
194                 LBUG();
195                 GOTO(out_create_dchild, rc = -ESTALE);
196         }
197
198         if (dchild->d_inode) {
199                 struct mds_body *body;
200                 rc = 0;
201                 body = lustre_msg_buf(req->rq_repmsg, 0);
202                 body->ino = dchild->d_inode->i_ino;
203                 body->generation = dchild->d_inode->i_generation;
204         } else {
205                 CERROR("child doesn't exist (dir %ld, name %s)\n",
206                        dir->i_ino, rec->ur_name);
207                 rc = -ENOENT;
208                 LBUG();
209         }
210
211 out_create_dchild:
212         l_dput(dchild);
213         up(&dir->i_sem);
214 out_create_de:
215         l_dput(de);
216         req->rq_status = rc;
217         return 0;
218 }
219
220 static int mds_reint_create(struct mds_update_record *rec, int offset,
221                             struct ptlrpc_request *req)
222 {
223         struct dentry *de = NULL;
224         struct mds_obd *mds = mds_req2mds(req);
225         struct dentry *dchild = NULL;
226         struct inode *dir;
227         void *handle;
228         struct lustre_handle lockh;
229         int rc = 0, err, flags, lock_mode, type = rec->ur_mode & S_IFMT;
230         __u64 res_id[3] = {0,0,0};
231         ENTRY;
232
233         /* requests were at offset 2, replies go back at 1 */
234         if (offset)
235                 offset = 1;
236
237         if (strcmp(req->rq_export->export_obd->obd_type->typ_name, "mds") != 0)
238                 LBUG();
239
240         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
241         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE)) {
242                 LBUG();
243                 GOTO(out_create_de, rc = -ESTALE);
244         }
245         dir = de->d_inode;
246         CDEBUG(D_INODE, "parent ino %ld name %s mode %o\n",
247                dir->i_ino, rec->ur_name, rec->ur_mode);
248
249         lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
250         res_id[0] = dir->i_ino;
251
252         rc = ldlm_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
253                              NULL, 0, lock_mode, &lockh);
254         if (rc == 0) {
255                 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
256                 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
257                                       (struct lustre_handle *)&mds->mds_connh,
258                                       NULL, mds->mds_local_namespace, NULL,
259                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
260                                       &flags, (void *)mds_lock_callback, NULL,
261                                       0, &lockh);
262                 if (rc != ELDLM_OK) {
263                         CERROR("lock enqueue: err: %d\n", rc);
264                         GOTO(out_create_de, rc = -EIO);
265                 }
266         }
267         ldlm_lock_dump((void *)(unsigned long)lockh.addr);
268
269         down(&dir->i_sem);
270         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
271         if (IS_ERR(dchild)) {
272                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
273                 up(&dir->i_sem);
274                 LBUG();
275                 GOTO(out_create_dchild, rc = -ESTALE);
276         }
277
278         if (dchild->d_inode) {
279                 struct mds_body *body;
280                 struct obdo *obdo;
281                 struct inode *inode = dchild->d_inode;
282                 CDEBUG(D_INODE, "child exists (dir %ld, name %s, ino %ld)\n",
283                        dir->i_ino, rec->ur_name, dchild->d_inode->i_ino);
284
285                 body = lustre_msg_buf(req->rq_repmsg, offset);
286                 mds_pack_inode2fid(&body->fid1, inode);
287                 mds_pack_inode2body(body, inode);
288 #warning FIXME: This ext3/N-specific code does not belong here
289                 /* If i_file_acl is set, this inode has an EA */
290                 if (S_ISREG(inode->i_mode) && inode->u.ext3_i.i_file_acl) {
291                         obdo = lustre_msg_buf(req->rq_repmsg, offset + 1);
292                         mds_fs_get_obdo(mds, inode, obdo);
293                 }
294                 /* now a normal case for intent locking */
295                 GOTO(out_create_dchild, rc = -EEXIST);
296         }
297
298         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb->s_dev);
299
300         if (dir->i_mode & S_ISGID) {
301                 rec->ur_gid = dir->i_gid;
302                 if (S_ISDIR(rec->ur_mode))
303                         rec->ur_mode |= S_ISGID;
304         }
305
306         switch (type) {
307         case S_IFREG: {
308                 handle = mds_fs_start(mds, dir, MDS_FSOP_CREATE);
309                 if (!handle)
310                         GOTO(out_create_dchild, PTR_ERR(handle));
311                 rc = vfs_create(dir, dchild, rec->ur_mode);
312                 EXIT;
313                 break;
314         }
315         case S_IFDIR: {
316                 handle = mds_fs_start(mds, dir, MDS_FSOP_MKDIR);
317                 if (!handle)
318                         GOTO(out_create_dchild, PTR_ERR(handle));
319                 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
320                 EXIT;
321                 break;
322         }
323         case S_IFLNK: {
324                 handle = mds_fs_start(mds, dir, MDS_FSOP_SYMLINK);
325                 if (!handle)
326                         GOTO(out_create_dchild, PTR_ERR(handle));
327                 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
328                 EXIT;
329                 break;
330         }
331         case S_IFCHR:
332         case S_IFBLK:
333         case S_IFIFO:
334         case S_IFSOCK: {
335                 int rdev = rec->ur_rdev;
336                 handle = mds_fs_start(mds, dir, MDS_FSOP_MKNOD);
337                 if (!handle)
338                         GOTO(out_create_dchild, PTR_ERR(handle));
339                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
340                 EXIT;
341                 break;
342         }
343         default:
344                 CERROR("bad file type %d for create of %s\n",type,rec->ur_name);
345                 GOTO(out_create_dchild, rc = -EINVAL);
346         }
347
348         if (rc) {
349                 CERROR("error during create: %d\n", rc);
350                 if (rc != -ENOSPC)
351                         LBUG();
352                 GOTO(out_create_commit, rc);
353         } else {
354                 struct iattr iattr;
355                 struct inode *inode = dchild->d_inode;
356                 struct mds_body *body;
357
358                 CDEBUG(D_INODE, "created ino %ld\n", dchild->d_inode->i_ino);
359                 if (!offset && type == S_IFREG) {
360                         struct obdo *obdo;
361                         obdo = lustre_msg_buf(req->rq_reqmsg, 2);
362                         rc = mds_fs_set_obdo(mds, inode, handle, obdo);
363                         if (rc) {
364                                 CERROR("error %d setting obdo for %ld\n",
365                                        rc, inode->i_ino);
366                                 GOTO(out_create_unlink, rc);
367                         }
368                 }
369
370                 iattr.ia_atime = rec->ur_time;
371                 iattr.ia_ctime = rec->ur_time;
372                 iattr.ia_mtime = rec->ur_time;
373                 iattr.ia_uid = rec->ur_uid;
374                 iattr.ia_gid = rec->ur_gid;
375                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
376                         ATTR_MTIME | ATTR_CTIME;
377
378                 rc = mds_fs_setattr(mds, dchild, handle, &iattr);
379                 if (rc) {
380                         CERROR("error on setattr: rc = %d\n", rc);
381                         /* XXX should we abort here in case of error? */
382                 }
383
384                 rc = mds_update_last_rcvd(mds, handle, req);
385                 if (rc) {
386                         CERROR("error on update_last_rcvd: rc = %d\n", rc);
387                         /* XXX should we abort here in case of error? */
388                 }
389
390                 body = lustre_msg_buf(req->rq_repmsg, offset);
391                 body->ino = inode->i_ino;
392                 body->generation = inode->i_generation;
393         }
394         EXIT;
395 out_create_commit:
396         err = mds_fs_commit(mds, dir, handle);
397         if (err) {
398                 CERROR("error on commit: err = %d\n", err);
399                 if (!rc)
400                         rc = err;
401         }
402 out_create_dchild:
403         l_dput(dchild);
404         up(&dir->i_sem);
405         ldlm_lock_decref(&lockh, lock_mode);
406 out_create_de:
407         l_dput(de);
408         req->rq_status = rc;
409         return 0;
410
411 out_create_unlink:
412         /* Destroy the file we just created.  This should not need extra
413          * journal credits, as we have already modified all of the blocks
414          * needed in order to create the file in the first place.
415          */
416         switch(type) {
417         case S_IFDIR:
418                 err = vfs_rmdir(dir, dchild);
419                 if (err)
420                         CERROR("failed rmdir in error path: rc = %d\n", err);
421                 break;
422         default:
423                 err = vfs_unlink(dir, dchild);
424                 if (err)
425                         CERROR("failed unlink in error path: rc = %d\n", err);
426         }
427
428         goto out_create_commit;
429 }
430
431 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
432                             struct ptlrpc_request *req)
433 {
434         struct dentry *de = NULL;
435         struct dentry *dchild = NULL;
436         struct mds_obd *mds = mds_req2mds(req);
437         struct obdo *obdo;
438         struct inode *dir, *inode;
439         int lock_mode, flags;
440         __u64 res_id[3] = {0};
441         struct lustre_handle lockh;
442         void *handle;
443         int rc = 0;
444         int err;
445         ENTRY;
446
447         de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
448         if (IS_ERR(de) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK)) {
449                 LBUG();
450                 GOTO(out_unlink, rc = -ESTALE);
451         }
452
453         dir = de->d_inode;
454         CDEBUG(D_INODE, "parent ino %ld\n", dir->i_ino);
455         lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
456         res_id[0] = dir->i_ino;
457
458         rc = ldlm_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
459                                    NULL, 0, lock_mode, &lockh);
460         if (rc == 0) {
461                 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
462                 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
463                                       (struct lustre_handle *)&mds->mds_connh,
464                                       NULL, mds->mds_local_namespace, NULL,
465                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
466                                       &flags, (void *)mds_lock_callback, NULL,
467                                       0, &lockh);
468                 if (rc != ELDLM_OK) {
469                         CERROR("lock enqueue: err: %d\n", rc);
470                         GOTO(out_unlink_de, rc = -EIO);
471                 }
472         } else
473                 ldlm_lock_dump((void *)(unsigned long)lockh.addr);
474
475         down(&dir->i_sem);
476         dchild = lookup_one_len(rec->ur_name, de, rec->ur_namelen - 1);
477         if (IS_ERR(dchild)) {
478                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
479                 LBUG();
480                 GOTO(out_unlink_de, rc = -ESTALE);
481         }
482
483         inode = dchild->d_inode;
484         if (!inode) {
485                 CERROR("child doesn't exist (dir %ld, name %s\n",
486                        dir->i_ino, rec->ur_name);
487                 GOTO(out_unlink_dchild, rc = -ENOENT);
488         }
489
490 #if 0 /* in intent case the client doesn't have the inode */
491         if (inode->i_ino != rec->ur_fid2->id) {
492                 CERROR("inode and FID ID do not match (%ld != %Ld)\n",
493                        inode->i_ino, rec->ur_fid2->id);
494                 LBUG();
495                 GOTO(out_unlink_dchild, rc = -ESTALE);
496         }
497         if (inode->i_generation != rec->ur_fid2->generation) {
498                 CERROR("inode and FID GENERATION do not match (%d != %d)\n",
499                        inode->i_generation, rec->ur_fid2->generation);
500                 LBUG();
501                 GOTO(out_unlink_dchild, rc = -ESTALE);
502         }
503 #endif
504
505         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dir->i_sb->s_dev);
506
507         switch (inode->i_mode & S_IFMT) {
508         case S_IFDIR:
509                 handle = mds_fs_start(mds, dir, MDS_FSOP_RMDIR);
510                 if (!handle)
511                         GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
512                 rc = vfs_rmdir(dir, dchild);
513                 break;
514         default:
515                 if (offset) {
516                         obdo = lustre_msg_buf(req->rq_repmsg, 1); 
517                         rc = mds_fs_get_obdo(mds, inode, obdo);
518                         if (rc < 0)
519                                 CDEBUG(D_INFO, "No obdo for ino %ld err %d\n",
520                                        inode->i_ino, rc);
521                 }
522
523                 handle = mds_fs_start(mds, dir, MDS_FSOP_UNLINK);
524                 if (!handle)
525                         GOTO(out_unlink_dchild, rc = PTR_ERR(handle));
526                 rc = vfs_unlink(dir, dchild);
527                 break;
528         }
529
530         if (!rc)
531                 rc = mds_update_last_rcvd(mds, handle, req);
532         err = mds_fs_commit(mds, dir, handle);
533         if (err) {
534                 CERROR("error on commit: err = %d\n", err);
535                 if (!rc)
536                         rc = err;
537         }
538
539         EXIT;
540 out_unlink_dchild:
541         if (!rc)
542                 res_id[0] = inode->i_ino;
543         l_dput(dchild);
544 out_unlink_de:
545         up(&dir->i_sem);
546         ldlm_lock_decref(&lockh, lock_mode);
547         if (!rc) { 
548                 /* Take an exclusive lock on the resource that we're
549                  * about to free, to force everyone to drop their
550                  * locks. */
551                 LDLM_DEBUG_NOLOCK("getting EX lock res %Lu", res_id[0]);
552                 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
553                                       (struct lustre_handle *)&mds->mds_connh,
554                                       NULL, mds->mds_local_namespace, NULL, 
555                                       res_id,
556                                       LDLM_PLAIN, NULL, 0, LCK_EX, &flags,
557                                       (void *)mds_lock_callback, NULL, 0, 
558                                       &lockh);
559                 if (rc) 
560                         CERROR("failed to get child inode lock (child ino %Ld, "
561                                "dir ino %ld)\n",
562                                res_id[0], de->d_inode->i_ino);
563         }
564
565         l_dput(de);
566
567         if (!rc) { 
568                 ldlm_lock_decref(&lockh, LCK_EX);
569                 rc = ldlm_cli_cancel(&lockh);
570                 if (rc < 0)
571                         CERROR("failed to cancel child inode lock ino "
572                                "%Ld: %d\n", res_id[0], rc);
573         }
574
575 out_unlink:
576         req->rq_status = rc;
577         return 0;
578 }
579
580 static int mds_reint_link(struct mds_update_record *rec, int offset,
581                             struct ptlrpc_request *req)
582 {
583         struct dentry *de_src = NULL;
584         struct dentry *de_tgt_dir = NULL;
585         struct dentry *dchild = NULL;
586         struct mds_obd *mds = mds_req2mds(req);
587         void *handle;
588         int rc = 0;
589         int err;
590
591         ENTRY;
592         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
593         if (IS_ERR(de_src) || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK)) {
594                 GOTO(out_link, rc = -ESTALE);
595         }
596
597         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
598         if (IS_ERR(de_tgt_dir)) {
599                 GOTO(out_link_de_src, rc = -ESTALE);
600         }
601
602         down(&de_tgt_dir->d_inode->i_sem);
603         dchild = lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen - 1);
604         if (IS_ERR(dchild)) {
605                 CERROR("child lookup error %ld\n", PTR_ERR(dchild));
606                 GOTO(out_link_de_tgt_dir, rc = -ESTALE);
607         }
608
609         if (dchild->d_inode) {
610                 CERROR("child exists (dir %ld, name %s\n",
611                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
612                 GOTO(out_link_dchild, rc = -EEXIST);
613         }
614
615         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE,
616                        de_src->d_inode->i_sb->s_dev);
617
618         handle = mds_fs_start(mds, de_tgt_dir->d_inode, MDS_FSOP_LINK);
619         if (!handle)
620                 GOTO(out_link_dchild, rc = PTR_ERR(handle));
621
622         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
623
624         if (!rc)
625                 rc = mds_update_last_rcvd(mds, handle, req);
626
627         err = mds_fs_commit(mds, de_tgt_dir->d_inode, handle);
628         if (err) {
629                 CERROR("error on commit: err = %d\n", err);
630                 if (!rc)
631                         rc = err;
632         }
633         EXIT;
634
635 out_link_dchild:
636         l_dput(dchild);
637 out_link_de_tgt_dir:
638         up(&de_tgt_dir->d_inode->i_sem);
639         l_dput(de_tgt_dir);
640 out_link_de_src:
641         l_dput(de_src);
642 out_link:
643         req->rq_status = rc;
644         return 0;
645 }
646
647 static int mds_reint_rename(struct mds_update_record *rec, int offset,
648                             struct ptlrpc_request *req)
649 {
650         struct dentry *de_srcdir = NULL;
651         struct dentry *de_tgtdir = NULL;
652         struct dentry *de_old = NULL;
653         struct dentry *de_new = NULL;
654         struct mds_obd *mds = mds_req2mds(req);
655         struct lustre_handle tgtlockh, srclockh, oldhandle;
656         int flags, lock_mode, rc = 0, err;
657         void *handle;
658         __u64 res_id[3] = {0};
659         ENTRY;
660
661         de_srcdir = mds_fid2dentry(mds, rec->ur_fid1, NULL);
662         if (IS_ERR(de_srcdir))
663                 GOTO(out_rename, rc = -ESTALE);
664
665         lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
666         res_id[0] = de_srcdir->d_inode->i_ino;
667
668         rc = ldlm_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
669                                    NULL, 0, lock_mode, &srclockh);
670         if (rc == 0) {
671                 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
672                 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
673                                       (struct lustre_handle *)&mds->mds_connh,
674                                       NULL, mds->mds_local_namespace, NULL,
675                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
676                                       &flags, (void *)mds_lock_callback, NULL,
677                                       0, &srclockh);
678                 if (rc != ELDLM_OK) {
679                         CERROR("lock enqueue: err: %d\n", rc);
680                         GOTO(out_rename_srcput, rc = -EIO);
681                 }
682         } else
683                 ldlm_lock_dump((void *)(unsigned long)srclockh.addr);
684
685         de_tgtdir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
686         if (IS_ERR(de_tgtdir))
687                 GOTO(out_rename_srcdir, rc = -ESTALE);
688
689         lock_mode = (req->rq_reqmsg->opc == MDS_REINT) ? LCK_CW : LCK_PW;
690         res_id[0] = de_tgtdir->d_inode->i_ino;
691
692         rc = ldlm_lock_match(mds->mds_local_namespace, res_id, LDLM_PLAIN,
693                                    NULL, 0, lock_mode, &tgtlockh);
694         if (rc == 0) {
695                 LDLM_DEBUG_NOLOCK("enqueue res %Lu", res_id[0]);
696                 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
697                                       (struct lustre_handle *)&mds->mds_connh,
698                                       NULL, mds->mds_local_namespace, NULL,
699                                       res_id, LDLM_PLAIN, NULL, 0, lock_mode,
700                                       &flags, (void *)mds_lock_callback, NULL,
701                                       0, &tgtlockh);
702                 if (rc != ELDLM_OK) {
703                         CERROR("lock enqueue: err: %d\n", rc);
704                         GOTO(out_rename_tgtput, rc = -EIO);
705                 }
706         } else
707                 ldlm_lock_dump((void *)(unsigned long)tgtlockh.addr);
708
709         double_lock(de_tgtdir, de_srcdir);
710
711         de_old = lookup_one_len(rec->ur_name, de_srcdir, rec->ur_namelen - 1);
712         if (IS_ERR(de_old)) {
713                 CERROR("old child lookup error (%*s): %ld\n",
714                        rec->ur_namelen - 1, rec->ur_name, PTR_ERR(de_old));
715                 GOTO(out_rename_tgtdir, rc = -ENOENT);
716         }
717
718         de_new = lookup_one_len(rec->ur_tgt, de_tgtdir, rec->ur_tgtlen - 1);
719         if (IS_ERR(de_new)) {
720                 CERROR("new child lookup error (%*s): %ld\n",
721                        rec->ur_tgtlen - 1, rec->ur_tgt, PTR_ERR(de_new));
722                 GOTO(out_rename_deold, rc = -ENOENT);
723         }
724
725         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
726                        de_srcdir->d_inode->i_sb->s_dev);
727
728         handle = mds_fs_start(mds, de_tgtdir->d_inode, MDS_FSOP_RENAME);
729         if (!handle)
730                 GOTO(out_rename_denew, rc = PTR_ERR(handle));
731         lock_kernel();
732         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new,
733                         NULL);
734         unlock_kernel();
735
736         if (!rc)
737                 rc = mds_update_last_rcvd(mds, handle, req);
738
739         err = mds_fs_commit(mds, de_tgtdir->d_inode, handle);
740         if (err) {
741                 CERROR("error on commit: err = %d\n", err);
742                 if (!rc)
743                         rc = err;
744         }
745         EXIT;
746
747 out_rename_denew:
748         l_dput(de_new);
749 out_rename_deold:
750         if (!rc) { 
751                 res_id[0] = de_old->d_inode->i_ino;
752                 /* Take an exclusive lock on the resource that we're
753                  * about to free, to force everyone to drop their
754                  * locks. */
755                 LDLM_DEBUG_NOLOCK("getting EX lock res %Lu", res_id[0]);
756                 rc = ldlm_cli_enqueue(mds->mds_ldlm_client, mds->mds_ldlm_conn,
757                                       (struct lustre_handle *)&mds->mds_connh,
758                                       NULL, mds->mds_local_namespace, NULL, 
759                                       res_id,
760                                       LDLM_PLAIN, NULL, 0, LCK_EX, &flags,
761                                       (void *)mds_lock_callback, NULL, 0, 
762                                       &oldhandle);
763                 if (rc) 
764                         CERROR("failed to get child inode lock (child ino %Ld, "
765                                "dir ino %ld)\n",
766                                res_id[0], de_old->d_inode->i_ino);
767         }
768
769         l_dput(de_old);
770
771         if (!rc) { 
772                 ldlm_lock_decref(&oldhandle, LCK_EX);
773                 rc = ldlm_cli_cancel(&oldhandle);
774                 if (rc < 0)
775                         CERROR("failed to cancel child inode lock ino "
776                                "%Ld: %d\n", res_id[0], rc);
777         }
778  out_rename_tgtdir:
779         double_up(&de_srcdir->d_inode->i_sem, &de_tgtdir->d_inode->i_sem);
780         ldlm_lock_decref(&tgtlockh, lock_mode);
781  out_rename_tgtput:
782         l_dput(de_tgtdir);
783  out_rename_srcdir:
784         ldlm_lock_decref(&srclockh, lock_mode);
785  out_rename_srcput:
786         l_dput(de_srcdir);
787  out_rename:
788         req->rq_status = rc;
789         return 0;
790 }
791
792 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
793                            struct ptlrpc_request *);
794
795 static mds_reinter reinters[REINT_MAX+1] = {
796         [REINT_SETATTR]   mds_reint_setattr,
797         [REINT_CREATE]    mds_reint_create,
798         [REINT_UNLINK]    mds_reint_unlink,
799         [REINT_LINK]      mds_reint_link,
800         [REINT_RENAME]    mds_reint_rename,
801         [REINT_RECREATE]  mds_reint_recreate,
802 };
803
804 int mds_reint_rec(struct mds_update_record *rec, int offset,
805                   struct ptlrpc_request *req)
806 {
807         struct mds_obd *mds = mds_req2mds(req);
808         struct obd_run_ctxt saved;
809
810         int rc;
811
812         if (rec->ur_opcode < 1 || rec->ur_opcode > REINT_MAX) {
813                 CERROR("opcode %d not valid\n", rec->ur_opcode);
814                 rc = req->rq_status = -EINVAL;
815                 RETURN(rc);
816         }
817
818         push_ctxt(&saved, &mds->mds_ctxt);
819         rc = reinters[rec->ur_opcode](rec, offset, req);
820         pop_ctxt(&saved);
821
822         return rc;
823 }