Whamcloud - gitweb
Fixes and cleanups in lmv.
[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_log.h>
42 #include <linux/lustre_fsfilt.h>
43
44 #include "mds_internal.h"
45
46 void mds_commit_cb(struct obd_device *obd, __u64 transno, void *data,
47                    int error)
48 {
49         obd_transno_commit_cb(obd, transno, error);
50 }
51
52 struct mds_logcancel_data {
53         struct lov_mds_md      *mlcd_lmm;
54         int                     mlcd_size;
55         int                     mlcd_cookielen;
56         int                     mlcd_eadatalen;
57         struct llog_cookie      mlcd_cookies[0];
58 };
59
60
61 static void mds_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
62                                   void *cb_data, int error)
63 {
64         struct mds_logcancel_data *mlcd = cb_data;
65         struct lov_stripe_md *lsm = NULL;
66         struct llog_ctxt *ctxt;
67         int rc;
68
69         obd_transno_commit_cb(obd, transno, error);
70
71         CDEBUG(D_HA, "cancelling %d cookies\n",
72                (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
73
74         rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, mlcd->mlcd_lmm,
75                           mlcd->mlcd_eadatalen);
76         if (rc < 0) {
77                 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
78                        (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
79                        rc);
80         } else {
81                 ///* XXX 0 normally, SENDNOW for debug */);
82                 ctxt = llog_get_context(&obd->obd_llogs,
83                                         mlcd->mlcd_cookies[0].lgc_subsys + 1);
84                 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
85                                  sizeof(*mlcd->mlcd_cookies),
86                                  mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
87                 if (rc)
88                         CERROR("error cancelling %d log cookies: rc %d\n",
89                                (int)(mlcd->mlcd_cookielen /
90                                      sizeof(*mlcd->mlcd_cookies)), rc);
91         }
92
93         OBD_FREE(mlcd, mlcd->mlcd_size);
94 }
95
96 /* Assumes caller has already pushed us into the kernel context. */
97 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
98                        struct ptlrpc_request *req, int rc, __u32 op_data)
99 {
100         struct mds_export_data *med = &req->rq_export->exp_mds_data;
101         struct mds_client_data *mcd = med->med_mcd;
102         struct obd_device *obd = req->rq_export->exp_obd;
103         int err;
104         __u64 transno;
105         loff_t off;
106         int log_pri = D_HA;
107         ENTRY;
108
109         /* if the export has already been failed, we have no last_rcvd slot */
110         if (req->rq_export->exp_failed) {
111                 CERROR("committing transaction for disconnected client\n");
112                 if (handle)
113                         GOTO(commit, rc);
114                 RETURN(rc);
115         }
116
117         if (IS_ERR(handle))
118                 RETURN(rc);
119
120         if (handle == NULL) {
121                 /* if we're starting our own xaction, use our own inode */
122                 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
123                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
124                 if (IS_ERR(handle)) {
125                         CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
126                         RETURN(PTR_ERR(handle));
127                 }
128         }
129
130         off = med->med_off;
131
132         transno = req->rq_reqmsg->transno;
133         if (rc != 0) {
134                 LASSERT(transno == 0);
135         } else if (transno == 0) {
136                 spin_lock(&mds->mds_transno_lock);
137                 transno = ++mds->mds_last_transno;
138                 spin_unlock(&mds->mds_transno_lock);
139         } else {
140                 spin_lock(&mds->mds_transno_lock);
141                 if (transno > mds->mds_last_transno)
142                         mds->mds_last_transno = transno;
143                 spin_unlock(&mds->mds_transno_lock);
144         }
145         req->rq_repmsg->transno = req->rq_transno = transno;
146         mcd->mcd_last_transno = cpu_to_le64(transno);
147         mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
148         mcd->mcd_last_result = cpu_to_le32(rc);
149         mcd->mcd_last_data = cpu_to_le32(op_data);
150
151
152         fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb,
153                               transno, handle, mds_commit_cb, NULL);
154         
155         err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd, sizeof(*mcd),
156                                   &off, 0);
157
158         if (err) {
159                 log_pri = D_ERROR;
160                 if (rc == 0)
161                         rc = err;
162         }
163
164         DEBUG_REQ(log_pri, req,
165                   "wrote trans #"LPU64" client %s at idx %u: err = %d",
166                   transno, mcd->mcd_uuid, med->med_idx, err);
167
168         err = mds_lov_write_objids(obd);
169         if (err) {
170                 log_pri = D_ERROR;
171                 if (rc == 0)
172                         rc = err;
173         }
174         CDEBUG(log_pri, "wrote objids: err = %d\n", err);
175
176 commit:
177         err = fsfilt_commit(obd, mds->mds_sb, inode, handle, 0);
178         if (err) {
179                 CERROR("error committing transaction: %d\n", err);
180                 if (!rc)
181                         rc = err;
182         }
183
184         RETURN(rc);
185 }
186
187 /* this gives the same functionality as the code between
188  * sys_chmod and inode_setattr
189  * chown_common and inode_setattr
190  * utimes and inode_setattr
191  */
192 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
193 {
194         time_t now = LTIME_S(CURRENT_TIME);
195         struct iattr *attr = &rec->ur_iattr;
196         unsigned int ia_valid = attr->ia_valid;
197         int error;
198         ENTRY;
199
200         /* only fix up attrs if the client VFS didn't already */
201         if (!(ia_valid & ATTR_RAW))
202                 RETURN(0);
203
204         if (!(ia_valid & ATTR_CTIME_SET))
205                 LTIME_S(attr->ia_ctime) = now;
206         if (!(ia_valid & ATTR_ATIME_SET))
207                 LTIME_S(attr->ia_atime) = now;
208         if (!(ia_valid & ATTR_MTIME_SET))
209                 LTIME_S(attr->ia_mtime) = now;
210
211         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
212                 RETURN(-EPERM);
213
214         /* times */
215         if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
216                 if (rec->_ur_fsuid != inode->i_uid &&
217                     (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
218                         RETURN(error);
219         }
220
221         if (ia_valid & ATTR_SIZE) {
222                 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
223                         RETURN(error);
224         }
225
226         if (ia_valid & ATTR_UID) {
227                 /* chown */
228                 error = -EPERM;
229                 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
230                         RETURN(-EPERM);
231                 if (attr->ia_uid == (uid_t) -1)
232                         attr->ia_uid = inode->i_uid;
233                 if (attr->ia_gid == (gid_t) -1)
234                         attr->ia_gid = inode->i_gid;
235                 attr->ia_mode = inode->i_mode;
236                 /*
237                  * If the user or group of a non-directory has been
238                  * changed by a non-root user, remove the setuid bit.
239                  * 19981026 David C Niemi <niemi@tux.org>
240                  *
241                  * Changed this to apply to all users, including root,
242                  * to avoid some races. This is the behavior we had in
243                  * 2.0. The check for non-root was definitely wrong
244                  * for 2.2 anyway, as it should have been using
245                  * CAP_FSETID rather than fsuid -- 19990830 SD.
246                  */
247                 if ((inode->i_mode & S_ISUID) == S_ISUID &&
248                     !S_ISDIR(inode->i_mode)) {
249                         attr->ia_mode &= ~S_ISUID;
250                         attr->ia_valid |= ATTR_MODE;
251                 }
252                 /*
253                  * Likewise, if the user or group of a non-directory
254                  * has been changed by a non-root user, remove the
255                  * setgid bit UNLESS there is no group execute bit
256                  * (this would be a file marked for mandatory
257                  * locking).  19981026 David C Niemi <niemi@tux.org>
258                  *
259                  * Removed the fsuid check (see the comment above) --
260                  * 19990830 SD.
261                  */
262                 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
263                      (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
264                         attr->ia_mode &= ~S_ISGID;
265                         attr->ia_valid |= ATTR_MODE;
266                 }
267         } else if (ia_valid & ATTR_MODE) {
268                 int mode = attr->ia_mode;
269                 /* chmod */
270                 if (attr->ia_mode == (mode_t) -1)
271                         attr->ia_mode = inode->i_mode;
272                 attr->ia_mode =
273                         (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
274         }
275         RETURN(0);
276 }
277
278 void mds_steal_ack_locks(struct ptlrpc_request *req)
279 {
280         struct obd_export         *exp = req->rq_export;
281         struct list_head          *tmp;
282         struct ptlrpc_reply_state *oldrep;
283         struct ptlrpc_service     *svc;
284         unsigned long              flags;
285         char                       str[PTL_NALFMT_SIZE];
286         int                        i;
287
288         /* CAVEAT EMPTOR: spinlock order */
289         spin_lock_irqsave (&exp->exp_lock, flags);
290         list_for_each (tmp, &exp->exp_outstanding_replies) {
291                 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
292
293                 if (oldrep->rs_xid != req->rq_xid)
294                         continue;
295
296                 if (oldrep->rs_msg.opc != req->rq_reqmsg->opc)
297                         CERROR ("Resent req xid "LPX64" has mismatched opc: "
298                                 "new %d old %d\n", req->rq_xid,
299                                 req->rq_reqmsg->opc, oldrep->rs_msg.opc);
300
301                 svc = oldrep->rs_srv_ni->sni_service;
302                 spin_lock (&svc->srv_lock);
303
304                 list_del_init (&oldrep->rs_exp_list);
305
306                 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
307                       " o%d NID %s\n",
308                       oldrep->rs_nlocks, oldrep,
309                       oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg.opc,
310                       ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
311
312                 for (i = 0; i < oldrep->rs_nlocks; i++)
313                         ptlrpc_save_lock(req,
314                                          &oldrep->rs_locks[i],
315                                          oldrep->rs_modes[i]);
316                 oldrep->rs_nlocks = 0;
317
318                 DEBUG_REQ(D_HA, req, "stole locks for");
319                 ptlrpc_schedule_difficult_reply (oldrep);
320
321                 spin_unlock (&svc->srv_lock);
322                 spin_unlock_irqrestore (&exp->exp_lock, flags);
323                 return;
324         }
325         spin_unlock_irqrestore (&exp->exp_lock, flags);
326 }
327
328 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
329 {
330         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
331                   mcd->mcd_last_transno, mcd->mcd_last_result);
332         req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
333         req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
334
335         mds_steal_ack_locks(req);
336 }
337
338 static void reconstruct_reint_setattr(struct mds_update_record *rec,
339                                       int offset, struct ptlrpc_request *req)
340 {
341         struct mds_export_data *med = &req->rq_export->exp_mds_data;
342         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
343         struct dentry *de;
344         struct mds_body *body;
345
346         mds_req_from_mcd(req, med->med_mcd);
347
348         de = mds_fid2dentry(obd, rec->ur_fid1, NULL);
349         if (IS_ERR(de)) {
350                 LASSERT(PTR_ERR(de) == req->rq_status);
351                 return;
352         }
353
354         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
355         mds_pack_inode2fid(req2obd(req), &body->fid1, de->d_inode);
356         mds_pack_inode2body(req2obd(req), body, de->d_inode);
357
358         /* Don't return OST-specific attributes if we didn't just set them */
359         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
360                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
361         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
362                 body->valid |= OBD_MD_FLMTIME;
363         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
364                 body->valid |= OBD_MD_FLATIME;
365
366         l_dput(de);
367 }
368
369 /* In the raw-setattr case, we lock the child inode.
370  * In the write-back case or if being called from open, the client holds a lock
371  * already.
372  *
373  * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
374 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
375                              struct ptlrpc_request *req,
376                              struct lustre_handle *lh)
377 {
378         struct mds_obd *mds = mds_req2mds(req);
379         struct obd_device *obd = req->rq_export->exp_obd;
380         struct mds_body *body;
381         struct dentry *de;
382         struct inode *inode = NULL;
383         struct lustre_handle lockh[2] = {{0}, {0}};
384         int parent_mode;
385         void *handle = NULL;
386         struct mds_logcancel_data *mlcd = NULL;
387         int rc = 0, cleanup_phase = 0, err, locked = 0;
388         ENTRY;
389
390         LASSERT(offset == 0);
391
392         DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x", rec->ur_fid1->id,
393                   rec->ur_fid1->generation, rec->ur_iattr.ia_valid);
394
395         MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
396
397         if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
398                 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
399                 if (IS_ERR(de))
400                         GOTO(cleanup, rc = PTR_ERR(de));
401         } else {
402                 __u64 lockpart = MDS_INODELOCK_UPDATE;
403                 if (rec->ur_iattr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID) )
404                         lockpart |= MDS_INODELOCK_LOOKUP;
405                 de = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW,
406                                            lockh, &parent_mode, NULL, 0, lockpart);
407                 if (IS_ERR(de))
408                         GOTO(cleanup, rc = PTR_ERR(de));
409                 locked = 1;
410         }
411
412         cleanup_phase = 1;
413
414         inode = de->d_inode;
415         LASSERT(inode);
416         if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
417             rec->ur_eadata != NULL)
418                 down(&inode->i_sem);
419
420         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
421
422         handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
423         if (IS_ERR(handle))
424                 GOTO(cleanup, rc = PTR_ERR(handle));
425
426         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
427                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
428                        LTIME_S(rec->ur_iattr.ia_mtime),
429                        LTIME_S(rec->ur_iattr.ia_ctime));
430         rc = mds_fix_attr(inode, rec);
431         if (rc)
432                 GOTO(cleanup, rc);
433
434         if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG)    /* ioctl */
435                 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
436                                       (long)&rec->ur_iattr.ia_attr_flags);
437         else                                            /* setattr */
438                 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
439
440         if (rc == 0 && (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
441             rec->ur_eadata != NULL) {
442                 struct lov_stripe_md *lsm = NULL;
443
444                 rc = ll_permission(inode, MAY_WRITE, NULL);
445                 if (rc < 0)
446                         GOTO(cleanup, rc);
447
448                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
449                                    mds->mds_osc_exp, 0, &lsm, rec->ur_eadata);
450                 if (rc)
451                         GOTO(cleanup, rc);
452
453                 obd_free_memmd(mds->mds_osc_exp, &lsm);
454
455                 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
456                                    rec->ur_eadatalen);
457                 if (rc)
458                         GOTO(cleanup, rc);
459         }
460
461         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
462         mds_pack_inode2fid(obd, &body->fid1, inode);
463         mds_pack_inode2body(obd, body, inode);
464
465         /* Don't return OST-specific attributes if we didn't just set them */
466         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
467                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
468         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
469                 body->valid |= OBD_MD_FLMTIME;
470         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
471                 body->valid |= OBD_MD_FLATIME;
472
473         if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_osc_obd)) {
474                 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
475                           rec->ur_eadatalen);
476                 if (mlcd) {
477                         mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
478                                 rec->ur_eadatalen;
479                         mlcd->mlcd_eadatalen = rec->ur_eadatalen;
480                         mlcd->mlcd_cookielen = rec->ur_cookielen;
481                         mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
482                                 mlcd->mlcd_cookielen;
483                         memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
484                                mlcd->mlcd_cookielen);
485                         memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
486                                mlcd->mlcd_eadatalen);
487                 } else {
488                         CERROR("unable to allocate log cancel data\n");
489                 }
490         }
491         EXIT;
492  cleanup:
493         if (mlcd != NULL)
494                 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
495                                       handle, mds_cancel_cookies_cb, mlcd);
496         err = mds_finish_transno(mds, inode, handle, req, rc, 0);
497         switch (cleanup_phase) {
498         case 1:
499                 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
500                     rec->ur_eadata != NULL)
501                         up(&inode->i_sem);
502                 l_dput(de);
503                 if (locked) {
504 #ifdef S_PDIROPS
505                         if (lockh[1].cookie != 0)
506                                 ldlm_lock_decref(lockh + 1, parent_mode);
507 #endif
508                         if (rc) {
509                                 ldlm_lock_decref(lockh, LCK_PW);
510                         } else {
511                                 ptlrpc_save_lock (req, lockh, LCK_PW);
512                         }
513                 }
514         case 0:
515                 break;
516         default:
517                 LBUG();
518         }
519         if (err && !rc)
520                 rc = err;
521
522         req->rq_status = rc;
523         return 0;
524 }
525
526 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
527                                      struct ptlrpc_request *req)
528 {
529         struct mds_export_data *med = &req->rq_export->exp_mds_data;
530         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
531         struct dentry *parent, *child;
532         struct mds_body *body;
533
534         mds_req_from_mcd(req, med->med_mcd);
535
536         if (req->rq_status)
537                 return;
538
539         parent = mds_fid2dentry(obd, rec->ur_fid1, NULL);
540         LASSERT(!IS_ERR(parent));
541         child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
542         LASSERT(!IS_ERR(child));
543         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
544         mds_pack_inode2fid(req2obd(req), &body->fid1, child->d_inode);
545         mds_pack_inode2body(req2obd(req), body, child->d_inode);
546         l_dput(parent);
547         l_dput(child);
548 }
549
550 static int mds_reint_create(struct mds_update_record *rec, int offset,
551                             struct ptlrpc_request *req,
552                             struct lustre_handle *lh)
553 {
554         struct dentry *dparent = NULL;
555         struct mds_obd *mds = mds_req2mds(req);
556         struct obd_device *obd = req->rq_export->exp_obd;
557         struct dentry *dchild = NULL;
558         struct inode *dir = NULL;
559         void *handle = NULL;
560         struct lustre_handle lockh[2] = {{0}, {0}};
561         int parent_mode;
562         int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
563         int created = 0;
564         struct dentry_params dp;
565         struct mea *mea = NULL;
566         int mea_size;
567         ENTRY;
568
569         LASSERT(offset == 0);
570         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, LUSTRE_MDS_NAME));
571
572         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
573                   rec->ur_fid1->id, rec->ur_fid1->generation,
574                   rec->ur_name, rec->ur_mode);
575
576         MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
577
578         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
579                 GOTO(cleanup, rc = -ESTALE);
580
581         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW, lockh,
582                                         &parent_mode, rec->ur_name,
583                                         rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
584         if (IS_ERR(dparent)) {
585                 rc = PTR_ERR(dparent);
586                 CERROR("parent lookup error %d\n", rc);
587                 GOTO(cleanup, rc);
588         }
589         cleanup_phase = 1; /* locked parent dentry */
590         dir = dparent->d_inode;
591         LASSERT(dir);
592
593         ldlm_lock_dump_handle(D_OTHER, lockh);
594
595         /* try to retrieve MEA data for this dir */
596         rc = mds_get_lmv_attr(obd, dparent->d_inode, &mea, &mea_size);
597         if (rc)
598                 GOTO(cleanup, rc);
599
600         if (mea != NULL) {
601                 /* dir is already splitted, check is requested filename
602                  * should live at this MDS or at another one */
603                 int i;
604                 i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
605                 if (mea->mea_master != i) {
606                         CERROR("inapropriate MDS(%d) for %s. should be %d\n",
607                                 mea->mea_master, rec->ur_name, i);
608                         GOTO(cleanup, rc = -ERESTART);
609                 }
610         }
611
612         dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
613         if (IS_ERR(dchild)) {
614                 rc = PTR_ERR(dchild);
615                 CERROR("child lookup error %d\n", rc);
616                 GOTO(cleanup, rc);
617         }
618
619         cleanup_phase = 2; /* child dentry */
620
621         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
622
623         if (type == S_IFREG || type == S_IFDIR) {
624                 if ((rc = mds_try_to_split_dir(obd, dparent, &mea, 0))) {
625                         if (rc > 0) {
626                                 /* dir got splitted */
627                                 GOTO(cleanup, rc = -ERESTART);
628                         } else {
629                                 /* error happened during spitting */
630                                 GOTO(cleanup, rc);
631                         }
632                 }
633         }
634
635         if (dir->i_mode & S_ISGID) {
636                 if (S_ISDIR(rec->ur_mode))
637                         rec->ur_mode |= S_ISGID;
638         }
639
640         dchild->d_fsdata = (void *)&dp;
641         dp.p_inum = (unsigned long)rec->ur_fid2->id;
642         dp.p_ptr = req;
643
644         switch (type) {
645         case S_IFREG:{
646                 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
647                 if (IS_ERR(handle))
648                         GOTO(cleanup, rc = PTR_ERR(handle));
649                 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
650                 EXIT;
651                 break;
652         }
653         case S_IFDIR:{
654                 int nstripes = 0;
655                 int i;
656                 
657                 /* as Peter asked, mkdir() should distribute new directories
658                  * over the whole cluster in order to distribute namespace
659                  * processing load. first, we calculate which MDS to use to
660                  * put new directory's inode in */
661                 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1, 
662                                       rec->ur_flags);
663                 if (i == mds->mds_num) {
664                         /* inode will be created locally */
665
666                         handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
667                         if (IS_ERR(handle))
668                                 GOTO(cleanup, rc = PTR_ERR(handle));
669
670                         rc = vfs_mkdir(dir, dchild, rec->ur_mode);
671
672                         if (rec->ur_eadata)
673                                 nstripes = *(u16 *)rec->ur_eadata;
674
675                         if (rc == 0 && nstripes) {
676                                 /* FIXME: error handling here */
677                                 mds_try_to_split_dir(obd, dchild,
678                                                         NULL, nstripes);
679                         }
680                 } else if (!DENTRY_VALID(dchild)) {
681                         /* inode will be created on another MDS */
682                         struct obdo *oa = NULL;
683                         struct mds_body *body;
684                         
685                         /* first, create that inode */
686                         oa = obdo_alloc();
687                         LASSERT(oa != NULL);
688                         oa->o_mds = i;
689                         obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
690                                         OBD_MD_FLMTIME | OBD_MD_FLCTIME |
691                                         OBD_MD_FLUID | OBD_MD_FLGID);
692                         oa->o_mode = dir->i_mode;
693                         CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
694                                         obd->obd_name, i);
695                         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
696                                 CWARN("%s: replay dir creation %*s -> %u/%u\n",
697                                       obd->obd_name, rec->ur_namelen - 1,
698                                       rec->ur_name, (unsigned) rec->ur_fid2->id,
699                                       (unsigned) rec->ur_fid2->generation);
700                                 oa->o_id = rec->ur_fid2->id;
701                                 oa->o_generation = rec->ur_fid2->generation;
702                                 oa->o_flags |= OBD_FL_RECREATE_OBJS;
703                         }
704
705                         rc = obd_create(mds->mds_lmv_exp, oa, NULL, NULL);
706                         if (rc) {
707                                 obdo_free(oa);
708                                 GOTO(cleanup, rc);
709                         }
710                         
711                         /* now, add new dir entry for it */
712                         handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
713                         if (IS_ERR(handle)) {
714                                 obdo_free(oa);
715                                 GOTO(cleanup, rc = PTR_ERR(handle));
716                         }
717                         rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
718                                                   rec->ur_namelen - 1,
719                                                   oa->o_id, oa->o_generation,
720                                                   i);
721                         LASSERT(rc == 0);
722
723                         /* fill reply */
724                         body = lustre_msg_buf(req->rq_repmsg,
725                                               offset, sizeof (*body));
726                         body->valid |= OBD_MD_FLID | OBD_MD_MDS;
727                         body->fid1.id = oa->o_id;
728                         body->fid1.mds = i;
729                         body->fid1.generation = oa->o_generation;
730                         obdo_free(oa);
731                 } else {
732                         /* requested name exists in the directory */
733                         rc = -EEXIST;
734                 }
735                 EXIT;
736                 break;
737         }
738         case S_IFLNK:{
739                 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
740                 if (IS_ERR(handle))
741                         GOTO(cleanup, rc = PTR_ERR(handle));
742                 if (rec->ur_tgt == NULL)        /* no target supplied */
743                         rc = -EINVAL;           /* -EPROTO? */
744                 else
745                         rc = vfs_symlink(dir, dchild, rec->ur_tgt);
746                 EXIT;
747                 break;
748         }
749         case S_IFCHR:
750         case S_IFBLK:
751         case S_IFIFO:
752         case S_IFSOCK:{
753                 int rdev = rec->ur_rdev;
754                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
755                 if (IS_ERR(handle))
756                         GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
757                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
758                 EXIT;
759                 break;
760         }
761         default:
762                 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
763                 dchild->d_fsdata = NULL;
764                 GOTO(cleanup, rc = -EINVAL);
765         }
766
767         /* In case we stored the desired inum in here, we want to clean up. */
768         if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
769                 dchild->d_fsdata = NULL;
770
771         if (rc) {
772                 CDEBUG(D_INODE, "error during create: %d\n", rc);
773                 GOTO(cleanup, rc);
774         } else if (dchild->d_inode) {
775                 struct iattr iattr;
776                 struct inode *inode = dchild->d_inode;
777                 struct mds_body *body;
778
779                 created = 1;
780                 LTIME_S(iattr.ia_atime) = rec->ur_time;
781                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
782                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
783                 iattr.ia_uid = rec->_ur_fsuid;
784                 if (dir->i_mode & S_ISGID)
785                         iattr.ia_gid = dir->i_gid;
786                 else
787                         iattr.ia_gid = rec->_ur_fsgid;
788                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
789                         ATTR_MTIME | ATTR_CTIME;
790
791                 if (rec->ur_fid2->id) {
792                         LASSERT(rec->ur_fid2->id == inode->i_ino);
793                         inode->i_generation = rec->ur_fid2->generation;
794                         /* Dirtied and committed by the upcoming setattr. */
795                         CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
796                                inode->i_ino, inode->i_generation);
797                 } else {
798                         struct lustre_handle child_ino_lockh;
799
800                         CDEBUG(D_INODE, "created ino %lu with gen %x\n",
801                                inode->i_ino, inode->i_generation);
802
803                         /* The inode we were allocated may have just been freed
804                          * by an unlink operation.  We take this lock to
805                          * synchronize against the matching reply-ack-lock taken
806                          * in unlink, to avoid replay problems if this reply
807                          * makes it out to the client but the unlink's does not.
808                          * See bug 2029 for more detail.*/
809                         rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
810                         if (rc != ELDLM_OK) {
811                                 CERROR("error locking for unlink/create sync: "
812                                        "%d\n", rc);
813                         } else {
814                                 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
815                         }
816                 }
817
818                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
819                 if (rc)
820                         CERROR("error on child setattr: rc = %d\n", rc);
821
822                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
823                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
824                 if (rc)
825                         CERROR("error on parent setattr: rc = %d\n", rc);
826
827                 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
828                 mds_pack_inode2fid(obd, &body->fid1, inode);
829                 mds_pack_inode2body(obd, body, inode);
830         }
831         EXIT;
832
833 cleanup:
834         err = mds_finish_transno(mds, dir, handle, req, rc, 0);
835
836         if (rc && created) {
837                 /* Destroy the file we just created.  This should not need
838                  * extra journal credits, as we have already modified all of
839                  * the blocks needed in order to create the file in the first
840                  * place.
841                  */
842                 switch (type) {
843                 case S_IFDIR:
844                         err = vfs_rmdir(dir, dchild);
845                         if (err)
846                                 CERROR("rmdir in error path: %d\n", err);
847                         break;
848                 default:
849                         err = vfs_unlink(dir, dchild);
850                         if (err)
851                                 CERROR("unlink in error path: %d\n", err);
852                         break;
853                 }
854         } else {
855                 rc = err;
856         }
857         switch (cleanup_phase) {
858         case 2: /* child dentry */
859                 l_dput(dchild);
860         case 1: /* locked parent dentry */
861 #ifdef S_PDIROPS
862                 if (lockh[1].cookie != 0)
863                         ldlm_lock_decref(lockh + 1, parent_mode);
864 #endif
865                 if (rc) {
866                         ldlm_lock_decref(lockh, LCK_PW);
867                 } else {
868                         ptlrpc_save_lock (req, lockh, LCK_PW);
869                 }
870                 l_dput(dparent);
871         case 0:
872                 break;
873         default:
874                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
875                 LBUG();
876         }
877         if (mea)
878                 OBD_FREE(mea, mea_size);
879         req->rq_status = rc;
880         return 0;
881 }
882
883 static int res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
884            ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
885 {
886         int i;
887
888         for (i = 0; i < RES_NAME_SIZE; i++) {
889                 /* return 1 here, because enqueue_ordered will skip resources
890                  * of all zeroes if they're sorted to the end of the list. */
891                 if (res1->name[i] == 0 && res2->name[i] != 0)
892                         return 1;
893                 if (res2->name[i] == 0 && res1->name[i] != 0)
894                         return 0;
895
896                 if (res1->name[i] > res2->name[i])
897                         return 1;
898                 if (res1->name[i] < res2->name[i])
899                         return 0;
900         }
901
902         if (!p1 || !p2)
903                 return 0;
904
905         if (memcmp(p1, p2, sizeof(*p1)) < 0)
906                 return 1;
907
908         return 0;
909 }
910
911 /* This function doesn't use ldlm_match_or_enqueue because we're always called
912  * with EX or PW locks, and the MDS is no longer allowed to match write locks,
913  * because they take the place of local semaphores.
914  *
915  * One or two locks are taken in numerical order.  A res_id->name[0] of 0 means
916  * no lock is taken for that res_id.  Must be at least one non-zero res_id. */
917 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
918                           struct lustre_handle *p1_lockh, int p1_lock_mode,
919                           ldlm_policy_data_t *p1_policy,
920                           struct ldlm_res_id *p2_res_id,
921                           struct lustre_handle *p2_lockh, int p2_lock_mode,
922                           ldlm_policy_data_t *p2_policy)
923 {
924         struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
925         struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
926         int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
927         ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
928         int rc, flags;
929         ENTRY;
930
931         LASSERT(p1_res_id != NULL && p2_res_id != NULL);
932
933         CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n", res_id[0]->name[0],
934                res_id[1]->name[0]);
935
936         if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
937                 handles[1] = p1_lockh;
938                 handles[0] = p2_lockh;
939                 res_id[1] = p1_res_id;
940                 res_id[0] = p2_res_id;
941                 lock_modes[1] = p1_lock_mode;
942                 lock_modes[0] = p2_lock_mode;
943                 policies[1] = p1_policy;
944                 policies[0] = p2_policy;
945         }
946
947         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
948                res_id[0]->name[0], res_id[1]->name[0]);
949
950         flags = LDLM_FL_LOCAL_ONLY;
951         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
952                               LDLM_IBITS, policies[0], lock_modes[0], &flags,
953                               mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
954                               NULL, 0, NULL, handles[0]);
955         if (rc != ELDLM_OK)
956                 RETURN(-EIO);
957         ldlm_lock_dump_handle(D_OTHER, handles[0]);
958
959         if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
960             (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
961                 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
962                 ldlm_lock_addref(handles[1], lock_modes[1]);
963         } else if (res_id[1]->name[0] != 0) {
964                 flags = LDLM_FL_LOCAL_ONLY;
965                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
966                                       *res_id[1], LDLM_IBITS, policies[1],
967                                       lock_modes[1], &flags, mds_blocking_ast,
968                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
969                                       NULL, handles[1]);
970                 if (rc != ELDLM_OK) {
971                         ldlm_lock_decref(handles[0], lock_modes[0]);
972                         RETURN(-EIO);
973                 }
974                 ldlm_lock_dump_handle(D_OTHER, handles[1]);
975         }
976
977         RETURN(0);
978 }
979
980 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
981                            struct lustre_handle *p1_lockh, int p1_lock_mode,
982                            ldlm_policy_data_t *p1_policy,
983                            struct ldlm_res_id *p2_res_id,
984                            struct lustre_handle *p2_lockh, int p2_lock_mode,
985                            ldlm_policy_data_t *p2_policy,
986                            struct ldlm_res_id *c1_res_id,
987                            struct lustre_handle *c1_lockh, int c1_lock_mode,
988                            ldlm_policy_data_t *c1_policy,
989                            struct ldlm_res_id *c2_res_id,
990                            struct lustre_handle *c2_lockh, int c2_lock_mode,
991                            ldlm_policy_data_t *c2_policy)
992 {
993         struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
994                                           c1_res_id, c2_res_id };
995         struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
996                                                  c1_lockh, c2_lockh };
997         int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
998                               c1_lock_mode, c2_lock_mode };
999         ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1000                                             c1_policy, c2_policy};
1001         int rc, i, j, sorted, flags;
1002         ENTRY;
1003
1004         CDEBUG(D_DLMTRACE,
1005                "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1006                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1007                res_id[3]->name[0]);
1008
1009         /* simple insertion sort - we have at most 4 elements */
1010         for (i = 1; i < 4; i++) {
1011                 j = i - 1;
1012                 dlm_handles[4] = dlm_handles[i];
1013                 res_id[4] = res_id[i];
1014                 lock_modes[4] = lock_modes[i];
1015                 policies[4] = policies[i];
1016
1017                 sorted = 0;
1018                 do {
1019                         if (res_gt(res_id[j], res_id[4], policies[j],
1020                                    policies[4])) {
1021                                 dlm_handles[j + 1] = dlm_handles[j];
1022                                 res_id[j + 1] = res_id[j];
1023                                 lock_modes[j + 1] = lock_modes[j];
1024                                 policies[j + 1] = policies[j];
1025                                 j--;
1026                         } else {
1027                                 sorted = 1;
1028                         }
1029                 } while (j >= 0 && !sorted);
1030
1031                 dlm_handles[j + 1] = dlm_handles[4];
1032                 res_id[j + 1] = res_id[4];
1033                 lock_modes[j + 1] = lock_modes[4];
1034                 policies[j + 1] = policies[4];
1035         }
1036
1037         CDEBUG(D_DLMTRACE,
1038                "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1039                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1040                res_id[3]->name[0]);
1041
1042         /* XXX we could send ASTs on all these locks first before blocking? */
1043         for (i = 0; i < 4; i++) {
1044                 flags = 0;
1045                 if (res_id[i]->name[0] == 0)
1046                         break;
1047                 if (i != 0 &&
1048                     !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1049                     (policies[i]->l_inodebits.bits &
1050                      policies[i-1]->l_inodebits.bits) ) {
1051                         memcpy(dlm_handles[i], dlm_handles[i-1],
1052                                sizeof(*(dlm_handles[i])));
1053                         ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1054                 } else {
1055                         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1056                                               *res_id[i], LDLM_IBITS,
1057                                               policies[i],
1058                                               lock_modes[i], &flags,
1059                                               mds_blocking_ast,
1060                                               ldlm_completion_ast, NULL, NULL,
1061                                               NULL, 0, NULL, dlm_handles[i]);
1062                         if (rc != ELDLM_OK)
1063                                 GOTO(out_err, rc = -EIO);
1064                         ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1065                 }
1066         }
1067
1068         RETURN(0);
1069 out_err:
1070         while (i-- > 0)
1071                 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1072
1073         return rc;
1074 }
1075
1076 /* In the unlikely case that the child changed while we were waiting
1077  * on the lock, we need to drop the lock on the old child and either:
1078  * - if the child has a lower resource name, then we have to also
1079  *   drop the parent lock and regain the locks in the right order
1080  * - in the rename case, if the child has a lower resource name than one of
1081  *   the other parent/child resources (maxres) we also need to reget the locks
1082  * - if the child has a higher resource name (this is the common case)
1083  *   we can just get the lock on the new child (still in lock order)
1084  *
1085  * Returns 0 if the child did not change or if it changed but could be locked.
1086  * Returns 1 if the child changed and we need to re-lock (no locks held).
1087  * Returns -ve error with a valid dchild (no locks held). */
1088 static int mds_verify_child(struct obd_device *obd,
1089                             struct ldlm_res_id *parent_res_id,
1090                             struct lustre_handle *parent_lockh,
1091                             struct dentry *dparent, int parent_mode,
1092                             struct ldlm_res_id *child_res_id,
1093                             struct lustre_handle *child_lockh,
1094                             struct dentry **dchildp, int child_mode,
1095                             ldlm_policy_data_t *child_policy,
1096                             const char *name, int namelen,
1097                             struct ldlm_res_id *maxres)
1098 {
1099         struct dentry *vchild, *dchild = *dchildp;
1100         int rc = 0, cleanup_phase = 2; /* parent, child locks */
1101         ENTRY;
1102
1103         vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1104         if (IS_ERR(vchild))
1105                 GOTO(cleanup, rc = PTR_ERR(vchild));
1106
1107         if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1108                 if  (child_res_id->name[0] == vchild->d_inum &&
1109                                 child_res_id->name[1] == vchild->d_generation) {
1110                         if (dchild != NULL)
1111                                 l_dput(dchild);
1112                         *dchildp = vchild;
1113                         RETURN(0);
1114                 }
1115                 goto changed;
1116         }
1117
1118         if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1119                    (vchild->d_inode != NULL &&
1120                     child_res_id->name[0] == vchild->d_inode->i_ino &&
1121                     child_res_id->name[1] == vchild->d_inode->i_generation))) {
1122                 if (dchild != NULL)
1123                         l_dput(dchild);
1124                 *dchildp = vchild;
1125
1126                 RETURN(0);
1127         }
1128
1129 changed:
1130         CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1131                vchild->d_inode, dchild ? dchild->d_inode : 0,
1132                vchild->d_inode ? vchild->d_inode->i_ino : 0,
1133                child_res_id->name[0]);
1134         if (child_res_id->name[0] != 0)
1135                 ldlm_lock_decref(child_lockh, child_mode);
1136         if (dchild)
1137                 l_dput(dchild);
1138
1139         cleanup_phase = 1; /* parent lock only */
1140         *dchildp = dchild = vchild;
1141
1142         if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1143                 int flags = 0;
1144                 if (dchild->d_inode) {
1145                         child_res_id->name[0] = dchild->d_inode->i_ino;
1146                         child_res_id->name[1] = dchild->d_inode->i_generation;
1147                 } else {
1148                         child_res_id->name[0] = dchild->d_inum;
1149                         child_res_id->name[1] = dchild->d_generation;
1150                 }
1151
1152                 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1153                     res_gt(maxres, child_res_id, NULL, NULL)) {
1154                         CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1155                                child_res_id->name[0], parent_res_id->name[0],
1156                                maxres->name[0]);
1157                         GOTO(cleanup, rc = 1);
1158                 }
1159
1160                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1161                                       *child_res_id, LDLM_IBITS, child_policy,
1162                                       child_mode, &flags, mds_blocking_ast,
1163                                       ldlm_completion_ast, NULL, NULL, NULL, 0,
1164                                       NULL, child_lockh);
1165                 if (rc != ELDLM_OK)
1166                         GOTO(cleanup, rc = -EIO);
1167
1168         } else {
1169                 memset(child_res_id, 0, sizeof(*child_res_id));
1170         }
1171
1172         EXIT;
1173 cleanup:
1174         if (rc) {
1175                 switch(cleanup_phase) {
1176                 case 2:
1177                         if (child_res_id->name[0] != 0)
1178                                 ldlm_lock_decref(child_lockh, child_mode);
1179                 case 1:
1180                         ldlm_lock_decref(parent_lockh, parent_mode);
1181                 }
1182         }
1183         return rc;
1184 }
1185
1186 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1187                                 struct ll_fid *fid,
1188                                 struct lustre_handle *parent_lockh,
1189                                 struct dentry **dparentp, int parent_mode,
1190                                 __u64 parent_lockpart, int *update_mode,
1191                                 char *name, int namelen,
1192                                 struct lustre_handle *child_lockh,
1193                                 struct dentry **dchildp, int child_mode,
1194                                 __u64 child_lockpart)
1195 {
1196         struct ldlm_res_id child_res_id = { .name = {0} };
1197         struct ldlm_res_id parent_res_id = { .name = {0} };
1198         ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1199         ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1200         struct inode *inode;
1201         int rc = 0, cleanup_phase = 0;
1202         ENTRY;
1203
1204         /* Step 1: Lookup parent */
1205         *dparentp = mds_fid2dentry(mds, fid, NULL);
1206         if (IS_ERR(*dparentp)) {
1207                 rc = PTR_ERR(*dparentp);
1208                 *dparentp = NULL;
1209                 RETURN(rc);
1210         }
1211
1212         CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1213                (*dparentp)->d_inode->i_ino, name);
1214
1215         parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
1216         parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
1217 #ifdef S_PDIROPS
1218         parent_lockh[1].cookie = 0;
1219         if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1220                 struct ldlm_res_id res_id = { .name = {0} };
1221                 ldlm_policy_data_t policy;
1222                 int flags = 0;
1223                 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1224                 res_id.name[0] = (*dparentp)->d_inode->i_ino;
1225                 res_id.name[1] = (*dparentp)->d_inode->i_generation;
1226                 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1227                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
1228                                       LDLM_IBITS, &policy, *update_mode, &flags,
1229                                       mds_blocking_ast, ldlm_completion_ast,
1230                                       NULL, NULL, NULL, 0, NULL, parent_lockh+1);
1231                 if (rc != ELDLM_OK)
1232                         RETURN(-ENOLCK);
1233
1234                 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1235                 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
1236                        (*dparentp)->d_inode->i_ino, 
1237                        (*dparentp)->d_inode->i_generation,
1238                        parent_res_id.name[2]);
1239         }
1240 #endif
1241
1242         cleanup_phase = 1; /* parent dentry */
1243
1244         /* Step 2: Lookup child (without DLM lock, to get resource name) */
1245         *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1246         if (IS_ERR(*dchildp)) {
1247                 rc = PTR_ERR(*dchildp);
1248                 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1249                 GOTO(cleanup, rc);
1250         }
1251
1252         if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1253                 /* inode lives on another MDS: return * mds/ino/gen
1254                  * and LOOKUP lock. drop possible UPDATE lock! */
1255                 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1256                 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1257                 child_res_id.name[0] = (*dchildp)->d_inum;
1258                 child_res_id.name[1] = (*dchildp)->d_generation;
1259                 goto retry_locks;
1260         }
1261
1262         inode = (*dchildp)->d_inode;
1263         if (inode != NULL)
1264                 inode = igrab(inode);
1265         if (inode == NULL)
1266                 goto retry_locks;
1267
1268         child_res_id.name[0] = inode->i_ino;
1269         child_res_id.name[1] = inode->i_generation;
1270
1271         iput(inode);
1272
1273 retry_locks:
1274         cleanup_phase = 2; /* child dentry */
1275
1276         /* Step 3: Lock parent and child in resource order.  If child doesn't
1277          *         exist, we still have to lock the parent and re-lookup. */
1278         rc = enqueue_ordered_locks(obd,&parent_res_id,parent_lockh,parent_mode,
1279                                    &parent_policy, &child_res_id, child_lockh,
1280                                    child_mode, &child_policy);
1281         if (rc)
1282                 GOTO(cleanup, rc);
1283
1284         if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1285                 cleanup_phase = 4; /* child lock */
1286         else
1287                 cleanup_phase = 3; /* parent lock */
1288
1289         /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1290         rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1291                               parent_mode, &child_res_id, child_lockh, 
1292                               dchildp, child_mode, &child_policy,
1293                               name, namelen, &parent_res_id);
1294         if (rc > 0)
1295                 goto retry_locks;
1296         if (rc < 0) {
1297                 cleanup_phase = 3;
1298                 GOTO(cleanup, rc);
1299         }
1300
1301 cleanup:
1302         if (rc) {
1303                 switch (cleanup_phase) {
1304                 case 4:
1305                         ldlm_lock_decref(child_lockh, child_mode);
1306                 case 3:
1307                         ldlm_lock_decref(parent_lockh, parent_mode);
1308                 case 2:
1309                         l_dput(*dchildp);
1310                 case 1:
1311 #ifdef S_PDIROPS
1312                         if (parent_lockh[1].cookie)
1313                                 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1314 #endif
1315                         l_dput(*dparentp);
1316                 default: ;
1317                 }
1318         }
1319         return rc;
1320 }
1321
1322 void mds_reconstruct_generic(struct ptlrpc_request *req)
1323 {
1324         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1325
1326         mds_req_from_mcd(req, med->med_mcd);
1327 }
1328
1329 int mds_create_local_dentry(struct mds_update_record *rec,
1330                            struct obd_device *obd)
1331 {
1332         struct mds_obd *mds = &obd->u.mds;
1333         struct inode *fids_dir = mds->mds_fids_dir->d_inode;
1334         int fidlen = 0, rc, cleanup_phase = 0;
1335         struct dentry *new_child = NULL;
1336         char *fidname = rec->ur_name;
1337         struct dentry *child = NULL;
1338         struct lustre_handle lockh[2] = {{0}, {0}};
1339         void *handle;
1340         ENTRY;
1341
1342         down(&fids_dir->i_sem);
1343         fidlen = ll_fid2str(fidname, rec->ur_fid1->id, rec->ur_fid1->generation);
1344         CDEBUG(D_OTHER, "look for local dentry '%s' for %u/%u\n",
1345                         fidname, (unsigned) rec->ur_fid1->id,
1346                         (unsigned) rec->ur_fid1->generation);
1347
1348         new_child = lookup_one_len(fidname, mds->mds_fids_dir, fidlen);
1349         up(&fids_dir->i_sem);
1350         if (IS_ERR(new_child)) {
1351                 CERROR("can't lookup %s: %d\n", fidname,
1352                                 (int) PTR_ERR(new_child));
1353                 GOTO(cleanup, rc = PTR_ERR(new_child));
1354         }
1355         cleanup_phase = 1;
1356
1357         if (new_child->d_inode != NULL) {
1358                 /* nice. we've already have local dentry! */
1359                 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n", 
1360                        (unsigned) new_child->d_inode->i_ino,
1361                        (unsigned) new_child->d_inode->i_generation);
1362                 rec->ur_fid1->id = fids_dir->i_ino;
1363                 rec->ur_fid1->generation = fids_dir->i_generation;
1364                 rec->ur_namelen = fidlen + 1;
1365                 GOTO(cleanup, rc = 0);
1366         }
1367
1368         /* new, local dentry will be added soon. we need no aliases here */
1369         d_drop(new_child);
1370
1371         child = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_EX,
1372                                       lockh, NULL, NULL, 0,
1373                                       MDS_INODELOCK_UPDATE);
1374         if (IS_ERR(child)) {
1375                 CERROR("can't get victim\n");
1376                 GOTO(cleanup, rc = PTR_ERR(child));
1377         }
1378         cleanup_phase = 2;
1379
1380         handle = fsfilt_start(obd, fids_dir, FSFILT_OP_LINK, NULL);
1381         if (IS_ERR(handle))
1382                 GOTO(cleanup, rc = PTR_ERR(handle));
1383
1384         rc = fsfilt_add_dir_entry(obd, mds->mds_fids_dir, fidname, fidlen,
1385                                   rec->ur_fid1->id, rec->ur_fid1->generation,
1386                                   mds->mds_num);
1387         if (rc)
1388                 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1389                        (unsigned long) child->d_inode->i_ino,
1390                        (unsigned long) child->d_inode->i_generation, rc);
1391         else {
1392                 if (S_ISDIR(child->d_inode->i_mode)) {
1393                         fids_dir->i_nlink++;
1394                         mark_inode_dirty(fids_dir);
1395                 }
1396                 mark_inode_dirty(child->d_inode);
1397         }
1398         fsfilt_commit(obd, mds->mds_sb, fids_dir, handle, 0);
1399
1400         rec->ur_fid1->id = fids_dir->i_ino;
1401         rec->ur_fid1->generation = fids_dir->i_generation;
1402         rec->ur_namelen = fidlen + 1;
1403
1404 cleanup:
1405         switch(cleanup_phase) {
1406                 case 2:
1407                         ldlm_lock_decref(lockh, LCK_EX);
1408                         dput(child);
1409                 case 1:
1410                         dput(new_child);
1411                 case 0:
1412                        break; 
1413         }
1414         RETURN(rc);
1415 }
1416
1417 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1418                                         struct ptlrpc_request *slave)
1419 {
1420         void *cookie, *cookie2;
1421         struct mds_body *body2;
1422         struct mds_body *body;
1423         void *ea, *ea2;
1424         ENTRY;
1425
1426         body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1427         LASSERT(body != NULL);
1428
1429         body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1430         LASSERT(body2 != NULL);
1431
1432         if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER))) {
1433                 RETURN(0);
1434         }
1435
1436         memcpy(body2, body, sizeof(*body));
1437         body2->valid &= ~OBD_MD_FLCOOKIE;
1438
1439         if (!(body->valid & OBD_MD_FLEASIZE) &&
1440             !(body->valid & OBD_MD_FLDIREA))
1441                 RETURN(0);
1442
1443         if (body->eadatasize == 0) {
1444                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1445                 RETURN(0);
1446         }
1447
1448         LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1449         
1450         ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1451         LASSERT(ea != NULL);
1452         
1453         ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1454         LASSERT(ea2 != NULL);
1455
1456         memcpy(ea2, ea, body->eadatasize);
1457
1458         if (body->valid & OBD_MD_FLCOOKIE) {
1459                 LASSERT(master->rq_repmsg->buflens[2] >=
1460                                 slave->rq_repmsg->buflens[2]);
1461                 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1462                                 slave->rq_repmsg->buflens[2]);
1463                 LASSERT(cookie != NULL);
1464
1465                 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1466                                 master->rq_repmsg->buflens[2]);
1467                 LASSERT(cookie2 != NULL);
1468                 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1469                 body2->valid |= OBD_MD_FLCOOKIE;
1470         }
1471         RETURN(0);
1472 }
1473
1474 static int mds_reint_unlink_remote(struct mds_update_record *rec, int offset,
1475                                    struct ptlrpc_request *req,
1476                                    struct lustre_handle *parent_lockh,
1477                                    int update_mode,
1478                                    struct dentry *dparent,
1479                                    struct lustre_handle *child_lockh,
1480                                    struct dentry *dchild)
1481 {
1482         struct mds_obd *mds = mds_req2mds(req);
1483         struct mdc_op_data op_data;
1484         int rc = 0, cleanup_phase = 0;
1485         struct ptlrpc_request *request = NULL;
1486         ENTRY;
1487
1488         LASSERT(offset == 0 || offset == 2);
1489
1490         DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode %u/%u/%u)",
1491                   rec->ur_namelen - 1, rec->ur_name, (unsigned)dchild->d_mdsnum,
1492                   (unsigned) dchild->d_inum, (unsigned) dchild->d_generation);
1493
1494         /* time to drop i_nlink on remote MDS */ 
1495         op_data.fid1.mds = dchild->d_mdsnum;
1496         op_data.fid1.id = dchild->d_inum;
1497         op_data.fid1.generation = dchild->d_generation;
1498         op_data.create_mode = rec->ur_mode;
1499         op_data.namelen = 0;
1500         op_data.name = NULL;
1501         rc = md_unlink(mds->mds_lmv_exp, &op_data, &request);
1502         cleanup_phase = 2;
1503         if (request) {
1504                 mds_copy_unlink_reply(req, request);
1505                 ptlrpc_req_finished(request);
1506         }
1507         if (rc == 0)
1508                 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1509         req->rq_status = rc;
1510
1511 #ifdef S_PDIROPS
1512         if (parent_lockh[1].cookie != 0)
1513                 ldlm_lock_decref(parent_lockh + 1, update_mode);
1514 #endif
1515         ldlm_lock_decref(child_lockh, LCK_EX);
1516         if (rc)
1517                 ldlm_lock_decref(parent_lockh, LCK_PW);
1518         else
1519                 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1520         l_dput(dchild);
1521         l_dput(dparent);
1522
1523         return 0;
1524 }
1525
1526 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1527                             struct ptlrpc_request *req,
1528                             struct lustre_handle *lh)
1529 {
1530         struct dentry *dparent, *dchild;
1531         struct mds_obd *mds = mds_req2mds(req);
1532         struct obd_device *obd = req->rq_export->exp_obd;
1533         struct mds_body *body = NULL;
1534         struct inode *child_inode;
1535         struct lustre_handle parent_lockh[2] = {{0}, {0}}; 
1536         struct lustre_handle child_lockh = {0}, child_reuse_lockh = {0};
1537         char fidname[LL_FID_NAMELEN];
1538         void *handle = NULL;
1539         int rc = 0, log_unlink = 0, cleanup_phase = 0;
1540         int unlink_by_fid = 0;
1541         int update_mode;
1542         ENTRY;
1543
1544         LASSERT(offset == 0 || offset == 2);
1545
1546         DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1547                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1548
1549         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1550
1551         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1552                 GOTO(cleanup, rc = -ENOENT);
1553
1554         if (rec->ur_namelen == 1) {
1555                 /* this is request to drop i_nlink on local inode */
1556                 unlink_by_fid = 1;
1557                 rec->ur_name = fidname;
1558                 rc = mds_create_local_dentry(rec, obd);
1559                 LASSERT(rc == 0);
1560         }
1561         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1562                                          parent_lockh, &dparent, LCK_PW,
1563                                          MDS_INODELOCK_UPDATE, &update_mode,
1564                                          rec->ur_name, rec->ur_namelen,
1565                                          &child_lockh, &dchild, LCK_EX,
1566                                          MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE);
1567         if (rc)
1568                 GOTO(cleanup, rc);
1569
1570         if (dchild->d_flags & DCACHE_CROSS_REF) {
1571                 /* we should have parent lock only here */
1572                 LASSERT(unlink_by_fid == 0);
1573                 LASSERT(dchild->d_mdsnum != mds->mds_num);
1574                 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
1575                                         update_mode, dparent, &child_lockh, dchild);
1576                 RETURN(0);
1577         }
1578
1579         cleanup_phase = 1; /* dchild, dparent, locks */
1580
1581         dget(dchild);
1582         child_inode = dchild->d_inode;
1583         if (child_inode == NULL) {
1584                 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1585                        dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
1586                 GOTO(cleanup, rc = -ENOENT);
1587         }
1588
1589         cleanup_phase = 2; /* dchild has a lock */
1590
1591         /* Step 4: Get a lock on the ino to sync with creation WRT inode
1592          * reuse (see bug 2029). */
1593         rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1594         if (rc != ELDLM_OK)
1595                 GOTO(cleanup, rc);
1596
1597         cleanup_phase = 3; /* child inum lock */
1598
1599         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1600
1601         /* ldlm_reply in buf[0] if called via intent */
1602         if (offset)
1603                 offset = 1;
1604
1605         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
1606         LASSERT(body != NULL);
1607
1608         /* If this is the last reference to this inode, get the OBD EA
1609          * data first so the client can destroy OST objects.
1610          * we only do the object removal if no open files remain.
1611          * Nobody can get at this name anymore because of the locks so
1612          * we make decisions here as to whether to remove the inode */
1613         if (S_ISREG(child_inode->i_mode) && child_inode->i_nlink == 1 &&
1614             mds_open_orphan_count(child_inode) == 0) {
1615                 mds_pack_inode2fid(obd, &body->fid1, child_inode);
1616                 mds_pack_inode2body(obd, body, child_inode);
1617                 mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1618                             child_inode, 1);
1619                 if (!(body->valid & OBD_MD_FLEASIZE)) {
1620                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1621                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
1622                 } else {
1623                         log_unlink = 1;
1624                 }
1625         }
1626
1627         /* We have to do these checks ourselves, in case we are making an
1628          * orphan.  The client tells us whether rmdir() or unlink() was called,
1629          * so we need to return appropriate errors (bug 72).
1630          *
1631          * We don't have to check permissions, because vfs_rename (called from
1632          * mds_open_unlink_rename) also calls may_delete. */
1633         if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1634                 if (!S_ISDIR(child_inode->i_mode))
1635                         GOTO(cleanup, rc = -ENOTDIR);
1636         } else {
1637                 if (S_ISDIR(child_inode->i_mode))
1638                         GOTO(cleanup, rc = -EISDIR);
1639         }
1640
1641         /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1642         switch (child_inode->i_mode & S_IFMT) {
1643         case S_IFDIR:
1644                 /* Drop any lingering child directories before we start our
1645                  * transaction, to avoid doing multiple inode dirty/delete
1646                  * in our compound transaction (bug 1321). */
1647                 shrink_dcache_parent(dchild);
1648                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1649                                       NULL);
1650                 if (IS_ERR(handle))
1651                         GOTO(cleanup, rc = PTR_ERR(handle));
1652                 cleanup_phase = 4; /* transaction */
1653                 rc = vfs_rmdir(dparent->d_inode, dchild);
1654                 break;
1655         case S_IFREG: {
1656 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
1657                 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
1658                                                         offset + 1, 0);
1659                 handle = fsfilt_start_log(obd, dparent->d_inode,
1660                                           FSFILT_OP_UNLINK, NULL,
1661                                           le32_to_cpu(lmm->lmm_stripe_count));
1662                 if (IS_ERR(handle))
1663                         GOTO(cleanup, rc = PTR_ERR(handle));
1664
1665                 cleanup_phase = 4; /* transaction */
1666                 rc = vfs_unlink(dparent->d_inode, dchild);
1667
1668                 if (!rc && log_unlink)
1669                         if (mds_log_op_unlink(obd, child_inode,
1670                                 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
1671                                 req->rq_repmsg->buflens[offset + 1],
1672                                 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
1673                                 req->rq_repmsg->buflens[offset + 2]) > 0)
1674                                 body->valid |= OBD_MD_FLCOOKIE;
1675                 break;
1676         }
1677         case S_IFLNK:
1678         case S_IFCHR:
1679         case S_IFBLK:
1680         case S_IFIFO:
1681         case S_IFSOCK:
1682                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1683                                       NULL);
1684                 if (IS_ERR(handle))
1685                         GOTO(cleanup, rc = PTR_ERR(handle));
1686                 cleanup_phase = 4; /* transaction */
1687                 rc = vfs_unlink(dparent->d_inode, dchild);
1688                 break;
1689         default:
1690                 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1691                        rec->ur_name);
1692                 LBUG();
1693                 GOTO(cleanup, rc = -EINVAL);
1694         }
1695
1696  cleanup:
1697         if (rc == 0) {
1698                 struct iattr iattr;
1699                 int err;
1700
1701                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1702                 LTIME_S(iattr.ia_mtime) = LTIME_S(rec->ur_time);
1703                 LTIME_S(iattr.ia_ctime) = LTIME_S(rec->ur_time);
1704
1705                 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1706                 if (err)
1707                         CERROR("error on parent setattr: rc = %d\n", err);
1708         }
1709
1710         switch(cleanup_phase) {
1711         case 4:
1712                 LASSERT(dchild != NULL && dchild->d_inode != NULL);
1713                 LASSERT(atomic_read(&dchild->d_inode->i_count) > 0);
1714                 if (rc == 0 && dchild->d_inode->i_nlink == 0 &&
1715                                 mds_open_orphan_count(dchild->d_inode) > 0) {
1716                         /* filesystem is really going to destroy an inode
1717                          * we have to delay this till inode is opened -bzzz */
1718                         mds_open_unlink_rename(rec, obd, dparent, dchild, NULL);
1719                 }
1720                 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1721                                         rc, 0);
1722                 if (!rc)
1723                         (void)obd_set_info(mds->mds_osc_exp, strlen("unlinked"),
1724                                            "unlinked", 0, NULL);
1725         case 3: /* child ino-reuse lock */
1726                 if (rc && body != NULL) {
1727                         // Don't unlink the OST objects if the MDS unlink failed
1728                         body->valid = 0;
1729                 }
1730                 if (rc)
1731                         ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1732                 else
1733                         ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
1734         case 2: /* child lock */
1735                 ldlm_lock_decref(&child_lockh, LCK_EX);
1736         case 1: /* child and parent dentry, parent lock */
1737 #ifdef S_PDIROPS
1738                 if (parent_lockh[1].cookie != 0)
1739                         ldlm_lock_decref(parent_lockh + 1, update_mode);
1740 #endif
1741                 if (rc)
1742                         ldlm_lock_decref(parent_lockh, LCK_PW);
1743                 else
1744                         ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1745                 l_dput(dchild);
1746                 l_dput(dchild);
1747                 l_dput(dparent);
1748         case 0:
1749                 break;
1750         default:
1751                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1752                 LBUG();
1753         }
1754         req->rq_status = rc;
1755         return 0;
1756 }
1757
1758 /*
1759  * to service requests from remote MDS to increment i_nlink
1760  */
1761 static int mds_reint_link_acquire(struct mds_update_record *rec,
1762                                  int offset, struct ptlrpc_request *req,
1763                                  struct lustre_handle *lh)
1764 {
1765         struct obd_device *obd = req->rq_export->exp_obd;
1766         struct ldlm_res_id src_res_id = { .name = {0} };
1767         struct lustre_handle *handle = NULL, src_lockh = {0};
1768         struct mds_obd *mds = mds_req2mds(req);
1769         int rc = 0, cleanup_phase = 0;
1770         struct dentry *de_src = NULL;
1771         ldlm_policy_data_t policy;
1772         int flags = 0;
1773         ENTRY;
1774
1775         DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks %u/%u/%u\n",
1776                   obd->obd_name, (unsigned) rec->ur_fid1->mds,
1777                   (unsigned) rec->ur_fid1->id,
1778                   (unsigned) rec->ur_fid1->generation);
1779
1780         /* Step 1: Lookup the source inode and target directory by FID */
1781         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1782         if (IS_ERR(de_src))
1783                 GOTO(cleanup, rc = PTR_ERR(de_src));
1784         cleanup_phase = 1; /* source dentry */
1785
1786         src_res_id.name[0] = de_src->d_inode->i_ino;
1787         src_res_id.name[1] = de_src->d_inode->i_generation;
1788         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1789
1790         rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1791                         src_res_id, LDLM_IBITS, &policy,
1792                         LCK_EX, &flags, mds_blocking_ast,
1793                         ldlm_completion_ast, NULL, NULL,
1794                         NULL, 0, NULL, &src_lockh);
1795         if (rc != ELDLM_OK)
1796                 GOTO(cleanup, rc = -ENOLCK);
1797         cleanup_phase = 2; /* lock */
1798
1799         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1800
1801         handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
1802         if (IS_ERR(handle)) {
1803                 rc = PTR_ERR(handle);
1804                 GOTO(cleanup, rc);
1805         }
1806         de_src->d_inode->i_nlink++;
1807         mark_inode_dirty(de_src->d_inode);
1808
1809 cleanup:
1810         rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
1811                                         handle, req, rc, 0);
1812         EXIT;
1813         switch (cleanup_phase) {
1814                 case 2:
1815                         if (rc)
1816                                 ldlm_lock_decref(&src_lockh, LCK_EX);
1817                         else
1818                                 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
1819                 case 1:
1820                         l_dput(de_src);
1821                 case 0:
1822                         break;
1823                 default:
1824                         CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1825                         LBUG();
1826         }
1827         req->rq_status = rc;
1828         return 0;
1829 }
1830
1831 /*
1832  * request to link to foreign inode:
1833  *  - acquire i_nlinks on this inode
1834  *  - add dentry
1835  */
1836 static int mds_reint_link_to_remote(struct mds_update_record *rec,
1837                                     int offset, struct ptlrpc_request *req,
1838                                     struct lustre_handle *lh)
1839 {
1840         struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
1841         struct obd_device *obd = req->rq_export->exp_obd;
1842         struct dentry *de_tgt_dir = NULL;
1843         struct mds_obd *mds = mds_req2mds(req);
1844         int rc = 0, cleanup_phase = 0;
1845         struct mdc_op_data op_data;
1846         struct ptlrpc_request *request = NULL;
1847         int update_mode;
1848         ENTRY;
1849
1850 #define fmt     "%s: request to link %u/%u/%u:%*s to foreign inode %u/%u/%u\n"
1851         DEBUG_REQ(D_INODE, req, fmt, obd->obd_name,
1852                   (unsigned) rec->ur_fid2->mds,
1853                   (unsigned) rec->ur_fid2->id,
1854                   (unsigned) rec->ur_fid2->generation,
1855                   rec->ur_namelen - 1, rec->ur_name,
1856                   (unsigned) rec->ur_fid1->mds,
1857                   (unsigned) rec->ur_fid1->id,
1858                   (unsigned)rec->ur_fid1->generation);
1859
1860         de_tgt_dir = mds_fid2locked_dentry(obd, rec->ur_fid2, NULL, LCK_EX,
1861                                            tgt_dir_lockh, &update_mode,
1862                                            rec->ur_name, rec->ur_namelen - 1,
1863                                            MDS_INODELOCK_UPDATE);
1864         if (IS_ERR(de_tgt_dir))
1865                 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
1866         cleanup_phase = 1;
1867
1868         op_data.fid1 = *(rec->ur_fid1);
1869         op_data.namelen = 0;
1870         op_data.name = NULL;
1871         rc = md_link(mds->mds_lmv_exp, &op_data, &request);
1872         if (rc)
1873                 GOTO(cleanup, rc);
1874
1875         cleanup_phase = 2;
1876         if (request)
1877                 ptlrpc_req_finished(request);
1878
1879         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
1880
1881         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1882         if (IS_ERR(handle)) {
1883                 rc = PTR_ERR(handle);
1884                 GOTO(cleanup, rc);
1885         }
1886         
1887         rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
1888                                   rec->ur_namelen - 1, rec->ur_fid1->id,
1889                                   rec->ur_fid1->generation, rec->ur_fid1->mds);
1890         cleanup_phase = 3;
1891
1892 cleanup:
1893         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
1894                                 handle, req, rc, 0);
1895         EXIT;
1896
1897         switch (cleanup_phase) {
1898                 case 3:
1899                         if (rc) {
1900                                 /* FIXME: drop i_nlink on remote inode here */
1901                                 CERROR("MUST drop drop i_nlink here\n");
1902                         }
1903                 case 2:
1904                 case 1:
1905                         if (rc) {
1906                                 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
1907 #ifdef S_PDIROPS
1908                                 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
1909 #endif
1910                         } else {
1911                                 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
1912 #ifdef S_PDIROPS
1913                                 ptlrpc_save_lock(req, tgt_dir_lockh+1, update_mode);
1914 #endif
1915                         }
1916                         l_dput(de_tgt_dir);
1917                         break;
1918                 default:
1919                         CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1920                         LBUG();
1921         }
1922         req->rq_status = rc;
1923         return 0;
1924 }
1925
1926 static int mds_reint_link(struct mds_update_record *rec, int offset,
1927                           struct ptlrpc_request *req,
1928                           struct lustre_handle *lh)
1929 {
1930         struct obd_device *obd = req->rq_export->exp_obd;
1931         struct dentry *de_src = NULL;
1932         struct dentry *de_tgt_dir = NULL;
1933         struct dentry *dchild = NULL;
1934         struct mds_obd *mds = mds_req2mds(req);
1935         struct lustre_handle *handle = NULL;
1936         struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
1937         struct ldlm_res_id src_res_id = { .name = {0} };
1938         struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
1939         ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
1940         ldlm_policy_data_t tgt_dir_policy =
1941                                        {.l_inodebits = {MDS_INODELOCK_UPDATE}};
1942         int rc = 0, cleanup_phase = 0;
1943         int update_mode = 0;
1944         ENTRY;
1945
1946         LASSERT(offset == 0);
1947
1948         DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
1949                   rec->ur_fid1->id, rec->ur_fid1->generation,
1950                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
1951
1952         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1953         
1954 //      memset(tgt_dir_lockh, 0, 2*sizeof(tgt_dir_lockh[0]));
1955         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1956                 GOTO(cleanup, rc = -ENOENT);
1957
1958         if (rec->ur_fid1->mds != mds->mds_num) {
1959                 rc = mds_reint_link_to_remote(rec, offset, req, lh);
1960                 RETURN(rc);
1961         }
1962         
1963         if (rec->ur_namelen == 1) {
1964                 rc = mds_reint_link_acquire(rec, offset, req, lh);
1965                 RETURN(rc);
1966         }
1967
1968         /* Step 1: Lookup the source inode and target directory by FID */
1969         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1970         if (IS_ERR(de_src))
1971                 GOTO(cleanup, rc = PTR_ERR(de_src));
1972
1973         cleanup_phase = 1; /* source dentry */
1974
1975         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
1976         if (IS_ERR(de_tgt_dir)) {
1977                 rc = PTR_ERR(de_tgt_dir);
1978                 de_tgt_dir = NULL;
1979                 GOTO(cleanup, rc);
1980         }
1981
1982         cleanup_phase = 2; /* target directory dentry */
1983
1984         CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
1985                de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
1986                de_src->d_inode->i_ino);
1987
1988         /* Step 2: Take the two locks */
1989         src_res_id.name[0] = de_src->d_inode->i_ino;
1990         src_res_id.name[1] = de_src->d_inode->i_generation;
1991         tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
1992         tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
1993 #ifdef S_PDIROPS
1994         if (IS_PDIROPS(de_tgt_dir->d_inode)) {
1995                 int flags = 0;
1996                 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
1997                 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1998                                       tgt_dir_res_id, LDLM_IBITS, &src_policy,
1999                                       update_mode, &flags, mds_blocking_ast,
2000                                       ldlm_completion_ast, NULL, NULL,
2001                                       NULL, 0, NULL, tgt_dir_lockh + 1);
2002                 if (rc != ELDLM_OK)
2003                         GOTO(cleanup, rc = -ENOLCK);
2004
2005                 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
2006                                                         rec->ur_namelen - 1);
2007                 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
2008                        de_tgt_dir->d_inode->i_ino,
2009                        de_tgt_dir->d_inode->i_generation,
2010                        tgt_dir_res_id.name[2]);
2011         }
2012 #endif
2013         rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2014                                    &src_policy,
2015                                    &tgt_dir_res_id, tgt_dir_lockh, LCK_EX,
2016                                    &tgt_dir_policy);
2017         if (rc)
2018                 GOTO(cleanup, rc);
2019
2020         cleanup_phase = 3; /* locks */
2021
2022         /* Step 3: Lookup the child */
2023         dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
2024         if (IS_ERR(dchild)) {
2025                 rc = PTR_ERR(dchild);
2026                 if (rc != -EPERM && rc != -EACCES)
2027                         CERROR("child lookup error %d\n", rc);
2028                 GOTO(cleanup, rc);
2029         }
2030
2031         cleanup_phase = 4; /* child dentry */
2032
2033         if (dchild->d_inode) {
2034                 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2035                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
2036                 rc = -EEXIST;
2037                 GOTO(cleanup, rc);
2038         }
2039
2040         /* Step 4: Do it. */
2041         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2042
2043         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2044         if (IS_ERR(handle)) {
2045                 rc = PTR_ERR(handle);
2046                 GOTO(cleanup, rc);
2047         }
2048
2049         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2050         if (rc && rc != -EPERM && rc != -EACCES)
2051                 CERROR("vfs_link error %d\n", rc);
2052 cleanup:
2053         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2054                                 handle, req, rc, 0);
2055         EXIT;
2056
2057         switch (cleanup_phase) {
2058         case 4: /* child dentry */
2059                 l_dput(dchild);
2060         case 3: /* locks */
2061                 if (rc) {
2062                         ldlm_lock_decref(&src_lockh, LCK_EX);
2063                         ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2064                 } else {
2065                         ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2066                         ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2067                 }
2068         case 2: /* target dentry */
2069 #ifdef S_PDIROPS
2070                 if (tgt_dir_lockh[1].cookie && update_mode)
2071                         ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2072 #endif
2073                 if (de_tgt_dir)
2074                         l_dput(de_tgt_dir);
2075         case 1: /* source dentry */
2076                 l_dput(de_src);
2077         case 0:
2078                 break;
2079         default:
2080                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2081                 LBUG();
2082         }
2083         req->rq_status = rc;
2084         return 0;
2085 }
2086
2087 /*
2088  * add a hard link in the PENDING directory, only used by rename()
2089  */
2090 static int mds_add_link_orphan(struct mds_update_record *rec,
2091                                struct obd_device *obd,
2092                                struct dentry *dentry)
2093 {
2094         struct mds_obd *mds = &obd->u.mds;
2095         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
2096         struct dentry *pending_child;
2097         char fidname[LL_FID_NAMELEN];
2098         int fidlen = 0, rc;
2099         ENTRY;
2100
2101         LASSERT(dentry->d_inode);
2102         LASSERT(!mds_inode_is_orphan(dentry->d_inode));
2103
2104         down(&pending_dir->i_sem);
2105         fidlen = ll_fid2str(fidname, dentry->d_inode->i_ino,
2106                             dentry->d_inode->i_generation);
2107
2108         CDEBUG(D_ERROR, "pending destroy of %dx open file %s = %s\n",
2109                mds_open_orphan_count(dentry->d_inode),
2110                rec->ur_name, fidname);
2111
2112         pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
2113         if (IS_ERR(pending_child))
2114                 GOTO(out_lock, rc = PTR_ERR(pending_child));
2115
2116         if (pending_child->d_inode != NULL) {
2117                 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
2118                 LASSERT(pending_child->d_inode == dentry->d_inode);
2119                 GOTO(out_dput, rc = 0);
2120         }
2121
2122         lock_kernel();
2123         rc = vfs_link(dentry, pending_dir, pending_child);
2124         unlock_kernel();
2125         if (rc)
2126                 CERROR("error addlink orphan %s to PENDING: rc = %d\n",
2127                        rec->ur_name, rc);
2128         else
2129                 mds_inode_set_orphan(dentry->d_inode);
2130 out_dput:
2131         l_dput(pending_child);
2132 out_lock:
2133         up(&pending_dir->i_sem);
2134         RETURN(rc);
2135 }
2136
2137 /* The idea here is that we need to get four locks in the end:
2138  * one on each parent directory, one on each child.  We need to take
2139  * these locks in some kind of order (to avoid deadlocks), and the order
2140  * I selected is "increasing resource number" order.  We need to look up
2141  * the children, however, before we know what the resource number(s) are.
2142  * Thus the following plan:
2143  *
2144  * 1,2. Look up the parents
2145  * 3,4. Look up the children
2146  * 5. Take locks on the parents and children, in order
2147  * 6. Verify that the children haven't changed since they were looked up
2148  *
2149  * If there was a race and the children changed since they were first looked
2150  * up, it is possible that mds_verify_child() will be able to just grab the
2151  * lock on the new child resource (if it has a higher resource than any other)
2152  * but we need to compare against not only its parent, but also against the
2153  * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2154  *
2155  * We need the fancy igrab() on the child inodes because we aren't holding a
2156  * lock on the parent after the lookup is done, so dentry->d_inode may change
2157  * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2158  */
2159 static int mds_get_parents_children_locked(struct obd_device *obd,
2160                                            struct mds_obd *mds,
2161                                            struct ll_fid *p1_fid,
2162                                            struct dentry **de_srcdirp,
2163                                            struct ll_fid *p2_fid,
2164                                            struct dentry **de_tgtdirp,
2165                                            int parent_mode,
2166                                            const char *old_name, int old_len,
2167                                            struct dentry **de_oldp,
2168                                            const char *new_name, int new_len,
2169                                            struct dentry **de_newp,
2170                                            struct lustre_handle *dlm_handles,
2171                                            int child_mode)
2172 {
2173         struct ldlm_res_id p1_res_id = { .name = {0} };
2174         struct ldlm_res_id p2_res_id = { .name = {0} };
2175         struct ldlm_res_id c1_res_id = { .name = {0} };
2176         struct ldlm_res_id c2_res_id = { .name = {0} };
2177         ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2178         /* Only dentry should change, but the inode itself would be
2179            intact otherwise */
2180         ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2181         /* If something is going to be replaced, both dentry and inode locks are
2182            needed */
2183         ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
2184                                                         MDS_INODELOCK_UPDATE}};
2185         struct ldlm_res_id *maxres_src, *maxres_tgt;
2186         struct inode *inode;
2187         int rc = 0, cleanup_phase = 0;
2188         ENTRY;
2189
2190         /* Step 1: Lookup the source directory */
2191         *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
2192         if (IS_ERR(*de_srcdirp))
2193                 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2194
2195         cleanup_phase = 1; /* source directory dentry */
2196
2197         p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
2198         p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
2199
2200         /* Step 2: Lookup the target directory */
2201         if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
2202                 *de_tgtdirp = dget(*de_srcdirp);
2203         } else {
2204                 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
2205                 if (IS_ERR(*de_tgtdirp)) {
2206                         rc = PTR_ERR(*de_tgtdirp);
2207                         *de_tgtdirp = NULL;
2208                         GOTO(cleanup, rc);
2209                 }
2210         }
2211
2212         cleanup_phase = 2; /* target directory dentry */
2213
2214         p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
2215         p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
2216
2217 #ifdef S_PDIROPS
2218         dlm_handles[5].cookie = 0;
2219         dlm_handles[6].cookie = 0;
2220         if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2221                 /* Get a temp lock on just ino, gen to flush client cache and
2222                  * to protect dirs from concurrent splitting */
2223                 rc = enqueue_ordered_locks(obd, &p1_res_id, &(dlm_handles[5]),
2224                                            LCK_PW, &p_policy, &p2_res_id,
2225                                            &(dlm_handles[6]),LCK_PW,&p_policy);
2226                 if (rc != ELDLM_OK)
2227                         GOTO(cleanup, rc);
2228
2229                 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2230                 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2231                 CDEBUG(D_INFO, "take locks on %lu:%u:"LPX64", %lu:%u:"LPX64"\n",
2232                        (*de_srcdirp)->d_inode->i_ino,
2233                        (*de_srcdirp)->d_inode->i_generation, p1_res_id.name[2],
2234                        (*de_tgtdirp)->d_inode->i_ino,
2235                        (*de_tgtdirp)->d_inode->i_generation, p2_res_id.name[2]);
2236         }
2237 #endif
2238
2239         /* Step 3: Lookup the source child entry */
2240         *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
2241         if (IS_ERR(*de_oldp)) {
2242                 rc = PTR_ERR(*de_oldp);
2243                 CERROR("old child lookup error (%*s): %d\n",
2244                        old_len - 1, old_name, rc);
2245                 GOTO(cleanup, rc);
2246         }
2247
2248         cleanup_phase = 3; /* original name dentry */
2249
2250         inode = (*de_oldp)->d_inode;
2251         if (inode != NULL) {
2252                 inode = igrab(inode);
2253                 if (inode == NULL)
2254                         GOTO(cleanup, rc = -ENOENT);
2255
2256                 c1_res_id.name[0] = inode->i_ino;
2257                 c1_res_id.name[1] = inode->i_generation;
2258                 iput(inode);
2259         } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2260                 c1_res_id.name[0] = (*de_oldp)->d_inum;
2261                 c1_res_id.name[1] = (*de_oldp)->d_generation;
2262         }
2263
2264         /* Step 4: Lookup the target child entry */
2265         *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
2266         if (IS_ERR(*de_newp)) {
2267                 rc = PTR_ERR(*de_newp);
2268                 CERROR("new child lookup error (%*s): %d\n",
2269                        old_len - 1, old_name, rc);
2270                 GOTO(cleanup, rc);
2271         }
2272
2273         cleanup_phase = 4; /* target dentry */
2274
2275         inode = (*de_newp)->d_inode;
2276         if (inode != NULL) {
2277                 inode = igrab(inode);
2278                 if (inode == NULL)
2279                         goto retry_locks;
2280
2281                 c2_res_id.name[0] = inode->i_ino;
2282                 c2_res_id.name[1] = inode->i_generation;
2283                 iput(inode);
2284         } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2285                 c2_res_id.name[0] = (*de_newp)->d_inum;
2286                 c2_res_id.name[1] = (*de_newp)->d_generation;
2287         }
2288
2289 retry_locks:
2290         /* Step 5: Take locks on the parents and child(ren) */
2291         maxres_src = &p1_res_id;
2292         maxres_tgt = &p2_res_id;
2293         cleanup_phase = 4; /* target dentry */
2294
2295         if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL,NULL))
2296                 maxres_src = &c1_res_id;
2297         if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL,NULL))
2298                 maxres_tgt = &c2_res_id;
2299
2300         rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
2301                                     &p_policy,
2302                                     &p2_res_id, &dlm_handles[1], parent_mode,
2303                                     &p_policy,
2304                                     &c1_res_id, &dlm_handles[2], child_mode,
2305                                     &c1_policy,
2306                                     &c2_res_id, &dlm_handles[3], child_mode,
2307                                     &c2_policy);
2308         if (rc)
2309                 GOTO(cleanup, rc);
2310
2311         cleanup_phase = 6; /* parent and child(ren) locks */
2312
2313         /* Step 6a: Re-lookup source child to verify it hasn't changed */
2314         rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2315                               parent_mode, &c1_res_id, &dlm_handles[2],
2316                               de_oldp, child_mode, &c1_policy, old_name,old_len,
2317                               maxres_tgt);
2318         if (rc) {
2319                 if (c2_res_id.name[0] != 0)
2320                         ldlm_lock_decref(&dlm_handles[3], child_mode);
2321                 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2322                 cleanup_phase = 4;
2323                 if (rc > 0)
2324                         goto retry_locks;
2325                 GOTO(cleanup, rc);
2326         }
2327
2328         if (!DENTRY_VALID(*de_oldp))
2329                 GOTO(cleanup, rc = -ENOENT);
2330
2331         /* Step 6b: Re-lookup target child to verify it hasn't changed */
2332         rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2333                               parent_mode, &c2_res_id, &dlm_handles[3],
2334                               de_newp, child_mode, &c2_policy, new_name,
2335                               new_len, maxres_src);
2336         if (rc) {
2337                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2338                 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2339                 cleanup_phase = 4;
2340                 if (rc > 0)
2341                         goto retry_locks;
2342                 GOTO(cleanup, rc);
2343         }
2344
2345         EXIT;
2346 cleanup:
2347         if (rc) {
2348                 switch (cleanup_phase) {
2349                 case 6: /* child lock(s) */
2350                         if (c2_res_id.name[0] != 0)
2351                                 ldlm_lock_decref(&dlm_handles[3], child_mode);
2352                         if (c1_res_id.name[0] != 0)
2353                                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2354                 case 5: /* parent locks */
2355                         ldlm_lock_decref(&dlm_handles[1], parent_mode);
2356                         ldlm_lock_decref(&dlm_handles[0], parent_mode);
2357                 case 4: /* target dentry */
2358                         l_dput(*de_newp);
2359                 case 3: /* source dentry */
2360                         l_dput(*de_oldp);
2361                 case 2: /* target directory dentry */
2362                         l_dput(*de_tgtdirp);
2363                 case 1: /* source directry dentry */
2364                         l_dput(*de_srcdirp);
2365                 }
2366         }
2367
2368         return rc;
2369 }
2370
2371 static int mds_reint_rename_create_name(struct mds_update_record *rec,
2372                                         int offset, struct ptlrpc_request *req)
2373 {
2374         struct obd_device *obd = req->rq_export->exp_obd;
2375         struct dentry *de_srcdir = NULL;
2376         struct dentry *de_new = NULL;
2377         struct mds_obd *mds = mds_req2mds(req);
2378         struct lustre_handle parent_lockh[2] = {{0}, {0}};
2379         struct lustre_handle child_lockh = {0};
2380         int cleanup_phase = 0;
2381         void *handle = NULL;
2382         int update_mode, rc = 0;
2383         ENTRY;
2384
2385         /* another MDS executing rename operation has asked us
2386          * to create target name. such a creation should destroy
2387          * existing target name */
2388
2389         CDEBUG(D_OTHER, "%s: request to create name %s for %lu/%lu/%lu\n",
2390                         obd->obd_name, rec->ur_tgt,
2391                         (unsigned long) rec->ur_fid1->mds,
2392                         (unsigned long) rec->ur_fid1->id,
2393                         (unsigned long) rec->ur_fid1->generation);
2394
2395         /* first, lookup the target */
2396         child_lockh.cookie = 0;
2397         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid2, parent_lockh,
2398                                          &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2399                                          &update_mode, rec->ur_tgt, rec->ur_tgtlen,
2400                                          &child_lockh, &de_new, LCK_EX,
2401                                          MDS_INODELOCK_LOOKUP);
2402         if (rc)
2403                 GOTO(cleanup, rc);
2404
2405         cleanup_phase = 1;
2406
2407         LASSERT(de_srcdir);
2408         LASSERT(de_srcdir->d_inode);
2409         LASSERT(de_new);
2410
2411         if (de_new->d_inode) {
2412                 /* name exists and points to local inode
2413                  * try to unlink this name and create new one */
2414                 CERROR("%s: %s points to local inode %lu/%lu\n",
2415                        obd->obd_name, rec->ur_tgt,
2416                        (unsigned long) de_new->d_inode->i_ino,
2417                        (unsigned long) de_new->d_inode->i_generation);
2418                 handle = fsfilt_start(obd, de_srcdir->d_inode,
2419                                       FSFILT_OP_RENAME, NULL);
2420                 if (IS_ERR(handle))
2421                         GOTO(cleanup, rc = PTR_ERR(handle));
2422                 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de_new);
2423                 if (rc)
2424                         GOTO(cleanup, rc);
2425         } else if (de_new->d_flags & DCACHE_CROSS_REF) {
2426                 /* name exists adn points to remove inode */
2427                 CERROR("%s: %s points to remote inode %lu/%lu/%lu\n",
2428                        obd->obd_name, rec->ur_tgt,
2429                        (unsigned long) de_new->d_mdsnum,
2430                        (unsigned long) de_new->d_inum,
2431                        (unsigned long) de_new->d_generation);
2432         } else {
2433                 /* name doesn't exist. the simplest case */
2434                 handle = fsfilt_start(obd, de_srcdir->d_inode,
2435                                       FSFILT_OP_LINK, NULL);
2436                 if (IS_ERR(handle))
2437                         GOTO(cleanup, rc = PTR_ERR(handle));
2438         }
2439        
2440         cleanup_phase = 2;
2441         rc = fsfilt_add_dir_entry(obd, de_srcdir, rec->ur_tgt,
2442                         rec->ur_tgtlen - 1, rec->ur_fid1->id,
2443                         rec->ur_fid1->generation, rec->ur_fid1->mds);
2444         if (rc)
2445                 CERROR("add_dir_entry() returned error %d\n", rc);
2446 cleanup:
2447         EXIT;
2448         rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2449                                 handle, req, rc, 0);
2450         switch(cleanup_phase) {
2451                 case 2:
2452                 case 1:
2453 #ifdef S_PDIROPS
2454                         if (parent_lockh[1].cookie != 0)
2455                                 ldlm_lock_decref(&parent_lockh[1], update_mode);
2456 #endif
2457                         ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2458                         if (child_lockh.cookie != 0)
2459                                 ldlm_lock_decref(&child_lockh, LCK_EX);
2460                         l_dput(de_new);
2461                         l_dput(de_srcdir);
2462                         break;
2463                 default:
2464                         LBUG();
2465         }
2466
2467         req->rq_status = rc;
2468
2469         RETURN(0);
2470 }
2471
2472 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
2473                                       struct ptlrpc_request *req)
2474 {
2475         struct obd_device *obd = req->rq_export->exp_obd;
2476         struct ptlrpc_request *req2 = NULL;
2477         struct dentry *de_srcdir = NULL;
2478         struct dentry *de_old = NULL;
2479         struct mds_obd *mds = mds_req2mds(req);
2480         struct lustre_handle parent_lockh[2] = {{0}, {0}};
2481         struct lustre_handle child_lockh = {0};
2482         struct mdc_op_data opdata;
2483         void *handle = NULL;
2484         int update_mode, rc = 0;
2485         ENTRY;
2486
2487         CDEBUG(D_OTHER, "%s: move name %s onto another mds%u\n",
2488                obd->obd_name, rec->ur_name, rec->ur_fid2->mds + 1);
2489         memset(&opdata, 0, sizeof(opdata));
2490
2491         child_lockh.cookie = 0;
2492         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1, parent_lockh,
2493                                          &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2494                                          &update_mode, rec->ur_name, 
2495                                          rec->ur_namelen, &child_lockh, &de_old,
2496                                          LCK_EX, MDS_INODELOCK_LOOKUP);
2497         LASSERT(rc == 0);
2498         LASSERT(de_srcdir);
2499         LASSERT(de_srcdir->d_inode);
2500         LASSERT(de_old);
2501        
2502         /* we already know the target should be created on another MDS
2503          * so, we have to request that MDS to do it */
2504
2505         /* prepare source fid */
2506         if (de_old->d_flags & DCACHE_CROSS_REF) {
2507                 LASSERT(de_old->d_inode == NULL);
2508                 CDEBUG(D_OTHER, "request to move remote name\n");
2509                 opdata.fid1.mds = de_old->d_mdsnum;
2510                 opdata.fid1.id = de_old->d_inum;
2511                 opdata.fid1.generation = de_old->d_generation;
2512         } else if (de_old->d_inode == NULL) {
2513                 /* oh, source doesn't exist */
2514                 GOTO(cleanup, rc = -ENOENT);
2515         } else {
2516                 LASSERT(de_old->d_inode != NULL);
2517                 CDEBUG(D_OTHER, "request to move local name\n");
2518                 opdata.fid1.mds = mds->mds_num;
2519                 opdata.fid1.id = de_old->d_inode->i_ino;
2520                 opdata.fid1.generation = de_old->d_inode->i_generation;
2521         }
2522
2523         opdata.fid2 = *(rec->ur_fid2);
2524         rc = md_rename(mds->mds_lmv_exp, &opdata, NULL, 0, rec->ur_tgt,
2525                        rec->ur_tgtlen - 1, &req2);
2526        
2527         if (rc)
2528                 GOTO(cleanup, rc);
2529
2530         handle = fsfilt_start(obd, de_srcdir->d_inode, FSFILT_OP_UNLINK, NULL);
2531         if (IS_ERR(handle))
2532                 GOTO(cleanup, rc = PTR_ERR(handle));
2533         rc = fsfilt_del_dir_entry(obd, de_old);
2534         d_drop(de_old);
2535
2536 cleanup:
2537         EXIT;
2538         rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2539                                 handle, req, rc, 0);
2540         if (req2)
2541                 ptlrpc_req_finished(req2);
2542
2543 #ifdef S_PDIROPS
2544         if (parent_lockh[1].cookie != 0)
2545                 ldlm_lock_decref(&parent_lockh[1], update_mode);
2546 #endif
2547         ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2548         if (child_lockh.cookie != 0)
2549                 ldlm_lock_decref(&child_lockh, LCK_EX);
2550
2551         l_dput(de_old);
2552         l_dput(de_srcdir);
2553
2554         req->rq_status = rc;
2555         RETURN(0);
2556
2557 }
2558
2559 static int mds_reint_rename(struct mds_update_record *rec, int offset,
2560                             struct ptlrpc_request *req,
2561                             struct lustre_handle *lockh)
2562 {
2563         struct obd_device *obd = req->rq_export->exp_obd;
2564         struct dentry *de_srcdir = NULL;
2565         struct dentry *de_tgtdir = NULL;
2566         struct dentry *de_old = NULL;
2567         struct dentry *de_new = NULL;
2568         struct mds_obd *mds = mds_req2mds(req);
2569         struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
2570         struct mds_body *body = NULL;
2571         int rc = 0, lock_count = 3;
2572         int cleanup_phase = 0;
2573         void *handle = NULL;
2574         ENTRY;
2575
2576         LASSERT(offset == 0);
2577
2578         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
2579                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
2580                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
2581
2582         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2583
2584         if (rec->ur_namelen == 1) {
2585                 rc = mds_reint_rename_create_name(rec, offset, req);
2586                 RETURN(rc);
2587         }
2588
2589         if (rec->ur_fid2->mds != mds->mds_num) {
2590                 rc = mds_reint_rename_to_remote(rec, offset, req);
2591                 RETURN(rc);
2592         }
2593         
2594         rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
2595                                              rec->ur_fid2, &de_tgtdir, LCK_PW,
2596                                              rec->ur_name, rec->ur_namelen,
2597                                              &de_old, rec->ur_tgt,
2598                                              rec->ur_tgtlen, &de_new,
2599                                              dlm_handles, LCK_EX);
2600         if (rc)
2601                 GOTO(cleanup, rc);
2602
2603         cleanup_phase = 1; /* parent(s), children, locks */
2604
2605         if (de_new->d_inode)
2606                 lock_count = 4;
2607
2608         /* sanity check for src inode */
2609         if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2610             de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
2611                 GOTO(cleanup, rc = -EINVAL);
2612
2613         /* sanity check for dest inode */
2614         if (de_new->d_inode &&
2615             (de_new->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2616              de_new->d_inode->i_ino == de_tgtdir->d_inode->i_ino))
2617                 GOTO(cleanup, rc = -EINVAL);
2618
2619         if (de_old->d_inode == de_new->d_inode)
2620                 GOTO(cleanup, rc = 0);
2621
2622         /* if we are about to remove the target at first, pass the EA of
2623          * that inode to client to perform and cleanup on OST */
2624         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
2625         LASSERT(body != NULL);
2626
2627         if (de_new->d_inode &&
2628             S_ISREG(de_new->d_inode->i_mode) &&
2629             de_new->d_inode->i_nlink == 1 &&
2630             mds_open_orphan_count(de_new->d_inode) == 0) {
2631                 mds_pack_inode2fid(obd, &body->fid1, de_new->d_inode);
2632                 mds_pack_inode2body(obd, body, de_new->d_inode);
2633                 mds_pack_md(obd, req->rq_repmsg, 1, body, de_new->d_inode, 1);
2634                 if (!(body->valid & OBD_MD_FLEASIZE)) {
2635                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2636                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
2637                 } else {
2638                         /* XXX need log unlink? */
2639                 }
2640         }
2641
2642         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
2643                        de_srcdir->d_inode->i_sb);
2644
2645         handle = fsfilt_start(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME, NULL);
2646         if (IS_ERR(handle))
2647                 GOTO(cleanup, rc = PTR_ERR(handle));
2648
2649         /* FIXME need adjust the journal block count? */
2650         /* if the target should be moved to PENDING, we at first increase the
2651          * link and later vfs_rename() will decrease the link count again */
2652         if (de_new->d_inode &&
2653             S_ISREG(de_new->d_inode->i_mode) &&
2654             de_new->d_inode->i_nlink == 1 &&
2655             mds_open_orphan_count(de_new->d_inode) > 0) {
2656                 rc = mds_add_link_orphan(rec, obd, de_new);
2657                 if (rc)
2658                         GOTO(cleanup, rc);
2659         }
2660
2661         lock_kernel();
2662         de_old->d_fsdata = req;
2663         de_new->d_fsdata = req;
2664         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
2665         unlock_kernel();
2666
2667         GOTO(cleanup, rc);
2668 cleanup:
2669         rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
2670                                 handle, req, rc, 0);
2671         switch (cleanup_phase) {
2672         case 1:
2673 #ifdef S_PDIROPS
2674                 if (dlm_handles[5].cookie != 0)
2675                         ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2676                 if (dlm_handles[6].cookie != 0)
2677                         ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2678 #endif
2679                 if (rc) {
2680                         if (lock_count == 4)
2681                                 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
2682                         ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
2683                         ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
2684                         ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
2685                 } else {
2686                         if (lock_count == 4)
2687                                 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
2688                         ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
2689                         ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
2690                         ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
2691                 }
2692                 l_dput(de_new);
2693                 l_dput(de_old);
2694                 l_dput(de_tgtdir);
2695                 l_dput(de_srcdir);
2696         case 0:
2697                 break;
2698         default:
2699                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2700                 LBUG();
2701         }
2702         req->rq_status = rc;
2703         return 0;
2704 }
2705
2706 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
2707                            struct ptlrpc_request *, struct lustre_handle *);
2708
2709 static mds_reinter reinters[REINT_MAX + 1] = {
2710         [REINT_SETATTR] mds_reint_setattr,
2711         [REINT_CREATE] mds_reint_create,
2712         [REINT_LINK] mds_reint_link,
2713         [REINT_UNLINK] mds_reint_unlink,
2714         [REINT_RENAME] mds_reint_rename,
2715         [REINT_OPEN] mds_open
2716 };
2717
2718 int mds_reint_rec(struct mds_update_record *rec, int offset,
2719                   struct ptlrpc_request *req, struct lustre_handle *lockh)
2720 {
2721         struct obd_device *obd = req->rq_export->exp_obd;
2722         struct lvfs_run_ctxt saved;
2723         int rc;
2724
2725         /* checked by unpacker */
2726         LASSERT(rec->ur_opcode <= REINT_MAX &&
2727                 reinters[rec->ur_opcode] != NULL);
2728
2729         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
2730         rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
2731         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
2732
2733         return rc;
2734 }