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