Whamcloud - gitweb
Land b_smallfix onto HEAD (20040414_1359)
[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/obd_support.h>
35 #include <linux/obd_class.h>
36 #include <linux/obd.h>
37 #include <linux/lustre_lib.h>
38 #include <linux/lustre_idl.h>
39 #include <linux/lustre_mds.h>
40 #include <linux/lustre_dlm.h>
41 #include <linux/lustre_fsfilt.h>
42
43 #include "mds_internal.h"
44
45 void mds_commit_cb(struct obd_device *obd, __u64 transno, void *data,
46                    int error)
47 {
48         obd_transno_commit_cb(obd, transno, error);
49 }
50
51 struct mds_logcancel_data {
52         struct lov_mds_md      *mlcd_lmm;
53         int                     mlcd_size;
54         int                     mlcd_cookielen;
55         int                     mlcd_eadatalen;
56         struct llog_cookie      mlcd_cookies[0];
57 };
58
59
60 static void mds_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
61                                   void *cb_data, int error)
62 {
63         struct mds_logcancel_data *mlcd = cb_data;
64         struct lov_stripe_md *lsm = NULL;
65         struct llog_ctxt *ctxt;
66         int rc;
67
68         obd_transno_commit_cb(obd, transno, error);
69
70         CDEBUG(D_HA, "cancelling %d cookies\n",
71                (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
72
73         rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, mlcd->mlcd_lmm,
74                           mlcd->mlcd_eadatalen);
75         if (rc < 0) {
76                 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
77                        (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
78                        rc);
79         } else {
80                 ///* XXX 0 normally, SENDNOW for debug */);
81                 ctxt = llog_get_context(obd,mlcd->mlcd_cookies[0].lgc_subsys+1);
82                 rc = llog_cancel(ctxt, lsm, mlcd->mlcd_cookielen /
83                                                 sizeof(*mlcd->mlcd_cookies),
84                                  mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW);
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         }
90
91         OBD_FREE(mlcd, mlcd->mlcd_size);
92 }
93
94 /* Assumes caller has already pushed us into the kernel context. */
95 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
96                        struct ptlrpc_request *req, int rc, __u32 op_data)
97 {
98         struct mds_export_data *med = &req->rq_export->exp_mds_data;
99         struct mds_client_data *mcd = med->med_mcd;
100         struct obd_device *obd = req->rq_export->exp_obd;
101         int err;
102         __u64 transno;
103         loff_t off;
104         int log_pri = D_HA;
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(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                 LASSERT(transno == 0);
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         mcd->mcd_last_transno = cpu_to_le64(transno);
145         mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
146         mcd->mcd_last_result = cpu_to_le32(rc);
147         mcd->mcd_last_data = cpu_to_le32(op_data);
148
149         fsfilt_add_journal_cb(req->rq_export->exp_obd, transno, handle,
150                               mds_commit_cb, NULL);
151         err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd, sizeof(*mcd),
152                                   &off, 0);
153
154         if (err) {
155                 log_pri = D_ERROR;
156                 if (rc == 0)
157                         rc = err;
158         }
159
160         DEBUG_REQ(log_pri, req,
161                   "wrote trans #"LPU64" client %s at idx %u: err = %d",
162                   transno, mcd->mcd_uuid, med->med_idx, err);
163
164         err = mds_lov_write_objids(obd);
165         if (err) {
166                 log_pri = D_ERROR;
167                 if (rc == 0)
168                         rc = err;
169         }
170         CDEBUG(log_pri, "wrote objids: err = %d\n", err);
171
172 commit:
173         err = fsfilt_commit(obd, inode, handle, 0);
174         if (err) {
175                 CERROR("error committing transaction: %d\n", err);
176                 if (!rc)
177                         rc = err;
178         }
179
180         RETURN(rc);
181 }
182
183 /* this gives the same functionality as the code between
184  * sys_chmod and inode_setattr
185  * chown_common and inode_setattr
186  * utimes and inode_setattr
187  */
188 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
189 {
190         time_t now = LTIME_S(CURRENT_TIME);
191         struct iattr *attr = &rec->ur_iattr;
192         unsigned int ia_valid = attr->ia_valid;
193         int error;
194         ENTRY;
195
196         /* only fix up attrs if the client VFS didn't already */
197         if (!(ia_valid & ATTR_RAW))
198                 RETURN(0);
199
200         if (!(ia_valid & ATTR_CTIME_SET))
201                 LTIME_S(attr->ia_ctime) = now;
202         if (!(ia_valid & ATTR_ATIME_SET))
203                 LTIME_S(attr->ia_atime) = now;
204         if (!(ia_valid & ATTR_MTIME_SET))
205                 LTIME_S(attr->ia_mtime) = now;
206
207         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
208                 RETURN(-EPERM);
209
210         /* times */
211         if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
212                 if (rec->ur_fsuid != inode->i_uid &&
213                     (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
214                         RETURN(error);
215         }
216
217         if (ia_valid & ATTR_SIZE) {
218                 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
219                         RETURN(error);
220         }
221
222         if (ia_valid & ATTR_UID) {
223                 /* chown */
224                 error = -EPERM;
225                 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
226                         RETURN(-EPERM);
227                 if (attr->ia_uid == (uid_t) -1)
228                         attr->ia_uid = inode->i_uid;
229                 if (attr->ia_gid == (gid_t) -1)
230                         attr->ia_gid = inode->i_gid;
231                 attr->ia_mode = inode->i_mode;
232                 /*
233                  * If the user or group of a non-directory has been
234                  * changed by a non-root user, remove the setuid bit.
235                  * 19981026 David C Niemi <niemi@tux.org>
236                  *
237                  * Changed this to apply to all users, including root,
238                  * to avoid some races. This is the behavior we had in
239                  * 2.0. The check for non-root was definitely wrong
240                  * for 2.2 anyway, as it should have been using
241                  * CAP_FSETID rather than fsuid -- 19990830 SD.
242                  */
243                 if ((inode->i_mode & S_ISUID) == S_ISUID &&
244                     !S_ISDIR(inode->i_mode)) {
245                         attr->ia_mode &= ~S_ISUID;
246                         attr->ia_valid |= ATTR_MODE;
247                 }
248                 /*
249                  * Likewise, if the user or group of a non-directory
250                  * has been changed by a non-root user, remove the
251                  * setgid bit UNLESS there is no group execute bit
252                  * (this would be a file marked for mandatory
253                  * locking).  19981026 David C Niemi <niemi@tux.org>
254                  *
255                  * Removed the fsuid check (see the comment above) --
256                  * 19990830 SD.
257                  */
258                 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
259                      (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
260                         attr->ia_mode &= ~S_ISGID;
261                         attr->ia_valid |= ATTR_MODE;
262                 }
263         } else if (ia_valid & ATTR_MODE) {
264                 int mode = attr->ia_mode;
265                 /* chmod */
266                 if (attr->ia_mode == (mode_t) -1)
267                         attr->ia_mode = inode->i_mode;
268                 attr->ia_mode =
269                         (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
270         }
271         RETURN(0);
272 }
273
274 void mds_steal_ack_locks(struct ptlrpc_request *req)
275 {
276         struct obd_export         *exp = req->rq_export;
277         struct list_head          *tmp;
278         struct ptlrpc_reply_state *oldrep;
279         struct ptlrpc_service     *svc;
280         unsigned long              flags;
281         int                        i;
282
283         /* CAVEAT EMPTOR: spinlock order */
284         spin_lock_irqsave (&exp->exp_lock, flags);
285         list_for_each (tmp, &exp->exp_outstanding_replies) {
286                 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
287
288                 if (oldrep->rs_xid != req->rq_xid)
289                         continue;
290
291                 if (oldrep->rs_msg.opc != req->rq_reqmsg->opc)
292                         CERROR ("Resent req xid "LPX64" has mismatched opc: "
293                                 "new %d old %d\n", req->rq_xid,
294                                 req->rq_reqmsg->opc, oldrep->rs_msg.opc);
295
296                 svc = oldrep->rs_srv_ni->sni_service;
297                 spin_lock (&svc->srv_lock);
298
299                 list_del_init (&oldrep->rs_exp_list);
300
301                 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
302                       " o%d NID"LPX64"\n",
303                       oldrep->rs_nlocks, oldrep,
304                       oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg.opc,
305                       exp->exp_connection->c_peer.peer_nid);
306
307                 for (i = 0; i < oldrep->rs_nlocks; i++)
308                         ptlrpc_save_lock(req,
309                                          &oldrep->rs_locks[i],
310                                          oldrep->rs_modes[i]);
311                 oldrep->rs_nlocks = 0;
312
313                 DEBUG_REQ(D_HA, req, "stole locks for");
314                 ptlrpc_schedule_difficult_reply (oldrep);
315
316                 spin_unlock (&svc->srv_lock);
317                 spin_unlock_irqrestore (&exp->exp_lock, flags);
318                 return;
319         }
320         spin_unlock_irqrestore (&exp->exp_lock, flags);
321 }
322
323 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
324 {
325         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
326                   mcd->mcd_last_transno, mcd->mcd_last_result);
327         req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
328         req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
329
330         mds_steal_ack_locks(req);
331 }
332
333 static void reconstruct_reint_setattr(struct mds_update_record *rec,
334                                       int offset, struct ptlrpc_request *req)
335 {
336         struct mds_export_data *med = &req->rq_export->exp_mds_data;
337         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
338         struct dentry *de;
339         struct mds_body *body;
340
341         mds_req_from_mcd(req, med->med_mcd);
342
343         de = mds_fid2dentry(obd, rec->ur_fid1, NULL);
344         if (IS_ERR(de)) {
345                 LASSERT(PTR_ERR(de) == req->rq_status);
346                 return;
347         }
348
349         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
350         mds_pack_inode2fid(&body->fid1, de->d_inode);
351         mds_pack_inode2body(body, de->d_inode);
352
353         /* Don't return OST-specific attributes if we didn't just set them */
354         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
355                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
356         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
357                 body->valid |= OBD_MD_FLMTIME;
358         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
359                 body->valid |= OBD_MD_FLATIME;
360
361         l_dput(de);
362 }
363
364 /* In the raw-setattr case, we lock the child inode.
365  * In the write-back case or if being called from open, the client holds a lock
366  * already.
367  *
368  * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
369 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
370                              struct ptlrpc_request *req,
371                              struct lustre_handle *lh)
372 {
373         struct mds_obd *mds = mds_req2mds(req);
374         struct obd_device *obd = req->rq_export->exp_obd;
375         struct mds_body *body;
376         struct dentry *de;
377         struct inode *inode = NULL;
378         struct lustre_handle lockh;
379         void *handle = NULL;
380         struct mds_logcancel_data *mlcd = NULL;
381         int rc = 0, cleanup_phase = 0, err, locked = 0;
382         ENTRY;
383
384         LASSERT(offset == 0);
385
386         DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x", rec->ur_fid1->id,
387                   rec->ur_fid1->generation, rec->ur_iattr.ia_valid);
388
389         MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
390
391         if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
392                 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
393                 if (IS_ERR(de))
394                         GOTO(cleanup, rc = PTR_ERR(de));
395         } else {
396                 __u64 lockpart = MDS_INODELOCK_UPDATE;
397                 if (rec->ur_iattr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID) )
398                         lockpart |= MDS_INODELOCK_LOOKUP;
399                 de = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW,
400                                            &lockh, NULL, 0, lockpart);
401                 if (IS_ERR(de))
402                         GOTO(cleanup, rc = PTR_ERR(de));
403                 locked = 1;
404         }
405
406         cleanup_phase = 1;
407
408         inode = de->d_inode;
409         LASSERT(inode);
410         if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
411             rec->ur_eadata != NULL)
412                 down(&inode->i_sem);
413
414         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
415
416         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
417         if (IS_ERR(handle))
418                 GOTO(cleanup, rc = PTR_ERR(handle));
419
420         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
421                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
422                        LTIME_S(rec->ur_iattr.ia_mtime),
423                        LTIME_S(rec->ur_iattr.ia_ctime));
424         rc = mds_fix_attr(inode, rec);
425         if (rc)
426                 GOTO(cleanup, rc);
427
428         if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG)    /* ioctl */
429                 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
430                                       (long)&rec->ur_iattr.ia_attr_flags);
431         else                                            /* setattr */
432                 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
433
434         if (rc == 0 && (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
435             rec->ur_eadata != NULL) {
436                 struct lov_stripe_md *lsm = NULL;
437
438                 rc = ll_permission(inode, MAY_WRITE, NULL);
439                 if (rc < 0)
440                         GOTO(cleanup, rc);
441
442                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
443                                    mds->mds_osc_exp, 0, &lsm, rec->ur_eadata);
444                 if (rc)
445                         GOTO(cleanup, rc);
446
447                 obd_free_memmd(mds->mds_osc_exp, &lsm);
448
449                 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
450                                    rec->ur_eadatalen);
451                 if (rc)
452                         GOTO(cleanup, rc);
453         }
454
455         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
456         mds_pack_inode2fid(&body->fid1, inode);
457         mds_pack_inode2body(body, inode);
458
459         /* Don't return OST-specific attributes if we didn't just set them */
460         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
461                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
462         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
463                 body->valid |= OBD_MD_FLMTIME;
464         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
465                 body->valid |= OBD_MD_FLATIME;
466
467         if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_osc_obd)) {
468                 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
469                           rec->ur_eadatalen);
470                 if (mlcd) {
471                         mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
472                                 rec->ur_eadatalen;
473                         mlcd->mlcd_eadatalen = rec->ur_eadatalen;
474                         mlcd->mlcd_cookielen = rec->ur_cookielen;
475                         mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
476                                 mlcd->mlcd_cookielen;
477                         memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
478                                mlcd->mlcd_cookielen);
479                         memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
480                                mlcd->mlcd_eadatalen);
481                 } else {
482                         CERROR("unable to allocate log cancel data\n");
483                 }
484         }
485         EXIT;
486  cleanup:
487         if (mlcd != NULL)
488                 fsfilt_add_journal_cb(req->rq_export->exp_obd, 0, handle,
489                                       mds_cancel_cookies_cb, mlcd);
490         err = mds_finish_transno(mds, inode, handle, req, rc, 0);
491         switch (cleanup_phase) {
492         case 1:
493                 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
494                     rec->ur_eadata != NULL)
495                         up(&inode->i_sem);
496                 l_dput(de);
497                 if (locked) {
498                         if (rc) {
499                                 ldlm_lock_decref(&lockh, LCK_PW);
500                         } else {
501                                 ptlrpc_save_lock (req, &lockh, LCK_PW);
502                         }
503                 }
504         case 0:
505                 break;
506         default:
507                 LBUG();
508         }
509         if (err && !rc)
510                 rc = err;
511
512         req->rq_status = rc;
513         return 0;
514 }
515
516 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
517                                      struct ptlrpc_request *req)
518 {
519         struct mds_export_data *med = &req->rq_export->exp_mds_data;
520         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
521         struct dentry *parent, *child;
522         struct mds_body *body;
523
524         mds_req_from_mcd(req, med->med_mcd);
525
526         if (req->rq_status)
527                 return;
528
529         parent = mds_fid2dentry(obd, rec->ur_fid1, NULL);
530         LASSERT(!IS_ERR(parent));
531         child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
532         LASSERT(!IS_ERR(child));
533         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
534         mds_pack_inode2fid(&body->fid1, child->d_inode);
535         mds_pack_inode2body(body, child->d_inode);
536         l_dput(parent);
537         l_dput(child);
538 }
539
540 static int mds_reint_create(struct mds_update_record *rec, int offset,
541                             struct ptlrpc_request *req,
542                             struct lustre_handle *lh)
543 {
544         struct dentry *dparent = NULL;
545         struct mds_obd *mds = mds_req2mds(req);
546         struct obd_device *obd = req->rq_export->exp_obd;
547         struct dentry *dchild = NULL;
548         struct inode *dir = NULL;
549         void *handle = NULL;
550         struct lustre_handle lockh;
551         int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
552         int created = 0;
553         struct dentry_params dp;
554         ENTRY;
555
556         LASSERT(offset == 0);
557         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
558
559         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
560                   rec->ur_fid1->id, rec->ur_fid1->generation,
561                   rec->ur_name, rec->ur_mode);
562
563         MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
564
565         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
566                 GOTO(cleanup, rc = -ESTALE);
567
568         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW, &lockh,
569                                         rec->ur_name, rec->ur_namelen - 1,
570                                         MDS_INODELOCK_UPDATE);
571         if (IS_ERR(dparent)) {
572                 rc = PTR_ERR(dparent);
573                 CERROR("parent lookup error %d\n", rc);
574                 GOTO(cleanup, rc);
575         }
576         cleanup_phase = 1; /* locked parent dentry */
577         dir = dparent->d_inode;
578         LASSERT(dir);
579
580         ldlm_lock_dump_handle(D_OTHER, &lockh);
581
582         dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
583         if (IS_ERR(dchild)) {
584                 rc = PTR_ERR(dchild);
585                 CERROR("child lookup error %d\n", rc);
586                 GOTO(cleanup, rc);
587         }
588
589         cleanup_phase = 2; /* child dentry */
590
591         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
592
593         if (dir->i_mode & S_ISGID) {
594                 if (S_ISDIR(rec->ur_mode))
595                         rec->ur_mode |= S_ISGID;
596         }
597
598         dchild->d_fsdata = (void *)&dp;
599         dp.p_inum = (unsigned long)rec->ur_fid2->id;
600         dp.p_ptr = req;
601
602         switch (type) {
603         case S_IFREG:{
604                 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
605                 if (IS_ERR(handle))
606                         GOTO(cleanup, rc = PTR_ERR(handle));
607                 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
608                 EXIT;
609                 break;
610         }
611         case S_IFDIR:{
612                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
613                 if (IS_ERR(handle))
614                         GOTO(cleanup, rc = PTR_ERR(handle));
615                 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
616                 EXIT;
617                 break;
618         }
619         case S_IFLNK:{
620                 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
621                 if (IS_ERR(handle))
622                         GOTO(cleanup, rc = PTR_ERR(handle));
623                 if (rec->ur_tgt == NULL)        /* no target supplied */
624                         rc = -EINVAL;           /* -EPROTO? */
625                 else
626                         rc = vfs_symlink(dir, dchild, rec->ur_tgt);
627                 EXIT;
628                 break;
629         }
630         case S_IFCHR:
631         case S_IFBLK:
632         case S_IFIFO:
633         case S_IFSOCK:{
634                 int rdev = rec->ur_rdev;
635                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
636                 if (IS_ERR(handle))
637                         GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
638                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
639                 EXIT;
640                 break;
641         }
642         default:
643                 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
644                 dchild->d_fsdata = NULL;
645                 GOTO(cleanup, rc = -EINVAL);
646         }
647
648         /* In case we stored the desired inum in here, we want to clean up. */
649         if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
650                 dchild->d_fsdata = NULL;
651
652         if (rc) {
653                 CDEBUG(D_INODE, "error during create: %d\n", rc);
654                 GOTO(cleanup, rc);
655         } else {
656                 struct iattr iattr;
657                 struct inode *inode = dchild->d_inode;
658                 struct mds_body *body;
659
660                 created = 1;
661                 LTIME_S(iattr.ia_atime) = rec->ur_time;
662                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
663                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
664                 iattr.ia_uid = rec->ur_fsuid;
665                 if (dir->i_mode & S_ISGID)
666                         iattr.ia_gid = dir->i_gid;
667                 else
668                         iattr.ia_gid = rec->ur_fsgid;
669                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
670                         ATTR_MTIME | ATTR_CTIME;
671
672                 if (rec->ur_fid2->id) {
673                         LASSERT(rec->ur_fid2->id == inode->i_ino);
674                         inode->i_generation = rec->ur_fid2->generation;
675                         /* Dirtied and committed by the upcoming setattr. */
676                         CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
677                                inode->i_ino, inode->i_generation);
678                 } else {
679                         struct lustre_handle child_ino_lockh;
680
681                         CDEBUG(D_INODE, "created ino %lu with gen %x\n",
682                                inode->i_ino, inode->i_generation);
683
684                         /* The inode we were allocated may have just been freed
685                          * by an unlink operation.  We take this lock to
686                          * synchronize against the matching reply-ack-lock taken
687                          * in unlink, to avoid replay problems if this reply
688                          * makes it out to the client but the unlink's does not.
689                          * See bug 2029 for more detail.*/
690                         rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
691                         if (rc != ELDLM_OK) {
692                                 CERROR("error locking for unlink/create sync: "
693                                        "%d\n", rc);
694                         } else {
695                                 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
696                         }
697                 }
698
699                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
700                 if (rc)
701                         CERROR("error on child setattr: rc = %d\n", rc);
702
703                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
704                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
705                 if (rc)
706                         CERROR("error on parent setattr: rc = %d\n", rc);
707
708                 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
709                 mds_pack_inode2fid(&body->fid1, inode);
710                 mds_pack_inode2body(body, inode);
711         }
712         EXIT;
713
714 cleanup:
715         err = mds_finish_transno(mds, dir, handle, req, rc, 0);
716
717         if (rc && created) {
718                 /* Destroy the file we just created.  This should not need
719                  * extra journal credits, as we have already modified all of
720                  * the blocks needed in order to create the file in the first
721                  * place.
722                  */
723                 switch (type) {
724                 case S_IFDIR:
725                         err = vfs_rmdir(dir, dchild);
726                         if (err)
727                                 CERROR("rmdir in error path: %d\n", err);
728                         break;
729                 default:
730                         err = vfs_unlink(dir, dchild);
731                         if (err)
732                                 CERROR("unlink in error path: %d\n", err);
733                         break;
734                 }
735         } else {
736                 rc = err;
737         }
738         switch (cleanup_phase) {
739         case 2: /* child dentry */
740                 l_dput(dchild);
741         case 1: /* locked parent dentry */
742                 if (rc) {
743                         ldlm_lock_decref(&lockh, LCK_PW);
744                 } else {
745                         ptlrpc_save_lock (req, &lockh, LCK_PW);
746                 }
747                 l_dput(dparent);
748         case 0:
749                 break;
750         default:
751                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
752                 LBUG();
753         }
754         req->rq_status = rc;
755         return 0;
756 }
757
758 static int res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
759            ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
760 {
761         int i;
762
763         for (i = 0; i < RES_NAME_SIZE; i++) {
764                 /* return 1 here, because enqueue_ordered will skip resources
765                  * of all zeroes if they're sorted to the end of the list. */
766                 if (res1->name[i] == 0 && res2->name[i] != 0)
767                         return 1;
768                 if (res2->name[i] == 0 && res1->name[i] != 0)
769                         return 0;
770
771                 if (res1->name[i] > res2->name[i])
772                         return 1;
773                 if (res1->name[i] < res2->name[i])
774                         return 0;
775         }
776
777         if (!p1 || !p2)
778                 return 0;
779
780         if (memcmp(p1, p2, sizeof(*p1)) < 0)
781                 return 1;
782
783         return 0;
784 }
785
786 /* This function doesn't use ldlm_match_or_enqueue because we're always called
787  * with EX or PW locks, and the MDS is no longer allowed to match write locks,
788  * because they take the place of local semaphores.
789  *
790  * One or two locks are taken in numerical order.  A res_id->name[0] of 0 means
791  * no lock is taken for that res_id.  Must be at least one non-zero res_id. */
792 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
793                           struct lustre_handle *p1_lockh, int p1_lock_mode,
794                           ldlm_policy_data_t *p1_policy,
795                           struct ldlm_res_id *p2_res_id,
796                           struct lustre_handle *p2_lockh, int p2_lock_mode,
797                           ldlm_policy_data_t *p2_policy)
798 {
799         struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
800         struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
801         int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
802         ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
803         int rc, flags;
804         ENTRY;
805
806         LASSERT(p1_res_id != NULL && p2_res_id != NULL);
807
808         CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n", res_id[0]->name[0],
809                res_id[1]->name[0]);
810
811         if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
812                 handles[1] = p1_lockh;
813                 handles[0] = p2_lockh;
814                 res_id[1] = p1_res_id;
815                 res_id[0] = p2_res_id;
816                 lock_modes[1] = p1_lock_mode;
817                 lock_modes[0] = p2_lock_mode;
818                 policies[1] = p1_policy;
819                 policies[0] = p2_policy;
820         }
821
822         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
823                res_id[0]->name[0], res_id[1]->name[0]);
824
825         flags = LDLM_FL_LOCAL_ONLY;
826         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
827                               LDLM_IBITS, policies[0], lock_modes[0], &flags,
828                               mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
829                               NULL, 0, NULL, handles[0]);
830         if (rc != ELDLM_OK)
831                 RETURN(-EIO);
832         ldlm_lock_dump_handle(D_OTHER, handles[0]);
833
834         if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
835             (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
836                 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
837                 ldlm_lock_addref(handles[1], lock_modes[1]);
838         } else if (res_id[1]->name[0] != 0) {
839                 flags = LDLM_FL_LOCAL_ONLY;
840                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
841                                       *res_id[1], LDLM_IBITS, policies[1],
842                                       lock_modes[1], &flags, mds_blocking_ast,
843                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
844                                       NULL, handles[1]);
845                 if (rc != ELDLM_OK) {
846                         ldlm_lock_decref(handles[0], lock_modes[0]);
847                         RETURN(-EIO);
848                 }
849                 ldlm_lock_dump_handle(D_OTHER, handles[1]);
850         }
851
852         RETURN(0);
853 }
854
855 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
856                            struct lustre_handle *p1_lockh, int p1_lock_mode,
857                            ldlm_policy_data_t *p1_policy,
858                            struct ldlm_res_id *p2_res_id,
859                            struct lustre_handle *p2_lockh, int p2_lock_mode,
860                            ldlm_policy_data_t *p2_policy,
861                            struct ldlm_res_id *c1_res_id,
862                            struct lustre_handle *c1_lockh, int c1_lock_mode,
863                            ldlm_policy_data_t *c1_policy,
864                            struct ldlm_res_id *c2_res_id,
865                            struct lustre_handle *c2_lockh, int c2_lock_mode,
866                            ldlm_policy_data_t *c2_policy)
867 {
868         struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
869                                           c1_res_id, c2_res_id };
870         struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
871                                                  c1_lockh, c2_lockh };
872         int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
873                               c1_lock_mode, c2_lock_mode };
874         ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
875                                             c1_policy, c2_policy};
876         int rc, i, j, sorted, flags;
877         ENTRY;
878
879         CDEBUG(D_DLMTRACE,
880                "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
881                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
882                res_id[3]->name[0]);
883
884         /* simple insertion sort - we have at most 4 elements */
885         for (i = 1; i < 4; i++) {
886                 j = i - 1;
887                 dlm_handles[4] = dlm_handles[i];
888                 res_id[4] = res_id[i];
889                 lock_modes[4] = lock_modes[i];
890                 policies[4] = policies[i];
891
892                 sorted = 0;
893                 do {
894                         if (res_gt(res_id[j], res_id[4], policies[j],
895                                    policies[4])) {
896                                 dlm_handles[j + 1] = dlm_handles[j];
897                                 res_id[j + 1] = res_id[j];
898                                 lock_modes[j + 1] = lock_modes[j];
899                                 policies[j + 1] = policies[j];
900                                 j--;
901                         } else {
902                                 sorted = 1;
903                         }
904                 } while (j >= 0 && !sorted);
905
906                 dlm_handles[j + 1] = dlm_handles[4];
907                 res_id[j + 1] = res_id[4];
908                 lock_modes[j + 1] = lock_modes[4];
909                 policies[j + 1] = policies[4];
910         }
911
912         CDEBUG(D_DLMTRACE,
913                "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
914                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
915                res_id[3]->name[0]);
916
917         /* XXX we could send ASTs on all these locks first before blocking? */
918         for (i = 0; i < 4; i++) {
919                 flags = 0;
920                 if (res_id[i]->name[0] == 0)
921                         break;
922                 if (i != 0 &&
923                     !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
924                     (policies[i]->l_inodebits.bits &
925                      policies[i-1]->l_inodebits.bits) ) {
926                         memcpy(dlm_handles[i], dlm_handles[i-1],
927                                sizeof(*(dlm_handles[i])));
928                         ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
929                 } else {
930                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
931                                               *res_id[i], LDLM_IBITS,
932                                               policies[i],
933                                               lock_modes[i], &flags,
934                                               mds_blocking_ast,
935                                               ldlm_completion_ast, NULL, NULL,
936                                               NULL, 0, NULL, dlm_handles[i]);
937                         if (rc != ELDLM_OK)
938                                 GOTO(out_err, rc = -EIO);
939                         ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
940                 }
941         }
942
943         RETURN(0);
944 out_err:
945         while (i-- > 0)
946                 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
947
948         return rc;
949 }
950
951 /* In the unlikely case that the child changed while we were waiting
952  * on the lock, we need to drop the lock on the old child and either:
953  * - if the child has a lower resource name, then we have to also
954  *   drop the parent lock and regain the locks in the right order
955  * - in the rename case, if the child has a lower resource name than one of
956  *   the other parent/child resources (maxres) we also need to reget the locks
957  * - if the child has a higher resource name (this is the common case)
958  *   we can just get the lock on the new child (still in lock order)
959  *
960  * Returns 0 if the child did not change or if it changed but could be locked.
961  * Returns 1 if the child changed and we need to re-lock (no locks held).
962  * Returns -ve error with a valid dchild (no locks held). */
963 static int mds_verify_child(struct obd_device *obd,
964                             struct ldlm_res_id *parent_res_id,
965                             struct lustre_handle *parent_lockh,
966                             struct dentry *dparent, int parent_mode,
967                             struct ldlm_res_id *child_res_id,
968                             struct lustre_handle *child_lockh,
969                             struct dentry **dchildp, int child_mode,
970                             ldlm_policy_data_t *child_policy,
971                             const char *name, int namelen,
972                             struct ldlm_res_id *maxres)
973 {
974         struct dentry *vchild, *dchild = *dchildp;
975         int rc = 0, cleanup_phase = 2; /* parent, child locks */
976         ENTRY;
977
978         vchild = ll_lookup_one_len(name, dparent, namelen - 1);
979         if (IS_ERR(vchild))
980                 GOTO(cleanup, rc = PTR_ERR(vchild));
981
982         if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
983                    (vchild->d_inode != NULL &&
984                     child_res_id->name[0] == vchild->d_inode->i_ino &&
985                     child_res_id->name[1] == vchild->d_inode->i_generation))) {
986                 if (dchild != NULL)
987                         l_dput(dchild);
988                 *dchildp = vchild;
989
990                 RETURN(0);
991         }
992
993         CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
994                vchild->d_inode, dchild ? dchild->d_inode : 0,
995                vchild->d_inode ? vchild->d_inode->i_ino : 0,
996                child_res_id->name[0]);
997         if (child_res_id->name[0] != 0)
998                 ldlm_lock_decref(child_lockh, child_mode);
999         if (dchild)
1000                 l_dput(dchild);
1001
1002         cleanup_phase = 1; /* parent lock only */
1003         *dchildp = dchild = vchild;
1004
1005         if (dchild->d_inode) {
1006                 int flags = 0;
1007                 child_res_id->name[0] = dchild->d_inode->i_ino;
1008                 child_res_id->name[1] = dchild->d_inode->i_generation;
1009
1010                 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1011                     res_gt(maxres, child_res_id, NULL, NULL)) {
1012                         CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1013                                child_res_id->name[0], parent_res_id->name[0],
1014                                maxres->name[0]);
1015                         GOTO(cleanup, rc = 1);
1016                 }
1017
1018                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1019                                       *child_res_id, LDLM_IBITS, child_policy,
1020                                       child_mode, &flags, mds_blocking_ast,
1021                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
1022                                       NULL, child_lockh);
1023                 if (rc != ELDLM_OK)
1024                         GOTO(cleanup, rc = -EIO);
1025
1026         } else {
1027                 memset(child_res_id, 0, sizeof(*child_res_id));
1028         }
1029
1030         EXIT;
1031 cleanup:
1032         if (rc) {
1033                 switch(cleanup_phase) {
1034                 case 2:
1035                         if (child_res_id->name[0] != 0)
1036                                 ldlm_lock_decref(child_lockh, child_mode);
1037                 case 1:
1038                         ldlm_lock_decref(parent_lockh, parent_mode);
1039                 }
1040         }
1041         return rc;
1042 }
1043
1044 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1045                                 struct ll_fid *fid,
1046                                 struct lustre_handle *parent_lockh,
1047                                 struct dentry **dparentp, int parent_mode,
1048                                 __u64 parent_lockpart,
1049                                 char *name, int namelen,
1050                                 struct lustre_handle *child_lockh,
1051                                 struct dentry **dchildp, int child_mode,
1052                                 __u64 child_lockpart)
1053 {
1054         struct ldlm_res_id child_res_id = { .name = {0} };
1055         struct ldlm_res_id parent_res_id = { .name = {0} };
1056         ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1057         ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1058         struct inode *inode;
1059         int rc = 0, cleanup_phase = 0;
1060         ENTRY;
1061
1062         /* Step 1: Lookup parent */
1063         *dparentp = mds_fid2dentry(mds, fid, NULL);
1064         if (IS_ERR(*dparentp)) {
1065                 rc = PTR_ERR(*dparentp);
1066                 *dparentp = NULL;
1067                 RETURN(rc);
1068         }
1069
1070         CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1071                (*dparentp)->d_inode->i_ino, name);
1072
1073         parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
1074         parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
1075
1076         cleanup_phase = 1; /* parent dentry */
1077
1078         /* Step 2: Lookup child (without DLM lock, to get resource name) */
1079         *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1080         if (IS_ERR(*dchildp)) {
1081                 rc = PTR_ERR(*dchildp);
1082                 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1083                 GOTO(cleanup, rc);
1084         }
1085
1086         inode = (*dchildp)->d_inode;
1087         if (inode != NULL)
1088                 inode = igrab(inode);
1089         if (inode == NULL)
1090                 goto retry_locks;
1091
1092         child_res_id.name[0] = inode->i_ino;
1093         child_res_id.name[1] = inode->i_generation;
1094
1095         iput(inode);
1096
1097 retry_locks:
1098         cleanup_phase = 2; /* child dentry */
1099
1100         /* Step 3: Lock parent and child in resource order.  If child doesn't
1101          *         exist, we still have to lock the parent and re-lookup. */
1102         rc = enqueue_ordered_locks(obd,&parent_res_id,parent_lockh,parent_mode,
1103                                    &parent_policy,
1104                                    &child_res_id, child_lockh, child_mode,
1105                                    &child_policy);
1106         if (rc)
1107                 GOTO(cleanup, rc);
1108
1109         if (!(*dchildp)->d_inode)
1110                 cleanup_phase = 3; /* parent lock */
1111         else
1112                 cleanup_phase = 4; /* child lock */
1113
1114         /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1115         rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1116                               parent_mode, &child_res_id, child_lockh, 
1117                               dchildp, child_mode, &child_policy,
1118                               name, namelen, &parent_res_id);
1119         if (rc > 0)
1120                 goto retry_locks;
1121         if (rc < 0) {
1122                 cleanup_phase = 3;
1123                 GOTO(cleanup, rc);
1124         }
1125
1126 cleanup:
1127         if (rc) {
1128                 switch (cleanup_phase) {
1129                 case 4:
1130                         ldlm_lock_decref(child_lockh, child_mode);
1131                 case 3:
1132                         ldlm_lock_decref(parent_lockh, parent_mode);
1133                 case 2:
1134                         l_dput(*dchildp);
1135                 case 1:
1136                         l_dput(*dparentp);
1137                 default: ;
1138                 }
1139         }
1140         return rc;
1141 }
1142
1143 void mds_reconstruct_generic(struct ptlrpc_request *req)
1144 {
1145         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1146
1147         mds_req_from_mcd(req, med->med_mcd);
1148 }
1149
1150 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1151                             struct ptlrpc_request *req,
1152                             struct lustre_handle *lh)
1153 {
1154         struct dentry *dparent, *dchild;
1155         struct mds_obd *mds = mds_req2mds(req);
1156         struct obd_device *obd = req->rq_export->exp_obd;
1157         struct mds_body *body = NULL;
1158         struct inode *child_inode;
1159         struct lustre_handle parent_lockh, child_lockh, child_reuse_lockh;
1160         void *handle = NULL;
1161         int rc = 0, log_unlink = 0, cleanup_phase = 0;
1162         ENTRY;
1163
1164         LASSERT(offset == 0 || offset == 2);
1165
1166         DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1167                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1168
1169         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1170
1171         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1172                 GOTO(cleanup, rc = -ENOENT);
1173
1174         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1175                                          &parent_lockh, &dparent, LCK_PW,
1176                                          MDS_INODELOCK_UPDATE,
1177                                          rec->ur_name, rec->ur_namelen,
1178                                          &child_lockh, &dchild, LCK_EX,
1179                                          MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE);
1180         if (rc)
1181                 GOTO(cleanup, rc);
1182
1183         cleanup_phase = 1; /* dchild, dparent, locks */
1184
1185         child_inode = dchild->d_inode;
1186         if (child_inode == NULL) {
1187                 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1188                        dparent->d_inode->i_ino, rec->ur_name);
1189                 GOTO(cleanup, rc = -ENOENT);
1190         }
1191
1192         cleanup_phase = 2; /* dchild has a lock */
1193
1194         /* Step 4: Get a lock on the ino to sync with creation WRT inode
1195          * reuse (see bug 2029). */
1196         rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1197         if (rc != ELDLM_OK)
1198                 GOTO(cleanup, rc);
1199
1200         cleanup_phase = 3; /* child inum lock */
1201
1202         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1203
1204         /* ldlm_reply in buf[0] if called via intent */
1205         if (offset)
1206                 offset = 1;
1207
1208         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
1209         LASSERT(body != NULL);
1210
1211         /* If this is the last reference to this inode, get the OBD EA
1212          * data first so the client can destroy OST objects.
1213          * we only do the object removal if no open files remain.
1214          * Nobody can get at this name anymore because of the locks so
1215          * we make decisions here as to whether to remove the inode */
1216         if (S_ISREG(child_inode->i_mode) && child_inode->i_nlink == 1 &&
1217             mds_open_orphan_count(child_inode) == 0) {
1218                 mds_pack_inode2fid(&body->fid1, child_inode);
1219                 mds_pack_inode2body(body, child_inode);
1220                 mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1221                             child_inode, 1);
1222                 if (!(body->valid & OBD_MD_FLEASIZE)) {
1223                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1224                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
1225                 } else {
1226                         log_unlink = 1;
1227                 }
1228         }
1229
1230         /* We have to do these checks ourselves, in case we are making an
1231          * orphan.  The client tells us whether rmdir() or unlink() was called,
1232          * so we need to return appropriate errors (bug 72).
1233          *
1234          * We don't have to check permissions, because vfs_rename (called from
1235          * mds_open_unlink_rename) also calls may_delete. */
1236         if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1237                 if (!S_ISDIR(child_inode->i_mode))
1238                         GOTO(cleanup, rc = -ENOTDIR);
1239         } else {
1240                 if (S_ISDIR(child_inode->i_mode))
1241                         GOTO(cleanup, rc = -EISDIR);
1242         }
1243
1244         if (child_inode->i_nlink == (S_ISDIR(child_inode->i_mode) ? 2 : 1) &&
1245             mds_open_orphan_count(child_inode) > 0) {
1246                 rc = mds_open_unlink_rename(rec, obd, dparent, dchild, &handle);
1247                 cleanup_phase = 4; /* transaction */
1248                 GOTO(cleanup, rc);
1249         }
1250
1251         /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1252         switch (child_inode->i_mode & S_IFMT) {
1253         case S_IFDIR:
1254                 /* Drop any lingering child directories before we start our
1255                  * transaction, to avoid doing multiple inode dirty/delete
1256                  * in our compound transaction (bug 1321). */
1257                 shrink_dcache_parent(dchild);
1258                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1259                                       NULL);
1260                 if (IS_ERR(handle))
1261                         GOTO(cleanup, rc = PTR_ERR(handle));
1262                 cleanup_phase = 4; /* transaction */
1263                 rc = vfs_rmdir(dparent->d_inode, dchild);
1264                 break;
1265         case S_IFREG: {
1266                 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
1267                                                         offset + 1, 0);
1268                 handle = fsfilt_start_log(obd, dparent->d_inode,
1269                                           FSFILT_OP_UNLINK, NULL,
1270                                           le32_to_cpu(lmm->lmm_stripe_count));
1271                 if (IS_ERR(handle))
1272                         GOTO(cleanup, rc = PTR_ERR(handle));
1273
1274                 cleanup_phase = 4; /* transaction */
1275                 rc = vfs_unlink(dparent->d_inode, dchild);
1276
1277                 if (!rc && log_unlink)
1278                         if (mds_log_op_unlink(obd, child_inode,
1279                                 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
1280                                 req->rq_repmsg->buflens[offset + 1],
1281                                 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
1282                                 req->rq_repmsg->buflens[offset + 2]) > 0)
1283                                 body->valid |= OBD_MD_FLCOOKIE;
1284                 break;
1285         }
1286         case S_IFLNK:
1287         case S_IFCHR:
1288         case S_IFBLK:
1289         case S_IFIFO:
1290         case S_IFSOCK:
1291                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1292                                       NULL);
1293                 if (IS_ERR(handle))
1294                         GOTO(cleanup, rc = PTR_ERR(handle));
1295                 cleanup_phase = 4; /* transaction */
1296                 rc = vfs_unlink(dparent->d_inode, dchild);
1297                 break;
1298         default:
1299                 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1300                        rec->ur_name);
1301                 LBUG();
1302                 GOTO(cleanup, rc = -EINVAL);
1303         }
1304
1305  cleanup:
1306         if (rc == 0) {
1307                 struct iattr iattr;
1308                 int err;
1309
1310                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1311                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1312                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1313
1314                 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1315                 if (err)
1316                         CERROR("error on parent setattr: rc = %d\n", err);
1317         }
1318
1319         switch(cleanup_phase) {
1320         case 4:
1321                 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1322                                         rc, 0);
1323                 if (!rc)
1324                         (void)obd_set_info(mds->mds_osc_exp, strlen("unlinked"),
1325                                            "unlinked", 0, NULL);
1326         case 3: /* child ino-reuse lock */
1327                 if (rc && body != NULL) {
1328                         // Don't unlink the OST objects if the MDS unlink failed
1329                         body->valid = 0;
1330                 }
1331                 if (rc)
1332                         ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1333                 else
1334                         ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
1335         case 2: /* child lock */
1336                 ldlm_lock_decref(&child_lockh, LCK_EX);
1337         case 1: /* child and parent dentry, parent lock */
1338                 if (rc)
1339                         ldlm_lock_decref(&parent_lockh, LCK_PW);
1340                 else
1341                         ptlrpc_save_lock(req, &parent_lockh, LCK_PW);
1342                 l_dput(dchild);
1343                 l_dput(dparent);
1344         case 0:
1345                 break;
1346         default:
1347                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1348                 LBUG();
1349         }
1350         req->rq_status = rc;
1351         return 0;
1352 }
1353
1354 static int mds_reint_link(struct mds_update_record *rec, int offset,
1355                           struct ptlrpc_request *req,
1356                           struct lustre_handle *lh)
1357 {
1358         struct obd_device *obd = req->rq_export->exp_obd;
1359         struct dentry *de_src = NULL;
1360         struct dentry *de_tgt_dir = NULL;
1361         struct dentry *dchild = NULL;
1362         struct mds_obd *mds = mds_req2mds(req);
1363         struct lustre_handle *handle = NULL, tgt_dir_lockh, src_lockh;
1364         struct ldlm_res_id src_res_id = { .name = {0} };
1365         struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
1366         ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
1367         ldlm_policy_data_t tgt_dir_policy =
1368                                        {.l_inodebits = {MDS_INODELOCK_UPDATE}};
1369
1370         int rc = 0, cleanup_phase = 0;
1371         ENTRY;
1372
1373         LASSERT(offset == 0);
1374
1375         DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
1376                   rec->ur_fid1->id, rec->ur_fid1->generation,
1377                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
1378
1379         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1380
1381         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1382                 GOTO(cleanup, rc = -ENOENT);
1383
1384         /* Step 1: Lookup the source inode and target directory by FID */
1385         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1386         if (IS_ERR(de_src))
1387                 GOTO(cleanup, rc = PTR_ERR(de_src));
1388
1389         cleanup_phase = 1; /* source dentry */
1390
1391         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
1392         if (IS_ERR(de_tgt_dir)) {
1393                 rc = PTR_ERR(de_tgt_dir);
1394                 de_tgt_dir = NULL;
1395                 GOTO(cleanup, rc);
1396         }
1397
1398         cleanup_phase = 2; /* target directory dentry */
1399
1400         CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
1401                de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
1402                de_src->d_inode->i_ino);
1403
1404         /* Step 2: Take the two locks */
1405         src_res_id.name[0] = de_src->d_inode->i_ino;
1406         src_res_id.name[1] = de_src->d_inode->i_generation;
1407         tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
1408         tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
1409
1410         rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
1411                                    &src_policy,
1412                                    &tgt_dir_res_id, &tgt_dir_lockh, LCK_EX,
1413                                    &tgt_dir_policy);
1414         if (rc)
1415                 GOTO(cleanup, rc);
1416
1417         cleanup_phase = 3; /* locks */
1418
1419         /* Step 3: Lookup the child */
1420         dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
1421         if (IS_ERR(dchild)) {
1422                 rc = PTR_ERR(dchild);
1423                 if (rc != -EPERM && rc != -EACCES)
1424                         CERROR("child lookup error %d\n", rc);
1425                 GOTO(cleanup, rc);
1426         }
1427
1428         cleanup_phase = 4; /* child dentry */
1429
1430         if (dchild->d_inode) {
1431                 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
1432                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
1433                 rc = -EEXIST;
1434                 GOTO(cleanup, rc);
1435         }
1436
1437         /* Step 4: Do it. */
1438         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1439
1440         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1441         if (IS_ERR(handle)) {
1442                 rc = PTR_ERR(handle);
1443                 GOTO(cleanup, rc);
1444         }
1445
1446         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
1447         if (rc && rc != -EPERM && rc != -EACCES)
1448                 CERROR("vfs_link error %d\n", rc);
1449 cleanup:
1450         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
1451                                 handle, req, rc, 0);
1452         EXIT;
1453
1454         switch (cleanup_phase) {
1455         case 4: /* child dentry */
1456                 l_dput(dchild);
1457         case 3: /* locks */
1458                 if (rc) {
1459                         ldlm_lock_decref(&src_lockh, LCK_EX);
1460                         ldlm_lock_decref(&tgt_dir_lockh, LCK_EX);
1461                 } else {
1462                         ptlrpc_save_lock(req, &src_lockh, LCK_EX);
1463                         ptlrpc_save_lock(req, &tgt_dir_lockh, LCK_EX);
1464                 }
1465         case 2: /* target dentry */
1466                 l_dput(de_tgt_dir);
1467         case 1: /* source dentry */
1468                 l_dput(de_src);
1469         case 0:
1470                 break;
1471         default:
1472                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1473                 LBUG();
1474         }
1475         req->rq_status = rc;
1476         return 0;
1477 }
1478
1479 /*
1480  * add a hard link in the PENDING directory, only used by rename()
1481  */
1482 static int mds_add_link_orphan(struct mds_update_record *rec,
1483                                struct obd_device *obd,
1484                                struct dentry *dentry)
1485 {
1486         struct mds_obd *mds = &obd->u.mds;
1487         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1488         struct dentry *pending_child;
1489         char fidname[LL_FID_NAMELEN];
1490         int fidlen = 0, rc;
1491         ENTRY;
1492
1493         LASSERT(dentry->d_inode);
1494         LASSERT(!mds_inode_is_orphan(dentry->d_inode));
1495
1496         down(&pending_dir->i_sem);
1497         fidlen = ll_fid2str(fidname, dentry->d_inode->i_ino,
1498                             dentry->d_inode->i_generation);
1499
1500         CDEBUG(D_ERROR, "pending destroy of %dx open file %s = %s\n",
1501                mds_open_orphan_count(dentry->d_inode),
1502                rec->ur_name, fidname);
1503
1504         pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
1505         if (IS_ERR(pending_child))
1506                 GOTO(out_lock, rc = PTR_ERR(pending_child));
1507
1508         if (pending_child->d_inode != NULL) {
1509                 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1510                 LASSERT(pending_child->d_inode == dentry->d_inode);
1511                 GOTO(out_dput, rc = 0);
1512         }
1513
1514         lock_kernel();
1515         rc = vfs_link(dentry, pending_dir, pending_child);
1516         unlock_kernel();
1517         if (rc)
1518                 CERROR("error addlink orphan %s to PENDING: rc = %d\n",
1519                        rec->ur_name, rc);
1520         else
1521                 mds_inode_set_orphan(dentry->d_inode);
1522 out_dput:
1523         l_dput(pending_child);
1524 out_lock:
1525         up(&pending_dir->i_sem);
1526         RETURN(rc);
1527 }
1528
1529 /* The idea here is that we need to get four locks in the end:
1530  * one on each parent directory, one on each child.  We need to take
1531  * these locks in some kind of order (to avoid deadlocks), and the order
1532  * I selected is "increasing resource number" order.  We need to look up
1533  * the children, however, before we know what the resource number(s) are.
1534  * Thus the following plan:
1535  *
1536  * 1,2. Look up the parents
1537  * 3,4. Look up the children
1538  * 5. Take locks on the parents and children, in order
1539  * 6. Verify that the children haven't changed since they were looked up
1540  *
1541  * If there was a race and the children changed since they were first looked
1542  * up, it is possible that mds_verify_child() will be able to just grab the
1543  * lock on the new child resource (if it has a higher resource than any other)
1544  * but we need to compare against not only its parent, but also against the
1545  * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
1546  *
1547  * We need the fancy igrab() on the child inodes because we aren't holding a
1548  * lock on the parent after the lookup is done, so dentry->d_inode may change
1549  * at any time, and igrab() itself doesn't like getting passed a NULL argument.
1550  */
1551 static int mds_get_parents_children_locked(struct obd_device *obd,
1552                                            struct mds_obd *mds,
1553                                            struct ll_fid *p1_fid,
1554                                            struct dentry **de_srcdirp,
1555                                            struct ll_fid *p2_fid,
1556                                            struct dentry **de_tgtdirp,
1557                                            int parent_mode,
1558                                            const char *old_name, int old_len,
1559                                            struct dentry **de_oldp,
1560                                            const char *new_name, int new_len,
1561                                            struct dentry **de_newp,
1562                                            struct lustre_handle *dlm_handles,
1563                                            int child_mode)
1564 {
1565         struct ldlm_res_id p1_res_id = { .name = {0} };
1566         struct ldlm_res_id p2_res_id = { .name = {0} };
1567         struct ldlm_res_id c1_res_id = { .name = {0} };
1568         struct ldlm_res_id c2_res_id = { .name = {0} };
1569         ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
1570         /* Only dentry should change, but the inode itself would be
1571            intact otherwise */
1572         ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
1573         /* If something is going to be replaced, both dentry and inode locks are
1574            needed */
1575         ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
1576                                                         MDS_INODELOCK_UPDATE}};
1577         struct ldlm_res_id *maxres_src, *maxres_tgt;
1578         struct inode *inode;
1579         int rc = 0, cleanup_phase = 0;
1580         ENTRY;
1581
1582         /* Step 1: Lookup the source directory */
1583         *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
1584         if (IS_ERR(*de_srcdirp))
1585                 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
1586
1587         cleanup_phase = 1; /* source directory dentry */
1588
1589         p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
1590         p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
1591
1592         /* Step 2: Lookup the target directory */
1593         if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
1594                 *de_tgtdirp = dget(*de_srcdirp);
1595         } else {
1596                 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
1597                 if (IS_ERR(*de_tgtdirp)) {
1598                         rc = PTR_ERR(*de_tgtdirp);
1599                         *de_tgtdirp = NULL;
1600                         GOTO(cleanup, rc);
1601                 }
1602         }
1603
1604         cleanup_phase = 2; /* target directory dentry */
1605
1606         p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
1607         p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
1608
1609         /* Step 3: Lookup the source child entry */
1610         *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
1611         if (IS_ERR(*de_oldp)) {
1612                 rc = PTR_ERR(*de_oldp);
1613                 CERROR("old child lookup error (%*s): %d\n",
1614                        old_len - 1, old_name, rc);
1615                 GOTO(cleanup, rc);
1616         }
1617
1618         cleanup_phase = 3; /* original name dentry */
1619
1620         inode = (*de_oldp)->d_inode;
1621         if (inode != NULL)
1622                 inode = igrab(inode);
1623         if (inode == NULL)
1624                 GOTO(cleanup, rc = -ENOENT);
1625
1626         c1_res_id.name[0] = inode->i_ino;
1627         c1_res_id.name[1] = inode->i_generation;
1628         iput(inode);
1629
1630         /* Step 4: Lookup the target child entry */
1631         *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
1632         if (IS_ERR(*de_newp)) {
1633                 rc = PTR_ERR(*de_newp);
1634                 CERROR("new child lookup error (%*s): %d\n",
1635                        old_len - 1, old_name, rc);
1636                 GOTO(cleanup, rc);
1637         }
1638
1639         cleanup_phase = 4; /* target dentry */
1640
1641         inode = (*de_newp)->d_inode;
1642         if (inode != NULL)
1643                 inode = igrab(inode);
1644         if (inode == NULL)
1645                 goto retry_locks;
1646
1647         c2_res_id.name[0] = inode->i_ino;
1648         c2_res_id.name[1] = inode->i_generation;
1649
1650         iput(inode);
1651
1652 retry_locks:
1653         /* Step 5: Take locks on the parents and child(ren) */
1654         maxres_src = &p1_res_id;
1655         maxres_tgt = &p2_res_id;
1656         cleanup_phase = 4; /* target dentry */
1657
1658         if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL,NULL))
1659                 maxres_src = &c1_res_id;
1660         if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL,NULL))
1661                 maxres_tgt = &c2_res_id;
1662
1663         rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
1664                                     &p_policy,
1665                                     &p2_res_id, &dlm_handles[1], parent_mode,
1666                                     &p_policy,
1667                                     &c1_res_id, &dlm_handles[2], child_mode,
1668                                     &c1_policy,
1669                                     &c2_res_id, &dlm_handles[3], child_mode,
1670                                     &c2_policy);
1671         if (rc)
1672                 GOTO(cleanup, rc);
1673
1674         cleanup_phase = 6; /* parent and child(ren) locks */
1675
1676         /* Step 6a: Re-lookup source child to verify it hasn't changed */
1677         rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
1678                               parent_mode, &c1_res_id, &dlm_handles[2],
1679                               de_oldp, child_mode, &c1_policy, old_name,old_len,
1680                               maxres_tgt);
1681         if (rc) {
1682                 if (c2_res_id.name[0] != 0)
1683                         ldlm_lock_decref(&dlm_handles[3], child_mode);
1684                 ldlm_lock_decref(&dlm_handles[1], parent_mode);
1685                 cleanup_phase = 4;
1686                 if (rc > 0)
1687                         goto retry_locks;
1688                 GOTO(cleanup, rc);
1689         }
1690
1691         if ((*de_oldp)->d_inode == NULL)
1692                 GOTO(cleanup, rc = -ENOENT);
1693
1694         /* Step 6b: Re-lookup target child to verify it hasn't changed */
1695         rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
1696                               parent_mode, &c2_res_id, &dlm_handles[3],
1697                               de_newp, child_mode, &c2_policy, new_name,
1698                               new_len, maxres_src);
1699         if (rc) {
1700                 ldlm_lock_decref(&dlm_handles[2], child_mode);
1701                 ldlm_lock_decref(&dlm_handles[0], parent_mode);
1702                 cleanup_phase = 4;
1703                 if (rc > 0)
1704                         goto retry_locks;
1705                 GOTO(cleanup, rc);
1706         }
1707
1708         EXIT;
1709 cleanup:
1710         if (rc) {
1711                 switch (cleanup_phase) {
1712                 case 6: /* child lock(s) */
1713                         if (c2_res_id.name[0] != 0)
1714                                 ldlm_lock_decref(&dlm_handles[3], child_mode);
1715                         if (c1_res_id.name[0] != 0)
1716                                 ldlm_lock_decref(&dlm_handles[2], child_mode);
1717                 case 5: /* parent locks */
1718                         ldlm_lock_decref(&dlm_handles[1], parent_mode);
1719                         ldlm_lock_decref(&dlm_handles[0], parent_mode);
1720                 case 4: /* target dentry */
1721                         l_dput(*de_newp);
1722                 case 3: /* source dentry */
1723                         l_dput(*de_oldp);
1724                 case 2: /* target directory dentry */
1725                         l_dput(*de_tgtdirp);
1726                 case 1: /* source directry dentry */
1727                         l_dput(*de_srcdirp);
1728                 }
1729         }
1730
1731         return rc;
1732 }
1733
1734 static int mds_reint_rename(struct mds_update_record *rec, int offset,
1735                             struct ptlrpc_request *req,
1736                             struct lustre_handle *lockh)
1737 {
1738         struct obd_device *obd = req->rq_export->exp_obd;
1739         struct dentry *de_srcdir = NULL;
1740         struct dentry *de_tgtdir = NULL;
1741         struct dentry *de_old = NULL;
1742         struct dentry *de_new = NULL;
1743         struct mds_obd *mds = mds_req2mds(req);
1744         struct lustre_handle dlm_handles[4];
1745         struct mds_body *body = NULL;
1746         int rc = 0, lock_count = 3;
1747         int cleanup_phase = 0;
1748         void *handle = NULL;
1749         ENTRY;
1750
1751         LASSERT(offset == 0);
1752
1753         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
1754                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
1755                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
1756
1757         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1758
1759         rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
1760                                              rec->ur_fid2, &de_tgtdir, LCK_PW,
1761                                              rec->ur_name, rec->ur_namelen,
1762                                              &de_old, rec->ur_tgt,
1763                                              rec->ur_tgtlen, &de_new,
1764                                              dlm_handles, LCK_EX);
1765         if (rc)
1766                 GOTO(cleanup, rc);
1767
1768         cleanup_phase = 1; /* parent(s), children, locks */
1769
1770         if (de_new->d_inode)
1771                 lock_count = 4;
1772
1773         /* sanity check for src inode */
1774         if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
1775             de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
1776                 GOTO(cleanup, rc = -EINVAL);
1777
1778         /* sanity check for dest inode */
1779         if (de_new->d_inode &&
1780             (de_new->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
1781              de_new->d_inode->i_ino == de_tgtdir->d_inode->i_ino))
1782                 GOTO(cleanup, rc = -EINVAL);
1783
1784         if (de_old->d_inode == de_new->d_inode) {
1785                 GOTO(cleanup, rc = 0);
1786         }
1787
1788         /* if we are about to remove the target at first, pass the EA of
1789          * that inode to client to perform and cleanup on OST */
1790         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
1791         LASSERT(body != NULL);
1792
1793         if (de_new->d_inode &&
1794             S_ISREG(de_new->d_inode->i_mode) &&
1795             de_new->d_inode->i_nlink == 1 &&
1796             mds_open_orphan_count(de_new->d_inode) == 0) {
1797                 mds_pack_inode2fid(&body->fid1, de_new->d_inode);
1798                 mds_pack_inode2body(body, de_new->d_inode);
1799                 mds_pack_md(obd, req->rq_repmsg, 1, body, de_new->d_inode, 1);
1800                 if (!(body->valid & OBD_MD_FLEASIZE)) {
1801                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1802                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
1803                 } else {
1804                         /* XXX need log unlink? */
1805                 }
1806         }
1807
1808         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
1809                        de_srcdir->d_inode->i_sb);
1810
1811         handle = fsfilt_start(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME, NULL);
1812         if (IS_ERR(handle))
1813                 GOTO(cleanup, rc = PTR_ERR(handle));
1814
1815         /* FIXME need adjust the journal block count? */
1816         /* if the target should be moved to PENDING, we at first increase the
1817          * link and later vfs_rename() will decrease the link count again */
1818         if (de_new->d_inode &&
1819             S_ISREG(de_new->d_inode->i_mode) &&
1820             de_new->d_inode->i_nlink == 1 &&
1821             mds_open_orphan_count(de_new->d_inode) > 0) {
1822                 rc = mds_add_link_orphan(rec, obd, de_new);
1823                 if (rc)
1824                         GOTO(cleanup, rc);
1825         }
1826
1827         lock_kernel();
1828         de_old->d_fsdata = req;
1829         de_new->d_fsdata = req;
1830         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
1831         unlock_kernel();
1832
1833         GOTO(cleanup, rc);
1834 cleanup:
1835         rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
1836                                 handle, req, rc, 0);
1837         switch (cleanup_phase) {
1838         case 1:
1839                 if (rc) {
1840                         if (lock_count == 4)
1841                                 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
1842                         ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
1843                         ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
1844                         ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
1845                 } else {
1846                         if (lock_count == 4)
1847                                 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
1848                         ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
1849                         ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
1850                         ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
1851                 }
1852                 l_dput(de_new);
1853                 l_dput(de_old);
1854                 l_dput(de_tgtdir);
1855                 l_dput(de_srcdir);
1856         case 0:
1857                 break;
1858         default:
1859                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1860                 LBUG();
1861         }
1862         req->rq_status = rc;
1863         return 0;
1864 }
1865
1866 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
1867                            struct ptlrpc_request *, struct lustre_handle *);
1868
1869 static mds_reinter reinters[REINT_MAX + 1] = {
1870         [REINT_SETATTR] mds_reint_setattr,
1871         [REINT_CREATE] mds_reint_create,
1872         [REINT_LINK] mds_reint_link,
1873         [REINT_UNLINK] mds_reint_unlink,
1874         [REINT_RENAME] mds_reint_rename,
1875         [REINT_OPEN] mds_open
1876 };
1877
1878 int mds_reint_rec(struct mds_update_record *rec, int offset,
1879                   struct ptlrpc_request *req, struct lustre_handle *lockh)
1880 {
1881         struct obd_device *obd = req->rq_export->exp_obd;
1882         struct obd_run_ctxt saved;
1883         int rc;
1884         ENTRY;
1885
1886         /* checked by unpacker */
1887         LASSERT(rec->ur_opcode <= REINT_MAX &&
1888                 reinters[rec->ur_opcode] != NULL);
1889
1890         push_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);
1891         rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
1892         pop_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);
1893
1894         RETURN(rc);
1895 }