Whamcloud - gitweb
3d5a7f942304a056c42eb3ee89bf5da470979ed2
[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                                                     __mdd_fid_rec(env, 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         struct lu_fid_pack  *pack = &mdd_env_info(env)->mti_pack;
1402         int rc;
1403         ENTRY;
1404
1405         if (unlikely(mdd_is_dead_obj(mdd_obj)))
1406                 RETURN(-ESTALE);
1407
1408         rc = mdd_object_exists(mdd_obj);
1409         if (unlikely(rc == 0))
1410                 RETURN(-ESTALE);
1411         else if (unlikely(rc < 0)) {
1412                 CERROR("Object "DFID" locates on remote server\n",
1413                         PFID(mdo2fid(mdd_obj)));
1414                 LBUG();
1415         }
1416
1417         /* The common filename length check. */
1418         if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1419                 RETURN(-ENAMETOOLONG);
1420
1421         rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1422                                             MOR_TGT_PARENT);
1423         if (rc)
1424                 RETURN(rc);
1425
1426         if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1427                    dt_try_as_dir(env, dir))) {
1428
1429                 rc = dir->do_index_ops->dio_lookup(env, dir,
1430                                                  (struct dt_rec *)pack, key,
1431                                                  mdd_object_capa(env, mdd_obj));
1432                 if (rc > 0)
1433                         rc = fid_unpack(pack, fid);
1434                 else if (rc == 0)
1435                         rc = -ENOENT;
1436         } else
1437                 rc = -ENOTDIR;
1438
1439         RETURN(rc);
1440 }
1441
1442 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1443                           const struct lu_name *lname, struct mdd_object *child,
1444                           struct md_attr *ma, struct thandle *handle,
1445                           const struct md_op_spec *spec)
1446 {
1447         int rc;
1448         ENTRY;
1449
1450         /*
1451          * Update attributes for child.
1452          *
1453          * FIXME:
1454          *  (1) the valid bits should be converted between Lustre and Linux;
1455          *  (2) maybe, the child attributes should be set in OSD when creation.
1456          */
1457
1458         rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1459         if (rc != 0)
1460                 RETURN(rc);
1461
1462         if (S_ISDIR(ma->ma_attr.la_mode)) {
1463                 /* Add "." and ".." for newly created dir */
1464                 __mdd_ref_add(env, child, handle);
1465                 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1466                                              dot, handle, BYPASS_CAPA);
1467                 if (rc == 0)
1468                         rc = __mdd_index_insert_only(env, child, pfid,
1469                                                      dotdot, handle,
1470                                                      BYPASS_CAPA);
1471                 if (rc != 0)
1472                         __mdd_ref_del(env, child, handle, 1);
1473         }
1474         if (rc == 0)
1475                 mdd_links_add(env, child, pfid, lname, handle);
1476
1477         RETURN(rc);
1478 }
1479
1480 /* has not lock on pobj yet */
1481 static int mdd_create_sanity_check(const struct lu_env *env,
1482                                    struct md_object *pobj,
1483                                    const struct lu_name *lname,
1484                                    struct md_attr *ma,
1485                                    struct md_op_spec *spec)
1486 {
1487         struct mdd_thread_info *info = mdd_env_info(env);
1488         struct lu_attr    *la        = &info->mti_la;
1489         struct lu_fid     *fid       = &info->mti_fid;
1490         struct mdd_object *obj       = md2mdd_obj(pobj);
1491         struct mdd_device *m         = mdo2mdd(pobj);
1492         int lookup                   = spec->sp_cr_lookup;
1493         int rc;
1494         ENTRY;
1495
1496         /* EEXIST check */
1497         if (mdd_is_dead_obj(obj))
1498                 RETURN(-ENOENT);
1499
1500         /*
1501          * In some cases this lookup is not needed - we know before if name
1502          * exists or not because MDT performs lookup for it.
1503          * name length check is done in lookup.
1504          */
1505         if (lookup) {
1506                 /*
1507                  * Check if the name already exist, though it will be checked in
1508                  * _index_insert also, for avoiding rolling back if exists
1509                  * _index_insert.
1510                  */
1511                 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1512                                          MAY_WRITE | MAY_EXEC);
1513                 if (rc != -ENOENT)
1514                         RETURN(rc ? : -EEXIST);
1515         } else {
1516                 /*
1517                  * Check WRITE permission for the parent.
1518                  * EXEC permission have been checked
1519                  * when lookup before create already.
1520                  */
1521                 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1522                                                     MOR_TGT_PARENT);
1523                 if (rc)
1524                         RETURN(rc);
1525         }
1526
1527         /* sgid check */
1528         rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1529         if (rc != 0)
1530                 RETURN(rc);
1531
1532         if (la->la_mode & S_ISGID) {
1533                 ma->ma_attr.la_gid = la->la_gid;
1534                 if (S_ISDIR(ma->ma_attr.la_mode)) {
1535                         ma->ma_attr.la_mode |= S_ISGID;
1536                         ma->ma_attr.la_valid |= LA_MODE;
1537                 }
1538         }
1539
1540         switch (ma->ma_attr.la_mode & S_IFMT) {
1541         case S_IFLNK: {
1542                 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1543
1544                 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1545                         RETURN(-ENAMETOOLONG);
1546                 else
1547                         RETURN(0);
1548         }
1549         case S_IFDIR:
1550         case S_IFREG:
1551         case S_IFCHR:
1552         case S_IFBLK:
1553         case S_IFIFO:
1554         case S_IFSOCK:
1555                 rc = 0;
1556                 break;
1557         default:
1558                 rc = -EINVAL;
1559                 break;
1560         }
1561         RETURN(rc);
1562 }
1563
1564 /*
1565  * Create object and insert it into namespace.
1566  */
1567 static int mdd_create(const struct lu_env *env,
1568                       struct md_object *pobj,
1569                       const struct lu_name *lname,
1570                       struct md_object *child,
1571                       struct md_op_spec *spec,
1572                       struct md_attr* ma)
1573 {
1574         struct mdd_thread_info *info = mdd_env_info(env);
1575         struct lu_attr         *la = &info->mti_la_for_fix;
1576         struct md_attr         *ma_acl = &info->mti_ma;
1577         struct mdd_object      *mdd_pobj = md2mdd_obj(pobj);
1578         struct mdd_object      *son = md2mdd_obj(child);
1579         struct mdd_device      *mdd = mdo2mdd(pobj);
1580         struct lu_attr         *attr = &ma->ma_attr;
1581         struct lov_mds_md      *lmm = NULL;
1582         struct thandle         *handle;
1583         struct dynlock_handle  *dlh;
1584         const char             *name = lname->ln_name;
1585         int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1586         int got_def_acl = 0;
1587 #ifdef HAVE_QUOTA_SUPPORT
1588         struct obd_device *obd = mdd->mdd_obd_dev;
1589         struct mds_obd *mds = &obd->u.mds;
1590         unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1591         unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1592         int quota_opc = 0, block_count = 0;
1593         int inode_pending[MAXQUOTAS] = { 0, 0 };
1594         int block_pending[MAXQUOTAS] = { 0, 0 };
1595         int parent_pending[MAXQUOTAS] = { 0, 0 };
1596 #endif
1597         ENTRY;
1598
1599         /*
1600          * Two operations have to be performed:
1601          *
1602          *  - an allocation of a new object (->do_create()), and
1603          *
1604          *  - an insertion into a parent index (->dio_insert()).
1605          *
1606          * Due to locking, operation order is not important, when both are
1607          * successful, *but* error handling cases are quite different:
1608          *
1609          *  - if insertion is done first, and following object creation fails,
1610          *  insertion has to be rolled back, but this operation might fail
1611          *  also leaving us with dangling index entry.
1612          *
1613          *  - if creation is done first, is has to be undone if insertion
1614          *  fails, leaving us with leaked space, which is neither good, nor
1615          *  fatal.
1616          *
1617          * It seems that creation-first is simplest solution, but it is
1618          * sub-optimal in the frequent
1619          *
1620          *         $ mkdir foo
1621          *         $ mkdir foo
1622          *
1623          * case, because second mkdir is bound to create object, only to
1624          * destroy it immediately.
1625          *
1626          * To avoid this follow local file systems that do double lookup:
1627          *
1628          *     0. lookup -> -EEXIST (mdd_create_sanity_check())
1629          *
1630          *     1. create            (mdd_object_create_internal())
1631          *
1632          *     2. insert            (__mdd_index_insert(), lookup again)
1633          */
1634
1635         /* Sanity checks before big job. */
1636         rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1637         if (rc)
1638                 RETURN(rc);
1639
1640 #ifdef HAVE_QUOTA_SUPPORT
1641         if (mds->mds_quota) {
1642                 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1643
1644                 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1645                 if (!rc) {
1646                         int same = 0;
1647
1648                         quota_opc = FSFILT_OP_CREATE;
1649                         mdd_quota_wrapper(&ma->ma_attr, qcids);
1650                         mdd_quota_wrapper(la_tmp, qpids);
1651                         /* get file quota for child */
1652                         lquota_chkquota(mds_quota_interface_ref, obd, qcids,
1653                                         inode_pending, 1, NULL, 0, NULL, 0);
1654                         switch (ma->ma_attr.la_mode & S_IFMT) {
1655                         case S_IFLNK:
1656                         case S_IFDIR:
1657                                 block_count = 2;
1658                                 break;
1659                         case S_IFREG:
1660                                 block_count = 1;
1661                                 break;
1662                         }
1663                         if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1664                             qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1665                                 block_count += 1;
1666                                 same = 1;
1667                         }
1668                         /* get block quota for child and parent */
1669                         if (block_count)
1670                                 lquota_chkquota(mds_quota_interface_ref, obd,
1671                                                 qcids, block_pending,
1672                                                 block_count, NULL,
1673                                                 LQUOTA_FLAGS_BLK, NULL, 0);
1674                         if (!same)
1675                                 lquota_chkquota(mds_quota_interface_ref, obd,
1676                                                 qpids, parent_pending, 1, NULL,
1677                                                 LQUOTA_FLAGS_BLK, NULL, 0);
1678                 }
1679         }
1680 #endif
1681
1682         /*
1683          * No RPC inside the transaction, so OST objects should be created at
1684          * first.
1685          */
1686         if (S_ISREG(attr->la_mode)) {
1687                 lmm_size = ma->ma_lmm_size;
1688                 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1689                                     spec, attr);
1690                 if (rc)
1691                         GOTO(out_pending, rc);
1692         }
1693
1694         if (!S_ISLNK(attr->la_mode)) {
1695                 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
1696                 ma_acl->ma_acl = info->mti_xattr_buf;
1697                 ma_acl->ma_need = MA_ACL_DEF;
1698                 ma_acl->ma_valid = 0;
1699
1700                 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1701                 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
1702                 mdd_read_unlock(env, mdd_pobj);
1703                 if (rc)
1704                         GOTO(out_free, rc);
1705                 else if (ma_acl->ma_valid & MA_ACL_DEF)
1706                         got_def_acl = 1;
1707         }
1708
1709         mdd_txn_param_build(env, mdd, MDD_TXN_MKDIR_OP);
1710         handle = mdd_trans_start(env, mdd);
1711         if (IS_ERR(handle))
1712                 GOTO(out_free, rc = PTR_ERR(handle));
1713
1714         dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1715         if (dlh == NULL)
1716                 GOTO(out_trans, rc = -ENOMEM);
1717
1718         mdd_write_lock(env, son, MOR_TGT_CHILD);
1719         rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
1720         if (rc) {
1721                 mdd_write_unlock(env, son);
1722                 GOTO(cleanup, rc);
1723         }
1724
1725         created = 1;
1726
1727 #ifdef CONFIG_FS_POSIX_ACL
1728         if (got_def_acl) {
1729                 struct lu_buf *acl_buf = &info->mti_buf;
1730                 acl_buf->lb_buf = ma_acl->ma_acl;
1731                 acl_buf->lb_len = ma_acl->ma_acl_size;
1732
1733                 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1734                 if (rc) {
1735                         mdd_write_unlock(env, son);
1736                         GOTO(cleanup, rc);
1737                 } else {
1738                         ma->ma_attr.la_valid |= LA_MODE;
1739                 }
1740         }
1741 #endif
1742
1743         rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1744                                    son, ma, handle, spec);
1745         mdd_write_unlock(env, son);
1746         if (rc)
1747                 /*
1748                  * Object has no links, so it will be destroyed when last
1749                  * reference is released. (XXX not now.)
1750                  */
1751                 GOTO(cleanup, rc);
1752
1753         initialized = 1;
1754
1755         rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1756                                 name, S_ISDIR(attr->la_mode), handle,
1757                                 mdd_object_capa(env, mdd_pobj));
1758
1759         if (rc)
1760                 GOTO(cleanup, rc);
1761
1762         inserted = 1;
1763
1764         /* No need mdd_lsm_sanity_check here */
1765         rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1766         if (rc) {
1767                 CERROR("error on stripe info copy %d \n", rc);
1768                 GOTO(cleanup, rc);
1769         }
1770         if (lmm && lmm_size > 0) {
1771                 /* Set Lov here, do not get lmm again later */
1772                 memcpy(ma->ma_lmm, lmm, lmm_size);
1773                 ma->ma_lmm_size = lmm_size;
1774                 ma->ma_valid |= MA_LOV;
1775         }
1776
1777         if (S_ISLNK(attr->la_mode)) {
1778                 struct md_ucred  *uc = md_ucred(env);
1779                 struct dt_object *dt = mdd_object_child(son);
1780                 const char *target_name = spec->u.sp_symname;
1781                 int sym_len = strlen(target_name);
1782                 const struct lu_buf *buf;
1783                 loff_t pos = 0;
1784
1785                 buf = mdd_buf_get_const(env, target_name, sym_len);
1786                 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1787                                                 mdd_object_capa(env, son),
1788                                                 uc->mu_cap &
1789                                                 CFS_CAP_SYS_RESOURCE_MASK);
1790
1791                 if (rc == sym_len)
1792                         rc = 0;
1793                 else
1794                         GOTO(cleanup, rc = -EFAULT);
1795         }
1796
1797         *la = ma->ma_attr;
1798         la->la_valid = LA_CTIME | LA_MTIME;
1799         rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1800         if (rc)
1801                 GOTO(cleanup, rc);
1802
1803         /* Return attr back. */
1804         rc = mdd_attr_get_internal_locked(env, son, ma);
1805         EXIT;
1806 cleanup:
1807         if (rc && created) {
1808                 int rc2 = 0;
1809
1810                 if (inserted) {
1811                         rc2 = __mdd_index_delete(env, mdd_pobj, name,
1812                                                  S_ISDIR(attr->la_mode),
1813                                                  handle, BYPASS_CAPA);
1814                         if (rc2)
1815                                 CERROR("error can not cleanup destroy %d\n",
1816                                        rc2);
1817                 }
1818
1819                 if (rc2 == 0) {
1820                         mdd_write_lock(env, son, MOR_TGT_CHILD);
1821                         __mdd_ref_del(env, son, handle, 0);
1822                         if (initialized && S_ISDIR(attr->la_mode))
1823                                 __mdd_ref_del(env, son, handle, 1);
1824                         mdd_write_unlock(env, son);
1825                 }
1826         }
1827
1828         /* update lov_objid data, must be before transaction stop! */
1829         if (rc == 0)
1830                 mdd_lov_objid_update(mdd, lmm);
1831
1832         mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1833 out_trans:
1834         if (rc == 0)
1835                 rc = mdd_changelog_ns_store(env, mdd,
1836                             S_ISDIR(attr->la_mode) ? CL_MKDIR :
1837                             S_ISREG(attr->la_mode) ? CL_CREATE :
1838                             S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1839                             son, mdd_pobj, NULL, lname, handle);
1840         mdd_trans_stop(env, mdd, rc, handle);
1841 out_free:
1842         /* finis lov_create stuff, free all temporary data */
1843         mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1844 out_pending:
1845 #ifdef HAVE_QUOTA_SUPPORT
1846         if (quota_opc) {
1847                 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1848                                       inode_pending, 0);
1849                 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1850                                       block_pending, 1);
1851                 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
1852                                       parent_pending, 1);
1853                 /* Trigger dqacq on the owner of child and parent. If failed,
1854                  * the next call for lquota_chkquota will process it. */
1855                 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1856                               quota_opc);
1857         }
1858 #endif
1859         return rc;
1860 }
1861
1862 /*
1863  * Get locks on parents in proper order
1864  * RETURN: < 0 - error, rename_order if successful
1865  */
1866 enum rename_order {
1867         MDD_RN_SAME,
1868         MDD_RN_SRCTGT,
1869         MDD_RN_TGTSRC
1870 };
1871
1872 static int mdd_rename_order(const struct lu_env *env,
1873                             struct mdd_device *mdd,
1874                             struct mdd_object *src_pobj,
1875                             struct mdd_object *tgt_pobj)
1876 {
1877         /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1878         int rc;
1879         ENTRY;
1880
1881         if (src_pobj == tgt_pobj)
1882                 RETURN(MDD_RN_SAME);
1883
1884         /* compared the parent child relationship of src_p&tgt_p */
1885         if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1886                 rc = MDD_RN_SRCTGT;
1887         } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1888                 rc = MDD_RN_TGTSRC;
1889         } else {
1890                 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1891                 if (rc == -EREMOTE)
1892                         rc = 0;
1893
1894                 if (rc == 1)
1895                         rc = MDD_RN_TGTSRC;
1896                 else if (rc == 0)
1897                         rc = MDD_RN_SRCTGT;
1898         }
1899
1900         RETURN(rc);
1901 }
1902
1903 /* has not mdd_write{read}_lock on any obj yet. */
1904 static int mdd_rename_sanity_check(const struct lu_env *env,
1905                                    struct mdd_object *src_pobj,
1906                                    struct mdd_object *tgt_pobj,
1907                                    struct mdd_object *sobj,
1908                                    struct mdd_object *tobj,
1909                                    struct md_attr *ma)
1910 {
1911         int rc = 0;
1912         ENTRY;
1913
1914         if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1915                 RETURN(0);
1916
1917         /* XXX: when get here, sobj must NOT be NULL,
1918          * the other case has been processed in cml_rename
1919          * before mdd_rename and enable MDS_PERM_BYPASS. */
1920         LASSERT(sobj);
1921
1922         rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
1923         if (rc)
1924                 RETURN(rc);
1925
1926         /* XXX: when get here, "tobj == NULL" means tobj must
1927          * NOT exist (neither on remote MDS, such case has been
1928          * processed in cml_rename before mdd_rename and enable
1929          * MDS_PERM_BYPASS).
1930          * So check may_create, but not check may_unlink. */
1931         if (!tobj)
1932                 rc = mdd_may_create(env, tgt_pobj, NULL,
1933                                     (src_pobj != tgt_pobj), 0);
1934         else
1935                 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
1936                                     (src_pobj != tgt_pobj), 1);
1937
1938         if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1939             S_ISDIR(ma->ma_attr.la_mode))
1940                 rc = __mdd_may_link(env, tgt_pobj);
1941
1942         RETURN(rc);
1943 }
1944
1945 /* src object can be remote that is why we use only fid and type of object */
1946 static int mdd_rename(const struct lu_env *env,
1947                       struct md_object *src_pobj, struct md_object *tgt_pobj,
1948                       const struct lu_fid *lf, const struct lu_name *lsname,
1949                       struct md_object *tobj, const struct lu_name *ltname,
1950                       struct md_attr *ma)
1951 {
1952         const char *sname = lsname->ln_name;
1953         const char *tname = ltname->ln_name;
1954         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
1955         struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
1956         struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1957         struct mdd_device *mdd = mdo2mdd(src_pobj);
1958         struct mdd_object *mdd_sobj = NULL;                  /* source object */
1959         struct mdd_object *mdd_tobj = NULL;
1960         struct dynlock_handle *sdlh, *tdlh;
1961         struct thandle *handle;
1962         const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
1963         const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
1964         int is_dir;
1965         int rc, rc2;
1966
1967 #ifdef HAVE_QUOTA_SUPPORT
1968         struct obd_device *obd = mdd->mdd_obd_dev;
1969         struct mds_obd *mds = &obd->u.mds;
1970         unsigned int qspids[MAXQUOTAS] = { 0, 0 };
1971         unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
1972         unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
1973         int quota_copc = 0, quota_popc = 0;
1974         int rec_pending[MAXQUOTAS] = { 0, 0 };
1975 #endif
1976         ENTRY;
1977
1978         LASSERT(ma->ma_attr.la_mode & S_IFMT);
1979         is_dir = S_ISDIR(ma->ma_attr.la_mode);
1980
1981         if (tobj)
1982                 mdd_tobj = md2mdd_obj(tobj);
1983
1984 #ifdef HAVE_QUOTA_SUPPORT
1985         if (mds->mds_quota) {
1986                 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1987
1988                 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
1989                 if (!rc) {
1990                         mdd_quota_wrapper(la_tmp, qspids);
1991                         if (!tobj) {
1992                                 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
1993                                                 BYPASS_CAPA);
1994                                 if (!rc) {
1995                                         void *data = NULL;
1996                                         mdd_data_get(env, mdd_tpobj, &data);
1997                                         quota_popc = FSFILT_OP_LINK;
1998                                         mdd_quota_wrapper(la_tmp, qtpids);
1999                                         /* get block quota for target parent */
2000                                         lquota_chkquota(mds_quota_interface_ref,
2001                                                         obd, qtpids,
2002                                                         rec_pending, 1, NULL,
2003                                                         LQUOTA_FLAGS_BLK,
2004                                                         data, 1);
2005                                 }
2006                         }
2007                 }
2008         }
2009 #endif
2010         mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_OP);
2011         handle = mdd_trans_start(env, mdd);
2012         if (IS_ERR(handle))
2013                 GOTO(out_pending, rc = PTR_ERR(handle));
2014
2015         /* FIXME: Should consider tobj and sobj too in rename_lock. */
2016         rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2017         if (rc < 0)
2018                 GOTO(cleanup_unlocked, rc);
2019
2020         /* Get locks in determined order */
2021         if (rc == MDD_RN_SAME) {
2022                 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2023                                           sname, MOR_SRC_PARENT);
2024                 /* check hashes to determine do we need one lock or two */
2025                 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2026                         tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2027                                 MOR_TGT_PARENT);
2028                 else
2029                         tdlh = sdlh;
2030         } else if (rc == MDD_RN_SRCTGT) {
2031                 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2032                 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2033         } else {
2034                 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2035                 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2036         }
2037         if (sdlh == NULL || tdlh == NULL)
2038                 GOTO(cleanup, rc = -ENOMEM);
2039
2040         mdd_sobj = mdd_object_find(env, mdd, lf);
2041         rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2042                                      mdd_sobj, mdd_tobj, ma);
2043         if (rc)
2044                 GOTO(cleanup, rc);
2045
2046         /* Remove source name from source directory */
2047         rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2048                                 mdd_object_capa(env, mdd_spobj));
2049         if (rc)
2050                 GOTO(cleanup, rc);
2051
2052         /* "mv dir1 dir2" needs "dir1/.." link update */
2053         if (is_dir && mdd_sobj) {
2054                 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2055                                         mdd_object_capa(env, mdd_sobj));
2056                 if (rc)
2057                         GOTO(fixup_spobj2, rc);
2058
2059                 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2060                                       handle, mdd_object_capa(env, mdd_sobj));
2061                 if (rc) {
2062                         GOTO(fixup_spobj, rc);
2063                 }
2064         }
2065
2066         /* Remove target name from target directory
2067          * Here tobj can be remote one, so we do index_delete unconditionally
2068          * and -ENOENT is allowed.
2069          */
2070         rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2071                                 mdd_object_capa(env, mdd_tpobj));
2072         if (rc != 0) {
2073                 if (mdd_tobj) {
2074                         /* tname might been renamed to something else */
2075                         GOTO(fixup_spobj, rc);
2076                 }
2077                 if (rc != -ENOENT)
2078                         GOTO(fixup_spobj, rc);
2079         }
2080
2081         /* Insert new fid with target name into target dir */
2082         rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2083                                 mdd_object_capa(env, mdd_tpobj));
2084         if (rc)
2085                 GOTO(fixup_tpobj, rc);
2086
2087         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2088         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2089
2090         /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2091         if (mdd_sobj) {
2092                 la->la_valid = LA_CTIME;
2093                 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2094                                                         handle, 0);
2095                 if (rc)
2096                         GOTO(fixup_tpobj, rc);
2097         }
2098
2099         /* Remove old target object
2100          * For tobj is remote case cmm layer has processed
2101          * and set tobj to NULL then. So when tobj is NOT NULL,
2102          * it must be local one.
2103          */
2104         if (tobj && mdd_object_exists(mdd_tobj)) {
2105                 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2106                 if (mdd_is_dead_obj(mdd_tobj)) {
2107                         mdd_write_unlock(env, mdd_tobj);
2108                         /* shld not be dead, something is wrong */
2109                         CERROR("tobj is dead, something is wrong\n");
2110                         rc = -EINVAL;
2111                         goto cleanup;
2112                 }
2113                 __mdd_ref_del(env, mdd_tobj, handle, 0);
2114
2115                 /* Remove dot reference. */
2116                 if (is_dir)
2117                         __mdd_ref_del(env, mdd_tobj, handle, 1);
2118
2119                 la->la_valid = LA_CTIME;
2120                 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2121                 if (rc)
2122                         GOTO(fixup_tpobj, rc);
2123
2124                 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2125                 mdd_write_unlock(env, mdd_tobj);
2126                 if (rc)
2127                         GOTO(fixup_tpobj, rc);
2128
2129 #ifdef HAVE_QUOTA_SUPPORT
2130                 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2131                     ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2132                         quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2133                         mdd_quota_wrapper(&ma->ma_attr, qtcids);
2134                 }
2135 #endif
2136         }
2137
2138         la->la_valid = LA_CTIME | LA_MTIME;
2139         rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2140         if (rc)
2141                 GOTO(fixup_tpobj, rc);
2142
2143         if (mdd_spobj != mdd_tpobj) {
2144                 la->la_valid = LA_CTIME | LA_MTIME;
2145                 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2146                                                   handle, 0);
2147         }
2148
2149         if (rc == 0 && mdd_sobj) {
2150                 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2151                 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2152                                       mdo2fid(mdd_tpobj), ltname, handle);
2153                 if (rc == -ENOENT)
2154                         /* Old files might not have EA entry */
2155                         mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2156                                       lsname, handle);
2157                 mdd_write_unlock(env, mdd_sobj);
2158                 /* We don't fail the transaction if the link ea can't be
2159                    updated -- fid2path will use alternate lookup method. */
2160                 rc = 0;
2161         }
2162
2163         EXIT;
2164
2165 fixup_tpobj:
2166         if (rc) {
2167                 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2168                                          BYPASS_CAPA);
2169                 if (rc2)
2170                         CWARN("tp obj fix error %d\n",rc2);
2171
2172                 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2173                     !mdd_is_dead_obj(mdd_tobj)) {
2174                         rc2 = __mdd_index_insert(env, mdd_tpobj,
2175                                          mdo2fid(mdd_tobj), tname,
2176                                          is_dir, handle,
2177                                          BYPASS_CAPA);
2178
2179                         if (rc2)
2180                                 CWARN("tp obj fix error %d\n",rc2);
2181                 }
2182         }
2183
2184 fixup_spobj:
2185         if (rc && is_dir && mdd_sobj) {
2186                 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2187                                               BYPASS_CAPA);
2188
2189                 if (rc2)
2190                         CWARN("sp obj dotdot delete error %d\n",rc2);
2191
2192
2193                 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2194                                               dotdot, handle, BYPASS_CAPA);
2195                 if (rc2)
2196                         CWARN("sp obj dotdot insert error %d\n",rc2);
2197         }
2198
2199 fixup_spobj2:
2200         if (rc) {
2201                 rc2 = __mdd_index_insert(env, mdd_spobj,
2202                                          lf, sname, is_dir, handle, BYPASS_CAPA);
2203                 if (rc2)
2204                         CWARN("sp obj fix error %d\n",rc2);
2205         }
2206 cleanup:
2207         if (likely(tdlh) && sdlh != tdlh)
2208                 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2209         if (likely(sdlh))
2210                 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2211 cleanup_unlocked:
2212         if (rc == 0)
2213                 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, mdd_tobj,
2214                                             mdd_spobj, lf, lsname, handle);
2215         if (rc == 0)
2216                 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, mdd_tobj,
2217                                             mdd_tpobj, lf, ltname, handle);
2218
2219         mdd_trans_stop(env, mdd, rc, handle);
2220         if (mdd_sobj)
2221                 mdd_object_put(env, mdd_sobj);
2222 out_pending:
2223 #ifdef HAVE_QUOTA_SUPPORT
2224         if (mds->mds_quota) {
2225                 if (quota_popc)
2226                         lquota_pending_commit(mds_quota_interface_ref, obd,
2227                                               qtpids, rec_pending, 1);
2228
2229                 if (quota_copc) {
2230                         /* Trigger dqrel on the source owner of parent.
2231                          * If failed, the next call for lquota_chkquota will
2232                          * process it. */
2233                         lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2234                                       FSFILT_OP_UNLINK_PARTIAL_PARENT);
2235
2236                         /* Trigger dqrel on the target owner of child.
2237                          * If failed, the next call for lquota_chkquota
2238                          * will process it. */
2239                         lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2240                                       qtpids, rc, quota_copc);
2241                 }
2242         }
2243 #endif
2244         return rc;
2245 }
2246
2247 /** enable/disable storing of hardlink info */
2248 int mdd_linkea_enable = 1;
2249 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2250                 "record hardlink info in EAs");
2251
2252 /** Read the link EA into a temp buffer.
2253  * Uses the name_buf since it is generally large.
2254  * \retval IS_ERR err
2255  * \retval ptr to \a lu_buf (always \a mti_big_buf)
2256  */
2257 struct lu_buf *mdd_links_get(const struct lu_env *env,
2258                              struct mdd_object *mdd_obj)
2259 {
2260         struct lu_buf *buf;
2261         struct lustre_capa *capa;
2262         struct link_ea_header *leh;
2263         int rc;
2264
2265         /* First try a small buf */
2266         buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2267         if (buf->lb_buf == NULL)
2268                 return ERR_PTR(-ENOMEM);
2269
2270         capa = mdd_object_capa(env, mdd_obj);
2271         rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2272         if (rc == -ERANGE) {
2273                 /* Buf was too small, figure out what we need. */
2274                 buf->lb_buf = NULL;
2275                 buf->lb_len = 0;
2276                 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2277                 if (rc < 0)
2278                         return ERR_PTR(rc);
2279                 buf = mdd_buf_alloc(env, rc);
2280                 if (buf->lb_buf == NULL)
2281                         return ERR_PTR(-ENOMEM);
2282                 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2283         }
2284         if (rc < 0)
2285                 return ERR_PTR(rc);
2286
2287         leh = buf->lb_buf;
2288         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2289                 leh->leh_magic = LINK_EA_MAGIC;
2290                 leh->leh_reccount = __swab32(leh->leh_reccount);
2291                 leh->leh_len = __swab64(leh->leh_len);
2292                 /* entries are swabbed by mdd_lee_unpack */
2293         }
2294         if (leh->leh_magic != LINK_EA_MAGIC)
2295                 return ERR_PTR(-EINVAL);
2296         if (leh->leh_reccount == 0)
2297                 return ERR_PTR(-ENODATA);
2298
2299         return buf;
2300 }
2301
2302 /** Pack a link_ea_entry.
2303  * All elements are stored as chars to avoid alignment issues.
2304  * Numbers are always big-endian
2305  * \param packbuf is a temp fid buffer
2306  * \retval record length
2307  */
2308 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2309                          const struct lu_fid *pfid, struct lu_fid* packbuf)
2310 {
2311         char *ptr;
2312         int reclen;
2313
2314         fid_pack(&lee->lee_parent_fid, pfid, packbuf);
2315         ptr = (char *)&lee->lee_parent_fid + lee->lee_parent_fid.fp_len;
2316         strncpy(ptr, lname->ln_name, lname->ln_namelen);
2317         reclen = lee->lee_parent_fid.fp_len + lname->ln_namelen +
2318                 sizeof(lee->lee_reclen);
2319         lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2320         lee->lee_reclen[1] = reclen & 0xff;
2321         return reclen;
2322 }
2323
2324 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2325                     struct lu_name *lname, struct lu_fid *pfid)
2326 {
2327         *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2328         fid_unpack(&lee->lee_parent_fid, pfid);
2329         lname->ln_name = (char *)&lee->lee_parent_fid +
2330                 lee->lee_parent_fid.fp_len;
2331         lname->ln_namelen = *reclen - lee->lee_parent_fid.fp_len -
2332                 sizeof(lee->lee_reclen);
2333 }
2334
2335 /** Add a record to the end of link ea buf */
2336 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2337                            const struct lu_fid *pfid,
2338                            const struct lu_name *lname)
2339 {
2340         struct link_ea_header *leh;
2341         struct link_ea_entry *lee;
2342         int reclen;
2343
2344         if (lname == NULL || pfid == NULL)
2345                 return -EINVAL;
2346
2347         /* Make sure our buf is big enough for the new one */
2348         leh = buf->lb_buf;
2349         reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2350         if (leh->leh_len + reclen > buf->lb_len) {
2351                 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2352                         return -ENOMEM;
2353         }
2354
2355         leh = buf->lb_buf;
2356         lee = buf->lb_buf + leh->leh_len;
2357         reclen = mdd_lee_pack(lee, lname, pfid, &mdd_env_info(env)->mti_fid2);
2358         leh->leh_len += reclen;
2359         leh->leh_reccount++;
2360         return 0;
2361 }
2362
2363 /* For pathologic linkers, we don't want to spend lots of time scanning the
2364  * link ea.  Limit ourseleves to something reasonable; links not in the EA
2365  * can be looked up via (slower) parent lookup.
2366  */
2367 #define LINKEA_MAX_COUNT 128
2368
2369 static int mdd_links_add(const struct lu_env *env,
2370                          struct mdd_object *mdd_obj,
2371                          const struct lu_fid *pfid,
2372                          const struct lu_name *lname,
2373                          struct thandle *handle)
2374 {
2375         struct lu_buf *buf;
2376         struct link_ea_header *leh;
2377         int rc;
2378         ENTRY;
2379
2380         if (!mdd_linkea_enable)
2381                 RETURN(0);
2382
2383         buf = mdd_links_get(env, mdd_obj);
2384         if (IS_ERR(buf)) {
2385                 rc = PTR_ERR(buf);
2386                 if (rc != -ENODATA) {
2387                         CERROR("link_ea read failed %d "DFID"\n", rc,
2388                                PFID(mdd_object_fid(mdd_obj)));
2389                         RETURN (rc);
2390                 }
2391                 /* empty EA; start one */
2392                 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2393                 if (buf->lb_buf == NULL)
2394                         RETURN(-ENOMEM);
2395                 leh = buf->lb_buf;
2396                 leh->leh_magic = LINK_EA_MAGIC;
2397                 leh->leh_len = sizeof(struct link_ea_header);
2398                 leh->leh_reccount = 0;
2399         }
2400
2401         leh = buf->lb_buf;
2402         if (leh->leh_reccount > LINKEA_MAX_COUNT)
2403                 RETURN(-EOVERFLOW);
2404
2405         rc = __mdd_links_add(env, buf, pfid, lname);
2406         if (rc)
2407                 RETURN(rc);
2408
2409         leh = buf->lb_buf;
2410         rc = __mdd_xattr_set(env, mdd_obj,
2411                              mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2412                              XATTR_NAME_LINK, 0, handle);
2413         if (rc)
2414                 CERROR("link_ea add failed %d "DFID"\n", rc,
2415                        PFID(mdd_object_fid(mdd_obj)));
2416
2417         if (buf->lb_vmalloc)
2418                 /* if we vmalloced a large buffer drop it */
2419                 mdd_buf_put(buf);
2420
2421         RETURN (rc);
2422 }
2423
2424 static int mdd_links_rename(const struct lu_env *env,
2425                             struct mdd_object *mdd_obj,
2426                             const struct lu_fid *oldpfid,
2427                             const struct lu_name *oldlname,
2428                             const struct lu_fid *newpfid,
2429                             const struct lu_name *newlname,
2430                             struct thandle *handle)
2431 {
2432         struct lu_buf  *buf;
2433         struct link_ea_header *leh;
2434         struct link_ea_entry  *lee;
2435         struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2436         struct lu_fid  *tmpfid = &mdd_env_info(env)->mti_fid;
2437         int reclen = 0;
2438         int count;
2439         int rc, rc2 = 0;
2440         ENTRY;
2441
2442         if (!mdd_linkea_enable)
2443                 RETURN(0);
2444
2445         if (mdd_obj->mod_flags & DEAD_OBJ)
2446                 /* No more links, don't bother */
2447                 RETURN(0);
2448
2449         buf = mdd_links_get(env, mdd_obj);
2450         if (IS_ERR(buf)) {
2451                 rc = PTR_ERR(buf);
2452                 CERROR("link_ea read failed %d "DFID"\n",
2453                        rc, PFID(mdd_object_fid(mdd_obj)));
2454                 RETURN(rc);
2455         }
2456         leh = buf->lb_buf;
2457         lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2458
2459         /* Find the old record */
2460         for(count = 0; count <= leh->leh_reccount; count++) {
2461                 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2462                 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2463                     lu_fid_eq(tmpfid, oldpfid) &&
2464                     (strncmp(tmpname->ln_name, oldlname->ln_name,
2465                              tmpname->ln_namelen) == 0))
2466                         break;
2467                 lee = (struct link_ea_entry *)((char *)lee + reclen);
2468         }
2469         if (count > leh->leh_reccount) {
2470                 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2471                        oldlname->ln_namelen, oldlname->ln_name);
2472                 GOTO(out, rc = -ENOENT);
2473         }
2474
2475         /* Remove the old record */
2476         leh->leh_reccount--;
2477         leh->leh_len -= reclen;
2478         memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2479                 (char *)lee);
2480
2481         /* If renaming, add the new record */
2482         if (newpfid != NULL) {
2483                 /* if the add fails, we still delete the out-of-date old link */
2484                 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2485                 leh = buf->lb_buf;
2486         }
2487
2488         rc = __mdd_xattr_set(env, mdd_obj,
2489                              mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2490                              XATTR_NAME_LINK, 0, handle);
2491
2492 out:
2493         if (rc == 0)
2494                 rc = rc2;
2495         if (rc)
2496                 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2497                        oldlname->ln_namelen, oldlname->ln_name, rc,
2498                        PFID(mdd_object_fid(mdd_obj)));
2499
2500         if (buf->lb_vmalloc)
2501                 /* if we vmalloced a large buffer drop it */
2502                 mdd_buf_put(buf);
2503
2504         RETURN (rc);
2505 }
2506
2507 const struct md_dir_operations mdd_dir_ops = {
2508         .mdo_is_subdir     = mdd_is_subdir,
2509         .mdo_lookup        = mdd_lookup,
2510         .mdo_create        = mdd_create,
2511         .mdo_rename        = mdd_rename,
2512         .mdo_link          = mdd_link,
2513         .mdo_unlink        = mdd_unlink,
2514         .mdo_name_insert   = mdd_name_insert,
2515         .mdo_name_remove   = mdd_name_remove,
2516         .mdo_rename_tgt    = mdd_rename_tgt,
2517         .mdo_create_data   = mdd_create_data
2518 };