Whamcloud - gitweb
LU-1303 mds: integration lod/osp into the stack
[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         int reclen;
614
615         /* Not recording */
616         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
617                 return 0;
618
619         /* we'll be writing payload + llog header */
620         reclen = sizeof(struct llog_changelog_rec);
621         if (fname)
622                 reclen += fname->ln_namelen;
623         reclen = llog_data_len(reclen);
624
625         return mdd_declare_llog_record(env, mdd, reclen, handle);
626 }
627
628 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
629                                            struct mdd_device *mdd,
630                                            const struct lu_name *tname,
631                                            const struct lu_name *sname,
632                                            struct thandle *handle)
633 {
634         int reclen;
635
636         /* Not recording */
637         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
638                 return 0;
639
640         /* we'll be writing payload + llog header */
641         reclen = sizeof(struct llog_changelog_ext_rec);
642         if (tname)
643                 reclen += tname->ln_namelen;
644         if (sname)
645                 reclen += 1 + sname->ln_namelen;
646         reclen = llog_data_len(reclen);
647
648         return mdd_declare_llog_record(env, mdd, reclen, handle);
649 }
650
651 /** Store a namespace change changelog record
652  * If this fails, we must fail the whole transaction; we don't
653  * want the change to commit without the log entry.
654  * \param target - mdd_object of change
655  * \param parent - parent dir/object
656  * \param tname - target name string
657  * \param handle - transacion handle
658  */
659 static int mdd_changelog_ns_store(const struct lu_env  *env,
660                                   struct mdd_device    *mdd,
661                                   enum changelog_rec_type type,
662                                   unsigned flags,
663                                   struct mdd_object    *target,
664                                   struct mdd_object    *parent,
665                                   const struct lu_name *tname,
666                                   struct thandle *handle)
667 {
668         struct llog_changelog_rec *rec;
669         struct lu_buf *buf;
670         int reclen;
671         int rc;
672         ENTRY;
673
674         /* Not recording */
675         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
676                 RETURN(0);
677         if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
678                 RETURN(0);
679
680         LASSERT(target != NULL);
681         LASSERT(parent != NULL);
682         LASSERT(tname != NULL);
683         LASSERT(handle != NULL);
684
685         reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
686         buf = mdd_buf_alloc(env, reclen);
687         if (buf->lb_buf == NULL)
688                 RETURN(-ENOMEM);
689         rec = (struct llog_changelog_rec *)buf->lb_buf;
690
691         rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
692         rec->cr.cr_type = (__u32)type;
693         rec->cr.cr_tfid = *mdo2fid(target);
694         rec->cr.cr_pfid = *mdo2fid(parent);
695         rec->cr.cr_namelen = tname->ln_namelen;
696         memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
697
698         target->mod_cltime = cfs_time_current_64();
699
700         rc = mdd_changelog_llog_write(mdd, rec, handle);
701         if (rc < 0) {
702                 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
703                         rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
704                         PFID(&rec->cr.cr_pfid));
705                 RETURN(-EFAULT);
706         }
707
708         RETURN(0);
709 }
710
711
712 /** Store a namespace change changelog record
713  * If this fails, we must fail the whole transaction; we don't
714  * want the change to commit without the log entry.
715  * \param target - mdd_object of change
716  * \param tpfid - target parent dir/object fid
717  * \param sfid - source object fid
718  * \param spfid - source parent fid
719  * \param tname - target name string
720  * \param sname - source name string
721  * \param handle - transacion handle
722  */
723 static int mdd_changelog_ext_ns_store(const struct lu_env  *env,
724                                       struct mdd_device    *mdd,
725                                       enum changelog_rec_type type,
726                                       unsigned flags,
727                                       struct mdd_object    *target,
728                                       const struct lu_fid  *tpfid,
729                                       const struct lu_fid  *sfid,
730                                       const struct lu_fid  *spfid,
731                                       const struct lu_name *tname,
732                                       const struct lu_name *sname,
733                                       struct thandle *handle)
734 {
735         struct llog_changelog_ext_rec *rec;
736         struct lu_buf *buf;
737         int reclen;
738         int rc;
739         ENTRY;
740
741         /* Not recording */
742         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
743                 RETURN(0);
744         if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
745                 RETURN(0);
746
747         LASSERT(sfid != NULL);
748         LASSERT(tpfid != NULL);
749         LASSERT(tname != NULL);
750         LASSERT(handle != NULL);
751
752         reclen = sizeof(*rec) + tname->ln_namelen;
753         if (sname != NULL)
754                 reclen += 1 + sname->ln_namelen;
755         reclen = llog_data_len(reclen);
756         buf = mdd_buf_alloc(env, reclen);
757         if (buf->lb_buf == NULL)
758                 RETURN(-ENOMEM);
759         rec = (struct llog_changelog_ext_rec *)buf->lb_buf;
760
761         rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
762         rec->cr.cr_type = (__u32)type;
763         rec->cr.cr_pfid = *tpfid;
764         rec->cr.cr_sfid = *sfid;
765         rec->cr.cr_spfid = *spfid;
766         rec->cr.cr_namelen = tname->ln_namelen;
767         memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
768         if (sname) {
769                 LASSERT(sfid != NULL);
770                 rec->cr.cr_name[tname->ln_namelen] = '\0';
771                 memcpy(rec->cr.cr_name + tname->ln_namelen + 1, sname->ln_name,
772                         sname->ln_namelen);
773                 rec->cr.cr_namelen += 1 + sname->ln_namelen;
774         }
775
776         if (likely(target != NULL)) {
777                 rec->cr.cr_tfid = *mdo2fid(target);
778                 target->mod_cltime = cfs_time_current_64();
779         } else {
780                 fid_zero(&rec->cr.cr_tfid);
781         }
782
783         rc = mdd_changelog_ext_llog_write(mdd, rec, handle);
784         if (rc < 0) {
785                 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
786                         rc, type, tname->ln_name, PFID(sfid), PFID(tpfid));
787                 return -EFAULT;
788         }
789
790         return 0;
791 }
792
793 static int mdd_declare_link(const struct lu_env *env,
794                             struct mdd_device *mdd,
795                             struct mdd_object *p,
796                             struct mdd_object *c,
797                             const struct lu_name *name,
798                             struct thandle *handle)
799 {
800         int rc;
801
802         rc = mdo_declare_index_insert(env, p, mdo2fid(c), name->ln_name,handle);
803         if (rc)
804                 return rc;
805
806         rc = mdo_declare_ref_add(env, c, handle);
807         if (rc)
808                 return rc;
809
810         rc = mdo_declare_attr_set(env, p, NULL, handle);
811         if (rc)
812                 return rc;
813
814         rc = mdo_declare_attr_set(env, c, NULL, handle);
815         if (rc)
816                 return rc;
817
818         rc = mdd_declare_links_add(env, c, handle);
819         if (rc)
820                 return rc;
821
822         rc = mdd_declare_changelog_store(env, mdd, name, handle);
823
824         return rc;
825 }
826
827 static int mdd_link(const struct lu_env *env, struct md_object *tgt_obj,
828                     struct md_object *src_obj, const struct lu_name *lname,
829                     struct md_attr *ma)
830 {
831         const char *name = lname->ln_name;
832         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
833         struct mdd_object *mdd_tobj = md2mdd_obj(tgt_obj);
834         struct mdd_object *mdd_sobj = md2mdd_obj(src_obj);
835         struct mdd_device *mdd = mdo2mdd(src_obj);
836         struct dynlock_handle *dlh;
837         struct thandle *handle;
838         int rc;
839         ENTRY;
840
841         handle = mdd_trans_create(env, mdd);
842         if (IS_ERR(handle))
843                 GOTO(out_pending, rc = PTR_ERR(handle));
844
845         rc = mdd_declare_link(env, mdd, mdd_tobj, mdd_sobj, lname, handle);
846         if (rc)
847                 GOTO(stop, rc);
848
849         rc = mdd_trans_start(env, mdd, handle);
850         if (rc)
851                 GOTO(stop, rc);
852
853         dlh = mdd_pdo_write_lock(env, mdd_tobj, name, MOR_TGT_CHILD);
854         if (dlh == NULL)
855                 GOTO(out_trans, rc = -ENOMEM);
856         mdd_write_lock(env, mdd_sobj, MOR_TGT_CHILD);
857
858         rc = mdd_link_sanity_check(env, mdd_tobj, lname, mdd_sobj);
859         if (rc)
860                 GOTO(out_unlock, rc);
861
862         rc = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
863                                      name, handle,
864                                      mdd_object_capa(env, mdd_tobj));
865         if (rc)
866                 GOTO(out_unlock, rc);
867
868         rc = mdo_ref_add(env, mdd_sobj, handle);
869         if (rc != 0) {
870                 __mdd_index_delete_only(env, mdd_tobj, name, handle,
871                                         mdd_object_capa(env, mdd_tobj));
872                 GOTO(out_unlock, rc);
873         }
874
875         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
876         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
877
878         la->la_valid = LA_CTIME | LA_MTIME;
879         rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
880         if (rc)
881                 GOTO(out_unlock, rc);
882
883         la->la_valid = LA_CTIME;
884         rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
885         if (rc == 0) {
886                 mdd_links_add(env, mdd_sobj,
887                               mdo2fid(mdd_tobj), lname, handle, 0);
888         }
889
890         EXIT;
891 out_unlock:
892         mdd_write_unlock(env, mdd_sobj);
893         mdd_pdo_write_unlock(env, mdd_tobj, dlh);
894 out_trans:
895         if (rc == 0)
896                 rc = mdd_changelog_ns_store(env, mdd, CL_HARDLINK, 0, mdd_sobj,
897                                             mdd_tobj, lname, handle);
898 stop:
899         mdd_trans_stop(env, mdd, rc, handle);
900 out_pending:
901         return rc;
902 }
903
904 int mdd_declare_finish_unlink(const struct lu_env *env,
905                               struct mdd_object *obj,
906                               struct md_attr *ma,
907                               struct thandle *handle)
908 {
909         int rc;
910
911         rc = orph_declare_index_insert(env, obj, handle);
912         if (rc)
913                 return rc;
914
915         return mdo_declare_destroy(env, obj, handle);
916 }
917
918 /* caller should take a lock before calling */
919 int mdd_finish_unlink(const struct lu_env *env,
920                       struct mdd_object *obj, struct md_attr *ma,
921                       struct thandle *th)
922 {
923         int rc = 0;
924         int is_dir = S_ISDIR(ma->ma_attr.la_mode);
925         ENTRY;
926
927         LASSERT(mdd_write_locked(env, obj) != 0);
928
929         if (rc == 0 && (ma->ma_attr.la_nlink == 0 || is_dir)) {
930                 obj->mod_flags |= DEAD_OBJ;
931                 /* add new orphan and the object
932                  * will be deleted during mdd_close() */
933                 if (obj->mod_count) {
934                         rc = __mdd_orphan_add(env, obj, th);
935                         if (rc == 0)
936                                 CDEBUG(D_HA, "Object "DFID" is inserted into "
937                                         "orphan list, open count = %d\n",
938                                         PFID(mdd_object_fid(obj)),
939                                         obj->mod_count);
940                         else
941                                 CERROR("Object "DFID" fail to be an orphan, "
942                                        "open count = %d, maybe cause failed "
943                                        "open replay\n",
944                                         PFID(mdd_object_fid(obj)),
945                                         obj->mod_count);
946                 } else {
947                         rc = mdo_destroy(env, obj, th);
948                 }
949         }
950
951         RETURN(rc);
952 }
953
954 /*
955  * pobj maybe NULL
956  * has mdd_write_lock on cobj already, but not on pobj yet
957  */
958 int mdd_unlink_sanity_check(const struct lu_env *env, struct mdd_object *pobj,
959                             struct mdd_object *cobj, struct lu_attr *cattr)
960 {
961         int rc;
962         ENTRY;
963
964         rc = mdd_may_delete(env, pobj, cobj, cattr, NULL, 1, 1);
965
966         RETURN(rc);
967 }
968
969 static int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
970                               struct mdd_object *p, struct mdd_object *c,
971                               const struct lu_name *name, struct md_attr *ma,
972                               struct thandle *handle)
973 {
974         int rc;
975
976         rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
977         if (rc)
978                 return rc;
979
980         rc = mdo_declare_ref_del(env, p, handle);
981         if (rc)
982                 return rc;
983
984         rc = mdo_declare_ref_del(env, c, handle);
985         if (rc)
986                 return rc;
987
988         rc = mdo_declare_ref_del(env, c, handle);
989         if (rc)
990                 return rc;
991
992         rc = mdo_declare_attr_set(env, p, NULL, handle);
993         if (rc)
994                 return rc;
995
996         rc = mdo_declare_attr_set(env, c, NULL, handle);
997         if (rc)
998                 return rc;
999
1000         rc = mdd_declare_finish_unlink(env, c, ma, handle);
1001         if (rc)
1002                 return rc;
1003
1004         rc = mdd_declare_links_add(env, c, handle);
1005         if (rc)
1006                 return rc;
1007
1008         rc = mdd_declare_changelog_store(env, mdd, name, handle);
1009
1010         return rc;
1011 }
1012
1013 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1014                       struct md_object *cobj, const struct lu_name *lname,
1015                       struct md_attr *ma)
1016 {
1017         const char *name = lname->ln_name;
1018         struct lu_attr     *cattr = &mdd_env_info(env)->mti_cattr;
1019         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
1020         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1021         struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
1022         struct mdd_device *mdd = mdo2mdd(pobj);
1023         struct dynlock_handle *dlh;
1024         struct thandle    *handle;
1025         int rc, is_dir;
1026         ENTRY;
1027
1028         if (mdd_object_exists(mdd_cobj) <= 0)
1029                 RETURN(-ENOENT);
1030
1031         handle = mdd_trans_create(env, mdd);
1032         if (IS_ERR(handle))
1033                 RETURN(PTR_ERR(handle));
1034
1035         rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1036                                 lname, ma, handle);
1037         if (rc)
1038                 GOTO(stop, rc);
1039
1040         rc = mdd_trans_start(env, mdd, handle);
1041         if (rc)
1042                 GOTO(stop, rc);
1043
1044         dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1045         if (dlh == NULL)
1046                 GOTO(stop, rc = -ENOMEM);
1047         mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1048
1049         /* fetch cattr */
1050         rc = mdd_la_get(env, mdd_cobj, cattr, mdd_object_capa(env, mdd_cobj));
1051         if (rc)
1052                 GOTO(cleanup, rc);
1053
1054         is_dir = S_ISDIR(cattr->la_mode);
1055
1056         rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, cattr);
1057         if (rc)
1058                 GOTO(cleanup, rc);
1059
1060         rc = mdo_ref_del(env, mdd_cobj, handle);
1061         if (rc != 0) {
1062                 __mdd_index_insert_only(env, mdd_pobj, mdo2fid(mdd_cobj),
1063                                         name, handle,
1064                                         mdd_object_capa(env, mdd_pobj));
1065                 GOTO(cleanup, rc);
1066         }
1067
1068         rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1069                                 mdd_object_capa(env, mdd_pobj));
1070         if (rc)
1071                 GOTO(cleanup, rc);
1072
1073         if (is_dir)
1074                 /* unlink dot */
1075                 mdo_ref_del(env, mdd_cobj, handle);
1076
1077         /* fetch updated nlink */
1078         rc = mdd_la_get(env, mdd_cobj, cattr, mdd_object_capa(env, mdd_cobj));
1079         if (rc)
1080                 GOTO(cleanup, rc);
1081
1082         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1083         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1084
1085         la->la_valid = LA_CTIME | LA_MTIME;
1086         rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1087         if (rc)
1088                 GOTO(cleanup, rc);
1089
1090         if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1091                 /* update ctime of an unlinked file only if it is still
1092                  * opened or a link still exists */
1093                 la->la_valid = LA_CTIME;
1094                 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1095                 if (rc)
1096                         GOTO(cleanup, rc);
1097         }
1098
1099         /* XXX: this transfer to ma will be removed with LOD/OSP */
1100         ma->ma_attr = *cattr;
1101         ma->ma_valid |= MA_INODE;
1102         rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1103
1104         /* fetch updated nlink */
1105         if (rc == 0)
1106                 rc = mdd_la_get(env, mdd_cobj, cattr,
1107                                 mdd_object_capa(env, mdd_cobj));
1108
1109         if (!is_dir)
1110                 /* old files may not have link ea; ignore errors */
1111                 mdd_links_rename(env, mdd_cobj, mdo2fid(mdd_pobj),
1112                                  lname, NULL, NULL, handle);
1113
1114         /* if object is removed then we can't get its attrs, use last get */
1115         if (cattr->la_nlink == 0) {
1116                 ma->ma_attr = *cattr;
1117                 ma->ma_valid |= MA_INODE;
1118         }
1119         EXIT;
1120 cleanup:
1121         mdd_write_unlock(env, mdd_cobj);
1122         mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1123         if (rc == 0) {
1124                 int cl_flags;
1125
1126                 cl_flags = (cattr->la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1127                 if ((ma->ma_valid & MA_HSM) &&
1128                     (ma->ma_hsm.mh_flags & HS_EXISTS))
1129                         cl_flags |= CLF_UNLINK_HSM_EXISTS;
1130
1131                 rc = mdd_changelog_ns_store(env, mdd,
1132                         is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1133                         mdd_cobj, mdd_pobj, lname, handle);
1134         }
1135
1136 stop:
1137         mdd_trans_stop(env, mdd, rc, handle);
1138
1139         return rc;
1140 }
1141
1142 /*
1143  * The permission has been checked when obj created, no need check again.
1144  */
1145 static int mdd_cd_sanity_check(const struct lu_env *env,
1146                                struct mdd_object *obj)
1147 {
1148         ENTRY;
1149
1150         /* EEXIST check */
1151         if (!obj || mdd_is_dead_obj(obj))
1152                 RETURN(-ENOENT);
1153
1154         RETURN(0);
1155
1156 }
1157
1158 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1159                            struct md_object *cobj, const struct md_op_spec *spec,
1160                            struct md_attr *ma)
1161 {
1162         struct mdd_device *mdd = mdo2mdd(cobj);
1163         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1164         struct mdd_object *son = md2mdd_obj(cobj);
1165         struct thandle    *handle;
1166         const struct lu_buf *buf;
1167         struct lu_attr    *attr = &mdd_env_info(env)->mti_cattr;
1168         int                rc;
1169         ENTRY;
1170
1171         /* do not let users to create stripes via .lustre/
1172          * mdd_obf_setup() sets IMMUTE_OBJ on this directory */
1173         if (pobj && mdd_pobj->mod_flags & IMMUTE_OBJ)
1174                 RETURN(-ENOENT);
1175
1176         rc = mdd_cd_sanity_check(env, son);
1177         if (rc)
1178                 RETURN(rc);
1179
1180         if (!md_should_create(spec->sp_cr_flags))
1181                 RETURN(0);
1182
1183         /*
1184          * there are following use cases for this function:
1185          * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1186          *    striping can be specified or not
1187          * 2) CMD?
1188          */
1189         rc = mdd_la_get(env, son, attr, mdd_object_capa(env, son));
1190         if (rc)
1191                 RETURN(rc);
1192
1193         /* calling ->ah_make_hint() is used to transfer information from parent */
1194         mdd_object_make_hint(env, mdd_pobj, son, attr);
1195
1196         handle = mdd_trans_create(env, mdd);
1197         if (IS_ERR(handle))
1198                 GOTO(out_free, rc = PTR_ERR(handle));
1199
1200         /*
1201          * XXX: Setting the lov ea is not locked but setting the attr is locked?
1202          * Should this be fixed?
1203          */
1204         CDEBUG(D_OTHER, "ea %p/%u, cr_flags %Lo, no_create %u\n",
1205                spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1206                spec->sp_cr_flags, spec->no_create);
1207
1208         if (spec->no_create) {
1209                 /* replay case */
1210                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1211                                         spec->u.sp_ea.eadatalen);
1212         } else  if (!(spec->sp_cr_flags & MDS_OPEN_HAS_OBJS)) {
1213                 if (spec->sp_cr_flags & MDS_OPEN_HAS_EA) {
1214                         /* lfs setstripe */
1215                         buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1216                                                 spec->u.sp_ea.eadatalen);
1217                 } else {
1218                         buf = &LU_BUF_NULL;
1219                 }
1220         } else {
1221                 /* MDS_OPEN_HAS_OBJS is not used anymore ? */
1222                 LBUG();
1223         }
1224
1225         rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1226                                   XATTR_NAME_LOV, 0, handle);
1227         if (rc)
1228                 GOTO(stop, rc);
1229
1230         rc = mdd_trans_start(env, mdd, handle);
1231         if (rc)
1232                 GOTO(stop, rc);
1233
1234         rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1235                           0, handle, mdd_object_capa(env, son));
1236 stop:
1237         mdd_trans_stop(env, mdd, rc, handle);
1238 out_free:
1239         RETURN(rc);
1240 }
1241
1242 /* Get fid from name and parent */
1243 static int
1244 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1245              const struct lu_name *lname, struct lu_fid* fid, int mask)
1246 {
1247         const char          *name = lname->ln_name;
1248         const struct dt_key *key = (const struct dt_key *)name;
1249         struct mdd_object   *mdd_obj = md2mdd_obj(pobj);
1250         struct mdd_device   *m = mdo2mdd(pobj);
1251         struct dt_object    *dir = mdd_object_child(mdd_obj);
1252         int rc;
1253         ENTRY;
1254
1255         if (unlikely(mdd_is_dead_obj(mdd_obj)))
1256                 RETURN(-ESTALE);
1257
1258         rc = mdd_object_exists(mdd_obj);
1259         if (unlikely(rc == 0))
1260                 RETURN(-ESTALE);
1261         else if (unlikely(rc < 0)) {
1262                 CERROR("Object "DFID" locates on remote server\n",
1263                         PFID(mdo2fid(mdd_obj)));
1264                 RETURN(-EINVAL);
1265         }
1266
1267         /* The common filename length check. */
1268         if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1269                 RETURN(-ENAMETOOLONG);
1270
1271         rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1272                                             MOR_TGT_PARENT);
1273         if (rc)
1274                 RETURN(rc);
1275
1276         if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1277                    dt_try_as_dir(env, dir))) {
1278
1279                 rc = dir->do_index_ops->dio_lookup(env, dir,
1280                                                  (struct dt_rec *)fid, key,
1281                                                  mdd_object_capa(env, mdd_obj));
1282                 if (rc > 0)
1283                         rc = 0;
1284                 else if (rc == 0)
1285                         rc = -ENOENT;
1286         } else
1287                 rc = -ENOTDIR;
1288
1289         RETURN(rc);
1290 }
1291
1292 int mdd_declare_object_initialize(const struct lu_env *env,
1293                                   struct mdd_object *child,
1294                                   struct lu_attr *attr,
1295                                   struct thandle *handle)
1296 {
1297         int rc;
1298
1299         rc = mdo_declare_attr_set(env, child, attr, handle);
1300         if (rc == 0 && S_ISDIR(attr->la_mode)) {
1301                 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1302                                               dot, handle);
1303                 if (rc == 0)
1304                         rc = mdo_declare_ref_add(env, child, handle);
1305         }
1306
1307         if (rc == 0)
1308                 mdd_declare_links_add(env, child, handle);
1309
1310         return rc;
1311 }
1312
1313 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1314                           const struct lu_name *lname, struct mdd_object *child,
1315                           struct lu_attr *attr, struct thandle *handle,
1316                           const struct md_op_spec *spec)
1317 {
1318         int rc;
1319         ENTRY;
1320
1321         /*
1322          * Update attributes for child.
1323          *
1324          * FIXME:
1325          *  (1) the valid bits should be converted between Lustre and Linux;
1326          *  (2) maybe, the child attributes should be set in OSD when creation.
1327          */
1328
1329         rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1330         if (rc != 0)
1331                 RETURN(rc);
1332
1333         if (S_ISDIR(attr->la_mode)) {
1334                 /* Add "." and ".." for newly created dir */
1335                 mdo_ref_add(env, child, handle);
1336                 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1337                                              dot, handle, BYPASS_CAPA);
1338                 if (rc == 0)
1339                         rc = __mdd_index_insert_only(env, child, pfid,
1340                                                      dotdot, handle,
1341                                                      BYPASS_CAPA);
1342                 if (rc != 0)
1343                         mdo_ref_del(env, child, handle);
1344         }
1345         if (rc == 0)
1346                 mdd_links_add(env, child, pfid, lname, handle, 1);
1347
1348         RETURN(rc);
1349 }
1350
1351 /* has not lock on pobj yet */
1352 static int mdd_create_sanity_check(const struct lu_env *env,
1353                                    struct md_object *pobj,
1354                                    struct lu_attr *pattr,
1355                                    const struct lu_name *lname,
1356                                    struct lu_attr *cattr,
1357                                    struct md_op_spec *spec)
1358 {
1359         struct mdd_thread_info *info = mdd_env_info(env);
1360         struct lu_fid     *fid       = &info->mti_fid;
1361         struct mdd_object *obj       = md2mdd_obj(pobj);
1362         struct mdd_device *m         = mdo2mdd(pobj);
1363         int lookup                   = spec->sp_cr_lookup;
1364         int rc;
1365         ENTRY;
1366
1367         /* EEXIST check */
1368         if (mdd_is_dead_obj(obj))
1369                 RETURN(-ENOENT);
1370
1371         /*
1372          * In some cases this lookup is not needed - we know before if name
1373          * exists or not because MDT performs lookup for it.
1374          * name length check is done in lookup.
1375          */
1376         if (lookup) {
1377                 /*
1378                  * Check if the name already exist, though it will be checked in
1379                  * _index_insert also, for avoiding rolling back if exists
1380                  * _index_insert.
1381                  */
1382                 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1383                                          MAY_WRITE | MAY_EXEC);
1384                 if (rc != -ENOENT)
1385                         RETURN(rc ? : -EEXIST);
1386         } else {
1387                 /*
1388                  * Check WRITE permission for the parent.
1389                  * EXEC permission have been checked
1390                  * when lookup before create already.
1391                  */
1392                 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1393                                                     MOR_TGT_PARENT);
1394                 if (rc)
1395                         RETURN(rc);
1396         }
1397
1398         /* sgid check */
1399         if (pattr->la_mode & S_ISGID) {
1400                 cattr->la_gid = pattr->la_gid;
1401                 if (S_ISDIR(cattr->la_mode)) {
1402                         cattr->la_mode |= S_ISGID;
1403                         cattr->la_valid |= LA_MODE;
1404                 }
1405         }
1406
1407         switch (cattr->la_mode & S_IFMT) {
1408         case S_IFLNK: {
1409                 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1410
1411                 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1412                         RETURN(-ENAMETOOLONG);
1413                 else
1414                         RETURN(0);
1415         }
1416         case S_IFDIR:
1417         case S_IFREG:
1418         case S_IFCHR:
1419         case S_IFBLK:
1420         case S_IFIFO:
1421         case S_IFSOCK:
1422                 rc = 0;
1423                 break;
1424         default:
1425                 rc = -EINVAL;
1426                 break;
1427         }
1428         RETURN(rc);
1429 }
1430
1431 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1432                               struct mdd_object *p, struct mdd_object *c,
1433                               const struct lu_name *name,
1434                               struct lu_attr *attr,
1435                               int got_def_acl,
1436                               struct thandle *handle,
1437                               const struct md_op_spec *spec)
1438 {
1439         struct mdd_thread_info *info = mdd_env_info(env);
1440         int            rc = 0;
1441
1442         rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1443         if (rc)
1444                 GOTO(out, rc);
1445
1446 #ifdef CONFIG_FS_POSIX_ACL
1447         if (got_def_acl > 0) {
1448                 struct lu_buf *acl_buf;
1449
1450                 acl_buf = mdd_buf_get(env, NULL, got_def_acl);
1451                 /* if dir, then can inherit default ACl */
1452                 if (S_ISDIR(attr->la_mode)) {
1453                         rc = mdo_declare_xattr_set(env, c, acl_buf,
1454                                                    XATTR_NAME_ACL_DEFAULT,
1455                                                    0, handle);
1456                         if (rc)
1457                                 GOTO(out, rc);
1458                 }
1459
1460                 rc = mdo_declare_attr_set(env, c, &info->mti_pattr, handle);
1461                 if (rc)
1462                         GOTO(out, rc);
1463
1464                 rc = mdo_declare_xattr_set(env, c, acl_buf,
1465                                            XATTR_NAME_ACL_ACCESS, 0, handle);
1466                 if (rc)
1467                         GOTO(out, rc);
1468         }
1469 #endif
1470
1471         if (S_ISDIR(attr->la_mode)) {
1472                 rc = mdo_declare_ref_add(env, p, handle);
1473                 if (rc)
1474                         GOTO(out, rc);
1475         }
1476
1477         rc = mdd_declare_object_initialize(env, c, attr, handle);
1478         if (rc)
1479                 GOTO(out, rc);
1480
1481         rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1482                                       name->ln_name, handle);
1483         if (rc)
1484                 GOTO(out, rc);
1485
1486         /* replay case, create LOV EA from client data */
1487         if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1488                 const struct lu_buf *buf;
1489
1490                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1491                                         spec->u.sp_ea.eadatalen);
1492                 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1493                                            0, handle);
1494                 if (rc)
1495                         GOTO(out, rc);
1496         }
1497
1498         if (S_ISLNK(attr->la_mode)) {
1499                 rc = dt_declare_record_write(env, mdd_object_child(c),
1500                                              strlen(spec->u.sp_symname), 0,
1501                                              handle);
1502                 if (rc)
1503                         GOTO(out, rc);
1504         }
1505
1506         rc = mdo_declare_attr_set(env, p, attr, handle);
1507         if (rc)
1508                 return rc;
1509
1510         rc = mdd_declare_changelog_store(env, mdd, name, handle);
1511         if (rc)
1512                 return rc;
1513
1514 out:
1515         return rc;
1516 }
1517
1518
1519 /*
1520  * Create object and insert it into namespace.
1521  */
1522 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
1523                       const struct lu_name *lname, struct md_object *child,
1524                       struct md_op_spec *spec, struct md_attr* ma)
1525 {
1526         struct mdd_thread_info *info = mdd_env_info(env);
1527         struct lu_attr         *la = &info->mti_la_for_fix;
1528         struct mdd_object      *mdd_pobj = md2mdd_obj(pobj);
1529         struct mdd_object      *son = md2mdd_obj(child);
1530         struct mdd_device      *mdd = mdo2mdd(pobj);
1531         struct lu_attr         *attr = &ma->ma_attr;
1532         struct thandle         *handle;
1533         struct lu_attr         *pattr = &info->mti_pattr;
1534         struct dynlock_handle  *dlh;
1535         const char             *name = lname->ln_name;
1536         int rc, created = 0, initialized = 0, inserted = 0;
1537         int got_def_acl = 0;
1538         ENTRY;
1539
1540         /*
1541          * Two operations have to be performed:
1542          *
1543          *  - an allocation of a new object (->do_create()), and
1544          *
1545          *  - an insertion into a parent index (->dio_insert()).
1546          *
1547          * Due to locking, operation order is not important, when both are
1548          * successful, *but* error handling cases are quite different:
1549          *
1550          *  - if insertion is done first, and following object creation fails,
1551          *  insertion has to be rolled back, but this operation might fail
1552          *  also leaving us with dangling index entry.
1553          *
1554          *  - if creation is done first, is has to be undone if insertion
1555          *  fails, leaving us with leaked space, which is neither good, nor
1556          *  fatal.
1557          *
1558          * It seems that creation-first is simplest solution, but it is
1559          * sub-optimal in the frequent
1560          *
1561          *         $ mkdir foo
1562          *         $ mkdir foo
1563          *
1564          * case, because second mkdir is bound to create object, only to
1565          * destroy it immediately.
1566          *
1567          * To avoid this follow local file systems that do double lookup:
1568          *
1569          *     0. lookup -> -EEXIST (mdd_create_sanity_check())
1570          *
1571          *     1. create            (mdd_object_create_internal())
1572          *
1573          *     2. insert            (__mdd_index_insert(), lookup again)
1574          */
1575
1576         rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1577         if (rc != 0)
1578                 RETURN(rc);
1579
1580         /* Sanity checks before big job. */
1581         rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
1582         if (rc)
1583                 RETURN(rc);
1584
1585         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
1586                 GOTO(out_free, rc = -EINPROGRESS);
1587
1588         if (!S_ISLNK(attr->la_mode)) {
1589                 struct lu_buf *acl_buf;
1590
1591                 acl_buf = mdd_buf_get(env, info->mti_xattr_buf,
1592                                 sizeof(info->mti_xattr_buf));
1593                 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1594                 rc = mdo_xattr_get(env, mdd_pobj, acl_buf,
1595                                 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1596                 mdd_read_unlock(env, mdd_pobj);
1597                 if (rc > 0)
1598                         got_def_acl = rc;
1599                 else if (rc < 0 && rc != -EOPNOTSUPP && rc != -ENODATA)
1600                         GOTO(out_free, rc);
1601         }
1602
1603         mdd_object_make_hint(env, mdd_pobj, son, attr);
1604
1605         handle = mdd_trans_create(env, mdd);
1606         if (IS_ERR(handle))
1607                 GOTO(out_free, rc = PTR_ERR(handle));
1608
1609         rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
1610                                 got_def_acl, handle, spec);
1611         if (rc)
1612                 GOTO(out_stop, rc);
1613
1614         rc = mdd_trans_start(env, mdd, handle);
1615         if (rc)
1616                 GOTO(out_stop, rc);
1617
1618         dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1619         if (dlh == NULL)
1620                 GOTO(out_trans, rc = -ENOMEM);
1621
1622         mdd_write_lock(env, son, MOR_TGT_CHILD);
1623         rc = mdd_object_create_internal(env, mdd_pobj, son, attr, handle, spec);
1624         if (rc) {
1625                 mdd_write_unlock(env, son);
1626                 GOTO(cleanup, rc);
1627         }
1628
1629         created = 1;
1630
1631 #ifdef CONFIG_FS_POSIX_ACL
1632         if (got_def_acl) {
1633                 struct lu_buf *acl_buf;
1634
1635                 acl_buf = mdd_buf_get(env, info->mti_xattr_buf, got_def_acl);
1636                 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1637                 if (rc) {
1638                         mdd_write_unlock(env, son);
1639                         GOTO(cleanup, rc);
1640                 }
1641         }
1642 #endif
1643
1644         rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1645                                    son, attr, handle, spec);
1646
1647         /*
1648          * in case of replay we just set LOVEA provided by the client
1649          * XXX: I think it would be interesting to try "old" way where
1650          *      MDT calls this xattr_set(LOV) in a different transaction.
1651          *      probably this way we code can be made better.
1652          */
1653         if (rc == 0 &&
1654                         (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA))) {
1655                 const struct lu_buf *buf;
1656
1657                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1658                                 spec->u.sp_ea.eadatalen);
1659                 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
1660                                 BYPASS_CAPA);
1661         }
1662         mdd_write_unlock(env, son);
1663         if (rc)
1664                 /*
1665                  * Object has no links, so it will be destroyed when last
1666                  * reference is released. (XXX not now.)
1667                  */
1668                 GOTO(cleanup, rc);
1669
1670         initialized = 1;
1671
1672         rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1673                                 name, S_ISDIR(attr->la_mode), handle,
1674                                 mdd_object_capa(env, mdd_pobj));
1675         if (rc)
1676                 GOTO(cleanup, rc);
1677
1678         inserted = 1;
1679
1680         if (S_ISLNK(attr->la_mode)) {
1681                 struct md_ucred  *uc = md_ucred(env);
1682                 struct dt_object *dt = mdd_object_child(son);
1683                 const char *target_name = spec->u.sp_symname;
1684                 int sym_len = strlen(target_name);
1685                 const struct lu_buf *buf;
1686                 loff_t pos = 0;
1687
1688                 buf = mdd_buf_get_const(env, target_name, sym_len);
1689                 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1690                                                 mdd_object_capa(env, son),
1691                                                 uc->mu_cap &
1692                                                 CFS_CAP_SYS_RESOURCE_MASK);
1693
1694                 if (rc == sym_len)
1695                         rc = 0;
1696                 else
1697                         GOTO(cleanup, rc = -EFAULT);
1698         }
1699
1700         *la = *attr;
1701         la->la_valid = LA_CTIME | LA_MTIME;
1702         rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1703         if (rc)
1704                 GOTO(cleanup, rc);
1705
1706         EXIT;
1707 cleanup:
1708         if (rc != 0 && created != 0) {
1709                 int rc2;
1710
1711                 if (inserted != 0) {
1712                         rc2 = __mdd_index_delete(env, mdd_pobj, name,
1713                                                  S_ISDIR(attr->la_mode),
1714                                                  handle, BYPASS_CAPA);
1715                         if (rc2 != 0)
1716                                 goto out_stop;
1717                 }
1718
1719                 mdd_write_lock(env, son, MOR_TGT_CHILD);
1720                 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
1721                         /* Drop the reference, no need to delete "."/"..",
1722                          * because the object to be destroied directly. */
1723                         rc2 = mdo_ref_del(env, son, handle);
1724                         if (rc2 != 0) {
1725                                 mdd_write_unlock(env, son);
1726                                 goto out_stop;
1727                         }
1728                 }
1729
1730                 rc2 = mdo_ref_del(env, son, handle);
1731                 if (rc2 != 0) {
1732                         mdd_write_unlock(env, son);
1733                         goto out_stop;
1734                 }
1735
1736                 mdo_destroy(env, son, handle);
1737                 mdd_write_unlock(env, son);
1738         }
1739
1740         mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1741 out_trans:
1742         if (rc == 0)
1743                 rc = mdd_changelog_ns_store(env, mdd,
1744                         S_ISDIR(attr->la_mode) ? CL_MKDIR :
1745                         S_ISREG(attr->la_mode) ? CL_CREATE :
1746                         S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1747                         0, son, mdd_pobj, lname, handle);
1748 out_stop:
1749         mdd_trans_stop(env, mdd, rc, handle);
1750 out_free:
1751         /* The child object shouldn't be cached anymore */
1752         if (rc)
1753                 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE,
1754                             &child->mo_lu.lo_header->loh_flags);
1755         return rc;
1756 }
1757
1758 /*
1759  * Get locks on parents in proper order
1760  * RETURN: < 0 - error, rename_order if successful
1761  */
1762 enum rename_order {
1763         MDD_RN_SAME,
1764         MDD_RN_SRCTGT,
1765         MDD_RN_TGTSRC
1766 };
1767
1768 static int mdd_rename_order(const struct lu_env *env,
1769                             struct mdd_device *mdd,
1770                             struct mdd_object *src_pobj,
1771                             struct mdd_object *tgt_pobj)
1772 {
1773         /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1774         int rc;
1775         ENTRY;
1776
1777         if (src_pobj == tgt_pobj)
1778                 RETURN(MDD_RN_SAME);
1779
1780         /* compared the parent child relationship of src_p&tgt_p */
1781         if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1782                 rc = MDD_RN_SRCTGT;
1783         } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1784                 rc = MDD_RN_TGTSRC;
1785         } else {
1786                 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1787                 if (rc == -EREMOTE)
1788                         rc = 0;
1789
1790                 if (rc == 1)
1791                         rc = MDD_RN_TGTSRC;
1792                 else if (rc == 0)
1793                         rc = MDD_RN_SRCTGT;
1794         }
1795
1796         RETURN(rc);
1797 }
1798
1799 /* has not mdd_write{read}_lock on any obj yet. */
1800 static int mdd_rename_sanity_check(const struct lu_env *env,
1801                                    struct mdd_object *src_pobj,
1802                                    struct mdd_object *tgt_pobj,
1803                                    struct mdd_object *sobj,
1804                                    struct mdd_object *tobj,
1805                                    struct lu_attr *so_attr,
1806                                    struct lu_attr *tg_attr)
1807 {
1808         int rc = 0;
1809         ENTRY;
1810
1811         /* XXX: when get here, sobj must NOT be NULL,
1812          * the other case has been processed in cml_rename
1813          * before mdd_rename and enable MDS_PERM_BYPASS. */
1814         LASSERT(sobj);
1815
1816         rc = mdd_may_delete(env, src_pobj, sobj, so_attr, NULL, 1, 0);
1817         if (rc)
1818                 RETURN(rc);
1819
1820         /* XXX: when get here, "tobj == NULL" means tobj must
1821          * NOT exist (neither on remote MDS, such case has been
1822          * processed in cml_rename before mdd_rename and enable
1823          * MDS_PERM_BYPASS).
1824          * So check may_create, but not check may_unlink. */
1825         if (!tobj)
1826                 rc = mdd_may_create(env, tgt_pobj, NULL,
1827                                     (src_pobj != tgt_pobj), 0);
1828         else
1829                 rc = mdd_may_delete(env, tgt_pobj, tobj, tg_attr, so_attr,
1830                                     (src_pobj != tgt_pobj), 1);
1831
1832         if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1833             S_ISDIR(so_attr->la_mode))
1834                 rc = __mdd_may_link(env, tgt_pobj);
1835
1836         RETURN(rc);
1837 }
1838
1839 static int mdd_declare_rename(const struct lu_env *env,
1840                               struct mdd_device *mdd,
1841                               struct mdd_object *mdd_spobj,
1842                               struct mdd_object *mdd_tpobj,
1843                               struct mdd_object *mdd_sobj,
1844                               struct mdd_object *mdd_tobj,
1845                               const struct lu_name *tname,
1846                               const struct lu_name *sname,
1847                               struct md_attr *ma,
1848                               struct thandle *handle)
1849 {
1850         int rc;
1851
1852         LASSERT(mdd_spobj);
1853         LASSERT(mdd_tpobj);
1854         LASSERT(mdd_sobj);
1855
1856         /* name from source dir */
1857         rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
1858         if (rc)
1859                 return rc;
1860
1861         /* .. from source child */
1862         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
1863                 /* source child can be directory,
1864                  * counted by source dir's nlink */
1865                 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
1866                 if (rc)
1867                         return rc;
1868
1869                 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
1870                 if (rc)
1871                         return rc;
1872
1873                 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
1874                                               dotdot, handle);
1875                 if (rc)
1876                         return rc;
1877
1878                 /* new target child can be directory,
1879                  * counted by target dir's nlink */
1880                 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
1881                 if (rc)
1882                         return rc;
1883
1884         }
1885
1886         rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
1887         if (rc)
1888                 return rc;
1889
1890         rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
1891         if (rc)
1892                 return rc;
1893         mdd_declare_links_add(env, mdd_sobj, handle);
1894         if (rc)
1895                 return rc;
1896
1897         rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
1898         if (rc)
1899                 return rc;
1900
1901         /* new name */
1902         rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
1903                         tname->ln_name, handle);
1904         if (rc)
1905                 return rc;
1906
1907         /* name from target dir (old name), we declare it unconditionally
1908          * as mdd_rename() calls delete unconditionally as well. so just
1909          * to balance declarations vs calls to change ... */
1910         rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
1911         if (rc)
1912                 return rc;
1913
1914         if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
1915                 /* delete target child in target parent directory */
1916                 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
1917                 if (rc)
1918                         return rc;
1919
1920                 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
1921                         /* target child can be directory,
1922                          * delete "." reference in target child directory */
1923                         rc = mdo_declare_ref_del(env, mdd_tobj, handle);
1924                         if (rc)
1925                                 return rc;
1926
1927                         /* delete ".." reference in target parent directory */
1928                         rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
1929                         if (rc)
1930                                 return rc;
1931                 }
1932
1933                 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
1934                 if (rc)
1935                         return rc;
1936
1937                 mdd_declare_links_add(env, mdd_tobj, handle);
1938                 if (rc)
1939                         return rc;
1940
1941                 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
1942                 if (rc)
1943                         return rc;
1944         }
1945
1946         rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
1947         if (rc)
1948                 return rc;
1949
1950         return rc;
1951 }
1952
1953 /* src object can be remote that is why we use only fid and type of object */
1954 static int mdd_rename(const struct lu_env *env,
1955                       struct md_object *src_pobj, struct md_object *tgt_pobj,
1956                       const struct lu_fid *lf, const struct lu_name *lsname,
1957                       struct md_object *tobj, const struct lu_name *ltname,
1958                       struct md_attr *ma)
1959 {
1960         const char *sname = lsname->ln_name;
1961         const char *tname = ltname->ln_name;
1962         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
1963         struct lu_attr    *so_attr = &mdd_env_info(env)->mti_cattr;
1964         struct lu_attr    *tg_attr = &mdd_env_info(env)->mti_pattr;
1965         struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
1966         struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1967         struct mdd_device *mdd = mdo2mdd(src_pobj);
1968         struct mdd_object *mdd_sobj = NULL;                  /* source object */
1969         struct mdd_object *mdd_tobj = NULL;
1970         struct dynlock_handle *sdlh, *tdlh;
1971         struct thandle *handle;
1972         const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
1973         const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
1974         int is_dir;
1975         unsigned cl_flags = 0;
1976         int rc, rc2;
1977         ENTRY;
1978
1979         if (tobj)
1980                 mdd_tobj = md2mdd_obj(tobj);
1981
1982         mdd_sobj = mdd_object_find(env, mdd, lf);
1983
1984         handle = mdd_trans_create(env, mdd);
1985         if (IS_ERR(handle))
1986                 GOTO(out_pending, rc = PTR_ERR(handle));
1987
1988         rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
1989                                 mdd_tobj, lsname, ltname, ma, handle);
1990         if (rc)
1991                 GOTO(stop, rc);
1992
1993         rc = mdd_trans_start(env, mdd, handle);
1994         if (rc)
1995                 GOTO(stop, rc);
1996
1997         /* FIXME: Should consider tobj and sobj too in rename_lock. */
1998         rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
1999         if (rc < 0)
2000                 GOTO(cleanup_unlocked, rc);
2001
2002         /* Get locks in determined order */
2003         if (rc == MDD_RN_SAME) {
2004                 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2005                                           sname, MOR_SRC_PARENT);
2006                 /* check hashes to determine do we need one lock or two */
2007                 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2008                         tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2009                                 MOR_TGT_PARENT);
2010                 else
2011                         tdlh = sdlh;
2012         } else if (rc == MDD_RN_SRCTGT) {
2013                 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2014                 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2015         } else {
2016                 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2017                 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2018         }
2019         if (sdlh == NULL || tdlh == NULL)
2020                 GOTO(cleanup, rc = -ENOMEM);
2021
2022         rc = mdd_la_get(env, mdd_sobj, so_attr,
2023                         mdd_object_capa(env, mdd_sobj));
2024         if (rc)
2025                 GOTO(cleanup, rc);
2026
2027         if (mdd_tobj) {
2028                 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2029                                 mdd_object_capa(env, mdd_tobj));
2030                 if (rc)
2031                         GOTO(cleanup, rc);
2032         }
2033
2034         rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj, mdd_sobj,
2035                                      mdd_tobj, so_attr, tg_attr);
2036         if (rc)
2037                 GOTO(cleanup, rc);
2038
2039         is_dir = S_ISDIR(so_attr->la_mode);
2040
2041         /* Remove source name from source directory */
2042         rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2043                                 mdd_object_capa(env, mdd_spobj));
2044         if (rc)
2045                 GOTO(cleanup, rc);
2046
2047         /* "mv dir1 dir2" needs "dir1/.." link update */
2048         if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2049                 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2050                                         mdd_object_capa(env, mdd_sobj));
2051                 if (rc)
2052                         GOTO(fixup_spobj2, rc);
2053
2054                 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2055                                       handle, mdd_object_capa(env, mdd_sobj));
2056                 if (rc)
2057                         GOTO(fixup_spobj, rc);
2058         }
2059
2060         /* Remove target name from target directory
2061          * Here tobj can be remote one, so we do index_delete unconditionally
2062          * and -ENOENT is allowed.
2063          */
2064         rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2065                                 mdd_object_capa(env, mdd_tpobj));
2066         if (rc != 0) {
2067                 if (mdd_tobj) {
2068                         /* tname might been renamed to something else */
2069                         GOTO(fixup_spobj, rc);
2070                 }
2071                 if (rc != -ENOENT)
2072                         GOTO(fixup_spobj, rc);
2073         }
2074
2075         /* Insert new fid with target name into target dir */
2076         rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2077                                 mdd_object_capa(env, mdd_tpobj));
2078         if (rc)
2079                 GOTO(fixup_tpobj, rc);
2080
2081         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2082         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2083
2084         /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2085         if (mdd_sobj) {
2086                 la->la_valid = LA_CTIME;
2087                 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
2088                 if (rc)
2089                         GOTO(fixup_tpobj, rc);
2090         }
2091
2092         /* Remove old target object
2093          * For tobj is remote case cmm layer has processed
2094          * and set tobj to NULL then. So when tobj is NOT NULL,
2095          * it must be local one.
2096          */
2097         if (tobj && mdd_object_exists(mdd_tobj)) {
2098                 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2099                 if (mdd_is_dead_obj(mdd_tobj)) {
2100                         mdd_write_unlock(env, mdd_tobj);
2101                         /* shld not be dead, something is wrong */
2102                         CERROR("tobj is dead, something is wrong\n");
2103                         rc = -EINVAL;
2104                         goto cleanup;
2105                 }
2106                 mdo_ref_del(env, mdd_tobj, handle);
2107
2108                 /* Remove dot reference. */
2109                 if (S_ISDIR(tg_attr->la_mode))
2110                         mdo_ref_del(env, mdd_tobj, handle);
2111
2112                 /* fetch updated nlink */
2113                 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2114                                 mdd_object_capa(env, mdd_tobj));
2115                 if (rc)
2116                         GOTO(fixup_tpobj, rc);
2117
2118                 la->la_valid = LA_CTIME;
2119                 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2120                 if (rc)
2121                         GOTO(fixup_tpobj, rc);
2122
2123                 /* XXX: this transfer to ma will be removed with LOD/OSP */
2124                 ma->ma_attr = *tg_attr;
2125                 ma->ma_valid |= MA_INODE;
2126                 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2127                 mdd_write_unlock(env, mdd_tobj);
2128                 if (rc)
2129                         GOTO(fixup_tpobj, rc);
2130
2131                 /* fetch updated nlink */
2132                 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2133                                 mdd_object_capa(env, mdd_tobj));
2134                 if (rc)
2135                         GOTO(fixup_tpobj, rc);
2136                 /* XXX: this transfer to ma will be removed with LOD/OSP */
2137                 ma->ma_attr = *tg_attr;
2138                 ma->ma_valid |= MA_INODE;
2139
2140                 if (so_attr->la_nlink == 0)
2141                         cl_flags |= CLF_RENAME_LAST;
2142         }
2143
2144         la->la_valid = LA_CTIME | LA_MTIME;
2145         rc = mdd_attr_check_set_internal(env, mdd_spobj, la, handle, 0);
2146         if (rc)
2147                 GOTO(fixup_tpobj, rc);
2148
2149         if (mdd_spobj != mdd_tpobj) {
2150                 la->la_valid = LA_CTIME | LA_MTIME;
2151                 rc = mdd_attr_check_set_internal(env, mdd_tpobj, la,
2152                                                  handle, 0);
2153         }
2154
2155         if (rc == 0 && mdd_sobj) {
2156                 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2157                 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2158                                       mdo2fid(mdd_tpobj), ltname, handle);
2159                 if (rc == -ENOENT)
2160                         /* Old files might not have EA entry */
2161                         mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2162                                       lsname, handle, 0);
2163                 mdd_write_unlock(env, mdd_sobj);
2164                 /* We don't fail the transaction if the link ea can't be
2165                    updated -- fid2path will use alternate lookup method. */
2166                 rc = 0;
2167         }
2168
2169         EXIT;
2170
2171 fixup_tpobj:
2172         if (rc) {
2173                 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2174                                          BYPASS_CAPA);
2175                 if (rc2)
2176                         CWARN("tp obj fix error %d\n",rc2);
2177
2178                 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2179                     !mdd_is_dead_obj(mdd_tobj)) {
2180                         rc2 = __mdd_index_insert(env, mdd_tpobj,
2181                                          mdo2fid(mdd_tobj), tname,
2182                                          is_dir, handle,
2183                                          BYPASS_CAPA);
2184
2185                         if (rc2)
2186                                 CWARN("tp obj fix error %d\n",rc2);
2187                 }
2188         }
2189
2190 fixup_spobj:
2191         if (rc && is_dir && mdd_sobj) {
2192                 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2193                                               BYPASS_CAPA);
2194
2195                 if (rc2)
2196                         CWARN("sp obj dotdot delete error %d\n",rc2);
2197
2198
2199                 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2200                                               dotdot, handle, BYPASS_CAPA);
2201                 if (rc2)
2202                         CWARN("sp obj dotdot insert error %d\n",rc2);
2203         }
2204
2205 fixup_spobj2:
2206         if (rc) {
2207                 rc2 = __mdd_index_insert(env, mdd_spobj,
2208                                          lf, sname, is_dir, handle, BYPASS_CAPA);
2209                 if (rc2)
2210                         CWARN("sp obj fix error %d\n",rc2);
2211         }
2212 cleanup:
2213         if (likely(tdlh) && sdlh != tdlh)
2214                 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2215         if (likely(sdlh))
2216                 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2217 cleanup_unlocked:
2218         if (rc == 0)
2219                 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2220                                                 mdd_tobj, tpobj_fid, lf,
2221                                                 spobj_fid, ltname, lsname,
2222                                                 handle);
2223
2224 stop:
2225         mdd_trans_stop(env, mdd, rc, handle);
2226         if (mdd_sobj)
2227                 mdd_object_put(env, mdd_sobj);
2228 out_pending:
2229         return rc;
2230 }
2231
2232 /** enable/disable storing of hardlink info */
2233 int mdd_linkea_enable = 1;
2234 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2235                 "record hardlink info in EAs");
2236
2237 /** Read the link EA into a temp buffer.
2238  * Uses the name_buf since it is generally large.
2239  * \retval IS_ERR err
2240  * \retval ptr to \a lu_buf (always \a mti_big_buf)
2241  */
2242 struct lu_buf *mdd_links_get(const struct lu_env *env,
2243                              struct mdd_object *mdd_obj)
2244 {
2245         struct lu_buf *buf;
2246         struct lustre_capa *capa;
2247         struct link_ea_header *leh;
2248         int rc;
2249
2250         /* First try a small buf */
2251         buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2252         if (buf->lb_buf == NULL)
2253                 return ERR_PTR(-ENOMEM);
2254
2255         if (!mdd_object_exists(mdd_obj))
2256                 return ERR_PTR(-ENODATA);
2257
2258         capa = mdd_object_capa(env, mdd_obj);
2259         rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2260         if (rc == -ERANGE) {
2261                 /* Buf was too small, figure out what we need. */
2262                 mdd_buf_put(buf);
2263                 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2264                 if (rc < 0)
2265                         return ERR_PTR(rc);
2266                 buf = mdd_buf_alloc(env, rc);
2267                 if (buf->lb_buf == NULL)
2268                         return ERR_PTR(-ENOMEM);
2269                 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2270         }
2271         if (rc < 0)
2272                 return ERR_PTR(rc);
2273
2274         leh = buf->lb_buf;
2275         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2276                 leh->leh_magic = LINK_EA_MAGIC;
2277                 leh->leh_reccount = __swab32(leh->leh_reccount);
2278                 leh->leh_len = __swab64(leh->leh_len);
2279                 /* entries are swabbed by mdd_lee_unpack */
2280         }
2281         if (leh->leh_magic != LINK_EA_MAGIC)
2282                 return ERR_PTR(-EINVAL);
2283         if (leh->leh_reccount == 0)
2284                 return ERR_PTR(-ENODATA);
2285
2286         return buf;
2287 }
2288
2289 /** Pack a link_ea_entry.
2290  * All elements are stored as chars to avoid alignment issues.
2291  * Numbers are always big-endian
2292  * \retval record length
2293  */
2294 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2295                         const struct lu_fid *pfid)
2296 {
2297         struct lu_fid   tmpfid;
2298         int             reclen;
2299
2300         fid_cpu_to_be(&tmpfid, pfid);
2301         memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2302         memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2303         reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2304
2305         lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2306         lee->lee_reclen[1] = reclen & 0xff;
2307         return reclen;
2308 }
2309
2310 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2311                     struct lu_name *lname, struct lu_fid *pfid)
2312 {
2313         *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2314         memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2315         fid_be_to_cpu(pfid, pfid);
2316         lname->ln_name = lee->lee_name;
2317         lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2318 }
2319
2320 /** Add a record to the end of link ea buf */
2321 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2322                            const struct lu_fid *pfid,
2323                            const struct lu_name *lname)
2324 {
2325         struct link_ea_header *leh;
2326         struct link_ea_entry *lee;
2327         int reclen;
2328
2329         if (lname == NULL || pfid == NULL)
2330                 return -EINVAL;
2331
2332         /* Make sure our buf is big enough for the new one */
2333         leh = buf->lb_buf;
2334         reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2335         if (leh->leh_len + reclen > buf->lb_len) {
2336                 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2337                         return -ENOMEM;
2338         }
2339
2340         leh = buf->lb_buf;
2341         lee = buf->lb_buf + leh->leh_len;
2342         reclen = mdd_lee_pack(lee, lname, pfid);
2343         leh->leh_len += reclen;
2344         leh->leh_reccount++;
2345         return 0;
2346 }
2347
2348 static int mdd_declare_links_add(const struct lu_env *env,
2349                                  struct mdd_object *mdd_obj,
2350                                  struct thandle *handle)
2351 {
2352         int rc;
2353
2354         /* XXX: max size? */
2355         rc = mdo_declare_xattr_set(env, mdd_obj,
2356                              mdd_buf_get_const(env, NULL, 4096),
2357                              XATTR_NAME_LINK, 0, handle);
2358
2359         return rc;
2360 }
2361
2362 /* For pathologic linkers, we don't want to spend lots of time scanning the
2363  * link ea.  Limit ourseleves to something reasonable; links not in the EA
2364  * can be looked up via (slower) parent lookup.
2365  */
2366 #define LINKEA_MAX_COUNT 128
2367
2368 static int mdd_links_add(const struct lu_env *env,
2369                          struct mdd_object *mdd_obj,
2370                          const struct lu_fid *pfid,
2371                          const struct lu_name *lname,
2372                          struct thandle *handle, int first)
2373 {
2374         struct lu_buf *buf;
2375         struct link_ea_header *leh;
2376         int rc;
2377         ENTRY;
2378
2379         if (!mdd_linkea_enable)
2380                 RETURN(0);
2381
2382         buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2383         if (IS_ERR(buf)) {
2384                 rc = PTR_ERR(buf);
2385                 if (rc != -ENODATA) {
2386                         CERROR("link_ea read failed %d "DFID"\n", rc,
2387                                PFID(mdd_object_fid(mdd_obj)));
2388                         RETURN (rc);
2389                 }
2390                 /* empty EA; start one */
2391                 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2392                 if (buf->lb_buf == NULL)
2393                         RETURN(-ENOMEM);
2394                 leh = buf->lb_buf;
2395                 leh->leh_magic = LINK_EA_MAGIC;
2396                 leh->leh_len = sizeof(struct link_ea_header);
2397                 leh->leh_reccount = 0;
2398         }
2399
2400         leh = buf->lb_buf;
2401         if (leh->leh_reccount > LINKEA_MAX_COUNT)
2402                 RETURN(-EOVERFLOW);
2403
2404         rc = __mdd_links_add(env, buf, pfid, lname);
2405         if (rc)
2406                 RETURN(rc);
2407
2408         leh = buf->lb_buf;
2409         rc = mdo_xattr_set(env, mdd_obj,
2410                              mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2411                              XATTR_NAME_LINK, 0, handle,
2412                              mdd_object_capa(env, mdd_obj));
2413         if (rc) {
2414                 if (rc == -ENOSPC)
2415                         CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
2416                                PFID(mdd_object_fid(mdd_obj)));
2417                 else
2418                         CERROR("link_ea add failed %d "DFID"\n", rc,
2419                                PFID(mdd_object_fid(mdd_obj)));
2420         }
2421
2422         if (buf->lb_len > OBD_ALLOC_BIG)
2423                 /* if we vmalloced a large buffer drop it */
2424                 mdd_buf_put(buf);
2425
2426         RETURN (rc);
2427 }
2428
2429 static int mdd_links_rename(const struct lu_env *env,
2430                             struct mdd_object *mdd_obj,
2431                             const struct lu_fid *oldpfid,
2432                             const struct lu_name *oldlname,
2433                             const struct lu_fid *newpfid,
2434                             const struct lu_name *newlname,
2435                             struct thandle *handle)
2436 {
2437         struct lu_buf  *buf;
2438         struct link_ea_header *leh;
2439         struct link_ea_entry  *lee;
2440         struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2441         struct lu_fid  *tmpfid = &mdd_env_info(env)->mti_fid;
2442         int reclen = 0;
2443         int count;
2444         int rc, rc2 = 0;
2445         ENTRY;
2446
2447         if (!mdd_linkea_enable)
2448                 RETURN(0);
2449
2450         if (mdd_obj->mod_flags & DEAD_OBJ)
2451                 /* No more links, don't bother */
2452                 RETURN(0);
2453
2454         buf = mdd_links_get(env, mdd_obj);
2455         if (IS_ERR(buf)) {
2456                 rc = PTR_ERR(buf);
2457                 if (rc == -ENODATA)
2458                         CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
2459                                rc, PFID(mdd_object_fid(mdd_obj)));
2460                 else
2461                         CERROR("link_ea read failed %d "DFID"\n",
2462                                rc, PFID(mdd_object_fid(mdd_obj)));
2463                 RETURN(rc);
2464         }
2465         leh = buf->lb_buf;
2466         lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2467
2468         /* Find the old record */
2469         for(count = 0; count < leh->leh_reccount; count++) {
2470                 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2471                 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2472                     lu_fid_eq(tmpfid, oldpfid) &&
2473                     (strncmp(tmpname->ln_name, oldlname->ln_name,
2474                              tmpname->ln_namelen) == 0))
2475                         break;
2476                 lee = (struct link_ea_entry *)((char *)lee + reclen);
2477         }
2478         if ((count + 1) > leh->leh_reccount) {
2479                 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2480                        oldlname->ln_namelen, oldlname->ln_name);
2481                 GOTO(out, rc = -ENOENT);
2482         }
2483
2484         /* Remove the old record */
2485         leh->leh_reccount--;
2486         leh->leh_len -= reclen;
2487         memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2488                 (char *)lee);
2489
2490         /* If renaming, add the new record */
2491         if (newpfid != NULL) {
2492                 /* if the add fails, we still delete the out-of-date old link */
2493                 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2494                 leh = buf->lb_buf;
2495         }
2496
2497         rc = mdo_xattr_set(env, mdd_obj,
2498                            mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2499                            XATTR_NAME_LINK, 0, handle,
2500                            mdd_object_capa(env, mdd_obj));
2501
2502 out:
2503         if (rc == 0)
2504                 rc = rc2;
2505         if (rc)
2506                 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2507                        oldlname->ln_namelen, oldlname->ln_name, rc,
2508                        PFID(mdd_object_fid(mdd_obj)));
2509
2510         if (buf->lb_len > OBD_ALLOC_BIG)
2511                 /* if we vmalloced a large buffer drop it */
2512                 mdd_buf_put(buf);
2513
2514         RETURN (rc);
2515 }
2516
2517 const struct md_dir_operations mdd_dir_ops = {
2518         .mdo_is_subdir     = mdd_is_subdir,
2519         .mdo_lookup        = mdd_lookup,
2520         .mdo_create        = mdd_create,
2521         .mdo_rename        = mdd_rename,
2522         .mdo_link          = mdd_link,
2523         .mdo_unlink        = mdd_unlink,
2524         .mdo_create_data   = mdd_create_data,
2525 };