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