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