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