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