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