Whamcloud - gitweb
b4725961b24e39639ae59abde9ec15e101731ef6
[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, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdd/mdd_dir.c
37  *
38  * Lustre Metadata Server (mdd) routines
39  *
40  * Author: Wang Di <wangdi@intel.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_MDS
44
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
49
50 #include "mdd_internal.h"
51
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
54
55 static struct lu_name lname_dotdot = {
56         (char *) dotdot,
57         sizeof(dotdot) - 1
58 };
59
60 static inline int
61 mdd_name_check(struct mdd_device *m, const struct lu_name *ln)
62 {
63         if (!lu_name_is_valid(ln))
64                 return -EINVAL;
65         else if (ln->ln_namelen > m->mdd_dt_conf.ddp_max_name_len)
66                 return -ENAMETOOLONG;
67         else
68                 return 0;
69 }
70
71 /* Get FID from name and parent */
72 static int
73 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
74              const struct lu_attr *pattr, const struct lu_name *lname,
75              struct lu_fid* fid, int mask)
76 {
77         const char *name                = lname->ln_name;
78         const struct dt_key *key        = (const struct dt_key *)name;
79         struct mdd_object *mdd_obj      = md2mdd_obj(pobj);
80         struct mdd_device *m            = mdo2mdd(pobj);
81         struct dt_object *dir           = mdd_object_child(mdd_obj);
82         int rc;
83         ENTRY;
84
85         if (unlikely(mdd_is_dead_obj(mdd_obj)))
86                 RETURN(-ESTALE);
87
88         if (mdd_object_remote(mdd_obj)) {
89                 CDEBUG(D_INFO, "%s: Object "DFID" locates on remote server\n",
90                        mdd2obd_dev(m)->obd_name, PFID(mdo2fid(mdd_obj)));
91         } else if (!mdd_object_exists(mdd_obj)) {
92                 RETURN(-ESTALE);
93         }
94
95         rc = mdd_permission_internal_locked(env, mdd_obj, pattr, mask,
96                                             MOR_TGT_PARENT);
97         if (rc)
98                 RETURN(rc);
99
100         if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
101                    dt_try_as_dir(env, dir)))
102                 rc = dt_lookup(env, dir, (struct dt_rec *)fid, key);
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);
118         if (rc != 0)
119                 RETURN(rc);
120
121         rc = __mdd_lookup(env, pobj, pattr, lname, fid,
122                           (spec != NULL && spec->sp_permitted) ? 0 : MAY_EXEC);
123         RETURN(rc);
124 }
125
126 static inline int mdd_parent_fid(const struct lu_env *env,
127                                  struct mdd_object *obj,
128                                  const struct lu_attr *attr,
129                                  struct lu_fid *fid)
130 {
131         return __mdd_lookup(env, &obj->mod_obj, attr, &lname_dotdot, fid, 0);
132 }
133
134 /*
135  * For root fid use special function, which does not compare version component
136  * of fid. Version component is different for root fids on all MDTs.
137  */
138 int mdd_is_root(struct mdd_device *mdd, const struct lu_fid *fid)
139 {
140         return fid_seq(&mdd->mdd_root_fid) == fid_seq(fid) &&
141                 fid_oid(&mdd->mdd_root_fid) == fid_oid(fid);
142 }
143
144 /*
145  * return 1: if lf is the fid of the ancestor of p1;
146  * return 0: if not;
147  *
148  * return -EREMOTE: if remote object is found, in this
149  * case fid of remote object is saved to @pf;
150  *
151  * otherwise: values < 0, errors.
152  */
153 static int mdd_is_parent(const struct lu_env *env,
154                         struct mdd_device *mdd,
155                         struct mdd_object *p1,
156                         const struct lu_attr *attr,
157                         const struct lu_fid *lf,
158                         struct lu_fid *pf)
159 {
160         struct mdd_object *parent = NULL;
161         struct lu_fid *pfid;
162         int rc;
163         ENTRY;
164
165         LASSERT(!lu_fid_eq(mdo2fid(p1), lf));
166         pfid = &mdd_env_info(env)->mti_fid;
167
168         /* Check for root first. */
169         if (mdd_is_root(mdd, mdo2fid(p1)))
170                 RETURN(0);
171
172         for(;;) {
173                 /* this is done recursively */
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 an 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);
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);
268         if (!IS_ERR(it)) {
269                 result = iops->get(env, it, (const struct dt_key *)"");
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 /**
292  * Determine if the target object can be hard linked, and right now it only
293  * checks if the link count reach the maximum limit. Note: for ldiskfs, the
294  * directory nlink count might exceed the maximum link count(see
295  * osd_object_ref_add), so it only check nlink for non-directories.
296  *
297  * \param[in] env       thread environment
298  * \param[in] obj       object being linked to
299  * \param[in] la        attributes of \a obj
300  *
301  * \retval              0 if \a obj can be hard linked
302  * \retval              negative error if \a obj is a directory or has too
303  *                      many links
304  */
305 static int __mdd_may_link(const struct lu_env *env, struct mdd_object *obj,
306                           const struct lu_attr *la)
307 {
308         struct mdd_device *m = mdd_obj2mdd_dev(obj);
309         ENTRY;
310
311         LASSERT(la != NULL);
312
313         /* Subdir count limitation can be broken through
314          * (see osd_object_ref_add), so only check non-directory here. */
315         if (!S_ISDIR(la->la_mode) &&
316             la->la_nlink >= m->mdd_dt_conf.ddp_max_nlink)
317                 RETURN(-EMLINK);
318
319         RETURN(0);
320 }
321
322 /**
323  * Check whether it may create the cobj under the pobj.
324  *
325  * \param[in] env       execution environment
326  * \param[in] pobj      the parent directory
327  * \param[in] pattr     the attribute of the parent directory
328  * \param[in] cobj      the child to be created
329  * \param[in] check_perm        if check WRITE|EXEC permission for parent
330  *
331  * \retval              = 0 create the child under this dir is allowed
332  * \retval              negative errno create the child under this dir is
333  *                      not allowed
334  */
335 int mdd_may_create(const struct lu_env *env, struct mdd_object *pobj,
336                    const struct lu_attr *pattr, struct mdd_object *cobj,
337                    bool check_perm)
338 {
339         struct mdd_thread_info *info = mdd_env_info(env);
340         struct lu_buf   *xbuf;
341         int rc = 0;
342         ENTRY;
343
344         if (cobj && mdd_object_exists(cobj))
345                 RETURN(-EEXIST);
346
347         if (mdd_is_dead_obj(pobj))
348                 RETURN(-ENOENT);
349
350         /* If the parent is a sub-stripe, check whether it is dead */
351         xbuf = mdd_buf_get(env, info->mti_key, sizeof(info->mti_key));
352         rc = mdo_xattr_get(env, pobj, xbuf, XATTR_NAME_LMV);
353         if (unlikely(rc > 0)) {
354                 struct lmv_mds_md_v1  *lmv1 = xbuf->lb_buf;
355
356                 if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE &&
357                     le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_DEAD)
358                         RETURN(-ESTALE);
359         }
360         rc = 0;
361
362         if (check_perm)
363                 rc = mdd_permission_internal_locked(env, pobj, pattr,
364                                                     MAY_WRITE | MAY_EXEC,
365                                                     MOR_TGT_PARENT);
366         RETURN(rc);
367 }
368
369 /*
370  * Check whether can unlink from the pobj in the case of "cobj == NULL".
371  */
372 int mdd_may_unlink(const struct lu_env *env, struct mdd_object *pobj,
373                    const struct lu_attr *pattr, const struct lu_attr *attr)
374 {
375         int rc;
376         ENTRY;
377
378         if (mdd_is_dead_obj(pobj))
379                 RETURN(-ENOENT);
380
381         if (attr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
382                 RETURN(-EPERM);
383
384         rc = mdd_permission_internal_locked(env, pobj, pattr,
385                                             MAY_WRITE | MAY_EXEC,
386                                             MOR_TGT_PARENT);
387         if (rc != 0)
388                 RETURN(rc);
389
390         if (pattr->la_flags & LUSTRE_APPEND_FL)
391                 RETURN(-EPERM);
392
393         RETURN(rc);
394 }
395
396 /*
397  * pobj == NULL is remote ops case, under such case, pobj's
398  * VTX feature has been checked already, no need check again.
399  */
400 static inline int mdd_is_sticky(const struct lu_env *env,
401                                 struct mdd_object *pobj,
402                                 const struct lu_attr *pattr,
403                                 struct mdd_object *cobj,
404                                 const struct lu_attr *cattr)
405 {
406         struct lu_ucred *uc = lu_ucred_assert(env);
407
408         if (pobj != NULL) {
409                 LASSERT(pattr != NULL);
410                 if (!(pattr->la_mode & S_ISVTX) ||
411                     (pattr->la_uid == uc->uc_fsuid))
412                         return 0;
413         }
414
415         LASSERT(cattr != NULL);
416         if (cattr->la_uid == uc->uc_fsuid)
417                 return 0;
418
419         return !md_capable(uc, CFS_CAP_FOWNER);
420 }
421
422 static int mdd_may_delete_entry(const struct lu_env *env,
423                                 struct mdd_object *pobj,
424                                 const struct lu_attr *pattr,
425                                 int check_perm)
426 {
427         ENTRY;
428
429         LASSERT(pobj != NULL);
430         if (!mdd_object_exists(pobj))
431                 RETURN(-ENOENT);
432
433         if (mdd_is_dead_obj(pobj))
434                 RETURN(-ENOENT);
435
436         if (check_perm) {
437                 int rc;
438                 rc = mdd_permission_internal_locked(env, pobj, pattr,
439                                             MAY_WRITE | MAY_EXEC,
440                                             MOR_TGT_PARENT);
441                 if (rc)
442                         RETURN(rc);
443         }
444
445         if (pattr->la_flags & LUSTRE_APPEND_FL)
446                 RETURN(-EPERM);
447
448         RETURN(0);
449 }
450
451 /*
452  * Check whether it may delete the cobj from the pobj.
453  * pobj maybe NULL
454  */
455 int mdd_may_delete(const struct lu_env *env, struct mdd_object *tpobj,
456                    const struct lu_attr *tpattr, struct mdd_object *tobj,
457                    const struct lu_attr *tattr, const struct lu_attr *cattr,
458                    int check_perm, int check_empty)
459 {
460         int rc = 0;
461         ENTRY;
462
463         if (tpobj) {
464                 LASSERT(tpattr != NULL);
465                 rc = mdd_may_delete_entry(env, tpobj, tpattr, check_perm);
466                 if (rc != 0)
467                         RETURN(rc);
468         }
469
470         if (tobj == NULL)
471                 RETURN(0);
472
473         if (!mdd_object_exists(tobj))
474                 RETURN(-ENOENT);
475
476         if (mdd_is_dead_obj(tobj))
477                 RETURN(-ESTALE);
478
479         if (mdd_is_sticky(env, tpobj, tpattr, tobj, tattr))
480                 RETURN(-EPERM);
481
482         if (tattr->la_flags & (LUSTRE_APPEND_FL | LUSTRE_IMMUTABLE_FL))
483                 RETURN(-EPERM);
484
485         /* additional check the rename case */
486         if (cattr) {
487                 if (S_ISDIR(cattr->la_mode)) {
488                         struct mdd_device *mdd = mdo2mdd(&tobj->mod_obj);
489
490                         if (!S_ISDIR(tattr->la_mode))
491                                 RETURN(-ENOTDIR);
492
493                         if (lu_fid_eq(mdo2fid(tobj), &mdd->mdd_root_fid))
494                                 RETURN(-EBUSY);
495                 } else if (S_ISDIR(tattr->la_mode))
496                         RETURN(-EISDIR);
497         }
498
499         if (S_ISDIR(tattr->la_mode) && check_empty)
500                 rc = mdd_dir_is_empty(env, tobj);
501
502         RETURN(rc);
503 }
504
505 /**
506  * Check whether it can create the link file(linked to @src_obj) under
507  * the target directory(@tgt_obj), and src_obj has been locked by
508  * mdd_write_lock.
509  *
510  * \param[in] env       execution environment
511  * \param[in] tgt_obj   the target directory
512  * \param[in] tattr     attributes of target directory
513  * \param[in] lname     the link name
514  * \param[in] src_obj   source object for link
515  * \param[in] cattr     attributes for source object
516  *
517  * \retval              = 0 it is allowed to create the link file under tgt_obj
518  * \retval              negative error not allowed to create the link file
519  */
520 static int mdd_link_sanity_check(const struct lu_env *env,
521                                  struct mdd_object *tgt_obj,
522                                  const struct lu_attr *tattr,
523                                  const struct lu_name *lname,
524                                  struct mdd_object *src_obj,
525                                  const struct lu_attr *cattr)
526 {
527         struct mdd_device *m = mdd_obj2mdd_dev(src_obj);
528         int rc = 0;
529         ENTRY;
530
531         if (!mdd_object_exists(src_obj))
532                 RETURN(-ENOENT);
533
534         if (mdd_is_dead_obj(src_obj))
535                 RETURN(-ESTALE);
536
537         /* Local ops, no lookup before link, check filename length here. */
538         rc = mdd_name_check(m, lname);
539         if (rc < 0)
540                 RETURN(rc);
541
542         if (cattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
543                 RETURN(-EPERM);
544
545         if (S_ISDIR(mdd_object_type(src_obj)))
546                 RETURN(-EPERM);
547
548         LASSERT(src_obj != tgt_obj);
549         rc = mdd_may_create(env, tgt_obj, tattr, NULL, true);
550         if (rc != 0)
551                 RETURN(rc);
552
553         rc = __mdd_may_link(env, src_obj, cattr);
554
555         RETURN(rc);
556 }
557
558 static int __mdd_index_delete_only(const struct lu_env *env, struct mdd_object *pobj,
559                                    const char *name, struct thandle *handle)
560 {
561         struct dt_object *next = mdd_object_child(pobj);
562         int rc;
563         ENTRY;
564
565         if (dt_try_as_dir(env, next))
566                 rc = dt_delete(env, next, (struct dt_key *)name, handle);
567         else
568                 rc = -ENOTDIR;
569
570         RETURN(rc);
571 }
572
573 static int __mdd_index_insert_only(const struct lu_env *env,
574                                    struct mdd_object *pobj,
575                                    const struct lu_fid *lf, __u32 type,
576                                    const char *name,
577                                    struct thandle *handle)
578 {
579         struct dt_object *next = mdd_object_child(pobj);
580         int               rc;
581         ENTRY;
582
583         if (dt_try_as_dir(env, next)) {
584                 struct dt_insert_rec    *rec = &mdd_env_info(env)->mti_dt_rec;
585                 struct lu_ucred         *uc  = lu_ucred_check(env);
586                 int                      ignore_quota;
587
588                 rec->rec_fid = lf;
589                 rec->rec_type = type;
590                 ignore_quota = uc ? uc->uc_cap & CFS_CAP_SYS_RESOURCE_MASK : 1;
591                 rc = dt_insert(env, next, (const struct dt_rec *)rec,
592                                (const struct dt_key *)name, handle,
593                                ignore_quota);
594         } else {
595                 rc = -ENOTDIR;
596         }
597         RETURN(rc);
598 }
599
600 /* insert named index, add reference if isdir */
601 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
602                               const struct lu_fid *lf, __u32 type,
603                               const char *name, struct thandle *handle)
604 {
605         int rc;
606         ENTRY;
607
608         rc = __mdd_index_insert_only(env, pobj, lf, type, name, handle);
609         if (rc == 0 && S_ISDIR(type)) {
610                 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
611                 mdo_ref_add(env, pobj, handle);
612                 mdd_write_unlock(env, pobj);
613         }
614
615         RETURN(rc);
616 }
617
618 /* delete named index, drop reference if isdir */
619 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
620                               const char *name, int is_dir,
621                               struct thandle *handle)
622 {
623         int               rc;
624         ENTRY;
625
626         rc = __mdd_index_delete_only(env, pobj, name, handle);
627         if (rc == 0 && is_dir) {
628                 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
629                 mdo_ref_del(env, pobj, handle);
630                 mdd_write_unlock(env, pobj);
631         }
632
633         RETURN(rc);
634 }
635
636 static int mdd_llog_record_calc_size(const struct lu_env *env,
637                                      const struct lu_name *tname,
638                                      const struct lu_name *sname)
639 {
640         const struct lu_ucred   *uc = lu_ucred(env);
641         enum changelog_rec_flags crf = 0;
642         size_t                   hdr_size = sizeof(struct llog_changelog_rec) -
643                                             sizeof(struct changelog_rec);
644
645         if (sname != NULL)
646                 crf |= CLF_RENAME;
647
648         if (uc != NULL && uc->uc_jobid[0] != '\0')
649                 crf |= CLF_JOBID;
650
651         return llog_data_len(hdr_size + changelog_rec_offset(crf) +
652                              (tname != NULL ? tname->ln_namelen : 0) +
653                              (sname != NULL ? 1 + sname->ln_namelen : 0));
654 }
655
656 int mdd_declare_changelog_store(const struct lu_env *env,
657                                 struct mdd_device *mdd,
658                                 const struct lu_name *tname,
659                                 const struct lu_name *sname,
660                                 struct thandle *handle)
661 {
662         struct obd_device               *obd = mdd2obd_dev(mdd);
663         struct llog_ctxt                *ctxt;
664         struct llog_changelog_rec       *rec;
665         struct lu_buf                   *buf;
666         struct thandle                  *llog_th;
667         int                              reclen;
668         int                              rc;
669
670         /* Not recording */
671         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
672                 return 0;
673
674         reclen = mdd_llog_record_calc_size(env, tname, sname);
675         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
676         if (buf->lb_buf == NULL)
677                 return -ENOMEM;
678
679         rec = buf->lb_buf;
680         rec->cr_hdr.lrh_len = reclen;
681         rec->cr_hdr.lrh_type = CHANGELOG_REC;
682
683         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
684         if (ctxt == NULL)
685                 return -ENXIO;
686
687         llog_th = thandle_get_sub(env, handle, ctxt->loc_handle->lgh_obj);
688         if (IS_ERR(llog_th))
689                 GOTO(out_put, rc = PTR_ERR(llog_th));
690
691         rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, llog_th);
692
693 out_put:
694         llog_ctxt_put(ctxt);
695
696         return rc;
697 }
698
699 /** Add a changelog entry \a rec to the changelog llog
700  * \param mdd
701  * \param rec
702  * \param handle - currently ignored since llogs start their own transaction;
703  *              this will hopefully be fixed in llog rewrite
704  * \retval 0 ok
705  */
706 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
707                         struct llog_changelog_rec *rec, struct thandle *th)
708 {
709         struct obd_device       *obd = mdd2obd_dev(mdd);
710         struct llog_ctxt        *ctxt;
711         struct thandle          *llog_th;
712         int                      rc;
713
714         rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) +
715                                             changelog_rec_varsize(&rec->cr));
716
717         /* llog_lvfs_write_rec sets the llog tail len */
718         rec->cr_hdr.lrh_type = CHANGELOG_REC;
719         rec->cr.cr_time = cl_time();
720
721         spin_lock(&mdd->mdd_cl.mc_lock);
722         /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
723          * but as long as the MDD transactions are ordered correctly for e.g.
724          * rename conflicts, I don't think this should matter. */
725         rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
726         spin_unlock(&mdd->mdd_cl.mc_lock);
727
728         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
729         if (ctxt == NULL)
730                 return -ENXIO;
731
732         llog_th = thandle_get_sub(env, th, ctxt->loc_handle->lgh_obj);
733         if (IS_ERR(llog_th))
734                 GOTO(out_put, rc = PTR_ERR(llog_th));
735
736         /* nested journal transaction */
737         rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, llog_th);
738
739 out_put:
740         llog_ctxt_put(ctxt);
741         if (rc > 0)
742                 rc = 0;
743         return rc;
744 }
745
746 static void mdd_changelog_rec_ext_rename(struct changelog_rec *rec,
747                                          const struct lu_fid *sfid,
748                                          const struct lu_fid *spfid,
749                                          const struct lu_name *sname)
750 {
751         struct changelog_ext_rename     *rnm = changelog_rec_rename(rec);
752         size_t                           extsize = sname->ln_namelen + 1;
753
754         LASSERT(sfid != NULL);
755         LASSERT(spfid != NULL);
756         LASSERT(sname != NULL);
757
758         rnm->cr_sfid = *sfid;
759         rnm->cr_spfid = *spfid;
760
761         changelog_rec_name(rec)[rec->cr_namelen] = '\0';
762         strlcpy(changelog_rec_sname(rec), sname->ln_name, extsize);
763         rec->cr_namelen += extsize;
764 }
765
766 void mdd_changelog_rec_ext_jobid(struct changelog_rec *rec, const char *jobid)
767 {
768         struct changelog_ext_jobid      *jid = changelog_rec_jobid(rec);
769
770         if (jobid == NULL || jobid[0] == '\0')
771                 return;
772
773         strlcpy(jid->cr_jobid, jobid, sizeof(jid->cr_jobid));
774 }
775
776 /** Store a namespace change changelog record
777  * If this fails, we must fail the whole transaction; we don't
778  * want the change to commit without the log entry.
779  * \param target - mdd_object of change
780  * \param tpfid - target parent dir/object fid
781  * \param sfid - source object fid
782  * \param spfid - source parent fid
783  * \param tname - target name string
784  * \param sname - source name string
785  * \param handle - transacion handle
786  */
787 int mdd_changelog_ns_store(const struct lu_env *env,
788                            struct mdd_device *mdd,
789                            enum changelog_rec_type type,
790                            enum changelog_rec_flags crf,
791                            struct mdd_object *target,
792                            const struct lu_fid *tpfid,
793                            const struct lu_fid *sfid,
794                            const struct lu_fid *spfid,
795                            const struct lu_name *tname,
796                            const struct lu_name *sname,
797                            struct thandle *handle)
798 {
799         const struct lu_ucred           *uc = lu_ucred(env);
800         struct llog_changelog_rec       *rec;
801         struct lu_buf                   *buf;
802         int                              reclen;
803         int                              rc;
804         ENTRY;
805
806         /* Not recording */
807         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
808                 RETURN(0);
809
810         if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
811                 RETURN(0);
812
813         LASSERT(tpfid != NULL);
814         LASSERT(tname != NULL);
815         LASSERT(handle != NULL);
816
817         reclen = mdd_llog_record_calc_size(env, tname, sname);
818         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
819         if (buf->lb_buf == NULL)
820                 RETURN(-ENOMEM);
821         rec = buf->lb_buf;
822
823         crf &= CLF_FLAGMASK;
824
825         if (uc != NULL && uc->uc_jobid[0] != '\0')
826                 crf |= CLF_JOBID;
827
828         if (sname != NULL)
829                 crf |= CLF_RENAME;
830         else
831                 crf |= CLF_VERSION;
832
833         rec->cr.cr_flags = crf;
834         rec->cr.cr_type = (__u32)type;
835         rec->cr.cr_pfid = *tpfid;
836         rec->cr.cr_namelen = tname->ln_namelen;
837         memcpy(changelog_rec_name(&rec->cr), tname->ln_name, tname->ln_namelen);
838
839         if (crf & CLF_RENAME)
840                 mdd_changelog_rec_ext_rename(&rec->cr, sfid, spfid, sname);
841
842         if (crf & CLF_JOBID)
843                 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
844
845         if (likely(target != NULL)) {
846                 rec->cr.cr_tfid = *mdo2fid(target);
847                 target->mod_cltime = cfs_time_current_64();
848         } else {
849                 fid_zero(&rec->cr.cr_tfid);
850         }
851
852         rc = mdd_changelog_store(env, mdd, rec, handle);
853         if (rc < 0) {
854                 CERROR("%s: cannot store changelog record: type = %d, "
855                        "name = '%s', t = "DFID", p = "DFID": rc = %d\n",
856                        mdd2obd_dev(mdd)->obd_name, type, tname->ln_name,
857                        PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid), rc);
858                 return -EFAULT;
859         }
860
861         return 0;
862 }
863
864 static int __mdd_links_add(const struct lu_env *env,
865                            struct mdd_object *mdd_obj,
866                            struct linkea_data *ldata,
867                            const struct lu_name *lname,
868                            const struct lu_fid *pfid,
869                            int first, int check)
870 {
871         int rc;
872
873         if (ldata->ld_leh == NULL) {
874                 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
875                 if (rc) {
876                         if (rc != -ENODATA)
877                                 return rc;
878                         rc = linkea_data_new(ldata,
879                                              &mdd_env_info(env)->mti_link_buf);
880                         if (rc)
881                                 return rc;
882                 }
883         }
884
885         if (check) {
886                 rc = linkea_links_find(ldata, lname, pfid);
887                 if (rc && rc != -ENOENT)
888                         return rc;
889                 if (rc == 0)
890                         return -EEXIST;
891         }
892
893         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE)) {
894                 struct lu_fid *tfid = &mdd_env_info(env)->mti_fid2;
895
896                 *tfid = *pfid;
897                 tfid->f_ver = ~0;
898                 linkea_add_buf(ldata, lname, tfid);
899         }
900
901         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_MORE2))
902                 linkea_add_buf(ldata, lname, pfid);
903
904         return linkea_add_buf(ldata, lname, pfid);
905 }
906
907 static int __mdd_links_del(const struct lu_env *env,
908                            struct mdd_object *mdd_obj,
909                            struct linkea_data *ldata,
910                            const struct lu_name *lname,
911                            const struct lu_fid *pfid)
912 {
913         int rc;
914
915         if (ldata->ld_leh == NULL) {
916                 rc = mdd_links_read(env, mdd_obj, ldata);
917                 if (rc)
918                         return rc;
919         }
920
921         rc = linkea_links_find(ldata, lname, pfid);
922         if (rc)
923                 return rc;
924
925         linkea_del_buf(ldata, lname);
926         return 0;
927 }
928
929 static int mdd_linkea_prepare(const struct lu_env *env,
930                               struct mdd_object *mdd_obj,
931                               const struct lu_fid *oldpfid,
932                               const struct lu_name *oldlname,
933                               const struct lu_fid *newpfid,
934                               const struct lu_name *newlname,
935                               int first, int check,
936                               struct linkea_data *ldata)
937 {
938         int rc = 0;
939         int rc2 = 0;
940         ENTRY;
941
942         if (OBD_FAIL_CHECK(OBD_FAIL_FID_IGIF))
943                 return 0;
944
945         LASSERT(oldpfid != NULL || newpfid != NULL);
946
947         if (mdd_obj->mod_flags & DEAD_OBJ) {
948                 /* Prevent linkea to be updated which is NOT necessary. */
949                 ldata->ld_reclen = 0;
950                 /* No more links, don't bother */
951                 RETURN(0);
952         }
953
954         if (oldpfid != NULL) {
955                 rc = __mdd_links_del(env, mdd_obj, ldata, oldlname, oldpfid);
956                 if (rc) {
957                         if ((check == 1) ||
958                             (rc != -ENODATA && rc != -ENOENT))
959                                 RETURN(rc);
960                         /* No changes done. */
961                         rc = 0;
962                 }
963         }
964
965         /* If renaming, add the new record */
966         if (newpfid != NULL) {
967                 /* even if the add fails, we still delete the out-of-date
968                  * old link */
969                 rc2 = __mdd_links_add(env, mdd_obj, ldata, newlname, newpfid,
970                                       first, check);
971         }
972
973         rc = rc != 0 ? rc : rc2;
974
975         RETURN(rc);
976 }
977
978 int mdd_links_rename(const struct lu_env *env,
979                      struct mdd_object *mdd_obj,
980                      const struct lu_fid *oldpfid,
981                      const struct lu_name *oldlname,
982                      const struct lu_fid *newpfid,
983                      const struct lu_name *newlname,
984                      struct thandle *handle,
985                      struct linkea_data *ldata,
986                      int first, int check)
987 {
988         int rc = 0;
989         ENTRY;
990
991         if (ldata == NULL) {
992                 ldata = &mdd_env_info(env)->mti_link_data;
993                 memset(ldata, 0, sizeof(*ldata));
994                 rc = mdd_linkea_prepare(env, mdd_obj, oldpfid, oldlname,
995                                         newpfid, newlname, first, check,
996                                         ldata);
997                 if (rc != 0)
998                         GOTO(out, rc);
999         }
1000
1001         if (ldata->ld_reclen != 0)
1002                 rc = mdd_links_write(env, mdd_obj, ldata, handle);
1003         EXIT;
1004 out:
1005         if (rc != 0) {
1006                 int error = 1;
1007                 if (rc == -EOVERFLOW || rc == -ENOSPC)
1008                         error = 0;
1009                 if (oldpfid == NULL)
1010                         CDEBUG(error ? D_ERROR : D_OTHER,
1011                                "link_ea add '%.*s' failed %d "DFID"\n",
1012                                newlname->ln_namelen, newlname->ln_name,
1013                                rc, PFID(mdd_object_fid(mdd_obj)));
1014                 else if (newpfid == NULL)
1015                         CDEBUG(error ? D_ERROR : D_OTHER,
1016                                "link_ea del '%.*s' failed %d "DFID"\n",
1017                                oldlname->ln_namelen, oldlname->ln_name,
1018                                rc, PFID(mdd_object_fid(mdd_obj)));
1019                 else
1020                         CDEBUG(error ? D_ERROR : D_OTHER,
1021                                "link_ea rename '%.*s'->'%.*s' failed %d "
1022                                DFID"\n",
1023                                oldlname->ln_namelen, oldlname->ln_name,
1024                                newlname->ln_namelen, newlname->ln_name,
1025                                rc, PFID(mdd_object_fid(mdd_obj)));
1026         }
1027
1028         if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1029                 /* if we vmalloced a large buffer drop it */
1030                 lu_buf_free(ldata->ld_buf);
1031
1032         return rc;
1033 }
1034
1035 static inline int mdd_links_add(const struct lu_env *env,
1036                                 struct mdd_object *mdd_obj,
1037                                 const struct lu_fid *pfid,
1038                                 const struct lu_name *lname,
1039                                 struct thandle *handle,
1040                                 struct linkea_data *ldata, int first)
1041 {
1042         return mdd_links_rename(env, mdd_obj, NULL, NULL,
1043                                 pfid, lname, handle, ldata, first, 0);
1044 }
1045
1046 static inline int mdd_links_del(const struct lu_env *env,
1047                                 struct mdd_object *mdd_obj,
1048                                 const struct lu_fid *pfid,
1049                                 const struct lu_name *lname,
1050                                 struct thandle *handle)
1051 {
1052         return mdd_links_rename(env, mdd_obj, pfid, lname,
1053                                 NULL, NULL, handle, NULL, 0, 0);
1054 }
1055
1056 /** Read the link EA into a temp buffer.
1057  * Uses the mdd_thread_info::mti_big_buf since it is generally large.
1058  * A pointer to the buffer is stored in \a ldata::ld_buf.
1059  *
1060  * \retval 0 or error
1061  */
1062 int mdd_links_read(const struct lu_env *env, struct mdd_object *mdd_obj,
1063                    struct linkea_data *ldata)
1064 {
1065         int rc;
1066
1067         if (!mdd_object_exists(mdd_obj))
1068                 return -ENODATA;
1069
1070         /* First try a small buf */
1071         LASSERT(env != NULL);
1072         ldata->ld_buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_link_buf,
1073                                                PAGE_CACHE_SIZE);
1074         if (ldata->ld_buf->lb_buf == NULL)
1075                 return -ENOMEM;
1076
1077         rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf, XATTR_NAME_LINK);
1078         if (rc == -ERANGE) {
1079                 /* Buf was too small, figure out what we need. */
1080                 lu_buf_free(ldata->ld_buf);
1081                 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1082                                    XATTR_NAME_LINK);
1083                 if (rc < 0)
1084                         return rc;
1085                 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
1086                 if (ldata->ld_buf->lb_buf == NULL)
1087                         return -ENOMEM;
1088                 rc = mdo_xattr_get(env, mdd_obj, ldata->ld_buf,
1089                                   XATTR_NAME_LINK);
1090         }
1091         if (rc < 0) {
1092                 lu_buf_free(ldata->ld_buf);
1093                 ldata->ld_buf = NULL;
1094                 return rc;
1095         }
1096
1097         return linkea_init(ldata);
1098 }
1099
1100 /** Read the link EA into a temp buffer.
1101  * Uses the name_buf since it is generally large.
1102  * \retval IS_ERR err
1103  * \retval ptr to \a lu_buf (always \a mti_big_buf)
1104  */
1105 struct lu_buf *mdd_links_get(const struct lu_env *env,
1106                              struct mdd_object *mdd_obj)
1107 {
1108         struct linkea_data ldata = { NULL };
1109         int rc;
1110
1111         rc = mdd_links_read(env, mdd_obj, &ldata);
1112         return rc ? ERR_PTR(rc) : ldata.ld_buf;
1113 }
1114
1115 int mdd_links_write(const struct lu_env *env, struct mdd_object *mdd_obj,
1116                     struct linkea_data *ldata, struct thandle *handle)
1117 {
1118         const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ld_buf->lb_buf,
1119                                                      ldata->ld_leh->leh_len);
1120         int                 rc;
1121
1122         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_LINKEA))
1123                 return 0;
1124
1125         rc = mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle);
1126         if (unlikely(rc == -ENOSPC) && S_ISREG(mdd_object_type(mdd_obj)) &&
1127             mdd_object_remote(mdd_obj) == 0) {
1128                 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1129                 struct thandle  *sub_th;
1130
1131                 /* XXX: If the linkEA is overflow, then we need to notify the
1132                  *      namespace LFSCK to skip "nlink" attribute verification
1133                  *      on this object to avoid the "nlink" to be shrinked by
1134                  *      wrong. It may be not good an interaction with LFSCK
1135                  *      like this. We will consider to replace it with other
1136                  *      mechanism in future. LU-5802. */
1137                 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK,
1138                                LFSCK_TYPE_NAMESPACE);
1139
1140                 sub_th = thandle_get_sub_by_dt(env, handle,
1141                                 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom);
1142                 lfsck_in_notify(env, mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1143                                 lr, sub_th);
1144         }
1145
1146         return rc;
1147 }
1148
1149 int mdd_declare_links_add(const struct lu_env *env, struct mdd_object *mdd_obj,
1150                           struct thandle *handle, struct linkea_data *ldata,
1151                           enum mdd_links_add_overflow overflow)
1152 {
1153         int     rc;
1154         int     ea_len;
1155         void    *linkea;
1156
1157         if (ldata != NULL && ldata->ld_leh != NULL) {
1158                 ea_len = ldata->ld_leh->leh_len;
1159                 linkea = ldata->ld_buf->lb_buf;
1160         } else {
1161                 ea_len = DEFAULT_LINKEA_SIZE;
1162                 linkea = NULL;
1163         }
1164
1165         /* XXX: max size? */
1166         rc = mdo_declare_xattr_set(env, mdd_obj,
1167                                    mdd_buf_get_const(env, linkea, ea_len),
1168                                    XATTR_NAME_LINK, 0, handle);
1169         if (rc != 0)
1170                 return rc;
1171
1172         if (mdd_object_remote(mdd_obj) == 0 && overflow == MLAO_CHECK) {
1173                 struct lfsck_request *lr = &mdd_env_info(env)->mti_lr;
1174                 struct thandle  *sub_th;
1175
1176                 /* XXX: If the linkEA is overflow, then we need to notify the
1177                  *      namespace LFSCK to skip "nlink" attribute verification
1178                  *      on this object to avoid the "nlink" to be shrinked by
1179                  *      wrong. It may be not good an interaction with LFSCK
1180                  *      like this. We will consider to replace it with other
1181                  *      mechanism in future. LU-5802. */
1182                 lfsck_pack_rfa(lr, mdo2fid(mdd_obj), LE_SKIP_NLINK_DECLARE,
1183                                LFSCK_TYPE_NAMESPACE);
1184
1185                 sub_th = thandle_get_sub_by_dt(env, handle,
1186                                 mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom);
1187                 rc = lfsck_in_notify(env,
1188                                      mdo2mdd(&mdd_obj->mod_obj)->mdd_bottom,
1189                                      lr, sub_th);
1190         }
1191
1192         return rc;
1193 }
1194
1195 static inline int mdd_declare_links_del(const struct lu_env *env,
1196                                         struct mdd_object *c,
1197                                         struct thandle *handle)
1198 {
1199         int rc = 0;
1200
1201         /* For directory, the linkEA will be removed together
1202          * with the object. */
1203         if (!S_ISDIR(mdd_object_type(c)))
1204                 rc = mdd_declare_links_add(env, c, handle, NULL, MLAO_IGNORE);
1205
1206         return rc;
1207 }
1208
1209 static int mdd_declare_link(const struct lu_env *env,
1210                             struct mdd_device *mdd,
1211                             struct mdd_object *p,
1212                             struct mdd_object *c,
1213                             const struct lu_name *name,
1214                             struct thandle *handle,
1215                             struct lu_attr *la,
1216                             struct linkea_data *data)
1217 {
1218         int rc;
1219
1220         rc = mdo_declare_index_insert(env, p, mdo2fid(c), mdd_object_type(c),
1221                                       name->ln_name, handle);
1222         if (rc != 0)
1223                 return rc;
1224
1225         rc = mdo_declare_ref_add(env, c, handle);
1226         if (rc != 0)
1227                 return rc;
1228
1229         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1230                 rc = mdo_declare_ref_add(env, c, handle);
1231                 if (rc != 0)
1232                         return rc;
1233         }
1234
1235         la->la_valid = LA_CTIME | LA_MTIME;
1236         rc = mdo_declare_attr_set(env, p, la, handle);
1237         if (rc != 0)
1238                 return rc;
1239
1240         la->la_valid = LA_CTIME;
1241         rc = mdo_declare_attr_set(env, c, la, handle);
1242         if (rc != 0)
1243                 return rc;
1244
1245         rc = mdd_declare_links_add(env, c, handle, data,
1246                         S_ISREG(mdd_object_type(c)) ? MLAO_CHECK : MLAO_IGNORE);
1247         if (rc != 0)
1248                 return rc;
1249
1250         rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1251
1252         return rc;
1253 }
1254
1255 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
1256                     struct md_object *src_obj, const struct lu_name *lname,
1257                     struct md_attr *ma)
1258 {
1259         const char *name = lname->ln_name;
1260         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
1261         struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
1262         struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
1263         struct lu_attr    *cattr = MDD_ENV_VAR(env, cattr);
1264         struct lu_attr    *tattr = MDD_ENV_VAR(env, tattr);
1265         struct mdd_device *mdd = mdo2mdd(src_obj);
1266         struct thandle *handle;
1267         struct linkea_data *ldata = &mdd_env_info(env)->mti_link_data;
1268         int rc;
1269         ENTRY;
1270
1271         rc = mdd_la_get(env, mdd_sobj, cattr);
1272         if (rc != 0)
1273                 RETURN(rc);
1274
1275         rc = mdd_la_get(env, mdd_tobj, tattr);
1276         if (rc != 0)
1277                 RETURN(rc);
1278
1279         handle = mdd_trans_create(env, mdd);
1280         if (IS_ERR(handle))
1281                 GOTO(out_pending, rc = PTR_ERR(handle));
1282
1283         memset(ldata, 0, sizeof(*ldata));
1284
1285         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1286         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1287
1288         rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle,
1289                               la, ldata);
1290         if (rc)
1291                 GOTO(stop, rc);
1292
1293         rc = mdd_trans_start(env, mdd, handle);
1294         if (rc)
1295                 GOTO(stop, rc);
1296
1297         mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
1298         rc = mdd_link_sanity_check(env, mdd_tobj, tattr, lname, mdd_sobj,
1299                                    cattr);
1300         if (rc)
1301                 GOTO(out_unlock, rc);
1302
1303         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LESS_NLINK)) {
1304                 rc = mdo_ref_add(env, mdd_sobj, handle);
1305                 if (rc != 0)
1306                         GOTO(out_unlock, rc);
1307         }
1308
1309         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MORE_NLINK)) {
1310                 rc = mdo_ref_add(env, mdd_sobj, handle);
1311                 if (rc != 0)
1312                         GOTO(out_unlock, rc);
1313         }
1314
1315         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING3)) {
1316                 struct lu_fid tfid = *mdo2fid(mdd_sobj);
1317
1318                 tfid.f_oid++;
1319                 rc = __mdd_index_insert_only(env, mdd_tobj, &tfid,
1320                                              mdd_object_type(mdd_sobj),
1321                                              name, handle);
1322         } else {
1323                 rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
1324                                              mdd_object_type(mdd_sobj),
1325                                              name, handle);
1326         }
1327
1328         if (rc != 0) {
1329                 mdo_ref_del(env, mdd_sobj, handle);
1330                 GOTO(out_unlock, rc);
1331         }
1332
1333         la->la_valid = LA_CTIME | LA_MTIME;
1334         rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
1335         if (rc)
1336                 GOTO(out_unlock, rc);
1337
1338         la->la_valid = LA_CTIME;
1339         rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
1340         if (rc == 0) {
1341                 rc = mdd_linkea_prepare(env, mdd_sobj, NULL, NULL,
1342                                         mdo2fid(mdd_tobj), lname, 0, 0,
1343                                         ldata);
1344                 if (rc == 0)
1345                         mdd_links_add(env, mdd_sobj, mdo2fid(mdd_tobj),
1346                                       lname, handle, ldata, 0);
1347                 /* The failure of links_add should not cause the link
1348                  * failure, reset rc here */
1349                 rc = 0;
1350         }
1351         EXIT;
1352 out_unlock:
1353         mdd_write_unlock(env, mdd_sobj);
1354         if (rc == 0)
1355                 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
1356                                             mdo2fid(mdd_tobj), NULL, NULL,
1357                                             lname, NULL, handle);
1358 stop:
1359         mdd_trans_stop(env, mdd, rc, handle);
1360
1361         if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
1362                 /* if we vmalloced a large buffer drop it */
1363                 lu_buf_free(ldata->ld_buf);
1364 out_pending:
1365         return rc;
1366 }
1367
1368 static int mdd_mark_dead_object(const struct lu_env *env,
1369                                 struct mdd_object *obj, struct thandle *handle,
1370                                 bool declare)
1371 {
1372         struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
1373         int rc;
1374
1375         if (!declare)
1376                 obj->mod_flags |= DEAD_OBJ;
1377
1378         if (!S_ISDIR(mdd_object_type(obj)))
1379                 return 0;
1380
1381         attr->la_valid = LA_FLAGS;
1382         attr->la_flags = LUSTRE_SLAVE_DEAD_FL;
1383
1384         if (declare)
1385                 rc = mdo_declare_attr_set(env, obj, attr, handle);
1386         else
1387                 rc = mdo_attr_set(env, obj, attr, handle);
1388
1389         return rc;
1390 }
1391
1392 static int mdd_declare_finish_unlink(const struct lu_env *env,
1393                                      struct mdd_object *obj,
1394                                      struct thandle *handle)
1395 {
1396         int     rc;
1397
1398         rc = mdd_mark_dead_object(env, obj, handle, true);
1399         if (rc != 0)
1400                 return rc;
1401
1402         rc = orph_declare_index_insert(env, obj, mdd_object_type(obj), handle);
1403         if (rc != 0)
1404                 return rc;
1405
1406         rc = mdo_declare_destroy(env, obj, handle);
1407         if (rc != 0)
1408                 return rc;
1409
1410         return mdd_declare_links_del(env, obj, handle);
1411 }
1412
1413 /* caller should take a lock before calling */
1414 int mdd_finish_unlink(const struct lu_env *env,
1415                       struct mdd_object *obj, struct md_attr *ma,
1416                       const struct mdd_object *pobj,
1417                       const struct lu_name *lname,
1418                       struct thandle *th)
1419 {
1420         int rc = 0;
1421         int is_dir = S_ISDIR(ma->ma_attr.la_mode);
1422         ENTRY;
1423
1424         LASSERT(mdd_write_locked(env, obj) != 0);
1425
1426         if (ma->ma_attr.la_nlink == 0 || is_dir) {
1427                 rc = mdd_mark_dead_object(env, obj, th, false);
1428                 if (rc != 0)
1429                         RETURN(rc);
1430
1431                 /* add new orphan and the object
1432                  * will be deleted during mdd_close() */
1433                 if (obj->mod_count) {
1434                         rc = __mdd_orphan_add(env, obj, th);
1435                         if (rc == 0)
1436                                 CDEBUG(D_HA, "Object "DFID" is inserted into "
1437                                         "orphan list, open count = %d\n",
1438                                         PFID(mdd_object_fid(obj)),
1439                                         obj->mod_count);
1440                         else
1441                                 CERROR("Object "DFID" fail to be an orphan, "
1442                                        "open count = %d, maybe cause failed "
1443                                        "open replay\n",
1444                                         PFID(mdd_object_fid(obj)),
1445                                         obj->mod_count);
1446                 } else {
1447                         rc = mdo_destroy(env, obj, th);
1448                 }
1449         } else if (!is_dir) {
1450                 /* old files may not have link ea; ignore errors */
1451                 mdd_links_del(env, obj, mdo2fid(pobj), lname, th);
1452         }
1453
1454         RETURN(rc);
1455 }
1456
1457 /*
1458  * pobj maybe NULL
1459  * has mdd_write_lock on cobj already, but not on pobj yet
1460  */
1461 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
1462                             const struct lu_attr *pattr,
1463                             struct mdd_object *cobj,
1464                             const struct lu_attr *cattr)
1465 {
1466         int rc;
1467         ENTRY;
1468
1469         rc = mdd_may_delete(env, pobj, pattr, cobj, cattr, NULL, 1, 1);
1470
1471         RETURN(rc);
1472 }
1473
1474 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1475                               struct mdd_object *p, struct mdd_object *c,
1476                               const struct lu_name *name, struct md_attr *ma,
1477                               struct thandle *handle, int no_name, int is_dir)
1478 {
1479         struct lu_attr  *la = &mdd_env_info(env)->mti_la_for_fix;
1480         int              rc;
1481
1482         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1483                 if (likely(no_name == 0)) {
1484                         rc = mdo_declare_index_delete(env, p, name->ln_name,
1485                                                       handle);
1486                         if (rc != 0)
1487                                 return rc;
1488                 }
1489
1490                 if (is_dir != 0) {
1491                         rc = mdo_declare_ref_del(env, p, handle);
1492                         if (rc != 0)
1493                                 return rc;
1494                 }
1495         }
1496
1497         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1498         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1499         la->la_valid = LA_CTIME | LA_MTIME;
1500         rc = mdo_declare_attr_set(env, p, la, handle);
1501         if (rc)
1502                 return rc;
1503
1504         if (c != NULL) {
1505                 rc = mdo_declare_ref_del(env, c, handle);
1506                 if (rc)
1507                         return rc;
1508
1509                 rc = mdo_declare_ref_del(env, c, handle);
1510                 if (rc)
1511                         return rc;
1512
1513                 la->la_valid = LA_CTIME;
1514                 rc = mdo_declare_attr_set(env, c, la, handle);
1515                 if (rc)
1516                         return rc;
1517
1518                 rc = mdd_declare_finish_unlink(env, c, handle);
1519                 if (rc)
1520                         return rc;
1521
1522                 /* FIXME: need changelog for remove entry */
1523                 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
1524         }
1525
1526         return rc;
1527 }
1528
1529 /*
1530  * test if a file has an HSM archive
1531  * if HSM attributes are not found in ma update them from
1532  * HSM xattr
1533  */
1534 static bool mdd_hsm_archive_exists(const struct lu_env *env,
1535                                    struct mdd_object *obj,
1536                                    struct md_attr *ma)
1537 {
1538         ENTRY;
1539
1540         if (!(ma->ma_valid & MA_HSM)) {
1541                 /* no HSM MD provided, read xattr */
1542                 struct lu_buf   *hsm_buf;
1543                 const size_t     buflen = sizeof(struct hsm_attrs);
1544                 int              rc;
1545
1546                 hsm_buf = mdd_buf_get(env, NULL, 0);
1547                 lu_buf_alloc(hsm_buf, buflen);
1548                 rc = mdo_xattr_get(env, obj, hsm_buf, XATTR_NAME_HSM);
1549                 rc = lustre_buf2hsm(hsm_buf->lb_buf, rc, &ma->ma_hsm);
1550                 lu_buf_free(hsm_buf);
1551                 if (rc < 0)
1552                         RETURN(false);
1553
1554                 ma->ma_valid = MA_HSM;
1555         }
1556         if (ma->ma_hsm.mh_flags & HS_EXISTS)
1557                 RETURN(true);
1558         RETURN(false);
1559 }
1560
1561 /**
1562  * Delete name entry and the object.
1563  * Note: no_name == 1 means it only destory the object, i.e. name_entry
1564  * does not exist for this object, and it could only happen during resending
1565  * of remote unlink. see the comments in mdt_reint_unlink. Unfortunately, lname
1566  * is also needed in this case(needed by changelog), so we have to add another
1567  * parameter(no_name)here. XXX: this is only needed in DNE phase I, on Phase II,
1568  * the ENOENT failure should be able to be fixed by redo mechanism.
1569  */
1570 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1571                       struct md_object *cobj, const struct lu_name *lname,
1572                       struct md_attr *ma, int no_name)
1573 {
1574         const char *name = lname->ln_name;
1575         struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
1576         struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
1577         struct lu_attr *la = &mdd_env_info(env)->mti_la_for_fix;
1578         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1579         struct mdd_object *mdd_cobj = NULL;
1580         struct mdd_device *mdd = mdo2mdd(pobj);
1581         struct thandle    *handle;
1582         int rc, is_dir = 0;
1583         ENTRY;
1584
1585         /* cobj == NULL means only delete name entry */
1586         if (likely(cobj != NULL)) {
1587                 mdd_cobj = md2mdd_obj(cobj);
1588                 if (mdd_object_exists(mdd_cobj) == 0)
1589                         RETURN(-ENOENT);
1590         }
1591
1592         rc = mdd_la_get(env, mdd_pobj, pattr);
1593         if (rc)
1594                 RETURN(rc);
1595
1596         if (likely(mdd_cobj != NULL)) {
1597                 /* fetch cattr */
1598                 rc = mdd_la_get(env, mdd_cobj, cattr);
1599                 if (rc)
1600                         RETURN(rc);
1601
1602                 is_dir = S_ISDIR(cattr->la_mode);
1603         }
1604
1605         rc = mdd_unlink_sanity_check(env, mdd_pobj, pattr, mdd_cobj, cattr);
1606         if (rc)
1607                 RETURN(rc);
1608
1609         handle = mdd_trans_create(env, mdd);
1610         if (IS_ERR(handle))
1611                 RETURN(PTR_ERR(handle));
1612
1613         rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1614                                 lname, ma, handle, no_name, is_dir);
1615         if (rc)
1616                 GOTO(stop, rc);
1617
1618         rc = mdd_trans_start(env, mdd, handle);
1619         if (rc)
1620                 GOTO(stop, rc);
1621
1622         if (likely(mdd_cobj != NULL))
1623                 mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1624
1625         if (likely(no_name == 0) && !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING2)) {
1626                 rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
1627                 if (rc)
1628                         GOTO(cleanup, rc);
1629         }
1630
1631         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_MUL_REF) ||
1632             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_NAMEENTRY))
1633                 GOTO(cleanup, rc = 0);
1634
1635         if (likely(mdd_cobj != NULL)) {
1636                 rc = mdo_ref_del(env, mdd_cobj, handle);
1637                 if (rc != 0) {
1638                         __mdd_index_insert_only(env, mdd_pobj,
1639                                                 mdo2fid(mdd_cobj),
1640                                                 mdd_object_type(mdd_cobj),
1641                                                 name, handle);
1642                         GOTO(cleanup, rc);
1643                 }
1644
1645                 if (is_dir)
1646                         /* unlink dot */
1647                         mdo_ref_del(env, mdd_cobj, handle);
1648
1649                 /* fetch updated nlink */
1650                 rc = mdd_la_get(env, mdd_cobj, cattr);
1651                 if (rc)
1652                         GOTO(cleanup, rc);
1653         }
1654
1655         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1656         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1657
1658         la->la_valid = LA_CTIME | LA_MTIME;
1659         rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
1660         if (rc)
1661                 GOTO(cleanup, rc);
1662
1663         /* Enough for only unlink the entry */
1664         if (unlikely(mdd_cobj == NULL))
1665                 GOTO(stop, rc);
1666
1667         if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1668                 /* update ctime of an unlinked file only if it is still
1669                  * opened or a link still exists */
1670                 la->la_valid = LA_CTIME;
1671                 rc = mdd_update_time(env, mdd_cobj, cattr, la, handle);
1672                 if (rc)
1673                         GOTO(cleanup, rc);
1674         }
1675
1676         /* XXX: this transfer to ma will be removed with LOD/OSP */
1677         ma->ma_attr = *cattr;
1678         ma->ma_valid |= MA_INODE;
1679         rc = mdd_finish_unlink(env, mdd_cobj, ma, mdd_pobj, lname, handle);
1680
1681         /* fetch updated nlink */
1682         if (rc == 0)
1683                 rc = mdd_la_get(env, mdd_cobj, cattr);
1684
1685         /* if object is removed then we can't get its attrs, use last get */
1686         if (cattr->la_nlink == 0) {
1687                 ma->ma_attr = *cattr;
1688                 ma->ma_valid |= MA_INODE;
1689         }
1690         EXIT;
1691 cleanup:
1692         if (likely(mdd_cobj != NULL))
1693                 mdd_write_unlock(env, mdd_cobj);
1694
1695         if (rc == 0) {
1696                 int cl_flags = 0;
1697
1698                 if (cattr->la_nlink == 0) {
1699                         cl_flags |= CLF_UNLINK_LAST;
1700                         /* search for an existing archive */
1701                         if (mdd_hsm_archive_exists(env, mdd_cobj, ma))
1702                                 cl_flags |= CLF_UNLINK_HSM_EXISTS;
1703                 }
1704
1705                 rc = mdd_changelog_ns_store(env, mdd,
1706                         is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1707                         mdd_cobj, mdo2fid(mdd_pobj), NULL, NULL, lname, NULL,
1708                         handle);
1709         }
1710
1711 stop:
1712         mdd_trans_stop(env, mdd, rc, handle);
1713
1714         return rc;
1715 }
1716
1717 /*
1718  * The permission has been checked when obj created, no need check again.
1719  */
1720 static int mdd_cd_sanity_check(const struct lu_env *env,
1721                                struct mdd_object *obj)
1722 {
1723         ENTRY;
1724
1725         /* EEXIST check */
1726         if (!obj || mdd_is_dead_obj(obj))
1727                 RETURN(-ENOENT);
1728
1729         RETURN(0);
1730 }
1731
1732 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1733                            struct md_object *cobj, const struct md_op_spec *spec,
1734                            struct md_attr *ma)
1735 {
1736         struct mdd_device *mdd = mdo2mdd(cobj);
1737         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1738         struct mdd_object *son = md2mdd_obj(cobj);
1739         struct thandle    *handle;
1740         const struct lu_buf *buf;
1741         struct lu_attr    *attr = MDD_ENV_VAR(env, cattr);
1742         struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1743         int                rc;
1744         ENTRY;
1745
1746         rc = mdd_cd_sanity_check(env, son);
1747         if (rc)
1748                 RETURN(rc);
1749
1750         if (!md_should_create(spec->sp_cr_flags))
1751                 RETURN(0);
1752
1753         /*
1754          * there are following use cases for this function:
1755          * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1756          *    striping can be specified or not
1757          * 2) CMD?
1758          */
1759         rc = mdd_la_get(env, son, attr);
1760         if (rc)
1761                 RETURN(rc);
1762
1763         /* calling ->ah_make_hint() is used to transfer information from parent */
1764         mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
1765
1766         handle = mdd_trans_create(env, mdd);
1767         if (IS_ERR(handle))
1768                 GOTO(out_free, rc = PTR_ERR(handle));
1769
1770         /*
1771          * XXX: Setting the lov ea is not locked but setting the attr is locked?
1772          * Should this be fixed?
1773          */
1774         CDEBUG(D_OTHER, "ea %p/%u, cr_flags "LPO64", no_create %u\n",
1775                spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1776                spec->sp_cr_flags, spec->no_create);
1777
1778         if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1779                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1780                                         spec->u.sp_ea.eadatalen);
1781         } else {
1782                 buf = &LU_BUF_NULL;
1783         }
1784
1785         rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1786                                   XATTR_NAME_LOV, 0, handle);
1787         if (rc)
1788                 GOTO(stop, rc);
1789
1790         rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1791         if (rc)
1792                 GOTO(stop, rc);
1793
1794         rc = mdd_trans_start(env, mdd, handle);
1795         if (rc)
1796                 GOTO(stop, rc);
1797
1798         rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1799                           0, handle);
1800
1801         if (rc)
1802                 GOTO(stop, rc);
1803
1804         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, son, handle);
1805
1806 stop:
1807         mdd_trans_stop(env, mdd, rc, handle);
1808 out_free:
1809         RETURN(rc);
1810 }
1811
1812 static int mdd_declare_object_initialize(const struct lu_env *env,
1813                                          struct mdd_object *parent,
1814                                          struct mdd_object *child,
1815                                          struct lu_attr *attr,
1816                                          struct thandle *handle)
1817 {
1818         int rc;
1819         ENTRY;
1820
1821         /*
1822          * inode mode has been set in creation time, and it's based on umask,
1823          * la_mode and acl, don't set here again! (which will go wrong
1824          * because below function doesn't consider umask).
1825          * I'd suggest set all object attributes in creation time, see above.
1826          */
1827         LASSERT(attr->la_valid & (LA_MODE | LA_TYPE));
1828         attr->la_valid &= ~(LA_MODE | LA_TYPE);
1829         rc = mdo_declare_attr_set(env, child, attr, handle);
1830         attr->la_valid |= LA_MODE | LA_TYPE;
1831         if (rc != 0 || !S_ISDIR(attr->la_mode))
1832                 RETURN(rc);
1833
1834         rc = mdo_declare_index_insert(env, child, mdo2fid(child), S_IFDIR,
1835                                       dot, handle);
1836         if (rc != 0)
1837                 RETURN(rc);
1838
1839         rc = mdo_declare_ref_add(env, child, handle);
1840         if (rc != 0)
1841                 RETURN(rc);
1842
1843         rc = mdo_declare_index_insert(env, child, mdo2fid(parent), S_IFDIR,
1844                                       dotdot, handle);
1845
1846         RETURN(rc);
1847 }
1848
1849 static int mdd_object_initialize(const struct lu_env *env,
1850                                  const struct lu_fid *pfid,
1851                                  struct mdd_object *child,
1852                                  struct lu_attr *attr, struct thandle *handle,
1853                                  const struct md_op_spec *spec)
1854 {
1855         int rc = 0;
1856         ENTRY;
1857
1858         if (S_ISDIR(attr->la_mode)) {
1859                 /* Add "." and ".." for newly created dir */
1860                 mdo_ref_add(env, child, handle);
1861                 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1862                                              S_IFDIR, dot, handle);
1863                 if (rc == 0)
1864                         rc = __mdd_index_insert_only(env, child, pfid, S_IFDIR,
1865                                                      dotdot, handle);
1866                 if (rc != 0)
1867                         mdo_ref_del(env, child, handle);
1868         }
1869
1870         RETURN(rc);
1871 }
1872
1873 /**
1874  * This function checks whether it can create a file/dir under the
1875  * directory(@pobj). The directory(@pobj) is not being locked by
1876  * mdd lock.
1877  *
1878  * \param[in] env       execution environment
1879  * \param[in] pobj      the directory to create files
1880  * \param[in] pattr     the attributes of the directory
1881  * \param[in] lname     the name of the created file/dir
1882  * \param[in] cattr     the attributes of the file/dir
1883  * \param[in] spec      create specification
1884  *
1885  * \retval              = 0 it is allowed to create file/dir under
1886  *                      the directory
1887  * \retval              negative error not allowed to create file/dir
1888  *                      under the directory
1889  */
1890 static int mdd_create_sanity_check(const struct lu_env *env,
1891                                    struct md_object *pobj,
1892                                    const struct lu_attr *pattr,
1893                                    const struct lu_name *lname,
1894                                    struct lu_attr *cattr,
1895                                    struct md_op_spec *spec)
1896 {
1897         struct mdd_thread_info *info = mdd_env_info(env);
1898         struct lu_fid     *fid       = &info->mti_fid;
1899         struct mdd_object *obj       = md2mdd_obj(pobj);
1900         struct mdd_device *m         = mdo2mdd(pobj);
1901         bool            check_perm = true;
1902         int rc;
1903         ENTRY;
1904
1905         /* EEXIST check */
1906         if (mdd_is_dead_obj(obj))
1907                 RETURN(-ENOENT);
1908
1909         /*
1910          * In some cases this lookup is not needed - we know before if name
1911          * exists or not because MDT performs lookup for it.
1912          * name length check is done in lookup.
1913          */
1914         if (spec->sp_cr_lookup) {
1915                 /*
1916                  * Check if the name already exist, though it will be checked in
1917                  * _index_insert also, for avoiding rolling back if exists
1918                  * _index_insert.
1919                  */
1920                 rc = __mdd_lookup(env, pobj, pattr, lname, fid,
1921                                   MAY_WRITE | MAY_EXEC);
1922                 if (rc != -ENOENT)
1923                         RETURN(rc ? : -EEXIST);
1924
1925                 /* Permission is already being checked in mdd_lookup */
1926                 check_perm = false;
1927         }
1928
1929         rc = mdd_may_create(env, obj, pattr, NULL, check_perm);
1930         if (rc != 0)
1931                 RETURN(rc);
1932
1933         /* sgid check */
1934         if (pattr->la_mode & S_ISGID) {
1935                 cattr->la_gid = pattr->la_gid;
1936                 if (S_ISDIR(cattr->la_mode)) {
1937                         cattr->la_mode |= S_ISGID;
1938                         cattr->la_valid |= LA_MODE;
1939                 }
1940         }
1941
1942         rc = mdd_name_check(m, lname);
1943         if (rc < 0)
1944                 RETURN(rc);
1945
1946         switch (cattr->la_mode & S_IFMT) {
1947         case S_IFLNK: {
1948                 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1949
1950                 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1951                         RETURN(-ENAMETOOLONG);
1952                 else
1953                         RETURN(0);
1954         }
1955         case S_IFDIR:
1956         case S_IFREG:
1957         case S_IFCHR:
1958         case S_IFBLK:
1959         case S_IFIFO:
1960         case S_IFSOCK:
1961                 rc = 0;
1962                 break;
1963         default:
1964                 rc = -EINVAL;
1965                 break;
1966         }
1967         RETURN(rc);
1968 }
1969
1970 static int mdd_declare_object_create(const struct lu_env *env,
1971                                      struct mdd_device *mdd,
1972                                      struct mdd_object *p, struct mdd_object *c,
1973                                      struct lu_attr *attr,
1974                                      struct thandle *handle,
1975                                      const struct md_op_spec *spec,
1976                                      struct lu_buf *def_acl_buf,
1977                                      struct lu_buf *acl_buf,
1978                                      struct dt_allocation_hint *hint)
1979 {
1980         int rc;
1981
1982         rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec,
1983                                                 hint);
1984         if (rc)
1985                 GOTO(out, rc);
1986
1987 #ifdef CONFIG_FS_POSIX_ACL
1988         if (def_acl_buf->lb_len > 0 && S_ISDIR(attr->la_mode)) {
1989                 /* if dir, then can inherit default ACl */
1990                 rc = mdo_declare_xattr_set(env, c, def_acl_buf,
1991                                            XATTR_NAME_ACL_DEFAULT,
1992                                            0, handle);
1993                 if (rc)
1994                         GOTO(out, rc);
1995         }
1996
1997         if (acl_buf->lb_len > 0) {
1998                 rc = mdo_declare_attr_set(env, c, attr, handle);
1999                 if (rc)
2000                         GOTO(out, rc);
2001
2002                 rc = mdo_declare_xattr_set(env, c, acl_buf,
2003                                            XATTR_NAME_ACL_ACCESS, 0, handle);
2004                 if (rc)
2005                         GOTO(out, rc);
2006         }
2007 #endif
2008         rc = mdd_declare_object_initialize(env, p, c, attr, handle);
2009         if (rc)
2010                 GOTO(out, rc);
2011
2012         /* replay case, create LOV EA from client data */
2013         if (spec->no_create ||
2014             (spec->sp_cr_flags & MDS_OPEN_HAS_EA && S_ISREG(attr->la_mode))) {
2015                 const struct lu_buf *buf;
2016
2017                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2018                                         spec->u.sp_ea.eadatalen);
2019                 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV, 0,
2020                                            handle);
2021                 if (rc)
2022                         GOTO(out, rc);
2023         }
2024
2025         if (S_ISLNK(attr->la_mode)) {
2026                 const char *target_name = spec->u.sp_symname;
2027                 int sym_len = strlen(target_name);
2028                 const struct lu_buf *buf;
2029
2030                 buf = mdd_buf_get_const(env, target_name, sym_len);
2031                 rc = dt_declare_record_write(env, mdd_object_child(c),
2032                                              buf, 0, handle);
2033                 if (rc)
2034                         GOTO(out, rc);
2035         }
2036 out:
2037         return rc;
2038 }
2039
2040 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
2041                               struct mdd_object *p, struct mdd_object *c,
2042                               const struct lu_name *name,
2043                               struct lu_attr *attr,
2044                               struct thandle *handle,
2045                               const struct md_op_spec *spec,
2046                               struct linkea_data *ldata,
2047                               struct lu_buf *def_acl_buf,
2048                               struct lu_buf *acl_buf,
2049                               struct dt_allocation_hint *hint)
2050 {
2051         int rc;
2052
2053         rc = mdd_declare_object_create(env, mdd, p, c, attr, handle, spec,
2054                                        def_acl_buf, acl_buf, hint);
2055         if (rc)
2056                 GOTO(out, rc);
2057
2058         if (S_ISDIR(attr->la_mode)) {
2059                 rc = mdo_declare_ref_add(env, p, handle);
2060                 if (rc)
2061                         GOTO(out, rc);
2062         }
2063
2064         if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2065                 rc = orph_declare_index_insert(env, c, attr->la_mode, handle);
2066                 if (rc)
2067                         GOTO(out, rc);
2068         } else {
2069                 struct lu_attr  *la = &mdd_env_info(env)->mti_la_for_fix;
2070
2071                 rc = mdo_declare_index_insert(env, p, mdo2fid(c), attr->la_mode,
2072                                               name->ln_name, handle);
2073                 if (rc != 0)
2074                         return rc;
2075
2076                 rc = mdd_declare_links_add(env, c, handle, ldata, MLAO_IGNORE);
2077                 if (rc)
2078                         return rc;
2079
2080                 *la = *attr;
2081                 la->la_valid = LA_CTIME | LA_MTIME;
2082                 rc = mdo_declare_attr_set(env, p, la, handle);
2083                 if (rc)
2084                         return rc;
2085
2086                 rc = mdd_declare_changelog_store(env, mdd, name, NULL, handle);
2087                 if (rc)
2088                         return rc;
2089         }
2090 out:
2091         return rc;
2092 }
2093
2094 static int mdd_acl_init(const struct lu_env *env, struct mdd_object *pobj,
2095                         struct lu_attr *la, struct lu_buf *def_acl_buf,
2096                         struct lu_buf *acl_buf)
2097 {
2098         int     rc;
2099         ENTRY;
2100
2101         if (S_ISLNK(la->la_mode)) {
2102                 acl_buf->lb_len = 0;
2103                 def_acl_buf->lb_len = 0;
2104                 RETURN(0);
2105         }
2106
2107         mdd_read_lock(env, pobj, MOR_TGT_PARENT);
2108         rc = mdo_xattr_get(env, pobj, def_acl_buf,
2109                            XATTR_NAME_ACL_DEFAULT);
2110         mdd_read_unlock(env, pobj);
2111         if (rc > 0) {
2112                 /* If there are default ACL, fix mode/ACL by default ACL */
2113                 def_acl_buf->lb_len = rc;
2114                 LASSERT(def_acl_buf->lb_len <= acl_buf->lb_len);
2115                 memcpy(acl_buf->lb_buf, def_acl_buf->lb_buf, rc);
2116                 acl_buf->lb_len = rc;
2117                 rc = __mdd_fix_mode_acl(env, acl_buf, &la->la_mode);
2118                 if (rc < 0)
2119                         RETURN(rc);
2120         } else if (rc == -ENODATA || rc == -EOPNOTSUPP) {
2121                 /* If there are no default ACL, fix mode by mask */
2122                 struct lu_ucred *uc = lu_ucred(env);
2123
2124                 /* The create triggered by MDT internal events, such as
2125                  * LFSCK reset, will not contain valid "uc". */
2126                 if (unlikely(uc != NULL))
2127                         la->la_mode &= ~uc->uc_umask;
2128                 rc = 0;
2129                 acl_buf->lb_len = 0;
2130                 def_acl_buf->lb_len = 0;
2131         }
2132
2133         RETURN(rc);
2134 }
2135
2136 /**
2137  * Create a metadata object and initialize it, set acl, xattr.
2138  **/
2139 static int mdd_object_create(const struct lu_env *env, struct mdd_object *pobj,
2140                              struct mdd_object *son, struct lu_attr *attr,
2141                              struct md_op_spec *spec, struct lu_buf *acl_buf,
2142                              struct lu_buf *def_acl_buf,
2143                              struct dt_allocation_hint *hint,
2144                              struct thandle *handle)
2145 {
2146         int                     rc;
2147
2148         mdd_write_lock(env, son, MOR_TGT_CHILD);
2149         rc = mdd_object_create_internal(env, NULL, son, attr, handle, spec,
2150                                         hint);
2151         if (rc)
2152                 GOTO(unlock, rc);
2153
2154         /* Note: In DNE phase I, for striped dir, though sub-stripes will be
2155          * created in declare phase, they also needs to be added to master
2156          * object as sub-directory entry. So it has to initialize the master
2157          * object, then set dir striped EA.(in mdo_xattr_set) */
2158         rc = mdd_object_initialize(env, mdo2fid(pobj), son, attr, handle,
2159                                    spec);
2160         if (rc != 0)
2161                 GOTO(err_destroy, rc);
2162
2163         /*
2164          * in case of replay we just set LOVEA provided by the client
2165          * XXX: I think it would be interesting to try "old" way where
2166          *      MDT calls this xattr_set(LOV) in a different transaction.
2167          *      probably this way we code can be made better.
2168          */
2169
2170         /* During creation, there are only a few cases we need do xattr_set to
2171          * create stripes.
2172          * 1. regular file: see comments above.
2173          * 2. dir: inherit default striping or pool settings from parent.
2174          * 3. create striped directory with provided stripeEA.
2175          * 4. create striped directory because inherit default layout from the
2176          * parent.
2177          */
2178         if (spec->no_create ||
2179             (S_ISREG(attr->la_mode) && spec->sp_cr_flags & MDS_OPEN_HAS_EA) ||
2180             S_ISDIR(attr->la_mode)) {
2181                 const struct lu_buf *buf;
2182
2183                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
2184                                         spec->u.sp_ea.eadatalen);
2185                 rc = mdo_xattr_set(env, son, buf,
2186                                    S_ISDIR(attr->la_mode) ? XATTR_NAME_LMV :
2187                                                             XATTR_NAME_LOV, 0,
2188                                    handle);
2189                 if (rc != 0)
2190                         GOTO(err_destroy, rc);
2191         }
2192
2193 #ifdef CONFIG_FS_POSIX_ACL
2194         if (def_acl_buf != NULL && def_acl_buf->lb_len > 0 &&
2195             S_ISDIR(attr->la_mode)) {
2196                 /* set default acl */
2197                 rc = mdo_xattr_set(env, son, def_acl_buf,
2198                                    XATTR_NAME_ACL_DEFAULT, 0,
2199                                    handle);
2200                 if (rc)
2201                         GOTO(err_destroy, rc);
2202         }
2203         /* set its own acl */
2204         if (acl_buf != NULL && acl_buf->lb_len > 0) {
2205                 rc = mdo_xattr_set(env, son, acl_buf,
2206                                    XATTR_NAME_ACL_ACCESS,
2207                                    0, handle);
2208                 if (rc)
2209                         GOTO(err_destroy, rc);
2210         }
2211 #endif
2212
2213         if (S_ISLNK(attr->la_mode)) {
2214                 struct lu_ucred  *uc = lu_ucred_assert(env);
2215                 struct dt_object *dt = mdd_object_child(son);
2216                 const char *target_name = spec->u.sp_symname;
2217                 int sym_len = strlen(target_name);
2218                 const struct lu_buf *buf;
2219                 loff_t pos = 0;
2220
2221                 buf = mdd_buf_get_const(env, target_name, sym_len);
2222                 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
2223                                                 uc->uc_cap &
2224                                                 CFS_CAP_SYS_RESOURCE_MASK);
2225
2226                 if (rc == sym_len)
2227                         rc = 0;
2228                 else
2229                         GOTO(err_initlized, rc = -EFAULT);
2230         }
2231
2232 err_initlized:
2233         if (unlikely(rc != 0)) {
2234                 int rc2;
2235                 if (S_ISDIR(attr->la_mode)) {
2236                         /* Drop the reference, no need to delete "."/"..",
2237                          * because the object to be destroied directly. */
2238                         rc2 = mdo_ref_del(env, son, handle);
2239                         if (rc2 != 0)
2240                                 GOTO(unlock, rc);
2241                 }
2242                 rc2 = mdo_ref_del(env, son, handle);
2243                 if (rc2 != 0)
2244                         GOTO(unlock, rc);
2245 err_destroy:
2246                 mdo_destroy(env, son, handle);
2247         }
2248 unlock:
2249         mdd_write_unlock(env, son);
2250         RETURN(rc);
2251 }
2252
2253 /*
2254  * Create object and insert it into namespace.
2255  */
2256 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
2257                       const struct lu_name *lname, struct md_object *child,
2258                       struct md_op_spec *spec, struct md_attr* ma)
2259 {
2260         struct mdd_thread_info  *info = mdd_env_info(env);
2261         struct lu_attr          *la = &info->mti_la_for_fix;
2262         struct mdd_object       *mdd_pobj = md2mdd_obj(pobj);
2263         struct mdd_object       *son = md2mdd_obj(child);
2264         struct mdd_device       *mdd = mdo2mdd(pobj);
2265         struct lu_attr          *attr = &ma->ma_attr;
2266         struct thandle          *handle;
2267         struct lu_attr          *pattr = &info->mti_pattr;
2268         struct lu_buf           acl_buf;
2269         struct lu_buf           def_acl_buf;
2270         struct linkea_data      *ldata = &info->mti_link_data;
2271         const char              *name = lname->ln_name;
2272         struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
2273         int                      rc;
2274         int                      rc2;
2275         ENTRY;
2276
2277         /*
2278          * Two operations have to be performed:
2279          *
2280          *  - an allocation of a new object (->do_create()), and
2281          *
2282          *  - an insertion into a parent index (->dio_insert()).
2283          *
2284          * Due to locking, operation order is not important, when both are
2285          * successful, *but* error handling cases are quite different:
2286          *
2287          *  - if insertion is done first, and following object creation fails,
2288          *  insertion has to be rolled back, but this operation might fail
2289          *  also leaving us with dangling index entry.
2290          *
2291          *  - if creation is done first, is has to be undone if insertion
2292          *  fails, leaving us with leaked space, which is neither good, nor
2293          *  fatal.
2294          *
2295          * It seems that creation-first is simplest solution, but it is
2296          * sub-optimal in the frequent
2297          *
2298          *         $ mkdir foo
2299          *         $ mkdir foo
2300          *
2301          * case, because second mkdir is bound to create object, only to
2302          * destroy it immediately.
2303          *
2304          * To avoid this follow local file systems that do double lookup:
2305          *
2306          *     0. lookup -> -EEXIST (mdd_create_sanity_check())
2307          *
2308          *     1. create            (mdd_object_create_internal())
2309          *
2310          *     2. insert            (__mdd_index_insert(), lookup again)
2311          */
2312
2313         rc = mdd_la_get(env, mdd_pobj, pattr);
2314         if (rc != 0)
2315                 RETURN(rc);
2316
2317         /* Sanity checks before big job. */
2318         rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
2319         if (rc)
2320                 RETURN(rc);
2321
2322         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
2323                 GOTO(out_free, rc = -EINPROGRESS);
2324
2325         handle = mdd_trans_create(env, mdd);
2326         if (IS_ERR(handle))
2327                 GOTO(out_free, rc = PTR_ERR(handle));
2328
2329         acl_buf.lb_buf = info->mti_xattr_buf;
2330         acl_buf.lb_len = sizeof(info->mti_xattr_buf);
2331         def_acl_buf.lb_buf = info->mti_key;
2332         def_acl_buf.lb_len = sizeof(info->mti_key);
2333         rc = mdd_acl_init(env, mdd_pobj, attr, &def_acl_buf, &acl_buf);
2334         if (rc < 0)
2335                 GOTO(out_stop, rc);
2336
2337         mdd_object_make_hint(env, mdd_pobj, son, attr, spec, hint);
2338
2339         memset(ldata, 0, sizeof(*ldata));
2340         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PARENT)) {
2341                 struct lu_fid tfid = *mdd_object_fid(mdd_pobj);
2342
2343                 tfid.f_oid--;
2344                 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2345                                         &tfid, lname, 1, 0, ldata);
2346         } else {
2347                 rc = mdd_linkea_prepare(env, son, NULL, NULL,
2348                                         mdd_object_fid(mdd_pobj),
2349                                         lname, 1, 0, ldata);
2350         }
2351
2352         rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
2353                                 handle, spec, ldata, &def_acl_buf, &acl_buf,
2354                                 hint);
2355         if (rc)
2356                 GOTO(out_stop, rc);
2357
2358         rc = mdd_trans_start(env, mdd, handle);
2359         if (rc)
2360                 GOTO(out_stop, rc);
2361
2362         rc = mdd_object_create(env, mdd_pobj, son, attr, spec, &acl_buf,
2363                                &def_acl_buf, hint, handle);
2364         if (rc != 0)
2365                 GOTO(out_stop, rc);
2366
2367         if (unlikely(spec->sp_cr_flags & MDS_OPEN_VOLATILE)) {
2368                 mdd_write_lock(env, son, MOR_TGT_CHILD);
2369                 rc = __mdd_orphan_add(env, son, handle);
2370                 GOTO(out_volatile, rc);
2371         } else {
2372                 rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
2373                                         attr->la_mode, name, handle);
2374                 if (rc != 0)
2375                         GOTO(err_created, rc);
2376
2377                 mdd_links_add(env, son, mdo2fid(mdd_pobj), lname, handle,
2378                               ldata, 1);
2379
2380                 /* update parent directory mtime/ctime */
2381                 *la = *attr;
2382                 la->la_valid = LA_CTIME | LA_MTIME;
2383                 rc = mdd_update_time(env, mdd_pobj, pattr, la, handle);
2384                 if (rc)
2385                         GOTO(err_insert, rc);
2386         }
2387
2388         EXIT;
2389 err_insert:
2390         if (rc != 0) {
2391                 int rc2;
2392
2393                 if (spec->sp_cr_flags & MDS_OPEN_VOLATILE)
2394                         rc2 = __mdd_orphan_del(env, son, handle);
2395                 else
2396                         rc2 = __mdd_index_delete(env, mdd_pobj, name,
2397                                                  S_ISDIR(attr->la_mode),
2398                                                  handle);
2399                 if (rc2 != 0)
2400                         goto out_stop;
2401
2402 err_created:
2403                 mdd_write_lock(env, son, MOR_TGT_CHILD);
2404                 if (S_ISDIR(attr->la_mode)) {
2405                         /* Drop the reference, no need to delete "."/"..",
2406                          * because the object is to be destroyed directly. */
2407                         rc2 = mdo_ref_del(env, son, handle);
2408                         if (rc2 != 0) {
2409                                 mdd_write_unlock(env, son);
2410                                 goto out_stop;
2411                         }
2412                 }
2413 out_volatile:
2414                 /* For volatile files drop one link immediately, since there is
2415                  * no filename in the namespace, and save any error returned. */
2416                 rc2 = mdo_ref_del(env, son, handle);
2417                 if (rc2 != 0) {
2418                         mdd_write_unlock(env, son);
2419                         if (unlikely(rc == 0))
2420                                 rc = rc2;
2421                         goto out_stop;
2422                 }
2423
2424                 /* Don't destroy the volatile object on success */
2425                 if (likely(rc != 0))
2426                         mdo_destroy(env, son, handle);
2427                 mdd_write_unlock(env, son);
2428         }
2429
2430         if (rc == 0 && fid_is_namespace_visible(mdo2fid(son)) &&
2431             likely((spec->sp_cr_flags & MDS_OPEN_VOLATILE) == 0))
2432                 rc = mdd_changelog_ns_store(env, mdd,
2433                                 S_ISDIR(attr->la_mode) ? CL_MKDIR :
2434                                 S_ISREG(attr->la_mode) ? CL_CREATE :
2435                                 S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
2436                                 0, son, mdo2fid(mdd_pobj), NULL, NULL, lname,
2437                                 NULL, handle);
2438 out_stop:
2439         rc2 = mdd_trans_stop(env, mdd, rc, handle);
2440         if (rc == 0)
2441                 rc = rc2;
2442 out_free:
2443         if (ldata->ld_buf && ldata->ld_buf->lb_len > OBD_ALLOC_BIG)
2444                 /* if we vmalloced a large buffer drop it */
2445                 lu_buf_free(ldata->ld_buf);
2446
2447         /* The child object shouldn't be cached anymore */
2448         if (rc)
2449                 set_bit(LU_OBJECT_HEARD_BANSHEE,
2450                         &child->mo_lu.lo_header->loh_flags);
2451         return rc;
2452 }
2453
2454 /*
2455  * Get locks on parents in proper order
2456  * RETURN: < 0 - error, rename_order if successful
2457  */
2458 enum rename_order {
2459         MDD_RN_SAME,
2460         MDD_RN_SRCTGT,
2461         MDD_RN_TGTSRC
2462 };
2463
2464 static int mdd_rename_order(const struct lu_env *env,
2465                             struct mdd_device *mdd,
2466                             struct mdd_object *src_pobj,
2467                             const struct lu_attr *pattr,
2468                             struct mdd_object *tgt_pobj)
2469 {
2470         /* order of locking, 1 - tgt-src, 0 - src-tgt*/
2471         int rc;
2472         ENTRY;
2473
2474         if (src_pobj == tgt_pobj)
2475                 RETURN(MDD_RN_SAME);
2476
2477         /* compared the parent child relationship of src_p&tgt_p */
2478         if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
2479                 rc = MDD_RN_SRCTGT;
2480         } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
2481                 rc = MDD_RN_TGTSRC;
2482         } else {
2483                 rc = mdd_is_parent(env, mdd, src_pobj, pattr, mdo2fid(tgt_pobj),
2484                                    NULL);
2485                 if (rc == -EREMOTE)
2486                         rc = 0;
2487
2488                 if (rc == 1)
2489                         rc = MDD_RN_TGTSRC;
2490                 else if (rc == 0)
2491                         rc = MDD_RN_SRCTGT;
2492         }
2493
2494         RETURN(rc);
2495 }
2496
2497 /* has not mdd_write{read}_lock on any obj yet. */
2498 static int mdd_rename_sanity_check(const struct lu_env *env,
2499                                    struct mdd_object *src_pobj,
2500                                    const struct lu_attr *pattr,
2501                                    struct mdd_object *tgt_pobj,
2502                                    const struct lu_attr *tpattr,
2503                                    struct mdd_object *sobj,
2504                                    const struct lu_attr *cattr,
2505                                    struct mdd_object *tobj,
2506                                    const struct lu_attr *tattr)
2507 {
2508         int rc = 0;
2509         ENTRY;
2510
2511         /* XXX: when get here, sobj must NOT be NULL,
2512          * the other case has been processed in cld_rename
2513          * before mdd_rename and enable MDS_PERM_BYPASS. */
2514         LASSERT(sobj);
2515
2516         rc = mdd_may_delete(env, src_pobj, pattr, sobj, cattr, NULL, 1, 0);
2517         if (rc)
2518                 RETURN(rc);
2519
2520         /* XXX: when get here, "tobj == NULL" means tobj must
2521          * NOT exist (neither on remote MDS, such case has been
2522          * processed in cld_rename before mdd_rename and enable
2523          * MDS_PERM_BYPASS).
2524          * So check may_create, but not check may_unlink. */
2525         if (tobj == NULL)
2526                 rc = mdd_may_create(env, tgt_pobj, tpattr, NULL,
2527                                     (src_pobj != tgt_pobj));
2528         else
2529                 rc = mdd_may_delete(env, tgt_pobj, tpattr, tobj, tattr, cattr,
2530                                     (src_pobj != tgt_pobj), 1);
2531
2532         if (!rc && !tobj && (src_pobj != tgt_pobj) && S_ISDIR(cattr->la_mode))
2533                 rc = __mdd_may_link(env, tgt_pobj, tpattr);
2534
2535         RETURN(rc);
2536 }
2537
2538 static int mdd_declare_rename(const struct lu_env *env,
2539                               struct mdd_device *mdd,
2540                               struct mdd_object *mdd_spobj,
2541                               struct mdd_object *mdd_tpobj,
2542                               struct mdd_object *mdd_sobj,
2543                               struct mdd_object *mdd_tobj,
2544                               const struct lu_name *tname,
2545                               const struct lu_name *sname,
2546                               struct md_attr *ma,
2547                               struct linkea_data *ldata,
2548                               struct thandle *handle)
2549 {
2550         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
2551         int rc;
2552
2553         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2554         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2555
2556         LASSERT(mdd_spobj);
2557         LASSERT(mdd_tpobj);
2558         LASSERT(mdd_sobj);
2559
2560         /* name from source dir */
2561         rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
2562         if (rc)
2563                 return rc;
2564
2565         /* .. from source child */
2566         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
2567                 /* source child can be directory,
2568                  * counted by source dir's nlink */
2569                 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
2570                 if (rc)
2571                         return rc;
2572                 if (mdd_spobj != mdd_tpobj) {
2573                         rc = mdo_declare_index_delete(env, mdd_sobj, dotdot,
2574                                                       handle);
2575                         if (rc != 0)
2576                                 return rc;
2577
2578                         rc = mdo_declare_index_insert(env, mdd_sobj,
2579                                                       mdo2fid(mdd_tpobj),
2580                                                       S_IFDIR, dotdot, handle);
2581                         if (rc != 0)
2582                                 return rc;
2583                 }
2584
2585                 /* new target child can be directory,
2586                  * counted by target dir's nlink */
2587                 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
2588                 if (rc != 0)
2589                         return rc;
2590         }
2591
2592         la->la_valid = LA_CTIME | LA_MTIME;
2593         rc = mdo_declare_attr_set(env, mdd_spobj, la, handle);
2594         if (rc != 0)
2595                 return rc;
2596
2597         rc = mdo_declare_attr_set(env, mdd_tpobj, la, handle);
2598         if (rc != 0)
2599                 return rc;
2600
2601         la->la_valid = LA_CTIME;
2602         rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
2603         if (rc)
2604                 return rc;
2605
2606         rc = mdd_declare_links_add(env, mdd_sobj, handle, ldata,
2607                 S_ISREG(mdd_object_type(mdd_sobj)) ? MLAO_CHECK : MLAO_IGNORE);
2608         if (rc)
2609                 return rc;
2610
2611         /* new name */
2612         rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2613                                       mdd_object_type(mdd_sobj),
2614                                       tname->ln_name, handle);
2615         if (rc != 0)
2616                 return rc;
2617
2618         /* name from target dir (old name), we declare it unconditionally
2619          * as mdd_rename() calls delete unconditionally as well. so just
2620          * to balance declarations vs calls to change ... */
2621         rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2622         if (rc)
2623                 return rc;
2624
2625         if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2626                 /* delete target child in target parent directory */
2627                 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2628                 if (rc)
2629                         return rc;
2630
2631                 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2632                         /* target child can be directory,
2633                          * delete "." reference in target child directory */
2634                         rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2635                         if (rc)
2636                                 return rc;
2637
2638                         /* delete ".." reference in target parent directory */
2639                         rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2640                         if (rc)
2641                                 return rc;
2642                 }
2643
2644                 la->la_valid = LA_CTIME;
2645                 rc = mdo_declare_attr_set(env, mdd_tobj, la, handle);
2646                 if (rc)
2647                         return rc;
2648
2649                 rc = mdd_declare_finish_unlink(env, mdd_tobj, handle);
2650                 if (rc)
2651                         return rc;
2652         }
2653
2654         rc = mdd_declare_changelog_store(env, mdd, tname, sname, handle);
2655         if (rc)
2656                 return rc;
2657
2658         return rc;
2659 }
2660
2661 /* src object can be remote that is why we use only fid and type of object */
2662 static int mdd_rename(const struct lu_env *env,
2663                       struct md_object *src_pobj, struct md_object *tgt_pobj,
2664                       const struct lu_fid *lf, const struct lu_name *lsname,
2665                       struct md_object *tobj, const struct lu_name *ltname,
2666                       struct md_attr *ma)
2667 {
2668         const char *sname = lsname->ln_name;
2669         const char *tname = ltname->ln_name;
2670         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
2671         struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2672         struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2673         struct mdd_device *mdd = mdo2mdd(src_pobj);
2674         struct mdd_object *mdd_sobj = NULL;                  /* source object */
2675         struct mdd_object *mdd_tobj = NULL;
2676         struct lu_attr *cattr = MDD_ENV_VAR(env, cattr);
2677         struct lu_attr *pattr = MDD_ENV_VAR(env, pattr);
2678         struct lu_attr *tattr = MDD_ENV_VAR(env, tattr);
2679         struct lu_attr *tpattr = MDD_ENV_VAR(env, tpattr);
2680         struct thandle *handle;
2681         struct linkea_data  *ldata = &mdd_env_info(env)->mti_link_data;
2682         const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2683         const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2684         bool is_dir;
2685         bool tobj_ref = 0;
2686         bool tobj_locked = 0;
2687         unsigned cl_flags = 0;
2688         int rc, rc2;
2689         ENTRY;
2690
2691         if (tobj)
2692                 mdd_tobj = md2mdd_obj(tobj);
2693
2694         mdd_sobj = mdd_object_find(env, mdd, lf);
2695
2696         rc = mdd_la_get(env, mdd_sobj, cattr);
2697         if (rc)
2698                 GOTO(out_pending, rc);
2699
2700         rc = mdd_la_get(env, mdd_spobj, pattr);
2701         if (rc)
2702                 GOTO(out_pending, rc);
2703
2704         if (mdd_tobj) {
2705                 rc = mdd_la_get(env, mdd_tobj, tattr);
2706                 if (rc)
2707                         GOTO(out_pending, rc);
2708         }
2709
2710         rc = mdd_la_get(env, mdd_tpobj, tpattr);
2711         if (rc)
2712                 GOTO(out_pending, rc);
2713
2714         rc = mdd_rename_sanity_check(env, mdd_spobj, pattr, mdd_tpobj, tpattr,
2715                                      mdd_sobj, cattr, mdd_tobj, tattr);
2716         if (rc)
2717                 GOTO(out_pending, rc);
2718
2719         rc = mdd_name_check(mdd, ltname);
2720         if (rc < 0)
2721                 GOTO(out_pending, rc);
2722
2723         handle = mdd_trans_create(env, mdd);
2724         if (IS_ERR(handle))
2725                 GOTO(out_pending, rc = PTR_ERR(handle));
2726
2727         memset(ldata, 0, sizeof(*ldata));
2728         mdd_linkea_prepare(env, mdd_sobj, NULL, NULL, mdd_object_fid(mdd_tpobj),
2729                            ltname, 1, 0, ldata);
2730         rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2731                                 mdd_tobj, lsname, ltname, ma, ldata, handle);
2732         if (rc)
2733                 GOTO(stop, rc);
2734
2735         rc = mdd_trans_start(env, mdd, handle);
2736         if (rc)
2737                 GOTO(stop, rc);
2738
2739         /* FIXME: Should consider tobj and sobj too in rename_lock. */
2740         rc = mdd_rename_order(env, mdd, mdd_spobj, pattr, mdd_tpobj);
2741         if (rc < 0)
2742                 GOTO(cleanup_unlocked, rc);
2743
2744         is_dir = S_ISDIR(cattr->la_mode);
2745
2746         /* Remove source name from source directory */
2747         rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle);
2748         if (rc)
2749                 GOTO(cleanup, rc);
2750
2751         /* "mv dir1 dir2" needs "dir1/.." link update */
2752         if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2753                 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2754                 if (rc != 0)
2755                         GOTO(fixup_spobj2, rc);
2756
2757                 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, S_IFDIR,
2758                                              dotdot, handle);
2759                 if (rc != 0)
2760                         GOTO(fixup_spobj, rc);
2761         }
2762
2763         /* Remove target name from target directory
2764          * Here tobj can be remote one, so we do index_delete unconditionally
2765          * and -ENOENT is allowed.
2766          */
2767         rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2768         if (rc != 0) {
2769                 if (mdd_tobj) {
2770                         /* tname might been renamed to something else */
2771                         GOTO(fixup_spobj, rc);
2772                 }
2773                 if (rc != -ENOENT)
2774                         GOTO(fixup_spobj, rc);
2775         }
2776
2777         /* Insert new fid with target name into target dir */
2778         rc = __mdd_index_insert(env, mdd_tpobj, lf, cattr->la_mode,
2779                                 tname, handle);
2780         if (rc != 0)
2781                 GOTO(fixup_tpobj, rc);
2782
2783         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2784         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2785
2786         /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2787         if (mdd_sobj) {
2788                 la->la_valid = LA_CTIME;
2789                 rc = mdd_update_time(env, mdd_sobj, cattr, la, handle);
2790                 if (rc)
2791                         GOTO(fixup_tpobj, rc);
2792         }
2793
2794         /* Remove old target object
2795          * For tobj is remote case cmm layer has processed
2796          * and set tobj to NULL then. So when tobj is NOT NULL,
2797          * it must be local one.
2798          */
2799         if (tobj && mdd_object_exists(mdd_tobj)) {
2800                 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2801                 tobj_locked = 1;
2802                 if (mdd_is_dead_obj(mdd_tobj)) {
2803                         /* shld not be dead, something is wrong */
2804                         CERROR("tobj is dead, something is wrong\n");
2805                         rc = -EINVAL;
2806                         goto cleanup;
2807                 }
2808                 mdo_ref_del(env, mdd_tobj, handle);
2809
2810                 /* Remove dot reference. */
2811                 if (S_ISDIR(tattr->la_mode))
2812                         mdo_ref_del(env, mdd_tobj, handle);
2813                 tobj_ref = 1;
2814
2815                 /* fetch updated nlink */
2816                 rc = mdd_la_get(env, mdd_tobj, tattr);
2817                 if (rc != 0) {
2818                         CERROR("%s: Failed to get nlink for tobj "
2819                                 DFID": rc = %d\n",
2820                                 mdd2obd_dev(mdd)->obd_name,
2821                                 PFID(tpobj_fid), rc);
2822                         GOTO(fixup_tpobj, rc);
2823                 }
2824
2825                 la->la_valid = LA_CTIME;
2826                 rc = mdd_update_time(env, mdd_tobj, tattr, la, handle);
2827                 if (rc != 0) {
2828                         CERROR("%s: Failed to set ctime for tobj "
2829                                 DFID": rc = %d\n",
2830                                 mdd2obd_dev(mdd)->obd_name,
2831                                 PFID(tpobj_fid), rc);
2832                         GOTO(fixup_tpobj, rc);
2833                 }
2834
2835                 /* XXX: this transfer to ma will be removed with LOD/OSP */
2836                 ma->ma_attr = *tattr;
2837                 ma->ma_valid |= MA_INODE;
2838                 rc = mdd_finish_unlink(env, mdd_tobj, ma, mdd_tpobj, ltname,
2839                                        handle);
2840                 if (rc != 0) {
2841                         CERROR("%s: Failed to unlink tobj "
2842                                 DFID": rc = %d\n",
2843                                 mdd2obd_dev(mdd)->obd_name,
2844                                 PFID(tpobj_fid), rc);
2845                         GOTO(fixup_tpobj, rc);
2846                 }
2847
2848                 /* fetch updated nlink */
2849                 rc = mdd_la_get(env, mdd_tobj, tattr);
2850                 if (rc != 0) {
2851                         CERROR("%s: Failed to get nlink for tobj "
2852                                 DFID": rc = %d\n",
2853                                 mdd2obd_dev(mdd)->obd_name,
2854                                 PFID(tpobj_fid), rc);
2855                         GOTO(fixup_tpobj, rc);
2856                 }
2857                 /* XXX: this transfer to ma will be removed with LOD/OSP */
2858                 ma->ma_attr = *tattr;
2859                 ma->ma_valid |= MA_INODE;
2860
2861                 if (tattr->la_nlink == 0) {
2862                         cl_flags |= CLF_RENAME_LAST;
2863                         if (mdd_hsm_archive_exists(env, mdd_tobj, ma))
2864                                 cl_flags |= CLF_RENAME_LAST_EXISTS;
2865                 }
2866         }
2867
2868         la->la_valid = LA_CTIME | LA_MTIME;
2869         rc = mdd_update_time(env, mdd_spobj, pattr, la, handle);
2870         if (rc)
2871                 GOTO(fixup_tpobj, rc);
2872
2873         if (mdd_spobj != mdd_tpobj) {
2874                 la->la_valid = LA_CTIME | LA_MTIME;
2875                 rc = mdd_update_time(env, mdd_tpobj, tpattr, la, handle);
2876         }
2877
2878         if (rc == 0 && mdd_sobj) {
2879                 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2880                 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2881                                       mdo2fid(mdd_tpobj), ltname, handle, NULL,
2882                                       0, 0);
2883                 if (rc == -ENOENT)
2884                         /* Old files might not have EA entry */
2885                         mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2886                                       lsname, handle, NULL, 0);
2887                 mdd_write_unlock(env, mdd_sobj);
2888                 /* We don't fail the transaction if the link ea can't be
2889                    updated -- fid2path will use alternate lookup method. */
2890                 rc = 0;
2891         }
2892
2893         EXIT;
2894
2895 fixup_tpobj:
2896         if (rc) {
2897                 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle);
2898                 if (rc2)
2899                         CWARN("tp obj fix error %d\n",rc2);
2900
2901                 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2902                     !mdd_is_dead_obj(mdd_tobj)) {
2903                         if (tobj_ref) {
2904                                 mdo_ref_add(env, mdd_tobj, handle);
2905                                 if (is_dir)
2906                                         mdo_ref_add(env, mdd_tobj, handle);
2907                         }
2908
2909                         rc2 = __mdd_index_insert(env, mdd_tpobj,
2910                                                   mdo2fid(mdd_tobj),
2911                                                   mdd_object_type(mdd_tobj),
2912                                                   tname, handle);
2913                         if (rc2 != 0)
2914                                 CWARN("tp obj fix error: rc = %d\n", rc2);
2915                 }
2916         }
2917
2918 fixup_spobj:
2919         if (rc && is_dir && mdd_sobj && mdd_spobj != mdd_tpobj) {
2920                 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle);
2921                 if (rc2)
2922                         CWARN("%s: sp obj dotdot delete error: rc = %d\n",
2923                                mdd2obd_dev(mdd)->obd_name, rc2);
2924
2925
2926                 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid, S_IFDIR,
2927                                               dotdot, handle);
2928                 if (rc2 != 0)
2929                         CWARN("%s: sp obj dotdot insert error: rc = %d\n",
2930                               mdd2obd_dev(mdd)->obd_name, rc2);
2931         }
2932
2933 fixup_spobj2:
2934         if (rc != 0) {
2935                 rc2 = __mdd_index_insert(env, mdd_spobj, lf,
2936                                          mdd_object_type(mdd_sobj), sname,
2937                                          handle);
2938                 if (rc2 != 0)
2939                         CWARN("sp obj fix error: rc = %d\n", rc2);
2940         }
2941
2942 cleanup:
2943         if (tobj_locked)
2944                 mdd_write_unlock(env, mdd_tobj);
2945
2946 cleanup_unlocked:
2947         if (rc == 0)
2948                 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, cl_flags,
2949                                             mdd_tobj, tpobj_fid, lf, spobj_fid,
2950                                             ltname, lsname, handle);
2951
2952 stop:
2953         mdd_trans_stop(env, mdd, rc, handle);
2954
2955 out_pending:
2956         mdd_object_put(env, mdd_sobj);
2957         return rc;
2958 }
2959
2960 /**
2961  * During migration once the parent FID has been changed,
2962  * we need update the parent FID in linkea.
2963  **/
2964 static int mdd_linkea_update_child_internal(const struct lu_env *env,
2965                                             struct mdd_object *parent,
2966                                             struct mdd_object *child,
2967                                             const char *name, int namelen,
2968                                             struct thandle *handle,
2969                                             bool declare)
2970 {
2971         struct mdd_thread_info  *info = mdd_env_info(env);
2972         struct linkea_data      ldata = { NULL };
2973         struct lu_buf           *buf = &info->mti_link_buf;
2974         int                     count;
2975         int                     rc = 0;
2976
2977         ENTRY;
2978
2979         buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2980         if (buf->lb_buf == NULL)
2981                 RETURN(-ENOMEM);
2982
2983         ldata.ld_buf = buf;
2984         rc = mdd_links_read(env, child, &ldata);
2985         if (rc != 0) {
2986                 if (rc == -ENOENT || rc == -ENODATA)
2987                         rc = 0;
2988                 RETURN(rc);
2989         }
2990
2991         LASSERT(ldata.ld_leh != NULL);
2992         ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
2993         for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
2994                 struct mdd_device *mdd = mdo2mdd(&child->mod_obj);
2995                 struct lu_name lname;
2996                 struct lu_fid  fid;
2997
2998                 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
2999                                     &lname, &fid);
3000
3001                 if (strncmp(lname.ln_name, name, namelen) != 0 ||
3002                     lu_fid_eq(&fid, mdd_object_fid(parent))) {
3003                         ldata.ld_lee = (struct link_ea_entry *)
3004                                        ((char *)ldata.ld_lee +
3005                                         ldata.ld_reclen);
3006                         continue;
3007                 }
3008
3009                 CDEBUG(D_INFO, "%s: update "DFID" with %.*s:"DFID"\n",
3010                        mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(child)),
3011                        lname.ln_namelen, lname.ln_name,
3012                        PFID(mdd_object_fid(parent)));
3013                 /* update to the new parent fid */
3014                 linkea_entry_pack(ldata.ld_lee, &lname,
3015                                   mdd_object_fid(parent));
3016                 if (declare)
3017                         rc = mdd_declare_links_add(env, child, handle, &ldata,
3018                                                    MLAO_IGNORE);
3019                 else
3020                         rc = mdd_links_write(env, child, &ldata, handle);
3021                 break;
3022         }
3023         RETURN(rc);
3024 }
3025
3026 static int mdd_linkea_declare_update_child(const struct lu_env *env,
3027                                            struct mdd_object *parent,
3028                                            struct mdd_object *child,
3029                                            const char *name, int namelen,
3030                                            struct thandle *handle)
3031 {
3032         return mdd_linkea_update_child_internal(env, parent, child, name,
3033                                                 namelen, handle, true);
3034 }
3035
3036 static int mdd_linkea_update_child(const struct lu_env *env,
3037                                    struct mdd_object *parent,
3038                                    struct mdd_object *child,
3039                                    const char *name, int namelen,
3040                                    struct thandle *handle)
3041 {
3042         return mdd_linkea_update_child_internal(env, parent, child, name,
3043                                                 namelen, handle, false);
3044 }
3045
3046 static int mdd_update_linkea_internal(const struct lu_env *env,
3047                                       struct mdd_object *mdd_pobj,
3048                                       struct mdd_object *mdd_sobj,
3049                                       struct mdd_object *mdd_tobj,
3050                                       const struct lu_name *child_name,
3051                                       struct thandle *handle,
3052                                       int declare)
3053 {
3054         struct mdd_thread_info  *info = mdd_env_info(env);
3055         struct linkea_data      *ldata = &info->mti_link_data;
3056         int                     count;
3057         int                     rc = 0;
3058         ENTRY;
3059
3060         rc = mdd_links_read(env, mdd_sobj, ldata);
3061         if (rc != 0) {
3062                 if (rc == -ENOENT || rc == -ENODATA)
3063                         rc = 0;
3064                 RETURN(rc);
3065         }
3066
3067         /* If it is mulitple links file, we need update the name entry for
3068          * all parent */
3069         LASSERT(ldata->ld_leh != NULL);
3070         ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3071         for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3072                 struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3073                 struct mdd_object       *pobj;
3074                 struct lu_name          lname;
3075                 struct lu_fid           fid;
3076
3077                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3078                                     &lname, &fid);
3079                 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3080                                                          ldata->ld_reclen);
3081                 pobj = mdd_object_find(env, mdd, &fid);
3082                 if (IS_ERR(pobj)) {
3083                         CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3084                               mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3085                               PTR_ERR(pobj));
3086                         continue;
3087                 }
3088
3089                 if (!mdd_object_exists(pobj)) {
3090                         CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
3091                               mdd2obd_dev(mdd)->obd_name, PFID(&fid));
3092                         GOTO(next_put, rc);
3093                 }
3094
3095                 if (pobj == mdd_pobj &&
3096                     lname.ln_namelen == child_name->ln_namelen &&
3097                     strncmp(lname.ln_name, child_name->ln_name,
3098                             lname.ln_namelen) == 0) {
3099                         CDEBUG(D_INFO, "%s: skip its own %s: "DFID"\n",
3100                               mdd2obd_dev(mdd)->obd_name, child_name->ln_name,
3101                               PFID(&fid));
3102                         GOTO(next_put, rc);
3103                 }
3104
3105                 CDEBUG(D_INFO, "%s: update "DFID" with "DNAME":"DFID"\n",
3106                        mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(pobj)),
3107                        PNAME(&lname), PFID(mdd_object_fid(mdd_tobj)));
3108
3109                 if (declare) {
3110                         /* Remove source name from source directory */
3111                         /* Insert new fid with target name into target dir */
3112                         rc = mdo_declare_index_delete(env, pobj, lname.ln_name,
3113                                                       handle);
3114                         if (rc != 0)
3115                                 GOTO(next_put, rc);
3116
3117                         rc = mdo_declare_index_insert(env, pobj,
3118                                         mdd_object_fid(mdd_tobj),
3119                                         mdd_object_type(mdd_tobj),
3120                                         lname.ln_name, handle);
3121                         if (rc != 0)
3122                                 GOTO(next_put, rc);
3123
3124                         rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3125                         if (rc)
3126                                 GOTO(next_put, rc);
3127
3128                         rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3129                         if (rc)
3130                                 GOTO(next_put, rc);
3131                 } else {
3132                         rc = __mdd_index_delete(env, pobj, lname.ln_name,
3133                                                 0, handle);
3134                         if (rc)
3135                                 GOTO(next_put, rc);
3136
3137                         rc = __mdd_index_insert(env, pobj,
3138                                         mdd_object_fid(mdd_tobj),
3139                                         mdd_object_type(mdd_tobj),
3140                                         lname.ln_name, handle);
3141                         if (rc != 0)
3142                                 GOTO(next_put, rc);
3143
3144                         mdd_write_lock(env, mdd_tobj, MOR_SRC_CHILD);
3145                         rc = mdo_ref_add(env, mdd_tobj, handle);
3146                         mdd_write_unlock(env, mdd_tobj);
3147                         if (rc)
3148                                 GOTO(next_put, rc);
3149
3150                         mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
3151                         mdo_ref_del(env, mdd_sobj, handle);
3152                         mdd_write_unlock(env, mdd_sobj);
3153                 }
3154 next_put:
3155                 mdd_object_put(env, pobj);
3156                 if (rc != 0)
3157                         break;
3158         }
3159
3160         RETURN(rc);
3161 }
3162
3163 static int mdd_migrate_xattrs(const struct lu_env *env,
3164                               struct mdd_object *mdd_sobj,
3165                               struct mdd_object *mdd_tobj)
3166 {
3167         struct mdd_thread_info  *info = mdd_env_info(env);
3168         struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3169         char                    *xname;
3170         struct thandle          *handle;
3171         struct lu_buf           xbuf;
3172         int                     xlen;
3173         int                     rem;
3174         int                     xsize;
3175         int                     list_xsize;
3176         struct lu_buf           list_xbuf;
3177         int                     rc;
3178
3179         /* retrieve xattr list from the old object */
3180         list_xsize = mdo_xattr_list(env, mdd_sobj, &LU_BUF_NULL);
3181         if (list_xsize == -ENODATA)
3182                 return 0;
3183
3184         if (list_xsize < 0)
3185                 return list_xsize;
3186
3187         lu_buf_check_and_alloc(&info->mti_big_buf, list_xsize);
3188         if (info->mti_big_buf.lb_buf == NULL)
3189                 return -ENOMEM;
3190
3191         list_xbuf.lb_buf = info->mti_big_buf.lb_buf;
3192         list_xbuf.lb_len = list_xsize;
3193         rc = mdo_xattr_list(env, mdd_sobj, &list_xbuf);
3194         if (rc < 0)
3195                 return rc;
3196         rc = 0;
3197         rem = list_xsize;
3198         xname = list_xbuf.lb_buf;
3199         while (rem > 0) {
3200                 xlen = strnlen(xname, rem - 1) + 1;
3201                 if (strcmp(XATTR_NAME_LINK, xname) == 0 ||
3202                     strcmp(XATTR_NAME_LMA, xname) == 0 ||
3203                     strcmp(XATTR_NAME_LMV, xname) == 0)
3204                         goto next;
3205
3206                 /* For directory, if there are default layout, migrate here */
3207                 if (strcmp(XATTR_NAME_LOV, xname) == 0 &&
3208                     !S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu)))
3209                         goto next;
3210
3211                 xsize = mdo_xattr_get(env, mdd_sobj, &LU_BUF_NULL, xname);
3212                 if (xsize == -ENODATA)
3213                         goto next;
3214                 if (xsize < 0)
3215                         GOTO(out, rc);
3216
3217                 lu_buf_check_and_alloc(&info->mti_link_buf, xsize);
3218                 if (info->mti_link_buf.lb_buf == NULL)
3219                         GOTO(out, rc = -ENOMEM);
3220
3221                 xbuf.lb_len = xsize;
3222                 xbuf.lb_buf = info->mti_link_buf.lb_buf;
3223                 rc = mdo_xattr_get(env, mdd_sobj, &xbuf, xname);
3224                 if (rc == -ENODATA)
3225                         goto next;
3226                 if (rc < 0)
3227                         GOTO(out, rc);
3228
3229                 handle = mdd_trans_create(env, mdd);
3230                 if (IS_ERR(handle))
3231                         GOTO(out, rc = PTR_ERR(handle));
3232
3233                 rc = mdo_declare_xattr_set(env, mdd_tobj, &xbuf, xname, 0,
3234                                            handle);
3235                 if (rc != 0)
3236                         GOTO(stop_trans, rc);
3237                 /* Note: this transaction is part of migration, and it is not
3238                  * the last step of migration, so we set th_local = 1 to avoid
3239                  * update last rcvd for this transaction */
3240                 handle->th_local = 1;
3241                 rc = mdd_trans_start(env, mdd, handle);
3242                 if (rc != 0)
3243                         GOTO(stop_trans, rc);
3244
3245                 rc = mdo_xattr_set(env, mdd_tobj, &xbuf, xname, 0, handle);
3246                 if (rc == -EEXIST)
3247                         GOTO(stop_trans, rc = 0);
3248
3249                 if (rc != 0)
3250                         GOTO(stop_trans, rc);
3251 stop_trans:
3252                 mdd_trans_stop(env, mdd, rc, handle);
3253                 if (rc != 0)
3254                         GOTO(out, rc);
3255 next:
3256                 rem -= xlen;
3257                 memmove(xname, xname + xlen, rem);
3258         }
3259 out:
3260         return rc;
3261 }
3262
3263 static int mdd_declare_migrate_create(const struct lu_env *env,
3264                                       struct mdd_object *mdd_pobj,
3265                                       struct mdd_object *mdd_sobj,
3266                                       struct mdd_object *mdd_tobj,
3267                                       struct md_op_spec *spec,
3268                                       struct lu_attr *la,
3269                                       union lmv_mds_md *mgr_ea,
3270                                       struct linkea_data *ldata,
3271                                       struct thandle *handle)
3272 {
3273         struct lu_attr          *la_flag = MDD_ENV_VAR(env, la_for_fix);
3274         const struct lu_buf     *buf;
3275         int                     rc;
3276         int                     mgr_easize;
3277
3278         rc = mdd_declare_object_create_internal(env, mdd_pobj, mdd_tobj, la,
3279                                                 handle, spec, NULL);
3280         if (rc != 0)
3281                 return rc;
3282
3283         rc = mdd_declare_object_initialize(env, mdd_pobj, mdd_tobj, la,
3284                                            handle);
3285         if (rc != 0)
3286                 return rc;
3287
3288         if (S_ISLNK(la->la_mode)) {
3289                 const char *target_name = spec->u.sp_symname;
3290                 int sym_len = strlen(target_name);
3291                 const struct lu_buf *buf;
3292
3293                 buf = mdd_buf_get_const(env, target_name, sym_len);
3294                 rc = dt_declare_record_write(env, mdd_object_child(mdd_tobj),
3295                                              buf, 0, handle);
3296                 if (rc != 0)
3297                         return rc;
3298         } else if (S_ISDIR(la->la_mode)) {
3299                 rc = mdd_declare_links_add(env, mdd_tobj, handle, ldata,
3300                                            MLAO_IGNORE);
3301                 if (rc != 0)
3302                         return rc;
3303         }
3304
3305         if (spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
3306                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
3307                                         spec->u.sp_ea.eadatalen);
3308                 rc = mdo_declare_xattr_set(env, mdd_tobj, buf, XATTR_NAME_LOV,
3309                                            0, handle);
3310                 if (rc)
3311                         return rc;
3312         }
3313
3314         mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3315         buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3316         rc = mdo_declare_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV,
3317                                    0, handle);
3318         if (rc)
3319                 return rc;
3320
3321         la_flag->la_valid = LA_FLAGS;
3322         la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3323         rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3324
3325         return rc;
3326 }
3327
3328 static int mdd_migrate_create(const struct lu_env *env,
3329                               struct mdd_object *mdd_pobj,
3330                               struct mdd_object *mdd_sobj,
3331                               struct mdd_object *mdd_tobj,
3332                               const struct lu_name *lname,
3333                               struct lu_attr *la)
3334 {
3335         struct mdd_thread_info  *info = mdd_env_info(env);
3336         struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3337         struct md_op_spec       *spec = &info->mti_spec;
3338         struct lu_buf           lmm_buf = { NULL };
3339         struct lu_buf           link_buf = { NULL };
3340         const struct lu_buf     *buf;
3341         struct thandle          *handle;
3342         struct lmv_mds_md_v1    *mgr_ea;
3343         struct lu_attr          *la_flag = MDD_ENV_VAR(env, la_for_fix);
3344         struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
3345         int                     mgr_easize;
3346         struct linkea_data      *ldata = &mdd_env_info(env)->mti_link_data;
3347         int                     rc;
3348         ENTRY;
3349
3350         /* prepare spec for create */
3351         memset(spec, 0, sizeof(*spec));
3352         spec->sp_cr_lookup = 0;
3353         spec->sp_feat = &dt_directory_features;
3354         if (S_ISLNK(la->la_mode)) {
3355                 buf = lu_buf_check_and_alloc(
3356                                 &mdd_env_info(env)->mti_big_buf,
3357                                 la->la_size + 1);
3358                 link_buf = *buf;
3359                 link_buf.lb_len = la->la_size + 1;
3360                 memset(link_buf.lb_buf, 0, link_buf.lb_len);
3361                 rc = mdd_readlink(env, &mdd_sobj->mod_obj, &link_buf);
3362                 if (rc <= 0) {
3363                         rc = rc != 0 ? rc : -EFAULT;
3364                         CERROR("%s: "DFID" readlink failed: rc = %d\n",
3365                                mdd2obd_dev(mdd)->obd_name,
3366                                PFID(mdd_object_fid(mdd_sobj)), rc);
3367                         RETURN(rc);
3368                 }
3369                 spec->u.sp_symname = link_buf.lb_buf;
3370         } else if S_ISREG(la->la_mode) {
3371                 /* retrieve lov of the old object */
3372                 rc = mdd_get_lov_ea(env, mdd_sobj, &lmm_buf);
3373                 if (rc != 0 && rc != -ENODATA)
3374                         RETURN(rc);
3375                 if (lmm_buf.lb_buf != NULL && lmm_buf.lb_len != 0) {
3376                         spec->u.sp_ea.eadata = lmm_buf.lb_buf;
3377                         spec->u.sp_ea.eadatalen = lmm_buf.lb_len;
3378                         spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
3379                 }
3380         } else if (S_ISDIR(la->la_mode)) {
3381                 rc = mdd_links_read(env, mdd_sobj, ldata);
3382                 if (rc < 0 && rc != -ENODATA)
3383                         RETURN(rc);
3384         }
3385
3386         mgr_ea = (struct lmv_mds_md_v1 *)info->mti_xattr_buf;
3387         memset(mgr_ea, 0, sizeof(*mgr_ea));
3388         mgr_ea->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
3389         mgr_ea->lmv_stripe_count = cpu_to_le32(2);
3390         mgr_ea->lmv_master_mdt_index = mdd_seq_site(mdd)->ss_node_id;
3391         mgr_ea->lmv_hash_type = cpu_to_le32(LMV_HASH_FLAG_MIGRATION);
3392         fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[0], mdd_object_fid(mdd_sobj));
3393         fid_cpu_to_le(&mgr_ea->lmv_stripe_fids[1], mdd_object_fid(mdd_tobj));
3394
3395         mdd_object_make_hint(env, mdd_pobj, mdd_tobj, la, spec, hint);
3396
3397         handle = mdd_trans_create(env, mdd);
3398         if (IS_ERR(handle))
3399                 GOTO(out_free, rc = PTR_ERR(handle));
3400
3401         /* Note: this transaction is part of migration, and it is not
3402          * the last step of migration, so we set th_local = 1 to avoid
3403          * update last rcvd for this transaction */
3404         handle->th_local = 1;
3405         rc = mdd_declare_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
3406                                         spec, la,
3407                                         (union lmv_mds_md *)info->mti_xattr_buf,
3408                                         ldata, handle);
3409         if (rc != 0)
3410                 GOTO(stop_trans, rc);
3411
3412         rc = mdd_trans_start(env, mdd, handle);
3413         if (rc != 0)
3414                 GOTO(stop_trans, rc);
3415
3416         /* create the target object */
3417         rc = mdd_object_create(env, mdd_pobj, mdd_tobj, la, spec, NULL, NULL,
3418                                hint, handle);
3419         if (rc != 0)
3420                 GOTO(stop_trans, rc);
3421
3422         if (S_ISDIR(la->la_mode)) {
3423                 rc = mdd_links_write(env, mdd_tobj, ldata, handle);
3424                 if (rc != 0)
3425                         GOTO(stop_trans, rc);
3426         }
3427
3428         /* Set MIGRATE EA on the source inode, so once the migration needs
3429          * to be re-done during failover, the re-do process can locate the
3430          * target object which is already being created. */
3431         mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3432         buf = mdd_buf_get_const(env, mgr_ea, mgr_easize);
3433         rc = mdo_xattr_set(env, mdd_sobj, buf, XATTR_NAME_LMV, 0, handle);
3434         if (rc != 0)
3435                 GOTO(stop_trans, rc);
3436
3437         /* Set immutable flag, so any modification is disabled until
3438          * the migration is done. Once the migration is interrupted,
3439          * if the resume process find the migrating object has both
3440          * IMMUTALBE flag and MIGRATE EA, it need to clear IMMUTABLE
3441          * flag and approve the migration */
3442         la_flag->la_valid = LA_FLAGS;
3443         la_flag->la_flags = la->la_flags | LUSTRE_IMMUTABLE_FL;
3444         rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3445 stop_trans:
3446         if (handle != NULL)
3447                 mdd_trans_stop(env, mdd, rc, handle);
3448 out_free:
3449         if (lmm_buf.lb_buf != NULL)
3450                 OBD_FREE(lmm_buf.lb_buf, lmm_buf.lb_len);
3451         RETURN(rc);
3452 }
3453
3454 static int mdd_migrate_entries(const struct lu_env *env,
3455                                struct mdd_object *mdd_sobj,
3456                                struct mdd_object *mdd_tobj)
3457 {
3458         struct dt_object        *next = mdd_object_child(mdd_sobj);
3459         struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3460         struct dt_object        *dt_tobj = mdd_object_child(mdd_tobj);
3461         struct thandle          *handle;
3462         struct dt_it            *it;
3463         const struct dt_it_ops  *iops;
3464         int                      rc;
3465         int                      result;
3466         struct lu_dirent        *ent;
3467         ENTRY;
3468
3469         OBD_ALLOC(ent, NAME_MAX + sizeof(*ent) + 1);
3470         if (ent == NULL)
3471                 RETURN(-ENOMEM);
3472
3473         if (!dt_try_as_dir(env, next))
3474                 GOTO(out_ent, rc = -ENOTDIR);
3475         /*
3476          * iterate directories
3477          */
3478         iops = &next->do_index_ops->dio_it;
3479         it = iops->init(env, next, LUDA_FID | LUDA_TYPE);
3480         if (IS_ERR(it))
3481                 GOTO(out_ent, rc = PTR_ERR(it));
3482
3483         rc = iops->load(env, it, 0);
3484         if (rc == 0)
3485                 rc = iops->next(env, it);
3486         else if (rc > 0)
3487                 rc = 0;
3488         /*
3489          * At this point and across for-loop:
3490          *
3491          *  rc == 0 -> ok, proceed.
3492          *  rc >  0 -> end of directory.
3493          *  rc <  0 -> error.
3494          */
3495         do {
3496                 struct mdd_object       *child;
3497                 char                    *name = mdd_env_info(env)->mti_key;
3498                 int                     len;
3499                 int                     recsize;
3500                 int                     is_dir;
3501                 bool                    target_exist = false;
3502
3503                 len = iops->key_size(env, it);
3504                 if (len == 0)
3505                         goto next;
3506
3507                 result = iops->rec(env, it, (struct dt_rec *)ent,
3508                                    LUDA_FID | LUDA_TYPE);
3509                 if (result == -ESTALE)
3510                         goto next;
3511                 if (result != 0) {
3512                         rc = result;
3513                         goto out;
3514                 }
3515
3516                 fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
3517                 recsize = le16_to_cpu(ent->lde_reclen);
3518
3519                 /* Insert new fid with target name into target dir */
3520                 if ((ent->lde_namelen == 1 && ent->lde_name[0] == '.') ||
3521                     (ent->lde_namelen == 2 && ent->lde_name[0] == '.' &&
3522                      ent->lde_name[1] == '.'))
3523                         goto next;
3524
3525                 child = mdd_object_find(env, mdd, &ent->lde_fid);
3526                 if (IS_ERR(child))
3527                         GOTO(out, rc = PTR_ERR(child));
3528
3529                 is_dir = S_ISDIR(mdd_object_type(child));
3530
3531                 snprintf(name, ent->lde_namelen + 1, "%s", ent->lde_name);
3532
3533                 /* Check whether the name has been inserted to the target */
3534                 if (dt_try_as_dir(env, dt_tobj)) {
3535                         struct lu_fid *fid = &mdd_env_info(env)->mti_fid2;
3536
3537                         rc = dt_lookup(env, dt_tobj, (struct dt_rec *)fid,
3538                                        (struct dt_key *)name);
3539                         if (unlikely(rc == 0))
3540                                 target_exist = true;
3541                 }
3542
3543                 handle = mdd_trans_create(env, mdd);
3544                 if (IS_ERR(handle))
3545                         GOTO(out, rc = PTR_ERR(handle));
3546
3547                 /* Note: this transaction is part of migration, and it is not
3548                  * the last step of migration, so we set th_local = 1 to avoid
3549                  * updating last rcvd for this transaction */
3550                 handle->th_local = 1;
3551                 if (likely(!target_exist)) {
3552                         rc = mdo_declare_index_insert(env, mdd_tobj,
3553                                                       &ent->lde_fid,
3554                                                       mdd_object_type(child),
3555                                                       name, handle);
3556                         if (rc != 0)
3557                                 GOTO(out_put, rc);
3558
3559                         if (is_dir) {
3560                                 rc = mdo_declare_ref_add(env, mdd_tobj, handle);
3561                                 if (rc != 0)
3562                                         GOTO(out_put, rc);
3563                         }
3564                 }
3565
3566                 rc = mdo_declare_index_delete(env, mdd_sobj, name, handle);
3567                 if (rc != 0)
3568                         GOTO(out_put, rc);
3569
3570                 if (is_dir) {
3571                         rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3572                         if (rc != 0)
3573                                 GOTO(out_put, rc);
3574
3575                         /* Update .. for child */
3576                         rc = mdo_declare_index_delete(env, child, dotdot,
3577                                                       handle);
3578                         if (rc != 0)
3579                                 GOTO(out_put, rc);
3580
3581                         rc = mdo_declare_index_insert(env, child,
3582                                                       mdd_object_fid(mdd_tobj),
3583                                                       S_IFDIR, dotdot, handle);
3584                         if (rc != 0)
3585                                 GOTO(out_put, rc);
3586                 }
3587
3588                 rc = mdd_linkea_declare_update_child(env, mdd_tobj,
3589                                                      child, name,
3590                                                      strlen(name),
3591                                                      handle);
3592                 if (rc != 0)
3593                         GOTO(out_put, rc);
3594
3595                 rc = mdd_trans_start(env, mdd, handle);
3596                 if (rc != 0) {
3597                         CERROR("%s: transaction start failed: rc = %d\n",
3598                                mdd2obd_dev(mdd)->obd_name, rc);
3599                         GOTO(out_put, rc);
3600                 }
3601
3602                 if (likely(!target_exist)) {
3603                         rc = __mdd_index_insert(env, mdd_tobj, &ent->lde_fid,
3604                                                 mdd_object_type(child),
3605                                                 name, handle);
3606                         if (rc != 0)
3607                                 GOTO(out_put, rc);
3608                 }
3609
3610                 rc = __mdd_index_delete(env, mdd_sobj, name, is_dir, handle);
3611                 if (rc != 0)
3612                         GOTO(out_put, rc);
3613
3614                 if (is_dir) {
3615                         rc = __mdd_index_delete_only(env, child, dotdot,
3616                                                      handle);
3617                         if (rc != 0)
3618                                 GOTO(out_put, rc);
3619
3620                         rc = __mdd_index_insert_only(env, child,
3621                                          mdd_object_fid(mdd_tobj), S_IFDIR,
3622                                          dotdot, handle);
3623                         if (rc != 0)
3624                                 GOTO(out_put, rc);
3625                 }
3626
3627                 rc = mdd_linkea_update_child(env, mdd_tobj, child, name,
3628                                              strlen(name), handle);
3629
3630 out_put:
3631                 mdd_object_put(env, child);
3632                 mdd_trans_stop(env, mdd, rc, handle);
3633                 if (rc != 0)
3634                         GOTO(out, rc);
3635 next:
3636                 result = iops->next(env, it);
3637                 if (OBD_FAIL_CHECK(OBD_FAIL_MIGRATE_ENTRIES))
3638                         GOTO(out, rc = -EINTR);
3639
3640                 if (result == -ESTALE)
3641                         goto next;
3642         } while (result == 0);
3643 out:
3644         iops->put(env, it);
3645         iops->fini(env, it);
3646 out_ent:
3647         OBD_FREE(ent, NAME_MAX + sizeof(*ent) + 1);
3648         RETURN(rc);
3649 }
3650
3651 static int mdd_declare_update_linkea(const struct lu_env *env,
3652                                      struct mdd_object *mdd_pobj,
3653                                      struct mdd_object *mdd_sobj,
3654                                      struct mdd_object *mdd_tobj,
3655                                      struct thandle *handle,
3656                                      const struct lu_name *child_name)
3657 {
3658         return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3659                                           child_name, handle, 1);
3660 }
3661
3662 static int mdd_update_linkea(const struct lu_env *env,
3663                              struct mdd_object *mdd_pobj,
3664                              struct mdd_object *mdd_sobj,
3665                              struct mdd_object *mdd_tobj,
3666                              struct thandle *handle,
3667                              const struct lu_name *child_name)
3668 {
3669         return mdd_update_linkea_internal(env, mdd_pobj, mdd_sobj, mdd_tobj,
3670                                           child_name, handle, 0);
3671 }
3672
3673 static int mdd_declare_migrate_update_name(const struct lu_env *env,
3674                                            struct mdd_object *mdd_pobj,
3675                                            struct mdd_object *mdd_sobj,
3676                                            struct mdd_object *mdd_tobj,
3677                                            const struct lu_name *lname,
3678                                            struct lu_attr *la,
3679                                            struct lu_attr *parent_la,
3680                                            struct thandle *handle)
3681 {
3682         struct lu_attr  *la_flag = MDD_ENV_VAR(env, tattr);
3683         int             rc;
3684
3685         /* Revert IMMUTABLE flag */
3686         la_flag->la_valid = LA_FLAGS;
3687         la_flag->la_flags = la->la_flags & ~LUSTRE_IMMUTABLE_FL;
3688         rc = mdo_declare_attr_set(env, mdd_sobj, la_flag, handle);
3689         if (rc != 0)
3690                 return rc;
3691
3692         /* delete entry from source dir */
3693         rc = mdo_declare_index_delete(env, mdd_pobj, lname->ln_name, handle);
3694         if (rc != 0)
3695                 return rc;
3696
3697         rc = mdd_declare_update_linkea(env, mdd_pobj, mdd_sobj,
3698                                        mdd_tobj, handle, lname);
3699         if (rc != 0)
3700                 return rc;
3701
3702         if (S_ISREG(mdd_object_type(mdd_sobj))) {
3703                 rc = mdo_declare_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3704                                            handle);
3705                 if (rc != 0)
3706                         return rc;
3707         }
3708
3709         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3710                 rc = mdo_declare_ref_del(env, mdd_pobj, handle);
3711                 if (rc != 0)
3712                         return rc;
3713         }
3714
3715         /* new name */
3716         rc = mdo_declare_index_insert(env, mdd_pobj, mdo2fid(mdd_tobj),
3717                                       mdd_object_type(mdd_tobj),
3718                                       lname->ln_name, handle);
3719         if (rc != 0)
3720                 return rc;
3721
3722         rc = mdd_declare_links_add(env, mdd_tobj, handle, NULL, MLAO_IGNORE);
3723         if (rc != 0)
3724                 return rc;
3725
3726         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3727                 rc = mdo_declare_ref_add(env, mdd_pobj, handle);
3728                 if (rc != 0)
3729                         return rc;
3730         }
3731
3732         /* delete old object */
3733         rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3734         if (rc != 0)
3735                 return rc;
3736
3737         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
3738                 /* delete old object */
3739                 rc = mdo_declare_ref_del(env, mdd_sobj, handle);
3740                 if (rc != 0)
3741                         return rc;
3742                 /* set nlink to 0 */
3743                 rc = mdo_declare_attr_set(env, mdd_sobj, la, handle);
3744                 if (rc != 0)
3745                         return rc;
3746         }
3747
3748         rc = mdd_declare_finish_unlink(env, mdd_sobj, handle);
3749         if (rc)
3750                 return rc;
3751
3752         rc = mdo_declare_attr_set(env, mdd_pobj, parent_la, handle);
3753
3754         return rc;
3755 }
3756
3757 static int mdd_migrate_update_name(const struct lu_env *env,
3758                                    struct mdd_object *mdd_pobj,
3759                                    struct mdd_object *mdd_sobj,
3760                                    struct mdd_object *mdd_tobj,
3761                                    const struct lu_name *lname,
3762                                    struct md_attr *ma)
3763 {
3764         struct lu_attr          *p_la = MDD_ENV_VAR(env, la_for_fix);
3765         struct lu_attr          *so_attr = MDD_ENV_VAR(env, cattr);
3766         struct lu_attr          *la_flag = MDD_ENV_VAR(env, tattr);
3767         struct mdd_device       *mdd = mdo2mdd(&mdd_sobj->mod_obj);
3768         struct thandle          *handle;
3769         int                     is_dir = S_ISDIR(mdd_object_type(mdd_sobj));
3770         const char              *name = lname->ln_name;
3771         int                     rc;
3772         ENTRY;
3773
3774         /* update time for parent */
3775         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
3776         p_la->la_ctime = p_la->la_mtime = ma->ma_attr.la_ctime;
3777         p_la->la_valid = LA_CTIME;
3778
3779         rc = mdd_la_get(env, mdd_sobj, so_attr);
3780         if (rc != 0)
3781                 RETURN(rc);
3782
3783         handle = mdd_trans_create(env, mdd);
3784         if (IS_ERR(handle))
3785                 RETURN(PTR_ERR(handle));
3786
3787         rc = mdd_declare_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj,
3788                                              lname, so_attr, p_la, handle);
3789         if (rc != 0) {
3790                 /* If the migration can not be fit in one transaction, just
3791                  * leave it in the original MDT */
3792                 if (rc == -E2BIG)
3793                         GOTO(stop_trans, rc = 0);
3794                 else
3795                         GOTO(stop_trans, rc);
3796         }
3797
3798         CDEBUG(D_INFO, "%s: update "DFID"/"DFID" with %s:"DFID"\n",
3799                mdd2obd_dev(mdd)->obd_name, PFID(mdd_object_fid(mdd_pobj)),
3800                PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
3801                PFID(mdd_object_fid(mdd_tobj)));
3802
3803         rc = mdd_trans_start(env, mdd, handle);
3804         if (rc != 0)
3805                 GOTO(stop_trans, rc);
3806
3807         /* Revert IMMUTABLE flag */
3808         la_flag->la_valid = LA_FLAGS;
3809         la_flag->la_flags = so_attr->la_flags & ~LUSTRE_IMMUTABLE_FL;
3810         rc = mdo_attr_set(env, mdd_sobj, la_flag, handle);
3811         if (rc != 0)
3812                 GOTO(stop_trans, rc);
3813
3814         /* Remove source name from source directory */
3815         rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle);
3816         if (rc != 0)
3817                 GOTO(stop_trans, rc);
3818
3819         rc = mdd_update_linkea(env, mdd_pobj, mdd_sobj, mdd_tobj,
3820                                handle, lname);
3821         if (rc != 0)
3822                 GOTO(stop_trans, rc);
3823
3824         if (S_ISREG(so_attr->la_mode)) {
3825                 if (so_attr->la_nlink == 1) {
3826                         rc = mdo_xattr_del(env, mdd_sobj, XATTR_NAME_LOV,
3827                                            handle);
3828                         if (rc != 0 && rc != -ENODATA)
3829                                 GOTO(stop_trans, rc);
3830                 }
3831         }
3832
3833         /* Insert new fid with target name into target dir */
3834         rc = __mdd_index_insert(env, mdd_pobj, mdd_object_fid(mdd_tobj),
3835                                 mdd_object_type(mdd_tobj), name, handle);
3836         if (rc != 0)
3837                 GOTO(stop_trans, rc);
3838
3839         rc = mdd_links_add(env, mdd_tobj, mdo2fid(mdd_pobj), lname, handle,
3840                            NULL, 1);
3841         if (rc != 0)
3842                 GOTO(stop_trans, rc);
3843
3844         mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
3845         mdo_ref_del(env, mdd_sobj, handle);
3846         if (is_dir)
3847                 mdo_ref_del(env, mdd_sobj, handle);
3848
3849         /* Get the attr again after ref_del */
3850         rc = mdd_la_get(env, mdd_sobj, so_attr);
3851         if (rc != 0)
3852                 GOTO(out_unlock, rc);
3853
3854         ma->ma_attr = *so_attr;
3855         ma->ma_valid |= MA_INODE;
3856         rc = mdd_finish_unlink(env, mdd_sobj, ma, mdd_pobj, lname, handle);
3857         if (rc != 0)
3858                 GOTO(out_unlock, rc);
3859
3860         rc = mdd_attr_set_internal(env, mdd_pobj, p_la, handle, 0);
3861         if (rc != 0)
3862                 GOTO(out_unlock, rc);
3863
3864 out_unlock:
3865         mdd_write_unlock(env, mdd_sobj);
3866
3867 stop_trans:
3868         mdd_trans_stop(env, mdd, rc, handle);
3869
3870         RETURN(rc);
3871 }
3872
3873 /**
3874  * Check whether we should migrate the file/dir
3875  * return val
3876  *      < 0  permission check failed or other error.
3877  *      = 0  the file can be migrated.
3878  *      > 0  the file does not need to be migrated, mostly
3879  *           for multiple link file
3880  **/
3881 static int mdd_migrate_sanity_check(const struct lu_env *env,
3882                                     struct mdd_object *pobj,
3883                                     const struct lu_attr *pattr,
3884                                     struct mdd_object *sobj,
3885                                     struct lu_attr *sattr)
3886 {
3887         struct mdd_thread_info  *info = mdd_env_info(env);
3888         struct linkea_data      *ldata = &info->mti_link_data;
3889         int                     mgr_easize;
3890         struct lu_buf           *mgr_buf;
3891         int                     count;
3892         int                     rc;
3893
3894         ENTRY;
3895
3896         mgr_easize = lmv_mds_md_size(2, LMV_MAGIC_V1);
3897         mgr_buf = lu_buf_check_and_alloc(&info->mti_big_buf, mgr_easize);
3898         if (mgr_buf->lb_buf == NULL)
3899                 RETURN(-ENOMEM);
3900
3901         rc = mdo_xattr_get(env, sobj, mgr_buf, XATTR_NAME_LMV);
3902         if (rc > 0) {
3903                 union lmv_mds_md *lmm = mgr_buf->lb_buf;
3904
3905                 /* If the object has migrateEA, it means IMMUTE flag
3906                  * is being set by previous migration process, so it
3907                  * needs to override the IMMUTE flag, otherwise the
3908                  * following sanity check will fail */
3909                 if (le32_to_cpu(lmm->lmv_md_v1.lmv_hash_type) &
3910                                                 LMV_HASH_FLAG_MIGRATION) {
3911                         struct mdd_device *mdd = mdo2mdd(&sobj->mod_obj);
3912
3913                         sattr->la_flags &= ~LUSTRE_IMMUTABLE_FL;
3914                         CDEBUG(D_HA, "%s: "DFID" override IMMUTE FLAG\n",
3915                                mdd2obd_dev(mdd)->obd_name,
3916                                PFID(mdd_object_fid(sobj)));
3917                 }
3918         }
3919
3920         rc = mdd_rename_sanity_check(env, pobj, pattr, pobj, pattr,
3921                                      sobj, sattr, NULL, NULL);
3922         if (rc != 0)
3923                 RETURN(rc);
3924
3925         /* Then it will check if the file should be migrated. If the file
3926          * has mulitple links, we only need migrate the file if all of its
3927          * entries has been migrated to the remote MDT */
3928         if (!S_ISREG(sattr->la_mode) || sattr->la_nlink < 2)
3929                 RETURN(0);
3930
3931         rc = mdd_links_read(env, sobj, ldata);
3932         if (rc != 0) {
3933                 /* For multiple links files, if there are no linkEA data at all,
3934                  * means the file might be created before linkEA is enabled, and
3935                  * all all of its links should not be migrated yet, otherwise
3936                  * it should have some linkEA there */
3937                 if (rc == -ENOENT || rc == -ENODATA)
3938                         RETURN(1);
3939                 RETURN(rc);
3940         }
3941
3942         /* If it is mulitple links file, we need update the name entry for
3943          * all parent */
3944         LASSERT(ldata->ld_leh != NULL);
3945         ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
3946         for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
3947                 struct mdd_device       *mdd = mdo2mdd(&sobj->mod_obj);
3948                 struct mdd_object       *lpobj;
3949                 struct lu_name          lname;
3950                 struct lu_fid           fid;
3951
3952                 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
3953                                     &lname, &fid);
3954                 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
3955                                                          ldata->ld_reclen);
3956                 lpobj = mdd_object_find(env, mdd, &fid);
3957                 if (IS_ERR(lpobj)) {
3958                         CWARN("%s: cannot find obj "DFID": rc = %ld\n",
3959                               mdd2obd_dev(mdd)->obd_name, PFID(&fid),
3960                               PTR_ERR(lpobj));
3961                         continue;
3962                 }
3963
3964                 if (!mdd_object_exists(lpobj) || mdd_object_remote(lpobj)) {
3965                         CDEBUG(D_INFO, DFID"%.*s: is on remote MDT.\n",
3966                                PFID(&fid), lname.ln_namelen, lname.ln_name);
3967                         mdd_object_put(env, lpobj);
3968                         continue;
3969                 }
3970
3971                 CDEBUG(D_INFO, DFID"still has local entry %.*s "DFID"\n",
3972                        PFID(mdd_object_fid(sobj)), lname.ln_namelen,
3973                        lname.ln_name, PFID(&fid));
3974                 mdd_object_put(env, lpobj);
3975                 rc = 1;
3976                 break;
3977         }
3978
3979         RETURN(rc);
3980 }
3981
3982 static int mdd_migrate(const struct lu_env *env, struct md_object *pobj,
3983                        struct md_object *sobj, const struct lu_name *lname,
3984                        struct md_object *tobj, struct md_attr *ma)
3985 {
3986         struct mdd_object       *mdd_pobj = md2mdd_obj(pobj);
3987         struct mdd_device       *mdd = mdo2mdd(pobj);
3988         struct mdd_object       *mdd_sobj = md2mdd_obj(sobj);
3989         struct mdd_object       *mdd_tobj = NULL;
3990         struct lu_attr          *so_attr = MDD_ENV_VAR(env, cattr);
3991         struct lu_attr          *pattr = MDD_ENV_VAR(env, pattr);
3992         int                     rc;
3993
3994         ENTRY;
3995         /* If the file will being migrated, it will check whether
3996          * the file is being opened by someone else right now */
3997         mdd_read_lock(env, mdd_sobj, MOR_SRC_CHILD);
3998         if (mdd_sobj->mod_count >= 1) {
3999                 CERROR("%s: "DFID"%s is already opened count %d: rc = %d\n",
4000                        mdd2obd_dev(mdd)->obd_name,
4001                        PFID(mdd_object_fid(mdd_sobj)), lname->ln_name,
4002                        mdd_sobj->mod_count, -EBUSY);
4003                 mdd_read_unlock(env, mdd_sobj);
4004                 GOTO(put, rc = -EBUSY);
4005         }
4006         mdd_read_unlock(env, mdd_sobj);
4007
4008         rc = mdd_la_get(env, mdd_sobj, so_attr);
4009         if (rc != 0)
4010                 GOTO(put, rc);
4011
4012         rc = mdd_la_get(env, mdd_pobj, pattr);
4013         if (rc != 0)
4014                 GOTO(put, rc);
4015
4016         rc = mdd_migrate_sanity_check(env, mdd_pobj, pattr, mdd_sobj, so_attr);
4017         if (rc != 0) {
4018                 if (rc > 0)
4019                         rc = 0;
4020                 GOTO(put, rc);
4021         }
4022
4023         /* Sigh, it is impossible to finish all of migration in a single
4024          * transaction, for example migrating big directory entries to the
4025          * new MDT, it needs insert all of name entries of children in the
4026          * new directory.
4027          *
4028          * So migration will be done in multiple steps and transactions.
4029          *
4030          * 1. create an orphan object on the remote MDT in one transaction.
4031          * 2. migrate extend attributes to the new target file/directory.
4032          * 3. For directory, migrate the entries to the new MDT and update
4033          * linkEA of each children. Because we can not migrate all entries
4034          * in a single transaction, so the migrating directory will become
4035          * a striped directory during migration, so once the process is
4036          * interrupted, the directory is still accessible. (During lookup,
4037          * client will locate the name by searching both original and target
4038          * object).
4039          * 4. Finally, update the name/FID to point to the new file/directory
4040          * in a separate transaction.
4041          */
4042
4043         /* step 1: Check whether the orphan object has been created, and create
4044          * orphan object on the remote MDT if needed */
4045         mdd_tobj = md2mdd_obj(tobj);
4046         if (!mdd_object_exists(mdd_tobj)) {
4047                 rc = mdd_migrate_create(env, mdd_pobj, mdd_sobj, mdd_tobj,
4048                                         lname, so_attr);
4049                 if (rc != 0)
4050                         GOTO(put, rc);
4051         }
4052
4053         /* step 2: migrate xattr */
4054         rc = mdd_migrate_xattrs(env, mdd_sobj, mdd_tobj);
4055         if (rc != 0)
4056                 GOTO(put, rc);
4057
4058         /* step 3: migrate name entries to the orphan object */
4059         if (S_ISDIR(lu_object_attr(&mdd_sobj->mod_obj.mo_lu))) {
4060                 rc = mdd_migrate_entries(env, mdd_sobj, mdd_tobj);
4061                 if (rc != 0)
4062                         GOTO(put, rc);
4063                 if (unlikely(OBD_FAIL_CHECK_RESET(OBD_FAIL_MIGRATE_NET_REP,
4064                                                   OBD_FAIL_MDS_REINT_NET_REP)))
4065                         GOTO(put, rc = 0);
4066         } else {
4067                 OBD_FAIL_TIMEOUT(OBD_FAIL_MIGRATE_DELAY, cfs_fail_val);
4068         }
4069
4070         /* step 4: update name entry to the new object */
4071         rc = mdd_migrate_update_name(env, mdd_pobj, mdd_sobj, mdd_tobj, lname,
4072                                      ma);
4073         if (rc != 0)
4074                 GOTO(put, rc);
4075 put:
4076         RETURN(rc);
4077 }
4078
4079 const struct md_dir_operations mdd_dir_ops = {
4080         .mdo_is_subdir     = mdd_is_subdir,
4081         .mdo_lookup        = mdd_lookup,
4082         .mdo_create        = mdd_create,
4083         .mdo_rename        = mdd_rename,
4084         .mdo_link          = mdd_link,
4085         .mdo_unlink        = mdd_unlink,
4086         .mdo_create_data   = mdd_create_data,
4087         .mdo_migrate       = mdd_migrate,
4088 };