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