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