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