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