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