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