Whamcloud - gitweb
d30103c96a24f7b5cd66ddf8beeec9eb600487de
[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-2005 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 the Lustre file system, http://www.lustre.org
13  *   Lustre is a trademark of Cluster File Systems, Inc.
14  *
15  *   You may have signed or agreed to another license before downloading
16  *   this software.  If so, you are bound by the terms and conditions
17  *   of that agreement, and the following does not apply to you.  See the
18  *   LICENSE file included with this distribution for more information.
19  *
20  *   If you did not agree to a different license, then this copy of Lustre
21  *   is open source software; you can redistribute it and/or modify it
22  *   under the terms of version 2 of the GNU General Public License as
23  *   published by the Free Software Foundation.
24  *
25  *   In either case, Lustre is distributed in the hope that it will be
26  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
27  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  *   license text for more details.
29  */
30
31 #ifndef EXPORT_SYMTAB
32 # define EXPORT_SYMTAB
33 #endif
34 #define DEBUG_SUBSYSTEM S_MDS
35
36 #include <linux/fs.h>
37 #include <obd_support.h>
38 #include <obd_class.h>
39 #include <obd.h>
40 #include <lustre_lib.h>
41 #include <lustre/lustre_idl.h>
42 #include <lustre_mds.h>
43 #include <lustre_dlm.h>
44 #include <lustre_fsfilt.h>
45 #include <lustre_ucache.h>
46
47 #include "mds_internal.h"
48
49 void mds_commit_cb(struct obd_device *obd, __u64 transno, void *data,
50                    int error)
51 {
52         obd_transno_commit_cb(obd, transno, error);
53 }
54
55 struct mds_logcancel_data {
56         struct lov_mds_md      *mlcd_lmm;
57         int                     mlcd_size;
58         int                     mlcd_cookielen;
59         int                     mlcd_eadatalen;
60         struct llog_cookie      mlcd_cookies[0];
61 };
62
63
64 static void mds_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
65                                   void *cb_data, int error)
66 {
67         struct mds_logcancel_data *mlcd = cb_data;
68         struct lov_stripe_md *lsm = NULL;
69         struct llog_ctxt *ctxt;
70         int rc;
71
72         obd_transno_commit_cb(obd, transno, error);
73
74         CDEBUG(D_HA, "cancelling %d cookies\n",
75                (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
76
77         rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, mlcd->mlcd_lmm,
78                           mlcd->mlcd_eadatalen);
79         if (rc < 0) {
80                 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
81                        (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
82                        rc);
83         } else {
84                 ///* XXX 0 normally, SENDNOW for debug */);
85                 rc = obd_checkmd(obd->u.mds.mds_osc_exp, obd->obd_self_export,
86                                  lsm);
87                 if (rc)
88                         CERROR("Can not revalidate lsm %p \n", lsm);
89
90                 ctxt = llog_get_context(obd,mlcd->mlcd_cookies[0].lgc_subsys+1);
91                 rc = llog_cancel(ctxt, lsm, mlcd->mlcd_cookielen /
92                                                 sizeof(*mlcd->mlcd_cookies),
93                                  mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW);
94                 if (rc)
95                         CERROR("error cancelling %d log cookies: rc %d\n",
96                                (int)(mlcd->mlcd_cookielen /
97                                      sizeof(*mlcd->mlcd_cookies)), rc);
98         }
99
100         OBD_FREE(mlcd, mlcd->mlcd_size);
101 }
102
103 /* Assumes caller has already pushed us into the kernel context. */
104 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
105                        struct ptlrpc_request *req, int rc, __u32 op_data, 
106                        int force_sync)
107 {
108         struct mds_export_data *med = &req->rq_export->exp_mds_data;
109         struct mds_client_data *mcd = med->med_mcd;
110         struct obd_device *obd = req->rq_export->exp_obd;
111         int err;
112         __u64 transno, prev_transno;
113         loff_t off;
114         int log_pri = D_HA;
115         ENTRY;
116
117         if (IS_ERR(handle)) {
118                 LASSERT(rc != 0);
119                 RETURN(rc);
120         }
121
122         /* if the export has already been failed, we have no last_rcvd slot */
123         if (req->rq_export->exp_failed || obd->obd_fail) {
124                 CWARN("commit transaction for disconnected client %s: rc %d\n",
125                       req->rq_export->exp_client_uuid.uuid, rc);
126                 if (rc == 0)
127                         rc = -ENOTCONN;
128                 if (handle)
129                         GOTO(commit, rc);
130                 RETURN(rc);
131         }
132
133         if (handle == NULL) {
134                 /* if we're starting our own xaction, use our own inode */
135                 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
136                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
137                 if (IS_ERR(handle)) {
138                         CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
139                         RETURN(PTR_ERR(handle));
140                 }
141         }
142
143         off = med->med_lr_off;
144
145         transno = lustre_msg_get_transno(req->rq_reqmsg);
146         if (rc != 0) {
147                 if (transno != 0) {
148                         CERROR("%s: replay %s transno "LPU64" failed: rc %d\n",
149                                obd->obd_name,
150                                libcfs_nid2str(req->rq_export->exp_connection->c_peer.nid),
151                                transno, rc);
152                         transno = 0;
153                 }
154         } else if (transno == 0) {
155                 spin_lock(&mds->mds_transno_lock);
156                 transno = ++mds->mds_last_transno;
157                 spin_unlock(&mds->mds_transno_lock);
158         } else {
159                 spin_lock(&mds->mds_transno_lock);
160                 if (transno > mds->mds_last_transno)
161                         mds->mds_last_transno = transno;
162                 spin_unlock(&mds->mds_transno_lock);
163         }
164
165         req->rq_transno = transno;
166         lustre_msg_set_transno(req->rq_repmsg, transno);
167         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
168                 prev_transno = le64_to_cpu(mcd->mcd_last_close_transno);
169                 mcd->mcd_last_close_transno = cpu_to_le64(transno);
170                 mcd->mcd_last_close_xid = cpu_to_le64(req->rq_xid);
171                 mcd->mcd_last_close_result = cpu_to_le32(rc);
172                 mcd->mcd_last_close_data = cpu_to_le32(op_data);
173         } else {
174                 prev_transno = le64_to_cpu(mcd->mcd_last_transno);
175                 mcd->mcd_last_transno = cpu_to_le64(transno);
176                 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
177                 mcd->mcd_last_result = cpu_to_le32(rc);
178                 mcd->mcd_last_data = cpu_to_le32(op_data);
179         }
180         /* update the server data to not lose the greatest transno. Bug 11125 */
181         if ((transno == 0) && (prev_transno == mds->mds_last_transno))
182                 mds_update_server_data(obd, 0);
183
184         if (off <= 0) {
185                 CERROR("client idx %d has offset %lld\n", med->med_lr_idx, off);
186                 err = -EINVAL;
187         } else {
188                 struct obd_export *exp = req->rq_export;
189
190                 if (!force_sync)
191                         force_sync = fsfilt_add_journal_cb(exp->exp_obd,transno, 
192                                                           handle, mds_commit_cb,
193                                                           NULL);
194
195                 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
196                                           sizeof(*mcd), &off,
197                                           force_sync | exp->exp_need_sync);
198                 if (force_sync)
199                         mds_commit_cb(obd, transno, NULL, err);
200         }
201
202         if (err) {
203                 log_pri = D_ERROR;
204                 if (rc == 0)
205                         rc = err;
206         }
207
208         DEBUG_REQ(log_pri, req,
209                   "wrote trans #"LPU64" rc %d client %s at idx %u: err = %d",
210                    transno, rc, mcd->mcd_uuid, med->med_lr_idx, err);
211
212         err = mds_lov_write_objids(obd);
213         if (err) {
214                 log_pri = D_ERROR;
215                 if (rc == 0)
216                         rc = err;
217         }
218         CDEBUG(log_pri, "wrote objids: err = %d\n", err);
219
220 commit:
221         err = fsfilt_commit(obd, inode, handle, 0);
222         if (err) {
223                 CERROR("error committing transaction: %d\n", err);
224                 if (!rc)
225                         rc = err;
226         }
227
228         RETURN(rc);
229 }
230
231 /* this gives the same functionality as the code between
232  * sys_chmod and inode_setattr
233  * chown_common and inode_setattr
234  * utimes and inode_setattr
235  */
236 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
237 {
238         time_t now = CURRENT_SECONDS;
239         struct iattr *attr = &rec->ur_iattr;
240         unsigned int ia_valid = attr->ia_valid;
241         int error;
242         ENTRY;
243
244         if (ia_valid & ATTR_RAW)
245                 attr->ia_valid &= ~ATTR_RAW;
246
247         if (!(ia_valid & ATTR_CTIME_SET))
248                 LTIME_S(attr->ia_ctime) = now;
249         else
250                 attr->ia_valid &= ~ATTR_CTIME_SET;
251         if (!(ia_valid & ATTR_ATIME_SET))
252                 LTIME_S(attr->ia_atime) = now;
253         if (!(ia_valid & ATTR_MTIME_SET))
254                 LTIME_S(attr->ia_mtime) = now;
255
256         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
257                 RETURN((attr->ia_valid & ~ATTR_ATTR_FLAG) ? -EPERM : 0);
258
259         /* times */
260         if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
261                 if (current->fsuid != inode->i_uid &&
262                     (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
263                         RETURN(error);
264         }
265
266         if (ia_valid & ATTR_SIZE &&
267             /* NFSD hack for open(O_CREAT|O_TRUNC)=mknod+truncate (bug 5781) */
268             !(rec->ur_uc.luc_fsuid == inode->i_uid &&
269               ia_valid & MDS_OPEN_OWNEROVERRIDE)) {
270                 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
271                         RETURN(error);
272         }
273
274         if (ia_valid & (ATTR_UID | ATTR_GID)) {
275                 /* chown */
276                 error = -EPERM;
277                 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
278                         RETURN(-EPERM);
279                 if (attr->ia_uid == (uid_t) -1)
280                         attr->ia_uid = inode->i_uid;
281                 if (attr->ia_gid == (gid_t) -1)
282                         attr->ia_gid = inode->i_gid;
283                 if (!(ia_valid & ATTR_MODE))
284                         attr->ia_mode = inode->i_mode;
285                 /*
286                  * If the user or group of a non-directory has been
287                  * changed by a non-root user, remove the setuid bit.
288                  * 19981026 David C Niemi <niemi@tux.org>
289                  *
290                  * Changed this to apply to all users, including root,
291                  * to avoid some races. This is the behavior we had in
292                  * 2.0. The check for non-root was definitely wrong
293                  * for 2.2 anyway, as it should have been using
294                  * CAP_FSETID rather than fsuid -- 19990830 SD.
295                  */
296                 if ((inode->i_mode & S_ISUID) == S_ISUID &&
297                     !S_ISDIR(inode->i_mode)) {
298                         attr->ia_mode &= ~S_ISUID;
299                         attr->ia_valid |= ATTR_MODE;
300                 }
301                 /*
302                  * Likewise, if the user or group of a non-directory
303                  * has been changed by a non-root user, remove the
304                  * setgid bit UNLESS there is no group execute bit
305                  * (this would be a file marked for mandatory
306                  * locking).  19981026 David C Niemi <niemi@tux.org>
307                  *
308                  * Removed the fsuid check (see the comment above) --
309                  * 19990830 SD.
310                  */
311                 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
312                      (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
313                         attr->ia_mode &= ~S_ISGID;
314                         attr->ia_valid |= ATTR_MODE;
315                 }
316         } else if (ia_valid & ATTR_MODE) {
317                 int mode = attr->ia_mode;
318                 /* chmod */
319                 if (attr->ia_mode == (umode_t)-1)
320                         mode = inode->i_mode;
321                 attr->ia_mode =
322                         (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
323         }
324         RETURN(0);
325 }
326
327 void mds_steal_ack_locks(struct ptlrpc_request *req)
328 {
329         struct obd_export         *exp = req->rq_export;
330         struct list_head          *tmp;
331         struct ptlrpc_reply_state *oldrep;
332         struct ptlrpc_service     *svc;
333         int                        i;
334
335         /* CAVEAT EMPTOR: spinlock order */
336         spin_lock(&exp->exp_lock);
337         list_for_each (tmp, &exp->exp_outstanding_replies) {
338                 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
339
340                 if (oldrep->rs_xid != req->rq_xid)
341                         continue;
342
343                 if (lustre_msg_get_opc(oldrep->rs_msg) !=
344                     lustre_msg_get_opc(req->rq_reqmsg))
345                         CERROR ("Resent req xid "LPX64" has mismatched opc: "
346                                 "new %d old %d\n", req->rq_xid,
347                                 lustre_msg_get_opc(req->rq_reqmsg),
348                                 lustre_msg_get_opc(oldrep->rs_msg));
349
350                 svc = oldrep->rs_service;
351                 spin_lock (&svc->srv_lock);
352
353                 list_del_init (&oldrep->rs_exp_list);
354
355                 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
356                       " o%d NID %s\n",
357                       oldrep->rs_nlocks, oldrep,
358                       oldrep->rs_xid, oldrep->rs_transno,
359                       lustre_msg_get_opc(oldrep->rs_msg),
360                       libcfs_nid2str(exp->exp_connection->c_peer.nid));
361
362                 for (i = 0; i < oldrep->rs_nlocks; i++)
363                         ptlrpc_save_lock(req,
364                                          &oldrep->rs_locks[i],
365                                          oldrep->rs_modes[i]);
366                 oldrep->rs_nlocks = 0;
367
368                 DEBUG_REQ(D_HA, req, "stole locks for");
369                 ptlrpc_schedule_difficult_reply (oldrep);
370
371                 spin_unlock (&svc->srv_lock);
372                 break;
373         }
374         spin_unlock(&exp->exp_lock);
375 }
376 EXPORT_SYMBOL(mds_steal_ack_locks);
377 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
378 {
379         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
380                 req->rq_transno = le64_to_cpu(mcd->mcd_last_close_transno);
381                 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
382                 req->rq_status = le32_to_cpu(mcd->mcd_last_close_result);
383                 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
384         } else {
385                 req->rq_transno = le64_to_cpu(mcd->mcd_last_transno);
386                 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
387                 req->rq_status = le32_to_cpu(mcd->mcd_last_result);
388                 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
389         }
390         DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
391                   req->rq_transno, req->rq_status);
392
393         mds_steal_ack_locks(req);
394 }
395
396 static void reconstruct_reint_setattr(struct mds_update_record *rec,
397                                       int offset, struct ptlrpc_request *req)
398 {
399         struct mds_export_data *med = &req->rq_export->exp_mds_data;
400         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
401         struct dentry *de;
402         struct mds_body *body;
403
404         mds_req_from_mcd(req, med->med_mcd);
405
406         de = mds_fid2dentry(obd, rec->ur_fid1, NULL);
407         if (IS_ERR(de)) {
408                 LASSERT(PTR_ERR(de) == req->rq_status);
409                 return;
410         }
411
412         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
413         mds_pack_inode2fid(&body->fid1, de->d_inode);
414         mds_pack_inode2body(body, de->d_inode);
415
416         /* Don't return OST-specific attributes if we didn't just set them */
417         if (rec->ur_iattr.ia_valid & ATTR_SIZE)
418                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
419         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
420                 body->valid |= OBD_MD_FLMTIME;
421         if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
422                 body->valid |= OBD_MD_FLATIME;
423
424         l_dput(de);
425 }
426
427 int mds_osc_setattr_async(struct obd_device *obd, __u32 uid, __u32 gid,
428                           struct lov_mds_md *lmm, int lmm_size,
429                           struct llog_cookie *logcookies, __u64 id, __u32 gen,
430                           struct obd_capa *oc)
431 {
432         struct mds_obd *mds = &obd->u.mds;
433         struct obd_trans_info oti = { 0 };
434         struct obd_info oinfo = { { { 0 } } };
435         int rc;
436         ENTRY;
437
438         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OST_SETATTR))
439                 RETURN(0);
440
441         /* first get memory EA */
442         OBDO_ALLOC(oinfo.oi_oa);
443         if (!oinfo.oi_oa)
444                 RETURN(-ENOMEM);
445
446         LASSERT(lmm);
447
448         rc = obd_unpackmd(mds->mds_osc_exp, &oinfo.oi_md, lmm, lmm_size);
449         if (rc < 0) {
450                 CERROR("Error unpack md %p for inode "LPU64"\n", lmm, id);
451                 GOTO(out, rc);
452         }
453
454         rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, oinfo.oi_md);
455         if (rc) {
456                 CERROR("Error revalidate lsm %p \n", oinfo.oi_md);
457                 GOTO(out, rc);
458         }
459
460         /* then fill oa */
461         oinfo.oi_oa->o_uid = uid;
462         oinfo.oi_oa->o_gid = gid;
463         oinfo.oi_oa->o_id = oinfo.oi_md->lsm_object_id;
464         oinfo.oi_oa->o_gr = oinfo.oi_md->lsm_object_gr;
465         oinfo.oi_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP |
466                                 OBD_MD_FLUID | OBD_MD_FLGID;
467         if (logcookies) {
468                 oinfo.oi_oa->o_valid |= OBD_MD_FLCOOKIE;
469                 oti.oti_logcookies = logcookies;
470         }
471
472         oinfo.oi_oa->o_fid = id;
473         oinfo.oi_oa->o_generation = gen;
474         oinfo.oi_oa->o_valid |= OBD_MD_FLFID | OBD_MD_FLGENER;
475         oinfo.oi_capa = oc;
476
477         /* do async setattr from mds to ost not waiting for responses. */
478         rc = obd_setattr_async(mds->mds_osc_exp, &oinfo, &oti, NULL);
479         if (rc)
480                 CDEBUG(D_INODE, "mds to ost setattr objid 0x"LPX64
481                        " on ost error %d\n", oinfo.oi_md->lsm_object_id, rc);
482 out:
483         if (oinfo.oi_md)
484                 obd_free_memmd(mds->mds_osc_exp, &oinfo.oi_md);
485         OBDO_FREE(oinfo.oi_oa);
486         RETURN(rc);
487 }
488 EXPORT_SYMBOL(mds_osc_setattr_async);
489
490 /* In the raw-setattr case, we lock the child inode.
491  * In the write-back case or if being called from open, the client holds a lock
492  * already.
493  *
494  * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
495 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
496                              struct ptlrpc_request *req,
497                              struct lustre_handle *lh)
498 {
499         unsigned int ia_valid = rec->ur_iattr.ia_valid;
500         struct mds_obd *mds = mds_req2mds(req);
501         struct obd_device *obd = req->rq_export->exp_obd;
502         struct mds_body *body;
503         struct dentry *de;
504         struct inode *inode = NULL;
505         struct lustre_handle lockh;
506         void *handle = NULL;
507         struct mds_logcancel_data *mlcd = NULL;
508         struct lov_mds_md *lmm = NULL;
509         struct llog_cookie *logcookies = NULL;
510         int lmm_size = 0, need_lock = 1, cookie_size = 0;
511         int rc = 0, cleanup_phase = 0, err, locked = 0, sync = 0;
512         unsigned int qcids[MAXQUOTAS] = { 0, 0 };
513         unsigned int qpids[MAXQUOTAS] = { rec->ur_iattr.ia_uid,
514                                           rec->ur_iattr.ia_gid };
515         ENTRY;
516
517         LASSERT(offset == REQ_REC_OFF);
518         offset = REPLY_REC_OFF;
519
520         DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x", rec->ur_fid1->id,
521                   rec->ur_fid1->generation, rec->ur_iattr.ia_valid);
522         OBD_COUNTER_INCREMENT(obd, setattr);
523
524         MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
525
526         if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN ||
527             (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)) {
528                 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
529                 if (IS_ERR(de))
530                         GOTO(cleanup, rc = PTR_ERR(de));
531                 if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
532                         GOTO(cleanup, rc = -EROFS);
533         } else {
534                 __u64 lockpart = MDS_INODELOCK_UPDATE;
535                 if (rec->ur_iattr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
536                         lockpart |= MDS_INODELOCK_LOOKUP;
537
538                 de = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_EX,
539                                            &lockh, lockpart);
540                 if (IS_ERR(de))
541                         GOTO(cleanup, rc = PTR_ERR(de));
542                 locked = 1;
543         }
544
545         cleanup_phase = 1;
546         inode = de->d_inode;
547         LASSERT(inode);
548
549         if ((rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) ||
550             (rec->ur_iattr.ia_valid & ATTR_SIZE)) {
551                 /* Check write access for the O_TRUNC case */
552                 if (mds_query_write_access(inode) < 0)
553                         GOTO(cleanup, rc = -ETXTBSY);
554         }
555
556         /* save uid/gid for quota acq/rel */
557         qcids[USRQUOTA] = inode->i_uid;
558         qcids[GRPQUOTA] = inode->i_gid;
559
560         if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
561             rec->ur_eadata != NULL) {
562                 LOCK_INODE_MUTEX(inode);
563                 need_lock = 0;
564         }
565
566         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
567
568         /* start a log jounal handle if needed */
569         if (S_ISREG(inode->i_mode) &&
570             rec->ur_iattr.ia_valid & (ATTR_UID | ATTR_GID)) {
571                 lmm_size = mds->mds_max_mdsize;
572                 OBD_ALLOC(lmm, lmm_size);
573                 if (lmm == NULL)
574                         GOTO(cleanup, rc = -ENOMEM);
575
576                 cleanup_phase = 2;
577                 rc = mds_get_md(obd, inode, lmm, &lmm_size, need_lock);
578                 if (rc < 0)
579                         GOTO(cleanup, rc);
580                 rc = 0;
581
582                 handle = fsfilt_start_log(obd, inode, FSFILT_OP_SETATTR, NULL,
583                                           le32_to_cpu(lmm->lmm_stripe_count));
584         } else {
585                 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
586         }
587         if (IS_ERR(handle))
588                 GOTO(cleanup, rc = PTR_ERR(handle));
589
590         if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
591                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
592                        LTIME_S(rec->ur_iattr.ia_mtime),
593                        LTIME_S(rec->ur_iattr.ia_ctime));
594         rc = mds_fix_attr(inode, rec);
595         if (rc)
596                 GOTO(cleanup, rc);
597
598         if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) {  /* ioctl */
599                 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
600                                       (long)&rec->ur_flags);
601         } else if (rec->ur_iattr.ia_valid) {            /* setattr */
602                 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
603                 /* journal chown/chgrp in llog, just like unlink */
604                 if (rc == 0 && lmm_size){
605                         cookie_size = mds_get_cookie_size(obd, lmm);
606                         OBD_ALLOC(logcookies, cookie_size);
607                         if (logcookies == NULL)
608                                 GOTO(cleanup, rc = -ENOMEM);
609
610                         if (mds_log_op_setattr(obd, inode->i_uid, inode->i_gid, 
611                                                lmm, lmm_size,
612                                                logcookies, cookie_size) <= 0) {
613                                 OBD_FREE(logcookies, cookie_size);
614                                 logcookies = NULL;
615                         }
616                 }
617         }
618
619         if (rc == 0 && (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
620             rec->ur_eadata != NULL) {
621                 struct lov_stripe_md *lsm = NULL;
622                 struct lov_user_md *lum = NULL;
623
624                 rc = ll_permission(inode, MAY_WRITE, NULL);
625                 if (rc < 0)
626                         GOTO(cleanup, rc);
627
628                 lum = rec->ur_eadata;
629                 /* if { size, offset, count } = { 0, -1, 0 } (i.e. all default
630                  * values specified) then delete default striping from dir. */
631                 if (S_ISDIR(inode->i_mode) &&
632                     ((lum->lmm_stripe_size == 0 &&
633                       lum->lmm_stripe_offset ==
634                       (typeof(lum->lmm_stripe_offset))(-1) &&
635                       lum->lmm_stripe_count == 0) ||
636                     /* lmm_stripe_size == -1 is deprecated in 1.4.6 */
637                     lum->lmm_stripe_size ==
638                     (typeof(lum->lmm_stripe_size))(-1))){
639                         rc = fsfilt_set_md(obd, inode, handle, NULL, 0, "lov");
640                         if (rc)
641                                 GOTO(cleanup, rc);
642                 } else {
643                         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
644                                            mds->mds_osc_exp, 0,
645                                            &lsm, rec->ur_eadata);
646                         if (rc)
647                                 GOTO(cleanup, rc);
648
649                         obd_free_memmd(mds->mds_osc_exp, &lsm);
650
651                         rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
652                                            rec->ur_eadatalen, "lov");
653                         if (rc)
654                                 GOTO(cleanup, rc);
655                 }
656         }
657
658         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
659         mds_pack_inode2fid(&body->fid1, inode);
660         mds_pack_inode2body(body, inode);
661
662         /* don't return OST-specific attributes if we didn't just set them. */
663         if (ia_valid & ATTR_SIZE)
664                 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
665         if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
666                 body->valid |= OBD_MD_FLMTIME;
667         if (ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
668                 body->valid |= OBD_MD_FLATIME;
669
670         if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_osc_obd)) {
671                 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
672                           rec->ur_eadatalen);
673                 if (mlcd) {
674                         mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
675                                 rec->ur_eadatalen;
676                         mlcd->mlcd_eadatalen = rec->ur_eadatalen;
677                         mlcd->mlcd_cookielen = rec->ur_cookielen;
678                         mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
679                                 mlcd->mlcd_cookielen;
680                         memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
681                                mlcd->mlcd_cookielen);
682                         memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
683                                mlcd->mlcd_eadatalen);
684                 } else {
685                         CERROR("unable to allocate log cancel data\n");
686                 }
687         }
688         EXIT;
689  cleanup:
690         if (mlcd != NULL)
691                 sync = fsfilt_add_journal_cb(req->rq_export->exp_obd, 0, handle,
692                                              mds_cancel_cookies_cb, mlcd);
693         err = mds_finish_transno(mds, inode, handle, req, rc, 0, sync);
694         /* do mds to ost setattr if needed */
695         if (!rc && !err && lmm_size)
696                 mds_osc_setattr_async(obd, inode->i_ino, inode->i_generation, lmm,
697                                       lmm_size, logcookies, rec->ur_fid1->id,
698                                       rec->ur_fid1->generation, NULL);
699
700         switch (cleanup_phase) {
701         case 2:
702                 OBD_FREE(lmm, mds->mds_max_mdsize);
703                 if (logcookies)
704                         OBD_FREE(logcookies, cookie_size);
705         case 1:
706                 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
707                     rec->ur_eadata != NULL)
708                         UNLOCK_INODE_MUTEX(inode);
709                 l_dput(de);
710                 if (locked) {
711                         if (rc) {
712                                 ldlm_lock_decref(&lockh, LCK_EX);
713                         } else {
714                                 ptlrpc_save_lock (req, &lockh, LCK_EX);
715                         }
716                 }
717         case 0:
718                 break;
719         default:
720                 LBUG();
721         }
722         if (err && !rc)
723                 rc = err;
724
725         req->rq_status = rc;
726
727         /* trigger dqrel/dqacq for original owner and new owner */
728         if (ia_valid & (ATTR_UID | ATTR_GID))
729                 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
730                               FSFILT_OP_SETATTR);
731
732         return 0;
733 }
734
735 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
736                                      struct ptlrpc_request *req)
737 {
738         struct mds_export_data *med = &req->rq_export->exp_mds_data;
739         struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
740         struct dentry *parent, *child;
741         struct mds_body *body;
742
743         mds_req_from_mcd(req, med->med_mcd);
744
745         if (req->rq_status)
746                 return;
747
748         parent = mds_fid2dentry(obd, rec->ur_fid1, NULL);
749         LASSERT(!IS_ERR(parent));
750         child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
751         LASSERT(!IS_ERR(child));
752
753         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
754         mds_pack_inode2fid(&body->fid1, child->d_inode);
755         mds_pack_inode2body(body, child->d_inode);
756
757         l_dput(parent);
758         l_dput(child);
759 }
760
761 static int mds_reint_create(struct mds_update_record *rec, int offset,
762                             struct ptlrpc_request *req,
763                             struct lustre_handle *lh)
764 {
765         struct dentry *dparent = NULL;
766         struct mds_obd *mds = mds_req2mds(req);
767         struct obd_device *obd = req->rq_export->exp_obd;
768         struct dentry *dchild = NULL;
769         struct inode *dir = NULL;
770         void *handle = NULL;
771         struct lustre_handle lockh;
772         int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
773         int created = 0;
774         unsigned int qcids[MAXQUOTAS] = { current->fsuid, current->fsgid };
775         unsigned int qpids[MAXQUOTAS] = { 0, 0 };
776         struct lvfs_dentry_params dp = LVFS_DENTRY_PARAMS_INIT;
777         ENTRY;
778
779         LASSERT(offset == REQ_REC_OFF);
780         offset = REPLY_REC_OFF;
781
782         LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
783                         LUSTRE_MDS_NAME));
784
785         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
786                   rec->ur_fid1->id, rec->ur_fid1->generation,
787                   rec->ur_name, rec->ur_mode);
788
789         MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
790
791         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
792                 GOTO(cleanup, rc = -ESTALE);
793
794         dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_EX, &lockh,
795                                         MDS_INODELOCK_UPDATE);
796         if (IS_ERR(dparent)) {
797                 rc = PTR_ERR(dparent);
798                 if (rc != -ENOENT)
799                         CERROR("parent "LPU64"/%u lookup error %d\n",
800                                rec->ur_fid1->id, rec->ur_fid1->generation, rc);
801                 GOTO(cleanup, rc);
802         }
803         cleanup_phase = 1; /* locked parent dentry */
804         dir = dparent->d_inode;
805         LASSERT(dir);
806
807         ldlm_lock_dump_handle(D_OTHER, &lockh);
808
809         dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
810         if (IS_ERR(dchild)) {
811                 rc = PTR_ERR(dchild);
812                 if (rc != -ENAMETOOLONG)
813                 CERROR("child lookup error %d\n", rc);
814                 GOTO(cleanup, rc);
815         }
816
817         cleanup_phase = 2; /* child dentry */
818
819         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
820
821         if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY) {
822                 if (dchild->d_inode)
823                         GOTO(cleanup, rc = -EEXIST);
824                 GOTO(cleanup, rc = -EROFS);
825         }
826
827         if (dir->i_mode & S_ISGID && S_ISDIR(rec->ur_mode))
828                 rec->ur_mode |= S_ISGID;
829
830         dchild->d_fsdata = (void *)&dp;
831         dp.ldp_inum = (unsigned long)rec->ur_fid2->id;
832         dp.ldp_ptr = req;
833
834         switch (type) {
835         case S_IFREG:{
836                 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
837                 if (IS_ERR(handle))
838                         GOTO(cleanup, rc = PTR_ERR(handle));
839                 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
840                 mds_counter_incr(req->rq_export, LPROC_MDS_MKNOD);
841                 EXIT;
842                 break;
843         }
844         case S_IFDIR:{
845                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
846                 if (IS_ERR(handle))
847                         GOTO(cleanup, rc = PTR_ERR(handle));
848                 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
849                 mds_counter_incr(req->rq_export, LPROC_MDS_MKDIR);
850                 EXIT;
851                 break;
852         }
853         case S_IFLNK:{
854                 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
855                 if (IS_ERR(handle))
856                         GOTO(cleanup, rc = PTR_ERR(handle));
857                 if (rec->ur_tgt == NULL)        /* no target supplied */
858                         rc = -EINVAL;           /* -EPROTO? */
859                 else
860                         rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
861                 mds_counter_incr(req->rq_export, LPROC_MDS_MKNOD);
862                 EXIT;
863                 break;
864         }
865         case S_IFCHR:
866         case S_IFBLK:
867         case S_IFIFO:
868         case S_IFSOCK:{
869                 int rdev = rec->ur_rdev;
870                 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
871                 if (IS_ERR(handle))
872                         GOTO(cleanup, rc = PTR_ERR(handle));
873                 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
874                 mds_counter_incr(req->rq_export, LPROC_MDS_MKNOD);
875                 EXIT;
876                 break;
877         }
878         default:
879                 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
880                 dchild->d_fsdata = NULL;
881                 GOTO(cleanup, rc = -EINVAL);
882         }
883
884         /* In case we stored the desired inum in here, we want to clean up. */
885         if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
886                 dchild->d_fsdata = NULL;
887
888         if (rc) {
889                 CDEBUG(D_INODE, "error during create: %d\n", rc);
890                 GOTO(cleanup, rc);
891         } else {
892                 struct iattr iattr;
893                 struct inode *inode = dchild->d_inode;
894                 struct mds_body *body;
895
896                 created = 1;
897                 LTIME_S(iattr.ia_atime) = rec->ur_time;
898                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
899                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
900                 iattr.ia_uid = current->fsuid;  /* set by push_ctxt already */
901                 if (dir->i_mode & S_ISGID)
902                         iattr.ia_gid = dir->i_gid;
903                 else
904                         iattr.ia_gid = current->fsgid;
905                 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
906                         ATTR_MTIME | ATTR_CTIME;
907
908                 if (rec->ur_fid2->id) {
909                         LASSERT(rec->ur_fid2->id == inode->i_ino);
910                         inode->i_generation = rec->ur_fid2->generation;
911                         /* Dirtied and committed by the upcoming setattr. */
912                         CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
913                                inode->i_ino, inode->i_generation);
914                 } else {
915                         CDEBUG(D_INODE, "created ino %lu with gen %x\n",
916                                inode->i_ino, inode->i_generation);
917                 }
918
919                 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
920                 if (rc)
921                         CERROR("error on child setattr: rc = %d\n", rc);
922
923                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
924                 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
925                 if (rc)
926                         CERROR("error on parent setattr: rc = %d\n", rc);
927
928                 if (S_ISDIR(inode->i_mode)) {
929                         struct lov_mds_md lmm;
930                         int lmm_size = sizeof(lmm);
931                         rc = mds_get_md(obd, dir, &lmm, &lmm_size, 1);
932                         if (rc > 0) {
933                                 LOCK_INODE_MUTEX(inode);
934                                 rc = fsfilt_set_md(obd, inode, handle,
935                                                    &lmm, lmm_size, "lov");
936                                 UNLOCK_INODE_MUTEX(inode);
937                         }
938                         if (rc)
939                                 CERROR("error on copy stripe info: rc = %d\n",
940                                         rc);
941                 }
942
943                 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
944                 mds_pack_inode2fid(&body->fid1, inode);
945                 mds_pack_inode2body(body, inode);
946         }
947         EXIT;
948
949 cleanup:
950         err = mds_finish_transno(mds, dir, handle, req, rc, 0, 0);
951
952         if (rc && created) {
953                 /* Destroy the file we just created.  This should not need
954                  * extra journal credits, as we have already modified all of
955                  * the blocks needed in order to create the file in the first
956                  * place.
957                  */
958                 switch (type) {
959                 case S_IFDIR:
960                         err = vfs_rmdir(dir, dchild);
961                         if (err)
962                                 CERROR("rmdir in error path: %d\n", err);
963                         break;
964                 default:
965                         err = vfs_unlink(dir, dchild);
966                         if (err)
967                                 CERROR("unlink in error path: %d\n", err);
968                         break;
969                 }
970         } else if (created) {
971                 /* The inode we were allocated may have just been freed
972                  * by an unlink operation.  We take this lock to
973                  * synchronize against the matching reply-ack-lock taken
974                  * in unlink, to avoid replay problems if this reply
975                  * makes it out to the client but the unlink's does not.
976                  * See bug 2029 for more detail.*/
977                 mds_lock_new_child(obd, dchild->d_inode, NULL);
978                 /* save uid/gid of create inode and parent */
979                 qpids[USRQUOTA] = dir->i_uid;
980                 qpids[GRPQUOTA] = dir->i_gid;
981         } else {
982                 rc = err;
983         }
984
985         switch (cleanup_phase) {
986         case 2: /* child dentry */
987                 l_dput(dchild);
988         case 1: /* locked parent dentry */
989                 if (rc) {
990                         ldlm_lock_decref(&lockh, LCK_EX);
991                 } else {
992                         ptlrpc_save_lock (req, &lockh, LCK_EX);
993                 }
994                 l_dput(dparent);
995         case 0:
996                 break;
997         default:
998                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
999                 LBUG();
1000         }
1001         req->rq_status = rc;
1002
1003         /* trigger dqacq on the owner of child and parent */
1004         lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1005                       FSFILT_OP_CREATE);
1006         return 0;
1007 }
1008
1009 int res_gt(const struct ldlm_res_id *res1, const struct ldlm_res_id *res2,
1010            ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1011 {
1012         int i;
1013
1014         for (i = 0; i < RES_NAME_SIZE; i++) {
1015                 /* return 1 here, because enqueue_ordered will skip resources
1016                  * of all zeroes if they're sorted to the end of the list. */
1017                 if (res1->name[i] == 0 && res2->name[i] != 0)
1018                         return 1;
1019                 if (res2->name[i] == 0 && res1->name[i] != 0)
1020                         return 0;
1021
1022                 if (res1->name[i] > res2->name[i])
1023                         return 1;
1024                 if (res1->name[i] < res2->name[i])
1025                         return 0;
1026         }
1027         if (!p1 || !p2)
1028                 return 0;
1029         if (memcmp(p1, p2, sizeof(*p1)) < 0)
1030                 return 1;
1031         return 0;
1032 }
1033
1034 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1035  * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1036  * because they take the place of local semaphores.
1037  *
1038  * One or two locks are taken in numerical order.  A res_id->name[0] of 0 means
1039  * no lock is taken for that res_id.  Must be at least one non-zero res_id. */
1040 int enqueue_ordered_locks(struct obd_device *obd,
1041                           const struct ldlm_res_id *p1_res_id,
1042                           struct lustre_handle *p1_lockh, int p1_lock_mode,
1043                           ldlm_policy_data_t *p1_policy,
1044                           const struct ldlm_res_id *p2_res_id,
1045                           struct lustre_handle *p2_lockh, int p2_lock_mode,
1046                           ldlm_policy_data_t *p2_policy)
1047 {
1048         const struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1049         struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1050         int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1051         ldlm_policy_data_t *policies[2] = {p1_policy, p2_policy};
1052         int rc, flags;
1053         ENTRY;
1054
1055         LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1056
1057         CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1058                res_id[0]->name[0], res_id[1]->name[0]);
1059
1060         if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1061                 handles[1] = p1_lockh;
1062                 handles[0] = p2_lockh;
1063                 res_id[1] = p1_res_id;
1064                 res_id[0] = p2_res_id;
1065                 lock_modes[1] = p1_lock_mode;
1066                 lock_modes[0] = p2_lock_mode;
1067                 policies[1] = p1_policy;
1068                 policies[0] = p2_policy;
1069         }
1070
1071         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1072                res_id[0]->name[0], res_id[1]->name[0]);
1073
1074         flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1075         rc = ldlm_cli_enqueue_local(obd->obd_namespace, res_id[0],
1076                                     LDLM_IBITS, policies[0], lock_modes[0],
1077                                     &flags, ldlm_blocking_ast,
1078                                     ldlm_completion_ast, NULL, NULL, 0,
1079                                     NULL, handles[0]);
1080         if (rc != ELDLM_OK)
1081                 RETURN(-EIO);
1082         ldlm_lock_dump_handle(D_OTHER, handles[0]);
1083
1084         if (memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) == 0 &&
1085             (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1086                 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1087                 ldlm_lock_addref(handles[1], lock_modes[1]);
1088         } else if (res_id[1]->name[0] != 0) {
1089                 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1090                 rc = ldlm_cli_enqueue_local(obd->obd_namespace, res_id[1],
1091                                             LDLM_IBITS, policies[1],
1092                                             lock_modes[1], &flags,
1093                                             ldlm_blocking_ast,
1094                                             ldlm_completion_ast, NULL, NULL,
1095                                             0, NULL, handles[1]);
1096                 if (rc != ELDLM_OK) {
1097                         ldlm_lock_decref(handles[0], lock_modes[0]);
1098                         RETURN(-EIO);
1099                 }
1100                 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1101         }
1102
1103         RETURN(0);
1104 }
1105
1106 static inline int res_eq(const struct ldlm_res_id *res1,
1107                          const struct ldlm_res_id *res2)
1108 {
1109         return !memcmp(res1, res2, sizeof(*res1));
1110 }
1111
1112 static inline void
1113 try_to_aggregate_locks(const struct ldlm_res_id *res1, ldlm_policy_data_t *p1,
1114                        const struct ldlm_res_id *res2, ldlm_policy_data_t *p2)
1115 {
1116         if (!res_eq(res1, res2))
1117                 return;
1118         /* XXX: any additional inodebits (to current LOOKUP and UPDATE)
1119          * should be taken with great care here */
1120         p1->l_inodebits.bits |= p2->l_inodebits.bits;
1121 }
1122
1123 int enqueue_4ordered_locks(struct obd_device *obd,
1124                            const struct ldlm_res_id *p1_res_id,
1125                            struct lustre_handle *p1_lockh, int p1_lock_mode,
1126                            ldlm_policy_data_t *p1_policy,
1127                            const struct ldlm_res_id *p2_res_id,
1128                            struct lustre_handle *p2_lockh, int p2_lock_mode,
1129                            ldlm_policy_data_t *p2_policy,
1130                            const struct ldlm_res_id *c1_res_id,
1131                            struct lustre_handle *c1_lockh, int c1_lock_mode,
1132                            ldlm_policy_data_t *c1_policy,
1133                            const struct ldlm_res_id *c2_res_id,
1134                            struct lustre_handle *c2_lockh, int c2_lock_mode,
1135                            ldlm_policy_data_t *c2_policy)
1136 {
1137         const struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1138                                                 c1_res_id, c2_res_id };
1139         struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1140                                                  c1_lockh, c2_lockh };
1141         int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1142                               c1_lock_mode, c2_lock_mode };
1143         ldlm_policy_data_t *policies[5] = {p1_policy, p2_policy,
1144                                            c1_policy, c2_policy};
1145         int rc, i, j, sorted, flags;
1146         ENTRY;
1147
1148         CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1149                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1150                res_id[3]->name[0]);
1151
1152         /* simple insertion sort - we have at most 4 elements */
1153         for (i = 1; i < 4; i++) {
1154                 j = i - 1;
1155                 dlm_handles[4] = dlm_handles[i];
1156                 res_id[4] = res_id[i];
1157                 lock_modes[4] = lock_modes[i];
1158                 policies[4] = policies[i];
1159
1160                 sorted = 0;
1161                 do {
1162                         if (res_gt(res_id[j], res_id[4], policies[j],
1163                                    policies[4])) {
1164                                 dlm_handles[j + 1] = dlm_handles[j];
1165                                 res_id[j + 1] = res_id[j];
1166                                 lock_modes[j + 1] = lock_modes[j];
1167                                 policies[j + 1] = policies[j];
1168                                 j--;
1169                         } else {
1170                                 sorted = 1;
1171                         }
1172                 } while (j >= 0 && !sorted);
1173
1174                 dlm_handles[j + 1] = dlm_handles[4];
1175                 res_id[j + 1] = res_id[4];
1176                 lock_modes[j + 1] = lock_modes[4];
1177                 policies[j + 1] = policies[4];
1178         }
1179
1180         CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1181                res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1182                res_id[3]->name[0]);
1183
1184         /* XXX we could send ASTs on all these locks first before blocking? */
1185         for (i = 0; i < 4; i++) {
1186                 flags = LDLM_FL_ATOMIC_CB;
1187                 if (res_id[i]->name[0] == 0)
1188                         break;
1189                 if (i && res_eq(res_id[i], res_id[i-1])) {
1190                         memcpy(dlm_handles[i], dlm_handles[i-1],
1191                                sizeof(*(dlm_handles[i])));
1192                         ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1193                 } else {
1194                         /* we need to enqueue locks with different inodebits
1195                          * at once, because otherwise concurrent thread can
1196                          * hit the windown between these two locks and we'll
1197                          * get to deadlock. see bug 10360. note also, that it
1198                          * is impossible to have >2 equal res. */
1199                         if (i < 3)
1200                                 try_to_aggregate_locks(res_id[i], policies[i],
1201                                                        res_id[i+1], policies[i+1]);
1202                         rc = ldlm_cli_enqueue_local(obd->obd_namespace,
1203                                                     res_id[i], LDLM_IBITS,
1204                                                     policies[i], lock_modes[i],
1205                                                     &flags, ldlm_blocking_ast,
1206                                                     ldlm_completion_ast, NULL,
1207                                                     NULL, 0, NULL,
1208                                                     dlm_handles[i]);
1209                         if (rc != ELDLM_OK)
1210                                 GOTO(out_err, rc = -EIO);
1211                         ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1212                 }
1213         }
1214
1215         RETURN(0);
1216 out_err:
1217         while (i-- > 0)
1218                 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1219
1220         return rc;
1221 }
1222
1223 /* In the unlikely case that the child changed while we were waiting
1224  * on the lock, we need to drop the lock on the old child and either:
1225  * - if the child has a lower resource name, then we have to also
1226  *   drop the parent lock and regain the locks in the right order
1227  * - in the rename case, if the child has a lower resource name than one of
1228  *   the other parent/child resources (maxres) we also need to reget the locks
1229  * - if the child has a higher resource name (this is the common case)
1230  *   we can just get the lock on the new child (still in lock order)
1231  *
1232  * Returns 0 if the child did not change or if it changed but could be locked.
1233  * Returns 1 if the child changed and we need to re-lock (no locks held).
1234  * Returns -ve error with a valid dchild (no locks held). */
1235 static int mds_verify_child(struct obd_device *obd,
1236                             const struct ldlm_res_id *parent_res_id,
1237                             struct lustre_handle *parent_lockh,
1238                             struct dentry *dparent, int parent_mode,
1239                             struct ldlm_res_id *child_res_id,
1240                             struct lustre_handle *child_lockh,
1241                             struct dentry **dchildp, int child_mode,
1242                             ldlm_policy_data_t *child_policy,
1243                             const char *name, int namelen,
1244                             const struct ldlm_res_id *maxres)
1245 {
1246         struct dentry *vchild, *dchild = *dchildp;
1247         int rc = 0, cleanup_phase = 2; /* parent, child locks */
1248         ENTRY;
1249
1250         vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1251         if (IS_ERR(vchild))
1252                 GOTO(cleanup, rc = PTR_ERR(vchild));
1253
1254         if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1255                    (vchild->d_inode != NULL &&
1256                     child_res_id->name[0] == vchild->d_inode->i_ino &&
1257                     child_res_id->name[1] == vchild->d_inode->i_generation))) {
1258                 if (dchild != NULL)
1259                         l_dput(dchild);
1260                 *dchildp = vchild;
1261
1262                 RETURN(0);
1263         }
1264
1265         CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1266                vchild->d_inode, dchild ? dchild->d_inode : 0,
1267                vchild->d_inode ? vchild->d_inode->i_ino : 0,
1268                child_res_id->name[0]);
1269         if (child_res_id->name[0] != 0)
1270                 ldlm_lock_decref(child_lockh, child_mode);
1271         if (dchild)
1272                 l_dput(dchild);
1273
1274         cleanup_phase = 1; /* parent lock only */
1275         *dchildp = dchild = vchild;
1276
1277         if (dchild->d_inode) {
1278                 int flags = LDLM_FL_ATOMIC_CB;
1279                 child_res_id->name[0] = dchild->d_inode->i_ino;
1280                 child_res_id->name[1] = dchild->d_inode->i_generation;
1281
1282                 /* Make sure that we don't try to re-enqueue a lock on the
1283                  * same resource if it happens that the source is renamed to
1284                  * the target by another thread (bug 9974, thanks racer :-) */
1285                 if (!res_gt(child_res_id, parent_res_id, NULL, NULL) ||
1286                     !res_gt(child_res_id, maxres, NULL, NULL)) {
1287                         CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1288                                child_res_id->name[0], parent_res_id->name[0],
1289                                maxres->name[0]);
1290                         GOTO(cleanup, rc = 1);
1291                 }
1292
1293                 rc = ldlm_cli_enqueue_local(obd->obd_namespace, child_res_id,
1294                                             LDLM_IBITS, child_policy,
1295                                             child_mode, &flags,
1296                                             ldlm_blocking_ast,
1297                                             ldlm_completion_ast, NULL,
1298                                             NULL, 0, NULL, child_lockh);
1299                 if (rc != ELDLM_OK)
1300                         GOTO(cleanup, rc = -EIO);
1301         } else {
1302                 memset(child_res_id, 0, sizeof(*child_res_id));
1303         }
1304
1305         EXIT;
1306 cleanup:
1307         if (rc) {
1308                 switch(cleanup_phase) {
1309                 case 2:
1310                         if (child_res_id->name[0] != 0)
1311                                 ldlm_lock_decref(child_lockh, child_mode);
1312                 case 1:
1313                         ldlm_lock_decref(parent_lockh, parent_mode);
1314                 }
1315         }
1316         return rc;
1317 }
1318
1319 #define INODE_CTIME_AGE (10)
1320 #define INODE_CTIME_OLD(inode) (LTIME_S(inode->i_ctime) +               \
1321                                 INODE_CTIME_AGE < CURRENT_SECONDS)
1322
1323 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1324                                 struct ll_fid *fid,
1325                                 struct lustre_handle *parent_lockh,
1326                                 struct dentry **dparentp, int parent_mode,
1327                                 __u64 parent_lockpart,
1328                                 char *name, int namelen,
1329                                 struct lustre_handle *child_lockh,
1330                                 struct dentry **dchildp, int child_mode,
1331                                 __u64 child_lockpart)
1332 {
1333         struct ldlm_res_id child_res_id = { .name = {0} };
1334         struct ldlm_res_id parent_res_id = { .name = {0} };
1335         ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1336         ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1337         struct inode *inode;
1338         int rc = 0, cleanup_phase = 0;
1339         ENTRY;
1340
1341         /* Step 1: Lookup parent */
1342         *dparentp = mds_fid2dentry(mds, fid, NULL);
1343         if (IS_ERR(*dparentp)) {
1344                 rc = PTR_ERR(*dparentp);
1345                 *dparentp = NULL;
1346                 RETURN(rc);
1347         }
1348
1349         CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1350                (*dparentp)->d_inode->i_ino, name);
1351
1352         parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
1353         parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
1354
1355         cleanup_phase = 1; /* parent dentry */
1356
1357         /* Step 2: Lookup child (without DLM lock, to get resource name) */
1358         *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1359         if (IS_ERR(*dchildp)) {
1360                 rc = PTR_ERR(*dchildp);
1361                 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1362                 GOTO(cleanup, rc);
1363         }
1364
1365         cleanup_phase = 2; /* child dentry */
1366         inode = (*dchildp)->d_inode;
1367         if (inode != NULL) {
1368                 if (is_bad_inode(inode)) {
1369                         CERROR("bad inode returned %lu/%u\n",
1370                                inode->i_ino, inode->i_generation);
1371                         GOTO(cleanup, rc = -ENOENT);
1372                 }
1373                 inode = igrab(inode);
1374         }
1375         if (inode == NULL)
1376                 goto retry_locks;
1377
1378         child_res_id.name[0] = inode->i_ino;
1379         child_res_id.name[1] = inode->i_generation;
1380
1381         /* If we want a LCK_CR for a directory, and this directory has not been
1382            changed for some time, we return not only a LOOKUP lock, but also an
1383            UPDATE lock to have negative dentry starts working for this dir.
1384            Also we apply same logic to non-directories. If the file is rarely
1385            changed - we return both locks and this might save us RPC on
1386            later STAT. */
1387         if ((child_mode & (LCK_CR|LCK_PR|LCK_CW)) && INODE_CTIME_OLD(inode))
1388                 child_policy.l_inodebits.bits |= MDS_INODELOCK_UPDATE;
1389
1390         iput(inode);
1391
1392 retry_locks:
1393         cleanup_phase = 2; /* child dentry */
1394
1395         /* Step 3: Lock parent and child in resource order.  If child doesn't
1396          *         exist, we still have to lock the parent and re-lookup. */
1397         rc = enqueue_ordered_locks(obd,&parent_res_id,parent_lockh,parent_mode,
1398                                    &parent_policy,
1399                                    &child_res_id, child_lockh, child_mode,
1400                                    &child_policy);
1401         if (rc)
1402                 GOTO(cleanup, rc);
1403
1404         if (!(*dchildp)->d_inode)
1405                 cleanup_phase = 3; /* parent lock */
1406         else
1407                 cleanup_phase = 4; /* child lock */
1408
1409         /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1410         rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1411                               parent_mode, &child_res_id, child_lockh, dchildp,
1412                               child_mode,&child_policy, name, namelen, &parent_res_id);
1413         if (rc > 0)
1414                 goto retry_locks;
1415         if (rc < 0) {
1416                 cleanup_phase = 2;
1417                 GOTO(cleanup, rc);
1418         }
1419
1420 cleanup:
1421         if (rc) {
1422                 switch (cleanup_phase) {
1423                 case 4:
1424                         ldlm_lock_decref(child_lockh, child_mode);
1425                 case 3:
1426                         ldlm_lock_decref(parent_lockh, parent_mode);
1427                 case 2:
1428                         l_dput(*dchildp);
1429                 case 1:
1430                         l_dput(*dparentp);
1431                 default: ;
1432                 }
1433         }
1434         return rc;
1435 }
1436
1437 void mds_reconstruct_generic(struct ptlrpc_request *req)
1438 {
1439         struct mds_export_data *med = &req->rq_export->exp_mds_data;
1440
1441         mds_req_from_mcd(req, med->med_mcd);
1442 }
1443
1444 /* If we are unlinking an open file/dir (i.e. creating an orphan) then
1445  * we instead link the inode into the PENDING directory until it is
1446  * finally released.  We can't simply call mds_reint_rename() or some
1447  * part thereof, because we don't have the inode to check for link
1448  * count/open status until after it is locked.
1449  *
1450  * For lock ordering, caller must get child->i_mutex first, then
1451  * pending->i_mutex before starting journal transaction.
1452  *
1453  * returns 1 on success
1454  * returns 0 if we lost a race and didn't make a new link
1455  * returns negative on error
1456  */
1457 static int mds_orphan_add_link(struct mds_update_record *rec,
1458                                struct obd_device *obd, struct dentry *dentry)
1459 {
1460         struct mds_obd *mds = &obd->u.mds;
1461         struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1462         struct inode *inode = dentry->d_inode;
1463         struct dentry *pending_child;
1464         char fidname[LL_FID_NAMELEN];
1465         int fidlen = 0, rc, mode;
1466         ENTRY;
1467
1468         LASSERT(inode != NULL);
1469         LASSERT(!mds_inode_is_orphan(inode));
1470 #ifndef HAVE_I_ALLOC_SEM
1471         LASSERT(TRYLOCK_INODE_MUTEX(inode) == 0);
1472 #endif
1473         LASSERT(TRYLOCK_INODE_MUTEX(pending_dir) == 0);
1474
1475         fidlen = ll_fid2str(fidname, inode->i_ino, inode->i_generation);
1476
1477         CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1478                mds_orphan_open_count(inode), inode->i_nlink,
1479                S_ISDIR(inode->i_mode) ? "dir" :
1480                 S_ISREG(inode->i_mode) ? "file" : "other",rec->ur_name,fidname);
1481
1482         if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1483                 RETURN(0);
1484
1485         pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
1486         if (IS_ERR(pending_child))
1487                 RETURN(PTR_ERR(pending_child));
1488
1489         if (pending_child->d_inode != NULL) {
1490                 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1491                 LASSERT(pending_child->d_inode == inode);
1492                 GOTO(out_dput, rc = 0);
1493         }
1494
1495         /* link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG
1496          * for linking and return real mode back then -bzzz */
1497         mode = inode->i_mode;
1498         inode->i_mode = S_IFREG;
1499         rc = vfs_link(dentry, pending_dir, pending_child);
1500         if (rc)
1501                 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1502                        rec->ur_name, rc);
1503         else
1504                 mds_inode_set_orphan(inode);
1505
1506         /* return mode and correct i_nlink if inode is directory */
1507         inode->i_mode = mode;
1508         LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1509                  S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1510                  inode->i_nlink);
1511         if (S_ISDIR(mode)) {
1512                 inode->i_nlink++;
1513                 pending_dir->i_nlink++;
1514                 mark_inode_dirty(inode);
1515                 mark_inode_dirty(pending_dir);
1516         }
1517
1518         GOTO(out_dput, rc = 1);
1519 out_dput:
1520         l_dput(pending_child);
1521         RETURN(rc);
1522 }
1523
1524 int mds_get_cookie_size(struct obd_device *obd, struct lov_mds_md *lmm)
1525 {
1526         int count = le32_to_cpu(lmm->lmm_stripe_count);
1527         int real_csize = count * sizeof(struct llog_cookie);
1528         return real_csize;
1529 }
1530
1531 void mds_shrink_reply(struct obd_device *obd, struct ptlrpc_request *req,
1532                       struct mds_body *body, int md_off)
1533 {
1534         int cookie_size = 0, md_size = 0;
1535
1536         if (body && body->valid & OBD_MD_FLEASIZE) {
1537                 md_size = body->eadatasize;
1538         }
1539         if (body && body->valid & OBD_MD_FLCOOKIE) {
1540                 LASSERT(body->valid & OBD_MD_FLEASIZE);
1541                 cookie_size = mds_get_cookie_size(obd, lustre_msg_buf(
1542                                                   req->rq_repmsg, md_off, 0));
1543         }
1544
1545         CDEBUG(D_INFO, "Shrink to md_size %d cookie_size %d \n", md_size,
1546                cookie_size);
1547
1548         lustre_shrink_reply(req, md_off, md_size, 1);
1549
1550         lustre_shrink_reply(req, md_off + (md_size > 0), cookie_size, 0);
1551 }
1552
1553 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1554                             struct ptlrpc_request *req,
1555                             struct lustre_handle *lh)
1556 {
1557         struct dentry *dparent = NULL, *dchild;
1558         struct mds_obd *mds = mds_req2mds(req);
1559         struct obd_device *obd = req->rq_export->exp_obd;
1560         struct mds_body *body = NULL;
1561         struct inode *child_inode = NULL;
1562         struct lustre_handle parent_lockh, child_lockh, child_reuse_lockh;
1563         void *handle = NULL;
1564         int rc = 0, cleanup_phase = 0;
1565         unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1566         unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1567         ENTRY;
1568
1569         LASSERT(offset == REQ_REC_OFF); /*  || offset == DLM_INTENT_REC_OFF); */
1570         offset = REPLY_REC_OFF;
1571
1572         DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1573                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1574
1575         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1576
1577         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1578                 GOTO(cleanup, rc = -ENOENT);
1579
1580         rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1581                                          &parent_lockh, &dparent, LCK_EX,
1582                                          MDS_INODELOCK_UPDATE,
1583                                          rec->ur_name, rec->ur_namelen,
1584                                          &child_lockh, &dchild, LCK_EX,
1585                                          MDS_INODELOCK_FULL);
1586         if (rc)
1587                 GOTO(cleanup, rc);
1588
1589         cleanup_phase = 1; /* dchild, dparent, locks */
1590
1591         dget(dchild);
1592         child_inode = dchild->d_inode;
1593         if (child_inode == NULL) {
1594                 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1595                        dparent->d_inode->i_ino, rec->ur_name);
1596                 GOTO(cleanup, rc = -ENOENT);
1597         }
1598
1599         /* save uid/gid for quota acquire/release */
1600         qcids[USRQUOTA] = child_inode->i_uid;
1601         qcids[GRPQUOTA] = child_inode->i_gid;
1602         qpids[USRQUOTA] = dparent->d_inode->i_uid;
1603         qpids[GRPQUOTA] = dparent->d_inode->i_gid;
1604
1605         cleanup_phase = 2; /* dchild has a lock */
1606
1607         /* We have to do these checks ourselves, in case we are making an
1608          * orphan.  The client tells us whether rmdir() or unlink() was called,
1609          * so we need to return appropriate errors (bug 72). */
1610         if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1611                 if (!S_ISDIR(child_inode->i_mode))
1612                         GOTO(cleanup, rc = -ENOTDIR);
1613         } else {
1614                 if (S_ISDIR(child_inode->i_mode))
1615                         GOTO(cleanup, rc = -EISDIR);
1616         }
1617
1618         /* Check for EROFS after we check ENODENT, ENOTDIR, and EISDIR */
1619         if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
1620                 GOTO(cleanup, rc = -EROFS);
1621
1622         /* Step 3: Get a lock on the ino to sync with creation WRT inode
1623          * reuse (see bug 2029). */
1624         rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1625         if (rc != ELDLM_OK)
1626                 GOTO(cleanup, rc);
1627
1628         cleanup_phase = 3; /* child inum lock */
1629
1630         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1631
1632         /* ldlm_reply in buf[0] if called via intent */
1633         if (offset == DLM_INTENT_REC_OFF)
1634                 offset = DLM_REPLY_REC_OFF;
1635
1636         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
1637         LASSERT(body != NULL);
1638
1639         /* child orphan sem protects orphan_dec_test && is_orphan race */
1640         MDS_DOWN_READ_ORPHAN_SEM(child_inode);
1641         cleanup_phase = 4; /* MDS_UP_READ_ORPHAN_SEM(new_inode) when finished */
1642
1643         /* If this is potentially the last reference to this inode, get the
1644          * OBD EA data first so the client can destroy OST objects.  We
1645          * only do the object removal later if no open files/links remain. */
1646         if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
1647             child_inode->i_nlink == 1) {
1648                 if (mds_orphan_open_count(child_inode) > 0) {
1649                         /* need to lock pending_dir before transaction */
1650                         LOCK_INODE_MUTEX(mds->mds_pending_dir->d_inode);
1651                         cleanup_phase = 5; /* UNLOCK_INODE_MUTEX(mds->mds_pending_dir->d_inode); */
1652                 } else if (S_ISREG(child_inode->i_mode)) {
1653                         mds_pack_inode2fid(&body->fid1, child_inode);
1654                         mds_pack_inode2body(body, child_inode);
1655                         mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1656                                     child_inode, MDS_PACK_MD_LOCK);
1657                 }
1658         }
1659
1660         /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1661         switch (child_inode->i_mode & S_IFMT) {
1662         case S_IFDIR:
1663                 /* Drop any lingering child directories before we start our
1664                  * transaction, to avoid doing multiple inode dirty/delete
1665                  * in our compound transaction (bug 1321). */
1666                 shrink_dcache_parent(dchild);
1667                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1668                                       NULL);
1669                 if (IS_ERR(handle))
1670                         GOTO(cleanup, rc = PTR_ERR(handle));
1671                 rc = vfs_rmdir(dparent->d_inode, dchild);
1672                 mds_counter_incr(req->rq_export, LPROC_MDS_RMDIR);
1673                 break;
1674         case S_IFREG: {
1675                 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
1676                                                         offset + 1, 0);
1677                 handle = fsfilt_start_log(obd, dparent->d_inode,
1678                                           FSFILT_OP_UNLINK, NULL,
1679                                           le32_to_cpu(lmm->lmm_stripe_count));
1680                 if (IS_ERR(handle))
1681                         GOTO(cleanup, rc = PTR_ERR(handle));
1682                 rc = vfs_unlink(dparent->d_inode, dchild);
1683                 mds_counter_incr(req->rq_export, LPROC_MDS_UNLINK);
1684                 break;
1685         }
1686         case S_IFLNK:
1687         case S_IFCHR:
1688         case S_IFBLK:
1689         case S_IFIFO:
1690         case S_IFSOCK:
1691                 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1692                                       NULL);
1693                 if (IS_ERR(handle))
1694                         GOTO(cleanup, rc = PTR_ERR(handle));
1695                 rc = vfs_unlink(dparent->d_inode, dchild);
1696                 mds_counter_incr(req->rq_export, LPROC_MDS_UNLINK);
1697                 break;
1698         default:
1699                 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1700                        rec->ur_name);
1701                 LBUG();
1702                 GOTO(cleanup, rc = -EINVAL);
1703         }
1704
1705         if (rc == 0 && child_inode->i_nlink == 0) {
1706                 if (mds_orphan_open_count(child_inode) > 0)
1707                         rc = mds_orphan_add_link(rec, obd, dchild);
1708
1709                 if (rc == 1)
1710                         GOTO(cleanup, rc = 0);
1711
1712                 if (!S_ISREG(child_inode->i_mode))
1713                         GOTO(cleanup, rc);
1714
1715                 if (!(body->valid & OBD_MD_FLEASIZE)) {
1716                         body->valid |=(OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1717                                        OBD_MD_FLATIME | OBD_MD_FLMTIME);
1718                 } else if (mds_log_op_unlink(obd,
1719                                 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
1720                                 lustre_msg_buflen(req->rq_repmsg, offset + 1),
1721                                 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
1722                                 lustre_msg_buflen(req->rq_repmsg, offset+2)) >
1723                            0) {
1724                         body->valid |= OBD_MD_FLCOOKIE;
1725                 }
1726         }
1727
1728         GOTO(cleanup, rc);
1729 cleanup:
1730         if (rc == 0) {
1731                 struct iattr iattr;
1732                 int err;
1733
1734                 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1735                 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1736                 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1737
1738                 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1739                 if (err)
1740                         CERROR("error on parent setattr: rc = %d\n", err);
1741         }
1742
1743         rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
1744                                 handle, req, rc, 0, 0);
1745         if (!rc)
1746                 (void)obd_set_info_async(mds->mds_osc_exp, strlen("unlinked"),
1747                                          "unlinked", 0, NULL, NULL);
1748         switch(cleanup_phase) {
1749         case 5: /* pending_dir semaphore */
1750                 UNLOCK_INODE_MUTEX(mds->mds_pending_dir->d_inode);
1751         case 4: /* child inode semaphore */
1752                 MDS_UP_READ_ORPHAN_SEM(child_inode);
1753         case 3: /* child ino-reuse lock */
1754                 if (rc && body != NULL) {
1755                         // Don't unlink the OST objects if the MDS unlink failed
1756                         body->valid = 0;
1757                 }
1758                 if (rc)
1759                         ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1760                 else
1761                         ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
1762         case 2: /* child lock */
1763                 ldlm_lock_decref(&child_lockh, LCK_EX);
1764         case 1: /* child and parent dentry, parent lock */
1765                 if (rc)
1766                         ldlm_lock_decref(&parent_lockh, LCK_EX);
1767                 else
1768                         ptlrpc_save_lock(req, &parent_lockh, LCK_EX);
1769                 l_dput(dchild);
1770                 l_dput(dchild);
1771                 l_dput(dparent);
1772         case 0:
1773                 break;
1774         default:
1775                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1776                 LBUG();
1777         }
1778         req->rq_status = rc;
1779
1780         mds_shrink_reply(obd, req, body, REPLY_REC_OFF + 1);
1781
1782         /* trigger dqrel on the owner of child and parent */
1783         lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc, FSFILT_OP_UNLINK);
1784         return 0;
1785 }
1786
1787 static int mds_reint_link(struct mds_update_record *rec, int offset,
1788                           struct ptlrpc_request *req,
1789                           struct lustre_handle *lh)
1790 {
1791         struct obd_device *obd = req->rq_export->exp_obd;
1792         struct dentry *de_src = NULL;
1793         struct dentry *de_tgt_dir = NULL;
1794         struct dentry *dchild = NULL;
1795         struct mds_obd *mds = mds_req2mds(req);
1796         struct lustre_handle *handle = NULL, tgt_dir_lockh, src_lockh;
1797         struct ldlm_res_id src_res_id = { .name = {0} };
1798         struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
1799         ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
1800         ldlm_policy_data_t tgt_dir_policy =
1801                                        {.l_inodebits = {MDS_INODELOCK_UPDATE}};
1802         int rc = 0, cleanup_phase = 0;
1803         ENTRY;
1804
1805         LASSERT(offset == REQ_REC_OFF);
1806
1807         DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
1808                   rec->ur_fid1->id, rec->ur_fid1->generation,
1809                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
1810         mds_counter_incr(req->rq_export, LPROC_MDS_LINK);
1811
1812         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1813
1814         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1815                 GOTO(cleanup, rc = -ENOENT);
1816
1817         /* Step 1: Lookup the source inode and target directory by FID */
1818         de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1819         if (IS_ERR(de_src))
1820                 GOTO(cleanup, rc = PTR_ERR(de_src));
1821
1822         cleanup_phase = 1; /* source dentry */
1823
1824         de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
1825         if (IS_ERR(de_tgt_dir)) {
1826                 rc = PTR_ERR(de_tgt_dir);
1827                 de_tgt_dir = NULL;
1828                 GOTO(cleanup, rc);
1829         }
1830
1831         cleanup_phase = 2; /* target directory dentry */
1832
1833         CDEBUG(D_INODE, "linking %.*s/%s to inode %lu\n",
1834                de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
1835                de_src->d_inode->i_ino);
1836
1837         /* Step 2: Take the two locks */
1838         src_res_id.name[0] = de_src->d_inode->i_ino;
1839         src_res_id.name[1] = de_src->d_inode->i_generation;
1840         tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
1841         tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
1842
1843         rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
1844                                    &src_policy,
1845                                    &tgt_dir_res_id, &tgt_dir_lockh, LCK_EX,
1846                                    &tgt_dir_policy);
1847         if (rc)
1848                 GOTO(cleanup, rc);
1849
1850         cleanup_phase = 3; /* locks */
1851
1852         if (mds_inode_is_orphan(de_src->d_inode)) {
1853                 CDEBUG(D_INODE, "an attempt to link an orphan inode %lu/%u\n",
1854                        de_src->d_inode->i_ino,
1855                        de_src->d_inode->i_generation);
1856                 GOTO(cleanup, rc = -ENOENT);
1857         }
1858
1859         /* Step 3: Lookup the child */
1860         dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
1861         if (IS_ERR(dchild)) {
1862                 rc = PTR_ERR(dchild);
1863                 if (rc != -EPERM && rc != -EACCES && rc != -ENAMETOOLONG)
1864                         CERROR("child lookup error %d\n", rc);
1865                 GOTO(cleanup, rc);
1866         }
1867
1868         cleanup_phase = 4; /* child dentry */
1869
1870         if (dchild->d_inode) {
1871                 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
1872                        de_tgt_dir->d_inode->i_ino, rec->ur_name);
1873                 rc = -EEXIST;
1874                 GOTO(cleanup, rc);
1875         }
1876
1877         /* Step 4: Do it. */
1878         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1879
1880         if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
1881                 GOTO(cleanup, rc = -EROFS);
1882
1883         handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1884         if (IS_ERR(handle))
1885                 GOTO(cleanup, rc = PTR_ERR(handle));
1886
1887         rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
1888         if (rc && rc != -EPERM && rc != -EACCES)
1889                 CERROR("vfs_link error %d\n", rc);
1890 cleanup:
1891         rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
1892                                 handle, req, rc, 0, 0);
1893         EXIT;
1894
1895         switch (cleanup_phase) {
1896         case 4: /* child dentry */
1897                 l_dput(dchild);
1898         case 3: /* locks */
1899                 if (rc) {
1900                         ldlm_lock_decref(&src_lockh, LCK_EX);
1901                         ldlm_lock_decref(&tgt_dir_lockh, LCK_EX);
1902                 } else {
1903                         ptlrpc_save_lock(req, &src_lockh, LCK_EX);
1904                         ptlrpc_save_lock(req, &tgt_dir_lockh, LCK_EX);
1905                 }
1906         case 2: /* target dentry */
1907                 l_dput(de_tgt_dir);
1908         case 1: /* source dentry */
1909                 l_dput(de_src);
1910         case 0:
1911                 break;
1912         default:
1913                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1914                 LBUG();
1915         }
1916         req->rq_status = rc;
1917         return 0;
1918 }
1919
1920 /* The idea here is that we need to get four locks in the end:
1921  * one on each parent directory, one on each child.  We need to take
1922  * these locks in some kind of order (to avoid deadlocks), and the order
1923  * I selected is "increasing resource number" order.  We need to look up
1924  * the children, however, before we know what the resource number(s) are.
1925  * Thus the following plan:
1926  *
1927  * 1,2. Look up the parents
1928  * 3,4. Look up the children
1929  * 5. Take locks on the parents and children, in order
1930  * 6. Verify that the children haven't changed since they were looked up
1931  *
1932  * If there was a race and the children changed since they were first looked
1933  * up, it is possible that mds_verify_child() will be able to just grab the
1934  * lock on the new child resource (if it has a higher resource than any other)
1935  * but we need to compare against not only its parent, but also against the
1936  * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
1937  *
1938  * We need the fancy igrab() on the child inodes because we aren't holding a
1939  * lock on the parent after the lookup is done, so dentry->d_inode may change
1940  * at any time, and igrab() itself doesn't like getting passed a NULL argument.
1941  */
1942 int mds_get_parents_children_locked(struct obd_device *obd,
1943                                     struct mds_obd *mds,
1944                                     struct ll_fid *p1_fid,
1945                                     struct dentry **de_srcdirp,
1946                                     struct ll_fid *p2_fid,
1947                                     struct dentry **de_tgtdirp,
1948                                     int parent_mode,
1949                                     const char *old_name, int old_len,
1950                                     struct dentry **de_oldp,
1951                                     const char *new_name, int new_len,
1952                                     struct dentry **de_newp,
1953                                     struct lustre_handle *dlm_handles,
1954                                     int child_mode)
1955 {
1956         struct ldlm_res_id p1_res_id = { .name = {0} };
1957         struct ldlm_res_id p2_res_id = { .name = {0} };
1958         struct ldlm_res_id c1_res_id = { .name = {0} };
1959         struct ldlm_res_id c2_res_id = { .name = {0} };
1960         ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
1961         /* Only dentry should disappear, but the inode itself would be
1962            intact otherwise. */
1963         ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
1964         /* If something is going to be replaced, both dentry and inode locks are           needed */
1965         ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
1966         struct ldlm_res_id *maxres_src, *maxres_tgt;
1967         struct inode *inode;
1968         int rc = 0, cleanup_phase = 0;
1969         ENTRY;
1970
1971         /* Step 1: Lookup the source directory */
1972         *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
1973         if (IS_ERR(*de_srcdirp))
1974                 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
1975
1976         cleanup_phase = 1; /* source directory dentry */
1977
1978         p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
1979         p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
1980
1981         /* Step 2: Lookup the target directory */
1982         if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
1983                 *de_tgtdirp = dget(*de_srcdirp);
1984         } else {
1985                 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
1986                 if (IS_ERR(*de_tgtdirp)) {
1987                         rc = PTR_ERR(*de_tgtdirp);
1988                         *de_tgtdirp = NULL;
1989                         GOTO(cleanup, rc);
1990                 }
1991         }
1992
1993         cleanup_phase = 2; /* target directory dentry */
1994
1995         p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
1996         p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
1997
1998         /* Step 3: Lookup the source child entry */
1999         *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
2000         if (IS_ERR(*de_oldp)) {
2001                 rc = PTR_ERR(*de_oldp);
2002                 CDEBUG(D_INODE, "old child lookup error (%.*s): %d\n",
2003                        old_len - 1, old_name, rc);
2004                 GOTO(cleanup, rc);
2005         }
2006
2007         cleanup_phase = 3; /* original name dentry */
2008
2009         inode = (*de_oldp)->d_inode;
2010         if (inode != NULL)
2011                 inode = igrab(inode);
2012         if (inode == NULL)
2013                 GOTO(cleanup, rc = -ENOENT);
2014
2015         c1_res_id.name[0] = inode->i_ino;
2016         c1_res_id.name[1] = inode->i_generation;
2017
2018         iput(inode);
2019
2020         /* Step 4: Lookup the target child entry */
2021         if (!new_name)
2022                 GOTO(retry_locks, rc);
2023         *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
2024         if (IS_ERR(*de_newp)) {
2025                 rc = PTR_ERR(*de_newp);
2026                 if (rc != -ENAMETOOLONG)
2027                 CERROR("new child lookup error (%.*s): %d\n",
2028                        old_len - 1, old_name, rc);
2029                 GOTO(cleanup, rc);
2030         }
2031
2032         cleanup_phase = 4; /* target dentry */
2033
2034         inode = (*de_newp)->d_inode;
2035         if (inode != NULL)
2036                 inode = igrab(inode);
2037         if (inode == NULL)
2038                 goto retry_locks;
2039
2040         c2_res_id.name[0] = inode->i_ino;
2041         c2_res_id.name[1] = inode->i_generation;
2042         iput(inode);
2043
2044 retry_locks:
2045         /* Step 5: Take locks on the parents and child(ren) */
2046         maxres_src = &p1_res_id;
2047         maxres_tgt = &p2_res_id;
2048         cleanup_phase = 4; /* target dentry */
2049
2050         if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id,NULL,NULL))
2051                 maxres_src = &c1_res_id;
2052         if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id,NULL,NULL))
2053                 maxres_tgt = &c2_res_id;
2054
2055         rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
2056                                     &p_policy,
2057                                     &p2_res_id, &dlm_handles[1], parent_mode,
2058                                     &p_policy,
2059                                     &c1_res_id, &dlm_handles[2], child_mode,
2060                                     &c1_policy,
2061                                     &c2_res_id, &dlm_handles[3], child_mode,
2062                                     &c2_policy);
2063         if (rc)
2064                 GOTO(cleanup, rc);
2065
2066         cleanup_phase = 6; /* parent and child(ren) locks */
2067
2068         /* Step 6a: Re-lookup source child to verify it hasn't changed */
2069         rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2070                               parent_mode, &c1_res_id, &dlm_handles[2], de_oldp,
2071                               child_mode, &c1_policy, old_name, old_len,
2072                               maxres_tgt);
2073         if (rc) {
2074                 if (c2_res_id.name[0] != 0)
2075                         ldlm_lock_decref(&dlm_handles[3], child_mode);
2076                 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2077                 cleanup_phase = 4;
2078                 if (rc > 0)
2079                         goto retry_locks;
2080                 GOTO(cleanup, rc);
2081         }
2082
2083         if ((*de_oldp)->d_inode == NULL)
2084                 GOTO(cleanup, rc = -ENOENT);
2085
2086         if (!new_name)
2087                 GOTO(cleanup, rc);
2088         /* Step 6b: Re-lookup target child to verify it hasn't changed */
2089         rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2090                               parent_mode, &c2_res_id, &dlm_handles[3], de_newp,
2091                               child_mode, &c2_policy, new_name, new_len,
2092                               maxres_src);
2093         if (rc) {
2094                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2095                 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2096                 cleanup_phase = 4;
2097                 if (rc > 0)
2098                         goto retry_locks;
2099                 GOTO(cleanup, rc);
2100         }
2101
2102         EXIT;
2103 cleanup:
2104         if (rc) {
2105                 switch (cleanup_phase) {
2106                 case 6: /* child lock(s) */
2107                         if (c2_res_id.name[0] != 0)
2108                                 ldlm_lock_decref(&dlm_handles[3], child_mode);
2109                         if (c1_res_id.name[0] != 0)
2110                                 ldlm_lock_decref(&dlm_handles[2], child_mode);
2111                 case 5: /* parent locks */
2112                         ldlm_lock_decref(&dlm_handles[1], parent_mode);
2113                         ldlm_lock_decref(&dlm_handles[0], parent_mode);
2114                 case 4: /* target dentry */
2115                         l_dput(*de_newp);
2116                 case 3: /* source dentry */
2117                         l_dput(*de_oldp);
2118                 case 2: /* target directory dentry */
2119                         l_dput(*de_tgtdirp);
2120                 case 1: /* source directry dentry */
2121                         l_dput(*de_srcdirp);
2122                 }
2123         }
2124
2125         return rc;
2126 }
2127
2128 static int mds_reint_rename(struct mds_update_record *rec, int offset,
2129                             struct ptlrpc_request *req,
2130                             struct lustre_handle *lockh)
2131 {
2132         struct obd_device *obd = req->rq_export->exp_obd;
2133         struct dentry *de_srcdir = NULL;
2134         struct dentry *de_tgtdir = NULL;
2135         struct dentry *de_old = NULL;
2136         struct dentry *de_new = NULL;
2137         struct inode *old_inode = NULL, *new_inode = NULL;
2138         struct mds_obd *mds = mds_req2mds(req);
2139         struct lustre_handle dlm_handles[4];
2140         struct mds_body *body = NULL;
2141         struct lov_mds_md *lmm = NULL;
2142         int rc = 0, lock_count = 3, cleanup_phase = 0;
2143         void *handle = NULL;
2144         unsigned int qcids[MAXQUOTAS] = { 0, 0 };
2145         unsigned int qpids[4] = { 0, 0, 0, 0 };
2146         ENTRY;
2147
2148         LASSERT(offset == REQ_REC_OFF);
2149         offset = REPLY_REC_OFF;
2150
2151         DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
2152                   rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
2153                   rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
2154         mds_counter_incr(req->rq_export, LPROC_MDS_RENAME);
2155
2156         MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2157
2158         rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
2159                                              rec->ur_fid2, &de_tgtdir, LCK_EX,
2160                                              rec->ur_name, rec->ur_namelen,
2161                                              &de_old, rec->ur_tgt,
2162                                              rec->ur_tgtlen, &de_new,
2163                                              dlm_handles, LCK_EX);
2164         if (rc)
2165                 GOTO(cleanup, rc);
2166
2167         cleanup_phase = 1; /* parent(s), children, locks */
2168
2169         old_inode = de_old->d_inode;
2170         new_inode = de_new->d_inode;
2171
2172         if (new_inode != NULL)
2173                 lock_count = 4;
2174
2175         /* sanity check for src inode */
2176         if (old_inode->i_ino == de_srcdir->d_inode->i_ino ||
2177             old_inode->i_ino == de_tgtdir->d_inode->i_ino)
2178                 GOTO(cleanup, rc = -EINVAL);
2179
2180         if (req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2181                 GOTO(cleanup, rc = -EROFS);
2182
2183         if (new_inode == NULL)
2184                 goto no_unlink;
2185
2186         igrab(new_inode);
2187         cleanup_phase = 2; /* iput(new_inode) when finished */
2188
2189         /* sanity check for dest inode */
2190         if (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
2191             new_inode->i_ino == de_tgtdir->d_inode->i_ino)
2192                 GOTO(cleanup, rc = -EINVAL);
2193
2194         if (old_inode == new_inode)
2195                 GOTO(cleanup, rc = 0);
2196
2197         /* save uids/gids for qunit acquire/release */
2198         qcids[USRQUOTA] = old_inode->i_uid;
2199         qcids[GRPQUOTA] = old_inode->i_gid;
2200         qpids[USRQUOTA] = de_tgtdir->d_inode->i_uid;
2201         qpids[GRPQUOTA] = de_tgtdir->d_inode->i_gid;
2202         qpids[2] = de_srcdir->d_inode->i_uid;
2203         qpids[3] = de_srcdir->d_inode->i_gid;
2204
2205         /* if we are about to remove the target at first, pass the EA of
2206          * that inode to client to perform and cleanup on OST */
2207         body = lustre_msg_buf(req->rq_repmsg, offset, sizeof(*body));
2208         LASSERT(body != NULL);
2209
2210         /* child orphan sem protects orphan_dec_test && is_orphan race */
2211         MDS_DOWN_READ_ORPHAN_SEM(new_inode);
2212         cleanup_phase = 3; /* MDS_UP_READ_ORPHAN_SEM(new_inode) when finished */
2213
2214         if ((S_ISDIR(new_inode->i_mode) && new_inode->i_nlink == 2) ||
2215             new_inode->i_nlink == 1) {
2216                 if (mds_orphan_open_count(new_inode) > 0) {
2217                         /* need to lock pending_dir before transaction */
2218                         LOCK_INODE_MUTEX(mds->mds_pending_dir->d_inode);
2219                         cleanup_phase = 4; /* UNLOCK_INODE_MUTEX(mds->mds_pending_dir->d_inode); */
2220                 } else if (S_ISREG(new_inode->i_mode)) {
2221                         mds_pack_inode2fid(&body->fid1, new_inode);
2222                         mds_pack_inode2body(body, new_inode);
2223                         mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
2224                                     new_inode, MDS_PACK_MD_LOCK);
2225                 }
2226         }
2227
2228 no_unlink:
2229         OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
2230                        de_srcdir->d_inode->i_sb);
2231
2232 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
2233         /* Check if we are moving old entry into its child. 2.6 does not
2234            check for this in vfs_rename() anymore */
2235         if (is_subdir(de_new, de_old))
2236                 GOTO(cleanup, rc = -EINVAL);
2237 #endif
2238
2239         lmm = lustre_msg_buf(req->rq_repmsg, offset + 1, 0);
2240         handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
2241                                   NULL, le32_to_cpu(lmm->lmm_stripe_count));
2242
2243         if (IS_ERR(handle))
2244                 GOTO(cleanup, rc = PTR_ERR(handle));
2245
2246         lock_kernel();
2247         de_old->d_fsdata = req;
2248         de_new->d_fsdata = req;
2249
2250         rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
2251         unlock_kernel();
2252
2253         if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
2254                 if (mds_orphan_open_count(new_inode) > 0)
2255                         rc = mds_orphan_add_link(rec, obd, de_new);
2256
2257                 if (rc == 1)
2258                         GOTO(cleanup, rc = 0);
2259
2260                 if (!S_ISREG(new_inode->i_mode))
2261                         GOTO(cleanup, rc);
2262
2263                 if (!(body->valid & OBD_MD_FLEASIZE)) {
2264                         body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2265                                         OBD_MD_FLATIME | OBD_MD_FLMTIME);
2266                 } else if (mds_log_op_unlink(obd,
2267                                              lustre_msg_buf(req->rq_repmsg,
2268                                                             offset + 1, 0),
2269                                              lustre_msg_buflen(req->rq_repmsg,
2270                                                                offset + 1),
2271                                              lustre_msg_buf(req->rq_repmsg,
2272                                                             offset + 2, 0),
2273                                              lustre_msg_buflen(req->rq_repmsg,
2274                                                                offset + 2))
2275                            > 0) {
2276                         body->valid |= OBD_MD_FLCOOKIE;
2277                 }
2278         }
2279
2280         GOTO(cleanup, rc);
2281 cleanup:
2282         rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
2283                                 handle, req, rc, 0, 0);
2284
2285         switch (cleanup_phase) {
2286         case 4:
2287                 UNLOCK_INODE_MUTEX(mds->mds_pending_dir->d_inode);
2288         case 3:
2289                 MDS_UP_READ_ORPHAN_SEM(new_inode);
2290         case 2:
2291                 iput(new_inode);
2292         case 1:
2293                 if (rc) {
2294                         if (lock_count == 4)
2295                                 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
2296                         ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
2297                         ldlm_lock_decref(&(dlm_handles[1]), LCK_EX);
2298                         ldlm_lock_decref(&(dlm_handles[0]), LCK_EX);
2299                 } else {
2300                         if (lock_count == 4)
2301                                 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
2302                         ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
2303                         ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_EX);
2304                         ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_EX);
2305                 }
2306                 l_dput(de_new);
2307                 l_dput(de_old);
2308                 l_dput(de_tgtdir);
2309                 l_dput(de_srcdir);
2310         case 0:
2311                 break;
2312         default:
2313                 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2314                 LBUG();
2315         }
2316         req->rq_status = rc;
2317
2318         /* acquire/release qunit */
2319         lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc, FSFILT_OP_RENAME);
2320         return 0;
2321 }
2322
2323 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
2324                            struct ptlrpc_request *, struct lustre_handle *);
2325
2326 static mds_reinter reinters[REINT_MAX] = {
2327         [REINT_SETATTR] mds_reint_setattr,
2328         [REINT_CREATE] mds_reint_create,
2329         [REINT_LINK] mds_reint_link,
2330         [REINT_UNLINK] mds_reint_unlink,
2331         [REINT_RENAME] mds_reint_rename,
2332         [REINT_OPEN] mds_open
2333 };
2334
2335 int mds_reint_rec(struct mds_update_record *rec, int offset,
2336                   struct ptlrpc_request *req, struct lustre_handle *lockh)
2337 {
2338         struct obd_device *obd = req->rq_export->exp_obd;
2339 #if 0
2340         struct mds_obd *mds = &obd->u.mds;
2341 #endif
2342         struct lvfs_run_ctxt saved;
2343         int rc;
2344         ENTRY;
2345
2346 #ifdef CRAY_XT3
2347         if (req->rq_uid != LNET_UID_ANY) {
2348                 /* non-root local cluster client
2349                  * NB root's creds are believed... */
2350                 LASSERT (req->rq_uid != 0);
2351                 rec->ur_uc.luc_fsuid = req->rq_uid;
2352                 rec->ur_uc.luc_cap = 0;
2353         }
2354 #endif
2355
2356 #if 0
2357         /* get group info of this user */
2358         rec->ur_uc.luc_uce = upcall_cache_get_entry(mds->mds_group_hash,
2359                                                     rec->ur_uc.luc_fsuid,
2360                                                     rec->ur_uc.luc_fsgid, 2,
2361                                                     &rec->ur_uc.luc_suppgid1);
2362
2363         if (IS_ERR(rec->ur_uc.luc_uce)) {
2364                 rc = PTR_ERR(rec->ur_uc.luc_uce);
2365                 rec->ur_uc.luc_uce = NULL;
2366                 RETURN(rc);
2367         }
2368
2369         /* checked by unpacker */
2370         LASSERT(rec->ur_opcode < REINT_MAX && reinters[rec->ur_opcode] != NULL);
2371
2372 #ifdef CRAY_XT3
2373         if (rec->ur_uc.luc_uce)
2374                 rec->ur_uc.luc_fsgid = rec->ur_uc.luc_uce->ue_primary;
2375 #endif
2376 #endif
2377
2378         push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
2379         rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
2380         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
2381
2382 #if 0
2383         upcall_cache_put_entry(mds->mds_group_hash, rec->ur_uc.luc_uce);
2384 #endif
2385         RETURN(rc);
2386 }