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