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