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