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