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