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