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