Whamcloud - gitweb
LU-2915 lfsck: LFSCK 1.5 technical debts (2)
[fs/lustre-release.git] / lustre / mdd / mdd_dir.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdd/mdd_dir.c
37  *
38  * Lustre Metadata Server (mdd) routines
39  *
40  * Author: Wang Di <wangdi@intel.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_MDS
44
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
49
50 #include "mdd_internal.h"
51
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
54
55 static struct lu_name lname_dotdot = {
56         (char *) dotdot,
57         sizeof(dotdot) - 1
58 };
59
60 static int __mdd_lookup(const struct lu_env *, struct md_object *,
61                         const struct lu_name *, struct lu_fid*, int);
62
63 static int
64 __mdd_lookup_locked(const struct lu_env *env, struct md_object *pobj,
65                     const struct lu_name *lname, struct lu_fid* fid, int mask)
66 {
67         const char *name = lname->ln_name;
68         struct mdd_object *mdd_obj = md2mdd_obj(pobj);
69         struct dynlock_handle *dlh;
70         int rc;
71
72         dlh = mdd_pdo_read_lock(env, mdd_obj, name, MOR_TGT_PARENT);
73         if (unlikely(dlh == NULL))
74                 return -ENOMEM;
75         rc = __mdd_lookup(env, pobj, lname, fid, mask);
76         mdd_pdo_read_unlock(env, mdd_obj, dlh);
77
78         return rc;
79 }
80
81 int mdd_lookup(const struct lu_env *env,
82                struct md_object *pobj, const struct lu_name *lname,
83                struct lu_fid* fid, struct md_op_spec *spec)
84 {
85         int rc;
86         ENTRY;
87         rc = __mdd_lookup_locked(env, pobj, lname, fid, MAY_EXEC);
88         RETURN(rc);
89 }
90
91 int mdd_parent_fid(const struct lu_env *env, struct mdd_object *obj,
92                    struct lu_fid *fid)
93 {
94         return __mdd_lookup_locked(env, &obj->mod_obj, &lname_dotdot, fid, 0);
95 }
96
97 /*
98  * For root fid use special function, which does not compare version component
99  * of fid. Version component is different for root fids on all MDTs.
100  */
101 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
102 {
103         return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
104                 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
105 }
106
107 /*
108  * return 1: if lf is the fid of the ancestor of p1;
109  * return 0: if not;
110  *
111  * return -EREMOTE: if remote object is found, in this
112  * case fid of remote object is saved to @pf;
113  *
114  * otherwise: values < 0, errors.
115  */
116 static int mdd_is_parent(const struct lu_env *env,
117                          struct mdd_device *mdd,
118                          struct mdd_object *p1,
119                          const struct lu_fid *lf,
120                          struct lu_fid *pf)
121 {
122         struct mdd_object *parent = NULL;
123         struct lu_fid *pfid;
124         int rc;
125         ENTRY;
126
127         LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
128         pfid = &mdd_env_info(env)->mti_fid;
129
130         /* Check for root first. */
131         if (mdd_is_root(mdd, mdo2fid(p1)))
132                 RETURN(0);
133
134         for(;;) {
135                 /* this is done recursively, bypass capa for each obj */
136                 mdd_set_capainfo(env, 4, p1, BYPASS_CAPA);
137                 rc = mdd_parent_fid(env, p1, pfid);
138                 if (rc)
139                         GOTO(out, rc);
140                 if (mdd_is_root(mdd, pfid))
141                         GOTO(out, rc = 0);
142                 if (lu_fid_eq(pfid, lf))
143                         GOTO(out, rc = 1);
144                 if (parent)
145                         mdd_object_put(env, parent);
146
147                 parent = mdd_object_find(env, mdd, pfid);
148                 if (IS_ERR(parent)) {
149                         GOTO(out, rc = PTR_ERR(parent));
150                 } else if (mdd_object_remote(parent)) {
151                         /*FIXME: Because of the restriction of rename in Phase I.
152                          * If the parent is remote, we just assumed lf is not the
153                          * parent of P1 for now */
154                         GOTO(out, rc = 0);
155                 }
156                 p1 = parent;
157         }
158         EXIT;
159 out:
160         if (parent && !IS_ERR(parent))
161                 mdd_object_put(env, parent);
162         return rc;
163 }
164
165 /*
166  * No permission check is needed.
167  *
168  * returns 1: if fid is ancestor of @mo;
169  * returns 0: if fid is not a ancestor of @mo;
170  *
171  * returns EREMOTE if remote object is found, fid of remote object is saved to
172  * @fid;
173  *
174  * returns < 0: if error
175  */
176 int mdd_is_subdir(const struct lu_env *env, struct md_object *mo,
177                   const struct lu_fid *fid, struct lu_fid *sfid)
178 {
179         struct mdd_device *mdd = mdo2mdd(mo);
180         int rc;
181         ENTRY;
182
183         if (!S_ISDIR(mdd_object_type(md2mdd_obj(mo))))
184                 RETURN(0);
185
186         rc = mdd_is_parent(env, mdd, md2mdd_obj(mo), fid, sfid);
187         if (rc == 0) {
188                 /* found root */
189                 fid_zero(sfid);
190         } else if (rc == 1) {
191                 /* found @fid is parent */
192                 *sfid = *fid;
193                 rc = 0;
194         }
195         RETURN(rc);
196 }
197
198 /*
199  * Check that @dir contains no entries except (possibly) dot and dotdot.
200  *
201  * Returns:
202  *
203  *             0        empty
204  *      -ENOTDIR        not a directory object
205  *    -ENOTEMPTY        not empty
206  *           -ve        other error
207  *
208  */
209 static int mdd_dir_is_empty(const struct lu_env *env,
210                             struct mdd_object *dir)
211 {
212         struct dt_it     *it;
213         struct dt_object *obj;
214         const struct dt_it_ops *iops;
215         int result;
216         ENTRY;
217
218         obj = mdd_object_child(dir);
219         if (!dt_try_as_dir(env, obj))
220                 RETURN(-ENOTDIR);
221
222         iops = &obj->do_index_ops->dio_it;
223         it = iops->init(env, obj, LUDA_64BITHASH, BYPASS_CAPA);
224         if (!IS_ERR(it)) {
225                 result = iops->get(env, it, (const void *)"");
226                 if (result > 0) {
227                         int i;
228                         for (result = 0, i = 0; result == 0 && i < 3; ++i)
229                                 result = iops->next(env, it);
230                         if (result == 0)
231                                 result = -ENOTEMPTY;
232                         else if (result == +1)
233                                 result = 0;
234                 } else if (result == 0)
235                         /*
236                          * Huh? Index contains no zero key?
237                          */
238                         result = -EIO;
239
240                 iops->put(env, it);
241                 iops->fini(env, it);
242         } else
243                 result = PTR_ERR(it);
244         RETURN(result);
245 }
246
247 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj)
248 {
249         struct mdd_device *m = mdd_obj2mdd_dev(obj);
250         struct lu_attr *la = &mdd_env_info(env)->mti_la;
251         int rc;
252         ENTRY;
253
254         rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
255         if (rc)
256                 RETURN(rc);
257
258         /*
259          * Subdir count limitation can be broken through.
260          */
261         if (la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink &&
262             !S_ISDIR(la->la_mode))
263                 RETURN(-EMLINK);
264         else
265                 RETURN(0);
266 }
267
268 /*
269  * Check whether it may create the cobj under the pobj.
270  * cobj maybe NULL
271  */
272 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
273                    struct mdd_object *cobj, int check_perm, int check_nlink)
274 {
275         int rc = 0;
276         ENTRY;
277
278         if (cobj && mdd_object_exists(cobj))
279                 RETURN(-EEXIST);
280
281         if (mdd_is_dead_obj(pobj))
282                 RETURN(-ENOENT);
283
284         if (check_perm)
285                 rc = mdd_permission_internal_locked(env, pobj, NULL,
286                                                     MAY_WRITE | MAY_EXEC,
287                                                     MOR_TGT_PARENT);
288         if (!rc && check_nlink)
289                 rc = __mdd_may_link(env, pobj);
290
291         RETURN(rc);
292 }
293
294 /*
295  * Check whether can unlink from the pobj in the case of "cobj == NULL".
296  */
297 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
298                    const struct lu_attr *attr)
299 {
300         int rc;
301         ENTRY;
302
303         if (mdd_is_dead_obj(pobj))
304                 RETURN(-ENOENT);
305
306         if ((attr->la_valid & LA_FLAGS) &&
307             (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
308                 RETURN(-EPERM);
309
310         rc = mdd_permission_internal_locked(env, pobj, NULL,
311                                             MAY_WRITE | MAY_EXEC,
312                                             MOR_TGT_PARENT);
313         if (rc)
314                 RETURN(rc);
315
316         if (mdd_is_append(pobj))
317                 RETURN(-EPERM);
318
319         RETURN(rc);
320 }
321
322 /*
323  * pobj == NULL is remote ops case, under such case, pobj's
324  * VTX feature has been checked already, no need check again.
325  */
326 static inline int mdd_is_sticky(const struct lu_env *env,
327                                 struct mdd_object *pobj,
328                                 struct mdd_object *cobj)
329 {
330         struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
331         struct lu_ucred *uc = lu_ucred_assert(env);
332         int rc;
333
334         if (pobj) {
335                 rc = mdd_la_get(env, pobj, tmp_la, BYPASS_CAPA);
336                 if (rc)
337                         return rc;
338
339                 if (!(tmp_la->la_mode & S_ISVTX) ||
340                     (tmp_la->la_uid == uc->uc_fsuid))
341                         return 0;
342         }
343
344         rc = mdd_la_get(env, cobj, tmp_la, BYPASS_CAPA);
345         if (rc)
346                 return rc;
347
348         if (tmp_la->la_uid == uc->uc_fsuid)
349                 return 0;
350
351         return !md_capable(uc, CFS_CAP_FOWNER);
352 }
353
354 static int mdd_may_delete_entry(const struct lu_env *env,
355                                 struct mdd_object *pobj, int check_perm)
356 {
357         ENTRY;
358
359         LASSERT(pobj != NULL);
360         if (!mdd_object_exists(pobj))
361                 RETURN(-ENOENT);
362
363         if (mdd_is_dead_obj(pobj))
364                 RETURN(-ENOENT);
365
366         if (check_perm) {
367                 int rc;
368                 rc = mdd_permission_internal_locked(env, pobj, NULL,
369                                             MAY_WRITE | MAY_EXEC,
370                                             MOR_TGT_PARENT);
371                 if (rc)
372                         RETURN(rc);
373         }
374
375         if (mdd_is_append(pobj))
376                 RETURN(-EPERM);
377
378         RETURN(0);
379 }
380
381 /*
382  * Check whether it may delete the cobj from the pobj.
383  * pobj maybe NULL
384  */
385 int mdd_may_delete(const struct lu_env *env, struct mdd_object *pobj,
386                    struct mdd_object *cobj, struct lu_attr *cattr,
387                    struct lu_attr *src_attr, int check_perm, int check_empty)
388 {
389         int rc = 0;
390         ENTRY;
391
392         if (pobj) {
393                 rc = mdd_may_delete_entry(env, pobj, check_perm);
394                 if (rc != 0)
395                         RETURN(rc);
396         }
397
398         if (cobj == NULL)
399                 RETURN(0);
400
401         if (!mdd_object_exists(cobj))
402                 RETURN(-ENOENT);
403
404         if (mdd_is_dead_obj(cobj))
405                 RETURN(-ESTALE);
406
407
408         if (mdd_is_sticky(env, pobj, cobj))
409                 RETURN(-EPERM);
410
411         if (mdd_is_immutable(cobj) || mdd_is_append(cobj))
412                 RETURN(-EPERM);
413
414         if ((cattr->la_valid & LA_FLAGS) &&
415             (cattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL)))
416                 RETURN(-EPERM);
417
418         /* additional check the rename case */
419         if (src_attr) {
420                 if (S_ISDIR(src_attr->la_mode)) {
421                         struct mdd_device *mdd = mdo2mdd(&cobj->mod_obj);
422
423                         if (!S_ISDIR(cattr->la_mode))
424                                 RETURN(-ENOTDIR);
425
426                         if (lu_fid_eq(mdo2fid(cobj), &mdd->mdd_root_fid))
427                                 RETURN(-EBUSY);
428                 } else if (S_ISDIR(cattr->la_mode))
429                         RETURN(-EISDIR);
430         }
431
432         if (S_ISDIR(cattr->la_mode) && check_empty)
433                 rc = mdd_dir_is_empty(env, cobj);
434
435         RETURN(rc);
436 }
437
438 /*
439  * tgt maybe NULL
440  * has mdd_write_lock on src already, but not on tgt yet
441  */
442 int mdd_link_sanity_check(const struct lu_env *env,
443                           struct mdd_object *tgt_obj,
444                           const struct lu_name *lname,
445                           struct mdd_object *src_obj)
446 {
447         struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
448         int rc = 0;
449         ENTRY;
450
451         if (!mdd_object_exists(src_obj))
452                 RETURN(-ENOENT);
453
454         if (mdd_is_dead_obj(src_obj))
455                 RETURN(-ESTALE);
456
457         /* Local ops, no lookup before link, check filename length here. */
458         if (lname && (lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
459                 RETURN(-ENAMETOOLONG);
460
461         if (mdd_is_immutable(src_obj) || mdd_is_append(src_obj))
462                 RETURN(-EPERM);
463
464         if (S_ISDIR(mdd_object_type(src_obj)))
465                 RETURN(-EPERM);
466
467         LASSERT(src_obj != tgt_obj);
468         if (tgt_obj) {
469                 rc = mdd_may_create(env, tgt_obj, NULL, 1, 0);
470                 if (rc)
471                         RETURN(rc);
472         }
473
474         rc = __mdd_may_link(env, src_obj);
475
476         RETURN(rc);
477 }
478
479 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
480                                    const char *name, struct thandle *handle,
481                                    struct lustre_capa *capa)
482 {
483         struct dt_object *next = mdd_object_child(pobj);
484         int               rc;
485         ENTRY;
486
487         if (dt_try_as_dir(env, next)) {
488                 rc = next->do_index_ops->dio_delete(env, next,
489                                                     (struct dt_key *)name,
490                                                     handle, capa);
491         } else
492                 rc = -ENOTDIR;
493
494         RETURN(rc);
495 }
496
497 static int __mdd_index_insert_only(const struct lu_env *env,
498                                    struct mdd_object *pobj,
499                                    const struct lu_fid *lf, const char *name,
500                                    struct thandle *handle,
501                                    struct lustre_capa *capa)
502 {
503         struct dt_object *next = mdd_object_child(pobj);
504         int               rc;
505         ENTRY;
506
507         if (dt_try_as_dir(env, next)) {
508                 struct lu_ucred  *uc = lu_ucred_check(env);
509                 int ignore_quota;
510
511                 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
512                 rc = next->do_index_ops->dio_insert(env, next,
513                                                     (struct dt_rec*)lf,
514                                                     (const struct dt_key *)name,
515                                                     handle, capa, ignore_quota);
516         } else {
517                 rc = -ENOTDIR;
518         }
519         RETURN(rc);
520 }
521
522 /* insert named index, add reference if isdir */
523 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
524                               const struct lu_fid *lf, const char *name, int is_dir,
525                               struct thandle *handle, struct lustre_capa *capa)
526 {
527         int               rc;
528         ENTRY;
529
530         rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
531         if (rc == 0 && is_dir) {
532                 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
533                 mdo_ref_add(env, pobj, handle);
534                 mdd_write_unlock(env, pobj);
535         }
536         RETURN(rc);
537 }
538
539 /* delete named index, drop reference if isdir */
540 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
541                               const char *name, int is_dir, struct thandle *handle,
542                               struct lustre_capa *capa)
543 {
544         int               rc;
545         ENTRY;
546
547         rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
548         if (rc == 0 && is_dir) {
549                 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
550                 mdo_ref_del(env, pobj, handle);
551                 mdd_write_unlock(env, pobj);
552         }
553
554         RETURN(rc);
555 }
556
557 int mdd_declare_changelog_store(const struct lu_env *env,
558                                 struct mdd_device *mdd,
559                                 const struct lu_name *fname,
560                                 struct thandle *handle)
561 {
562         struct obd_device               *obd = mdd2obd_dev(mdd);
563         struct llog_ctxt                *ctxt;
564         struct llog_changelog_rec       *rec;
565         struct lu_buf                   *buf;
566         int                              reclen;
567         int                              rc;
568
569         /* Not recording */
570         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
571                 return 0;
572
573         reclen = llog_data_len(sizeof(*rec) +
574                                (fname != NULL ? fname->ln_namelen : 0));
575         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
576         if (buf->lb_buf == NULL)
577                 return -ENOMEM;
578
579         rec = buf->lb_buf;
580         rec->cr_hdr.lrh_len = reclen;
581         rec->cr_hdr.lrh_type = CHANGELOG_REC;
582
583         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
584         if (ctxt == NULL)
585                 return -ENXIO;
586
587         rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
588         llog_ctxt_put(ctxt);
589
590         return rc;
591 }
592
593 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
594                                            struct mdd_device *mdd,
595                                            const struct lu_name *tname,
596                                            const struct lu_name *sname,
597                                            struct thandle *handle)
598 {
599         struct obd_device               *obd = mdd2obd_dev(mdd);
600         struct llog_ctxt                *ctxt;
601         struct llog_changelog_ext_rec   *rec;
602         struct lu_buf                   *buf;
603         int                              reclen;
604         int                              rc;
605
606         /* Not recording */
607         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
608                 return 0;
609
610         reclen = llog_data_len(sizeof(*rec) +
611                                (tname != NULL ? tname->ln_namelen : 0) +
612                                (sname != NULL ? 1 + sname->ln_namelen : 0));
613         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
614         if (buf->lb_buf == NULL)
615                 return -ENOMEM;
616
617         rec = buf->lb_buf;
618         rec->cr_hdr.lrh_len = reclen;
619         rec->cr_hdr.lrh_type = CHANGELOG_REC;
620
621         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
622         if (ctxt == NULL)
623                 return -ENXIO;
624
625         rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
626         llog_ctxt_put(ctxt);
627
628         return rc;
629 }
630
631 /** Add a changelog entry \a rec to the changelog llog
632  * \param mdd
633  * \param rec
634  * \param handle - currently ignored since llogs start their own transaction;
635  *                 this will hopefully be fixed in llog rewrite
636  * \retval 0 ok
637  */
638 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
639                         struct llog_changelog_rec *rec, struct thandle *th)
640 {
641         struct obd_device       *obd = mdd2obd_dev(mdd);
642         struct llog_ctxt        *ctxt;
643         int                      rc;
644
645         rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
646         rec->cr_hdr.lrh_type = CHANGELOG_REC;
647         rec->cr.cr_time = cl_time();
648
649         spin_lock(&mdd->mdd_cl.mc_lock);
650         /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
651          * but as long as the MDD transactions are ordered correctly for e.g.
652          * rename conflicts, I don't think this should matter. */
653         rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
654         spin_unlock(&mdd->mdd_cl.mc_lock);
655
656         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
657         if (ctxt == NULL)
658                 return -ENXIO;
659
660         rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
661         llog_ctxt_put(ctxt);
662         if (rc > 0)
663                 rc = 0;
664         return rc;
665 }
666
667 /** Add a changelog_ext entry \a rec to the changelog llog
668  * \param mdd
669  * \param rec
670  * \param handle - currently ignored since llogs start their own transaction;
671  *              this will hopefully be fixed in llog rewrite
672  * \retval 0 ok
673  */
674 int mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
675                             struct llog_changelog_ext_rec *rec,
676                             struct thandle *th)
677 {
678         struct obd_device       *obd = mdd2obd_dev(mdd);
679         struct llog_ctxt        *ctxt;
680         int                      rc;
681
682         rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
683         /* llog_lvfs_write_rec sets the llog tail len */
684         rec->cr_hdr.lrh_type = CHANGELOG_REC;
685         rec->cr.cr_time = cl_time();
686
687         spin_lock(&mdd->mdd_cl.mc_lock);
688         /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
689          * but as long as the MDD transactions are ordered correctly for e.g.
690          * rename conflicts, I don't think this should matter. */
691         rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
692         spin_unlock(&mdd->mdd_cl.mc_lock);
693
694         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
695         if (ctxt == NULL)
696                 return -ENXIO;
697
698         /* nested journal transaction */
699         rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
700         llog_ctxt_put(ctxt);
701         if (rc > 0)
702                 rc = 0;
703
704         return rc;
705 }
706
707 /** Store a namespace change changelog record
708  * If this fails, we must fail the whole transaction; we don't
709  * want the change to commit without the log entry.
710  * \param target - mdd_object of change
711  * \param parent - parent dir/object
712  * \param tname - target name string
713  * \param handle - transacion handle
714  */
715 int mdd_changelog_ns_store(const struct lu_env *env, struct mdd_device *mdd,
716                            enum changelog_rec_type type, unsigned flags,
717                            struct mdd_object *target, struct mdd_object *parent,
718                            const struct lu_name *tname, struct thandle *handle)
719 {
720         struct llog_changelog_rec *rec;
721         struct lu_buf *buf;
722         int reclen;
723         int rc;
724         ENTRY;
725
726         /* Not recording */
727         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
728                 RETURN(0);
729         if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
730                 RETURN(0);
731
732         LASSERT(target != NULL);
733         LASSERT(parent != NULL);
734         LASSERT(tname != NULL);
735         LASSERT(handle != NULL);
736
737         reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
738         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
739         if (buf->lb_buf == NULL)
740                 RETURN(-ENOMEM);
741         rec = buf->lb_buf;
742
743         rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
744         rec->cr.cr_type = (__u32)type;
745         rec->cr.cr_tfid = *mdo2fid(target);
746         rec->cr.cr_pfid = *mdo2fid(parent);
747         rec->cr.cr_namelen = tname->ln_namelen;
748         memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
749
750         target->mod_cltime = cfs_time_current_64();
751
752         rc = mdd_changelog_store(env, mdd, rec, handle);
753         if (rc < 0) {
754                 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
755                         rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
756                         PFID(&rec->cr.cr_pfid));
757                 RETURN(-EFAULT);
758         }
759
760         RETURN(0);
761 }
762
763
764 /** Store a namespace change changelog record
765  * If this fails, we must fail the whole transaction; we don't
766  * want the change to commit without the log entry.
767  * \param target - mdd_object of change
768  * \param tpfid - target parent dir/object fid
769  * \param sfid - source object fid
770  * \param spfid - source parent fid
771  * \param tname - target name string
772  * \param sname - source name string
773  * \param handle - transacion handle
774  */
775 static int mdd_changelog_ext_ns_store(const struct lu_env  *env,
776                                       struct mdd_device    *mdd,
777                                       enum changelog_rec_type type,
778                                       unsigned flags,
779                                       struct mdd_object    *target,
780                                       const struct lu_fid  *tpfid,
781                                       const struct lu_fid  *sfid,
782                                       const struct lu_fid  *spfid,
783                                       const struct lu_name *tname,
784                                       const struct lu_name *sname,
785                                       struct thandle *handle)
786 {
787         struct llog_changelog_ext_rec *rec;
788         struct lu_buf *buf;
789         int reclen;
790         int rc;
791         ENTRY;
792
793         /* Not recording */
794         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
795                 RETURN(0);
796         if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
797                 RETURN(0);
798
799         LASSERT(sfid != NULL);
800         LASSERT(tpfid != NULL);
801         LASSERT(tname != NULL);
802         LASSERT(handle != NULL);
803
804         reclen = llog_data_len(sizeof(*rec) +
805                                sname != NULL ? 1 + sname->ln_namelen : 0);
806         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
807         if (buf->lb_buf == NULL)
808                 RETURN(-ENOMEM);
809         rec = buf->lb_buf;
810
811         rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
812         rec->cr.cr_type = (__u32)type;
813         rec->cr.cr_pfid = *tpfid;
814         rec->cr.cr_sfid = *sfid;
815         rec->cr.cr_spfid = *spfid;
816         rec->cr.cr_namelen = tname->ln_namelen;
817         memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
818         if (sname) {
819                 rec->cr.cr_name[tname->ln_namelen] = '\0';
820                 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
821                         sname->ln_namelen);
822                 rec->cr.cr_namelen += 1 + sname->ln_namelen;
823         }
824
825         if (likely(target != NULL)) {
826                 rec->cr.cr_tfid = *mdo2fid(target);
827                 target->mod_cltime = cfs_time_current_64();
828         } else {
829                 fid_zero(&rec->cr.cr_tfid);
830         }
831
832         rc = mdd_changelog_ext_store(env, mdd, rec, handle);
833         if (rc < 0) {
834                 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
835                         rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
836                 return -EFAULT;
837         }
838
839         return 0;
840 }
841
842 static int __mdd_links_add(const struct lu_env *env,
843                            struct mdd_object *mdd_obj,
844                            struct linkea_data *ldata,
845                            const struct lu_name *lname,
846                            const struct lu_fid *pfid,
847                            int first, int check)
848 {
849         int rc;
850
851         if (ldata->ld_leh == NULL) {
852                 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
853                 if (rc) {
854                         if (rc != -ENODATA)
855                                 return rc;
856                         rc = linkea_data_new(ldata,
857                                              &mdd_env_info(env)->mti_link_buf);
858                         if (rc)
859                                 return rc;
860                 }
861         }
862
863         if (check) {
864                 rc = linkea_links_find(ldata, lname, pfid);
865                 if (rc && rc != -ENOENT)
866                         return rc;
867                 if (rc == 0)
868                         return -EEXIST;
869         }
870
871         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
872                 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
873
874                 *tfid = *pfid;
875                 tfid->f_ver = ~0;
876                 linkea_add_buf(ldata, lname, tfid);
877         }
878
879         return linkea_add_buf(ldata, lname, pfid);
880 }
881
882 static int __mdd_links_del(const struct lu_env *env,
883                            struct mdd_object *mdd_obj,
884                            struct linkea_data *ldata,
885                            const struct lu_name *lname,
886                            const struct lu_fid *pfid)
887 {
888         int rc;
889
890         if (ldata->ld_leh == NULL) {
891                 rc = mdd_links_read(env, mdd_obj, ldata);
892                 if (rc)
893                         return rc;
894         }
895
896         rc = linkea_links_find(ldata, lname, pfid);
897         if (rc)
898                 return rc;
899
900         linkea_del_buf(ldata, lname);
901         return 0;
902 }
903
904 static int mdd_linkea_prepare(const struct lu_env *env,
905                               struct mdd_object *mdd_obj,
906                               const struct lu_fid *oldpfid,
907                               const struct lu_name *oldlname,
908                               const struct lu_fid *newpfid,
909                               const struct lu_name *newlname,
910                               int first, int check,
911                               struct linkea_data *ldata)
912 {
913         int rc = 0;
914         int rc2 = 0;
915         ENTRY;
916
917         if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
918                 return 0;
919
920         LASSERT(oldpfid != NULL || newpfid != NULL);
921
922         if (mdd_obj->mod_flags & DEAD_OBJ)
923                 /* No more links, don't bother */
924                 RETURN(0);
925
926         if (oldpfid != NULL) {
927                 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
928                 if (rc) {
929                         if ((check == 0) ||
930                             (rc != -ENODATA && rc != -ENOENT))
931                                 RETURN(rc);
932                         /* No changes done. */
933                         rc = 0;
934                 }
935         }
936
937         /* If renaming, add the new record */
938         if (newpfid != NULL) {
939                 /* even if the add fails, we still delete the out-of-date
940                  * old link */
941                 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
942                                       first, check);
943                 if (rc2 == -EEXIST)
944                         rc2 = 0;
945         }
946
947         rc = rc != 0 ? rc : rc2;
948
949         RETURN(rc);
950 }
951
952 int mdd_links_rename(const struct lu_env *env,
953                      struct mdd_object *mdd_obj,
954                      const struct lu_fid *oldpfid,
955                      const struct lu_name *oldlname,
956                      const struct lu_fid *newpfid,
957                      const struct lu_name *newlname,
958                      struct thandle *handle,
959                      struct linkea_data *ldata,
960                      int first, int check)
961 {
962         int rc2 = 0;
963         int rc = 0;
964         ENTRY;
965
966         if (ldata == NULL) {
967                 ldata = &mdd_env_info(env)->mti_link_data;
968                 memset(ldata, 0, sizeof(*ldata));
969                 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
970                                         newpfid, newlname, first, check,
971                                         ldata);
972                 if (rc != 0)
973                         GOTO(out, rc);
974         }
975
976         if (ldata->ld_lee != NULL)
977                 rc = mdd_links_write(env, mdd_obj, ldata, handle);
978         EXIT;
979 out:
980         if (rc == 0)
981                 rc = rc2;
982         if (rc) {
983                 int error = 1;
984                 if (rc == -EOVERFLOW || rc == -ENOENT || rc == -ENOSPC)
985                         error = 0;
986                 if (oldpfid == NULL)
987                         CDEBUG(error ? D_ERROR : D_OTHER,
988                                "link_ea add '%.*s' failed %d "DFID"\n",
989                                newlname->ln_namelen, newlname->ln_name,
990                                rc, PFID(mdd_object_fid(mdd_obj)));
991                 else if (newpfid == NULL)
992                         CDEBUG(error ? D_ERROR : D_OTHER,
993                                "link_ea del '%.*s' failed %d "DFID"\n",
994                                oldlname->ln_namelen, oldlname->ln_name,
995                                rc, PFID(mdd_object_fid(mdd_obj)));
996                 else
997                         CDEBUG(error ? D_ERROR : D_OTHER,
998                                "link_ea rename '%.*s'->'%.*s' failed %d "
999                                DFID"\n",
1000                                oldlname->ln_namelen, oldlname->ln_name,
1001                                newlname->ln_namelen, newlname->ln_name,
1002                                rc, PFID(mdd_object_fid(mdd_obj)));
1003         }
1004
1005         if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1006                 /* if we vmalloced a large buffer drop it */
1007                 lu_buf_free(ldata->ld_buf);
1008
1009         return rc;
1010 }
1011
1012 static inline int mdd_links_add(const struct lu_env *env,
1013                                 struct mdd_object *mdd_obj,
1014                                 const struct lu_fid *pfid,
1015                                 const struct lu_name *lname,
1016                                 struct thandle *handle,
1017                                 struct linkea_data *data, int first)
1018 {
1019         return mdd_links_rename(env, mdd_obj, NULL, NULL,
1020                                 pfid, lname, handle, data, first, 0);
1021 }
1022
1023 static inline int mdd_links_del(const struct lu_env *env,
1024                                 struct mdd_object *mdd_obj,
1025                                 const struct lu_fid *pfid,
1026                                 const struct lu_name *lname,
1027                                 struct thandle *handle)
1028 {
1029         return mdd_links_rename(env, mdd_obj, pfid, lname,
1030                                 NULL, NULL, handle, NULL, 0, 0);
1031 }
1032
1033 /** Read the link EA into a temp buffer.
1034  * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1035  * A pointer to the buffer is stored in \a ldata::ld_buf.
1036  *
1037  * \retval 0 or error
1038  */
1039 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1040                    struct linkea_data *ldata)
1041 {
1042         int rc;
1043
1044         /* First try a small buf */
1045         LASSERT(env != NULL);
1046         ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1047                                                CFS_PAGE_SIZE);
1048         if (ldata->ld_buf->lb_buf == NULL)
1049                 return -ENOMEM;
1050
1051         if (!mdd_object_exists(mdd_obj))
1052                 return -ENODATA;
1053
1054         rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK,
1055                           BYPASS_CAPA);
1056         if (rc == -ERANGE) {
1057                 /* Buf was too small, figure out what we need. */
1058                 lu_buf_free(ldata->ld_buf);
1059                 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1060                                    XATTR_NAME_LINK, BYPASS_CAPA);
1061                 if (rc < 0)
1062                         return rc;
1063                 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1064                 if (ldata->ld_buf->lb_buf == NULL)
1065                         return -ENOMEM;
1066                 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1067                                   XATTR_NAME_LINK, BYPASS_CAPA);
1068         }
1069         if (rc < 0)
1070                 return rc;
1071
1072         linkea_init(ldata);
1073         return 0;
1074 }
1075
1076 /** Read the link EA into a temp buffer.
1077  * Uses the name_buf since it is generally large.
1078  * \retval IS_ERR err
1079  * \retval ptr to \a lu_buf (always \a mti_big_buf)
1080  */
1081 struct lu_buf *mdd_links_get(const struct lu_env *env,
1082                              struct mdd_object *mdd_obj)
1083 {
1084         struct linkea_data ldata = { 0 };
1085         int rc;
1086
1087         rc = mdd_links_read(env, mdd_obj, &ldata);
1088         return rc ? ERR_PTR(rc) : ldata.ld_buf;
1089 }
1090
1091 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1092                     struct linkea_data *ldata, struct thandle *handle)
1093 {
1094         const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1095                                                      ldata->ld_leh->leh_len);
1096         return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
1097                              mdd_object_capa(env, mdd_obj));
1098 }
1099
1100 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1101                           struct thandle *handle, struct linkea_data *ldata)
1102 {
1103         int     rc;
1104         int     ea_len;
1105         void    *linkea;
1106
1107         if (ldata != NULL && ldata->ld_lee != NULL) {
1108                 ea_len = ldata->ld_leh->leh_len;
1109                 linkea = ldata->ld_buf->lb_buf;
1110         } else {
1111                 ea_len = DEFAULT_LINKEA_SIZE;
1112                 linkea = NULL;
1113         }
1114
1115         /* XXX: max size? */
1116         rc = mdo_declare_xattr_set(env, mdd_obj,
1117                                    mdd_buf_get_const(env, linkea, ea_len),
1118                                    XATTR_NAME_LINK, 0, handle);
1119         return rc;
1120 }
1121
1122 static inline int mdd_declare_links_del(const struct lu_env *env,
1123                                         struct mdd_object *c,
1124                                         struct thandle *handle)
1125 {
1126         int rc = 0;
1127
1128         /* For directory, the linkEA will be removed together
1129          * with the object. */
1130         if (!S_ISDIR(mdd_object_type(c)))
1131                 rc = mdd_declare_links_add(env, c, handle, NULL);
1132
1133         return rc;
1134 }
1135
1136 static int mdd_declare_link(const struct lu_env *env,
1137                             struct mdd_device *mdd,
1138                             struct mdd_object *p,
1139                             struct mdd_object *c,
1140                             const struct lu_name *name,
1141                             struct thandle *handle,
1142                             struct lu_attr *la,
1143                             struct linkea_data *data)
1144 {
1145         int rc;
1146
1147         rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
1148         if (rc)
1149                 return rc;
1150
1151         rc = mdo_declare_ref_add(env, c, handle);
1152         if (rc)
1153                 return rc;
1154
1155         la->la_valid = LA_CTIME | LA_MTIME;
1156         rc = mdo_declare_attr_set(env, p, la, handle);
1157         if (rc != 0)
1158                 return rc;
1159
1160         la->la_valid = LA_CTIME;
1161         rc = mdo_declare_attr_set(env, c, la, handle);
1162         if (rc)
1163                 return rc;
1164
1165         rc = mdd_declare_links_add(env, c, handle, data);
1166         if (rc)
1167                 return rc;
1168
1169         rc = mdd_declare_changelog_store(env, mdd, name, handle);
1170
1171         return rc;
1172 }
1173
1174 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1175                     struct md_object *src_obj, const struct lu_name *lname,
1176                     struct md_attr *ma)
1177 {
1178         const char *name = lname->ln_name;
1179         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
1180         struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1181         struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1182         struct mdd_device *mdd = mdo2mdd(src_obj);
1183         struct dynlock_handle *dlh;
1184         struct thandle *handle;
1185         struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1186         int rc;
1187         ENTRY;
1188
1189         handle = mdd_trans_create(env, mdd);
1190         if (IS_ERR(handle))
1191                 GOTO(out_pending, rc = PTR_ERR(handle));
1192
1193         memset(ldata, 0, sizeof(*ldata));
1194
1195         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1196         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1197
1198         rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1199                               la, ldata);
1200         if (rc)
1201                 GOTO(stop, rc);
1202
1203         rc = mdd_trans_start(env, mdd, handle);
1204         if (rc)
1205                 GOTO(stop, rc);
1206
1207         dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
1208         if (dlh == NULL)
1209                 GOTO(out_trans, rc = -ENOMEM);
1210         mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1211
1212         rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
1213         if (rc)
1214                 GOTO(out_unlock, rc);
1215
1216         rc = mdo_ref_add(env, mdd_sobj, handle);
1217         if (rc)
1218                 GOTO(out_unlock, rc);
1219
1220
1221         rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1222                                      name, handle,
1223                                      mdd_object_capa(env, mdd_tobj));
1224         if (rc != 0) {
1225                 mdo_ref_del(env, mdd_sobj, handle);
1226                 GOTO(out_unlock, rc);
1227         }
1228
1229         la->la_valid = LA_CTIME | LA_MTIME;
1230         rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1231         if (rc)
1232                 GOTO(out_unlock, rc);
1233
1234         la->la_valid = LA_CTIME;
1235         rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
1236         if (rc == 0) {
1237                 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1238                                         mdo2fid(mdd_tobj), lname, 0, 0,
1239                                         ldata);
1240                 if (rc == 0)
1241                         mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1242                                       lname, handle, ldata, 0);
1243                 /* The failure of links_add should not cause the link
1244                  * failure, reset rc here */
1245                 rc = 0;
1246         }
1247         EXIT;
1248 out_unlock:
1249         mdd_write_unlock(env, mdd_sobj);
1250         mdd_pdo_write_unlock(env, mdd_tobj, dlh);
1251 out_trans:
1252         if (rc == 0)
1253                 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1254                                             mdd_tobj, lname, handle);
1255 stop:
1256         mdd_trans_stop(env, mdd, rc, handle);
1257
1258         if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1259                 /* if we vmalloced a large buffer drop it */
1260                 lu_buf_free(ldata->ld_buf);
1261 out_pending:
1262         return rc;
1263 }
1264
1265 int mdd_declare_finish_unlink(const struct lu_env *env,
1266                               struct mdd_object *obj,
1267                               struct md_attr *ma,
1268                               struct thandle *handle)
1269 {
1270         int     rc;
1271
1272         rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1273         if (rc)
1274                 return rc;
1275
1276         return mdo_declare_destroy(env, obj, handle);
1277 }
1278
1279 /* caller should take a lock before calling */
1280 int mdd_finish_unlink(const struct lu_env *env,
1281                       struct mdd_object *obj, struct md_attr *ma,
1282                       struct thandle *th)
1283 {
1284         int rc = 0;
1285         int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1286         ENTRY;
1287
1288         LASSERT(mdd_write_locked(env, obj) != 0);
1289
1290         if (ma->ma_attr.la_nlink == 0 || is_dir) {
1291                 obj->mod_flags |= DEAD_OBJ;
1292
1293                 /* add new orphan and the object
1294                  * will be deleted during mdd_close() */
1295                 if (obj->mod_count) {
1296                         rc = __mdd_orphan_add(env, obj, th);
1297                         if (rc == 0)
1298                                 CDEBUG(D_HA, "Object "DFID" is inserted into "
1299                                         "orphan list, open count = %d\n",
1300                                         PFID(mdd_object_fid(obj)),
1301                                         obj->mod_count);
1302                         else
1303                                 CERROR("Object "DFID" fail to be an orphan, "
1304                                        "open count = %d, maybe cause failed "
1305                                        "open replay\n",
1306                                         PFID(mdd_object_fid(obj)),
1307                                         obj->mod_count);
1308                 } else {
1309                         rc = mdo_destroy(env, obj, th);
1310                 }
1311         }
1312
1313         RETURN(rc);
1314 }
1315
1316 /*
1317  * pobj maybe NULL
1318  * has mdd_write_lock on cobj already, but not on pobj yet
1319  */
1320 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1321                             struct mdd_object *cobj, struct lu_attr *cattr)
1322 {
1323         int rc;
1324         ENTRY;
1325
1326         rc = mdd_may_delete(env, pobj, cobj, cattr, NULL, 1, 1);
1327
1328         RETURN(rc);
1329 }
1330
1331 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1332                               struct mdd_object *p, struct mdd_object *c,
1333                               const struct lu_name *name, struct md_attr *ma,
1334                               struct thandle *handle, int no_name)
1335 {
1336         struct lu_attr     *la = &mdd_env_info(env)->mti_la_for_fix;
1337         int rc;
1338
1339         if (likely(no_name == 0)) {
1340                 rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1341                 if (rc)
1342                         return rc;
1343         }
1344
1345         rc = mdo_declare_ref_del(env, p, handle);
1346         if (rc)
1347                 return rc;
1348
1349         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1350         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1351         la->la_valid = LA_CTIME | LA_MTIME;
1352         rc = mdo_declare_attr_set(env, p, la, handle);
1353         if (rc)
1354                 return rc;
1355
1356         if (c != NULL) {
1357                 rc = mdo_declare_ref_del(env, c, handle);
1358                 if (rc)
1359                         return rc;
1360
1361                 rc = mdo_declare_ref_del(env, c, handle);
1362                 if (rc)
1363                         return rc;
1364
1365                 la->la_valid = LA_CTIME;
1366                 rc = mdo_declare_attr_set(env, c, la, handle);
1367                 if (rc)
1368                         return rc;
1369
1370                 rc = mdd_declare_finish_unlink(env, c, ma, handle);
1371                 if (rc)
1372                         return rc;
1373
1374                 rc = mdd_declare_links_del(env, c, handle);
1375                 if (rc != 0)
1376                         return rc;
1377
1378                 /* FIXME: need changelog for remove entry */
1379                 rc = mdd_declare_changelog_store(env, mdd, name, handle);
1380         }
1381
1382         return rc;
1383 }
1384
1385 /**
1386  * Delete name entry and the object.
1387  * Note: no_name == 1 means it only destory the object, i.e. name_entry
1388  * does not exist for this object, and it could only happen during resending
1389  * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1390  * is also needed in this case(needed by changelog), so we have to add another
1391  * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1392  * the ENOENT failure should be able to be fixed by redo mechanism.
1393  */
1394 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1395                       struct md_object *cobj, const struct lu_name *lname,
1396                       struct md_attr *ma, int no_name)
1397 {
1398         const char *name = lname->ln_name;
1399         struct lu_attr     *cattr = &mdd_env_info(env)->mti_cattr;
1400         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
1401         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1402         struct mdd_object *mdd_cobj = NULL;
1403         struct mdd_device *mdd = mdo2mdd(pobj);
1404         struct dynlock_handle *dlh;
1405         struct thandle    *handle;
1406         int rc, is_dir = 0;
1407         ENTRY;
1408
1409         /* cobj == NULL means only delete name entry */
1410         if (likely(cobj != NULL)) {
1411                 mdd_cobj = md2mdd_obj(cobj);
1412                 if (mdd_object_exists(mdd_cobj) == 0)
1413                         RETURN(-ENOENT);
1414                 /* currently it is assume, it could only delete
1415                  * name entry of remote directory */
1416                 is_dir = 1;
1417         }
1418
1419         handle = mdd_trans_create(env, mdd);
1420         if (IS_ERR(handle))
1421                 RETURN(PTR_ERR(handle));
1422
1423         rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1424                                 lname, ma, handle, no_name);
1425         if (rc)
1426                 GOTO(stop, rc);
1427
1428         rc = mdd_trans_start(env, mdd, handle);
1429         if (rc)
1430                 GOTO(stop, rc);
1431
1432         dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1433         if (dlh == NULL)
1434                 GOTO(stop, rc = -ENOMEM);
1435
1436         if (likely(mdd_cobj != NULL)) {
1437                 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1438
1439                 /* fetch cattr */
1440                 rc = mdd_la_get(env, mdd_cobj, cattr,
1441                                 mdd_object_capa(env, mdd_cobj));
1442                 if (rc)
1443                         GOTO(cleanup, rc);
1444
1445                 is_dir = S_ISDIR(cattr->la_mode);
1446
1447         }
1448
1449         rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, cattr);
1450         if (rc)
1451                 GOTO(cleanup, rc);
1452
1453         if (likely(no_name == 0)) {
1454                 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1455                                         mdd_object_capa(env, mdd_pobj));
1456                 if (rc)
1457                         GOTO(cleanup, rc);
1458         }
1459
1460         if (likely(mdd_cobj != NULL)) {
1461                 rc = mdo_ref_del(env, mdd_cobj, handle);
1462                 if (rc != 0) {
1463                         __mdd_index_insert_only(env, mdd_pobj,
1464                                                 mdo2fid(mdd_cobj),
1465                                                 name, handle,
1466                                                 mdd_object_capa(env, mdd_pobj));
1467                         GOTO(cleanup, rc);
1468                 }
1469
1470                 if (is_dir)
1471                         /* unlink dot */
1472                         mdo_ref_del(env, mdd_cobj, handle);
1473
1474                 /* fetch updated nlink */
1475                 rc = mdd_la_get(env, mdd_cobj, cattr,
1476                                 mdd_object_capa(env, mdd_cobj));
1477                 if (rc)
1478                         GOTO(cleanup, rc);
1479         }
1480
1481         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1482         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1483
1484         la->la_valid = LA_CTIME | LA_MTIME;
1485         rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1486         if (rc)
1487                 GOTO(cleanup, rc);
1488
1489         /* Enough for only unlink the entry */
1490         if (unlikely(mdd_cobj == NULL)) {
1491                 mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1492                 GOTO(stop, rc);
1493         }
1494
1495         if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1496                 /* update ctime of an unlinked file only if it is still
1497                  * opened or a link still exists */
1498                 la->la_valid = LA_CTIME;
1499                 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1500                 if (rc)
1501                         GOTO(cleanup, rc);
1502         }
1503
1504         /* XXX: this transfer to ma will be removed with LOD/OSP */
1505         ma->ma_attr = *cattr;
1506         ma->ma_valid |= MA_INODE;
1507         rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1508
1509         /* fetch updated nlink */
1510         if (rc == 0)
1511                 rc = mdd_la_get(env, mdd_cobj, cattr,
1512                                 mdd_object_capa(env, mdd_cobj));
1513
1514         if (!is_dir)
1515                 /* old files may not have link ea; ignore errors */
1516                 mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
1517
1518         /* if object is removed then we can't get its attrs, use last get */
1519         if (cattr->la_nlink == 0) {
1520                 ma->ma_attr = *cattr;
1521                 ma->ma_valid |= MA_INODE;
1522         }
1523         EXIT;
1524 cleanup:
1525         mdd_write_unlock(env, mdd_cobj);
1526         mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1527         if (rc == 0) {
1528                 int cl_flags;
1529
1530                 cl_flags = (cattr->la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1531                 if ((ma->ma_valid & MA_HSM) &&
1532                     (ma->ma_hsm.mh_flags & HS_EXISTS))
1533                         cl_flags |= CLF_UNLINK_HSM_EXISTS;
1534
1535                 rc = mdd_changelog_ns_store(env, mdd,
1536                         is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1537                         mdd_cobj, mdd_pobj, lname, handle);
1538         }
1539
1540 stop:
1541         mdd_trans_stop(env, mdd, rc, handle);
1542
1543         return rc;
1544 }
1545
1546 /*
1547  * The permission has been checked when obj created, no need check again.
1548  */
1549 static int mdd_cd_sanity_check(const struct lu_env *env,
1550                                struct mdd_object *obj)
1551 {
1552         ENTRY;
1553
1554         /* EEXIST check */
1555         if (!obj || mdd_is_dead_obj(obj))
1556                 RETURN(-ENOENT);
1557
1558         RETURN(0);
1559
1560 }
1561
1562 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1563                            struct md_object *cobj, const struct md_op_spec *spec,
1564                            struct md_attr *ma)
1565 {
1566         struct mdd_device *mdd = mdo2mdd(cobj);
1567         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1568         struct mdd_object *son = md2mdd_obj(cobj);
1569         struct thandle    *handle;
1570         const struct lu_buf *buf;
1571         struct lu_attr    *attr = &mdd_env_info(env)->mti_cattr;
1572         int                rc;
1573         ENTRY;
1574
1575         rc = mdd_cd_sanity_check(env, son);
1576         if (rc)
1577                 RETURN(rc);
1578
1579         if (!md_should_create(spec->sp_cr_flags))
1580                 RETURN(0);
1581
1582         /*
1583          * there are following use cases for this function:
1584          * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1585          *    striping can be specified or not
1586          * 2) CMD?
1587          */
1588         rc = mdd_la_get(env, son, attr, mdd_object_capa(env, son));
1589         if (rc)
1590                 RETURN(rc);
1591
1592         /* calling ->ah_make_hint() is used to transfer information from parent */
1593         mdd_object_make_hint(env, mdd_pobj, son, attr);
1594
1595         handle = mdd_trans_create(env, mdd);
1596         if (IS_ERR(handle))
1597                 GOTO(out_free, rc = PTR_ERR(handle));
1598
1599         /*
1600          * XXX: Setting the lov ea is not locked but setting the attr is locked?
1601          * Should this be fixed?
1602          */
1603         CDEBUG(D_OTHER, "ea %p/%u, cr_flags %Lo, no_create %u\n",
1604                spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1605                spec->sp_cr_flags, spec->no_create);
1606
1607         if (spec->no_create || spec->sp_cr_flags & MDS_OPEN_HAS_EA) {
1608                 /* replay case or lfs setstripe */
1609                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1610                                         spec->u.sp_ea.eadatalen);
1611         } else {
1612                 buf = &LU_BUF_NULL;
1613         }
1614
1615         rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1616                                   XATTR_NAME_LOV, 0, handle);
1617         if (rc)
1618                 GOTO(stop, rc);
1619
1620         rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1621         if (rc)
1622                 GOTO(stop, rc);
1623
1624         rc = mdd_trans_start(env, mdd, handle);
1625         if (rc)
1626                 GOTO(stop, rc);
1627
1628         rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1629                           0, handle, mdd_object_capa(env, son));
1630
1631         if (rc)
1632                 GOTO(stop, rc);
1633
1634         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1635
1636 stop:
1637         mdd_trans_stop(env, mdd, rc, handle);
1638 out_free:
1639         RETURN(rc);
1640 }
1641
1642 /* Get fid from name and parent */
1643 static int
1644 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1645              const struct lu_name *lname, struct lu_fid* fid, int mask)
1646 {
1647         const char          *name = lname->ln_name;
1648         const struct dt_key *key = (const struct dt_key *)name;
1649         struct mdd_object   *mdd_obj = md2mdd_obj(pobj);
1650         struct mdd_device   *m = mdo2mdd(pobj);
1651         struct dt_object    *dir = mdd_object_child(mdd_obj);
1652         int rc;
1653         ENTRY;
1654
1655         if (unlikely(mdd_is_dead_obj(mdd_obj)))
1656                 RETURN(-ESTALE);
1657
1658         if (mdd_object_remote(mdd_obj)) {
1659                 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
1660                        mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
1661         } else if (!mdd_object_exists(mdd_obj)) {
1662                 RETURN(-ESTALE);
1663         }
1664
1665         /* The common filename length check. */
1666         if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1667                 RETURN(-ENAMETOOLONG);
1668
1669         rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1670                                             MOR_TGT_PARENT);
1671         if (rc)
1672                 RETURN(rc);
1673
1674         if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1675                    dt_try_as_dir(env, dir))) {
1676
1677                 rc = dir->do_index_ops->dio_lookup(env, dir,
1678                                                  (struct dt_rec *)fid, key,
1679                                                  mdd_object_capa(env, mdd_obj));
1680                 if (rc > 0)
1681                         rc = 0;
1682                 else if (rc == 0)
1683                         rc = -ENOENT;
1684         } else
1685                 rc = -ENOTDIR;
1686
1687         RETURN(rc);
1688 }
1689
1690 static int mdd_declare_object_initialize(const struct lu_env *env,
1691                                          struct mdd_object *parent,
1692                                          struct mdd_object *child,
1693                                          struct lu_attr *attr,
1694                                          struct thandle *handle,
1695                                          struct linkea_data *ldata)
1696 {
1697         int rc;
1698         ENTRY;
1699
1700         /*
1701          * inode mode has been set in creation time, and it's based on umask,
1702          * la_mode and acl, don't set here again! (which will go wrong
1703          * because below function doesn't consider umask).
1704          * I'd suggest set all object attributes in creation time, see above.
1705          */
1706         LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1707         attr->la_valid &= ~(LA_MODE | LA_TYPE);
1708         rc = mdo_declare_attr_set(env, child, attr, handle);
1709         attr->la_valid |= LA_MODE | LA_TYPE;
1710         if (rc == 0 && S_ISDIR(attr->la_mode)) {
1711                 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1712                                               dot, handle);
1713                 if (rc == 0)
1714                         rc = mdo_declare_ref_add(env, child, handle);
1715
1716                 rc = mdo_declare_index_insert(env, child, mdo2fid(parent),
1717                                               dotdot, handle);
1718         }
1719
1720         if (rc == 0)
1721                 mdd_declare_links_add(env, child, handle, ldata);
1722
1723         RETURN(rc);
1724 }
1725
1726 static int mdd_object_initialize(const struct lu_env *env,
1727                                  const struct lu_fid *pfid,
1728                                  const struct lu_name *lname,
1729                                  struct mdd_object *child,
1730                                  struct lu_attr *attr, struct thandle *handle,
1731                                  const struct md_op_spec *spec,
1732                                  struct linkea_data *ldata)
1733 {
1734         int rc;
1735         ENTRY;
1736
1737         /*
1738          * Update attributes for child.
1739          *
1740          * FIXME:
1741          *  (1) the valid bits should be converted between Lustre and Linux;
1742          *  (2) maybe, the child attributes should be set in OSD when creation.
1743          */
1744
1745         rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1746         /* arguments are supposed to stay the same */
1747         if (S_ISDIR(attr->la_mode)) {
1748                 /* Add "." and ".." for newly created dir */
1749                 mdo_ref_add(env, child, handle);
1750                 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1751                                              dot, handle, BYPASS_CAPA);
1752                 if (rc == 0)
1753                         rc = __mdd_index_insert_only(env, child, pfid,
1754                                                      dotdot, handle,
1755                                                      BYPASS_CAPA);
1756                 if (rc != 0)
1757                         mdo_ref_del(env, child, handle);
1758         }
1759
1760         if (rc == 0)
1761                 mdd_links_add(env, child, pfid, lname, handle, ldata, 1);
1762
1763         RETURN(rc);
1764 }
1765
1766 /* has not lock on pobj yet */
1767 static int mdd_create_sanity_check(const struct lu_env *env,
1768                                    struct md_object *pobj,
1769                                    struct lu_attr *pattr,
1770                                    const struct lu_name *lname,
1771                                    struct lu_attr *cattr,
1772                                    struct md_op_spec *spec)
1773 {
1774         struct mdd_thread_info *info = mdd_env_info(env);
1775         struct lu_fid     *fid       = &info->mti_fid;
1776         struct mdd_object *obj       = md2mdd_obj(pobj);
1777         struct mdd_device *m         = mdo2mdd(pobj);
1778         int rc;
1779         ENTRY;
1780
1781         /* EEXIST check */
1782         if (mdd_is_dead_obj(obj))
1783                 RETURN(-ENOENT);
1784
1785         /*
1786          * In some cases this lookup is not needed - we know before if name
1787          * exists or not because MDT performs lookup for it.
1788          * name length check is done in lookup.
1789          */
1790         if (spec->sp_cr_lookup) {
1791                 /*
1792                  * Check if the name already exist, though it will be checked in
1793                  * _index_insert also, for avoiding rolling back if exists
1794                  * _index_insert.
1795                  */
1796                 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1797                                          MAY_WRITE | MAY_EXEC);
1798                 if (rc != -ENOENT)
1799                         RETURN(rc ? : -EEXIST);
1800         } else {
1801                 /*
1802                  * Check WRITE permission for the parent.
1803                  * EXEC permission have been checked
1804                  * when lookup before create already.
1805                  */
1806                 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1807                                                     MOR_TGT_PARENT);
1808                 if (rc)
1809                         RETURN(rc);
1810         }
1811
1812         /* sgid check */
1813         if (pattr->la_mode & S_ISGID) {
1814                 cattr->la_gid = pattr->la_gid;
1815                 if (S_ISDIR(cattr->la_mode)) {
1816                         cattr->la_mode |= S_ISGID;
1817                         cattr->la_valid |= LA_MODE;
1818                 }
1819         }
1820
1821         switch (cattr->la_mode & S_IFMT) {
1822         case S_IFLNK: {
1823                 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1824
1825                 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1826                         RETURN(-ENAMETOOLONG);
1827                 else
1828                         RETURN(0);
1829         }
1830         case S_IFDIR:
1831         case S_IFREG:
1832         case S_IFCHR:
1833         case S_IFBLK:
1834         case S_IFIFO:
1835         case S_IFSOCK:
1836                 rc = 0;
1837                 break;
1838         default:
1839                 rc = -EINVAL;
1840                 break;
1841         }
1842         RETURN(rc);
1843 }
1844
1845 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1846                               struct mdd_object *p, struct mdd_object *c,
1847                               const struct lu_name *name,
1848                               struct lu_attr *attr,
1849                               int got_def_acl,
1850                               struct thandle *handle,
1851                               const struct md_op_spec *spec,
1852                               struct linkea_data *ldata)
1853 {
1854         int rc;
1855
1856         rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1857         if (rc)
1858                 GOTO(out, rc);
1859
1860 #ifdef CONFIG_FS_POSIX_ACL
1861         if (got_def_acl > 0) {
1862                 struct lu_buf *acl_buf;
1863
1864                 acl_buf = mdd_buf_get(env, NULL, got_def_acl);
1865                 /* if dir, then can inherit default ACl */
1866                 if (S_ISDIR(attr->la_mode)) {
1867                         rc = mdo_declare_xattr_set(env, c, acl_buf,
1868                                                    XATTR_NAME_ACL_DEFAULT,
1869                                                    0, handle);
1870                         if (rc)
1871                                 GOTO(out, rc);
1872                 }
1873
1874                 rc = mdo_declare_attr_set(env, c, attr, handle);
1875                 if (rc)
1876                         GOTO(out, rc);
1877
1878                 rc = mdo_declare_xattr_set(env, c, acl_buf,
1879                                            XATTR_NAME_ACL_ACCESS, 0, handle);
1880                 if (rc)
1881                         GOTO(out, rc);
1882         }
1883 #endif
1884
1885         if (S_ISDIR(attr->la_mode)) {
1886                 rc = mdo_declare_ref_add(env, p, handle);
1887                 if (rc)
1888                         GOTO(out, rc);
1889         }
1890
1891         rc = mdd_declare_object_initialize(env, p, c, attr, handle, ldata);
1892         if (rc)
1893                 GOTO(out, rc);
1894
1895         if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
1896                 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
1897         else
1898                 rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1899                                               name->ln_name, handle);
1900         if (rc)
1901                 GOTO(out, rc);
1902
1903         /* replay case, create LOV EA from client data */
1904         if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1905                 const struct lu_buf *buf;
1906
1907                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1908                                         spec->u.sp_ea.eadatalen);
1909                 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1910                                            0, handle);
1911                 if (rc)
1912                         GOTO(out, rc);
1913         }
1914
1915         if (S_ISLNK(attr->la_mode)) {
1916                 rc = dt_declare_record_write(env, mdd_object_child(c),
1917                                              strlen(spec->u.sp_symname), 0,
1918                                              handle);
1919                 if (rc)
1920                         GOTO(out, rc);
1921         }
1922
1923         if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
1924                 rc = mdo_declare_attr_set(env, p, attr, handle);
1925                 if (rc)
1926                         return rc;
1927         }
1928
1929         rc = mdd_declare_changelog_store(env, mdd, name, handle);
1930         if (rc)
1931                 return rc;
1932
1933 out:
1934         return rc;
1935 }
1936
1937 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
1938                         struct lu_attr *la, struct lu_buf *acl_buf,
1939                         int *got_def_acl, int *reset_acl)
1940 {
1941         int     rc;
1942         ENTRY;
1943
1944         if (S_ISLNK(la->la_mode))
1945                 RETURN(0);
1946
1947         mdd_read_lock(env, pobj, MOR_TGT_PARENT);
1948         rc = mdo_xattr_get(env, pobj, acl_buf,
1949                            XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1950         mdd_read_unlock(env, pobj);
1951         if (rc > 0) {
1952                 /* If there are default ACL, fix mode by default ACL */
1953                 *got_def_acl = rc;
1954                 acl_buf->lb_len = rc;
1955                 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
1956                 if (rc < 0)
1957                         RETURN(rc);
1958                 *reset_acl = rc;
1959         } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
1960                 /* If there are no default ACL, fix mode by mask */
1961                 struct lu_ucred *uc = lu_ucred(env);
1962
1963                 /* The create triggered by MDT internal events, such as
1964                  * LFSCK reset, will not contain valid "uc". */
1965                 if (unlikely(uc != NULL))
1966                         la->la_mode &= ~uc->uc_umask;
1967                 rc = 0;
1968         }
1969
1970         RETURN(rc);
1971 }
1972
1973 /*
1974  * Create object and insert it into namespace.
1975  */
1976 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
1977                       const struct lu_name *lname, struct md_object *child,
1978                       struct md_op_spec *spec, struct md_attr* ma)
1979 {
1980         struct mdd_thread_info  *info = mdd_env_info(env);
1981         struct lu_attr          *la = &info->mti_la_for_fix;
1982         struct mdd_object       *mdd_pobj = md2mdd_obj(pobj);
1983         struct mdd_object       *son = md2mdd_obj(child);
1984         struct mdd_device       *mdd = mdo2mdd(pobj);
1985         struct lu_attr          *attr = &ma->ma_attr;
1986         struct thandle          *handle;
1987         struct lu_attr          *pattr = &info->mti_pattr;
1988         struct lu_buf           acl_buf;
1989         struct linkea_data      *ldata = &info->mti_link_data;
1990         struct dynlock_handle   *dlh;
1991         const char              *name = lname->ln_name;
1992         int                      rc, created = 0, initialized = 0, inserted = 0;
1993         int                      got_def_acl = 0;
1994         int                      reset_acl = 0;
1995         ENTRY;
1996
1997         /*
1998          * Two operations have to be performed:
1999          *
2000          *  - an allocation of a new object (->do_create()), and
2001          *
2002          *  - an insertion into a parent index (->dio_insert()).
2003          *
2004          * Due to locking, operation order is not important, when both are
2005          * successful, *but* error handling cases are quite different:
2006          *
2007          *  - if insertion is done first, and following object creation fails,
2008          *  insertion has to be rolled back, but this operation might fail
2009          *  also leaving us with dangling index entry.
2010          *
2011          *  - if creation is done first, is has to be undone if insertion
2012          *  fails, leaving us with leaked space, which is neither good, nor
2013          *  fatal.
2014          *
2015          * It seems that creation-first is simplest solution, but it is
2016          * sub-optimal in the frequent
2017          *
2018          *         $ mkdir foo
2019          *         $ mkdir foo
2020          *
2021          * case, because second mkdir is bound to create object, only to
2022          * destroy it immediately.
2023          *
2024          * To avoid this follow local file systems that do double lookup:
2025          *
2026          *     0. lookup -> -EEXIST (mdd_create_sanity_check())
2027          *
2028          *     1. create            (mdd_object_create_internal())
2029          *
2030          *     2. insert            (__mdd_index_insert(), lookup again)
2031          */
2032
2033         rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
2034         if (rc != 0)
2035                 RETURN(rc);
2036
2037         /* Sanity checks before big job. */
2038         rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2039         if (rc)
2040                 RETURN(rc);
2041
2042         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2043                 GOTO(out_free, rc = -EINPROGRESS);
2044
2045         acl_buf.lb_buf = info->mti_xattr_buf;
2046         acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2047         rc = mdd_acl_init(env, mdd_pobj, attr, &acl_buf, &got_def_acl,
2048                           &reset_acl);
2049         if (rc < 0)
2050                 GOTO(out_free, rc);
2051
2052         mdd_object_make_hint(env, mdd_pobj, son, attr);
2053
2054         handle = mdd_trans_create(env, mdd);
2055         if (IS_ERR(handle))
2056                 GOTO(out_free, rc = PTR_ERR(handle));
2057
2058         memset(ldata, 0, sizeof(*ldata));
2059         mdd_linkea_prepare(env, son, NULL, NULL, mdd_object_fid(mdd_pobj),
2060                            lname, 1, 0, ldata);
2061         rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2062                                 got_def_acl, handle, spec, ldata);
2063         if (rc)
2064                 GOTO(out_stop, rc);
2065
2066         rc = mdd_trans_start(env, mdd, handle);
2067         if (rc)
2068                 GOTO(out_stop, rc);
2069
2070         dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
2071         if (dlh == NULL)
2072                 GOTO(out_trans, rc = -ENOMEM);
2073
2074         mdd_write_lock(env, son, MOR_TGT_CHILD);
2075         rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec);
2076         if (rc) {
2077                 mdd_write_unlock(env, son);
2078                 GOTO(cleanup, rc);
2079         }
2080
2081         created = 1;
2082
2083 #ifdef CONFIG_FS_POSIX_ACL
2084         if (got_def_acl) {
2085                 /* set default acl */
2086                 if (S_ISDIR(attr->la_mode)) {
2087                         LASSERTF(acl_buf.lb_len  == got_def_acl,
2088                                  "invalid acl_buf: %p:%d got_def %d\n",
2089                                  acl_buf.lb_buf, (int)acl_buf.lb_len,
2090                                  got_def_acl);
2091                         rc = mdo_xattr_set(env, son, &acl_buf,
2092                                            XATTR_NAME_ACL_DEFAULT, 0,
2093                                            handle, BYPASS_CAPA);
2094                         if (rc) {
2095                                 mdd_write_unlock(env, son);
2096                                 GOTO(cleanup, rc);
2097                         }
2098                 }
2099
2100                 /* set its own acl */
2101                 if (reset_acl) {
2102                         LASSERTF(acl_buf.lb_buf != NULL && acl_buf.lb_len != 0,
2103                                  "invalid acl_buf %p:%d\n", acl_buf.lb_buf,
2104                                  (int)acl_buf.lb_len);
2105                         rc = mdo_xattr_set(env, son, &acl_buf,
2106                                            XATTR_NAME_ACL_ACCESS,
2107                                            0, handle, BYPASS_CAPA);
2108                         if (rc) {
2109                                 mdd_write_unlock(env, son);
2110                                 GOTO(cleanup, rc);
2111                         }
2112                 }
2113         }
2114 #endif
2115
2116         rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
2117                                    son, attr, handle, spec, ldata);
2118
2119         /*
2120          * in case of replay we just set LOVEA provided by the client
2121          * XXX: I think it would be interesting to try "old" way where
2122          *      MDT calls this xattr_set(LOV) in a different transaction.
2123          *      probably this way we code can be made better.
2124          */
2125         if (rc == 0 && (spec->no_create ||
2126                         (spec->sp_cr_flags & MDS_OPEN_HAS_EA))) {
2127                 const struct lu_buf *buf;
2128
2129                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2130                                 spec->u.sp_ea.eadatalen);
2131                 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
2132                                 BYPASS_CAPA);
2133         }
2134
2135         if (rc == 0 && spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2136                 rc = __mdd_orphan_add(env, son, handle);
2137
2138         mdd_write_unlock(env, son);
2139
2140         if (rc != 0)
2141                 /*
2142                  * Object has no links, so it will be destroyed when last
2143                  * reference is released. (XXX not now.)
2144                  */
2145                 GOTO(cleanup, rc);
2146
2147         initialized = 1;
2148
2149         if (!(spec->sp_cr_flags & MDS_OPEN_VOLATILE))
2150                 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2151                                         name, S_ISDIR(attr->la_mode), handle,
2152                                         mdd_object_capa(env, mdd_pobj));
2153
2154         if (rc != 0)
2155                 GOTO(cleanup, rc);
2156
2157         inserted = 1;
2158
2159         if (S_ISLNK(attr->la_mode)) {
2160                 struct lu_ucred  *uc = lu_ucred_assert(env);
2161                 struct dt_object *dt = mdd_object_child(son);
2162                 const char *target_name = spec->u.sp_symname;
2163                 int sym_len = strlen(target_name);
2164                 const struct lu_buf *buf;
2165                 loff_t pos = 0;
2166
2167                 buf = mdd_buf_get_const(env, target_name, sym_len);
2168                 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2169                                                 mdd_object_capa(env, son),
2170                                                 uc->uc_cap &
2171                                                 CFS_CAP_SYS_RESOURCE_MASK);
2172
2173                 if (rc == sym_len)
2174                         rc = 0;
2175                 else
2176                         GOTO(cleanup, rc = -EFAULT);
2177         }
2178
2179         /* volatile file creation does not update parent directory times */
2180         if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2181                 GOTO(cleanup, rc = 0);
2182
2183         /* update parent directory mtime/ctime */
2184         *la = *attr;
2185         la->la_valid = LA_CTIME | LA_MTIME;
2186         rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
2187         if (rc)
2188                 GOTO(cleanup, rc);
2189
2190         EXIT;
2191 cleanup:
2192         if (rc != 0 && created != 0) {
2193                 int rc2;
2194
2195                 if (inserted != 0) {
2196                         if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2197                                 rc2 = __mdd_orphan_del(env, son, handle);
2198                         else
2199                                 rc2 = __mdd_index_delete(env, mdd_pobj, name,
2200                                                          S_ISDIR(attr->la_mode),
2201                                                          handle, BYPASS_CAPA);
2202                         if (rc2 != 0)
2203                                 goto out_stop;
2204                 }
2205
2206                 mdd_write_lock(env, son, MOR_TGT_CHILD);
2207                 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
2208                         /* Drop the reference, no need to delete "."/"..",
2209                          * because the object to be destroied directly. */
2210                         rc2 = mdo_ref_del(env, son, handle);
2211                         if (rc2 != 0) {
2212                                 mdd_write_unlock(env, son);
2213                                 goto out_stop;
2214                         }
2215                 }
2216
2217                 rc2 = mdo_ref_del(env, son, handle);
2218                 if (rc2 != 0) {
2219                         mdd_write_unlock(env, son);
2220                         goto out_stop;
2221                 }
2222
2223                 mdo_destroy(env, son, handle);
2224                 mdd_write_unlock(env, son);
2225         }
2226
2227         mdd_pdo_write_unlock(env, mdd_pobj, dlh);
2228 out_trans:
2229         if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)))
2230                 rc = mdd_changelog_ns_store(env, mdd,
2231                         S_ISDIR(attr->la_mode) ? CL_MKDIR :
2232                         S_ISREG(attr->la_mode) ? CL_CREATE :
2233                         S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2234                         0, son, mdd_pobj, lname, handle);
2235 out_stop:
2236         mdd_trans_stop(env, mdd, rc, handle);
2237 out_free:
2238         if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2239                 /* if we vmalloced a large buffer drop it */
2240                 lu_buf_free(ldata->ld_buf);
2241
2242         /* The child object shouldn't be cached anymore */
2243         if (rc)
2244                 set_bit(LU_OBJECT_HEARD_BANSHEE,
2245                             &child->mo_lu.lo_header->loh_flags);
2246         return rc;
2247 }
2248
2249 /*
2250  * Get locks on parents in proper order
2251  * RETURN: < 0 - error, rename_order if successful
2252  */
2253 enum rename_order {
2254         MDD_RN_SAME,
2255         MDD_RN_SRCTGT,
2256         MDD_RN_TGTSRC
2257 };
2258
2259 static int mdd_rename_order(const struct lu_env *env,
2260                             struct mdd_device *mdd,
2261                             struct mdd_object *src_pobj,
2262                             struct mdd_object *tgt_pobj)
2263 {
2264         /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2265         int rc;
2266         ENTRY;
2267
2268         if (src_pobj == tgt_pobj)
2269                 RETURN(MDD_RN_SAME);
2270
2271         /* compared the parent child relationship of src_p&tgt_p */
2272         if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2273                 rc = MDD_RN_SRCTGT;
2274         } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2275                 rc = MDD_RN_TGTSRC;
2276         } else {
2277                 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
2278                 if (rc == -EREMOTE)
2279                         rc = 0;
2280
2281                 if (rc == 1)
2282                         rc = MDD_RN_TGTSRC;
2283                 else if (rc == 0)
2284                         rc = MDD_RN_SRCTGT;
2285         }
2286
2287         RETURN(rc);
2288 }
2289
2290 /* has not mdd_write{read}_lock on any obj yet. */
2291 static int mdd_rename_sanity_check(const struct lu_env *env,
2292                                    struct mdd_object *src_pobj,
2293                                    struct mdd_object *tgt_pobj,
2294                                    struct mdd_object *sobj,
2295                                    struct mdd_object *tobj,
2296                                    struct lu_attr *so_attr,
2297                                    struct lu_attr *tg_attr)
2298 {
2299         int rc = 0;
2300         ENTRY;
2301
2302         /* XXX: when get here, sobj must NOT be NULL,
2303          * the other case has been processed in cld_rename
2304          * before mdd_rename and enable MDS_PERM_BYPASS. */
2305         LASSERT(sobj);
2306
2307         rc = mdd_may_delete(env, src_pobj, sobj, so_attr, NULL, 1, 0);
2308         if (rc)
2309                 RETURN(rc);
2310
2311         /* XXX: when get here, "tobj == NULL" means tobj must
2312          * NOT exist (neither on remote MDS, such case has been
2313          * processed in cld_rename before mdd_rename and enable
2314          * MDS_PERM_BYPASS).
2315          * So check may_create, but not check may_unlink. */
2316         if (!tobj)
2317                 rc = mdd_may_create(env, tgt_pobj, NULL,
2318                                     (src_pobj != tgt_pobj), 0);
2319         else
2320                 rc = mdd_may_delete(env, tgt_pobj, tobj, tg_attr, so_attr,
2321                                     (src_pobj != tgt_pobj), 1);
2322
2323         if (!rc && !tobj && (src_pobj != tgt_pobj) &&
2324             S_ISDIR(so_attr->la_mode))
2325                 rc = __mdd_may_link(env, tgt_pobj);
2326
2327         RETURN(rc);
2328 }
2329
2330 static int mdd_declare_rename(const struct lu_env *env,
2331                               struct mdd_device *mdd,
2332                               struct mdd_object *mdd_spobj,
2333                               struct mdd_object *mdd_tpobj,
2334                               struct mdd_object *mdd_sobj,
2335                               struct mdd_object *mdd_tobj,
2336                               const struct lu_name *tname,
2337                               const struct lu_name *sname,
2338                               struct md_attr *ma,
2339                               struct linkea_data *ldata,
2340                               struct thandle *handle)
2341 {
2342         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
2343         int rc;
2344
2345         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2346         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2347
2348         LASSERT(mdd_spobj);
2349         LASSERT(mdd_tpobj);
2350         LASSERT(mdd_sobj);
2351
2352         /* name from source dir */
2353         rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2354         if (rc)
2355                 return rc;
2356
2357         /* .. from source child */
2358         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2359                 /* source child can be directory,
2360                  * counted by source dir's nlink */
2361                 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2362                 if (rc)
2363                         return rc;
2364                 if (mdd_spobj != mdd_tpobj) {
2365                         rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2366                                                       handle);
2367                         if (rc)
2368                                 return rc;
2369
2370                         rc = mdo_declare_index_insert(env, mdd_sobj,
2371                                                       mdo2fid(mdd_tpobj),
2372                                                       dotdot, handle);
2373                         if (rc)
2374                                 return rc;
2375                 }
2376                 /* new target child can be directory,
2377                  * counted by target dir's nlink */
2378                 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2379                 if (rc)
2380                         return rc;
2381
2382         }
2383
2384         la->la_valid = LA_CTIME | LA_MTIME;
2385         rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2386         if (rc != 0)
2387                 return rc;
2388
2389         rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2390         if (rc != 0)
2391                 return rc;
2392
2393         la->la_valid = LA_CTIME;
2394         rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2395         if (rc)
2396                 return rc;
2397
2398         rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata);
2399         if (rc)
2400                 return rc;
2401
2402         /* new name */
2403         rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2404                         tname->ln_name, handle);
2405         if (rc)
2406                 return rc;
2407
2408         /* name from target dir (old name), we declare it unconditionally
2409          * as mdd_rename() calls delete unconditionally as well. so just
2410          * to balance declarations vs calls to change ... */
2411         rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2412         if (rc)
2413                 return rc;
2414
2415         if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2416                 /* delete target child in target parent directory */
2417                 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2418                 if (rc)
2419                         return rc;
2420
2421                 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2422                         /* target child can be directory,
2423                          * delete "." reference in target child directory */
2424                         rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2425                         if (rc)
2426                                 return rc;
2427
2428                         /* delete ".." reference in target parent directory */
2429                         rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2430                         if (rc)
2431                                 return rc;
2432                 }
2433
2434                 la->la_valid = LA_CTIME;
2435                 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2436                 if (rc)
2437                         return rc;
2438
2439                 mdd_declare_links_del(env, mdd_tobj, handle);
2440                 if (rc)
2441                         return rc;
2442
2443                 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2444                 if (rc)
2445                         return rc;
2446         }
2447
2448         rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2449         if (rc)
2450                 return rc;
2451
2452         return rc;
2453 }
2454
2455 /* src object can be remote that is why we use only fid and type of object */
2456 static int mdd_rename(const struct lu_env *env,
2457                       struct md_object *src_pobj, struct md_object *tgt_pobj,
2458                       const struct lu_fid *lf, const struct lu_name *lsname,
2459                       struct md_object *tobj, const struct lu_name *ltname,
2460                       struct md_attr *ma)
2461 {
2462         const char *sname = lsname->ln_name;
2463         const char *tname = ltname->ln_name;
2464         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
2465         struct lu_attr    *so_attr = &mdd_env_info(env)->mti_cattr;
2466         struct lu_attr    *tg_attr = &mdd_env_info(env)->mti_pattr;
2467         struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2468         struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2469         struct mdd_device *mdd = mdo2mdd(src_pobj);
2470         struct mdd_object *mdd_sobj = NULL;                  /* source object */
2471         struct mdd_object *mdd_tobj = NULL;
2472         struct dynlock_handle *sdlh = NULL, *tdlh = NULL;
2473         struct thandle *handle;
2474         struct linkea_data  *ldata = &mdd_env_info(env)->mti_link_data;
2475         const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2476         const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2477         bool is_dir;
2478         bool tobj_ref = 0;
2479         bool tobj_locked = 0;
2480         unsigned cl_flags = 0;
2481         int rc, rc2;
2482         ENTRY;
2483
2484         if (tobj)
2485                 mdd_tobj = md2mdd_obj(tobj);
2486
2487         mdd_sobj = mdd_object_find(env, mdd, lf);
2488
2489         rc = mdd_la_get(env, mdd_sobj, so_attr,
2490                         mdd_object_capa(env, mdd_sobj));
2491         if (rc)
2492                 GOTO(out_pending, rc);
2493
2494         if (mdd_tobj) {
2495                 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2496                                 mdd_object_capa(env, mdd_tobj));
2497                 if (rc)
2498                         GOTO(out_pending, rc);
2499         }
2500
2501         rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj, mdd_sobj,
2502                                      mdd_tobj, so_attr, tg_attr);
2503         if (rc)
2504                 GOTO(out_pending, rc);
2505
2506         handle = mdd_trans_create(env, mdd);
2507         if (IS_ERR(handle))
2508                 GOTO(out_pending, rc = PTR_ERR(handle));
2509
2510         memset(ldata, 0, sizeof(*ldata));
2511         mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2512                            ltname, 1, 0, ldata);
2513         rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2514                                 mdd_tobj, lsname, ltname, ma, ldata, handle);
2515         if (rc)
2516                 GOTO(stop, rc);
2517
2518         rc = mdd_trans_start(env, mdd, handle);
2519         if (rc)
2520                 GOTO(stop, rc);
2521
2522         /* FIXME: Should consider tobj and sobj too in rename_lock. */
2523         rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2524         if (rc < 0)
2525                 GOTO(cleanup_unlocked, rc);
2526
2527         /* Get locks in determined order */
2528         if (rc == MDD_RN_SAME) {
2529                 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2530                                           sname, MOR_SRC_PARENT);
2531                 /* check hashes to determine do we need one lock or two */
2532                 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2533                         tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2534                                 MOR_TGT_PARENT);
2535                 else
2536                         tdlh = sdlh;
2537         } else if (rc == MDD_RN_SRCTGT) {
2538                 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2539                 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2540         } else {
2541                 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2542                 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2543         }
2544         if (sdlh == NULL || tdlh == NULL)
2545                 GOTO(cleanup, rc = -ENOMEM);
2546
2547         is_dir = S_ISDIR(so_attr->la_mode);
2548
2549         /* Remove source name from source directory */
2550         rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2551                                 mdd_object_capa(env, mdd_spobj));
2552         if (rc)
2553                 GOTO(cleanup, rc);
2554
2555         /* "mv dir1 dir2" needs "dir1/.." link update */
2556         if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2557                 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2558                                         mdd_object_capa(env, mdd_sobj));
2559                 if (rc)
2560                         GOTO(fixup_spobj2, rc);
2561
2562                 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2563                                       handle, mdd_object_capa(env, mdd_sobj));
2564                 if (rc)
2565                         GOTO(fixup_spobj, rc);
2566         }
2567
2568         /* Remove target name from target directory
2569          * Here tobj can be remote one, so we do index_delete unconditionally
2570          * and -ENOENT is allowed.
2571          */
2572         rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2573                                 mdd_object_capa(env, mdd_tpobj));
2574         if (rc != 0) {
2575                 if (mdd_tobj) {
2576                         /* tname might been renamed to something else */
2577                         GOTO(fixup_spobj, rc);
2578                 }
2579                 if (rc != -ENOENT)
2580                         GOTO(fixup_spobj, rc);
2581         }
2582
2583         /* Insert new fid with target name into target dir */
2584         rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2585                                 mdd_object_capa(env, mdd_tpobj));
2586         if (rc)
2587                 GOTO(fixup_tpobj, rc);
2588
2589         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2590         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2591
2592         /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2593         if (mdd_sobj) {
2594                 la->la_valid = LA_CTIME;
2595                 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
2596                 if (rc)
2597                         GOTO(fixup_tpobj, rc);
2598         }
2599
2600         /* Remove old target object
2601          * For tobj is remote case cmm layer has processed
2602          * and set tobj to NULL then. So when tobj is NOT NULL,
2603          * it must be local one.
2604          */
2605         if (tobj && mdd_object_exists(mdd_tobj)) {
2606                 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2607                 tobj_locked = 1;
2608                 if (mdd_is_dead_obj(mdd_tobj)) {
2609                         /* shld not be dead, something is wrong */
2610                         CERROR("tobj is dead, something is wrong\n");
2611                         rc = -EINVAL;
2612                         goto cleanup;
2613                 }
2614                 mdo_ref_del(env, mdd_tobj, handle);
2615
2616                 /* Remove dot reference. */
2617                 if (S_ISDIR(tg_attr->la_mode))
2618                         mdo_ref_del(env, mdd_tobj, handle);
2619                 tobj_ref = 1;
2620
2621                 /* fetch updated nlink */
2622                 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2623                                 mdd_object_capa(env, mdd_tobj));
2624                 if (rc != 0) {
2625                         CERROR("%s: Failed to get nlink for tobj "
2626                                 DFID": rc = %d\n",
2627                                 mdd2obd_dev(mdd)->obd_name,
2628                                 PFID(tpobj_fid), rc);
2629                         GOTO(fixup_tpobj, rc);
2630                 }
2631
2632                 la->la_valid = LA_CTIME;
2633                 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2634                 if (rc != 0) {
2635                         CERROR("%s: Failed to set ctime for tobj "
2636                                 DFID": rc = %d\n",
2637                                 mdd2obd_dev(mdd)->obd_name,
2638                                 PFID(tpobj_fid), rc);
2639                         GOTO(fixup_tpobj, rc);
2640                 }
2641
2642                 /* XXX: this transfer to ma will be removed with LOD/OSP */
2643                 ma->ma_attr = *tg_attr;
2644                 ma->ma_valid |= MA_INODE;
2645                 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2646                 if (rc != 0) {
2647                         CERROR("%s: Failed to unlink tobj "
2648                                 DFID": rc = %d\n",
2649                                 mdd2obd_dev(mdd)->obd_name,
2650                                 PFID(tpobj_fid), rc);
2651                         GOTO(fixup_tpobj, rc);
2652                 }
2653
2654                 /* fetch updated nlink */
2655                 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2656                                 mdd_object_capa(env, mdd_tobj));
2657                 if (rc != 0) {
2658                         CERROR("%s: Failed to get nlink for tobj "
2659                                 DFID": rc = %d\n",
2660                                 mdd2obd_dev(mdd)->obd_name,
2661                                 PFID(tpobj_fid), rc);
2662                         GOTO(fixup_tpobj, rc);
2663                 }
2664                 /* XXX: this transfer to ma will be removed with LOD/OSP */
2665                 ma->ma_attr = *tg_attr;
2666                 ma->ma_valid |= MA_INODE;
2667
2668                 if (so_attr->la_nlink == 0)
2669                         cl_flags |= CLF_RENAME_LAST;
2670         }
2671
2672         la->la_valid = LA_CTIME | LA_MTIME;
2673         rc = mdd_attr_check_set_internal(env, mdd_spobj, la, handle, 0);
2674         if (rc)
2675                 GOTO(fixup_tpobj, rc);
2676
2677         if (mdd_spobj != mdd_tpobj) {
2678                 la->la_valid = LA_CTIME | LA_MTIME;
2679                 rc = mdd_attr_check_set_internal(env, mdd_tpobj, la,
2680                                                  handle, 0);
2681         }
2682
2683         if (rc == 0 && mdd_sobj) {
2684                 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2685                 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2686                                       mdo2fid(mdd_tpobj), ltname, handle, NULL,
2687                                       0, 0);
2688                 if (rc == -ENOENT)
2689                         /* Old files might not have EA entry */
2690                         mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2691                                       lsname, handle, NULL, 0);
2692                 mdd_write_unlock(env, mdd_sobj);
2693                 /* We don't fail the transaction if the link ea can't be
2694                    updated -- fid2path will use alternate lookup method. */
2695                 rc = 0;
2696         }
2697
2698         EXIT;
2699
2700 fixup_tpobj:
2701         if (rc) {
2702                 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2703                                          BYPASS_CAPA);
2704                 if (rc2)
2705                         CWARN("tp obj fix error %d\n",rc2);
2706
2707                 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2708                     !mdd_is_dead_obj(mdd_tobj)) {
2709                         if (tobj_ref) {
2710                                 mdo_ref_add(env, mdd_tobj, handle);
2711                                 if (is_dir)
2712                                         mdo_ref_add(env, mdd_tobj, handle);
2713                         }
2714
2715                         rc2 = __mdd_index_insert(env, mdd_tpobj,
2716                                          mdo2fid(mdd_tobj), tname,
2717                                          is_dir, handle,
2718                                          BYPASS_CAPA);
2719
2720                         if (rc2)
2721                                 CWARN("tp obj fix error %d\n",rc2);
2722                 }
2723         }
2724
2725 fixup_spobj:
2726         if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2727                 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2728                                               BYPASS_CAPA);
2729
2730                 if (rc2)
2731                         CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2732                                mdd2obd_dev(mdd)->obd_name, rc2);
2733
2734
2735                 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2736                                               dotdot, handle, BYPASS_CAPA);
2737                 if (rc2)
2738                         CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2739                               mdd2obd_dev(mdd)->obd_name, rc2);
2740         }
2741
2742 fixup_spobj2:
2743         if (rc) {
2744                 rc2 = __mdd_index_insert(env, mdd_spobj,
2745                                          lf, sname, is_dir, handle, BYPASS_CAPA);
2746                 if (rc2)
2747                         CWARN("sp obj fix error %d\n",rc2);
2748         }
2749 cleanup:
2750         if (tobj_locked)
2751                 mdd_write_unlock(env, mdd_tobj);
2752         if (likely(tdlh) && sdlh != tdlh)
2753                 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2754         if (likely(sdlh))
2755                 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2756 cleanup_unlocked:
2757         if (rc == 0)
2758                 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2759                                                 mdd_tobj, tpobj_fid, lf,
2760                                                 spobj_fid, ltname, lsname,
2761                                                 handle);
2762
2763 stop:
2764         mdd_trans_stop(env, mdd, rc, handle);
2765 out_pending:
2766         mdd_object_put(env, mdd_sobj);
2767         return rc;
2768 }
2769
2770 const struct md_dir_operations mdd_dir_ops = {
2771         .mdo_is_subdir     = mdd_is_subdir,
2772         .mdo_lookup        = mdd_lookup,
2773         .mdo_create        = mdd_create,
2774         .mdo_rename        = mdd_rename,
2775         .mdo_link          = mdd_link,
2776         .mdo_unlink        = mdd_unlink,
2777         .mdo_create_data   = mdd_create_data,
2778 };