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