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