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