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