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