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