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