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