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