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