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