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