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