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