Whamcloud - gitweb
- one more fid management correctness test.
[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  *  Lustre Metadata Server (mds) reintegration routines
6  *
7  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8  *   Author: Peter Braam <braam@clusterfs.com>
9  *   Author: Andreas Dilger <adilger@clusterfs.com>
10  *   Author: Phil Schwan <phil@clusterfs.com>
11  *
12  *   This file is part of Lustre, http://www.lustre.org.
13  *
14  *   Lustre is free software; you can redistribute it and/or
15  *   modify it under the terms of version 2 of the GNU General Public
16  *   License as published by the Free Software Foundation.
17  *
18  *   Lustre is distributed in the hope that it will be useful,
19  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *   GNU General Public License for more details.
22  *
23  *   You should have received a copy of the GNU General Public License
24  *   along with Lustre; if not, write to the Free Software
25  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #ifndef EXPORT_SYMTAB
29 # define EXPORT_SYMTAB
30 #endif
31 #define DEBUG_SUBSYSTEM S_MDS
32
33 #include <linux/fs.h>
34 #include <linux/jbd.h>
35 #include <linux/ext3_fs.h>
36 #include <linux/obd_support.h>
37 #include <linux/obd_class.h>
38 #include <linux/obd.h>
39 #include <linux/lustre_lib.h>
40 #include <linux/lustre_idl.h>
41 #include <linux/lustre_mds.h>
42 #include <linux/lustre_dlm.h>
43 #include <linux/lustre_log.h>
44 #include <linux/lustre_fsfilt.h>
45 #include "mds_internal.h"
46
47 struct mds_logcancel_data {
48         struct lov_mds_md      *mlcd_lmm;
49         int                     mlcd_size;
50         int                     mlcd_cookielen;
51         int                     mlcd_eadatalen;
52         struct llog_cookie      mlcd_cookies[0];
53 };
54
55 static void mds_cancel_cookies_cb(struct obd_device *obd,
56                                   __u64 transno, void *cb_data,
57                                   int error)
58 {
59         struct mds_logcancel_data *mlcd = cb_data;
60         struct lov_stripe_md *lsm = NULL;
61         struct llog_ctxt *ctxt;
62         int rc;
63
64         obd_transno_commit_cb(obd, transno, error);
65
66         CDEBUG(D_HA, "cancelling %d cookies\n",
67                (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
68
69         rc = obd_unpackmd(obd->u.mds.mds_lov_exp, &lsm, mlcd->mlcd_lmm,
70                           mlcd->mlcd_eadatalen);
71         if (rc < 0) {
72                 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
73                        (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
74                        rc);
75         } else {
76                 ///* XXX 0 normally, SENDNOW for debug */);
77                 ctxt = llog_get_context(&obd->obd_llogs,
78                                         mlcd->mlcd_cookies[0].lgc_subsys + 1);
79                 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
80                                  sizeof(*mlcd->mlcd_cookies),
81                                  mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
82                 if (rc)
83                         CERROR("error cancelling %d log cookies: rc %d\n",
84                                (int)(mlcd->mlcd_cookielen /
85                                      sizeof(*mlcd->mlcd_cookies)), rc);
86         }
87
88         OBD_FREE(mlcd, mlcd->mlcd_size);
89 }
90
91 /* Assumes caller has already pushed us into the kernel context. */
92 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
93                        struct ptlrpc_request *req, int rc, __u32 op_data)
94 {
95         struct mds_export_data *med = &req->rq_export->exp_mds_data;
96         struct obd_device *obd = req->rq_export->exp_obd;
97         struct mds_client_data *mcd = med->med_mcd;
98         int err, log_pri = D_HA;
99         __u64 transno;
100         loff_t off;
101         ENTRY;
102
103         /* if the export has already been failed, we have no last_rcvd slot */
104         if (req->rq_export->exp_failed) {
105                 CERROR("committing transaction for disconnected client\n");
106                 if (handle)
107                         GOTO(out_commit, rc);
108                 RETURN(rc);
109         }
110
111         if (IS_ERR(handle))
112                 RETURN(rc);
113
114         if (handle == NULL) {
115                 /* if we're starting our own xaction, use our own inode */
116                 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
117                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
118                 if (IS_ERR(handle)) {
119                         CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
120                         RETURN(PTR_ERR(handle));
121                 }
122         }
123
124         off = med->med_off;
125
126         transno = req->rq_reqmsg->transno;
127         if (rc != 0) {
128                 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
129         } else if (transno == 0) {
130                 spin_lock(&mds->mds_transno_lock);
131                 transno = ++mds->mds_last_transno;
132                 spin_unlock(&mds->mds_transno_lock);
133         } else {
134                 spin_lock(&mds->mds_transno_lock);
135                 if (transno > mds->mds_last_transno)
136                         mds->mds_last_transno = transno;
137                 spin_unlock(&mds->mds_transno_lock);
138         }
139         req->rq_repmsg->transno = req->rq_transno = transno;
140         mcd->mcd_last_transno = cpu_to_le64(transno);
141         mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
142         mcd->mcd_last_result = cpu_to_le32(rc);
143         mcd->mcd_last_data = cpu_to_le32(op_data);
144
145         fsfilt_add_journal_cb(obd, mds->mds_sb, transno, handle,
146                               mds_commit_last_transno_cb, NULL);
147         
148         err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
149                                   sizeof(*mcd), &off, 0);
150
151         if (err) {
152                 log_pri = D_ERROR;
153                 if (rc == 0)
154                         rc = err;
155         }
156
157         DEBUG_REQ(log_pri, req,
158                   "wrote trans #"LPU64" client %s at idx %u: err = %d",
159                   transno, mcd->mcd_uuid, med->med_idx, err);
160
161         err = mds_update_last_fid(obd, handle, 0);
162         if (err) {
163                 log_pri = D_ERROR;
164                 if (rc == 0)
165                         rc = err;
166         }
167                 
168         err = mds_lov_write_objids(obd);
169         if (err) {
170                 log_pri = D_ERROR;
171                 if (rc == 0)
172                         rc = err;
173         }
174         CDEBUG(log_pri, "wrote objids: err = %d\n", err);
175
176         EXIT;
177 out_commit:
178         err = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
179         if (err) {
180                 CERROR("error committing transaction: %d\n", err);
181                 if (!rc)
182                         rc = err;
183         }
184
185         return rc;
186 }
187
188 /* this gives the same functionality as the code between
189  * sys_chmod and inode_setattr
190  * chown_common and inode_setattr
191  * utimes and inode_setattr
192  */
193 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
194 {
195         time_t now = LTIME_S(CURRENT_TIME);
196         struct iattr *attr = &rec->ur_iattr;
197         unsigned int ia_valid = attr->ia_valid;
198         int error;
199         ENTRY;
200
201         /* only fix up attrs if the client VFS didn't already */
202         if (!(ia_valid & ATTR_RAW))
203                 RETURN(0);
204
205         if (!(ia_valid & ATTR_CTIME_SET))
206                 LTIME_S(attr->ia_ctime) = now;
207         if (!(ia_valid & ATTR_ATIME_SET))
208                 LTIME_S(attr->ia_atime) = now;
209         if (!(ia_valid & ATTR_MTIME_SET))
210                 LTIME_S(attr->ia_mtime) = now;
211
212         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
213                 RETURN(-EPERM);
214
215         /* times */
216         if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
217                 if (rec->ur_fsuid != inode->i_uid &&
218                     (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
219                         RETURN(error);
220         }
221
222         if (ia_valid & ATTR_SIZE) {
223                 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
224                         RETURN(error);
225         }
226
227         if (ia_valid & ATTR_UID) {
228                 /* chown */
229                 error = -EPERM;
230                 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
231                         RETURN(-EPERM);
232                 if (attr->ia_uid == (uid_t) -1)
233                         attr->ia_uid = inode->i_uid;
234                 if (attr->ia_gid == (gid_t) -1)
235                         attr->ia_gid = inode->i_gid;
236                 attr->ia_mode = inode->i_mode;
237                 /*
238                  * If the user or group of a non-directory has been
239                  * changed by a non-root user, remove the setuid bit.
240                  * 19981026 David C Niemi <niemi@tux.org>
241                  *
242                  * Changed this to apply to all users, including root,
243                  * to avoid some races. This is the behavior we had in
244                  * 2.0. The check for non-root was definitely wrong
245                  * for 2.2 anyway, as it should have been using
246                  * CAP_FSETID rather than fsuid -- 19990830 SD.
247                  */
248                 if ((inode->i_mode & S_ISUID) == S_ISUID &&
249                     !S_ISDIR(inode->i_mode)) {
250                         attr->ia_mode &= ~S_ISUID;
251                         attr->ia_valid |= ATTR_MODE;
252                 }
253                 /*
254                  * Likewise, if the user or group of a non-directory
255                  * has been changed by a non-root user, remove the
256                  * setgid bit UNLESS there is no group execute bit
257                  * (this would be a file marked for mandatory
258                  * locking).  19981026 David C Niemi <niemi@tux.org>
259                  *
260                  * Removed the fsuid check (see the comment above) --
261                  * 19990830 SD.
262                  */
263                 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
264                      (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
265                         attr->ia_mode &= ~S_ISGID;
266                         attr->ia_valid |= ATTR_MODE;
267                 }
268         } else if (ia_valid & ATTR_MODE) {
269                 int mode = attr->ia_mode;
270                 /* chmod */
271                 if (attr->ia_mode == (mode_t) -1)
272                         attr->ia_mode = inode->i_mode;
273                 attr->ia_mode =
274                         (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
275         }
276         RETURN(0);
277 }
278
279 void mds_steal_ack_locks(struct ptlrpc_request *req)
280 {
281         struct obd_export         *exp = req->rq_export;
282         struct list_head          *tmp;
283         struct ptlrpc_reply_state *oldrep;
284         struct ptlrpc_service     *svc;
285         struct llog_create_locks  *lcl;
286         unsigned long              flags;
287         char                       str[PTL_NALFMT_SIZE];
288         int                        i;
289
290         /* CAVEAT EMPTOR: spinlock order */
291         spin_lock_irqsave (&exp->exp_lock, flags);
292         list_for_each (tmp, &exp->exp_outstanding_replies) {
293                 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
294
295                 if (oldrep->rs_xid != req->rq_xid)
296                         continue;
297
298                 if (oldrep->rs_msg.opc != req->rq_reqmsg->opc)
299                         CERROR ("Resent req xid "LPX64" has mismatched opc: "
300                                 "new %d old %d\n", req->rq_xid,
301                                 req->rq_reqmsg->opc, oldrep->rs_msg.opc);
302
303                 svc = oldrep->rs_srv_ni->sni_service;
304                 spin_lock (&svc->srv_lock);
305
306                 list_del_init (&oldrep->rs_exp_list);
307
308                 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
309                       " o%d NID %s\n",
310                       oldrep->rs_nlocks, oldrep,
311                       oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg.opc,
312                       ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
313
314                 for (i = 0; i < oldrep->rs_nlocks; i++)
315                         ptlrpc_save_lock(req,
316                                          &oldrep->rs_locks[i],
317                                          oldrep->rs_modes[i]);
318                 oldrep->rs_nlocks = 0;
319
320                 lcl = oldrep->rs_llog_locks;
321                 oldrep->rs_llog_locks = NULL;
322                 if (lcl != NULL)
323                         ptlrpc_save_llog_lock(req, lcl);
324
325                 DEBUG_REQ(D_HA, req, "stole locks for");
326                 ptlrpc_schedule_difficult_reply (oldrep);
327
328                 spin_unlock (&svc->srv_lock);
329                 spin_unlock_irqrestore (&exp->exp_lock, flags);
330                 return;
331         }
332         spin_unlock_irqrestore (&exp->exp_lock, flags);
333 }
334
335 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
336 {
337         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
338                   mcd->mcd_last_transno, mcd->mcd_last_result);
339         req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
340         req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
341
342         mds_steal_ack_locks(req);
343 }
344
345 static void reconstruct_reint_setattr(struct mds_update_record *rec,
346                                       int offset, struct ptlrpc_request *req)
347 {
348         struct mds_export_data *med = &req->rq_export->exp_mds_data;
349         struct mds_body *body;
350         struct dentry *de;
351
352         mds_req_from_mcd(req, med->med_mcd);
353
354         de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
355         if (IS_ERR(de)) {
356                 LASSERT(PTR_ERR(de) == req->rq_status);
357                 return;
358         }
359
360         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
361         mds_pack_inode2body(req2obd(req), body, de->d_inode, 0);
362
363         /* Don't return OST-specific attributes if we didn't just set them */
364         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
365                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
366         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
367                 body->valid |= OBD_MD_FLMTIME;
368         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
369                 body->valid |= OBD_MD_FLATIME;
370
371         l_dput(de);
372 }
373
374 /* In the raw-setattr case, we lock the child inode.
375  * In the write-back case or if being called from open, the client holds a lock
376  * already.
377  *
378  * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
379 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
380                              struct ptlrpc_request *req, struct lustre_handle *lh)
381 {
382         struct mds_obd *mds = mds_req2mds(req);
383         struct obd_device *obd = req->rq_export->exp_obd;
384         struct mds_body *body;
385         struct dentry *de;
386         struct inode *inode = NULL;
387         struct lustre_handle lockh[2] = {{0}, {0}};
388         int parent_mode;
389         void *handle = NULL;
390         struct mds_logcancel_data *mlcd = NULL;
391         int rc = 0, cleanup_phase = 0, err;
392         int locked = 0;
393         ENTRY;
394
395         LASSERT(offset == 1);
396
397         DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
398                   id_ino(rec->ur_id1), id_gen(rec->ur_id1),
399                   rec->ur_iattr.ia_valid);
400
401         MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
402         MD_COUNTER_INCREMENT(obd, setattr);
403
404         if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
405                 de = mds_id2dentry(obd, rec->ur_id1, NULL);
406                 if (IS_ERR(de))
407                         GOTO(cleanup, rc = PTR_ERR(de));
408         } else {
409                 __u64 lockpart = MDS_INODELOCK_UPDATE;
410                 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
411                         lockpart |= MDS_INODELOCK_LOOKUP;
412                 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
413                                           lockh, &parent_mode, NULL, 0, lockpart);
414                 if (IS_ERR(de))
415                         GOTO(cleanup, rc = PTR_ERR(de));
416                 locked = 1;
417         }
418
419         cleanup_phase = 1;
420
421         inode = de->d_inode;
422         LASSERT(inode);
423         if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
424             rec->ur_eadata != NULL)
425                 down(&inode->i_sem);
426
427         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
428
429         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
430         if (IS_ERR(handle))
431                 GOTO(cleanup, rc = PTR_ERR(handle));
432
433         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
434                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
435                        LTIME_S(rec->ur_iattr.ia_mtime),
436                        LTIME_S(rec->ur_iattr.ia_ctime));
437         rc = mds_fix_attr(inode, rec);
438         if (rc)
439                 GOTO(cleanup, rc);
440
441         if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG)    /* ioctl */
442                 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
443                                       (long)&rec->ur_iattr.ia_attr_flags);
444         else                                            /* setattr */
445                 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
446
447         if (rc == 0 && (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
448             rec->ur_eadata != NULL) {
449                 struct lov_stripe_md *lsm = NULL;
450
451                 rc = ll_permission(inode, MAY_WRITE, NULL);
452                 if (rc < 0)
453                         GOTO(cleanup, rc);
454
455                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, mds->mds_lov_exp,
456                                    0, &lsm, rec->ur_eadata);
457                 if (rc)
458                         GOTO(cleanup, rc);
459
460                 obd_free_memmd(mds->mds_lov_exp, &lsm);
461
462                 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
463                                    rec->ur_eadatalen);
464                 if (rc)
465                         GOTO(cleanup, rc);
466         }
467
468         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
469         mds_pack_inode2body(obd, body, inode, 0);
470         body->id1 = *rec->ur_id1;
471
472         /* Don't return OST-specific attributes if we didn't just set them */
473         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
474                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
475         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
476                 body->valid |= OBD_MD_FLMTIME;
477         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
478                 body->valid |= OBD_MD_FLATIME;
479
480         if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_lov_obd)) {
481                 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
482                           rec->ur_eadatalen);
483                 if (mlcd) {
484                         mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
485                                 rec->ur_eadatalen;
486                         mlcd->mlcd_eadatalen = rec->ur_eadatalen;
487                         mlcd->mlcd_cookielen = rec->ur_cookielen;
488                         mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
489                                 mlcd->mlcd_cookielen;
490                         memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
491                                mlcd->mlcd_cookielen);
492                         memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
493                                mlcd->mlcd_eadatalen);
494                 } else {
495                         CERROR("unable to allocate log cancel data\n");
496                 }
497         }
498         EXIT;
499  cleanup:
500         if (mlcd != NULL)
501                 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
502                                       handle, mds_cancel_cookies_cb, mlcd);
503         err = mds_finish_transno(mds, inode, handle, req, rc, 0);
504         switch (cleanup_phase) {
505         case 1:
506                 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
507                     rec->ur_eadata != NULL)
508                         up(&inode->i_sem);
509                 l_dput(de);
510                 if (locked) {
511 #ifdef S_PDIROPS
512                         if (lockh[1].cookie != 0)
513                                 ldlm_lock_decref(lockh + 1, parent_mode);
514 #endif
515                         if (rc) {
516                                 ldlm_lock_decref(lockh, LCK_PW);
517                         } else {
518                                 ptlrpc_save_lock (req, lockh, LCK_PW);
519                         }
520                 }
521         case 0:
522                 break;
523         default:
524                 LBUG();
525         }
526         if (err && !rc)
527                 rc = err;
528
529         req->rq_status = rc;
530         return 0;
531 }
532
533 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
534                                      struct ptlrpc_request *req)
535 {
536         struct mds_export_data *med = &req->rq_export->exp_mds_data;
537         struct dentry *parent, *child;
538         struct mds_body *body;
539         ENTRY;
540
541         mds_req_from_mcd(req, med->med_mcd);
542
543         if (req->rq_status) {
544                 EXIT;
545                 return;
546         }
547
548         parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
549         LASSERT(!IS_ERR(parent));
550         child = ll_lookup_one_len(rec->ur_name, parent,
551                                   rec->ur_namelen - 1);
552         LASSERT(!IS_ERR(child));
553         if ((child->d_flags & DCACHE_CROSS_REF)) {
554                 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
555                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
556                 mds_pack_dentry2body(req2obd(req), body, child, 1);
557         } else if (child->d_inode == NULL) {
558                 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
559                           OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
560                 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
561         } else {
562                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
563                 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
564         }
565         l_dput(parent);
566         l_dput(child);
567         EXIT;
568 }
569
570 static int mds_reint_create(struct mds_update_record *rec, int offset,
571                             struct ptlrpc_request *req,
572                             struct lustre_handle *lh)
573 {
574         struct dentry *dparent = NULL;
575         struct mds_obd *mds = mds_req2mds(req);
576         struct obd_device *obd = req->rq_export->exp_obd;
577         struct dentry *dchild = NULL;
578         struct inode *dir = NULL;
579         void *handle = NULL;
580         struct lustre_handle lockh[2] = {{0}, {0}};
581         int parent_mode;
582         int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
583         int created = 0;
584         struct dentry_params dp;
585         struct mea *mea = NULL;
586         int mea_size;
587         ENTRY;
588
589         LASSERT(offset == 1);
590         
591         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
592                         LUSTRE_MDS_NAME));
593
594         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
595                   id_ino(rec->ur_id1), id_gen(rec->ur_id1),
596                   rec->ur_name, rec->ur_mode);
597
598         MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
599
600         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
601                 GOTO(cleanup, rc = -ESTALE);
602
603         dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
604                                        lockh, &parent_mode, rec->ur_name,
605                                        rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
606         if (IS_ERR(dparent)) {
607                 rc = PTR_ERR(dparent);
608                 CERROR("parent lookup error %d\n", rc);
609                 GOTO(cleanup, rc);
610         }
611         cleanup_phase = 1; /* locked parent dentry */
612         dir = dparent->d_inode;
613         LASSERT(dir);
614
615         ldlm_lock_dump_handle(D_OTHER, lockh);
616
617         /* try to retrieve MEA data for this dir */
618         rc = mds_get_lmv_attr(obd, dparent->d_inode, &mea, &mea_size);
619         if (rc)
620                 GOTO(cleanup, rc);
621
622         if (mea != NULL) {
623                 /*
624                  * dir is already splitted, check is requested filename should
625                  * live at this MDS or at another one.
626                  */
627                 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
628                 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
629                         CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
630                                " should be %lu(%d)\n",
631                                mea->mea_master, dparent->d_inode->i_ino,
632                                dparent->d_inode->i_generation, rec->ur_name,
633                                (unsigned long)id_group(&mea->mea_ids[i]), i);
634                         GOTO(cleanup, rc = -ERESTART);
635                 }
636         }
637
638         dchild = ll_lookup_one_len(rec->ur_name, dparent, 
639                                    rec->ur_namelen - 1);
640         if (IS_ERR(dchild)) {
641                 rc = PTR_ERR(dchild);
642                 CERROR("Can't find "DLID4"/%s, error %d\n",
643                        OLID4(rec->ur_id1), rec->ur_name, rc);
644                 GOTO(cleanup, rc);
645         }
646
647         cleanup_phase = 2; /* child dentry */
648
649         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
650
651         if (type == S_IFREG || type == S_IFDIR) {
652                 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
653                 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
654                        obd->obd_name, dparent->d_inode->i_ino,
655                        dparent->d_inode->i_generation, rc, parent_mode);
656                 if (rc > 0) {
657                         /* dir got splitted */
658                         GOTO(cleanup, rc = -ERESTART);
659                 } else if (rc < 0) {
660                         /* error happened during spitting. */
661                         GOTO(cleanup, rc);
662                 }
663         }
664
665         if (dir->i_mode & S_ISGID) {
666                 if (S_ISDIR(rec->ur_mode))
667                         rec->ur_mode |= S_ISGID;
668         }
669
670         /*
671          * here inode number should be used only in the case of replaying. It is
672          * needed to check if object already created in the case of creating
673          * remote inode.
674          */
675         dchild->d_fsdata = (void *)&dp;
676         dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
677         dp.p_ptr = req;
678
679         switch (type) {
680         case S_IFREG:{
681                 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
682                 if (IS_ERR(handle))
683                         GOTO(cleanup, rc = PTR_ERR(handle));
684                 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
685                 EXIT;
686                 break;
687         }
688         case S_IFDIR:{
689                 int i, nstripes = 0;
690                 struct lustre_id sid;
691                 
692                 /*
693                  * as Peter asked, mkdir() should distribute new directories
694                  * over the whole cluster in order to distribute namespace
695                  * processing load. first, we calculate which MDS to use to put
696                  * new directory's inode in.
697                  */
698                 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1, 
699                                       rec->ur_flags);
700                 if (i == mds->mds_num) {
701                         /* inode will be created locally */
702                         handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
703                         if (IS_ERR(handle))
704                                 GOTO(cleanup, rc = PTR_ERR(handle));
705
706                         rc = vfs_mkdir(dir, dchild, rec->ur_mode);
707                         if (rc) {
708                                 CERROR("Can't create dir %s, rc = %d\n",
709                                        dchild->d_name.name, rc);
710                                 GOTO(cleanup, rc);
711                         }
712
713                         down(&dchild->d_inode->i_sem);
714                         if (dp.p_inum) {
715                                 rc = mds_update_inode_sid(obd, dchild->d_inode,
716                                                           handle, rec->ur_id2);
717                                 if (rc) {
718                                         CERROR("mds_update_inode_sid() failed, inode %lu, "
719                                                "rc %d\n", dchild->d_inode->i_ino, rc);
720                                 }
721
722                                 /* 
723                                  * make sure, that fid is up-to-date.
724                                  */
725                                 mds_set_last_fid(obd, id_fid(rec->ur_id2));
726                         } else {
727                                 rc = mds_alloc_inode_sid(obd, dchild->d_inode,
728                                                          handle, &sid);
729                                 if (rc) {
730                                         CERROR("mds_alloc_inode_sid() failed, inode %lu, "
731                                                "rc %d\n", dchild->d_inode->i_ino, rc);
732                                 }
733                         }
734                         up(&dchild->d_inode->i_sem);
735                         
736                         if (rc)
737                                 GOTO(cleanup, rc);
738                         
739                         if (rec->ur_eadata)
740                                 nstripes = *(u16 *)rec->ur_eadata;
741
742                         if (rc == 0 && nstripes) {
743                                 /*
744                                  * we pass LCK_EX to split routine to signal,
745                                  * that we have exclusive access to the
746                                  * directory. Simple because nobody knows it
747                                  * already exists -bzzz
748                                  */
749                                 rc = mds_try_to_split_dir(obd, dchild,
750                                                           NULL, nstripes,
751                                                           LCK_EX);
752                                 if (rc > 0) {
753                                         /* dir got splitted */
754                                         rc = 0;
755                                 } else if (rc < 0) {
756                                         /* an error occured during
757                                          * splitting. */
758                                         GOTO(cleanup, rc);
759                                 }
760                         }
761                 } else if (!DENTRY_VALID(dchild)) {
762                         /* inode will be created on another MDS */
763                         struct obdo *oa = NULL;
764                         struct mds_body *body;
765                         
766                         /* first, create that inode */
767                         oa = obdo_alloc();
768                         if (!oa)
769                                 GOTO(cleanup, rc = -ENOMEM);
770
771                         oa->o_mds = i;
772                         oa->o_easize = 0;
773
774                         if (rec->ur_eadata) {
775                                 /* user asks for creating splitted dir */
776                                 oa->o_easize = *((u16 *) rec->ur_eadata);
777                         }
778
779                         obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
780                                         OBD_MD_FLMTIME | OBD_MD_FLCTIME |
781                                         OBD_MD_FLUID | OBD_MD_FLGID);
782                         
783                         oa->o_mode = dir->i_mode;
784                         
785                         CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
786                                obd->obd_name, i);
787
788                         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
789                                 /*
790                                  * here inode number and generation are
791                                  * important, as this is replay request and we
792                                  * need them to check if such an object is
793                                  * already created.
794                                  */
795                                 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
796                                        obd->obd_name, rec->ur_namelen - 1,
797                                        rec->ur_name, (unsigned)id_ino(rec->ur_id2),
798                                        (unsigned)id_gen(rec->ur_id2));
799                                 oa->o_id = id_ino(rec->ur_id2);
800                                 oa->o_fid = id_fid(rec->ur_id2);
801                                 oa->o_generation = id_gen(rec->ur_id2);
802                                 oa->o_flags |= OBD_FL_RECREATE_OBJS;
803                         }
804
805                         /* before obd_create() is called, o_fid is not known. */
806                         rc = obd_create(mds->mds_lmv_exp, oa, NULL, NULL);
807                         if (rc) {
808                                 CERROR("can't create remote inode: %d\n", rc);
809                                 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
810                                           id_ino(rec->ur_id1), id_gen(rec->ur_id1),
811                                           rec->ur_name, rec->ur_mode);
812                                 obdo_free(oa);
813                                 GOTO(cleanup, rc);
814                         }
815                         
816                         /* now, add new dir entry for it */
817                         handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
818                         if (IS_ERR(handle)) {
819                                 obdo_free(oa);
820                                 GOTO(cleanup, rc = PTR_ERR(handle));
821                         }
822
823                         /* creating local dentry for remote inode. */
824                         rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
825                                                   rec->ur_namelen - 1, oa->o_id,
826                                                   oa->o_generation, i, oa->o_fid);
827
828                         if (rc) {
829                                 CERROR("Can't create local entry %*s for "
830                                        "remote inode.\n", rec->ur_namelen - 1,
831                                         rec->ur_name);
832                                 GOTO(cleanup, rc);
833                         }
834
835                         /* fill reply */
836                         body = lustre_msg_buf(req->rq_repmsg,
837                                               0, sizeof(*body));
838                         body->valid |= OBD_MD_FLID | OBD_MD_MDS |
839                                 OBD_MD_FID;
840
841                         obdo2id(&body->id1, oa);
842                         obdo_free(oa);
843                 } else {
844                         /* requested name exists in the directory */
845                         rc = -EEXIST;
846                 }
847                 EXIT;
848                 break;
849         }
850         case S_IFLNK:{
851                 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
852                 if (IS_ERR(handle))
853                         GOTO(cleanup, rc = PTR_ERR(handle));
854                 if (rec->ur_tgt == NULL)        /* no target supplied */
855                         rc = -EINVAL;           /* -EPROTO? */
856                 else
857                         rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
858                 EXIT;
859                 break;
860         }
861         case S_IFCHR:
862         case S_IFBLK:
863         case S_IFIFO:
864         case S_IFSOCK:{
865                 int rdev = rec->ur_rdev;
866                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
867                 if (IS_ERR(handle))
868                         GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
869                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
870                 EXIT;
871                 break;
872         }
873         default:
874                 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
875                 dchild->d_fsdata = NULL;
876                 GOTO(cleanup, rc = -EINVAL);
877         }
878
879         /* In case we stored the desired inum in here, we want to clean up. */
880         if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
881                 dchild->d_fsdata = NULL;
882
883         if (rc) {
884                 CDEBUG(D_INODE, "error during create: %d\n", rc);
885                 GOTO(cleanup, rc);
886         } else if (dchild->d_inode) {
887                 struct iattr iattr;
888                 struct mds_body *body;
889                 struct inode *inode = dchild->d_inode;
890
891                 created = 1;
892                 iattr.ia_uid = rec->ur_fsuid;
893                 LTIME_S(iattr.ia_atime) = rec->ur_time;
894                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
895                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
896
897                 if (dir->i_mode & S_ISGID)
898                         iattr.ia_gid = dir->i_gid;
899                 else
900                         iattr.ia_gid = rec->ur_fsgid;
901
902                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
903                         ATTR_MTIME | ATTR_CTIME;
904
905                 if (id_ino(rec->ur_id2)) {
906                         LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
907                         inode->i_generation = id_gen(rec->ur_id2);
908
909                         if (type != S_IFDIR) {
910                                 /* 
911                                  * updating inode self id, as inode already
912                                  * exists and we should make sure, its sid will
913                                  * be the same as we reveived.
914                                  */
915                                 down(&inode->i_sem);
916                                 rc = mds_update_inode_sid(obd, inode,
917                                                           handle, rec->ur_id2);
918                                 up(&inode->i_sem);
919                                 if (rc) {
920                                         CERROR("Can't update inode self id, "
921                                                "rc = %d.\n", rc);
922                                 }
923
924                                 /* 
925                                  * make sure, that fid is up-to-date.
926                                  */
927                                 mds_set_last_fid(obd, id_fid(rec->ur_id2));
928                         }
929                         
930                         /* dirtied and committed by the upcoming setattr. */
931                         CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
932                                inode->i_ino, inode->i_generation);
933                 } else {
934                         struct lustre_handle child_ino_lockh;
935
936                         CDEBUG(D_INODE, "created ino %lu with gen %x\n",
937                                inode->i_ino, inode->i_generation);
938
939                         if (type != S_IFDIR) {
940                                 struct lustre_id sid;
941                                 
942                                 /* 
943                                  * allocate new id for @inode if it is not dir,
944                                  * because for dir it was already done.
945                                  */
946                                 down(&inode->i_sem);
947                                 rc = mds_alloc_inode_sid(obd, inode,
948                                                          handle, &sid);
949                                 up(&inode->i_sem);
950                                 if (rc) {
951                                         CERROR("mds_alloc_inode_sid() failed, "
952                                                "inode %lu, rc %d\n", inode->i_ino,
953                                                rc);
954                                 }
955                         }
956
957                         if (rc == 0) {
958                                 /*
959                                  * the inode we were allocated may have just
960                                  * been freed by an unlink operation.  We take
961                                  * this lock to synchronize against the matching
962                                  * reply-ack-lock taken in unlink, to avoid
963                                  * replay problems if this reply makes it out to
964                                  * the client but the unlink's does not.  See
965                                  * bug 2029 for more detail.
966                                  */
967                                 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
968                                 if (rc != ELDLM_OK) {
969                                         CERROR("error locking for unlink/create sync: "
970                                                "%d\n", rc);
971                                 } else {
972                                         ldlm_lock_decref(&child_ino_lockh, LCK_EX);
973                                 }
974                         }
975                 }
976
977                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
978                 if (rc)
979                         CERROR("error on child setattr: rc = %d\n", rc);
980
981                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
982                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
983                 if (rc)
984                         CERROR("error on parent setattr: rc = %d\n", rc);
985                 else
986                         MD_COUNTER_INCREMENT(obd, create);
987
988                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
989                 mds_pack_inode2body(obd, body, inode, 1);
990         }
991
992         EXIT;
993 cleanup:
994         err = mds_finish_transno(mds, dir, handle, req, rc, 0);
995
996         if (rc && created) {
997                 /* Destroy the file we just created. This should not need extra
998                  * journal credits, as we have already modified all of the
999                  * blocks needed in order to create the file in the first
1000                  * place. */
1001                 switch (type) {
1002                 case S_IFDIR:
1003                         err = vfs_rmdir(dir, dchild);
1004                         if (err)
1005                                 CERROR("rmdir in error path: %d\n", err);
1006                         break;
1007                 default:
1008                         err = vfs_unlink(dir, dchild);
1009                         if (err)
1010                                 CERROR("unlink in error path: %d\n", err);
1011                         break;
1012                 }
1013         } else {
1014                 rc = err;
1015         }
1016         switch (cleanup_phase) {
1017         case 2: /* child dentry */
1018                 l_dput(dchild);
1019         case 1: /* locked parent dentry */
1020 #ifdef S_PDIROPS
1021                 if (lockh[1].cookie != 0)
1022                         ldlm_lock_decref(lockh + 1, parent_mode);
1023 #endif
1024                 if (rc) {
1025                         ldlm_lock_decref(lockh, LCK_PW);
1026                 } else {
1027                         ptlrpc_save_lock(req, lockh, LCK_PW);
1028                 }
1029                 l_dput(dparent);
1030         case 0:
1031                 break;
1032         default:
1033                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1034                 LBUG();
1035         }
1036         if (mea)
1037                 OBD_FREE(mea, mea_size);
1038         req->rq_status = rc;
1039         return 0;
1040 }
1041
1042 static inline int
1043 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1044        ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1045 {
1046         int i;
1047
1048         for (i = 0; i < RES_NAME_SIZE; i++) {
1049                 /* 
1050                  * this is needed to make zeroed res_id entries to be put at the
1051                  * end of list in *ordered_locks() .
1052                  */
1053                 if (res1->name[i] == 0 && res2->name[i] != 0)
1054                         return 1;
1055                 if (res2->name[i] == 0 && res1->name[i] != 0)
1056                         return 0;
1057                 if (res1->name[i] > res2->name[i])
1058                         return 1;
1059                 if (res1->name[i] < res2->name[i])
1060                         return 0;
1061         }
1062
1063         if (!p1 || !p2)
1064                 return 0;
1065
1066         if (memcmp(p1, p2, sizeof(*p1)) < 0)
1067                 return 1;
1068
1069         return 0;
1070 }
1071
1072 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1073  * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1074  * because they take the place of local semaphores.
1075  *
1076  * One or two locks are taken in numerical order.  A res_id->name[0] of 0 means
1077  * no lock is taken for that res_id.  Must be at least one non-zero res_id. */
1078 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1079                           struct lustre_handle *p1_lockh, int p1_lock_mode,
1080                           ldlm_policy_data_t *p1_policy,
1081                           struct ldlm_res_id *p2_res_id,
1082                           struct lustre_handle *p2_lockh, int p2_lock_mode,
1083                           ldlm_policy_data_t *p2_policy)
1084 {
1085         int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1086         struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1087         struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1088         ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1089         int rc, flags;
1090         ENTRY;
1091
1092         LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1093
1094         CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1095                res_id[0]->name[0], res_id[1]->name[0]);
1096
1097         if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1098                 handles[1] = p1_lockh;
1099                 handles[0] = p2_lockh;
1100                 res_id[1] = p1_res_id;
1101                 res_id[0] = p2_res_id;
1102                 lock_modes[1] = p1_lock_mode;
1103                 lock_modes[0] = p2_lock_mode;
1104                 policies[1] = p1_policy;
1105                 policies[0] = p2_policy;
1106         }
1107
1108         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1109                res_id[0]->name[0], res_id[1]->name[0]);
1110
1111         flags = LDLM_FL_LOCAL_ONLY;
1112         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1113                               LDLM_IBITS, policies[0], lock_modes[0], &flags,
1114                               mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1115                               NULL, 0, NULL, handles[0]);
1116         if (rc != ELDLM_OK)
1117                 RETURN(-EIO);
1118         ldlm_lock_dump_handle(D_OTHER, handles[0]);
1119
1120         if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1121             (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1122                 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1123                 ldlm_lock_addref(handles[1], lock_modes[1]);
1124         } else if (res_id[1]->name[0] != 0) {
1125                 flags = LDLM_FL_LOCAL_ONLY;
1126                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1127                                       *res_id[1], LDLM_IBITS, policies[1],
1128                                       lock_modes[1], &flags, mds_blocking_ast,
1129                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
1130                                       NULL, handles[1]);
1131                 if (rc != ELDLM_OK) {
1132                         ldlm_lock_decref(handles[0], lock_modes[0]);
1133                         RETURN(-EIO);
1134                 }
1135                 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1136         }
1137
1138         RETURN(0);
1139 }
1140
1141 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1142                            struct lustre_handle *p1_lockh, int p1_lock_mode,
1143                            ldlm_policy_data_t *p1_policy,
1144                            struct ldlm_res_id *p2_res_id,
1145                            struct lustre_handle *p2_lockh, int p2_lock_mode,
1146                            ldlm_policy_data_t *p2_policy,
1147                            struct ldlm_res_id *c1_res_id,
1148                            struct lustre_handle *c1_lockh, int c1_lock_mode,
1149                            ldlm_policy_data_t *c1_policy,
1150                            struct ldlm_res_id *c2_res_id,
1151                            struct lustre_handle *c2_lockh, int c2_lock_mode,
1152                            ldlm_policy_data_t *c2_policy)
1153 {
1154         struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1155                                           c1_res_id, c2_res_id };
1156         struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1157                                                  c1_lockh, c2_lockh };
1158         int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1159                               c1_lock_mode, c2_lock_mode };
1160         ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1161                                             c1_policy, c2_policy};
1162         int rc, i, j, sorted, flags;
1163         ENTRY;
1164
1165         CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1166                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1167                res_id[3]->name[0]);
1168
1169         /* 
1170          * simple insertion sort - we have at most 4 elements. Note, that zeroed
1171          * res_id should be at the end of list after sorting is finished.
1172          */
1173         for (i = 1; i < 4; i++) {
1174                 j = i - 1;
1175                 dlm_handles[4] = dlm_handles[i];
1176                 res_id[4] = res_id[i];
1177                 lock_modes[4] = lock_modes[i];
1178                 policies[4] = policies[i];
1179
1180                 sorted = 0;
1181                 do {
1182                         if (res_gt(res_id[j], res_id[4], policies[j],
1183                                    policies[4])) {
1184                                 dlm_handles[j + 1] = dlm_handles[j];
1185                                 res_id[j + 1] = res_id[j];
1186                                 lock_modes[j + 1] = lock_modes[j];
1187                                 policies[j + 1] = policies[j];
1188                                 j--;
1189                         } else {
1190                                 sorted = 1;
1191                         }
1192                 } while (j >= 0 && !sorted);
1193
1194                 dlm_handles[j + 1] = dlm_handles[4];
1195                 res_id[j + 1] = res_id[4];
1196                 lock_modes[j + 1] = lock_modes[4];
1197                 policies[j + 1] = policies[4];
1198         }
1199
1200         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1201                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1202                res_id[3]->name[0]);
1203
1204         /* XXX we could send ASTs on all these locks first before blocking? */
1205         for (i = 0; i < 4; i++) {
1206                 flags = 0;
1207
1208                 /* 
1209                  * nevertheless zeroed res_ids should be at the end of list, and
1210                  * could use break here, I think, that it is more correctly for
1211                  * clear understanding of code to have continue here, as it
1212                  * clearly means, that zeroed res_id should be skipped and does
1213                  * not mean, that if we meet zeroed res_id we should stop
1214                  * locking loop.
1215                  */
1216                 if (res_id[i]->name[0] == 0)
1217                         continue;
1218                 
1219                 if (i != 0 &&
1220                     !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1221                     (policies[i]->l_inodebits.bits &
1222                      policies[i-1]->l_inodebits.bits) ) {
1223                         memcpy(dlm_handles[i], dlm_handles[i-1],
1224                                sizeof(*(dlm_handles[i])));
1225                         ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1226                 } else {
1227                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1228                                               *res_id[i], LDLM_IBITS,
1229                                               policies[i],
1230                                               lock_modes[i], &flags,
1231                                               mds_blocking_ast,
1232                                               ldlm_completion_ast, NULL, NULL,
1233                                               NULL, 0, NULL, dlm_handles[i]);
1234                         if (rc != ELDLM_OK)
1235                                 GOTO(out_err, rc = -EIO);
1236                         ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1237                 }
1238         }
1239
1240         RETURN(0);
1241 out_err:
1242         while (i-- > 0)
1243                 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1244
1245         return rc;
1246 }
1247
1248 /* In the unlikely case that the child changed while we were waiting
1249  * on the lock, we need to drop the lock on the old child and either:
1250  * - if the child has a lower resource name, then we have to also
1251  *   drop the parent lock and regain the locks in the right order
1252  * - in the rename case, if the child has a lower resource name than one of
1253  *   the other parent/child resources (maxres) we also need to reget the locks
1254  * - if the child has a higher resource name (this is the common case)
1255  *   we can just get the lock on the new child (still in lock order)
1256  *
1257  * Returns 0 if the child did not change or if it changed but could be locked.
1258  * Returns 1 if the child changed and we need to re-lock (no locks held).
1259  * Returns -ve error with a valid dchild (no locks held). */
1260 static int mds_verify_child(struct obd_device *obd,
1261                             struct ldlm_res_id *parent_res_id,
1262                             struct lustre_handle *parent_lockh,
1263                             struct dentry *dparent, int parent_mode,
1264                             struct ldlm_res_id *child_res_id,
1265                             struct lustre_handle *child_lockh,
1266                             struct dentry **dchildp, int child_mode,
1267                             ldlm_policy_data_t *child_policy,
1268                             const char *name, int namelen,
1269                             struct ldlm_res_id *maxres,
1270                             unsigned long child_ino,
1271                             __u32 child_gen)
1272 {
1273         struct lustre_id sid;
1274         struct dentry *vchild, *dchild = *dchildp;
1275         int rc = 0, cleanup_phase = 2; /* parent, child locks */
1276         ENTRY;
1277
1278         vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1279         if (IS_ERR(vchild))
1280                 GOTO(cleanup, rc = PTR_ERR(vchild));
1281
1282         if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1283                 if (child_gen == vchild->d_generation &&
1284                     child_ino == vchild->d_inum) {
1285                         if (dchild)
1286                                 l_dput(dchild);
1287                         *dchildp = vchild;
1288                         RETURN(0);
1289                 }
1290                 goto changed;
1291         }
1292
1293         if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1294                    (vchild->d_inode != NULL &&
1295                     child_gen == vchild->d_inode->i_generation &&
1296                     child_ino == vchild->d_inode->i_ino))) {
1297                 if (dchild)
1298                         l_dput(dchild);
1299                 *dchildp = vchild;
1300                 RETURN(0);
1301         }
1302
1303 changed:
1304         CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1305                vchild->d_inode, dchild ? dchild->d_inode : 0,
1306                vchild->d_inode ? vchild->d_inode->i_ino : 0,
1307                child_res_id->name[0]);
1308
1309         if (child_res_id->name[0] != 0)
1310                 ldlm_lock_decref(child_lockh, child_mode);
1311         if (dchild)
1312                 l_dput(dchild);
1313
1314         cleanup_phase = 1; /* parent lock only */
1315         *dchildp = dchild = vchild;
1316
1317         if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1318                 int flags = 0;
1319                 
1320                 if (dchild->d_inode) {
1321                         down(&dchild->d_inode->i_sem);
1322                         rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1323                         up(&dchild->d_inode->i_sem);
1324                         if (rc) {
1325                                 CERROR("Can't read inode self id, inode %lu,"
1326                                        " rc %d\n",  dchild->d_inode->i_ino, rc);
1327                                 GOTO(cleanup, rc);
1328                         }
1329                         child_res_id->name[0] = id_fid(&sid);
1330                         child_res_id->name[1] = id_group(&sid);
1331                 } else {
1332                         child_res_id->name[0] = dchild->d_fid;
1333                         child_res_id->name[1] = dchild->d_mdsnum;
1334                 }
1335
1336                 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1337                     res_gt(maxres, child_res_id, NULL, NULL)) {
1338                         CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1339                                child_res_id->name[0], parent_res_id->name[0],
1340                                maxres->name[0]);
1341                         GOTO(cleanup, rc = 1);
1342                 }
1343
1344                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1345                                       *child_res_id, LDLM_IBITS, child_policy,
1346                                       child_mode, &flags, mds_blocking_ast,
1347                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
1348                                       NULL, child_lockh);
1349                 if (rc != ELDLM_OK)
1350                         GOTO(cleanup, rc = -EIO);
1351
1352         } else {
1353                 memset(child_res_id, 0, sizeof(*child_res_id));
1354         }
1355
1356         EXIT;
1357 cleanup:
1358         if (rc) {
1359                 switch(cleanup_phase) {
1360                 case 2:
1361                         if (child_res_id->name[0] != 0)
1362                                 ldlm_lock_decref(child_lockh, child_mode);
1363                 case 1:
1364                         ldlm_lock_decref(parent_lockh, parent_mode);
1365                 }
1366         }
1367         return rc;
1368 }
1369
1370 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1371                                 struct lustre_id *id,
1372                                 struct lustre_handle *parent_lockh,
1373                                 struct dentry **dparentp, int parent_mode,
1374                                 __u64 parent_lockpart, int *update_mode,
1375                                 char *name, int namelen,
1376                                 struct lustre_handle *child_lockh,
1377                                 struct dentry **dchildp, int child_mode,
1378                                 __u64 child_lockpart)
1379 {
1380         ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1381         ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1382         struct ldlm_res_id parent_res_id = { .name = {0} };
1383         struct ldlm_res_id child_res_id = { .name = {0} };
1384         int rc = 0, cleanup_phase = 0;
1385         unsigned long child_ino;
1386         struct lustre_id sid;
1387         __u32 child_gen = 0;
1388         struct inode *inode;
1389         ENTRY;
1390
1391         /* Step 1: Lookup parent */
1392         *dparentp = mds_id2dentry(obd, id, NULL);
1393         if (IS_ERR(*dparentp)) {
1394                 rc = PTR_ERR(*dparentp);
1395                 *dparentp = NULL;
1396                 RETURN(rc);
1397         }
1398
1399         CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1400                (*dparentp)->d_inode->i_ino, name);
1401
1402         parent_res_id.name[0] = id_fid(id);
1403         parent_res_id.name[1] = id_group(id);
1404         
1405 #ifdef S_PDIROPS
1406         parent_lockh[1].cookie = 0;
1407         if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1408                 struct ldlm_res_id res_id = { .name = {0} };
1409                 ldlm_policy_data_t policy;
1410                 int flags = 0;
1411
1412                 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1413                 if (*update_mode) {
1414                         res_id.name[0] = id_fid(id);
1415                         res_id.name[1] = id_group(id);
1416                         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1417
1418                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1419                                               res_id, LDLM_IBITS, &policy,
1420                                               *update_mode, &flags,
1421                                               mds_blocking_ast,
1422                                               ldlm_completion_ast,
1423                                               NULL, NULL, NULL, 0, NULL,
1424                                               parent_lockh + 1);
1425                         if (rc != ELDLM_OK)
1426                                 RETURN(-ENOLCK);
1427                 }
1428
1429                 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1430                 
1431                 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1432                        (unsigned long)id_fid(id), (unsigned long)id_group(id),
1433                        parent_res_id.name[2]);
1434         }
1435 #endif
1436
1437         cleanup_phase = 1; /* parent dentry */
1438
1439         /* Step 2: Lookup child (without DLM lock, to get resource name) */
1440         *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1441         if (IS_ERR(*dchildp)) {
1442                 rc = PTR_ERR(*dchildp);
1443                 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1444                 GOTO(cleanup, rc);
1445         }
1446
1447         if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1448                 /*
1449                  * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1450                  * lock. Drop possible UPDATE lock!
1451                  */
1452                 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1453                 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1454
1455                 child_res_id.name[0] = (*dchildp)->d_fid;
1456                 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1457                 child_gen = (*dchildp)->d_generation;
1458                 child_ino = (*dchildp)->d_inum;
1459                 goto retry_locks;
1460         }
1461
1462         inode = (*dchildp)->d_inode;
1463         if (inode != NULL)
1464                 inode = igrab(inode);
1465         if (inode == NULL)
1466                 goto retry_locks;
1467
1468         down(&inode->i_sem);
1469         rc = mds_read_inode_sid(obd, inode, &sid);
1470         up(&inode->i_sem);
1471         if (rc) {
1472                 CERROR("Can't read inode self id, inode %lu, "
1473                        "rc %d\n", inode->i_ino, rc);
1474                 iput(inode);
1475                 GOTO(cleanup, rc);
1476         }
1477         
1478         child_res_id.name[0] = id_fid(&sid);
1479         child_res_id.name[1] = id_group(&sid);
1480         child_gen = inode->i_generation;
1481         child_ino = inode->i_ino;
1482         iput(inode);
1483
1484 retry_locks:
1485         cleanup_phase = 2; /* child dentry */
1486
1487         /* Step 3: Lock parent and child in resource order.  If child doesn't
1488          * exist, we still have to lock the parent and re-lookup. */
1489         rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1490                                    &parent_policy, &child_res_id, child_lockh,
1491                                    child_mode, &child_policy);
1492         if (rc)
1493                 GOTO(cleanup, rc);
1494
1495         if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1496                 cleanup_phase = 4; /* child lock */
1497         else
1498                 cleanup_phase = 3; /* parent lock */
1499
1500         /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1501         rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1502                               parent_mode, &child_res_id, child_lockh, 
1503                               dchildp, child_mode, &child_policy,
1504                               name, namelen, &parent_res_id,
1505                               child_ino, child_gen);
1506         if (rc > 0)
1507                 goto retry_locks;
1508         if (rc < 0) {
1509                 cleanup_phase = 3;
1510                 GOTO(cleanup, rc);
1511         }
1512
1513         EXIT;
1514 cleanup:
1515         if (rc) {
1516                 switch (cleanup_phase) {
1517                 case 4:
1518                         ldlm_lock_decref(child_lockh, child_mode);
1519                 case 3:
1520                         ldlm_lock_decref(parent_lockh, parent_mode);
1521                 case 2:
1522                         l_dput(*dchildp);
1523                 case 1:
1524 #ifdef S_PDIROPS
1525                         if (parent_lockh[1].cookie)
1526                                 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1527 #endif
1528                         l_dput(*dparentp);
1529                 }
1530         }
1531         return rc;
1532 }
1533
1534 void mds_reconstruct_generic(struct ptlrpc_request *req)
1535 {
1536         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1537         mds_req_from_mcd(req, med->med_mcd);
1538 }
1539
1540 /* If we are unlinking an open file/dir (i.e. creating an orphan) then
1541  * we instead link the inode into the PENDING directory until it is
1542  * finally released.  We can't simply call mds_reint_rename() or some
1543  * part thereof, because we don't have the inode to check for link
1544  * count/open status until after it is locked.
1545  *
1546  * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1547  * before starting journal transaction.
1548  *
1549  * returns 1 on success
1550  * returns 0 if we lost a race and didn't make a new link
1551  * returns negative on error
1552  */
1553 static int mds_orphan_add_link(struct mds_update_record *rec,
1554                                struct obd_device *obd, struct dentry *dentry)
1555 {
1556         struct mds_obd *mds = &obd->u.mds;
1557         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1558         struct inode *inode = dentry->d_inode;
1559         struct dentry *pending_child;
1560         char idname[LL_ID_NAMELEN];
1561         int idlen = 0, rc, mode;
1562         ENTRY;
1563
1564         LASSERT(inode != NULL);
1565         LASSERT(!mds_inode_is_orphan(inode));
1566 #ifndef HAVE_I_ALLOC_SEM
1567         LASSERT(down_trylock(&inode->i_sem) != 0);
1568 #endif
1569         LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1570
1571         idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1572
1573         CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1574                mds_orphan_open_count(inode), inode->i_nlink,
1575                S_ISDIR(inode->i_mode) ? "dir" :
1576                S_ISREG(inode->i_mode) ? "file" : "other",
1577                rec->ur_name, idname);
1578
1579         if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1580                 RETURN(0);
1581
1582         pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1583         if (IS_ERR(pending_child))
1584                 RETURN(PTR_ERR(pending_child));
1585
1586         if (pending_child->d_inode != NULL) {
1587                 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1588                 LASSERT(pending_child->d_inode == inode);
1589                 GOTO(out_dput, rc = 0);
1590         }
1591
1592         /* link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG
1593          * for linking and return real mode back then -bzzz */
1594         mode = inode->i_mode;
1595         inode->i_mode = S_IFREG;
1596         rc = vfs_link(dentry, pending_dir, pending_child);
1597         if (rc)
1598                 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1599                        rec->ur_name, rc);
1600         else
1601                 mds_inode_set_orphan(inode);
1602
1603         /* return mode and correct i_nlink if inode is directory */
1604         inode->i_mode = mode;
1605         LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1606                  S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1607                  inode->i_nlink);
1608         if (S_ISDIR(mode)) {
1609                 inode->i_nlink++;
1610                 pending_dir->i_nlink++;
1611                 mark_inode_dirty(inode);
1612                 mark_inode_dirty(pending_dir);
1613         }
1614
1615         EXIT;
1616 out_dput:
1617         l_dput(pending_child);
1618         return rc;
1619 }
1620
1621 int mds_create_local_dentry(struct mds_update_record *rec,
1622                             struct obd_device *obd)
1623 {
1624         struct mds_obd *mds = &obd->u.mds;
1625         struct inode *id_dir = mds->mds_id_dir->d_inode;
1626         int idlen = 0, rc, cleanup_phase = 0;
1627         struct dentry *new_child = NULL;
1628         char *idname = rec->ur_name;
1629         struct dentry *child = NULL;
1630         struct lustre_handle lockh[2] = {{0}, {0}};
1631         struct lustre_id sid;
1632         void *handle;
1633         ENTRY;
1634
1635         down(&id_dir->i_sem);
1636         idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1637                           id_gen(rec->ur_id1));
1638         
1639         CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1640                idname, OLID4(rec->ur_id1));
1641
1642         new_child = ll_lookup_one_len(idname, mds->mds_id_dir, 
1643                                       idlen);
1644         up(&id_dir->i_sem);
1645         if (IS_ERR(new_child)) {
1646                 CERROR("can't lookup %s: %d\n", idname,
1647                        (int) PTR_ERR(new_child));
1648                 GOTO(cleanup, rc = PTR_ERR(new_child));
1649         }
1650         cleanup_phase = 1;
1651
1652         down(&id_dir->i_sem);
1653         rc = mds_read_inode_sid(obd, id_dir, &sid);
1654         up(&id_dir->i_sem);
1655         if (rc) {
1656                 CERROR("Can't read inode self id, inode %lu, "
1657                        "rc %d\n", id_dir->i_ino, rc);
1658                 GOTO(cleanup, rc);
1659         }
1660         
1661         if (new_child->d_inode != NULL) {
1662                 /* nice. we've already have local dentry! */
1663                 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n", 
1664                        (unsigned)new_child->d_inode->i_ino,
1665                        (unsigned)new_child->d_inode->i_generation);
1666                 
1667                 id_ino(rec->ur_id1) = id_dir->i_ino;
1668                 id_gen(rec->ur_id1) = id_dir->i_generation;
1669                 rec->ur_namelen = idlen + 1;
1670
1671                 id_fid(rec->ur_id1) = id_fid(&sid);
1672                 id_group(rec->ur_id1) = id_group(&sid);
1673                 
1674                 GOTO(cleanup, rc = 0);
1675         }
1676
1677         /* new, local dentry will be added soon. we need no aliases here */
1678         d_drop(new_child);
1679
1680         if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1681                 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1682         } else {
1683                 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1684                                              LCK_EX, lockh, NULL, NULL, 0,
1685                                              MDS_INODELOCK_UPDATE);
1686         }
1687
1688         if (IS_ERR(child)) {
1689                 rc = PTR_ERR(child);
1690                 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1691                         CERROR("can't get victim: %d\n", rc);
1692                 GOTO(cleanup, rc);
1693         }
1694         cleanup_phase = 2;
1695
1696         handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1697         if (IS_ERR(handle))
1698                 GOTO(cleanup, rc = PTR_ERR(handle));
1699
1700         rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1701                                   idlen, id_ino(rec->ur_id1),
1702                                   id_gen(rec->ur_id1), mds->mds_num,
1703                                   id_fid(rec->ur_id1));
1704         if (rc)
1705                 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1706                        (unsigned long)child->d_inode->i_ino,
1707                        (unsigned long)child->d_inode->i_generation, rc);
1708         else {
1709                 if (S_ISDIR(child->d_inode->i_mode)) {
1710                         id_dir->i_nlink++;
1711                         mark_inode_dirty(id_dir);
1712                 }
1713                 mark_inode_dirty(child->d_inode);
1714         }
1715         fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1716
1717         id_ino(rec->ur_id1) = id_dir->i_ino;
1718         id_gen(rec->ur_id1) = id_dir->i_generation;
1719         rec->ur_namelen = idlen + 1;
1720
1721         id_fid(rec->ur_id1) = id_fid(&sid);
1722         id_group(rec->ur_id1) = id_group(&sid);
1723
1724         EXIT;
1725 cleanup:
1726         switch(cleanup_phase) {
1727                 case 2:
1728                         if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1729                                 ldlm_lock_decref(lockh, LCK_EX);
1730                         dput(child);
1731                 case 1:
1732                         dput(new_child);
1733                 case 0:
1734                        break; 
1735         }
1736         return rc;
1737 }
1738
1739 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1740                                  struct ptlrpc_request *slave)
1741 {
1742         void *cookie, *cookie2;
1743         struct mds_body *body2;
1744         struct mds_body *body;
1745         void *ea, *ea2;
1746         ENTRY;
1747
1748         body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1749         LASSERT(body != NULL);
1750
1751         body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1752         LASSERT(body2 != NULL);
1753
1754         if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1755                 RETURN(0);
1756
1757         memcpy(body2, body, sizeof(*body));
1758         body2->valid &= ~OBD_MD_FLCOOKIE;
1759
1760         if (!(body->valid & OBD_MD_FLEASIZE) &&
1761             !(body->valid & OBD_MD_FLDIREA))
1762                 RETURN(0);
1763
1764         if (body->eadatasize == 0) {
1765                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1766                 RETURN(0);
1767         }
1768
1769         LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1770         
1771         ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1772         LASSERT(ea != NULL);
1773         
1774         ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1775         LASSERT(ea2 != NULL);
1776
1777         memcpy(ea2, ea, body->eadatasize);
1778
1779         if (body->valid & OBD_MD_FLCOOKIE) {
1780                 LASSERT(master->rq_repmsg->buflens[2] >=
1781                                 slave->rq_repmsg->buflens[2]);
1782                 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1783                                 slave->rq_repmsg->buflens[2]);
1784                 LASSERT(cookie != NULL);
1785
1786                 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1787                                 master->rq_repmsg->buflens[2]);
1788                 LASSERT(cookie2 != NULL);
1789                 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1790                 body2->valid |= OBD_MD_FLCOOKIE;
1791         }
1792         RETURN(0);
1793 }
1794
1795 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1796                                    int offset, struct ptlrpc_request *req,
1797                                    struct lustre_handle *parent_lockh,
1798                                    int update_mode, struct dentry *dparent,
1799                                    struct lustre_handle *child_lockh,
1800                                    struct dentry *dchild)
1801 {
1802         struct obd_device *obd = req->rq_export->exp_obd;
1803         struct mds_obd *mds = mds_req2mds(req);
1804         struct ptlrpc_request *request = NULL;
1805         int rc = 0, cleanup_phase = 0;
1806         struct mdc_op_data op_data;
1807         void *handle;
1808         ENTRY;
1809
1810         LASSERT(offset == 1 || offset == 3);
1811
1812         /* time to drop i_nlink on remote MDS */
1813         memset(&op_data, 0, sizeof(op_data));
1814         mds_pack_dentry2id(obd, &op_data.id1, dchild, 1);
1815         op_data.create_mode = rec->ur_mode;
1816
1817         DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
1818                   rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data.id1));
1819         
1820         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1821                 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
1822                           rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data.id1));
1823         }
1824
1825         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1826                 op_data.create_mode |= MDS_MODE_REPLAY;
1827         
1828         rc = md_unlink(mds->mds_lmv_exp, &op_data, &request);
1829         cleanup_phase = 2;
1830
1831         if (request) {
1832                 if (rc == 0)
1833                         mds_copy_unlink_reply(req, request);
1834                 ptlrpc_req_finished(request);
1835         }
1836
1837         if (rc == 0) {
1838                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1839                                       NULL);
1840                 if (IS_ERR(handle))
1841                         GOTO(cleanup, rc = PTR_ERR(handle));
1842                 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1843                 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1844                                         rc, 0);
1845         }
1846         EXIT;
1847 cleanup:
1848         req->rq_status = rc;
1849
1850 #ifdef S_PDIROPS
1851         if (parent_lockh[1].cookie != 0)
1852                 ldlm_lock_decref(parent_lockh + 1, update_mode);
1853 #endif
1854         ldlm_lock_decref(child_lockh, LCK_EX);
1855         if (rc)
1856                 ldlm_lock_decref(parent_lockh, LCK_PW);
1857         else
1858                 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1859         l_dput(dchild);
1860         l_dput(dparent);
1861
1862         return 0;
1863 }
1864
1865 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1866                             struct ptlrpc_request *req,
1867                             struct lustre_handle *lh)
1868 {
1869         struct dentry *dparent = NULL, *dchild;
1870         struct mds_obd *mds = mds_req2mds(req);
1871         struct obd_device *obd = req->rq_export->exp_obd;
1872         struct mds_body *body = NULL;
1873         struct inode *child_inode = NULL;
1874         struct lustre_handle parent_lockh[2] = {{0}, {0}}; 
1875         struct lustre_handle child_lockh = {0};
1876         struct lustre_handle child_reuse_lockh = {0};
1877         struct lustre_handle *slave_lockh = NULL;
1878         char idname[LL_ID_NAMELEN];
1879         struct llog_create_locks *lcl = NULL;
1880         void *handle = NULL;
1881         int rc = 0, cleanup_phase = 0;
1882         int unlink_by_id = 0;
1883         int update_mode;
1884         ENTRY;
1885
1886         LASSERT(offset == 1 || offset == 3);
1887
1888         DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1889                   id_ino(rec->ur_id1), id_gen(rec->ur_id1),
1890                   rec->ur_name);
1891
1892         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1893
1894         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1895                 DEBUG_REQ(D_HA, req, "unlink replay\n");
1896                 LASSERT(offset == 1); /* should not come from intent */
1897                 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
1898                        lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
1899                        req->rq_repmsg->buflens[2]);
1900         }
1901
1902         MD_COUNTER_INCREMENT(obd, unlink);
1903
1904         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1905                 GOTO(cleanup, rc = -ENOENT);
1906
1907         if (rec->ur_namelen == 1) {
1908                 /* this is request to drop i_nlink on local inode */
1909                 unlink_by_id = 1;
1910                 rec->ur_name = idname;
1911                 rc = mds_create_local_dentry(rec, obd);
1912                 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
1913                         DEBUG_REQ(D_HA, req,
1914                                   "drop nlink on inode "DLID4" (replay)",
1915                                   OLID4(rec->ur_id1));
1916                         req->rq_status = 0;
1917                         RETURN(0);
1918                 }
1919         }
1920
1921         if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1922                 /* master mds for directory asks slave removing inode is already
1923                  * locked */
1924                 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1925                                                LCK_PW, parent_lockh,
1926                                                &update_mode, rec->ur_name,
1927                                                rec->ur_namelen,
1928                                                MDS_INODELOCK_UPDATE);
1929                 if (IS_ERR(dparent))
1930                         GOTO(cleanup, rc = PTR_ERR(dparent));
1931                 dchild = ll_lookup_one_len(rec->ur_name, dparent, 
1932                                            rec->ur_namelen - 1);
1933                 if (IS_ERR(dchild))
1934                         GOTO(cleanup, rc = PTR_ERR(dchild));
1935                 child_lockh.cookie = 0;
1936                 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
1937                 LASSERT(dchild->d_inode != NULL);
1938                 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
1939         } else {
1940                 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
1941                                                  parent_lockh, &dparent,
1942                                                  LCK_PW, MDS_INODELOCK_UPDATE,
1943                                                  &update_mode, rec->ur_name,
1944                                                  rec->ur_namelen, &child_lockh,
1945                                                  &dchild, LCK_EX,
1946                                                  MDS_INODELOCK_LOOKUP |
1947                                                  MDS_INODELOCK_UPDATE);
1948         }
1949         if (rc)
1950                 GOTO(cleanup, rc);
1951
1952         if (dchild->d_flags & DCACHE_CROSS_REF) {
1953                 /* we should have parent lock only here */
1954                 LASSERT(unlink_by_id == 0);
1955                 LASSERT(dchild->d_mdsnum != mds->mds_num);
1956                 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
1957                                         update_mode, dparent, &child_lockh, dchild);
1958                 RETURN(0);
1959         }
1960
1961         cleanup_phase = 1; /* dchild, dparent, locks */
1962
1963         dget(dchild);
1964         child_inode = dchild->d_inode;
1965         if (child_inode == NULL) {
1966                 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1967                        dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
1968                 GOTO(cleanup, rc = -ENOENT);
1969         }
1970
1971         cleanup_phase = 2; /* dchild has a lock */
1972
1973         /* We have to do these checks ourselves, in case we are making an
1974          * orphan.  The client tells us whether rmdir() or unlink() was called,
1975          * so we need to return appropriate errors (bug 72).
1976          *
1977          * We don't have to check permissions, because vfs_rename (called from
1978          * mds_open_unlink_rename) also calls may_delete. */
1979         if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1980                 if (!S_ISDIR(child_inode->i_mode))
1981                         GOTO(cleanup, rc = -ENOTDIR);
1982         } else {
1983                 if (S_ISDIR(child_inode->i_mode))
1984                         GOTO(cleanup, rc = -EISDIR);
1985         }
1986
1987         /* handle splitted dir */
1988         rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
1989         if (rc)
1990                 GOTO(cleanup, rc);
1991
1992         /* Step 4: Get a lock on the ino to sync with creation WRT inode
1993          * reuse (see bug 2029). */
1994         rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1995         if (rc != ELDLM_OK)
1996                 GOTO(cleanup, rc);
1997         cleanup_phase = 3; /* child inum lock */
1998
1999         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2000
2001         /* ldlm_reply in buf[0] if called via intent */
2002         if (offset == 3)
2003                 offset = 1;
2004         else
2005                 offset = 0;
2006
2007         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2008         LASSERT(body != NULL);
2009
2010         /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2011         DOWN_READ_I_ALLOC_SEM(child_inode);
2012         cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2013
2014         /* If this is potentially the last reference to this inode, get the
2015          * OBD EA data first so the client can destroy OST objects.  We
2016          * only do the object removal later if no open files/links remain. */
2017         if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2018             child_inode->i_nlink == 1) {
2019                 if (mds_orphan_open_count(child_inode) > 0) {
2020                         /* need to lock pending_dir before transaction */
2021                         down(&mds->mds_pending_dir->d_inode->i_sem);
2022                         cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2023                 } else if (S_ISREG(child_inode->i_mode)) {
2024                         mds_pack_inode2body(obd, body, child_inode, 0);
2025                         mds_pack_md(obd, req->rq_repmsg, offset + 1,
2026                                     body, child_inode, MDS_PACK_MD_LOCK);
2027                 }
2028         }
2029
2030         /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2031         switch (child_inode->i_mode & S_IFMT) {
2032         case S_IFDIR:
2033                 /* Drop any lingering child directories before we start our
2034                  * transaction, to avoid doing multiple inode dirty/delete
2035                  * in our compound transaction (bug 1321). */
2036                 shrink_dcache_parent(dchild);
2037                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2038                                       NULL);
2039                 if (IS_ERR(handle))
2040                         GOTO(cleanup, rc = PTR_ERR(handle));
2041                 rc = vfs_rmdir(dparent->d_inode, dchild);
2042                 break;
2043         case S_IFREG: {
2044 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2045                 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2046                                                         offset + 1, 0);
2047                 handle = fsfilt_start_log(obd, dparent->d_inode,
2048                                           FSFILT_OP_UNLINK, NULL,
2049                                           le32_to_cpu(lmm->lmm_stripe_count));
2050                 if (IS_ERR(handle))
2051                         GOTO(cleanup, rc = PTR_ERR(handle));
2052                 rc = vfs_unlink(dparent->d_inode, dchild);
2053                 break;
2054         }
2055         case S_IFLNK:
2056         case S_IFCHR:
2057         case S_IFBLK:
2058         case S_IFIFO:
2059         case S_IFSOCK:
2060                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2061                                       NULL);
2062                 if (IS_ERR(handle))
2063                         GOTO(cleanup, rc = PTR_ERR(handle));
2064                 rc = vfs_unlink(dparent->d_inode, dchild);
2065                 break;
2066         default:
2067                 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2068                        rec->ur_name);
2069                 LBUG();
2070                 GOTO(cleanup, rc = -EINVAL);
2071         }
2072
2073         if (rc == 0 && child_inode->i_nlink == 0) {
2074                 if (mds_orphan_open_count(child_inode) > 0)
2075                         rc = mds_orphan_add_link(rec, obd, dchild);
2076
2077                 if (rc == 1)
2078                         GOTO(cleanup, rc = 0);
2079
2080                 if (!S_ISREG(child_inode->i_mode))
2081                         GOTO(cleanup, rc);
2082
2083                 if (!(body->valid & OBD_MD_FLEASIZE)) {
2084                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2085                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
2086                 } else if (mds_log_op_unlink(obd, child_inode,
2087                                 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2088                                         req->rq_repmsg->buflens[offset + 1],
2089                                 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2090                                         req->rq_repmsg->buflens[offset+2], 
2091                                 &lcl) > 0){
2092                         body->valid |= OBD_MD_FLCOOKIE;
2093                 }
2094         }
2095
2096         GOTO(cleanup, rc);
2097
2098 cleanup:
2099         if (rc == 0) {
2100                 struct iattr iattr;
2101                 int err;
2102
2103                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2104                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2105                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2106
2107                 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2108                 if (err)
2109                         CERROR("error on parent setattr: rc = %d\n", err);
2110         }
2111         rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2112                                 handle, req, rc, 0);
2113         if (!rc)
2114                 (void)obd_set_info(mds->mds_lov_exp, strlen("unlinked"),
2115                                    "unlinked", 0, NULL);
2116         switch(cleanup_phase) {
2117         case 5: /* pending_dir semaphore */
2118                 up(&mds->mds_pending_dir->d_inode->i_sem);
2119         case 4: /* child inode semaphore */
2120                 UP_READ_I_ALLOC_SEM(child_inode);
2121                  /* handle splitted dir */
2122                 if (rc == 0) {
2123                         /* master directory can be non-empty or something else ... */
2124                         mds_unlink_slave_objs(obd, dchild);
2125                 }
2126                 if (lcl != NULL)
2127                         ptlrpc_save_llog_lock(req, lcl);
2128         case 3: /* child ino-reuse lock */
2129                 if (rc && body != NULL) {
2130                         // Don't unlink the OST objects if the MDS unlink failed
2131                         body->valid = 0;
2132                 }
2133                 if (rc)
2134                         ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2135                 else
2136                         ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2137         case 2: /* child lock */
2138                 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2139                 if (child_lockh.cookie)
2140                         ldlm_lock_decref(&child_lockh, LCK_EX);
2141         case 1: /* child and parent dentry, parent lock */
2142 #ifdef S_PDIROPS
2143                 if (parent_lockh[1].cookie != 0)
2144                         ldlm_lock_decref(parent_lockh + 1, update_mode);
2145 #endif
2146                 if (rc)
2147                         ldlm_lock_decref(parent_lockh, LCK_PW);
2148                 else
2149                         ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2150                 l_dput(dchild);
2151                 l_dput(dchild);
2152                 l_dput(dparent);
2153         case 0:
2154                 break;
2155         default:
2156                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2157                 LBUG();
2158         }
2159         req->rq_status = rc;
2160         return 0;
2161 }
2162
2163 /*
2164  * to service requests from remote MDS to increment i_nlink
2165  */
2166 static int mds_reint_link_acquire(struct mds_update_record *rec,
2167                                   int offset, struct ptlrpc_request *req,
2168                                   struct lustre_handle *lh)
2169 {
2170         struct obd_device *obd = req->rq_export->exp_obd;
2171         struct ldlm_res_id src_res_id = { .name = {0} };
2172         struct lustre_handle *handle = NULL, src_lockh = {0};
2173         struct mds_obd *mds = mds_req2mds(req);
2174         int rc = 0, cleanup_phase = 0;
2175         struct dentry *de_src = NULL;
2176         ldlm_policy_data_t policy;
2177         int flags = 0;
2178         ENTRY;
2179
2180         DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2181                   obd->obd_name, OLID4(rec->ur_id1));
2182
2183         /* Step 1: Lookup the source inode and target directory by ID */
2184         de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2185         if (IS_ERR(de_src))
2186                 GOTO(cleanup, rc = PTR_ERR(de_src));
2187         cleanup_phase = 1; /* source dentry */
2188
2189         src_res_id.name[0] = id_fid(rec->ur_id1);
2190         src_res_id.name[1] = id_group(rec->ur_id1);
2191         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2192
2193         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2194                               src_res_id, LDLM_IBITS, &policy,
2195                               LCK_EX, &flags, mds_blocking_ast,
2196                               ldlm_completion_ast, NULL, NULL,
2197                               NULL, 0, NULL, &src_lockh);
2198         if (rc != ELDLM_OK)
2199                 GOTO(cleanup, rc = -ENOLCK);
2200         cleanup_phase = 2; /* lock */
2201
2202         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2203
2204         handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2205         if (IS_ERR(handle)) {
2206                 rc = PTR_ERR(handle);
2207                 GOTO(cleanup, rc);
2208         }
2209         de_src->d_inode->i_nlink++;
2210         mark_inode_dirty(de_src->d_inode);
2211
2212 cleanup:
2213         rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2214                                         handle, req, rc, 0);
2215         EXIT;
2216         switch (cleanup_phase) {
2217                 case 2:
2218                         if (rc)
2219                                 ldlm_lock_decref(&src_lockh, LCK_EX);
2220                         else
2221                                 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2222                 case 1:
2223                         l_dput(de_src);
2224                 case 0:
2225                         break;
2226                 default:
2227                         CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2228                         LBUG();
2229         }
2230         req->rq_status = rc;
2231         return 0;
2232 }
2233
2234 /*
2235  * request to link to foreign inode:
2236  *  - acquire i_nlinks on this inode
2237  *  - add dentry
2238  */
2239 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2240                                     int offset, struct ptlrpc_request *req,
2241                                     struct lustre_handle *lh)
2242 {
2243         struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2244         struct obd_device *obd = req->rq_export->exp_obd;
2245         struct dentry *de_tgt_dir = NULL;
2246         struct mds_obd *mds = mds_req2mds(req);
2247         int rc = 0, cleanup_phase = 0;
2248         struct mdc_op_data op_data;
2249         struct ptlrpc_request *request = NULL;
2250         int update_mode;
2251         ENTRY;
2252
2253         DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2254                   ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2255                   OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2256                   OLID4(rec->ur_id1));
2257
2258         de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2259                                           tgt_dir_lockh, &update_mode,
2260                                           rec->ur_name, rec->ur_namelen - 1,
2261                                           MDS_INODELOCK_UPDATE);
2262         if (IS_ERR(de_tgt_dir))
2263                 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2264         cleanup_phase = 1;
2265
2266         op_data.id1 = *(rec->ur_id1);
2267         op_data.namelen = 0;
2268         op_data.name = NULL;
2269         rc = md_link(mds->mds_lmv_exp, &op_data, &request);
2270         if (rc)
2271                 GOTO(cleanup, rc);
2272
2273         cleanup_phase = 2;
2274         if (request)
2275                 ptlrpc_req_finished(request);
2276
2277         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2278
2279         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2280         if (IS_ERR(handle)) {
2281                 rc = PTR_ERR(handle);
2282                 GOTO(cleanup, rc);
2283         }
2284         
2285         rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2286                                   rec->ur_namelen - 1, id_ino(rec->ur_id1),
2287                                   id_gen(rec->ur_id1), id_group(rec->ur_id1),
2288                                   id_fid(rec->ur_id1));
2289         cleanup_phase = 3;
2290
2291 cleanup:
2292         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2293                                 handle, req, rc, 0);
2294         EXIT;
2295
2296         switch (cleanup_phase) {
2297                 case 3:
2298                         if (rc) {
2299                                 /* FIXME: drop i_nlink on remote inode here */
2300                                 CERROR("MUST drop drop i_nlink here\n");
2301                         }
2302                 case 2:
2303                 case 1:
2304                         if (rc) {
2305                                 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2306 #ifdef S_PDIROPS
2307                                 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2308 #endif
2309                         } else {
2310                                 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2311 #ifdef S_PDIROPS
2312                                 ptlrpc_save_lock(req, tgt_dir_lockh+1, update_mode);
2313 #endif
2314                         }
2315                         l_dput(de_tgt_dir);
2316                         break;
2317                 default:
2318                         CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2319                         LBUG();
2320         }
2321         req->rq_status = rc;
2322         return 0;
2323 }
2324
2325 static int mds_reint_link(struct mds_update_record *rec, int offset,
2326                           struct ptlrpc_request *req,
2327                           struct lustre_handle *lh)
2328 {
2329         struct obd_device *obd = req->rq_export->exp_obd;
2330         struct dentry *de_src = NULL;
2331         struct dentry *de_tgt_dir = NULL;
2332         struct dentry *dchild = NULL;
2333         struct mds_obd *mds = mds_req2mds(req);
2334         struct lustre_handle *handle = NULL;
2335         struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2336         struct ldlm_res_id src_res_id = { .name = {0} };
2337         struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2338         ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2339         ldlm_policy_data_t tgt_dir_policy =
2340                                        {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2341         int rc = 0, cleanup_phase = 0;
2342 #ifdef S_PDIROPS
2343         int update_mode = 0;
2344 #endif
2345         ENTRY;
2346
2347         LASSERT(offset == 1);
2348
2349         DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2350                   id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2351                   id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2352                   rec->ur_name);
2353
2354         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2355         MD_COUNTER_INCREMENT(obd, link);
2356         
2357 //      memset(tgt_dir_lockh, 0, 2*sizeof(tgt_dir_lockh[0]));
2358         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2359                 GOTO(cleanup, rc = -ENOENT);
2360
2361         if (id_group(rec->ur_id1) != mds->mds_num) {
2362                 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2363                 RETURN(rc);
2364         }
2365         
2366         if (rec->ur_namelen == 1) {
2367                 rc = mds_reint_link_acquire(rec, offset, req, lh);
2368                 RETURN(rc);
2369         }
2370
2371         /* Step 1: Lookup the source inode and target directory by ID */
2372         de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2373         if (IS_ERR(de_src))
2374                 GOTO(cleanup, rc = PTR_ERR(de_src));
2375
2376         cleanup_phase = 1; /* source dentry */
2377
2378         de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2379         if (IS_ERR(de_tgt_dir)) {
2380                 rc = PTR_ERR(de_tgt_dir);
2381                 de_tgt_dir = NULL;
2382                 GOTO(cleanup, rc);
2383         }
2384
2385         cleanup_phase = 2; /* target directory dentry */
2386
2387         CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2388                de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2389                rec->ur_name, de_src->d_inode->i_ino);
2390
2391         /* Step 2: Take the two locks */
2392         src_res_id.name[0] = id_fid(rec->ur_id1);
2393         src_res_id.name[1] = id_group(rec->ur_id1);
2394         tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2395         tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2396         
2397 #ifdef S_PDIROPS
2398         if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2399                 int flags = 0;
2400                 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2401                 if (update_mode) {
2402                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2403                                               tgt_dir_res_id, LDLM_IBITS,
2404                                               &src_policy, update_mode, &flags,
2405                                               mds_blocking_ast,
2406                                               ldlm_completion_ast, NULL, NULL,
2407                                               NULL, 0, NULL, tgt_dir_lockh + 1);
2408                         if (rc != ELDLM_OK)
2409                                 GOTO(cleanup, rc = -ENOLCK);
2410                 }
2411
2412                 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
2413                                                         rec->ur_namelen - 1);
2414                 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2415                        (unsigned long)id_fid(rec->ur_id2),
2416                        (unsigned long)id_group(rec->ur_id2),
2417                        tgt_dir_res_id.name[2]);
2418         }
2419 #endif
2420         rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2421                                    &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2422                                    LCK_EX, &tgt_dir_policy);
2423         if (rc)
2424                 GOTO(cleanup, rc);
2425
2426         cleanup_phase = 3; /* locks */
2427
2428         /* Step 3: Lookup the child */
2429         dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, 
2430                                    rec->ur_namelen - 1);
2431         if (IS_ERR(dchild)) {
2432                 rc = PTR_ERR(dchild);
2433                 if (rc != -EPERM && rc != -EACCES)
2434                         CERROR("child lookup error %d\n", rc);
2435                 GOTO(cleanup, rc);
2436         }
2437
2438         cleanup_phase = 4; /* child dentry */
2439
2440         if (dchild->d_inode) {
2441                 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2442                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
2443                 rc = -EEXIST;
2444                 GOTO(cleanup, rc);
2445         }
2446
2447         /* Step 4: Do it. */
2448         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2449
2450         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2451         if (IS_ERR(handle)) {
2452                 rc = PTR_ERR(handle);
2453                 GOTO(cleanup, rc);
2454         }
2455
2456         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2457         if (rc && rc != -EPERM && rc != -EACCES)
2458                 CERROR("vfs_link error %d\n", rc);
2459 cleanup:
2460         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2461                                 handle, req, rc, 0);
2462         EXIT;
2463
2464         switch (cleanup_phase) {
2465         case 4: /* child dentry */
2466                 l_dput(dchild);
2467         case 3: /* locks */
2468                 if (rc) {
2469                         ldlm_lock_decref(&src_lockh, LCK_EX);
2470                         ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2471                 } else {
2472                         ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2473                         ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2474                 }
2475         case 2: /* target dentry */
2476 #ifdef S_PDIROPS
2477                 if (tgt_dir_lockh[1].cookie && update_mode)
2478                         ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2479 #endif
2480                 if (de_tgt_dir)
2481                         l_dput(de_tgt_dir);
2482         case 1: /* source dentry */
2483                 l_dput(de_src);
2484         case 0:
2485                 break;
2486         default:
2487                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2488                 LBUG();
2489         }
2490         req->rq_status = rc;
2491         return 0;
2492 }
2493
2494 /* The idea here is that we need to get four locks in the end:
2495  * one on each parent directory, one on each child.  We need to take
2496  * these locks in some kind of order (to avoid deadlocks), and the order
2497  * I selected is "increasing resource number" order.  We need to look up
2498  * the children, however, before we know what the resource number(s) are.
2499  * Thus the following plan:
2500  *
2501  * 1,2. Look up the parents
2502  * 3,4. Look up the children
2503  * 5. Take locks on the parents and children, in order
2504  * 6. Verify that the children haven't changed since they were looked up
2505  *
2506  * If there was a race and the children changed since they were first looked
2507  * up, it is possible that mds_verify_child() will be able to just grab the
2508  * lock on the new child resource (if it has a higher resource than any other)
2509  * but we need to compare against not only its parent, but also against the
2510  * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2511  *
2512  * We need the fancy igrab() on the child inodes because we aren't holding a
2513  * lock on the parent after the lookup is done, so dentry->d_inode may change
2514  * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2515  */
2516 static int mds_get_parents_children_locked(struct obd_device *obd,
2517                                            struct mds_obd *mds,
2518                                            struct lustre_id *p1_id,
2519                                            struct dentry **de_srcdirp,
2520                                            struct lustre_id *p2_id,
2521                                            struct dentry **de_tgtdirp,
2522                                            int parent_mode,
2523                                            const char *old_name, int old_len,
2524                                            struct dentry **de_oldp,
2525                                            const char *new_name, int new_len,
2526                                            struct dentry **de_newp,
2527                                            struct lustre_handle *dlm_handles,
2528                                            int child_mode)
2529 {
2530         struct ldlm_res_id p1_res_id = { .name = {0} };
2531         struct ldlm_res_id p2_res_id = { .name = {0} };
2532         struct ldlm_res_id c1_res_id = { .name = {0} };
2533         struct ldlm_res_id c2_res_id = { .name = {0} };
2534         ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2535         /* Only dentry should change, but the inode itself would be
2536            intact otherwise */
2537         ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2538         /* If something is going to be replaced, both dentry and inode locks are
2539            needed */
2540         ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
2541                                                         MDS_INODELOCK_UPDATE}};
2542         struct ldlm_res_id *maxres_src, *maxres_tgt;
2543         struct inode *inode;
2544         __u32 child1_gen = 0;
2545         __u32 child2_gen = 0;
2546         unsigned long child1_ino;
2547         unsigned long child2_ino;
2548         int rc = 0, cleanup_phase = 0;
2549         ENTRY;
2550
2551         /* Step 1: Lookup the source directory */
2552         *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2553         if (IS_ERR(*de_srcdirp))
2554                 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2555
2556         cleanup_phase = 1; /* source directory dentry */
2557
2558         p1_res_id.name[0] = id_fid(p1_id);
2559         p1_res_id.name[1] = id_group(p1_id);
2560
2561         /* Step 2: Lookup the target directory */
2562         if (id_equal_stc(p1_id, p2_id)) {
2563                 *de_tgtdirp = dget(*de_srcdirp);
2564         } else {
2565                 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2566                 if (IS_ERR(*de_tgtdirp)) {
2567                         rc = PTR_ERR(*de_tgtdirp);
2568                         *de_tgtdirp = NULL;
2569                         GOTO(cleanup, rc);
2570                 }
2571         }
2572
2573         cleanup_phase = 2; /* target directory dentry */
2574
2575         p2_res_id.name[0] = id_fid(p2_id);
2576         p2_res_id.name[1] = id_group(p2_id);
2577
2578 #ifdef S_PDIROPS
2579         dlm_handles[5].cookie = 0;
2580         dlm_handles[6].cookie = 0;
2581         
2582         if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2583                 /*
2584                  * get a temp lock on just fid, group to flush client cache and
2585                  * to protect dirs from concurrent splitting.
2586                  */
2587                 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2588                                            LCK_PW, &p_policy, &p2_res_id,
2589                                            &dlm_handles[6], LCK_PW, &p_policy);
2590                 if (rc != ELDLM_OK)
2591                         GOTO(cleanup, rc);
2592                 
2593                 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2594                 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2595
2596                 CDEBUG(D_INFO, "take locks on "
2597                        LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2598                        p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2599                        p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2600         }
2601         cleanup_phase = 3;
2602 #endif
2603
2604         /* Step 3: Lookup the source child entry */
2605         *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, 
2606                                      old_len - 1);
2607         if (IS_ERR(*de_oldp)) {
2608                 rc = PTR_ERR(*de_oldp);
2609                 CERROR("old child lookup error (%*s): %d\n",
2610                        old_len - 1, old_name, rc);
2611                 GOTO(cleanup, rc);
2612         }
2613
2614         cleanup_phase = 4; /* original name dentry */
2615
2616         inode = (*de_oldp)->d_inode;
2617         if (inode != NULL) {
2618                 struct lustre_id sid;
2619                 
2620                 inode = igrab(inode);
2621                 if (inode == NULL)
2622                         GOTO(cleanup, rc = -ENOENT);
2623
2624                 down(&inode->i_sem);
2625                 rc = mds_read_inode_sid(obd, inode, &sid);
2626                 up(&inode->i_sem);
2627                 if (rc) {
2628                         CERROR("Can't read inode self id, inode %lu, "
2629                                "rc %d\n", inode->i_ino, rc);
2630                         iput(inode);
2631                         GOTO(cleanup, rc);
2632                 }
2633
2634                 c1_res_id.name[0] = id_fid(&sid);
2635                 c1_res_id.name[1] = id_group(&sid);
2636                 child1_gen = inode->i_generation;
2637                 child1_ino = inode->i_ino;
2638                 iput(inode);
2639         } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2640                 c1_res_id.name[0] = (*de_oldp)->d_fid;
2641                 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2642                 child1_gen = (*de_oldp)->d_generation;
2643                 child1_ino = (*de_oldp)->d_inum;
2644         } else {
2645                 GOTO(cleanup, rc = -ENOENT);
2646         }
2647
2648         /* Step 4: Lookup the target child entry */
2649         *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, 
2650                                      new_len - 1);
2651         if (IS_ERR(*de_newp)) {
2652                 rc = PTR_ERR(*de_newp);
2653                 CERROR("new child lookup error (%*s): %d\n",
2654                        old_len - 1, old_name, rc);
2655                 GOTO(cleanup, rc);
2656         }
2657
2658         cleanup_phase = 5; /* target dentry */
2659
2660         inode = (*de_newp)->d_inode;
2661         if (inode != NULL) {
2662                 struct lustre_id sid;
2663
2664                 inode = igrab(inode);
2665                 if (inode == NULL)
2666                         goto retry_locks;
2667
2668                 down(&inode->i_sem);
2669                 rc = mds_read_inode_sid(obd, inode, &sid);
2670                 up(&inode->i_sem);
2671                 if (rc) {
2672                         CERROR("Can't read inode self id, inode %lu, "
2673                                "rc %d\n", inode->i_ino, rc);
2674                         GOTO(cleanup, rc);
2675                 }
2676
2677                 c2_res_id.name[0] = id_fid(&sid);
2678                 c2_res_id.name[1] = id_group(&sid);
2679                 child2_gen = inode->i_generation;
2680                 child2_ino = inode->i_ino;
2681                 iput(inode);
2682         } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2683                 c2_res_id.name[0] = (*de_newp)->d_fid;
2684                 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2685                 child2_gen = (*de_newp)->d_generation;
2686                 child2_ino = (*de_newp)->d_inum;
2687         }
2688
2689 retry_locks:
2690         /* Step 5: Take locks on the parents and child(ren) */
2691         maxres_src = &p1_res_id;
2692         maxres_tgt = &p2_res_id;
2693         cleanup_phase = 5; /* target dentry */
2694
2695         if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2696                 maxres_src = &c1_res_id;
2697         if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2698                 maxres_tgt = &c2_res_id;
2699
2700         rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2701                                     &p_policy,
2702                                     &p2_res_id, &dlm_handles[1], parent_mode,
2703                                     &p_policy,
2704                                     &c1_res_id, &dlm_handles[2], child_mode,
2705                                     &c1_policy,
2706                                     &c2_res_id, &dlm_handles[3], child_mode,
2707                                     &c2_policy);
2708         if (rc)
2709                 GOTO(cleanup, rc);
2710
2711         cleanup_phase = 6; /* parent and child(ren) locks */
2712
2713         /* Step 6a: Re-lookup source child to verify it hasn't changed */
2714         rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2715                               parent_mode, &c1_res_id, &dlm_handles[2],
2716                               de_oldp, child_mode, &c1_policy, old_name,old_len,
2717                               maxres_tgt, child1_ino, child1_gen);
2718         if (rc) {
2719                 if (c2_res_id.name[0] != 0)
2720                         ldlm_lock_decref(&dlm_handles[3], child_mode);
2721                 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2722                 cleanup_phase = 5;
2723                 if (rc > 0)
2724                         goto retry_locks;
2725                 GOTO(cleanup, rc);
2726         }
2727
2728         if (!DENTRY_VALID(*de_oldp))
2729                 GOTO(cleanup, rc = -ENOENT);
2730
2731         /* Step 6b: Re-lookup target child to verify it hasn't changed */
2732         rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2733                               parent_mode, &c2_res_id, &dlm_handles[3],
2734                               de_newp, child_mode, &c2_policy, new_name,
2735                               new_len, maxres_src, child2_ino, child2_gen);
2736         if (rc) {
2737                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2738                 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2739                 cleanup_phase = 5;
2740                 if (rc > 0)
2741                         goto retry_locks;
2742                 GOTO(cleanup, rc);
2743         }
2744
2745         EXIT;
2746 cleanup:
2747         if (rc) {
2748                 switch (cleanup_phase) {
2749                 case 6: /* child lock(s) */
2750                         if (c2_res_id.name[0] != 0)
2751                                 ldlm_lock_decref(&dlm_handles[3], child_mode);
2752                         if (c1_res_id.name[0] != 0)
2753                                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2754                         if (dlm_handles[1].cookie != 0)
2755                                 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2756                         if (dlm_handles[0].cookie != 0)
2757                                 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2758                 case 5: /* target dentry */
2759                         l_dput(*de_newp);
2760                 case 4: /* source dentry */
2761                         l_dput(*de_oldp);
2762                 case 3:
2763 #ifdef S_PDIROPS
2764                         if (dlm_handles[5].cookie != 0)
2765                                 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2766                         if (dlm_handles[6].cookie != 0)
2767                                 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2768 #endif
2769                 case 2: /* target directory dentry */
2770                         l_dput(*de_tgtdirp);
2771                 case 1: /* source directry dentry */
2772                         l_dput(*de_srcdirp);
2773                 }
2774         }
2775
2776         return rc;
2777 }
2778
2779 /*
2780  * checks if dentry can be removed. This function also handles cross-ref
2781  * dentries.
2782  */
2783 static int mds_check_for_rename(struct obd_device *obd,
2784                                 struct dentry *dentry)
2785 {
2786         struct mds_obd *mds = &obd->u.mds;
2787         struct lustre_handle *rlockh;
2788         struct ptlrpc_request *req;
2789         struct mdc_op_data op_data;
2790         struct lookup_intent it;
2791         int handle_size, rc = 0;
2792         ENTRY;
2793
2794         LASSERT(dentry != NULL);
2795
2796         if (dentry->d_inode) {
2797                 if (S_ISDIR(dentry->d_inode->i_mode) &&
2798                     !mds_is_dir_empty(obd, dentry))
2799                         rc = -ENOTEMPTY;
2800         } else {
2801                 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
2802                 handle_size = sizeof(struct lustre_handle);
2803         
2804                 OBD_ALLOC(rlockh, handle_size);
2805                 if (rlockh == NULL)
2806                         RETURN(-ENOMEM);
2807
2808                 memset(rlockh, 0, handle_size);
2809                 memset(&op_data, 0, sizeof(op_data));
2810                 mds_pack_dentry2id(obd, &op_data.id1, dentry, 1);
2811
2812                 it.it_op = IT_UNLINK;
2813                 rc = md_enqueue(mds->mds_lmv_exp, LDLM_IBITS, &it, LCK_EX,
2814                                 &op_data, rlockh, NULL, 0, ldlm_completion_ast,
2815                                 mds_blocking_ast, NULL);
2816
2817                 if (rc)
2818                         RETURN(rc);
2819
2820                 if (rlockh->cookie != 0)
2821                         ldlm_lock_decref(rlockh, LCK_EX);
2822                 
2823                 if (it.d.lustre.it_data) {
2824                         req = (struct ptlrpc_request *)it.d.lustre.it_data;
2825                         ptlrpc_req_finished(req);
2826                 }
2827
2828                 if (it.d.lustre.it_status)
2829                         rc = it.d.lustre.it_status;
2830                 OBD_FREE(rlockh, handle_size);
2831         }
2832         RETURN(rc);
2833 }
2834
2835 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
2836                                 struct ptlrpc_request *req, struct lustre_id *id,
2837                                 struct dentry *de_dir, struct dentry *de,
2838                                 int del_cross_ref)
2839 {
2840         struct obd_device *obd = req->rq_export->exp_obd;
2841         struct mds_obd *mds = mds_req2mds(req);
2842         void *handle = NULL;
2843         int rc = 0;
2844         ENTRY;
2845
2846         if (de->d_inode) {
2847                 /*
2848                  * name exists and points to local inode try to unlink this name
2849                  * and create new one.
2850                  */
2851                 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
2852                        obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
2853                        (unsigned long)de->d_inode->i_generation);
2854
2855                 /* checking if we can remove local dentry. */
2856                 rc = mds_check_for_rename(obd, de);
2857                 if (rc)
2858                         GOTO(cleanup, rc);
2859
2860                 handle = fsfilt_start(obd, de_dir->d_inode,
2861                                       FSFILT_OP_RENAME, NULL);
2862                 if (IS_ERR(handle))
2863                         GOTO(cleanup, rc = PTR_ERR(handle));
2864                 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
2865                 if (rc)
2866                         GOTO(cleanup, rc);
2867         } else if (de->d_flags & DCACHE_CROSS_REF) {
2868                 struct lustre_id de_id;
2869
2870                 /* name exists and points to remote inode */
2871                 mds_pack_dentry2id(obd, &de_id, de, 1);
2872                 
2873                 CDEBUG(D_OTHER, "%s: %s points to remote inode "DLID4"\n",
2874                        obd->obd_name, rec->ur_tgt, OLID4(&de_id));
2875
2876                 /* checking if we can remove local dentry. */
2877                 rc = mds_check_for_rename(obd, de);
2878                 if (rc)
2879                         GOTO(cleanup, rc);
2880
2881                 /*
2882                  * to be fully POSIX compatible, we should add one more check:
2883                  *
2884                  * if de_new is subdir of dir rec->ur_id1. If so - return
2885                  * -EINVAL.
2886                  *
2887                  * I do not know how to implement it right now, because
2888                  * inodes/dentries for new and old names lie on different MDS,
2889                  * so add this notice here just to make it visible for the rest
2890                  * of developers and do not forget about. And when this check
2891                  * will be added, del_cross_ref should gone, that is local
2892                  * dentry is able to be removed if all checks passed.
2893                  * 
2894                  * Currently -EEXISTS is returned by fsfilt_add_dir_entry() what
2895                  * is not fully correct. --umka
2896                  */
2897
2898                 if (del_cross_ref) {
2899                         handle = fsfilt_start(obd, de_dir->d_inode,
2900                                               FSFILT_OP_RENAME, NULL);
2901                         if (IS_ERR(handle))
2902                                 GOTO(cleanup, rc = PTR_ERR(handle));
2903                         rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
2904                         if (rc)
2905                                 GOTO(cleanup, rc);
2906                 }
2907                 
2908         } else {
2909                 /* name doesn't exist. the simplest case. */
2910                 handle = fsfilt_start(obd, de_dir->d_inode,
2911                                       FSFILT_OP_LINK, NULL);
2912                 if (IS_ERR(handle))
2913                         GOTO(cleanup, rc = PTR_ERR(handle));
2914         }
2915
2916         rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
2917                                   rec->ur_tgtlen - 1, id_ino(id),
2918                                   id_gen(id), id_group(id), id_fid(id));
2919         if (rc) {
2920                 CERROR("add_dir_entry() returned error %d\n", rc);
2921                 GOTO(cleanup, rc);
2922         }
2923
2924 cleanup:
2925         EXIT;
2926         rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
2927                                 handle, req, rc, 0);
2928
2929         RETURN(rc);
2930 }
2931
2932 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
2933                                 struct ptlrpc_request *req, struct dentry *de_dir,
2934                                 struct dentry *de)
2935 {
2936         struct obd_device *obd = req->rq_export->exp_obd;
2937         struct mds_obd *mds = mds_req2mds(req);
2938         void *handle = NULL;
2939         int rc = 0;
2940         ENTRY;
2941
2942         handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
2943         if (IS_ERR(handle))
2944                 GOTO(cleanup, rc = PTR_ERR(handle));
2945         rc = fsfilt_del_dir_entry(obd, de);
2946         d_drop(de);
2947
2948 cleanup:
2949         EXIT;
2950         rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
2951                                 handle, req, rc, 0);
2952         RETURN(0);
2953 }
2954
2955 static int mds_reint_rename_create_name(struct mds_update_record *rec,
2956                                         int offset, struct ptlrpc_request *req)
2957 {
2958         struct lustre_handle parent_lockh[2] = {{0}, {0}};
2959         struct obd_device *obd = req->rq_export->exp_obd;
2960         struct mds_obd *mds = mds_req2mds(req);
2961         struct lustre_handle child_lockh = {0};
2962         struct dentry *de_tgtdir = NULL;
2963         struct dentry *de_new = NULL;
2964         int cleanup_phase = 0;
2965         int update_mode, rc = 0;
2966         ENTRY;
2967
2968         /*
2969          * another MDS executing rename operation has asked us to create target
2970          * name. such a creation should destroy existing target name.
2971          */
2972         CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
2973                obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
2974
2975         /* first, lookup the target */
2976         child_lockh.cookie = 0;
2977         
2978         rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
2979                                          &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
2980                                          &update_mode, rec->ur_tgt, rec->ur_tgtlen,
2981                                          &child_lockh, &de_new, LCK_EX,
2982                                          MDS_INODELOCK_LOOKUP);
2983         if (rc)
2984                 GOTO(cleanup, rc);
2985
2986         cleanup_phase = 1;
2987
2988         LASSERT(de_tgtdir);
2989         LASSERT(de_tgtdir->d_inode);
2990         LASSERT(de_new);
2991
2992         rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
2993                                   de_tgtdir, de_new, 0);
2994
2995         EXIT;
2996 cleanup:
2997         
2998         if (cleanup_phase == 1) {
2999 #ifdef S_PDIROPS
3000                 if (parent_lockh[1].cookie != 0)
3001                         ldlm_lock_decref(&parent_lockh[1], update_mode);
3002 #endif
3003                 ldlm_lock_decref(&parent_lockh[0], LCK_PW);
3004                 if (child_lockh.cookie != 0)
3005                         ldlm_lock_decref(&child_lockh, LCK_EX);
3006                 l_dput(de_new);
3007                 l_dput(de_tgtdir);
3008         }
3009
3010         req->rq_status = rc;
3011         return 0;
3012 }
3013
3014 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3015                                       struct ptlrpc_request *req)
3016 {
3017         struct obd_device *obd = req->rq_export->exp_obd;
3018         struct ptlrpc_request *req2 = NULL;
3019         struct dentry *de_srcdir = NULL;
3020         struct dentry *de_old = NULL;
3021         struct mds_obd *mds = mds_req2mds(req);
3022         struct lustre_handle parent_lockh[2] = {{0}, {0}};
3023         struct lustre_handle child_lockh = {0};
3024         struct mdc_op_data opdata;
3025         int update_mode, rc = 0;
3026         ENTRY;
3027
3028         CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3029                obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3030         memset(&opdata, 0, sizeof(opdata));
3031
3032         child_lockh.cookie = 0;
3033         rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3034                                          &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3035                                          &update_mode, rec->ur_name, 
3036                                          rec->ur_namelen, &child_lockh, &de_old,
3037                                          LCK_EX, MDS_INODELOCK_LOOKUP);
3038         LASSERT(rc == 0);
3039         LASSERT(de_srcdir);
3040         LASSERT(de_srcdir->d_inode);
3041         LASSERT(de_old);
3042        
3043         /*
3044          * we already know the target should be created on another MDS so, we
3045          * have to request that MDS to do it.
3046          */
3047
3048         /* prepare source id */
3049         if (de_old->d_flags & DCACHE_CROSS_REF) {
3050                 LASSERT(de_old->d_inode == NULL);
3051                 CDEBUG(D_OTHER, "request to move remote name\n");
3052                 mds_pack_dentry2id(obd, &opdata.id1, de_old, 1);
3053         } else if (de_old->d_inode == NULL) {
3054                 /* oh, source doesn't exist */
3055                 GOTO(cleanup, rc = -ENOENT);
3056         } else {
3057                 struct lustre_id sid;
3058                 struct inode *inode = de_old->d_inode;
3059                 
3060                 LASSERT(inode != NULL);
3061                 CDEBUG(D_OTHER, "request to move local name\n");
3062                 id_ino(&opdata.id1) = inode->i_ino;
3063                 id_group(&opdata.id1) = mds->mds_num;
3064                 id_gen(&opdata.id1) = inode->i_generation;
3065
3066                 down(&inode->i_sem);
3067                 rc = mds_read_inode_sid(obd, inode, &sid);
3068                 up(&inode->i_sem);
3069                 if (rc) {
3070                         CERROR("Can't read inode self id, "
3071                                "inode %lu, rc = %d\n",
3072                                inode->i_ino, rc);
3073                         GOTO(cleanup, rc);
3074                 }
3075
3076                 id_fid(&opdata.id1) = id_fid(&sid);
3077         }
3078
3079         opdata.id2 = *rec->ur_id2;
3080         rc = md_rename(mds->mds_lmv_exp, &opdata, NULL, 0,
3081                        rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3082        
3083         if (rc)
3084                 GOTO(cleanup, rc);
3085         
3086         rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3087                                   de_old);
3088
3089         EXIT;
3090 cleanup:
3091         if (req2)
3092                 ptlrpc_req_finished(req2);
3093
3094 #ifdef S_PDIROPS
3095         if (parent_lockh[1].cookie != 0)
3096                 ldlm_lock_decref(&parent_lockh[1], update_mode);
3097 #endif
3098         ldlm_lock_decref(&parent_lockh[0], LCK_PW);
3099         if (child_lockh.cookie != 0)
3100                 ldlm_lock_decref(&child_lockh, LCK_EX);
3101
3102         l_dput(de_old);
3103         l_dput(de_srcdir);
3104
3105         req->rq_status = rc;
3106         return 0;
3107 }
3108
3109 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3110                             struct ptlrpc_request *req, struct lustre_handle *lockh)
3111 {
3112         struct obd_device *obd = req->rq_export->exp_obd;
3113         struct dentry *de_srcdir = NULL;
3114         struct dentry *de_tgtdir = NULL;
3115         struct dentry *de_old = NULL;
3116         struct dentry *de_new = NULL;
3117         struct inode *old_inode = NULL, *new_inode = NULL;
3118         struct mds_obd *mds = mds_req2mds(req);
3119         struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3120         struct mds_body *body = NULL;
3121         struct llog_create_locks *lcl = NULL;
3122         struct lov_mds_md *lmm = NULL;
3123         int rc = 0, cleanup_phase = 0;
3124         void *handle = NULL;
3125         ENTRY;
3126
3127         LASSERT(offset == 1);
3128
3129         DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3130                   OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3131                   rec->ur_tgt);
3132
3133         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3134
3135         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3136                 DEBUG_REQ(D_HA, req, "rename replay\n");
3137                 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3138                        lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3139                        req->rq_repmsg->buflens[2]);
3140         }
3141
3142         MD_COUNTER_INCREMENT(obd, rename);
3143
3144         if (rec->ur_namelen == 1) {
3145                 rc = mds_reint_rename_create_name(rec, offset, req);
3146                 RETURN(rc);
3147         }
3148
3149         /* check if new name should be located on remote target. */
3150         if (id_group(rec->ur_id2) != mds->mds_num) {
3151                 rc = mds_reint_rename_to_remote(rec, offset, req);
3152                 RETURN(rc);
3153         }
3154         
3155         rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3156                                              rec->ur_id2, &de_tgtdir, LCK_PW,
3157                                              rec->ur_name, rec->ur_namelen,
3158                                              &de_old, rec->ur_tgt,
3159                                              rec->ur_tgtlen, &de_new,
3160                                              dlm_handles, LCK_EX);
3161         if (rc)
3162                 GOTO(cleanup, rc);
3163
3164         cleanup_phase = 1; /* parent(s), children, locks */
3165         old_inode = de_old->d_inode;
3166         new_inode = de_new->d_inode;
3167
3168         /* sanity check for src inode */
3169         if (de_old->d_flags & DCACHE_CROSS_REF) {
3170                 LASSERT(de_old->d_inode == NULL);
3171
3172                 /*
3173                  * in the case of cross-ref dir, we can perform this check only
3174                  * if child and parent lie on the same mds. This is because
3175                  * otherwise they can have the same inode numbers.
3176                  */
3177                 if (de_old->d_mdsnum == mds->mds_num) {
3178                         if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3179                             de_old->d_inum == de_tgtdir->d_inode->i_ino)
3180                                 GOTO(cleanup, rc = -EINVAL);
3181                 }
3182         } else {
3183                 LASSERT(de_old->d_inode != NULL);
3184                 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3185                     de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3186                         GOTO(cleanup, rc = -EINVAL);
3187         }
3188
3189         /* sanity check for dest inode */
3190         if (de_new->d_flags & DCACHE_CROSS_REF) {
3191                 LASSERT(new_inode == NULL);
3192
3193                 /* the same check about target dentry. */
3194                 if (de_new->d_mdsnum == mds->mds_num) {
3195                         if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3196                             de_new->d_inum == de_tgtdir->d_inode->i_ino)
3197                                 GOTO(cleanup, rc = -EINVAL);
3198                 }
3199                 
3200                 /*
3201                  * regular files usualy do not have ->rename() implemented. But
3202                  * we handle only this case when @de_new is cross-ref entry,
3203                  * because in other cases it will be handled by vfs_rename().
3204                  */
3205                 if (de_old->d_inode && (!de_old->d_inode->i_op || 
3206                     !de_old->d_inode->i_op->rename))
3207                         GOTO(cleanup, rc = -EPERM);
3208         } else {
3209                 if (new_inode &&
3210                     (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3211                      new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3212                         GOTO(cleanup, rc = -EINVAL);
3213
3214         }
3215         
3216 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3217         /*
3218          * check if we are moving old entry into its child. 2.6 does not check
3219          * for this in vfs_rename() anymore.
3220          */
3221         if (is_subdir(de_new, de_old))
3222                 GOTO(cleanup, rc = -EINVAL);
3223 #endif
3224         
3225         /* check if inodes point to each other. */
3226         if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3227             !(de_new->d_flags & DCACHE_CROSS_REF) &&
3228             old_inode == new_inode)
3229                 GOTO(cleanup, rc = 0);
3230
3231         /*
3232          * if we are about to remove the target at first, pass the EA of that
3233          * inode to client to perform and cleanup on OST.
3234          */
3235         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3236         LASSERT(body != NULL);
3237
3238         /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3239         if (new_inode) 
3240                 DOWN_READ_I_ALLOC_SEM(new_inode);
3241         
3242         cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3243
3244         if (new_inode && ((S_ISDIR(new_inode->i_mode) && 
3245             new_inode->i_nlink == 2) ||
3246             new_inode->i_nlink == 1)) {
3247                 if (mds_orphan_open_count(new_inode) > 0) {
3248                         /* need to lock pending_dir before transaction */
3249                         down(&mds->mds_pending_dir->d_inode->i_sem);
3250                         cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3251                 } else if (S_ISREG(new_inode->i_mode)) {
3252                         mds_pack_inode2body(obd, body, new_inode, 0);
3253                         mds_pack_md(obd, req->rq_repmsg, 1, body, 
3254                                     new_inode, MDS_PACK_MD_LOCK);
3255                  }
3256         }
3257
3258         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3259                        de_srcdir->d_inode->i_sb);
3260
3261         if (de_old->d_flags & DCACHE_CROSS_REF) {
3262                 struct lustre_id old_id;
3263
3264                 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3265                 
3266                 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3267                                           de_tgtdir, de_new, 1);
3268                 if (rc)
3269                         GOTO(cleanup, rc);
3270
3271                 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3272                                           de_old);
3273                 GOTO(cleanup, rc);
3274         }
3275
3276         lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3277         handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3278                                   NULL, le32_to_cpu(lmm->lmm_stripe_count));
3279
3280         if (IS_ERR(handle))
3281                 GOTO(cleanup, rc = PTR_ERR(handle));
3282
3283         lock_kernel();
3284         de_old->d_fsdata = req;
3285         de_new->d_fsdata = req;
3286         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3287         unlock_kernel();
3288
3289         if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3290                 if (mds_orphan_open_count(new_inode) > 0)
3291                         rc = mds_orphan_add_link(rec, obd, de_new);
3292
3293                 if (rc == 1)
3294                         GOTO(cleanup, rc = 0);
3295
3296                 if (!S_ISREG(new_inode->i_mode))
3297                         GOTO(cleanup, rc);
3298
3299                 if (!(body->valid & OBD_MD_FLEASIZE)) {
3300                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3301                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
3302                 } else if (mds_log_op_unlink(obd, new_inode,
3303                                              lustre_msg_buf(req->rq_repmsg,1,0),
3304                                              req->rq_repmsg->buflens[1],
3305                                              lustre_msg_buf(req->rq_repmsg,2,0),
3306                                              req->rq_repmsg->buflens[2], 
3307                                              &lcl) > 0) {
3308                         body->valid |= OBD_MD_FLCOOKIE;
3309                 }
3310         }
3311
3312         EXIT;
3313 cleanup:
3314         rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3315                                 handle, req, rc, 0);
3316
3317         switch (cleanup_phase) {
3318         case 3:
3319                 up(&mds->mds_pending_dir->d_inode->i_sem);
3320         case 2:
3321                 if (new_inode)
3322                         UP_READ_I_ALLOC_SEM(new_inode);
3323         case 1:
3324 #ifdef S_PDIROPS
3325                 if (dlm_handles[5].cookie != 0)
3326                         ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3327                 if (dlm_handles[6].cookie != 0)
3328                         ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3329 #endif
3330                 if (lcl != NULL)
3331                         ptlrpc_save_llog_lock(req, lcl);
3332
3333                 if (rc) {
3334                         if (dlm_handles[3].cookie != 0)
3335                                 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3336                         ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3337                         ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3338                         ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3339                 } else {
3340                         if (dlm_handles[3].cookie != 0)
3341                                 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3342                         ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3343                         ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3344                         ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3345                 }
3346                 l_dput(de_new);
3347                 l_dput(de_old);
3348                 l_dput(de_tgtdir);
3349                 l_dput(de_srcdir);
3350         case 0:
3351                 break;
3352         default:
3353                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3354                 LBUG();
3355         }
3356         req->rq_status = rc;
3357         return 0;
3358 }
3359
3360 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3361                            struct ptlrpc_request *, struct lustre_handle *);
3362
3363 static mds_reinter reinters[REINT_MAX + 1] = {
3364         [REINT_SETATTR] mds_reint_setattr,
3365         [REINT_CREATE] mds_reint_create,
3366         [REINT_LINK] mds_reint_link,
3367         [REINT_UNLINK] mds_reint_unlink,
3368         [REINT_RENAME] mds_reint_rename,
3369         [REINT_OPEN] mds_open
3370 };
3371
3372 int mds_reint_rec(struct mds_update_record *rec, int offset,
3373                   struct ptlrpc_request *req, struct lustre_handle *lockh)
3374 {
3375         struct obd_device *obd = req->rq_export->exp_obd;
3376         struct lvfs_run_ctxt saved;
3377         int rc;
3378
3379         /* checked by unpacker */
3380         LASSERT(rec->ur_opcode <= REINT_MAX &&
3381                 reinters[rec->ur_opcode] != NULL);
3382
3383         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3384         rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3385         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3386
3387         return rc;
3388 }