Whamcloud - gitweb
LU-591 mdd: link ea code cleanup
[fs/lustre-release.git] / lustre / mdd / mdd_dir.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdd/mdd_dir.c
37  *
38  * Lustre Metadata Server (mdd) routines
39  *
40  * Author: Wang Di <wangdi@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_MDS
44
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_mds.h>
48 #include <lustre_fid.h>
49
50 #include "mdd_internal.h"
51
52 static const char dot[] = ".";
53 static const char dotdot[] = "..";
54
55 static struct lu_name lname_dotdot = {
56         (char *) dotdot,
57         sizeof(dotdot) - 1
58 };
59
60 static int __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
61                         const struct lu_name *lname, struct lu_fid* fid,
62                         int mask);
63 static int mdd_declare_links_add(const struct lu_env *env,
64                                  struct mdd_object *mdd_obj,
65                                  struct thandle *handle);
66 static 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 md_ucred *uc = md_ucred(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->mu_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->mu_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 md_ucred  *uc = md_ucred(env);
513
514                 rc = next->do_index_ops->dio_insert(env, next,
515                                                     (struct dt_rec*)lf,
516                                                     (const struct dt_key *)name,
517                                                     handle, capa, uc->mu_cap &
518                                                     CFS_CAP_SYS_RESOURCE_MASK);
519         } else {
520                 rc = -ENOTDIR;
521         }
522         RETURN(rc);
523 }
524
525 /* insert named index, add reference if isdir */
526 static int __mdd_index_insert(const struct lu_env *env, struct mdd_object *pobj,
527                               const struct lu_fid *lf, const char *name, int is_dir,
528                               struct thandle *handle, struct lustre_capa *capa)
529 {
530         int               rc;
531         ENTRY;
532
533         rc = __mdd_index_insert_only(env, pobj, lf, name, handle, capa);
534         if (rc == 0 && is_dir) {
535                 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
536                 mdo_ref_add(env, pobj, handle);
537                 mdd_write_unlock(env, pobj);
538         }
539         RETURN(rc);
540 }
541
542 /* delete named index, drop reference if isdir */
543 static int __mdd_index_delete(const struct lu_env *env, struct mdd_object *pobj,
544                               const char *name, int is_dir, struct thandle *handle,
545                               struct lustre_capa *capa)
546 {
547         int               rc;
548         ENTRY;
549
550         rc = __mdd_index_delete_only(env, pobj, name, handle, capa);
551         if (rc == 0 && is_dir) {
552                 mdd_write_lock(env, pobj, MOR_TGT_PARENT);
553                 mdo_ref_del(env, pobj, handle);
554                 mdd_write_unlock(env, pobj);
555         }
556
557         RETURN(rc);
558 }
559
560 int mdd_declare_llog_record(const struct lu_env *env, struct mdd_device *mdd,
561                             int reclen, struct thandle *handle)
562 {
563         int rc;
564
565         /* XXX: this is a temporary solution to declare llog changes
566          *      will be fixed in 2.3 with new llog implementation */
567
568         LASSERT(mdd->mdd_capa);
569
570         /* XXX: Since we use the 'mdd_capa' as fake llog object here, we
571          *      have to set the parameter 'size' as INT_MAX or 0 to inform
572          *      OSD that this record write is for a llog write or catalog
573          *      header update, and osd declare function will reserve less
574          *      credits for optimization purpose.
575          *
576          *      Reserve 6 blocks for a llog write, since the llog file is
577          *      usually small, reserve 2 blocks for catalog header update,
578          *      because we know for sure that catalog header is already
579          *      allocated.
580          *
581          *      This hack should be removed in 2.3.
582          */
583
584         /* record itself */
585         rc = dt_declare_record_write(env, mdd->mdd_capa,
586                                      DECLARE_LLOG_WRITE, 0, handle);
587         if (rc)
588                 return rc;
589
590         /* header will be updated as well */
591         rc = dt_declare_record_write(env, mdd->mdd_capa,
592                                      DECLARE_LLOG_WRITE, 0, handle);
593         if (rc)
594                 return rc;
595
596         /* also we should be able to create new plain log */
597         rc = dt_declare_create(env, mdd->mdd_capa, NULL, NULL, NULL, handle);
598         if (rc)
599                 return rc;
600
601         /* new record referencing new plain llog */
602         rc = dt_declare_record_write(env, mdd->mdd_capa,
603                                      DECLARE_LLOG_WRITE, 0, handle);
604         if (rc)
605                 return rc;
606
607         /* catalog's header will be updated as well */
608         rc = dt_declare_record_write(env, mdd->mdd_capa,
609                                      DECLARE_LLOG_REWRITE, 0, handle);
610
611         return rc;
612 }
613
614 int mdd_declare_changelog_store(const struct lu_env *env,
615                                 struct mdd_device *mdd,
616                                 const struct lu_name *fname,
617                                 struct thandle *handle)
618 {
619         struct obd_device               *obd = mdd2obd_dev(mdd);
620         struct llog_ctxt                *ctxt;
621         struct llog_changelog_rec       *rec;
622         struct lu_buf                   *buf;
623         int                              reclen;
624         int                              rc;
625
626         /* Not recording */
627         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
628                 return 0;
629
630         reclen = llog_data_len(sizeof(*rec) +
631                                (fname != NULL ? fname->ln_namelen : 0));
632         buf = mdd_buf_alloc(env, reclen);
633         if (buf->lb_buf == NULL)
634                 return -ENOMEM;
635
636         rec = buf->lb_buf;
637         rec->cr_hdr.lrh_len = reclen;
638         rec->cr_hdr.lrh_type = CHANGELOG_REC;
639
640         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
641         if (ctxt == NULL)
642                 return -ENXIO;
643
644         rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
645         llog_ctxt_put(ctxt);
646
647         return rc;
648 }
649
650 static int mdd_declare_changelog_ext_store(const struct lu_env *env,
651                                            struct mdd_device *mdd,
652                                            const struct lu_name *tname,
653                                            const struct lu_name *sname,
654                                            struct thandle *handle)
655 {
656         struct obd_device               *obd = mdd2obd_dev(mdd);
657         struct llog_ctxt                *ctxt;
658         struct llog_changelog_ext_rec   *rec;
659         struct lu_buf                   *buf;
660         int                              reclen;
661         int                              rc;
662
663         /* Not recording */
664         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
665                 return 0;
666
667         reclen = llog_data_len(sizeof(*rec) +
668                                (tname != NULL ? tname->ln_namelen : 0) +
669                                (sname != NULL ? 1 + sname->ln_namelen : 0));
670         buf = mdd_buf_alloc(env, reclen);
671         if (buf->lb_buf == NULL)
672                 return -ENOMEM;
673
674         rec = buf->lb_buf;
675         rec->cr_hdr.lrh_len = reclen;
676         rec->cr_hdr.lrh_type = CHANGELOG_REC;
677
678         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
679         if (ctxt == NULL)
680                 return -ENXIO;
681
682         rc = llog_declare_add(env, ctxt->loc_handle, &rec->cr_hdr, handle);
683         llog_ctxt_put(ctxt);
684
685         return rc;
686 }
687
688 /** Add a changelog entry \a rec to the changelog llog
689  * \param mdd
690  * \param rec
691  * \param handle - currently ignored since llogs start their own transaction;
692  *                 this will hopefully be fixed in llog rewrite
693  * \retval 0 ok
694  */
695 int mdd_changelog_store(const struct lu_env *env, struct mdd_device *mdd,
696                         struct llog_changelog_rec *rec, struct thandle *th)
697 {
698         struct obd_device       *obd = mdd2obd_dev(mdd);
699         struct llog_ctxt        *ctxt;
700         int                      rc;
701
702         rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
703         rec->cr_hdr.lrh_type = CHANGELOG_REC;
704         rec->cr.cr_time = cl_time();
705
706         cfs_spin_lock(&mdd->mdd_cl.mc_lock);
707         /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
708          * but as long as the MDD transactions are ordered correctly for e.g.
709          * rename conflicts, I don't think this should matter. */
710         rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
711         cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
712
713         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
714         if (ctxt == NULL)
715                 return -ENXIO;
716
717         rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
718         llog_ctxt_put(ctxt);
719         if (rc > 0)
720                 rc = 0;
721         return rc;
722 }
723
724 /** Add a changelog_ext entry \a rec to the changelog llog
725  * \param mdd
726  * \param rec
727  * \param handle - currently ignored since llogs start their own transaction;
728  *              this will hopefully be fixed in llog rewrite
729  * \retval 0 ok
730  */
731 int mdd_changelog_ext_store(const struct lu_env *env, struct mdd_device *mdd,
732                             struct llog_changelog_ext_rec *rec,
733                             struct thandle *th)
734 {
735         struct obd_device       *obd = mdd2obd_dev(mdd);
736         struct llog_ctxt        *ctxt;
737         int                      rc;
738
739         rec->cr_hdr.lrh_len = llog_data_len(sizeof(*rec) + rec->cr.cr_namelen);
740         /* llog_lvfs_write_rec sets the llog tail len */
741         rec->cr_hdr.lrh_type = CHANGELOG_REC;
742         rec->cr.cr_time = cl_time();
743
744         cfs_spin_lock(&mdd->mdd_cl.mc_lock);
745         /* NB: I suppose it's possible llog_add adds out of order wrt cr_index,
746          * but as long as the MDD transactions are ordered correctly for e.g.
747          * rename conflicts, I don't think this should matter. */
748         rec->cr.cr_index = ++mdd->mdd_cl.mc_index;
749         cfs_spin_unlock(&mdd->mdd_cl.mc_lock);
750
751         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
752         if (ctxt == NULL)
753                 return -ENXIO;
754
755         /* nested journal transaction */
756         rc = llog_add(env, ctxt->loc_handle, &rec->cr_hdr, NULL, NULL, th);
757         llog_ctxt_put(ctxt);
758         if (rc > 0)
759                 rc = 0;
760
761         return rc;
762 }
763
764 /** Store a namespace change changelog record
765  * If this fails, we must fail the whole transaction; we don't
766  * want the change to commit without the log entry.
767  * \param target - mdd_object of change
768  * \param parent - parent dir/object
769  * \param tname - target name string
770  * \param handle - transacion handle
771  */
772 static int mdd_changelog_ns_store(const struct lu_env  *env,
773                                   struct mdd_device    *mdd,
774                                   enum changelog_rec_type type,
775                                   unsigned flags,
776                                   struct mdd_object    *target,
777                                   struct mdd_object    *parent,
778                                   const struct lu_name *tname,
779                                   struct thandle *handle)
780 {
781         struct llog_changelog_rec *rec;
782         struct lu_buf *buf;
783         int reclen;
784         int rc;
785         ENTRY;
786
787         /* Not recording */
788         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
789                 RETURN(0);
790         if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
791                 RETURN(0);
792
793         LASSERT(target != NULL);
794         LASSERT(parent != NULL);
795         LASSERT(tname != NULL);
796         LASSERT(handle != NULL);
797
798         reclen = llog_data_len(sizeof(*rec) + tname->ln_namelen);
799         buf = mdd_buf_alloc(env, reclen);
800         if (buf->lb_buf == NULL)
801                 RETURN(-ENOMEM);
802         rec = buf->lb_buf;
803
804         rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
805         rec->cr.cr_type = (__u32)type;
806         rec->cr.cr_tfid = *mdo2fid(target);
807         rec->cr.cr_pfid = *mdo2fid(parent);
808         rec->cr.cr_namelen = tname->ln_namelen;
809         memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
810
811         target->mod_cltime = cfs_time_current_64();
812
813         rc = mdd_changelog_store(env, mdd, rec, handle);
814         if (rc < 0) {
815                 CERROR("changelog failed: rc=%d, op%d %s c"DFID" p"DFID"\n",
816                         rc, type, tname->ln_name, PFID(&rec->cr.cr_tfid),
817                         PFID(&rec->cr.cr_pfid));
818                 RETURN(-EFAULT);
819         }
820
821         RETURN(0);
822 }
823
824
825 /** Store a namespace change changelog record
826  * If this fails, we must fail the whole transaction; we don't
827  * want the change to commit without the log entry.
828  * \param target - mdd_object of change
829  * \param tpfid - target parent dir/object fid
830  * \param sfid - source object fid
831  * \param spfid - source parent fid
832  * \param tname - target name string
833  * \param sname - source name string
834  * \param handle - transacion handle
835  */
836 static int mdd_changelog_ext_ns_store(const struct lu_env  *env,
837                                       struct mdd_device    *mdd,
838                                       enum changelog_rec_type type,
839                                       unsigned flags,
840                                       struct mdd_object    *target,
841                                       const struct lu_fid  *tpfid,
842                                       const struct lu_fid  *sfid,
843                                       const struct lu_fid  *spfid,
844                                       const struct lu_name *tname,
845                                       const struct lu_name *sname,
846                                       struct thandle *handle)
847 {
848         struct llog_changelog_ext_rec *rec;
849         struct lu_buf *buf;
850         int reclen;
851         int rc;
852         ENTRY;
853
854         /* Not recording */
855         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
856                 RETURN(0);
857         if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
858                 RETURN(0);
859
860         LASSERT(sfid != NULL);
861         LASSERT(tpfid != NULL);
862         LASSERT(tname != NULL);
863         LASSERT(handle != NULL);
864
865         reclen = llog_data_len(sizeof(*rec) +
866                                sname != NULL ? 1 + sname->ln_namelen : 0);
867         buf = mdd_buf_alloc(env, reclen);
868         if (buf->lb_buf == NULL)
869                 RETURN(-ENOMEM);
870         rec = buf->lb_buf;
871
872         rec->cr.cr_flags = CLF_EXT_VERSION | (CLF_FLAGMASK & flags);
873         rec->cr.cr_type = (__u32)type;
874         rec->cr.cr_pfid = *tpfid;
875         rec->cr.cr_sfid = *sfid;
876         rec->cr.cr_spfid = *spfid;
877         rec->cr.cr_namelen = tname->ln_namelen;
878         memcpy(rec->cr.cr_name, tname->ln_name, tname->ln_namelen);
879         if (sname) {
880                 LASSERT(sfid != NULL);
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 = __mdd_index_insert_only(env, mdd_tobj, mdo2fid(mdd_sobj),
974                                      name, handle,
975                                      mdd_object_capa(env, mdd_tobj));
976         if (rc)
977                 GOTO(out_unlock, rc);
978
979         rc = mdo_ref_add(env, mdd_sobj, handle);
980         if (rc != 0) {
981                 __mdd_index_delete_only(env, mdd_tobj, name, handle,
982                                         mdd_object_capa(env, mdd_tobj));
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, 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 int mdd_declare_unlink(const struct lu_env *env, struct mdd_device *mdd,
1081                               struct mdd_object *p, struct mdd_object *c,
1082                               const struct lu_name *name, struct md_attr *ma,
1083                               struct thandle *handle)
1084 {
1085         int rc;
1086
1087         rc = mdo_declare_index_delete(env, p, name->ln_name, handle);
1088         if (rc)
1089                 return rc;
1090
1091         rc = mdo_declare_ref_del(env, p, handle);
1092         if (rc)
1093                 return rc;
1094
1095         rc = mdo_declare_ref_del(env, c, handle);
1096         if (rc)
1097                 return rc;
1098
1099         rc = mdo_declare_ref_del(env, c, handle);
1100         if (rc)
1101                 return rc;
1102
1103         rc = mdo_declare_attr_set(env, p, NULL, handle);
1104         if (rc)
1105                 return rc;
1106
1107         rc = mdo_declare_attr_set(env, c, NULL, handle);
1108         if (rc)
1109                 return rc;
1110
1111         rc = mdd_declare_finish_unlink(env, c, ma, handle);
1112         if (rc)
1113                 return rc;
1114
1115         rc = mdd_declare_links_add(env, c, handle);
1116         if (rc)
1117                 return rc;
1118
1119         rc = mdd_declare_changelog_store(env, mdd, name, handle);
1120
1121         return rc;
1122 }
1123
1124 static int mdd_unlink(const struct lu_env *env, struct md_object *pobj,
1125                       struct md_object *cobj, const struct lu_name *lname,
1126                       struct md_attr *ma)
1127 {
1128         const char *name = lname->ln_name;
1129         struct lu_attr     *cattr = &mdd_env_info(env)->mti_cattr;
1130         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
1131         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1132         struct mdd_object *mdd_cobj = md2mdd_obj(cobj);
1133         struct mdd_device *mdd = mdo2mdd(pobj);
1134         struct dynlock_handle *dlh;
1135         struct thandle    *handle;
1136         int rc, is_dir;
1137         ENTRY;
1138
1139         if (mdd_object_exists(mdd_cobj) <= 0)
1140                 RETURN(-ENOENT);
1141
1142         handle = mdd_trans_create(env, mdd);
1143         if (IS_ERR(handle))
1144                 RETURN(PTR_ERR(handle));
1145
1146         rc = mdd_declare_unlink(env, mdd, mdd_pobj, mdd_cobj,
1147                                 lname, ma, handle);
1148         if (rc)
1149                 GOTO(stop, rc);
1150
1151         rc = mdd_trans_start(env, mdd, handle);
1152         if (rc)
1153                 GOTO(stop, rc);
1154
1155         dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1156         if (dlh == NULL)
1157                 GOTO(stop, rc = -ENOMEM);
1158         mdd_write_lock(env, mdd_cobj, MOR_TGT_CHILD);
1159
1160         /* fetch cattr */
1161         rc = mdd_la_get(env, mdd_cobj, cattr, mdd_object_capa(env, mdd_cobj));
1162         if (rc)
1163                 GOTO(cleanup, rc);
1164
1165         is_dir = S_ISDIR(cattr->la_mode);
1166
1167         rc = mdd_unlink_sanity_check(env, mdd_pobj, mdd_cobj, cattr);
1168         if (rc)
1169                 GOTO(cleanup, rc);
1170
1171         rc = mdo_ref_del(env, mdd_cobj, handle);
1172         if (rc != 0) {
1173                 __mdd_index_insert_only(env, mdd_pobj, mdo2fid(mdd_cobj),
1174                                         name, handle,
1175                                         mdd_object_capa(env, mdd_pobj));
1176                 GOTO(cleanup, rc);
1177         }
1178
1179         rc = __mdd_index_delete(env, mdd_pobj, name, is_dir, handle,
1180                                 mdd_object_capa(env, mdd_pobj));
1181         if (rc)
1182                 GOTO(cleanup, rc);
1183
1184         if (is_dir)
1185                 /* unlink dot */
1186                 mdo_ref_del(env, mdd_cobj, handle);
1187
1188         /* fetch updated nlink */
1189         rc = mdd_la_get(env, mdd_cobj, cattr, mdd_object_capa(env, mdd_cobj));
1190         if (rc)
1191                 GOTO(cleanup, rc);
1192
1193         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1194         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
1195
1196         la->la_valid = LA_CTIME | LA_MTIME;
1197         rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1198         if (rc)
1199                 GOTO(cleanup, rc);
1200
1201         if (cattr->la_nlink > 0 || mdd_cobj->mod_count > 0) {
1202                 /* update ctime of an unlinked file only if it is still
1203                  * opened or a link still exists */
1204                 la->la_valid = LA_CTIME;
1205                 rc = mdd_attr_check_set_internal(env, mdd_cobj, la, handle, 0);
1206                 if (rc)
1207                         GOTO(cleanup, rc);
1208         }
1209
1210         /* XXX: this transfer to ma will be removed with LOD/OSP */
1211         ma->ma_attr = *cattr;
1212         ma->ma_valid |= MA_INODE;
1213         rc = mdd_finish_unlink(env, mdd_cobj, ma, handle);
1214
1215         /* fetch updated nlink */
1216         if (rc == 0)
1217                 rc = mdd_la_get(env, mdd_cobj, cattr,
1218                                 mdd_object_capa(env, mdd_cobj));
1219
1220         if (!is_dir)
1221                 /* old files may not have link ea; ignore errors */
1222                 mdd_links_del(env, mdd_cobj, mdo2fid(mdd_pobj), lname, handle);
1223
1224         /* if object is removed then we can't get its attrs, use last get */
1225         if (cattr->la_nlink == 0) {
1226                 ma->ma_attr = *cattr;
1227                 ma->ma_valid |= MA_INODE;
1228         }
1229         EXIT;
1230 cleanup:
1231         mdd_write_unlock(env, mdd_cobj);
1232         mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1233         if (rc == 0) {
1234                 int cl_flags;
1235
1236                 cl_flags = (cattr->la_nlink == 0) ? CLF_UNLINK_LAST : 0;
1237                 if ((ma->ma_valid & MA_HSM) &&
1238                     (ma->ma_hsm.mh_flags & HS_EXISTS))
1239                         cl_flags |= CLF_UNLINK_HSM_EXISTS;
1240
1241                 rc = mdd_changelog_ns_store(env, mdd,
1242                         is_dir ? CL_RMDIR : CL_UNLINK, cl_flags,
1243                         mdd_cobj, mdd_pobj, lname, handle);
1244         }
1245
1246 stop:
1247         mdd_trans_stop(env, mdd, rc, handle);
1248
1249         return rc;
1250 }
1251
1252 /*
1253  * The permission has been checked when obj created, no need check again.
1254  */
1255 static int mdd_cd_sanity_check(const struct lu_env *env,
1256                                struct mdd_object *obj)
1257 {
1258         ENTRY;
1259
1260         /* EEXIST check */
1261         if (!obj || mdd_is_dead_obj(obj))
1262                 RETURN(-ENOENT);
1263
1264         RETURN(0);
1265
1266 }
1267
1268 static int mdd_create_data(const struct lu_env *env, struct md_object *pobj,
1269                            struct md_object *cobj, const struct md_op_spec *spec,
1270                            struct md_attr *ma)
1271 {
1272         struct mdd_device *mdd = mdo2mdd(cobj);
1273         struct mdd_object *mdd_pobj = md2mdd_obj(pobj);
1274         struct mdd_object *son = md2mdd_obj(cobj);
1275         struct thandle    *handle;
1276         const struct lu_buf *buf;
1277         struct lu_attr    *attr = &mdd_env_info(env)->mti_cattr;
1278         int                rc;
1279         ENTRY;
1280
1281         /* do not let users to create stripes via .lustre/
1282          * mdd_obf_setup() sets IMMUTE_OBJ on this directory */
1283         if (pobj && mdd_pobj->mod_flags & IMMUTE_OBJ)
1284                 RETURN(-ENOENT);
1285
1286         rc = mdd_cd_sanity_check(env, son);
1287         if (rc)
1288                 RETURN(rc);
1289
1290         if (!md_should_create(spec->sp_cr_flags))
1291                 RETURN(0);
1292
1293         /*
1294          * there are following use cases for this function:
1295          * 1) late striping - file was created with MDS_OPEN_DELAY_CREATE
1296          *    striping can be specified or not
1297          * 2) CMD?
1298          */
1299         rc = mdd_la_get(env, son, attr, mdd_object_capa(env, son));
1300         if (rc)
1301                 RETURN(rc);
1302
1303         /* calling ->ah_make_hint() is used to transfer information from parent */
1304         mdd_object_make_hint(env, mdd_pobj, son, attr);
1305
1306         handle = mdd_trans_create(env, mdd);
1307         if (IS_ERR(handle))
1308                 GOTO(out_free, rc = PTR_ERR(handle));
1309
1310         /*
1311          * XXX: Setting the lov ea is not locked but setting the attr is locked?
1312          * Should this be fixed?
1313          */
1314         CDEBUG(D_OTHER, "ea %p/%u, cr_flags %Lo, no_create %u\n",
1315                spec->u.sp_ea.eadata, spec->u.sp_ea.eadatalen,
1316                spec->sp_cr_flags, spec->no_create);
1317
1318         if (spec->no_create) {
1319                 /* replay case */
1320                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1321                                         spec->u.sp_ea.eadatalen);
1322         } else  if (!(spec->sp_cr_flags & MDS_OPEN_HAS_OBJS)) {
1323                 if (spec->sp_cr_flags & MDS_OPEN_HAS_EA) {
1324                         /* lfs setstripe */
1325                         buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1326                                                 spec->u.sp_ea.eadatalen);
1327                 } else {
1328                         buf = &LU_BUF_NULL;
1329                 }
1330         } else {
1331                 /* MDS_OPEN_HAS_OBJS is not used anymore ? */
1332                 LBUG();
1333         }
1334
1335         rc = dt_declare_xattr_set(env, mdd_object_child(son), buf,
1336                                   XATTR_NAME_LOV, 0, handle);
1337         if (rc)
1338                 GOTO(stop, rc);
1339
1340         rc = mdd_trans_start(env, mdd, handle);
1341         if (rc)
1342                 GOTO(stop, rc);
1343
1344         rc = dt_xattr_set(env, mdd_object_child(son), buf, XATTR_NAME_LOV,
1345                           0, handle, mdd_object_capa(env, son));
1346 stop:
1347         mdd_trans_stop(env, mdd, rc, handle);
1348 out_free:
1349         RETURN(rc);
1350 }
1351
1352 /* Get fid from name and parent */
1353 static int
1354 __mdd_lookup(const struct lu_env *env, struct md_object *pobj,
1355              const struct lu_name *lname, struct lu_fid* fid, int mask)
1356 {
1357         const char          *name = lname->ln_name;
1358         const struct dt_key *key = (const struct dt_key *)name;
1359         struct mdd_object   *mdd_obj = md2mdd_obj(pobj);
1360         struct mdd_device   *m = mdo2mdd(pobj);
1361         struct dt_object    *dir = mdd_object_child(mdd_obj);
1362         int rc;
1363         ENTRY;
1364
1365         if (unlikely(mdd_is_dead_obj(mdd_obj)))
1366                 RETURN(-ESTALE);
1367
1368         rc = mdd_object_exists(mdd_obj);
1369         if (unlikely(rc == 0))
1370                 RETURN(-ESTALE);
1371         else if (unlikely(rc < 0)) {
1372                 CERROR("Object "DFID" locates on remote server\n",
1373                         PFID(mdo2fid(mdd_obj)));
1374                 RETURN(-EINVAL);
1375         }
1376
1377         /* The common filename length check. */
1378         if (unlikely(lname->ln_namelen > m->mdd_dt_conf.ddp_max_name_len))
1379                 RETURN(-ENAMETOOLONG);
1380
1381         rc = mdd_permission_internal_locked(env, mdd_obj, NULL, mask,
1382                                             MOR_TGT_PARENT);
1383         if (rc)
1384                 RETURN(rc);
1385
1386         if (likely(S_ISDIR(mdd_object_type(mdd_obj)) &&
1387                    dt_try_as_dir(env, dir))) {
1388
1389                 rc = dir->do_index_ops->dio_lookup(env, dir,
1390                                                  (struct dt_rec *)fid, key,
1391                                                  mdd_object_capa(env, mdd_obj));
1392                 if (rc > 0)
1393                         rc = 0;
1394                 else if (rc == 0)
1395                         rc = -ENOENT;
1396         } else
1397                 rc = -ENOTDIR;
1398
1399         RETURN(rc);
1400 }
1401
1402 int mdd_declare_object_initialize(const struct lu_env *env,
1403                                   struct mdd_object *child,
1404                                   struct lu_attr *attr,
1405                                   struct thandle *handle)
1406 {
1407         int rc;
1408
1409         rc = mdo_declare_attr_set(env, child, attr, handle);
1410         if (rc == 0 && S_ISDIR(attr->la_mode)) {
1411                 rc = mdo_declare_index_insert(env, child, mdo2fid(child),
1412                                               dot, handle);
1413                 if (rc == 0)
1414                         rc = mdo_declare_ref_add(env, child, handle);
1415         }
1416
1417         if (rc == 0)
1418                 mdd_declare_links_add(env, child, handle);
1419
1420         return rc;
1421 }
1422
1423 int mdd_object_initialize(const struct lu_env *env, const struct lu_fid *pfid,
1424                           const struct lu_name *lname, struct mdd_object *child,
1425                           struct lu_attr *attr, struct thandle *handle,
1426                           const struct md_op_spec *spec)
1427 {
1428         int rc;
1429         ENTRY;
1430
1431         /*
1432          * Update attributes for child.
1433          *
1434          * FIXME:
1435          *  (1) the valid bits should be converted between Lustre and Linux;
1436          *  (2) maybe, the child attributes should be set in OSD when creation.
1437          */
1438
1439         rc = mdd_attr_set_internal(env, child, attr, handle, 0);
1440         if (rc != 0)
1441                 RETURN(rc);
1442
1443         if (S_ISDIR(attr->la_mode)) {
1444                 /* Add "." and ".." for newly created dir */
1445                 mdo_ref_add(env, child, handle);
1446                 rc = __mdd_index_insert_only(env, child, mdo2fid(child),
1447                                              dot, handle, BYPASS_CAPA);
1448                 if (rc == 0)
1449                         rc = __mdd_index_insert_only(env, child, pfid,
1450                                                      dotdot, handle,
1451                                                      BYPASS_CAPA);
1452                 if (rc != 0)
1453                         mdo_ref_del(env, child, handle);
1454         }
1455         if (rc == 0)
1456                 mdd_links_add(env, child, pfid, lname, handle, 1);
1457
1458         RETURN(rc);
1459 }
1460
1461 /* has not lock on pobj yet */
1462 static int mdd_create_sanity_check(const struct lu_env *env,
1463                                    struct md_object *pobj,
1464                                    struct lu_attr *pattr,
1465                                    const struct lu_name *lname,
1466                                    struct lu_attr *cattr,
1467                                    struct md_op_spec *spec)
1468 {
1469         struct mdd_thread_info *info = mdd_env_info(env);
1470         struct lu_fid     *fid       = &info->mti_fid;
1471         struct mdd_object *obj       = md2mdd_obj(pobj);
1472         struct mdd_device *m         = mdo2mdd(pobj);
1473         int lookup                   = spec->sp_cr_lookup;
1474         int rc;
1475         ENTRY;
1476
1477         /* EEXIST check */
1478         if (mdd_is_dead_obj(obj))
1479                 RETURN(-ENOENT);
1480
1481         /*
1482          * In some cases this lookup is not needed - we know before if name
1483          * exists or not because MDT performs lookup for it.
1484          * name length check is done in lookup.
1485          */
1486         if (lookup) {
1487                 /*
1488                  * Check if the name already exist, though it will be checked in
1489                  * _index_insert also, for avoiding rolling back if exists
1490                  * _index_insert.
1491                  */
1492                 rc = __mdd_lookup_locked(env, pobj, lname, fid,
1493                                          MAY_WRITE | MAY_EXEC);
1494                 if (rc != -ENOENT)
1495                         RETURN(rc ? : -EEXIST);
1496         } else {
1497                 /*
1498                  * Check WRITE permission for the parent.
1499                  * EXEC permission have been checked
1500                  * when lookup before create already.
1501                  */
1502                 rc = mdd_permission_internal_locked(env, obj, pattr, MAY_WRITE,
1503                                                     MOR_TGT_PARENT);
1504                 if (rc)
1505                         RETURN(rc);
1506         }
1507
1508         /* sgid check */
1509         if (pattr->la_mode & S_ISGID) {
1510                 cattr->la_gid = pattr->la_gid;
1511                 if (S_ISDIR(cattr->la_mode)) {
1512                         cattr->la_mode |= S_ISGID;
1513                         cattr->la_valid |= LA_MODE;
1514                 }
1515         }
1516
1517         switch (cattr->la_mode & S_IFMT) {
1518         case S_IFLNK: {
1519                 unsigned int symlen = strlen(spec->u.sp_symname) + 1;
1520
1521                 if (symlen > (1 << m->mdd_dt_conf.ddp_block_shift))
1522                         RETURN(-ENAMETOOLONG);
1523                 else
1524                         RETURN(0);
1525         }
1526         case S_IFDIR:
1527         case S_IFREG:
1528         case S_IFCHR:
1529         case S_IFBLK:
1530         case S_IFIFO:
1531         case S_IFSOCK:
1532                 rc = 0;
1533                 break;
1534         default:
1535                 rc = -EINVAL;
1536                 break;
1537         }
1538         RETURN(rc);
1539 }
1540
1541 static int mdd_declare_create(const struct lu_env *env, struct mdd_device *mdd,
1542                               struct mdd_object *p, struct mdd_object *c,
1543                               const struct lu_name *name,
1544                               struct lu_attr *attr,
1545                               int got_def_acl,
1546                               struct thandle *handle,
1547                               const struct md_op_spec *spec)
1548 {
1549         struct mdd_thread_info *info = mdd_env_info(env);
1550         int            rc = 0;
1551
1552         rc = mdd_declare_object_create_internal(env, p, c, attr, handle, spec);
1553         if (rc)
1554                 GOTO(out, rc);
1555
1556 #ifdef CONFIG_FS_POSIX_ACL
1557         if (got_def_acl > 0) {
1558                 struct lu_buf *acl_buf;
1559
1560                 acl_buf = mdd_buf_get(env, NULL, got_def_acl);
1561                 /* if dir, then can inherit default ACl */
1562                 if (S_ISDIR(attr->la_mode)) {
1563                         rc = mdo_declare_xattr_set(env, c, acl_buf,
1564                                                    XATTR_NAME_ACL_DEFAULT,
1565                                                    0, handle);
1566                         if (rc)
1567                                 GOTO(out, rc);
1568                 }
1569
1570                 rc = mdo_declare_attr_set(env, c, &info->mti_pattr, handle);
1571                 if (rc)
1572                         GOTO(out, rc);
1573
1574                 rc = mdo_declare_xattr_set(env, c, acl_buf,
1575                                            XATTR_NAME_ACL_ACCESS, 0, handle);
1576                 if (rc)
1577                         GOTO(out, rc);
1578         }
1579 #endif
1580
1581         if (S_ISDIR(attr->la_mode)) {
1582                 rc = mdo_declare_ref_add(env, p, handle);
1583                 if (rc)
1584                         GOTO(out, rc);
1585         }
1586
1587         rc = mdd_declare_object_initialize(env, c, attr, handle);
1588         if (rc)
1589                 GOTO(out, rc);
1590
1591         rc = mdo_declare_index_insert(env, p, mdo2fid(c),
1592                                       name->ln_name, handle);
1593         if (rc)
1594                 GOTO(out, rc);
1595
1596         /* replay case, create LOV EA from client data */
1597         if (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1598                 const struct lu_buf *buf;
1599
1600                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1601                                         spec->u.sp_ea.eadatalen);
1602                 rc = mdo_declare_xattr_set(env, c, buf, XATTR_NAME_LOV,
1603                                            0, handle);
1604                 if (rc)
1605                         GOTO(out, rc);
1606         }
1607
1608         if (S_ISLNK(attr->la_mode)) {
1609                 rc = dt_declare_record_write(env, mdd_object_child(c),
1610                                              strlen(spec->u.sp_symname), 0,
1611                                              handle);
1612                 if (rc)
1613                         GOTO(out, rc);
1614         }
1615
1616         rc = mdo_declare_attr_set(env, p, attr, handle);
1617         if (rc)
1618                 return rc;
1619
1620         rc = mdd_declare_changelog_store(env, mdd, name, handle);
1621         if (rc)
1622                 return rc;
1623
1624 out:
1625         return rc;
1626 }
1627
1628
1629 /*
1630  * Create object and insert it into namespace.
1631  */
1632 static int mdd_create(const struct lu_env *env, struct md_object *pobj,
1633                       const struct lu_name *lname, struct md_object *child,
1634                       struct md_op_spec *spec, struct md_attr* ma)
1635 {
1636         struct mdd_thread_info *info = mdd_env_info(env);
1637         struct lu_attr         *la = &info->mti_la_for_fix;
1638         struct mdd_object      *mdd_pobj = md2mdd_obj(pobj);
1639         struct mdd_object      *son = md2mdd_obj(child);
1640         struct mdd_device      *mdd = mdo2mdd(pobj);
1641         struct lu_attr         *attr = &ma->ma_attr;
1642         struct thandle         *handle;
1643         struct lu_attr         *pattr = &info->mti_pattr;
1644         struct dynlock_handle  *dlh;
1645         const char             *name = lname->ln_name;
1646         int rc, created = 0, initialized = 0, inserted = 0;
1647         int got_def_acl = 0;
1648         ENTRY;
1649
1650         /*
1651          * Two operations have to be performed:
1652          *
1653          *  - an allocation of a new object (->do_create()), and
1654          *
1655          *  - an insertion into a parent index (->dio_insert()).
1656          *
1657          * Due to locking, operation order is not important, when both are
1658          * successful, *but* error handling cases are quite different:
1659          *
1660          *  - if insertion is done first, and following object creation fails,
1661          *  insertion has to be rolled back, but this operation might fail
1662          *  also leaving us with dangling index entry.
1663          *
1664          *  - if creation is done first, is has to be undone if insertion
1665          *  fails, leaving us with leaked space, which is neither good, nor
1666          *  fatal.
1667          *
1668          * It seems that creation-first is simplest solution, but it is
1669          * sub-optimal in the frequent
1670          *
1671          *         $ mkdir foo
1672          *         $ mkdir foo
1673          *
1674          * case, because second mkdir is bound to create object, only to
1675          * destroy it immediately.
1676          *
1677          * To avoid this follow local file systems that do double lookup:
1678          *
1679          *     0. lookup -> -EEXIST (mdd_create_sanity_check())
1680          *
1681          *     1. create            (mdd_object_create_internal())
1682          *
1683          *     2. insert            (__mdd_index_insert(), lookup again)
1684          */
1685
1686         rc = mdd_la_get(env, mdd_pobj, pattr, BYPASS_CAPA);
1687         if (rc != 0)
1688                 RETURN(rc);
1689
1690         /* Sanity checks before big job. */
1691         rc = mdd_create_sanity_check(env, pobj, pattr, lname, attr, spec);
1692         if (rc)
1693                 RETURN(rc);
1694
1695         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DQACQ_NET))
1696                 GOTO(out_free, rc = -EINPROGRESS);
1697
1698         if (!S_ISLNK(attr->la_mode)) {
1699                 struct lu_buf *acl_buf;
1700
1701                 acl_buf = mdd_buf_get(env, info->mti_xattr_buf,
1702                                 sizeof(info->mti_xattr_buf));
1703                 mdd_read_lock(env, mdd_pobj, MOR_TGT_PARENT);
1704                 rc = mdo_xattr_get(env, mdd_pobj, acl_buf,
1705                                 XATTR_NAME_ACL_DEFAULT, BYPASS_CAPA);
1706                 mdd_read_unlock(env, mdd_pobj);
1707                 if (rc > 0)
1708                         got_def_acl = rc;
1709                 else if (rc < 0 && rc != -EOPNOTSUPP && rc != -ENODATA)
1710                         GOTO(out_free, rc);
1711         }
1712
1713         mdd_object_make_hint(env, mdd_pobj, son, attr);
1714
1715         handle = mdd_trans_create(env, mdd);
1716         if (IS_ERR(handle))
1717                 GOTO(out_free, rc = PTR_ERR(handle));
1718
1719         rc = mdd_declare_create(env, mdd, mdd_pobj, son, lname, attr,
1720                                 got_def_acl, handle, spec);
1721         if (rc)
1722                 GOTO(out_stop, rc);
1723
1724         rc = mdd_trans_start(env, mdd, handle);
1725         if (rc)
1726                 GOTO(out_stop, rc);
1727
1728         dlh = mdd_pdo_write_lock(env, mdd_pobj, name, MOR_TGT_PARENT);
1729         if (dlh == NULL)
1730                 GOTO(out_trans, rc = -ENOMEM);
1731
1732         mdd_write_lock(env, son, MOR_TGT_CHILD);
1733         rc = mdd_object_create_internal(env, mdd_pobj, son, attr, handle, spec);
1734         if (rc) {
1735                 mdd_write_unlock(env, son);
1736                 GOTO(cleanup, rc);
1737         }
1738
1739         created = 1;
1740
1741 #ifdef CONFIG_FS_POSIX_ACL
1742         if (got_def_acl) {
1743                 struct lu_buf *acl_buf;
1744
1745                 acl_buf = mdd_buf_get(env, info->mti_xattr_buf, got_def_acl);
1746                 rc = __mdd_acl_init(env, son, acl_buf, &attr->la_mode, handle);
1747                 if (rc) {
1748                         mdd_write_unlock(env, son);
1749                         GOTO(cleanup, rc);
1750                 }
1751         }
1752 #endif
1753
1754         rc = mdd_object_initialize(env, mdo2fid(mdd_pobj), lname,
1755                                    son, attr, handle, spec);
1756
1757         /*
1758          * in case of replay we just set LOVEA provided by the client
1759          * XXX: I think it would be interesting to try "old" way where
1760          *      MDT calls this xattr_set(LOV) in a different transaction.
1761          *      probably this way we code can be made better.
1762          */
1763         if (rc == 0 &&
1764                         (spec->no_create || (spec->sp_cr_flags & MDS_OPEN_HAS_EA))) {
1765                 const struct lu_buf *buf;
1766
1767                 buf = mdd_buf_get_const(env, spec->u.sp_ea.eadata,
1768                                 spec->u.sp_ea.eadatalen);
1769                 rc = mdo_xattr_set(env, son, buf, XATTR_NAME_LOV, 0, handle,
1770                                 BYPASS_CAPA);
1771         }
1772         mdd_write_unlock(env, son);
1773         if (rc)
1774                 /*
1775                  * Object has no links, so it will be destroyed when last
1776                  * reference is released. (XXX not now.)
1777                  */
1778                 GOTO(cleanup, rc);
1779
1780         initialized = 1;
1781
1782         rc = __mdd_index_insert(env, mdd_pobj, mdo2fid(son),
1783                                 name, S_ISDIR(attr->la_mode), handle,
1784                                 mdd_object_capa(env, mdd_pobj));
1785         if (rc)
1786                 GOTO(cleanup, rc);
1787
1788         inserted = 1;
1789
1790         if (S_ISLNK(attr->la_mode)) {
1791                 struct md_ucred  *uc = md_ucred(env);
1792                 struct dt_object *dt = mdd_object_child(son);
1793                 const char *target_name = spec->u.sp_symname;
1794                 int sym_len = strlen(target_name);
1795                 const struct lu_buf *buf;
1796                 loff_t pos = 0;
1797
1798                 buf = mdd_buf_get_const(env, target_name, sym_len);
1799                 rc = dt->do_body_ops->dbo_write(env, dt, buf, &pos, handle,
1800                                                 mdd_object_capa(env, son),
1801                                                 uc->mu_cap &
1802                                                 CFS_CAP_SYS_RESOURCE_MASK);
1803
1804                 if (rc == sym_len)
1805                         rc = 0;
1806                 else
1807                         GOTO(cleanup, rc = -EFAULT);
1808         }
1809
1810         *la = *attr;
1811         la->la_valid = LA_CTIME | LA_MTIME;
1812         rc = mdd_attr_check_set_internal(env, mdd_pobj, la, handle, 0);
1813         if (rc)
1814                 GOTO(cleanup, rc);
1815
1816         EXIT;
1817 cleanup:
1818         if (rc != 0 && created != 0) {
1819                 int rc2;
1820
1821                 if (inserted != 0) {
1822                         rc2 = __mdd_index_delete(env, mdd_pobj, name,
1823                                                  S_ISDIR(attr->la_mode),
1824                                                  handle, BYPASS_CAPA);
1825                         if (rc2 != 0)
1826                                 goto out_stop;
1827                 }
1828
1829                 mdd_write_lock(env, son, MOR_TGT_CHILD);
1830                 if (initialized != 0 && S_ISDIR(attr->la_mode)) {
1831                         /* Drop the reference, no need to delete "."/"..",
1832                          * because the object to be destroied directly. */
1833                         rc2 = mdo_ref_del(env, son, handle);
1834                         if (rc2 != 0) {
1835                                 mdd_write_unlock(env, son);
1836                                 goto out_stop;
1837                         }
1838                 }
1839
1840                 rc2 = mdo_ref_del(env, son, handle);
1841                 if (rc2 != 0) {
1842                         mdd_write_unlock(env, son);
1843                         goto out_stop;
1844                 }
1845
1846                 mdo_destroy(env, son, handle);
1847                 mdd_write_unlock(env, son);
1848         }
1849
1850         mdd_pdo_write_unlock(env, mdd_pobj, dlh);
1851 out_trans:
1852         if (rc == 0)
1853                 rc = mdd_changelog_ns_store(env, mdd,
1854                         S_ISDIR(attr->la_mode) ? CL_MKDIR :
1855                         S_ISREG(attr->la_mode) ? CL_CREATE :
1856                         S_ISLNK(attr->la_mode) ? CL_SOFTLINK : CL_MKNOD,
1857                         0, son, mdd_pobj, lname, handle);
1858 out_stop:
1859         mdd_trans_stop(env, mdd, rc, handle);
1860 out_free:
1861         /* The child object shouldn't be cached anymore */
1862         if (rc)
1863                 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE,
1864                             &child->mo_lu.lo_header->loh_flags);
1865         return rc;
1866 }
1867
1868 /*
1869  * Get locks on parents in proper order
1870  * RETURN: < 0 - error, rename_order if successful
1871  */
1872 enum rename_order {
1873         MDD_RN_SAME,
1874         MDD_RN_SRCTGT,
1875         MDD_RN_TGTSRC
1876 };
1877
1878 static int mdd_rename_order(const struct lu_env *env,
1879                             struct mdd_device *mdd,
1880                             struct mdd_object *src_pobj,
1881                             struct mdd_object *tgt_pobj)
1882 {
1883         /* order of locking, 1 - tgt-src, 0 - src-tgt*/
1884         int rc;
1885         ENTRY;
1886
1887         if (src_pobj == tgt_pobj)
1888                 RETURN(MDD_RN_SAME);
1889
1890         /* compared the parent child relationship of src_p&tgt_p */
1891         if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(src_pobj))){
1892                 rc = MDD_RN_SRCTGT;
1893         } else if (lu_fid_eq(&mdd->mdd_root_fid, mdo2fid(tgt_pobj))) {
1894                 rc = MDD_RN_TGTSRC;
1895         } else {
1896                 rc = mdd_is_parent(env, mdd, src_pobj, mdo2fid(tgt_pobj), NULL);
1897                 if (rc == -EREMOTE)
1898                         rc = 0;
1899
1900                 if (rc == 1)
1901                         rc = MDD_RN_TGTSRC;
1902                 else if (rc == 0)
1903                         rc = MDD_RN_SRCTGT;
1904         }
1905
1906         RETURN(rc);
1907 }
1908
1909 /* has not mdd_write{read}_lock on any obj yet. */
1910 static int mdd_rename_sanity_check(const struct lu_env *env,
1911                                    struct mdd_object *src_pobj,
1912                                    struct mdd_object *tgt_pobj,
1913                                    struct mdd_object *sobj,
1914                                    struct mdd_object *tobj,
1915                                    struct lu_attr *so_attr,
1916                                    struct lu_attr *tg_attr)
1917 {
1918         int rc = 0;
1919         ENTRY;
1920
1921         /* XXX: when get here, sobj must NOT be NULL,
1922          * the other case has been processed in cml_rename
1923          * before mdd_rename and enable MDS_PERM_BYPASS. */
1924         LASSERT(sobj);
1925
1926         rc = mdd_may_delete(env, src_pobj, sobj, so_attr, NULL, 1, 0);
1927         if (rc)
1928                 RETURN(rc);
1929
1930         /* XXX: when get here, "tobj == NULL" means tobj must
1931          * NOT exist (neither on remote MDS, such case has been
1932          * processed in cml_rename before mdd_rename and enable
1933          * MDS_PERM_BYPASS).
1934          * So check may_create, but not check may_unlink. */
1935         if (!tobj)
1936                 rc = mdd_may_create(env, tgt_pobj, NULL,
1937                                     (src_pobj != tgt_pobj), 0);
1938         else
1939                 rc = mdd_may_delete(env, tgt_pobj, tobj, tg_attr, so_attr,
1940                                     (src_pobj != tgt_pobj), 1);
1941
1942         if (!rc && !tobj && (src_pobj != tgt_pobj) &&
1943             S_ISDIR(so_attr->la_mode))
1944                 rc = __mdd_may_link(env, tgt_pobj);
1945
1946         RETURN(rc);
1947 }
1948
1949 static int mdd_declare_rename(const struct lu_env *env,
1950                               struct mdd_device *mdd,
1951                               struct mdd_object *mdd_spobj,
1952                               struct mdd_object *mdd_tpobj,
1953                               struct mdd_object *mdd_sobj,
1954                               struct mdd_object *mdd_tobj,
1955                               const struct lu_name *tname,
1956                               const struct lu_name *sname,
1957                               struct md_attr *ma,
1958                               struct thandle *handle)
1959 {
1960         int rc;
1961
1962         LASSERT(mdd_spobj);
1963         LASSERT(mdd_tpobj);
1964         LASSERT(mdd_sobj);
1965
1966         /* name from source dir */
1967         rc = mdo_declare_index_delete(env, mdd_spobj, sname->ln_name, handle);
1968         if (rc)
1969                 return rc;
1970
1971         /* .. from source child */
1972         if (S_ISDIR(mdd_object_type(mdd_sobj))) {
1973                 /* source child can be directory,
1974                  * counted by source dir's nlink */
1975                 rc = mdo_declare_ref_del(env, mdd_spobj, handle);
1976                 if (rc)
1977                         return rc;
1978
1979                 rc = mdo_declare_index_delete(env, mdd_sobj, dotdot, handle);
1980                 if (rc)
1981                         return rc;
1982
1983                 rc = mdo_declare_index_insert(env, mdd_sobj, mdo2fid(mdd_tpobj),
1984                                               dotdot, handle);
1985                 if (rc)
1986                         return rc;
1987
1988                 /* new target child can be directory,
1989                  * counted by target dir's nlink */
1990                 rc = mdo_declare_ref_add(env, mdd_tpobj, handle);
1991                 if (rc)
1992                         return rc;
1993
1994         }
1995
1996         rc = mdo_declare_attr_set(env, mdd_spobj, NULL, handle);
1997         if (rc)
1998                 return rc;
1999
2000         rc = mdo_declare_attr_set(env, mdd_sobj, NULL, handle);
2001         if (rc)
2002                 return rc;
2003         mdd_declare_links_add(env, mdd_sobj, handle);
2004         if (rc)
2005                 return rc;
2006
2007         rc = mdo_declare_attr_set(env, mdd_tpobj, NULL, handle);
2008         if (rc)
2009                 return rc;
2010
2011         /* new name */
2012         rc = mdo_declare_index_insert(env, mdd_tpobj, mdo2fid(mdd_sobj),
2013                         tname->ln_name, handle);
2014         if (rc)
2015                 return rc;
2016
2017         /* name from target dir (old name), we declare it unconditionally
2018          * as mdd_rename() calls delete unconditionally as well. so just
2019          * to balance declarations vs calls to change ... */
2020         rc = mdo_declare_index_delete(env, mdd_tpobj, tname->ln_name, handle);
2021         if (rc)
2022                 return rc;
2023
2024         if (mdd_tobj && mdd_object_exists(mdd_tobj)) {
2025                 /* delete target child in target parent directory */
2026                 rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2027                 if (rc)
2028                         return rc;
2029
2030                 if (S_ISDIR(mdd_object_type(mdd_tobj))) {
2031                         /* target child can be directory,
2032                          * delete "." reference in target child directory */
2033                         rc = mdo_declare_ref_del(env, mdd_tobj, handle);
2034                         if (rc)
2035                                 return rc;
2036
2037                         /* delete ".." reference in target parent directory */
2038                         rc = mdo_declare_ref_del(env, mdd_tpobj, handle);
2039                         if (rc)
2040                                 return rc;
2041                 }
2042
2043                 rc = mdo_declare_attr_set(env, mdd_tobj, NULL, handle);
2044                 if (rc)
2045                         return rc;
2046
2047                 mdd_declare_links_add(env, mdd_tobj, handle);
2048                 if (rc)
2049                         return rc;
2050
2051                 rc = mdd_declare_finish_unlink(env, mdd_tobj, ma, handle);
2052                 if (rc)
2053                         return rc;
2054         }
2055
2056         rc = mdd_declare_changelog_ext_store(env, mdd, tname, sname, handle);
2057         if (rc)
2058                 return rc;
2059
2060         return rc;
2061 }
2062
2063 /* src object can be remote that is why we use only fid and type of object */
2064 static int mdd_rename(const struct lu_env *env,
2065                       struct md_object *src_pobj, struct md_object *tgt_pobj,
2066                       const struct lu_fid *lf, const struct lu_name *lsname,
2067                       struct md_object *tobj, const struct lu_name *ltname,
2068                       struct md_attr *ma)
2069 {
2070         const char *sname = lsname->ln_name;
2071         const char *tname = ltname->ln_name;
2072         struct lu_attr    *la = &mdd_env_info(env)->mti_la_for_fix;
2073         struct lu_attr    *so_attr = &mdd_env_info(env)->mti_cattr;
2074         struct lu_attr    *tg_attr = &mdd_env_info(env)->mti_pattr;
2075         struct mdd_object *mdd_spobj = md2mdd_obj(src_pobj); /* source parent */
2076         struct mdd_object *mdd_tpobj = md2mdd_obj(tgt_pobj);
2077         struct mdd_device *mdd = mdo2mdd(src_pobj);
2078         struct mdd_object *mdd_sobj = NULL;                  /* source object */
2079         struct mdd_object *mdd_tobj = NULL;
2080         struct dynlock_handle *sdlh, *tdlh;
2081         struct thandle *handle;
2082         const struct lu_fid *tpobj_fid = mdo2fid(mdd_tpobj);
2083         const struct lu_fid *spobj_fid = mdo2fid(mdd_spobj);
2084         bool is_dir;
2085         bool tobj_ref = 0;
2086         bool tobj_locked = 0;
2087         unsigned cl_flags = 0;
2088         int rc, rc2;
2089         ENTRY;
2090
2091         if (tobj)
2092                 mdd_tobj = md2mdd_obj(tobj);
2093
2094         mdd_sobj = mdd_object_find(env, mdd, lf);
2095
2096         handle = mdd_trans_create(env, mdd);
2097         if (IS_ERR(handle))
2098                 GOTO(out_pending, rc = PTR_ERR(handle));
2099
2100         rc = mdd_declare_rename(env, mdd, mdd_spobj, mdd_tpobj, mdd_sobj,
2101                                 mdd_tobj, lsname, ltname, ma, handle);
2102         if (rc)
2103                 GOTO(stop, rc);
2104
2105         rc = mdd_trans_start(env, mdd, handle);
2106         if (rc)
2107                 GOTO(stop, rc);
2108
2109         /* FIXME: Should consider tobj and sobj too in rename_lock. */
2110         rc = mdd_rename_order(env, mdd, mdd_spobj, mdd_tpobj);
2111         if (rc < 0)
2112                 GOTO(cleanup_unlocked, rc);
2113
2114         /* Get locks in determined order */
2115         if (rc == MDD_RN_SAME) {
2116                 sdlh = mdd_pdo_write_lock(env, mdd_spobj,
2117                                           sname, MOR_SRC_PARENT);
2118                 /* check hashes to determine do we need one lock or two */
2119                 if (mdd_name2hash(sname) != mdd_name2hash(tname))
2120                         tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,
2121                                 MOR_TGT_PARENT);
2122                 else
2123                         tdlh = sdlh;
2124         } else if (rc == MDD_RN_SRCTGT) {
2125                 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_SRC_PARENT);
2126                 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_TGT_PARENT);
2127         } else {
2128                 tdlh = mdd_pdo_write_lock(env, mdd_tpobj, tname,MOR_SRC_PARENT);
2129                 sdlh = mdd_pdo_write_lock(env, mdd_spobj, sname,MOR_TGT_PARENT);
2130         }
2131         if (sdlh == NULL || tdlh == NULL)
2132                 GOTO(cleanup, rc = -ENOMEM);
2133
2134         rc = mdd_la_get(env, mdd_sobj, so_attr,
2135                         mdd_object_capa(env, mdd_sobj));
2136         if (rc)
2137                 GOTO(cleanup, rc);
2138
2139         if (mdd_tobj) {
2140                 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2141                                 mdd_object_capa(env, mdd_tobj));
2142                 if (rc)
2143                         GOTO(cleanup, rc);
2144         }
2145
2146         rc = mdd_rename_sanity_check(env, mdd_spobj, mdd_tpobj, mdd_sobj,
2147                                      mdd_tobj, so_attr, tg_attr);
2148         if (rc)
2149                 GOTO(cleanup, rc);
2150
2151         is_dir = S_ISDIR(so_attr->la_mode);
2152
2153         /* Remove source name from source directory */
2154         rc = __mdd_index_delete(env, mdd_spobj, sname, is_dir, handle,
2155                                 mdd_object_capa(env, mdd_spobj));
2156         if (rc)
2157                 GOTO(cleanup, rc);
2158
2159         /* "mv dir1 dir2" needs "dir1/.." link update */
2160         if (is_dir && mdd_sobj && !lu_fid_eq(spobj_fid, tpobj_fid)) {
2161                 rc = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2162                                         mdd_object_capa(env, mdd_sobj));
2163                 if (rc)
2164                         GOTO(fixup_spobj2, rc);
2165
2166                 rc = __mdd_index_insert_only(env, mdd_sobj, tpobj_fid, dotdot,
2167                                       handle, mdd_object_capa(env, mdd_sobj));
2168                 if (rc)
2169                         GOTO(fixup_spobj, rc);
2170         }
2171
2172         /* Remove target name from target directory
2173          * Here tobj can be remote one, so we do index_delete unconditionally
2174          * and -ENOENT is allowed.
2175          */
2176         rc = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2177                                 mdd_object_capa(env, mdd_tpobj));
2178         if (rc != 0) {
2179                 if (mdd_tobj) {
2180                         /* tname might been renamed to something else */
2181                         GOTO(fixup_spobj, rc);
2182                 }
2183                 if (rc != -ENOENT)
2184                         GOTO(fixup_spobj, rc);
2185         }
2186
2187         /* Insert new fid with target name into target dir */
2188         rc = __mdd_index_insert(env, mdd_tpobj, lf, tname, is_dir, handle,
2189                                 mdd_object_capa(env, mdd_tpobj));
2190         if (rc)
2191                 GOTO(fixup_tpobj, rc);
2192
2193         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
2194         la->la_ctime = la->la_mtime = ma->ma_attr.la_ctime;
2195
2196         /* XXX: mdd_sobj must be local one if it is NOT NULL. */
2197         if (mdd_sobj) {
2198                 la->la_valid = LA_CTIME;
2199                 rc = mdd_attr_check_set_internal(env, mdd_sobj, la, handle, 0);
2200                 if (rc)
2201                         GOTO(fixup_tpobj, rc);
2202         }
2203
2204         /* Remove old target object
2205          * For tobj is remote case cmm layer has processed
2206          * and set tobj to NULL then. So when tobj is NOT NULL,
2207          * it must be local one.
2208          */
2209         if (tobj && mdd_object_exists(mdd_tobj)) {
2210                 mdd_write_lock(env, mdd_tobj, MOR_TGT_CHILD);
2211                 tobj_locked = 1;
2212                 if (mdd_is_dead_obj(mdd_tobj)) {
2213                         /* shld not be dead, something is wrong */
2214                         CERROR("tobj is dead, something is wrong\n");
2215                         rc = -EINVAL;
2216                         goto cleanup;
2217                 }
2218                 mdo_ref_del(env, mdd_tobj, handle);
2219
2220                 /* Remove dot reference. */
2221                 if (S_ISDIR(tg_attr->la_mode))
2222                         mdo_ref_del(env, mdd_tobj, handle);
2223                 tobj_ref = 1;
2224
2225                 /* fetch updated nlink */
2226                 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2227                                 mdd_object_capa(env, mdd_tobj));
2228                 if (rc != 0) {
2229                         CERROR("%s: Failed to get nlink for tobj "
2230                                 DFID": rc = %d\n",
2231                                 mdd2obd_dev(mdd)->obd_name,
2232                                 PFID(tpobj_fid), rc);
2233                         GOTO(fixup_tpobj, rc);
2234                 }
2235
2236                 la->la_valid = LA_CTIME;
2237                 rc = mdd_attr_check_set_internal(env, mdd_tobj, la, handle, 0);
2238                 if (rc != 0) {
2239                         CERROR("%s: Failed to set ctime for tobj "
2240                                 DFID": rc = %d\n",
2241                                 mdd2obd_dev(mdd)->obd_name,
2242                                 PFID(tpobj_fid), rc);
2243                         GOTO(fixup_tpobj, rc);
2244                 }
2245
2246                 /* XXX: this transfer to ma will be removed with LOD/OSP */
2247                 ma->ma_attr = *tg_attr;
2248                 ma->ma_valid |= MA_INODE;
2249                 rc = mdd_finish_unlink(env, mdd_tobj, ma, handle);
2250                 if (rc != 0) {
2251                         CERROR("%s: Failed to unlink tobj "
2252                                 DFID": rc = %d\n",
2253                                 mdd2obd_dev(mdd)->obd_name,
2254                                 PFID(tpobj_fid), rc);
2255                         GOTO(fixup_tpobj, rc);
2256                 }
2257
2258                 /* fetch updated nlink */
2259                 rc = mdd_la_get(env, mdd_tobj, tg_attr,
2260                                 mdd_object_capa(env, mdd_tobj));
2261                 if (rc != 0) {
2262                         CERROR("%s: Failed to get nlink for tobj "
2263                                 DFID": rc = %d\n",
2264                                 mdd2obd_dev(mdd)->obd_name,
2265                                 PFID(tpobj_fid), rc);
2266                         GOTO(fixup_tpobj, rc);
2267                 }
2268                 /* XXX: this transfer to ma will be removed with LOD/OSP */
2269                 ma->ma_attr = *tg_attr;
2270                 ma->ma_valid |= MA_INODE;
2271
2272                 if (so_attr->la_nlink == 0)
2273                         cl_flags |= CLF_RENAME_LAST;
2274         }
2275
2276         la->la_valid = LA_CTIME | LA_MTIME;
2277         rc = mdd_attr_check_set_internal(env, mdd_spobj, la, handle, 0);
2278         if (rc)
2279                 GOTO(fixup_tpobj, rc);
2280
2281         if (mdd_spobj != mdd_tpobj) {
2282                 la->la_valid = LA_CTIME | LA_MTIME;
2283                 rc = mdd_attr_check_set_internal(env, mdd_tpobj, la,
2284                                                  handle, 0);
2285         }
2286
2287         if (rc == 0 && mdd_sobj) {
2288                 mdd_write_lock(env, mdd_sobj, MOR_SRC_CHILD);
2289                 rc = mdd_links_rename(env, mdd_sobj, mdo2fid(mdd_spobj), lsname,
2290                                       mdo2fid(mdd_tpobj), ltname, handle, 0, 0);
2291                 if (rc == -ENOENT)
2292                         /* Old files might not have EA entry */
2293                         mdd_links_add(env, mdd_sobj, mdo2fid(mdd_spobj),
2294                                       lsname, handle, 0);
2295                 mdd_write_unlock(env, mdd_sobj);
2296                 /* We don't fail the transaction if the link ea can't be
2297                    updated -- fid2path will use alternate lookup method. */
2298                 rc = 0;
2299         }
2300
2301         EXIT;
2302
2303 fixup_tpobj:
2304         if (rc) {
2305                 rc2 = __mdd_index_delete(env, mdd_tpobj, tname, is_dir, handle,
2306                                          BYPASS_CAPA);
2307                 if (rc2)
2308                         CWARN("tp obj fix error %d\n",rc2);
2309
2310                 if (mdd_tobj && mdd_object_exists(mdd_tobj) &&
2311                     !mdd_is_dead_obj(mdd_tobj)) {
2312                         if (tobj_ref) {
2313                                 mdo_ref_add(env, mdd_tobj, handle);
2314                                 if (is_dir)
2315                                         mdo_ref_add(env, mdd_tobj, handle);
2316                         }
2317
2318                         rc2 = __mdd_index_insert(env, mdd_tpobj,
2319                                          mdo2fid(mdd_tobj), tname,
2320                                          is_dir, handle,
2321                                          BYPASS_CAPA);
2322
2323                         if (rc2)
2324                                 CWARN("tp obj fix error %d\n",rc2);
2325                 }
2326         }
2327
2328 fixup_spobj:
2329         if (rc && is_dir && mdd_sobj) {
2330                 rc2 = __mdd_index_delete_only(env, mdd_sobj, dotdot, handle,
2331                                               BYPASS_CAPA);
2332
2333                 if (rc2)
2334                         CWARN("sp obj dotdot delete error %d\n",rc2);
2335
2336
2337                 rc2 = __mdd_index_insert_only(env, mdd_sobj, spobj_fid,
2338                                               dotdot, handle, BYPASS_CAPA);
2339                 if (rc2)
2340                         CWARN("sp obj dotdot insert error %d\n",rc2);
2341         }
2342
2343 fixup_spobj2:
2344         if (rc) {
2345                 rc2 = __mdd_index_insert(env, mdd_spobj,
2346                                          lf, sname, is_dir, handle, BYPASS_CAPA);
2347                 if (rc2)
2348                         CWARN("sp obj fix error %d\n",rc2);
2349         }
2350 cleanup:
2351         if (tobj_locked)
2352                 mdd_write_unlock(env, mdd_tobj);
2353         if (likely(tdlh) && sdlh != tdlh)
2354                 mdd_pdo_write_unlock(env, mdd_tpobj, tdlh);
2355         if (likely(sdlh))
2356                 mdd_pdo_write_unlock(env, mdd_spobj, sdlh);
2357 cleanup_unlocked:
2358         if (rc == 0)
2359                 rc = mdd_changelog_ext_ns_store(env, mdd, CL_RENAME, cl_flags,
2360                                                 mdd_tobj, tpobj_fid, lf,
2361                                                 spobj_fid, ltname, lsname,
2362                                                 handle);
2363
2364 stop:
2365         mdd_trans_stop(env, mdd, rc, handle);
2366         if (mdd_sobj)
2367                 mdd_object_put(env, mdd_sobj);
2368 out_pending:
2369         return rc;
2370 }
2371
2372 /**
2373  * The data that link search is done on.
2374  */
2375 struct mdd_link_data {
2376         /**
2377          * Buffer to keep link EA body.
2378          */
2379         struct lu_buf           *ml_buf;
2380         /**
2381          * The matched header, entry and its lenght in the EA
2382          */
2383         struct link_ea_header   *ml_leh;
2384         struct link_ea_entry    *ml_lee;
2385         int                      ml_reclen;
2386 };
2387
2388 static int mdd_links_new(const struct lu_env *env,
2389                          struct mdd_link_data *ldata)
2390 {
2391         ldata->ml_buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2392         if (ldata->ml_buf->lb_buf == NULL)
2393                 return -ENOMEM;
2394         ldata->ml_leh = ldata->ml_buf->lb_buf;
2395         ldata->ml_leh->leh_magic = LINK_EA_MAGIC;
2396         ldata->ml_leh->leh_len = sizeof(struct link_ea_header);
2397         ldata->ml_leh->leh_reccount = 0;
2398         return 0;
2399 }
2400
2401 /** Read the link EA into a temp buffer.
2402  * Uses the mdd_thread_info::mti_big_buf since it is generally large.
2403  * A pointer to the buffer is stored in \a ldata::ml_buf.
2404  *
2405  * \retval 0 or error
2406  */
2407 int mdd_links_read(const struct lu_env *env,
2408                    struct mdd_object *mdd_obj,
2409                    struct mdd_link_data *ldata)
2410 {
2411         struct lustre_capa *capa;
2412         struct link_ea_header *leh;
2413         int rc;
2414
2415         /* First try a small buf */
2416         LASSERT(env != NULL);
2417         ldata->ml_buf = mdd_buf_alloc(env, CFS_PAGE_SIZE);
2418         if (ldata->ml_buf->lb_buf == NULL)
2419                 return -ENOMEM;
2420
2421         if (!mdd_object_exists(mdd_obj))
2422                 return -ENODATA;
2423
2424         capa = mdd_object_capa(env, mdd_obj);
2425         rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2426                            XATTR_NAME_LINK, capa);
2427         if (rc == -ERANGE) {
2428                 /* Buf was too small, figure out what we need. */
2429                 mdd_buf_put(ldata->ml_buf);
2430                 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2431                                    XATTR_NAME_LINK, capa);
2432                 if (rc < 0)
2433                         return rc;
2434                 ldata->ml_buf = mdd_buf_alloc(env, rc);
2435                 if (ldata->ml_buf->lb_buf == NULL)
2436                         return -ENOMEM;
2437                 rc = mdo_xattr_get(env, mdd_obj, ldata->ml_buf,
2438                                    XATTR_NAME_LINK, capa);
2439         }
2440         if (rc < 0)
2441                 return rc;
2442
2443         leh = ldata->ml_buf->lb_buf;
2444         if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
2445                 leh->leh_magic = LINK_EA_MAGIC;
2446                 leh->leh_reccount = __swab32(leh->leh_reccount);
2447                 leh->leh_len = __swab64(leh->leh_len);
2448                 /* entries are swabbed by mdd_lee_unpack */
2449         }
2450         if (leh->leh_magic != LINK_EA_MAGIC)
2451                 return -EINVAL;
2452         if (leh->leh_reccount == 0)
2453                 return -ENODATA;
2454
2455         ldata->ml_leh = leh;
2456         return 0;
2457 }
2458
2459 /** Read the link EA into a temp buffer.
2460  * Uses the name_buf since it is generally large.
2461  * \retval IS_ERR err
2462  * \retval ptr to \a lu_buf (always \a mti_big_buf)
2463  */
2464 struct lu_buf *mdd_links_get(const struct lu_env *env,
2465                              struct mdd_object *mdd_obj)
2466 {
2467         struct mdd_link_data ldata = { 0 };
2468         int rc;
2469
2470         rc = mdd_links_read(env, mdd_obj, &ldata);
2471         return rc ? ERR_PTR(rc) : ldata.ml_buf;
2472 }
2473
2474 static int mdd_links_write(const struct lu_env *env,
2475                            struct mdd_object *mdd_obj,
2476                            struct mdd_link_data *ldata,
2477                            struct thandle *handle)
2478 {
2479         const struct lu_buf *buf = mdd_buf_get_const(env, ldata->ml_buf->lb_buf,
2480                                                      ldata->ml_leh->leh_len);
2481         return mdo_xattr_set(env, mdd_obj, buf, XATTR_NAME_LINK, 0, handle,
2482                              mdd_object_capa(env, mdd_obj));
2483 }
2484
2485 /** Pack a link_ea_entry.
2486  * All elements are stored as chars to avoid alignment issues.
2487  * Numbers are always big-endian
2488  * \retval record length
2489  */
2490 static int mdd_lee_pack(struct link_ea_entry *lee, const struct lu_name *lname,
2491                         const struct lu_fid *pfid)
2492 {
2493         struct lu_fid   tmpfid;
2494         int             reclen;
2495
2496         fid_cpu_to_be(&tmpfid, pfid);
2497         memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
2498         memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
2499         reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
2500
2501         lee->lee_reclen[0] = (reclen >> 8) & 0xff;
2502         lee->lee_reclen[1] = reclen & 0xff;
2503         return reclen;
2504 }
2505
2506 void mdd_lee_unpack(const struct link_ea_entry *lee, int *reclen,
2507                     struct lu_name *lname, struct lu_fid *pfid)
2508 {
2509         *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
2510         memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
2511         fid_be_to_cpu(pfid, pfid);
2512         lname->ln_name = lee->lee_name;
2513         lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
2514 }
2515
2516 static int mdd_declare_links_add(const struct lu_env *env,
2517                                  struct mdd_object *mdd_obj,
2518                                  struct thandle *handle)
2519 {
2520         int rc;
2521
2522         /* XXX: max size? */
2523         rc = mdo_declare_xattr_set(env, mdd_obj,
2524                              mdd_buf_get_const(env, NULL, 4096),
2525                              XATTR_NAME_LINK, 0, handle);
2526
2527         return rc;
2528 }
2529
2530 /* For pathologic linkers, we don't want to spend lots of time scanning the
2531  * link ea.  Limit ourseleves to something reasonable; links not in the EA
2532  * can be looked up via (slower) parent lookup.
2533  */
2534 #define LINKEA_MAX_COUNT 128
2535
2536 /** Add a record to the end of link ea buf */
2537 static int mdd_links_add_buf(const struct lu_env *env,
2538                              struct mdd_link_data *ldata,
2539                              const struct lu_name *lname,
2540                              const struct lu_fid *pfid)
2541 {
2542         LASSERT(ldata->ml_leh != NULL);
2543
2544         if (lname == NULL || pfid == NULL)
2545                 return -EINVAL;
2546
2547         /* Make sure our buf is big enough for the new one */
2548         if (ldata->ml_leh->leh_reccount > LINKEA_MAX_COUNT)
2549                 return -EOVERFLOW;
2550
2551         ldata->ml_reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
2552         if (ldata->ml_leh->leh_len + ldata->ml_reclen >
2553             ldata->ml_buf->lb_len) {
2554                 if (mdd_buf_grow(env, ldata->ml_leh->leh_len +
2555                                  ldata->ml_reclen) < 0)
2556                         return -ENOMEM;
2557         }
2558
2559         ldata->ml_leh = ldata->ml_buf->lb_buf;
2560         ldata->ml_lee = ldata->ml_buf->lb_buf + ldata->ml_leh->leh_len;
2561         ldata->ml_reclen = mdd_lee_pack(ldata->ml_lee, lname, pfid);
2562         ldata->ml_leh->leh_len += ldata->ml_reclen;
2563         ldata->ml_leh->leh_reccount++;
2564         CDEBUG(D_INODE, "New link_ea name '%.*s' is added\n",
2565                lname->ln_namelen, lname->ln_name);
2566         return 0;
2567 }
2568
2569 /** Del the current record from the link ea buf */
2570 static void mdd_links_del_buf(const struct lu_env *env,
2571                               struct mdd_link_data *ldata,
2572                               const struct lu_name *lname)
2573 {
2574         LASSERT(ldata->ml_leh != NULL);
2575
2576         ldata->ml_leh->leh_reccount--;
2577         ldata->ml_leh->leh_len -= ldata->ml_reclen;
2578         memmove(ldata->ml_lee, (char *)ldata->ml_lee + ldata->ml_reclen,
2579                 (char *)ldata->ml_leh + ldata->ml_leh->leh_len -
2580                 (char *)ldata->ml_lee);
2581         CDEBUG(D_INODE, "Old link_ea name '%.*s' is removed\n",
2582                lname->ln_namelen, lname->ln_name);
2583
2584 }
2585
2586 /**
2587  * Check if such a link exists in linkEA.
2588  *
2589  * \param mdd_obj object being handled
2590  * \param pfid parent fid the link to be found for
2591  * \param lname name in the parent's directory entry pointing to this object
2592  * \param ldata link data the search to be done on
2593  *
2594  * \retval   0 success
2595  * \retval -ENOENT link does not exist
2596  * \retval -ve on error
2597  */
2598 static int mdd_links_find(const struct lu_env  *env,
2599                           struct mdd_object    *mdd_obj,
2600                           struct mdd_link_data *ldata,
2601                           const struct lu_name *lname,
2602                           const struct lu_fid  *pfid)
2603 {
2604         struct lu_name *tmpname = &mdd_env_info(env)->mti_name2;
2605         struct lu_fid  *tmpfid = &mdd_env_info(env)->mti_fid;
2606         int count;
2607
2608         LASSERT(ldata->ml_leh != NULL);
2609
2610         /* link #0 */
2611         ldata->ml_lee = (struct link_ea_entry *)(ldata->ml_leh + 1);
2612
2613         for (count = 0; count < ldata->ml_leh->leh_reccount; count++) {
2614                 mdd_lee_unpack(ldata->ml_lee, &ldata->ml_reclen,
2615                                tmpname, tmpfid);
2616                 if (tmpname->ln_namelen == lname->ln_namelen &&
2617                     lu_fid_eq(tmpfid, pfid) &&
2618                     (strncmp(tmpname->ln_name, lname->ln_name,
2619                              tmpname->ln_namelen) == 0))
2620                         break;
2621                 ldata->ml_lee = (struct link_ea_entry *)((char *)ldata->ml_lee +
2622                                                          ldata->ml_reclen);
2623         }
2624
2625         if (count == ldata->ml_leh->leh_reccount) {
2626                 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
2627                        lname->ln_namelen, lname->ln_name);
2628                 return -ENOENT;
2629         }
2630         return 0;
2631 }
2632
2633 static int __mdd_links_add(const struct lu_env *env,
2634                            struct mdd_object *mdd_obj,
2635                            struct mdd_link_data *ldata,
2636                            const struct lu_name *lname,
2637                            const struct lu_fid *pfid,
2638                            int first, int check)
2639 {
2640         int rc;
2641
2642         if (ldata->ml_leh == NULL) {
2643                 rc = first ? -ENODATA : mdd_links_read(env, mdd_obj, ldata);
2644                 if (rc) {
2645                         if (rc != -ENODATA)
2646                                 return rc;
2647                         rc = mdd_links_new(env, ldata);
2648                         if (rc)
2649                                 return rc;
2650                 }
2651         }
2652
2653         if (check) {
2654                 rc = mdd_links_find(env, mdd_obj, ldata, lname, pfid);
2655                 if (rc && rc != -ENOENT)
2656                         return rc;
2657                 if (rc == 0)
2658                         return -EEXIST;
2659         }
2660
2661         return mdd_links_add_buf(env, ldata, lname, pfid);
2662 }
2663
2664 static int __mdd_links_del(const struct lu_env *env,
2665                            struct mdd_object *mdd_obj,
2666                            struct mdd_link_data *ldata,
2667                            const struct lu_name *lname,
2668                            const struct lu_fid *pfid)
2669 {
2670         int rc;
2671
2672         if (ldata->ml_leh == NULL) {
2673                 rc = mdd_links_read(env, mdd_obj, ldata);
2674                 if (rc)
2675                         return rc;
2676         }
2677
2678         rc = mdd_links_find(env, mdd_obj, ldata, lname, pfid);
2679         if (rc)
2680                 return rc;
2681
2682         mdd_links_del_buf(env, ldata, lname);
2683         return 0;
2684 }
2685
2686 static int mdd_links_rename(const struct lu_env *env,
2687                             struct mdd_object *mdd_obj,
2688                             const struct lu_fid *oldpfid,
2689                             const struct lu_name *oldlname,
2690                             const struct lu_fid *newpfid,
2691                             const struct lu_name *newlname,
2692                             struct thandle *handle,
2693                             int first, int check)
2694 {
2695         struct mdd_link_data ldata = { 0 };
2696         int updated = 0;
2697         int rc2 = 0;
2698         int rc = 0;
2699         ENTRY;
2700
2701         LASSERT(oldpfid != NULL || newpfid != NULL);
2702
2703         if (mdd_obj->mod_flags & DEAD_OBJ)
2704                 /* No more links, don't bother */
2705                 RETURN(0);
2706
2707         if (oldpfid != NULL) {
2708                 rc = __mdd_links_del(env, mdd_obj, &ldata,
2709                                      oldlname, oldpfid);
2710                 if (rc) {
2711                         if ((check == 0) ||
2712                             (rc != -ENODATA && rc != -ENOENT))
2713                                 GOTO(out, rc);
2714                         /* No changes done. */
2715                         rc = 0;
2716                 } else {
2717                         updated = 1;
2718                 }
2719         }
2720
2721         /* If renaming, add the new record */
2722         if (newpfid != NULL) {
2723                 /* even if the add fails, we still delete the out-of-date
2724                  * old link */
2725                 rc2 = __mdd_links_add(env, mdd_obj, &ldata,
2726                                       newlname, newpfid, first, check);
2727                 if (rc2 == -EEXIST)
2728                         rc2 = 0;
2729                 else if (rc2 == 0)
2730                         updated = 1;
2731         }
2732
2733         if (updated)
2734                 rc = mdd_links_write(env, mdd_obj, &ldata, handle);
2735         EXIT;
2736 out:
2737         if (rc == 0)
2738                 rc = rc2;
2739         if (rc) {
2740                 if (oldpfid == NULL)
2741                         CERROR("link_ea add '%.*s' failed %d "DFID"\n",
2742                                newlname->ln_namelen, newlname->ln_name,
2743                                rc, PFID(mdd_object_fid(mdd_obj)));
2744                 else if (newpfid == NULL)
2745                         CERROR("link_ea del '%.*s' failed %d "DFID"\n",
2746                                oldlname->ln_namelen, oldlname->ln_name,
2747                                rc, PFID(mdd_object_fid(mdd_obj)));
2748                 else
2749                         CERROR("link_ea rename '%.*s'->'%.*s' failed %d "
2750                                DFID"\n",
2751                                oldlname->ln_namelen, oldlname->ln_name,
2752                                newlname->ln_namelen, newlname->ln_name,
2753                                rc, PFID(mdd_object_fid(mdd_obj)));
2754         }
2755
2756         if (ldata.ml_buf && ldata.ml_buf->lb_len > OBD_ALLOC_BIG)
2757                 /* if we vmalloced a large buffer drop it */
2758                 mdd_buf_put(ldata.ml_buf);
2759
2760         return rc;
2761 }
2762
2763 static inline int mdd_links_add(const struct lu_env *env,
2764                                 struct mdd_object *mdd_obj,
2765                                 const struct lu_fid *pfid,
2766                                 const struct lu_name *lname,
2767                                 struct thandle *handle, int first)
2768 {
2769         return mdd_links_rename(env, mdd_obj, NULL, NULL,
2770                                 pfid, lname, handle, first, 0);
2771 }
2772
2773 static inline int mdd_links_del(const struct lu_env *env,
2774                                 struct mdd_object *mdd_obj,
2775                                 const struct lu_fid *pfid,
2776                                 const struct lu_name *lname,
2777                                 struct thandle *handle)
2778 {
2779         return mdd_links_rename(env, mdd_obj, pfid, lname,
2780                                 NULL, NULL, handle, 0, 0);
2781 }
2782
2783 const struct md_dir_operations mdd_dir_ops = {
2784         .mdo_is_subdir     = mdd_is_subdir,
2785         .mdo_lookup        = mdd_lookup,
2786         .mdo_create        = mdd_create,
2787         .mdo_rename        = mdd_rename,
2788         .mdo_link          = mdd_link,
2789         .mdo_unlink        = mdd_unlink,
2790         .mdo_create_data   = mdd_create_data,
2791 };