Whamcloud - gitweb
LU-365 Update copyright for files modified by Whamcloud
[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);
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);
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);
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);
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         mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_TGT_OP);
1244         handle = mdd_trans_start(env, mdd);
1245         if (IS_ERR(handle))
1246                 GOTO(out_pending, rc = PTR_ERR(handle));
1247
1248         dlh = mdd_pdo_write_lock(env, mdd_tpobj, name, MOR_TGT_PARENT);
1249         if (dlh == NULL)
1250                 GOTO(out_trans, rc = -ENOMEM);
1251         if (tobj)
1252                 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
1253
1254         rc = mdd_rt_sanity_check(env, mdd_tpobj, mdd_tobj, ma);
1255         if (rc)
1256                 GOTO(cleanup, rc);
1257
1258         /*
1259          * If rename_tgt is called then we should just re-insert name with
1260          * correct fid, no need to dec/inc parent nlink if obj is dir.
1261          */
1262         rc = __mdd_index_delete(env, mdd_tpobj, name, 0, handle, BYPASS_CAPA);
1263         if (rc)
1264                 GOTO(cleanup, rc);
1265
1266         rc = __mdd_index_insert_only(env, mdd_tpobj, lf, name, handle,
1267                                      BYPASS_CAPA);
1268         if (rc)
1269                 GOTO(cleanup, rc);
1270
1271         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1272         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1273
1274         la->la_valid = LA_CTIME | LA_MTIME;
1275         rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la, handle, 0);
1276         if (rc)
1277                 GOTO(cleanup, rc);
1278
1279         /*
1280          * For tobj is remote case cmm layer has processed
1281          * and pass NULL tobj to here. So when tobj is NOT NULL,
1282          * it must be local one.
1283          */
1284         if (tobj && mdd_object_exists(mdd_tobj)) {
1285                 __mdd_ref_del(env, mdd_tobj, handle, 0);
1286
1287                 /* Remove dot reference. */
1288                 if (S_ISDIR(ma->ma_attr.la_mode))
1289                         __mdd_ref_del(env, mdd_tobj, handle, 1);
1290
1291                 la->la_valid = LA_CTIME;
1292                 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
1293                 if (rc)
1294                         GOTO(cleanup, rc);
1295
1296                 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
1297                 if (rc)
1298                         GOTO(cleanup, rc);
1299
1300 #ifdef HAVE_QUOTA_SUPPORT
1301                 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
1302                     ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
1303                         quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
1304                         mdd_quota_wrapper(&ma->ma_attr, qcids);
1305                 }
1306 #endif
1307         }
1308         EXIT;
1309 cleanup:
1310         if (tobj)
1311                 mdd_write_unlock(env, mdd_tobj);
1312         mdd_pdo_write_unlock(env, mdd_tpobj, dlh);
1313 out_trans:
1314         if (rc == 0)
1315                 /* Bare EXT record with no RENAME in front of it signifies
1316                    a partial slave op */
1317                 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
1318                                             mdd_tpobj, NULL, lname, handle);
1319
1320         mdd_trans_stop(env, mdd, rc, handle);
1321 out_pending:
1322 #ifdef HAVE_QUOTA_SUPPORT
1323         if (mds->mds_quota) {
1324                 if (quota_popc)
1325                         lquota_pending_commit(mds_quota_interface_ref, obd,
1326                                               qpids, rec_pending, 1);
1327
1328                 if (quota_copc)
1329                         /* Trigger dqrel on the target owner of child.
1330                          * If failed, the next call for lquota_chkquota
1331                          * will process it. */
1332                         lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids,
1333                                       rc, quota_copc);
1334         }
1335 #endif
1336         return rc;
1337 }
1338
1339 /*
1340  * The permission has been checked when obj created, no need check again.
1341  */
1342 static int mdd_cd_sanity_check(const struct lu_env *env,
1343                                struct mdd_object *obj)
1344 {
1345         ENTRY;
1346
1347         /* EEXIST check */
1348         if (!obj || mdd_is_dead_obj(obj))
1349                 RETURN(-ENOENT);
1350
1351         RETURN(0);
1352
1353 }
1354
1355 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1356                            struct md_object *cobj, const struct md_op_spec *spec,
1357                            struct md_attr *ma)
1358 {
1359         struct mdd_device *mdd = mdo2mdd(cobj);
1360         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1361         struct mdd_object *son = md2mdd_obj(cobj);
1362         struct lu_attr    *attr = &ma->ma_attr;
1363         struct lov_mds_md *lmm = NULL;
1364         int                lmm_size = 0;
1365         struct thandle    *handle;
1366         int                rc;
1367         ENTRY;
1368
1369         rc = mdd_cd_sanity_check(env, son);
1370         if (rc)
1371                 RETURN(rc);
1372
1373         if (!md_should_create(spec->sp_cr_flags))
1374                 RETURN(0);
1375         lmm_size = ma->ma_lmm_size;
1376         rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1377                             spec, attr);
1378         if (rc)
1379                 RETURN(rc);
1380
1381         mdd_txn_param_build(env, mdd, MDD_TXN_CREATE_DATA_OP);
1382         handle = mdd_trans_start(env, mdd);
1383         if (IS_ERR(handle))
1384                 GOTO(out_free, rc = PTR_ERR(handle));
1385
1386         /*
1387          * XXX: Setting the lov ea is not locked but setting the attr is locked?
1388          * Should this be fixed?
1389          */
1390
1391         /* Replay creates has objects already */
1392 #if 0
1393         if (spec->no_create) {
1394                 CDEBUG(D_INFO, "we already have lov ea\n");
1395                 rc = mdd_lov_set_md(env, mdd_pobj, son,
1396                                     (struct lov_mds_md *)spec->u.sp_ea.eadata,
1397                                     spec->u.sp_ea.eadatalen, handle, 0);
1398         } else
1399 #endif
1400                 /* No need mdd_lsm_sanity_check here */
1401                 rc = mdd_lov_set_md(env, mdd_pobj, son, lmm,
1402                                     lmm_size, handle, 0);
1403
1404         if (rc == 0)
1405                rc = mdd_attr_get_internal_locked(env, son, ma);
1406
1407         /* update lov_objid data, must be before transaction stop! */
1408         if (rc == 0)
1409                 mdd_lov_objid_update(mdd, lmm);
1410
1411         mdd_trans_stop(env, mdd, rc, handle);
1412 out_free:
1413         /* Finish mdd_lov_create() stuff. */
1414         mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1415         RETURN(rc);
1416 }
1417
1418 /* Get fid from name and parent */
1419 static int
1420 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1421              const struct lu_name *lname, struct lu_fid* fid, int mask)
1422 {
1423         const char          *name = lname->ln_name;
1424         const struct dt_key *key = (const struct dt_key *)name;
1425         struct mdd_object   *mdd_obj = md2mdd_obj(pobj);
1426         struct mdd_device   *m = mdo2mdd(pobj);
1427         struct dt_object    *dir = mdd_object_child(mdd_obj);
1428         int rc;
1429         ENTRY;
1430
1431         if (unlikely(mdd_is_dead_obj(mdd_obj)))
1432                 RETURN(-ESTALE);
1433
1434         rc = mdd_object_exists(mdd_obj);
1435         if (unlikely(rc == 0))
1436                 RETURN(-ESTALE);
1437         else if (unlikely(rc < 0)) {
1438                 CERROR("Object "DFID" locates on remote server\n",
1439                         PFID(mdo2fid(mdd_obj)));
1440                 LBUG();
1441         }
1442
1443         /* The common filename length check. */
1444         if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1445                 RETURN(-ENAMETOOLONG);
1446
1447         rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1448                                             MOR_TGT_PARENT);
1449         if (rc)
1450                 RETURN(rc);
1451
1452         if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1453                    dt_try_as_dir(env, dir))) {
1454
1455                 rc = dir->do_index_ops->dio_lookup(env, dir,
1456                                                  (struct dt_rec *)fid, key,
1457                                                  mdd_object_capa(env, mdd_obj));
1458                 if (rc > 0)
1459                         rc = 0;
1460                 else if (rc == 0)
1461                         rc = -ENOENT;
1462         } else
1463                 rc = -ENOTDIR;
1464
1465         RETURN(rc);
1466 }
1467
1468 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1469                           const struct lu_name *lname, struct mdd_object *child,
1470                           struct md_attr *ma, struct thandle *handle,
1471                           const struct md_op_spec *spec)
1472 {
1473         int rc;
1474         ENTRY;
1475
1476         /*
1477          * Update attributes for child.
1478          *
1479          * FIXME:
1480          *  (1) the valid bits should be converted between Lustre and Linux;
1481          *  (2) maybe, the child attributes should be set in OSD when creation.
1482          */
1483
1484         rc = mdd_attr_set_internal(env, child, &ma->ma_attr, handle, 0);
1485         if (rc != 0)
1486                 RETURN(rc);
1487
1488         if (S_ISDIR(ma->ma_attr.la_mode)) {
1489                 /* Add "." and ".." for newly created dir */
1490                 __mdd_ref_add(env, child, handle);
1491                 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1492                                              dot, handle, BYPASS_CAPA);
1493                 if (rc == 0)
1494                         rc = __mdd_index_insert_only(env, child, pfid,
1495                                                      dotdot, handle,
1496                                                      BYPASS_CAPA);
1497                 if (rc != 0)
1498                         __mdd_ref_del(env, child, handle, 1);
1499         }
1500         if (rc == 0)
1501                 mdd_links_add(env, child, pfid, lname, handle, 1);
1502
1503         RETURN(rc);
1504 }
1505
1506 /* has not lock on pobj yet */
1507 static int mdd_create_sanity_check(const struct lu_env *env,
1508                                    struct md_object *pobj,
1509                                    const struct lu_name *lname,
1510                                    struct md_attr *ma,
1511                                    struct md_op_spec *spec)
1512 {
1513         struct mdd_thread_info *info = mdd_env_info(env);
1514         struct lu_attr    *la        = &info->mti_la;
1515         struct lu_fid     *fid       = &info->mti_fid;
1516         struct mdd_object *obj       = md2mdd_obj(pobj);
1517         struct mdd_device *m         = mdo2mdd(pobj);
1518         int lookup                   = spec->sp_cr_lookup;
1519         int rc;
1520         ENTRY;
1521
1522         /* EEXIST check */
1523         if (mdd_is_dead_obj(obj))
1524                 RETURN(-ENOENT);
1525
1526         /*
1527          * In some cases this lookup is not needed - we know before if name
1528          * exists or not because MDT performs lookup for it.
1529          * name length check is done in lookup.
1530          */
1531         if (lookup) {
1532                 /*
1533                  * Check if the name already exist, though it will be checked in
1534                  * _index_insert also, for avoiding rolling back if exists
1535                  * _index_insert.
1536                  */
1537                 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1538                                          MAY_WRITE | MAY_EXEC);
1539                 if (rc != -ENOENT)
1540                         RETURN(rc ? : -EEXIST);
1541         } else {
1542                 /*
1543                  * Check WRITE permission for the parent.
1544                  * EXEC permission have been checked
1545                  * when lookup before create already.
1546                  */
1547                 rc = mdd_permission_internal_locked(env, obj, NULL, MAY_WRITE,
1548                                                     MOR_TGT_PARENT);
1549                 if (rc)
1550                         RETURN(rc);
1551         }
1552
1553         /* sgid check */
1554         rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
1555         if (rc != 0)
1556                 RETURN(rc);
1557
1558         if (la->la_mode & S_ISGID) {
1559                 ma->ma_attr.la_gid = la->la_gid;
1560                 if (S_ISDIR(ma->ma_attr.la_mode)) {
1561                         ma->ma_attr.la_mode |= S_ISGID;
1562                         ma->ma_attr.la_valid |= LA_MODE;
1563                 }
1564         }
1565
1566         switch (ma->ma_attr.la_mode & S_IFMT) {
1567         case S_IFLNK: {
1568                 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1569
1570                 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1571                         RETURN(-ENAMETOOLONG);
1572                 else
1573                         RETURN(0);
1574         }
1575         case S_IFDIR:
1576         case S_IFREG:
1577         case S_IFCHR:
1578         case S_IFBLK:
1579         case S_IFIFO:
1580         case S_IFSOCK:
1581                 rc = 0;
1582                 break;
1583         default:
1584                 rc = -EINVAL;
1585                 break;
1586         }
1587         RETURN(rc);
1588 }
1589
1590 /*
1591  * Create object and insert it into namespace.
1592  */
1593 static int mdd_create(const struct lu_env *env,
1594                       struct md_object *pobj,
1595                       const struct lu_name *lname,
1596                       struct md_object *child,
1597                       struct md_op_spec *spec,
1598                       struct md_attr* ma)
1599 {
1600         struct mdd_thread_info *info = mdd_env_info(env);
1601         struct lu_attr         *la = &info->mti_la_for_fix;
1602         struct md_attr         *ma_acl = &info->mti_ma;
1603         struct mdd_object      *mdd_pobj = md2mdd_obj(pobj);
1604         struct mdd_object      *son = md2mdd_obj(child);
1605         struct mdd_device      *mdd = mdo2mdd(pobj);
1606         struct lu_attr         *attr = &ma->ma_attr;
1607         struct lov_mds_md      *lmm = NULL;
1608         struct thandle         *handle;
1609         struct dynlock_handle  *dlh;
1610         const char             *name = lname->ln_name;
1611         int rc, created = 0, initialized = 0, inserted = 0, lmm_size = 0;
1612         int got_def_acl = 0;
1613 #ifdef HAVE_QUOTA_SUPPORT
1614         struct obd_device *obd = mdd->mdd_obd_dev;
1615         struct obd_export *exp = md_quota(env)->mq_exp;
1616         struct mds_obd *mds = &obd->u.mds;
1617         unsigned int qcids[MAXQUOTAS] = { 0, 0 };
1618         unsigned int qpids[MAXQUOTAS] = { 0, 0 };
1619         int quota_opc = 0, block_count = 0;
1620         int inode_pending[MAXQUOTAS] = { 0, 0 };
1621         int block_pending[MAXQUOTAS] = { 0, 0 };
1622         int parent_pending[MAXQUOTAS] = { 0, 0 };
1623 #endif
1624         ENTRY;
1625
1626         /*
1627          * Two operations have to be performed:
1628          *
1629          *  - an allocation of a new object (->do_create()), and
1630          *
1631          *  - an insertion into a parent index (->dio_insert()).
1632          *
1633          * Due to locking, operation order is not important, when both are
1634          * successful, *but* error handling cases are quite different:
1635          *
1636          *  - if insertion is done first, and following object creation fails,
1637          *  insertion has to be rolled back, but this operation might fail
1638          *  also leaving us with dangling index entry.
1639          *
1640          *  - if creation is done first, is has to be undone if insertion
1641          *  fails, leaving us with leaked space, which is neither good, nor
1642          *  fatal.
1643          *
1644          * It seems that creation-first is simplest solution, but it is
1645          * sub-optimal in the frequent
1646          *
1647          *         $ mkdir foo
1648          *         $ mkdir foo
1649          *
1650          * case, because second mkdir is bound to create object, only to
1651          * destroy it immediately.
1652          *
1653          * To avoid this follow local file systems that do double lookup:
1654          *
1655          *     0. lookup -> -EEXIST (mdd_create_sanity_check())
1656          *
1657          *     1. create            (mdd_object_create_internal())
1658          *
1659          *     2. insert            (__mdd_index_insert(), lookup again)
1660          */
1661
1662         /* Sanity checks before big job. */
1663         rc = mdd_create_sanity_check(env, pobj, lname, ma, spec);
1664         if (rc)
1665                 RETURN(rc);
1666
1667 #ifdef HAVE_QUOTA_SUPPORT
1668         if (mds->mds_quota) {
1669                 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
1670
1671                 rc = mdd_la_get(env, mdd_pobj, la_tmp, BYPASS_CAPA);
1672                 if (!rc) {
1673                         int same = 0;
1674
1675                         quota_opc = FSFILT_OP_CREATE;
1676                         mdd_quota_wrapper(&ma->ma_attr, qcids);
1677                         mdd_quota_wrapper(la_tmp, qpids);
1678                         /* get file quota for child */
1679                         lquota_chkquota(mds_quota_interface_ref, obd, exp,
1680                                         qcids, inode_pending, 1, NULL, 0, NULL,
1681                                         0);
1682                         switch (ma->ma_attr.la_mode & S_IFMT) {
1683                         case S_IFLNK:
1684                         case S_IFDIR:
1685                                 block_count = 2;
1686                                 break;
1687                         case S_IFREG:
1688                                 block_count = 1;
1689                                 break;
1690                         }
1691                         if (qcids[USRQUOTA] == qpids[USRQUOTA] &&
1692                             qcids[GRPQUOTA] == qpids[GRPQUOTA]) {
1693                                 block_count += 1;
1694                                 same = 1;
1695                         }
1696                         /* get block quota for child and parent */
1697                         if (block_count)
1698                                 lquota_chkquota(mds_quota_interface_ref, obd,
1699                                                 exp, qcids, block_pending,
1700                                                 block_count, NULL,
1701                                                 LQUOTA_FLAGS_BLK, NULL, 0);
1702                         if (!same)
1703                                 lquota_chkquota(mds_quota_interface_ref, obd,
1704                                                 exp, qpids, parent_pending, 1,
1705                                                 NULL, LQUOTA_FLAGS_BLK, NULL,
1706                                                 0);
1707                 }
1708         }
1709 #endif
1710
1711         /*
1712          * No RPC inside the transaction, so OST objects should be created at
1713          * first.
1714          */
1715         if (S_ISREG(attr->la_mode)) {
1716                 lmm_size = ma->ma_lmm_size;
1717                 rc = mdd_lov_create(env, mdd, mdd_pobj, son, &lmm, &lmm_size,
1718                                     spec, attr);
1719                 if (rc)
1720                         GOTO(out_pending, rc);
1721         }
1722
1723         if (!S_ISLNK(attr->la_mode)) {
1724                 ma_acl->ma_acl_size = sizeof info->mti_xattr_buf;
1725                 ma_acl->ma_acl = info->mti_xattr_buf;
1726                 ma_acl->ma_need = MA_ACL_DEF;
1727                 ma_acl->ma_valid = 0;
1728
1729                 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1730                 rc = mdd_def_acl_get(env, mdd_pobj, ma_acl);
1731                 mdd_read_unlock(env, mdd_pobj);
1732                 if (rc)
1733                         GOTO(out_free, rc);
1734                 else if (ma_acl->ma_valid & MA_ACL_DEF)
1735                         got_def_acl = 1;
1736         }
1737
1738         mdd_txn_param_build(env, mdd, MDD_TXN_MKDIR_OP);
1739         handle = mdd_trans_start(env, mdd);
1740         if (IS_ERR(handle))
1741                 GOTO(out_free, rc = PTR_ERR(handle));
1742
1743         dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1744         if (dlh == NULL)
1745                 GOTO(out_trans, rc = -ENOMEM);
1746
1747         mdd_write_lock(env, son, MOR_TGT_CHILD);
1748         rc = mdd_object_create_internal(env, mdd_pobj, son, ma, handle, spec);
1749         if (rc) {
1750                 mdd_write_unlock(env, son);
1751                 GOTO(cleanup, rc);
1752         }
1753
1754         created = 1;
1755
1756 #ifdef CONFIG_FS_POSIX_ACL
1757         if (got_def_acl) {
1758                 struct lu_buf *acl_buf = &info->mti_buf;
1759                 acl_buf->lb_buf = ma_acl->ma_acl;
1760                 acl_buf->lb_len = ma_acl->ma_acl_size;
1761
1762                 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1763                 if (rc) {
1764                         mdd_write_unlock(env, son);
1765                         GOTO(cleanup, rc);
1766                 } else {
1767                         ma->ma_attr.la_valid |= LA_MODE;
1768                 }
1769         }
1770 #endif
1771
1772         rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1773                                    son, ma, handle, spec);
1774         mdd_write_unlock(env, son);
1775         if (rc)
1776                 /*
1777                  * Object has no links, so it will be destroyed when last
1778                  * reference is released. (XXX not now.)
1779                  */
1780                 GOTO(cleanup, rc);
1781
1782         initialized = 1;
1783
1784         rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1785                                 name, S_ISDIR(attr->la_mode), handle,
1786                                 mdd_object_capa(env, mdd_pobj));
1787
1788         if (rc)
1789                 GOTO(cleanup, rc);
1790
1791         inserted = 1;
1792
1793         /* No need mdd_lsm_sanity_check here */
1794         rc = mdd_lov_set_md(env, mdd_pobj, son, lmm, lmm_size, handle, 0);
1795         if (rc) {
1796                 CERROR("error on stripe info copy %d \n", rc);
1797                 GOTO(cleanup, rc);
1798         }
1799         if (lmm && lmm_size > 0) {
1800                 /* Set Lov here, do not get lmm again later */
1801                 memcpy(ma->ma_lmm, lmm, lmm_size);
1802                 ma->ma_lmm_size = lmm_size;
1803                 ma->ma_valid |= MA_LOV;
1804         }
1805
1806         if (S_ISLNK(attr->la_mode)) {
1807                 struct md_ucred  *uc = md_ucred(env);
1808                 struct dt_object *dt = mdd_object_child(son);
1809                 const char *target_name = spec->u.sp_symname;
1810                 int sym_len = strlen(target_name);
1811                 const struct lu_buf *buf;
1812                 loff_t pos = 0;
1813
1814                 buf = mdd_buf_get_const(env, target_name, sym_len);
1815                 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1816                                                 mdd_object_capa(env, son),
1817                                                 uc->mu_cap &
1818                                                 CFS_CAP_SYS_RESOURCE_MASK);
1819
1820                 if (rc == sym_len)
1821                         rc = 0;
1822                 else
1823                         GOTO(cleanup, rc = -EFAULT);
1824         }
1825
1826         *la = ma->ma_attr;
1827         la->la_valid = LA_CTIME | LA_MTIME;
1828         rc = mdd_attr_check_set_internal_locked(env, mdd_pobj, la, handle, 0);
1829         if (rc)
1830                 GOTO(cleanup, rc);
1831
1832         /* Return attr back. */
1833         rc = mdd_attr_get_internal_locked(env, son, ma);
1834         EXIT;
1835 cleanup:
1836         if (rc && created) {
1837                 int rc2 = 0;
1838
1839                 if (inserted) {
1840                         rc2 = __mdd_index_delete(env, mdd_pobj, name,
1841                                                  S_ISDIR(attr->la_mode),
1842                                                  handle, BYPASS_CAPA);
1843                         if (rc2)
1844                                 CERROR("error can not cleanup destroy %d\n",
1845                                        rc2);
1846                 }
1847
1848                 if (rc2 == 0) {
1849                         mdd_write_lock(env, son, MOR_TGT_CHILD);
1850                         __mdd_ref_del(env, son, handle, 0);
1851                         if (initialized && S_ISDIR(attr->la_mode))
1852                                 __mdd_ref_del(env, son, handle, 1);
1853                         mdd_write_unlock(env, son);
1854                 }
1855         }
1856
1857         /* update lov_objid data, must be before transaction stop! */
1858         if (rc == 0)
1859                 mdd_lov_objid_update(mdd, lmm);
1860
1861         mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1862 out_trans:
1863         if (rc == 0)
1864                 rc = mdd_changelog_ns_store(env, mdd,
1865                             S_ISDIR(attr->la_mode) ? CL_MKDIR :
1866                             S_ISREG(attr->la_mode) ? CL_CREATE :
1867                             S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1868                             0, son, mdd_pobj, NULL, lname, handle);
1869         mdd_trans_stop(env, mdd, rc, handle);
1870 out_free:
1871         /* finish lov_create stuff, free all temporary data */
1872         mdd_lov_create_finish(env, mdd, lmm, lmm_size, spec);
1873 out_pending:
1874 #ifdef HAVE_QUOTA_SUPPORT
1875         if (quota_opc) {
1876                 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1877                                       inode_pending, 0);
1878                 lquota_pending_commit(mds_quota_interface_ref, obd, qcids,
1879                                       block_pending, 1);
1880                 lquota_pending_commit(mds_quota_interface_ref, obd, qpids,
1881                                       parent_pending, 1);
1882                 /* Trigger dqacq on the owner of child and parent. If failed,
1883                  * the next call for lquota_chkquota will process it. */
1884                 lquota_adjust(mds_quota_interface_ref, obd, qcids, qpids, rc,
1885                               quota_opc);
1886         }
1887 #endif
1888         return rc;
1889 }
1890
1891 /*
1892  * Get locks on parents in proper order
1893  * RETURN: < 0 - error, rename_order if successful
1894  */
1895 enum rename_order {
1896         MDD_RN_SAME,
1897         MDD_RN_SRCTGT,
1898         MDD_RN_TGTSRC
1899 };
1900
1901 static int mdd_rename_order(const struct lu_env *env,
1902                             struct mdd_device *mdd,
1903                             struct mdd_object *src_pobj,
1904                             struct mdd_object *tgt_pobj)
1905 {
1906         /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1907         int rc;
1908         ENTRY;
1909
1910         if (src_pobj == tgt_pobj)
1911                 RETURN(MDD_RN_SAME);
1912
1913         /* compared the parent child relationship of src_p&tgt_p */
1914         if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1915                 rc = MDD_RN_SRCTGT;
1916         } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1917                 rc = MDD_RN_TGTSRC;
1918         } else {
1919                 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1920                 if (rc == -EREMOTE)
1921                         rc = 0;
1922
1923                 if (rc == 1)
1924                         rc = MDD_RN_TGTSRC;
1925                 else if (rc == 0)
1926                         rc = MDD_RN_SRCTGT;
1927         }
1928
1929         RETURN(rc);
1930 }
1931
1932 /* has not mdd_write{read}_lock on any obj yet. */
1933 static int mdd_rename_sanity_check(const struct lu_env *env,
1934                                    struct mdd_object *src_pobj,
1935                                    struct mdd_object *tgt_pobj,
1936                                    struct mdd_object *sobj,
1937                                    struct mdd_object *tobj,
1938                                    struct md_attr *ma)
1939 {
1940         int rc = 0;
1941         ENTRY;
1942
1943         if (unlikely(ma->ma_attr_flags & MDS_PERM_BYPASS))
1944                 RETURN(0);
1945
1946         /* XXX: when get here, sobj must NOT be NULL,
1947          * the other case has been processed in cml_rename
1948          * before mdd_rename and enable MDS_PERM_BYPASS. */
1949         LASSERT(sobj);
1950
1951         rc = mdd_may_delete(env, src_pobj, sobj, ma, 1, 0);
1952         if (rc)
1953                 RETURN(rc);
1954
1955         /* XXX: when get here, "tobj == NULL" means tobj must
1956          * NOT exist (neither on remote MDS, such case has been
1957          * processed in cml_rename before mdd_rename and enable
1958          * MDS_PERM_BYPASS).
1959          * So check may_create, but not check may_unlink. */
1960         if (!tobj)
1961                 rc = mdd_may_create(env, tgt_pobj, NULL,
1962                                     (src_pobj != tgt_pobj), 0);
1963         else
1964                 rc = mdd_may_delete(env, tgt_pobj, tobj, ma,
1965                                     (src_pobj != tgt_pobj), 1);
1966
1967         if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1968             S_ISDIR(ma->ma_attr.la_mode))
1969                 rc = __mdd_may_link(env, tgt_pobj);
1970
1971         RETURN(rc);
1972 }
1973
1974 /* src object can be remote that is why we use only fid and type of object */
1975 static int mdd_rename(const struct lu_env *env,
1976                       struct md_object *src_pobj, struct md_object *tgt_pobj,
1977                       const struct lu_fid *lf, const struct lu_name *lsname,
1978                       struct md_object *tobj, const struct lu_name *ltname,
1979                       struct md_attr *ma)
1980 {
1981         const char *sname = lsname->ln_name;
1982         const char *tname = ltname->ln_name;
1983         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
1984         struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
1985         struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
1986         struct mdd_device *mdd = mdo2mdd(src_pobj);
1987         struct mdd_object *mdd_sobj = NULL;                  /* source object */
1988         struct mdd_object *mdd_tobj = NULL;
1989         struct dynlock_handle *sdlh, *tdlh;
1990         struct thandle *handle;
1991         const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
1992         const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
1993         int is_dir;
1994         int rc, rc2;
1995
1996 #ifdef HAVE_QUOTA_SUPPORT
1997         struct obd_device *obd = mdd->mdd_obd_dev;
1998         struct obd_export *exp = md_quota(env)->mq_exp;
1999         struct mds_obd *mds = &obd->u.mds;
2000         unsigned int qspids[MAXQUOTAS] = { 0, 0 };
2001         unsigned int qtcids[MAXQUOTAS] = { 0, 0 };
2002         unsigned int qtpids[MAXQUOTAS] = { 0, 0 };
2003         int quota_copc = 0, quota_popc = 0;
2004         int rec_pending[MAXQUOTAS] = { 0, 0 };
2005 #endif
2006         ENTRY;
2007
2008         LASSERT(ma->ma_attr.la_mode & S_IFMT);
2009         is_dir = S_ISDIR(ma->ma_attr.la_mode);
2010
2011         if (tobj)
2012                 mdd_tobj = md2mdd_obj(tobj);
2013
2014 #ifdef HAVE_QUOTA_SUPPORT
2015         if (mds->mds_quota) {
2016                 struct lu_attr *la_tmp = &mdd_env_info(env)->mti_la;
2017
2018                 rc = mdd_la_get(env, mdd_spobj, la_tmp, BYPASS_CAPA);
2019                 if (!rc) {
2020                         mdd_quota_wrapper(la_tmp, qspids);
2021                         if (!tobj) {
2022                                 rc = mdd_la_get(env, mdd_tpobj, la_tmp,
2023                                                 BYPASS_CAPA);
2024                                 if (!rc) {
2025                                         void *data = NULL;
2026                                         mdd_data_get(env, mdd_tpobj, &data);
2027                                         quota_popc = FSFILT_OP_LINK;
2028                                         mdd_quota_wrapper(la_tmp, qtpids);
2029                                         /* get block quota for target parent */
2030                                         lquota_chkquota(mds_quota_interface_ref,
2031                                                         obd, exp, qtpids,
2032                                                         rec_pending, 1, NULL,
2033                                                         LQUOTA_FLAGS_BLK,
2034                                                         data, 1);
2035                                 }
2036                         }
2037                 }
2038         }
2039 #endif
2040         mdd_txn_param_build(env, mdd, MDD_TXN_RENAME_OP);
2041         handle = mdd_trans_start(env, mdd);
2042         if (IS_ERR(handle))
2043                 GOTO(out_pending, rc = PTR_ERR(handle));
2044
2045         /* FIXME: Should consider tobj and sobj too in rename_lock. */
2046         rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2047         if (rc < 0)
2048                 GOTO(cleanup_unlocked, rc);
2049
2050         /* Get locks in determined order */
2051         if (rc == MDD_RN_SAME) {
2052                 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2053                                           sname, MOR_SRC_PARENT);
2054                 /* check hashes to determine do we need one lock or two */
2055                 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2056                         tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2057                                 MOR_TGT_PARENT);
2058                 else
2059                         tdlh = sdlh;
2060         } else if (rc == MDD_RN_SRCTGT) {
2061                 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2062                 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2063         } else {
2064                 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2065                 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2066         }
2067         if (sdlh == NULL || tdlh == NULL)
2068                 GOTO(cleanup, rc = -ENOMEM);
2069
2070         mdd_sobj = mdd_object_find(env, mdd, lf);
2071         rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj,
2072                                      mdd_sobj, mdd_tobj, ma);
2073         if (rc)
2074                 GOTO(cleanup, rc);
2075
2076         /* Remove source name from source directory */
2077         rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2078                                 mdd_object_capa(env, mdd_spobj));
2079         if (rc)
2080                 GOTO(cleanup, rc);
2081
2082         /* "mv dir1 dir2" needs "dir1/.." link update */
2083         if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2084                 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2085                                         mdd_object_capa(env, mdd_sobj));
2086                 if (rc)
2087                         GOTO(fixup_spobj2, rc);
2088
2089                 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2090                                       handle, mdd_object_capa(env, mdd_sobj));
2091                 if (rc)
2092                         GOTO(fixup_spobj, rc);
2093         }
2094
2095         /* Remove target name from target directory
2096          * Here tobj can be remote one, so we do index_delete unconditionally
2097          * and -ENOENT is allowed.
2098          */
2099         rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2100                                 mdd_object_capa(env, mdd_tpobj));
2101         if (rc != 0) {
2102                 if (mdd_tobj) {
2103                         /* tname might been renamed to something else */
2104                         GOTO(fixup_spobj, rc);
2105                 }
2106                 if (rc != -ENOENT)
2107                         GOTO(fixup_spobj, rc);
2108         }
2109
2110         /* Insert new fid with target name into target dir */
2111         rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2112                                 mdd_object_capa(env, mdd_tpobj));
2113         if (rc)
2114                 GOTO(fixup_tpobj, rc);
2115
2116         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2117         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2118
2119         /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2120         if (mdd_sobj) {
2121                 la->la_valid = LA_CTIME;
2122                 rc = mdd_attr_check_set_internal_locked(env, mdd_sobj, la,
2123                                                         handle, 0);
2124                 if (rc)
2125                         GOTO(fixup_tpobj, rc);
2126         }
2127
2128         /* Remove old target object
2129          * For tobj is remote case cmm layer has processed
2130          * and set tobj to NULL then. So when tobj is NOT NULL,
2131          * it must be local one.
2132          */
2133         if (tobj && mdd_object_exists(mdd_tobj)) {
2134                 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2135                 if (mdd_is_dead_obj(mdd_tobj)) {
2136                         mdd_write_unlock(env, mdd_tobj);
2137                         /* shld not be dead, something is wrong */
2138                         CERROR("tobj is dead, something is wrong\n");
2139                         rc = -EINVAL;
2140                         goto cleanup;
2141                 }
2142                 __mdd_ref_del(env, mdd_tobj, handle, 0);
2143
2144                 /* Remove dot reference. */
2145                 if (is_dir)
2146                         __mdd_ref_del(env, mdd_tobj, handle, 1);
2147
2148                 la->la_valid = LA_CTIME;
2149                 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2150                 if (rc)
2151                         GOTO(fixup_tpobj, rc);
2152
2153                 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2154                 mdd_write_unlock(env, mdd_tobj);
2155                 if (rc)
2156                         GOTO(fixup_tpobj, rc);
2157
2158 #ifdef HAVE_QUOTA_SUPPORT
2159                 if (mds->mds_quota && ma->ma_valid & MA_INODE &&
2160                     ma->ma_attr.la_nlink == 0 && mdd_tobj->mod_count == 0) {
2161                         quota_copc = FSFILT_OP_UNLINK_PARTIAL_CHILD;
2162                         mdd_quota_wrapper(&ma->ma_attr, qtcids);
2163                 }
2164 #endif
2165         }
2166
2167         la->la_valid = LA_CTIME | LA_MTIME;
2168         rc = mdd_attr_check_set_internal_locked(env, mdd_spobj, la, handle, 0);
2169         if (rc)
2170                 GOTO(fixup_tpobj, rc);
2171
2172         if (mdd_spobj != mdd_tpobj) {
2173                 la->la_valid = LA_CTIME | LA_MTIME;
2174                 rc = mdd_attr_check_set_internal_locked(env, mdd_tpobj, la,
2175                                                   handle, 0);
2176         }
2177
2178         if (rc == 0 && mdd_sobj) {
2179                 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2180                 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2181                                       mdo2fid(mdd_tpobj), ltname, handle);
2182                 if (rc == -ENOENT)
2183                         /* Old files might not have EA entry */
2184                         mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2185                                       lsname, handle, 0);
2186                 mdd_write_unlock(env, mdd_sobj);
2187                 /* We don't fail the transaction if the link ea can't be
2188                    updated -- fid2path will use alternate lookup method. */
2189                 rc = 0;
2190         }
2191
2192         EXIT;
2193
2194 fixup_tpobj:
2195         if (rc) {
2196                 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2197                                          BYPASS_CAPA);
2198                 if (rc2)
2199                         CWARN("tp obj fix error %d\n",rc2);
2200
2201                 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2202                     !mdd_is_dead_obj(mdd_tobj)) {
2203                         rc2 = __mdd_index_insert(env, mdd_tpobj,
2204                                          mdo2fid(mdd_tobj), tname,
2205                                          is_dir, handle,
2206                                          BYPASS_CAPA);
2207
2208                         if (rc2)
2209                                 CWARN("tp obj fix error %d\n",rc2);
2210                 }
2211         }
2212
2213 fixup_spobj:
2214         if (rc && is_dir && mdd_sobj) {
2215                 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2216                                               BYPASS_CAPA);
2217
2218                 if (rc2)
2219                         CWARN("sp obj dotdot delete error %d\n",rc2);
2220
2221
2222                 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2223                                               dotdot, handle, BYPASS_CAPA);
2224                 if (rc2)
2225                         CWARN("sp obj dotdot insert error %d\n",rc2);
2226         }
2227
2228 fixup_spobj2:
2229         if (rc) {
2230                 rc2 = __mdd_index_insert(env, mdd_spobj,
2231                                          lf, sname, is_dir, handle, BYPASS_CAPA);
2232                 if (rc2)
2233                         CWARN("sp obj fix error %d\n",rc2);
2234         }
2235 cleanup:
2236         if (likely(tdlh) && sdlh != tdlh)
2237                 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2238         if (likely(sdlh))
2239                 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2240 cleanup_unlocked:
2241         if (rc == 0)
2242                 rc = mdd_changelog_ns_store(env, mdd, CL_RENAME, 0, mdd_tobj,
2243                                             mdd_spobj, lf, lsname, handle);
2244         if (rc == 0)
2245                 rc = mdd_changelog_ns_store(env, mdd, CL_EXT, 0, mdd_tobj,
2246                                             mdd_tpobj, lf, ltname, handle);
2247
2248         mdd_trans_stop(env, mdd, rc, handle);
2249         if (mdd_sobj)
2250                 mdd_object_put(env, mdd_sobj);
2251 out_pending:
2252 #ifdef HAVE_QUOTA_SUPPORT
2253         if (mds->mds_quota) {
2254                 if (quota_popc)
2255                         lquota_pending_commit(mds_quota_interface_ref, obd,
2256                                               qtpids, rec_pending, 1);
2257
2258                 if (quota_copc) {
2259                         /* Trigger dqrel on the source owner of parent.
2260                          * If failed, the next call for lquota_chkquota will
2261                          * process it. */
2262                         lquota_adjust(mds_quota_interface_ref, obd, 0, qspids, rc,
2263                                       FSFILT_OP_UNLINK_PARTIAL_PARENT);
2264
2265                         /* Trigger dqrel on the target owner of child.
2266                          * If failed, the next call for lquota_chkquota
2267                          * will process it. */
2268                         lquota_adjust(mds_quota_interface_ref, obd, qtcids,
2269                                       qtpids, rc, quota_copc);
2270                 }
2271         }
2272 #endif
2273         return rc;
2274 }
2275
2276 /** enable/disable storing of hardlink info */
2277 int mdd_linkea_enable = 1;
2278 CFS_MODULE_PARM(mdd_linkea_enable, "d", int, 0644,
2279                 "record hardlink info in EAs");
2280
2281 /** Read the link EA into a temp buffer.
2282  * Uses the name_buf since it is generally large.
2283  * \retval IS_ERR err
2284  * \retval ptr to \a lu_buf (always \a mti_big_buf)
2285  */
2286 struct lu_buf *mdd_links_get(const struct lu_env *env,
2287                              struct mdd_object *mdd_obj)
2288 {
2289         struct lu_buf *buf;
2290         struct lustre_capa *capa;
2291         struct link_ea_header *leh;
2292         int rc;
2293
2294         /* First try a small buf */
2295         buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2296         if (buf->lb_buf == NULL)
2297                 return ERR_PTR(-ENOMEM);
2298
2299         capa = mdd_object_capa(env, mdd_obj);
2300         rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2301         if (rc == -ERANGE) {
2302                 /* Buf was too small, figure out what we need. */
2303                 mdd_buf_put(buf);
2304                 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2305                 if (rc < 0)
2306                         return ERR_PTR(rc);
2307                 buf = mdd_buf_alloc(env, rc);
2308                 if (buf->lb_buf == NULL)
2309                         return ERR_PTR(-ENOMEM);
2310                 rc = mdo_xattr_get(env, mdd_obj, buf, XATTR_NAME_LINK, capa);
2311         }
2312         if (rc < 0)
2313                 return ERR_PTR(rc);
2314
2315         leh = buf->lb_buf;
2316         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2317                 leh->leh_magic = LINK_EA_MAGIC;
2318                 leh->leh_reccount = __swab32(leh->leh_reccount);
2319                 leh->leh_len = __swab64(leh->leh_len);
2320                 /* entries are swabbed by mdd_lee_unpack */
2321         }
2322         if (leh->leh_magic != LINK_EA_MAGIC)
2323                 return ERR_PTR(-EINVAL);
2324         if (leh->leh_reccount == 0)
2325                 return ERR_PTR(-ENODATA);
2326
2327         return buf;
2328 }
2329
2330 /** Pack a link_ea_entry.
2331  * All elements are stored as chars to avoid alignment issues.
2332  * Numbers are always big-endian
2333  * \retval record length
2334  */
2335 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2336                         const struct lu_fid *pfid)
2337 {
2338         struct lu_fid   tmpfid;
2339         int             reclen;
2340
2341         fid_cpu_to_be(&tmpfid, pfid);
2342         memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2343         memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2344         reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2345
2346         lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2347         lee->lee_reclen[1] = reclen & 0xff;
2348         return reclen;
2349 }
2350
2351 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2352                     struct lu_name *lname, struct lu_fid *pfid)
2353 {
2354         *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2355         memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2356         fid_be_to_cpu(pfid, pfid);
2357         lname->ln_name = lee->lee_name;
2358         lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2359 }
2360
2361 /** Add a record to the end of link ea buf */
2362 static int __mdd_links_add(const struct lu_env *env, struct lu_buf *buf,
2363                            const struct lu_fid *pfid,
2364                            const struct lu_name *lname)
2365 {
2366         struct link_ea_header *leh;
2367         struct link_ea_entry *lee;
2368         int reclen;
2369
2370         if (lname == NULL || pfid == NULL)
2371                 return -EINVAL;
2372
2373         /* Make sure our buf is big enough for the new one */
2374         leh = buf->lb_buf;
2375         reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2376         if (leh->leh_len + reclen > buf->lb_len) {
2377                 if (mdd_buf_grow(env, leh->leh_len + reclen) < 0)
2378                         return -ENOMEM;
2379         }
2380
2381         leh = buf->lb_buf;
2382         lee = buf->lb_buf + leh->leh_len;
2383         reclen = mdd_lee_pack(lee, lname, pfid);
2384         leh->leh_len += reclen;
2385         leh->leh_reccount++;
2386         return 0;
2387 }
2388
2389 /* For pathologic linkers, we don't want to spend lots of time scanning the
2390  * link ea.  Limit ourseleves to something reasonable; links not in the EA
2391  * can be looked up via (slower) parent lookup.
2392  */
2393 #define LINKEA_MAX_COUNT 128
2394
2395 static int mdd_links_add(const struct lu_env *env,
2396                          struct mdd_object *mdd_obj,
2397                          const struct lu_fid *pfid,
2398                          const struct lu_name *lname,
2399                          struct thandle *handle, int first)
2400 {
2401         struct lu_buf *buf;
2402         struct link_ea_header *leh;
2403         int rc;
2404         ENTRY;
2405
2406         if (!mdd_linkea_enable)
2407                 RETURN(0);
2408
2409         buf = first ? ERR_PTR(-ENODATA) : mdd_links_get(env, mdd_obj);
2410         if (IS_ERR(buf)) {
2411                 rc = PTR_ERR(buf);
2412                 if (rc != -ENODATA) {
2413                         CERROR("link_ea read failed %d "DFID"\n", rc,
2414                                PFID(mdd_object_fid(mdd_obj)));
2415                         RETURN (rc);
2416                 }
2417                 /* empty EA; start one */
2418                 buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2419                 if (buf->lb_buf == NULL)
2420                         RETURN(-ENOMEM);
2421                 leh = buf->lb_buf;
2422                 leh->leh_magic = LINK_EA_MAGIC;
2423                 leh->leh_len = sizeof(struct link_ea_header);
2424                 leh->leh_reccount = 0;
2425         }
2426
2427         leh = buf->lb_buf;
2428         if (leh->leh_reccount > LINKEA_MAX_COUNT)
2429                 RETURN(-EOVERFLOW);
2430
2431         rc = __mdd_links_add(env, buf, pfid, lname);
2432         if (rc)
2433                 RETURN(rc);
2434
2435         leh = buf->lb_buf;
2436         rc = __mdd_xattr_set(env, mdd_obj,
2437                              mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2438                              XATTR_NAME_LINK, 0, handle);
2439         if (rc) {
2440                 if (rc == -ENOSPC)
2441                         CDEBUG(D_INODE, "link_ea add failed %d "DFID"\n", rc,
2442                                PFID(mdd_object_fid(mdd_obj)));
2443                 else
2444                         CERROR("link_ea add failed %d "DFID"\n", rc,
2445                                PFID(mdd_object_fid(mdd_obj)));
2446         }
2447
2448         if (buf->lb_len > OBD_ALLOC_BIG)
2449                 /* if we vmalloced a large buffer drop it */
2450                 mdd_buf_put(buf);
2451
2452         RETURN (rc);
2453 }
2454
2455 static int mdd_links_rename(const struct lu_env *env,
2456                             struct mdd_object *mdd_obj,
2457                             const struct lu_fid *oldpfid,
2458                             const struct lu_name *oldlname,
2459                             const struct lu_fid *newpfid,
2460                             const struct lu_name *newlname,
2461                             struct thandle *handle)
2462 {
2463         struct lu_buf  *buf;
2464         struct link_ea_header *leh;
2465         struct link_ea_entry  *lee;
2466         struct lu_name *tmpname = &mdd_env_info(env)->mti_name;
2467         struct lu_fid  *tmpfid = &mdd_env_info(env)->mti_fid;
2468         int reclen = 0;
2469         int count;
2470         int rc, rc2 = 0;
2471         ENTRY;
2472
2473         if (!mdd_linkea_enable)
2474                 RETURN(0);
2475
2476         if (mdd_obj->mod_flags & DEAD_OBJ)
2477                 /* No more links, don't bother */
2478                 RETURN(0);
2479
2480         buf = mdd_links_get(env, mdd_obj);
2481         if (IS_ERR(buf)) {
2482                 rc = PTR_ERR(buf);
2483                 if (rc == -ENODATA)
2484                         CDEBUG(D_INODE, "link_ea read failed %d "DFID"\n",
2485                                rc, PFID(mdd_object_fid(mdd_obj)));
2486                 else
2487                         CERROR("link_ea read failed %d "DFID"\n",
2488                                rc, PFID(mdd_object_fid(mdd_obj)));
2489                 RETURN(rc);
2490         }
2491         leh = buf->lb_buf;
2492         lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
2493
2494         /* Find the old record */
2495         for(count = 0; count < leh->leh_reccount; count++) {
2496                 mdd_lee_unpack(lee, &reclen, tmpname, tmpfid);
2497                 if (tmpname->ln_namelen == oldlname->ln_namelen &&
2498                     lu_fid_eq(tmpfid, oldpfid) &&
2499                     (strncmp(tmpname->ln_name, oldlname->ln_name,
2500                              tmpname->ln_namelen) == 0))
2501                         break;
2502                 lee = (struct link_ea_entry *)((char *)lee + reclen);
2503         }
2504         if ((count + 1) > leh->leh_reccount) {
2505                 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2506                        oldlname->ln_namelen, oldlname->ln_name);
2507                 GOTO(out, rc = -ENOENT);
2508         }
2509
2510         /* Remove the old record */
2511         leh->leh_reccount--;
2512         leh->leh_len -= reclen;
2513         memmove(lee, (char *)lee + reclen, (char *)leh + leh->leh_len -
2514                 (char *)lee);
2515
2516         /* If renaming, add the new record */
2517         if (newpfid != NULL) {
2518                 /* if the add fails, we still delete the out-of-date old link */
2519                 rc2 = __mdd_links_add(env, buf, newpfid, newlname);
2520                 leh = buf->lb_buf;
2521         }
2522
2523         rc = __mdd_xattr_set(env, mdd_obj,
2524                              mdd_buf_get_const(env, buf->lb_buf, leh->leh_len),
2525                              XATTR_NAME_LINK, 0, handle);
2526
2527 out:
2528         if (rc == 0)
2529                 rc = rc2;
2530         if (rc)
2531                 CDEBUG(D_INODE, "link_ea mv/unlink '%.*s' failed %d "DFID"\n",
2532                        oldlname->ln_namelen, oldlname->ln_name, rc,
2533                        PFID(mdd_object_fid(mdd_obj)));
2534
2535         if (buf->lb_len > OBD_ALLOC_BIG)
2536                 /* if we vmalloced a large buffer drop it */
2537                 mdd_buf_put(buf);
2538
2539         RETURN (rc);
2540 }
2541
2542 const struct md_dir_operations mdd_dir_ops = {
2543         .mdo_is_subdir     = mdd_is_subdir,
2544         .mdo_lookup        = mdd_lookup,
2545         .mdo_create        = mdd_create,
2546         .mdo_rename        = mdd_rename,
2547         .mdo_link          = mdd_link,
2548         .mdo_unlink        = mdd_unlink,
2549         .mdo_name_insert   = mdd_name_insert,
2550         .mdo_name_remove   = mdd_name_remove,
2551         .mdo_rename_tgt    = mdd_rename_tgt,
2552         .mdo_create_data   = mdd_create_data,
2553 };