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