Whamcloud - gitweb
LU-2430 mdd: add lfs mv to migrate inode.
[fs/lustre-release.git] / lustre / mdd / mdd_object.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, 2013, 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_object.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 <linux/module.h>
46 #include <obd.h>
47 #include <obd_class.h>
48 #include <obd_support.h>
49 #include <lprocfs_status.h>
50 /* fid_be_cpu(), fid_cpu_to_be(). */
51 #include <lustre_fid.h>
52 #include <obd_lov.h>
53 #include <lustre_idmap.h>
54 #include <lustre_param.h>
55 #include <lustre_mds.h>
56 #include <lustre/lustre_idl.h>
57
58 #include "mdd_internal.h"
59
60 static const struct lu_object_operations mdd_lu_obj_ops;
61 extern struct kmem_cache *mdd_object_kmem;
62
63 static int mdd_xattr_get(const struct lu_env *env,
64                          struct md_object *obj, struct lu_buf *buf,
65                          const char *name);
66
67 int mdd_data_get(const struct lu_env *env, struct mdd_object *obj,
68                  void **data)
69 {
70         if (mdd_object_exists(obj) == 0) {
71                 CERROR("%s: object "DFID" not found: rc = -2\n",
72                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
73                 return -ENOENT;
74         }
75         mdo_data_get(env, obj, data);
76         return 0;
77 }
78
79 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
80                struct lu_attr *la, struct lustre_capa *capa)
81 {
82         if (mdd_object_exists(obj) == 0) {
83                 CERROR("%s: object "DFID" not found: rc = -2\n",
84                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
85                 return -ENOENT;
86         }
87
88         return mdo_attr_get(env, obj, la, capa);
89 }
90
91 void mdd_flags_xlate(struct mdd_object *obj, __u32 flags)
92 {
93         obj->mod_flags &= ~(APPEND_OBJ|IMMUTE_OBJ);
94
95         if (flags & LUSTRE_APPEND_FL)
96                 obj->mod_flags |= APPEND_OBJ;
97
98         if (flags & LUSTRE_IMMUTABLE_FL)
99                 obj->mod_flags |= IMMUTE_OBJ;
100 }
101
102 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
103 {
104         struct mdd_thread_info *info;
105
106         lu_env_refill((struct lu_env *)env);
107         info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
108         LASSERT(info != NULL);
109         return info;
110 }
111
112 const struct lu_name *mdd_name_get_const(const struct lu_env *env,
113                                          const void *area, ssize_t len)
114 {
115         struct lu_name *lname;
116
117         lname = &mdd_env_info(env)->mti_name;
118         lname->ln_name = area;
119         lname->ln_namelen = len;
120         return lname;
121 }
122
123 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
124 {
125         struct lu_buf *buf;
126
127         buf = &mdd_env_info(env)->mti_buf[0];
128         buf->lb_buf = area;
129         buf->lb_len = len;
130         return buf;
131 }
132
133 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
134                                        const void *area, ssize_t len)
135 {
136         struct lu_buf *buf;
137
138         buf = &mdd_env_info(env)->mti_buf[0];
139         buf->lb_buf = (void *)area;
140         buf->lb_len = len;
141         return buf;
142 }
143
144 struct lu_object *mdd_object_alloc(const struct lu_env *env,
145                                    const struct lu_object_header *hdr,
146                                    struct lu_device *d)
147 {
148         struct mdd_object *mdd_obj;
149
150         OBD_SLAB_ALLOC_PTR_GFP(mdd_obj, mdd_object_kmem, GFP_NOFS);
151         if (mdd_obj != NULL) {
152                 struct lu_object *o;
153
154                 o = mdd2lu_obj(mdd_obj);
155                 lu_object_init(o, NULL, d);
156                 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
157                 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
158                 mdd_obj->mod_count = 0;
159                 o->lo_ops = &mdd_lu_obj_ops;
160                 return o;
161         } else {
162                 return NULL;
163         }
164 }
165
166 static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
167                            const struct lu_object_conf *unused)
168 {
169         struct mdd_device *d = lu2mdd_dev(o->lo_dev);
170         struct mdd_object *mdd_obj = lu2mdd_obj(o);
171         struct lu_object  *below;
172         struct lu_device  *under;
173         ENTRY;
174
175         mdd_obj->mod_cltime = 0;
176         under = &d->mdd_child->dd_lu_dev;
177         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
178         if (IS_ERR(below))
179                 RETURN(PTR_ERR(below));
180
181         lu_object_add(o, below);
182
183         RETURN(0);
184 }
185
186 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
187 {
188         int rc = 0;
189
190         if (lu_object_exists(o)) {
191                 struct mdd_object *mdd_obj = lu2mdd_obj(o);
192                 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
193
194                 rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
195                 if (rc == 0)
196                         mdd_flags_xlate(mdd_obj, attr->la_flags);
197         }
198
199         return rc;
200 }
201
202 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
203 {
204         struct mdd_object *mdd = lu2mdd_obj(o);
205
206         lu_object_fini(o);
207         OBD_SLAB_FREE_PTR(mdd, mdd_object_kmem);
208 }
209
210 static int mdd_object_print(const struct lu_env *env, void *cookie,
211                             lu_printer_t p, const struct lu_object *o)
212 {
213         struct mdd_object *mdd = lu2mdd_obj((struct lu_object *)o);
214         return (*p)(env, cookie, LUSTRE_MDD_NAME"-object@%p(open_count=%d, "
215                     "valid=%x, cltime="LPU64", flags=%lx)",
216                     mdd, mdd->mod_count, mdd->mod_valid,
217                     mdd->mod_cltime, mdd->mod_flags);
218 }
219
220 static const struct lu_object_operations mdd_lu_obj_ops = {
221         .loo_object_init    = mdd_object_init,
222         .loo_object_start   = mdd_object_start,
223         .loo_object_free    = mdd_object_free,
224         .loo_object_print   = mdd_object_print,
225 };
226
227 struct mdd_object *mdd_object_find(const struct lu_env *env,
228                                    struct mdd_device *d,
229                                    const struct lu_fid *f)
230 {
231         return md2mdd_obj(md_object_find_slice(env, &d->mdd_md_dev, f));
232 }
233
234 /*
235  * No permission check is needed.
236  */
237 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
238                  struct md_attr *ma)
239 {
240         struct mdd_object *mdd_obj = md2mdd_obj(obj);
241         int               rc;
242
243         ENTRY;
244
245         rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
246                         mdd_object_capa(env, md2mdd_obj(obj)));
247         if ((ma->ma_need & MA_INODE) != 0 && mdd_is_dead_obj(mdd_obj))
248                 ma->ma_attr.la_nlink = 0;
249
250         RETURN(rc);
251 }
252
253 /*
254  * No permission check is needed.
255  */
256 static int mdd_xattr_get(const struct lu_env *env,
257                          struct md_object *obj, struct lu_buf *buf,
258                          const char *name)
259 {
260         struct mdd_object *mdd_obj = md2mdd_obj(obj);
261         int rc;
262
263         ENTRY;
264
265         if (mdd_object_exists(mdd_obj) == 0) {
266                 CERROR("%s: object "DFID" not found: rc = -2\n",
267                        mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
268                 return -ENOENT;
269         }
270
271         /* If the object has been delete from the namespace, then
272          * get linkEA should return -ENOENT as well */
273         if (unlikely((mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ)) &&
274                       strcmp(name, XATTR_NAME_LINK) == 0))
275                 RETURN(-ENOENT);
276
277         mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
278         rc = mdo_xattr_get(env, mdd_obj, buf, name,
279                            mdd_object_capa(env, mdd_obj));
280         mdd_read_unlock(env, mdd_obj);
281
282         RETURN(rc);
283 }
284
285 /*
286  * Permission check is done when open,
287  * no need check again.
288  */
289 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
290                  struct lu_buf *buf)
291 {
292         struct mdd_object *mdd_obj = md2mdd_obj(obj);
293         struct dt_object  *next;
294         loff_t             pos = 0;
295         int                rc;
296         ENTRY;
297
298         if (mdd_object_exists(mdd_obj) == 0) {
299                 CERROR("%s: object "DFID" not found: rc = -2\n",
300                        mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
301                 return -ENOENT;
302         }
303
304         next = mdd_object_child(mdd_obj);
305         LASSERT(next != NULL);
306         LASSERT(next->do_body_ops != NULL);
307         LASSERT(next->do_body_ops->dbo_read != NULL);
308         mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
309         rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
310                                          mdd_object_capa(env, mdd_obj));
311         mdd_read_unlock(env, mdd_obj);
312         RETURN(rc);
313 }
314
315 /*
316  * No permission check is needed.
317  */
318 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
319                           struct lu_buf *buf)
320 {
321         struct mdd_object *mdd_obj = md2mdd_obj(obj);
322         int rc;
323
324         ENTRY;
325
326         mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
327         rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
328         mdd_read_unlock(env, mdd_obj);
329
330         RETURN(rc);
331 }
332
333 int mdd_declare_object_create_internal(const struct lu_env *env,
334                                        struct mdd_object *p,
335                                        struct mdd_object *c,
336                                        struct lu_attr *attr,
337                                        struct thandle *handle,
338                                        const struct md_op_spec *spec,
339                                        struct dt_allocation_hint *hint)
340 {
341         struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
342         const struct dt_index_features *feat = spec->sp_feat;
343         int rc;
344         ENTRY;
345
346         if (feat != &dt_directory_features && feat != NULL) {
347                 dof->dof_type = DFT_INDEX;
348                 dof->u.dof_idx.di_feat = feat;
349
350         } else {
351                 dof->dof_type = dt_mode_to_dft(attr->la_mode);
352                 if (dof->dof_type == DFT_REGULAR) {
353                         dof->u.dof_reg.striped =
354                                 md_should_create(spec->sp_cr_flags);
355                         if (spec->sp_cr_flags & MDS_OPEN_HAS_EA)
356                                 dof->u.dof_reg.striped = 0;
357                         /* is this replay? */
358                         if (spec->no_create)
359                                 dof->u.dof_reg.striped = 0;
360                 }
361         }
362
363         rc = mdo_declare_create_obj(env, c, attr, hint, dof, handle);
364
365         RETURN(rc);
366 }
367
368 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
369                                struct mdd_object *c, struct lu_attr *attr,
370                                struct thandle *handle,
371                                const struct md_op_spec *spec,
372                                struct dt_allocation_hint *hint)
373 {
374         struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
375         int rc;
376         ENTRY;
377
378         LASSERT(!mdd_object_exists(c));
379
380         rc = mdo_create_obj(env, c, attr, hint, dof, handle);
381
382         LASSERT(ergo(rc == 0, mdd_object_exists(c)));
383
384         RETURN(rc);
385 }
386
387 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
388                           const struct lu_attr *attr, struct thandle *handle,
389                           int needacl)
390 {
391         int rc;
392         ENTRY;
393
394         rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
395 #ifdef CONFIG_FS_POSIX_ACL
396         if (!rc && (attr->la_valid & LA_MODE) && needacl)
397                 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
398 #endif
399         RETURN(rc);
400 }
401
402 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
403                     const struct lu_attr *oattr, struct lu_attr *attr,
404                     struct thandle *handle)
405 {
406         int rc = 0;
407         ENTRY;
408
409         LASSERT(attr->la_valid & LA_CTIME);
410         LASSERT(oattr != NULL);
411
412         /* Make sure the ctime is increased only, however, it's not strictly
413          * reliable at here because there is not guarantee to hold lock on
414          * object, so we just bypass some unnecessary cmtime setting first
415          * and OSD has to check it again. */
416         if (attr->la_ctime < oattr->la_ctime)
417                 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
418         else if (attr->la_valid == LA_CTIME &&
419                  attr->la_ctime == oattr->la_ctime)
420                 attr->la_valid &= ~LA_CTIME;
421
422         if (attr->la_valid != 0)
423                 rc = mdd_attr_set_internal(env, obj, attr, handle, 0);
424         RETURN(rc);
425 }
426
427 /*
428  * This gives the same functionality as the code between
429  * sys_chmod and inode_setattr
430  * chown_common and inode_setattr
431  * utimes and inode_setattr
432  * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
433  */
434 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
435                         const struct lu_attr *oattr, struct lu_attr *la,
436                         const unsigned long flags)
437 {
438         struct lu_ucred  *uc;
439         int               rc = 0;
440         ENTRY;
441
442         if (!la->la_valid)
443                 RETURN(0);
444
445         /* Do not permit change file type */
446         if (la->la_valid & LA_TYPE)
447                 RETURN(-EPERM);
448
449         /* They should not be processed by setattr */
450         if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
451                 RETURN(-EPERM);
452
453         LASSERT(oattr != NULL);
454
455         /* export destroy does not have ->le_ses, but we may want
456          * to drop LUSTRE_SOM_FL. */
457         uc = lu_ucred_check(env);
458         if (uc == NULL)
459                 RETURN(0);
460
461         if (la->la_valid == LA_CTIME) {
462                 if (!(flags & MDS_PERM_BYPASS))
463                         /* This is only for set ctime when rename's source is
464                          * on remote MDS. */
465                         rc = mdd_may_delete(env, NULL, NULL, obj, oattr, NULL,
466                                             1, 0);
467                 if (rc == 0 && la->la_ctime <= oattr->la_ctime)
468                         la->la_valid &= ~LA_CTIME;
469                 RETURN(rc);
470         }
471
472         if (la->la_valid == LA_ATIME) {
473                 /* This is atime only set for read atime update on close. */
474                 if (la->la_atime >= oattr->la_atime &&
475                     la->la_atime < (oattr->la_atime +
476                                 mdd_obj2mdd_dev(obj)->mdd_atime_diff))
477                         la->la_valid &= ~LA_ATIME;
478                 RETURN(0);
479         }
480
481         /* Check if flags change. */
482         if (la->la_valid & LA_FLAGS) {
483                 unsigned int oldflags = 0;
484                 unsigned int newflags = la->la_flags &
485                                 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
486
487                 if ((uc->uc_fsuid != oattr->la_uid) &&
488                     !md_capable(uc, CFS_CAP_FOWNER))
489                         RETURN(-EPERM);
490
491                 /* XXX: the IMMUTABLE and APPEND_ONLY flags can
492                  * only be changed by the relevant capability. */
493                 if (mdd_is_immutable(obj))
494                         oldflags |= LUSTRE_IMMUTABLE_FL;
495                 if (mdd_is_append(obj))
496                         oldflags |= LUSTRE_APPEND_FL;
497                 if ((oldflags ^ newflags) &&
498                     !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
499                         RETURN(-EPERM);
500
501                 if (!S_ISDIR(oattr->la_mode))
502                         la->la_flags &= ~LUSTRE_DIRSYNC_FL;
503         }
504
505         if ((mdd_is_immutable(obj) || mdd_is_append(obj)) &&
506             (la->la_valid & ~LA_FLAGS) &&
507             !(flags & MDS_PERM_BYPASS))
508                 RETURN(-EPERM);
509
510         /* Check for setting the obj time. */
511         if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
512             !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
513                 if ((uc->uc_fsuid != oattr->la_uid) &&
514                     !md_capable(uc, CFS_CAP_FOWNER)) {
515                         rc = mdd_permission_internal(env, obj, oattr,
516                                                      MAY_WRITE);
517                         if (rc)
518                                 RETURN(rc);
519                 }
520         }
521
522         if (la->la_valid & LA_KILL_SUID) {
523                 la->la_valid &= ~LA_KILL_SUID;
524                 if ((oattr->la_mode & S_ISUID) &&
525                     !(la->la_valid & LA_MODE)) {
526                         la->la_mode = oattr->la_mode;
527                         la->la_valid |= LA_MODE;
528                 }
529                 la->la_mode &= ~S_ISUID;
530         }
531
532         if (la->la_valid & LA_KILL_SGID) {
533                 la->la_valid &= ~LA_KILL_SGID;
534                 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
535                         (S_ISGID | S_IXGRP)) &&
536                     !(la->la_valid & LA_MODE)) {
537                         la->la_mode = oattr->la_mode;
538                         la->la_valid |= LA_MODE;
539                 }
540                 la->la_mode &= ~S_ISGID;
541         }
542
543         /* Make sure a caller can chmod. */
544         if (la->la_valid & LA_MODE) {
545                 if (!(flags & MDS_PERM_BYPASS) &&
546                     (uc->uc_fsuid != oattr->la_uid) &&
547                     !md_capable(uc, CFS_CAP_FOWNER))
548                         RETURN(-EPERM);
549
550                 if (la->la_mode == (umode_t) -1)
551                         la->la_mode = oattr->la_mode;
552                 else
553                         la->la_mode = (la->la_mode & S_IALLUGO) |
554                                         (oattr->la_mode & ~S_IALLUGO);
555
556                 /* Also check the setgid bit! */
557                 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
558                                        la->la_gid : oattr->la_gid) &&
559                     !md_capable(uc, CFS_CAP_FSETID))
560                         la->la_mode &= ~S_ISGID;
561         } else {
562                la->la_mode = oattr->la_mode;
563         }
564
565         /* Make sure a caller can chown. */
566         if (la->la_valid & LA_UID) {
567                 if (la->la_uid == (uid_t) -1)
568                         la->la_uid = oattr->la_uid;
569                 if (((uc->uc_fsuid != oattr->la_uid) ||
570                      (la->la_uid != oattr->la_uid)) &&
571                     !md_capable(uc, CFS_CAP_CHOWN))
572                         RETURN(-EPERM);
573
574                 /* If the user or group of a non-directory has been
575                  * changed by a non-root user, remove the setuid bit.
576                  * 19981026 David C Niemi <niemi@tux.org>
577                  *
578                  * Changed this to apply to all users, including root,
579                  * to avoid some races. This is the behavior we had in
580                  * 2.0. The check for non-root was definitely wrong
581                  * for 2.2 anyway, as it should have been using
582                  * CAP_FSETID rather than fsuid -- 19990830 SD. */
583                 if (((oattr->la_mode & S_ISUID) == S_ISUID) &&
584                 !S_ISDIR(oattr->la_mode)) {
585                         la->la_mode &= ~S_ISUID;
586                         la->la_valid |= LA_MODE;
587                 }
588         }
589
590         /* Make sure caller can chgrp. */
591         if (la->la_valid & LA_GID) {
592                 if (la->la_gid == (gid_t) -1)
593                         la->la_gid = oattr->la_gid;
594                 if (((uc->uc_fsuid != oattr->la_uid) ||
595                      ((la->la_gid != oattr->la_gid) &&
596                       !lustre_in_group_p(uc, la->la_gid))) &&
597                     !md_capable(uc, CFS_CAP_CHOWN))
598                         RETURN(-EPERM);
599
600                 /* Likewise, if the user or group of a non-directory
601                  * has been changed by a non-root user, remove the
602                  * setgid bit UNLESS there is no group execute bit
603                  * (this would be a file marked for mandatory
604                  * locking).  19981026 David C Niemi <niemi@tux.org>
605                  *
606                  * Removed the fsuid check (see the comment above) --
607                  * 19990830 SD. */
608                 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
609                     (S_ISGID | S_IXGRP)) && !S_ISDIR(oattr->la_mode)) {
610                         la->la_mode &= ~S_ISGID;
611                         la->la_valid |= LA_MODE;
612                 }
613         }
614
615         /* For both Size-on-MDS case and truncate case,
616          * "la->la_valid & (LA_SIZE | LA_BLOCKS)" are ture.
617          * We distinguish them by "flags & MDS_SOM".
618          * For SOM case, it is true, the MAY_WRITE perm has been checked
619          * when open, no need check again. For truncate case, it is false,
620          * the MAY_WRITE perm should be checked here. */
621         if (flags & MDS_SOM) {
622                 /* For the "Size-on-MDS" setattr update, merge coming
623                  * attributes with the set in the inode. BUG 10641 */
624                 if ((la->la_valid & LA_ATIME) &&
625                     (la->la_atime <= oattr->la_atime))
626                         la->la_valid &= ~LA_ATIME;
627
628                 /* OST attributes do not have a priority over MDS attributes,
629                  * so drop times if ctime is equal. */
630                 if ((la->la_valid & LA_CTIME) &&
631                     (la->la_ctime <= oattr->la_ctime))
632                         la->la_valid &= ~(LA_MTIME | LA_CTIME);
633         } else {
634                 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
635                         if (!((flags & MDS_OWNEROVERRIDE) &&
636                               (uc->uc_fsuid == oattr->la_uid)) &&
637                             !(flags & MDS_PERM_BYPASS)) {
638                                 rc = mdd_permission_internal(env, obj,
639                                                              oattr, MAY_WRITE);
640                                 if (rc != 0)
641                                         RETURN(rc);
642                         }
643                 }
644                 if (la->la_valid & LA_CTIME) {
645                         /* The pure setattr, it has the priority over what is
646                          * already set, do not drop it if ctime is equal. */
647                         if (la->la_ctime < oattr->la_ctime)
648                                 la->la_valid &= ~(LA_ATIME | LA_MTIME |
649                                                         LA_CTIME);
650                 }
651         }
652
653         RETURN(0);
654 }
655
656 /** Store a data change changelog record
657  * If this fails, we must fail the whole transaction; we don't
658  * want the change to commit without the log entry.
659  * \param mdd_obj - mdd_object of change
660  * \param handle - transacion handle
661  */
662 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
663                              enum changelog_rec_type type, int flags,
664                              struct mdd_object *mdd_obj, struct thandle *handle)
665 {
666         const struct lu_fid             *tfid;
667         struct llog_changelog_rec       *rec;
668         struct lu_buf                   *buf;
669         int                              reclen;
670         int                              rc;
671
672         /* Not recording */
673         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
674                 RETURN(0);
675         if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
676                 RETURN(0);
677
678         LASSERT(mdd_obj != NULL);
679         LASSERT(handle != NULL);
680
681         tfid = mdo2fid(mdd_obj);
682
683         if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
684             cfs_time_before_64(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
685                 /* Don't need multiple updates in this log */
686                 /* Don't check under lock - no big deal if we get an extra
687                    entry */
688                 RETURN(0);
689         }
690
691         reclen = llog_data_len(sizeof(*rec));
692         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
693         if (buf->lb_buf == NULL)
694                 RETURN(-ENOMEM);
695         rec = buf->lb_buf;
696
697         rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
698         rec->cr.cr_type = (__u32)type;
699         rec->cr.cr_tfid = *tfid;
700         rec->cr.cr_namelen = 0;
701         mdd_obj->mod_cltime = cfs_time_current_64();
702
703         rc = mdd_changelog_store(env, mdd, rec, handle);
704
705         RETURN(rc);
706 }
707
708 int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
709                   int flags, struct md_object *obj)
710 {
711         struct thandle *handle;
712         struct mdd_object *mdd_obj = md2mdd_obj(obj);
713         struct mdd_device *mdd = mdo2mdd(obj);
714         int rc;
715         ENTRY;
716
717         handle = mdd_trans_create(env, mdd);
718         if (IS_ERR(handle))
719                 RETURN(PTR_ERR(handle));
720
721         rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
722         if (rc)
723                 GOTO(stop, rc);
724
725         rc = mdd_trans_start(env, mdd, handle);
726         if (rc)
727                 GOTO(stop, rc);
728
729         rc = mdd_changelog_data_store(env, mdd, type, flags, mdd_obj,
730                                       handle);
731
732 stop:
733         mdd_trans_stop(env, mdd, rc, handle);
734
735         RETURN(rc);
736 }
737
738 /**
739  * Save LMA extended attributes with data from \a ma.
740  *
741  * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
742  * not, LMA EA will be first read from disk, modified and write back.
743  *
744  */
745 /* Precedence for choosing record type when multiple
746  * attributes change: setattr > mtime > ctime > atime
747  * (ctime changes when mtime does, plus chmod/chown.
748  * atime and ctime are independent.) */
749 static int mdd_attr_set_changelog(const struct lu_env *env,
750                                   struct md_object *obj, struct thandle *handle,
751                                   __u64 valid)
752 {
753         struct mdd_device *mdd = mdo2mdd(obj);
754         int bits, type = 0;
755
756         bits = (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
757         bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
758         bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
759         bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
760         bits = bits & mdd->mdd_cl.mc_mask;
761         if (bits == 0)
762                 return 0;
763
764         /* The record type is the lowest non-masked set bit */
765         while (bits && ((bits & 1) == 0)) {
766                 bits = bits >> 1;
767                 type++;
768         }
769
770         /* FYI we only store the first CLF_FLAGMASK bits of la_valid */
771         return mdd_changelog_data_store(env, mdd, type, (int)valid,
772                                         md2mdd_obj(obj), handle);
773 }
774
775 static int mdd_declare_attr_set(const struct lu_env *env,
776                                 struct mdd_device *mdd,
777                                 struct mdd_object *obj,
778                                 const struct lu_attr *attr,
779                                 struct thandle *handle)
780 {
781         int rc;
782
783         rc = mdo_declare_attr_set(env, obj, attr, handle);
784         if (rc)
785                 return rc;
786
787 #ifdef CONFIG_FS_POSIX_ACL
788         if (attr->la_valid & LA_MODE) {
789                 mdd_read_lock(env, obj, MOR_TGT_CHILD);
790                 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
791                                    XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
792                 mdd_read_unlock(env, obj);
793                 if (rc == -EOPNOTSUPP || rc == -ENODATA)
794                         rc = 0;
795                 else if (rc < 0)
796                         return rc;
797
798                 if (rc != 0) {
799                         struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
800                         rc = mdo_declare_xattr_set(env, obj, buf,
801                                                    XATTR_NAME_ACL_ACCESS, 0,
802                                                    handle);
803                         if (rc)
804                                 return rc;
805                 }
806         }
807 #endif
808
809         rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
810         return rc;
811 }
812
813 /*
814  * LU-3671
815  *
816  * permission changes may require sync operation, to mitigate performance
817  * impact, only do this for dir and when permission is reduced.
818  *
819  * For regular files, version is updated with permission change (see VBR), async
820  * permission won't cause any issue, while missing permission change on
821  * directory may affect accessibility of other objects after recovery.
822  */
823 static inline bool permission_needs_sync(const struct lu_attr *old,
824                                          const struct lu_attr *new)
825 {
826         if (!S_ISDIR(old->la_mode))
827                 return false;
828
829         if (new->la_valid & (LA_UID | LA_GID))
830                 return true;
831
832         if (new->la_valid & LA_MODE &&
833             new->la_mode & (S_ISUID | S_ISGID | S_ISVTX))
834                 return true;
835
836         if ((new->la_valid & LA_MODE) &&
837             ((new->la_mode & old->la_mode) & S_IRWXUGO) !=
838              (old->la_mode & S_IRWXUGO))
839                 return true;
840
841         return false;
842 }
843
844 /* set attr and LOV EA at once, return updated attr */
845 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
846                  const struct md_attr *ma)
847 {
848         struct mdd_object *mdd_obj = md2mdd_obj(obj);
849         struct mdd_device *mdd = mdo2mdd(obj);
850         struct thandle *handle;
851         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
852         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
853         const struct lu_attr *la = &ma->ma_attr;
854         int rc;
855         ENTRY;
856
857         /* we do not use ->attr_set() for LOV/SOM/HSM EA any more */
858         LASSERT((ma->ma_valid & MA_LOV) == 0);
859         LASSERT((ma->ma_valid & MA_HSM) == 0);
860         LASSERT((ma->ma_valid & MA_SOM) == 0);
861
862         rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
863         if (rc)
864                 RETURN(rc);
865
866         *la_copy = ma->ma_attr;
867         rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma->ma_attr_flags);
868         if (rc)
869                 RETURN(rc);
870
871         /* setattr on "close" only change atime, or do nothing */
872         if (la->la_valid == LA_ATIME && la_copy->la_valid == 0)
873                 RETURN(0);
874
875         handle = mdd_trans_create(env, mdd);
876         if (IS_ERR(handle))
877                 RETURN(PTR_ERR(handle));
878
879         rc = mdd_declare_attr_set(env, mdd, mdd_obj, la, handle);
880         if (rc)
881                 GOTO(stop, rc);
882
883         rc = mdd_trans_start(env, mdd, handle);
884         if (rc)
885                 GOTO(stop, rc);
886
887         if (mdd->mdd_sync_permission && permission_needs_sync(attr, la))
888                 handle->th_sync = 1;
889
890         if (la->la_valid & (LA_MTIME | LA_CTIME))
891                 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
892                        la->la_mtime, la->la_ctime);
893
894         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
895         if (la_copy->la_valid & LA_FLAGS) {
896                 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
897                 if (rc == 0)
898                         mdd_flags_xlate(mdd_obj, la_copy->la_flags);
899         } else if (la_copy->la_valid) { /* setattr */
900                 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
901         }
902         mdd_write_unlock(env, mdd_obj);
903
904         if (rc == 0)
905                 rc = mdd_attr_set_changelog(env, obj, handle, la->la_valid);
906
907         GOTO(stop, rc);
908
909 stop:
910         mdd_trans_stop(env, mdd, rc, handle);
911         return rc;
912 }
913
914 static int mdd_xattr_sanity_check(const struct lu_env *env,
915                                   struct mdd_object *obj,
916                                   const struct lu_attr *attr)
917 {
918         struct lu_ucred *uc     = lu_ucred_assert(env);
919         ENTRY;
920
921         if (mdd_is_immutable(obj) || mdd_is_append(obj))
922                 RETURN(-EPERM);
923
924         if ((uc->uc_fsuid != attr->la_uid) && !md_capable(uc, CFS_CAP_FOWNER))
925                 RETURN(-EPERM);
926
927         RETURN(0);
928 }
929
930 static int mdd_declare_xattr_set(const struct lu_env *env,
931                                  struct mdd_device *mdd,
932                                  struct mdd_object *obj,
933                                  const struct lu_buf *buf,
934                                  const char *name,
935                                  int fl, struct thandle *handle)
936 {
937         int     rc;
938
939         rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
940         if (rc)
941                 return rc;
942
943         /* Only record user and layout xattr changes */
944         if (strncmp(XATTR_USER_PREFIX, name,
945                     sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
946             strcmp(XATTR_NAME_LOV, name) == 0) {
947                 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
948                 if (rc)
949                         return rc;
950         }
951
952         /* If HSM data is modified, this could add a changelog */
953         if (strcmp(XATTR_NAME_HSM, name) == 0) {
954                 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
955                 if (rc)
956                         return rc;
957         }
958
959         rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
960         return rc;
961 }
962
963 /*
964  * Compare current and future data of HSM EA and add a changelog if needed.
965  *
966  * Caller should have write-locked \param obj.
967  *
968  * \param buf - Future HSM EA content.
969  * \retval 0 if no changelog is needed or changelog was added properly.
970  * \retval -ve errno if there was a problem
971  */
972 static int mdd_hsm_update_locked(const struct lu_env *env,
973                                  struct md_object *obj,
974                                  const struct lu_buf *buf,
975                                  struct thandle *handle)
976 {
977         struct mdd_thread_info *info = mdd_env_info(env);
978         struct mdd_device      *mdd = mdo2mdd(obj);
979         struct mdd_object      *mdd_obj = md2mdd_obj(obj);
980         struct lu_buf          *current_buf;
981         struct md_hsm          *current_mh;
982         struct md_hsm          *new_mh;
983         int                     rc;
984         ENTRY;
985
986         OBD_ALLOC_PTR(current_mh);
987         if (current_mh == NULL)
988                 RETURN(-ENOMEM);
989
990         /* Read HSM attrs from disk */
991         CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
992         current_buf = mdd_buf_get(env, info->mti_xattr_buf,
993                                   sizeof(info->mti_xattr_buf));
994         rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM,
995                            mdd_object_capa(env, mdd_obj));
996         rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
997         if (rc < 0 && rc != -ENODATA)
998                 GOTO(free, rc);
999         else if (rc == -ENODATA)
1000                 current_mh->mh_flags = 0;
1001
1002         /* Map future HSM xattr */
1003         OBD_ALLOC_PTR(new_mh);
1004         if (new_mh == NULL)
1005                 GOTO(free, rc = -ENOMEM);
1006         lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
1007
1008         /* If HSM flags are different, add a changelog */
1009         rc = 0;
1010         if (current_mh->mh_flags != new_mh->mh_flags) {
1011                 int flags = 0;
1012                 hsm_set_cl_event(&flags, HE_STATE);
1013                 if (new_mh->mh_flags & HS_DIRTY)
1014                         hsm_set_cl_flags(&flags, CLF_HSM_DIRTY);
1015
1016                 rc = mdd_changelog_data_store(env, mdd, CL_HSM, flags, mdd_obj,
1017                                               handle);
1018         }
1019
1020         OBD_FREE_PTR(new_mh);
1021 free:
1022         OBD_FREE_PTR(current_mh);
1023         return(rc);
1024 }
1025
1026 /**
1027  * The caller should guarantee to update the object ctime
1028  * after xattr_set if needed.
1029  */
1030 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
1031                          const struct lu_buf *buf, const char *name,
1032                          int fl)
1033 {
1034         struct mdd_object       *mdd_obj = md2mdd_obj(obj);
1035         struct lu_attr          *attr = MDD_ENV_VAR(env, cattr);
1036         struct mdd_device       *mdd = mdo2mdd(obj);
1037         struct thandle          *handle;
1038         int                      rc;
1039         ENTRY;
1040
1041         rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1042         if (rc)
1043                 RETURN(rc);
1044
1045         if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
1046                 rc = mdd_acl_set(env, mdd_obj, attr, buf, fl);
1047                 RETURN(rc);
1048         }
1049
1050         rc = mdd_xattr_sanity_check(env, mdd_obj, attr);
1051         if (rc)
1052                 RETURN(rc);
1053
1054         handle = mdd_trans_create(env, mdd);
1055         if (IS_ERR(handle))
1056                 RETURN(PTR_ERR(handle));
1057
1058         rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
1059         if (rc)
1060                 GOTO(stop, rc);
1061
1062         rc = mdd_trans_start(env, mdd, handle);
1063         if (rc)
1064                 GOTO(stop, rc);
1065
1066         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1067
1068         if (strcmp(XATTR_NAME_HSM, name) == 0) {
1069                 rc = mdd_hsm_update_locked(env, obj, buf, handle);
1070                 if (rc) {
1071                         mdd_write_unlock(env, mdd_obj);
1072                         GOTO(stop, rc);
1073                 }
1074         }
1075
1076         rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle,
1077                            mdd_object_capa(env, mdd_obj));
1078         mdd_write_unlock(env, mdd_obj);
1079         if (rc)
1080                 GOTO(stop, rc);
1081
1082         if (strcmp(XATTR_NAME_LOV, name) == 0)
1083                 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, mdd_obj,
1084                                               handle);
1085         else if (strncmp(XATTR_USER_PREFIX, name,
1086                         sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1087             strncmp(POSIX_ACL_XATTR_ACCESS, name,
1088                         sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1089             strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1090                         sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1091                 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1092                                               handle);
1093
1094 stop:
1095         mdd_trans_stop(env, mdd, rc, handle);
1096
1097         RETURN(rc);
1098 }
1099
1100 static int mdd_declare_xattr_del(const struct lu_env *env,
1101                                  struct mdd_device *mdd,
1102                                  struct mdd_object *obj,
1103                                  const char *name,
1104                                  struct thandle *handle)
1105 {
1106         int rc;
1107
1108         rc = mdo_declare_xattr_del(env, obj, name, handle);
1109         if (rc)
1110                 return rc;
1111
1112         /* Only record user xattr changes */
1113         if ((strncmp(XATTR_USER_PREFIX, name,
1114                      sizeof(XATTR_USER_PREFIX) - 1) == 0))
1115                 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1116
1117         return rc;
1118 }
1119
1120 /**
1121  * The caller should guarantee to update the object ctime
1122  * after xattr_set if needed.
1123  */
1124 int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1125                   const char *name)
1126 {
1127         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1128         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1129         struct mdd_device *mdd = mdo2mdd(obj);
1130         struct thandle *handle;
1131         int  rc;
1132         ENTRY;
1133
1134         rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1135         if (rc)
1136                 RETURN(rc);
1137
1138         rc = mdd_xattr_sanity_check(env, mdd_obj, attr);
1139         if (rc)
1140                 RETURN(rc);
1141
1142         handle = mdd_trans_create(env, mdd);
1143         if (IS_ERR(handle))
1144                 RETURN(PTR_ERR(handle));
1145
1146         rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
1147         if (rc)
1148                 GOTO(stop, rc);
1149
1150         rc = mdd_trans_start(env, mdd, handle);
1151         if (rc)
1152                 GOTO(stop, rc);
1153
1154         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1155         rc = mdo_xattr_del(env, mdd_obj, name, handle,
1156                            mdd_object_capa(env, mdd_obj));
1157         mdd_write_unlock(env, mdd_obj);
1158         if (rc)
1159                 GOTO(stop, rc);
1160
1161         /* Only record system & user xattr changes */
1162         if (strncmp(XATTR_USER_PREFIX, name,
1163                                   sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1164                           strncmp(POSIX_ACL_XATTR_ACCESS, name,
1165                                   sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1166                           strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1167                                   sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1168                 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1169                                               handle);
1170
1171 stop:
1172         mdd_trans_stop(env, mdd, rc, handle);
1173
1174         RETURN(rc);
1175 }
1176
1177 /*
1178  * read lov EA of an object
1179  * return the lov EA in an allocated lu_buf
1180  */
1181 int mdd_get_lov_ea(const struct lu_env *env, struct mdd_object *obj,
1182                    struct lu_buf *lmm_buf)
1183 {
1184         struct lu_buf   *buf = &mdd_env_info(env)->mti_big_buf;
1185         int              rc, bufsize;
1186         ENTRY;
1187
1188 repeat:
1189         rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_LOV,
1190                            mdd_object_capa(env, obj));
1191
1192         if (rc == -ERANGE) {
1193                 /* mti_big_buf is allocated but is too small
1194                  * we need to increase it */
1195                 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1196                                              buf->lb_len * 2);
1197                 if (buf->lb_buf == NULL)
1198                         GOTO(out, rc = -ENOMEM);
1199                 goto repeat;
1200         }
1201
1202         if (rc < 0)
1203                 RETURN(rc);
1204
1205         if (rc == 0)
1206                 RETURN(-ENODATA);
1207
1208         bufsize = rc;
1209         if (memcmp(buf, &LU_BUF_NULL, sizeof(*buf)) == 0) {
1210                 /* mti_big_buf was not allocated, so we have to
1211                  * allocate it based on the ea size */
1212                 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1213                                              bufsize);
1214                 if (buf->lb_buf == NULL)
1215                         GOTO(out, rc = -ENOMEM);
1216                 goto repeat;
1217         }
1218
1219         lu_buf_alloc(lmm_buf, bufsize);
1220         if (lmm_buf->lb_buf == NULL)
1221                 GOTO(out, rc = -ENOMEM);
1222
1223         memcpy(lmm_buf->lb_buf, buf->lb_buf, bufsize);
1224         rc = 0;
1225         EXIT;
1226
1227 out:
1228         if (rc < 0)
1229                 lu_buf_free(lmm_buf);
1230         return rc;
1231 }
1232
1233 static int mdd_xattr_hsm_replace(const struct lu_env *env,
1234                                  struct mdd_object *o, struct lu_buf *buf,
1235                                  struct thandle *handle)
1236 {
1237         struct hsm_attrs *attrs;
1238         __u32 hsm_flags;
1239         int flags = 0;
1240         int rc;
1241         ENTRY;
1242
1243         rc = mdo_xattr_set(env, o, buf, XATTR_NAME_HSM, LU_XATTR_REPLACE,
1244                            handle, mdd_object_capa(env, o));
1245         if (rc != 0)
1246                 RETURN(rc);
1247
1248         attrs = buf->lb_buf;
1249         hsm_flags = le32_to_cpu(attrs->hsm_flags);
1250         if (!(hsm_flags & HS_RELEASED) || mdd_is_dead_obj(o))
1251                 RETURN(0);
1252
1253         /* Add a changelog record for release. */
1254         hsm_set_cl_event(&flags, HE_RELEASE);
1255         rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
1256                                       flags, o, handle);
1257         RETURN(rc);
1258 }
1259
1260 /*
1261  *  check if layout swapping between 2 objects is allowed
1262  *  the rules are:
1263  *  - only normal FIDs or non-system IGIFs
1264  *  - same type of objects
1265  *  - same owner/group (so quotas are still valid)
1266  */
1267 static int mdd_layout_swap_allowed(const struct lu_env *env,
1268                                    struct mdd_object *o1,
1269                                    const struct lu_attr *attr1,
1270                                    struct mdd_object *o2,
1271                                    const struct lu_attr *attr2)
1272 {
1273         const struct lu_fid     *fid1, *fid2;
1274         ENTRY;
1275
1276         fid1 = mdo2fid(o1);
1277         fid2 = mdo2fid(o2);
1278
1279         if (!fid_is_norm(fid1) &&
1280             (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
1281                 RETURN(-EBADF);
1282
1283         if (!fid_is_norm(fid2) &&
1284             (!fid_is_igif(fid2) || IS_ERR(mdd_links_get(env, o2))))
1285                 RETURN(-EBADF);
1286
1287         if (mdd_object_type(o1) != mdd_object_type(o2)) {
1288                 if (S_ISDIR(mdd_object_type(o1)))
1289                         RETURN(-ENOTDIR);
1290                 if (S_ISREG(mdd_object_type(o1)))
1291                         RETURN(-EISDIR);
1292                 RETURN(-EBADF);
1293         }
1294
1295         if ((attr1->la_uid != attr2->la_uid) ||
1296             (attr1->la_gid != attr2->la_gid))
1297                 RETURN(-EPERM);
1298
1299         RETURN(0);
1300 }
1301
1302 /**
1303  * swap layouts between 2 lustre objects
1304  */
1305 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
1306                             struct md_object *obj2, __u64 flags)
1307 {
1308         struct mdd_thread_info  *info = mdd_env_info(env);
1309         struct mdd_object       *fst_o = md2mdd_obj(obj1);
1310         struct mdd_object       *snd_o = md2mdd_obj(obj2);
1311         struct lu_attr          *fst_la = MDD_ENV_VAR(env, cattr);
1312         struct lu_attr          *snd_la = MDD_ENV_VAR(env, tattr);
1313         struct mdd_device       *mdd = mdo2mdd(obj1);
1314         struct lov_mds_md       *fst_lmm, *snd_lmm;
1315         struct lu_buf           *fst_buf = &info->mti_buf[0];
1316         struct lu_buf           *snd_buf = &info->mti_buf[1];
1317         struct lu_buf           *fst_hsm_buf = &info->mti_buf[2];
1318         struct lu_buf           *snd_hsm_buf = &info->mti_buf[3];
1319         struct ost_id           *saved_oi = NULL;
1320         struct thandle          *handle;
1321         __u16                    fst_gen, snd_gen;
1322         int                      fst_fl;
1323         int                      rc;
1324         int                      rc2;
1325         ENTRY;
1326
1327         CLASSERT(ARRAY_SIZE(info->mti_buf) >= 4);
1328         memset(info->mti_buf, 0, sizeof(info->mti_buf));
1329
1330         /* we have to sort the 2 obj, so locking will always
1331          * be in the same order, even in case of 2 concurrent swaps */
1332         rc = lu_fid_cmp(mdo2fid(fst_o), mdo2fid(snd_o));
1333         if (rc == 0) /* same fid ? */
1334                 RETURN(-EPERM);
1335
1336         if (rc < 0)
1337                 swap(fst_o, snd_o);
1338
1339         rc = mdd_la_get(env, fst_o, fst_la, BYPASS_CAPA);
1340         if (rc != 0)
1341                 RETURN(rc);
1342
1343         rc = mdd_la_get(env, snd_o, snd_la, BYPASS_CAPA);
1344         if (rc != 0)
1345                 RETURN(rc);
1346
1347         /* check if layout swapping is allowed */
1348         rc = mdd_layout_swap_allowed(env, fst_o, fst_la, snd_o, snd_la);
1349         if (rc != 0)
1350                 RETURN(rc);
1351
1352         handle = mdd_trans_create(env, mdd);
1353         if (IS_ERR(handle))
1354                 RETURN(PTR_ERR(handle));
1355
1356         /* objects are already sorted */
1357         mdd_write_lock(env, fst_o, MOR_TGT_CHILD);
1358         mdd_write_lock(env, snd_o, MOR_TGT_CHILD);
1359
1360         rc = mdd_get_lov_ea(env, fst_o, fst_buf);
1361         if (rc < 0 && rc != -ENODATA)
1362                 GOTO(stop, rc);
1363
1364         rc = mdd_get_lov_ea(env, snd_o, snd_buf);
1365         if (rc < 0 && rc != -ENODATA)
1366                 GOTO(stop, rc);
1367
1368         /* swapping 2 non existant layouts is a success */
1369         if (fst_buf->lb_buf == NULL && snd_buf->lb_buf == NULL)
1370                 GOTO(stop, rc = 0);
1371
1372         /* to help inode migration between MDT, it is better to
1373          * start by the no layout file (if one), so we order the swap */
1374         if (snd_buf->lb_buf == NULL) {
1375                 swap(fst_o, snd_o);
1376                 swap(fst_buf, snd_buf);
1377         }
1378
1379         /* lmm and generation layout initialization */
1380         if (fst_buf->lb_buf != NULL) {
1381                 fst_lmm = fst_buf->lb_buf;
1382                 fst_gen = le16_to_cpu(fst_lmm->lmm_layout_gen);
1383                 fst_fl  = LU_XATTR_REPLACE;
1384         } else {
1385                 fst_lmm = NULL;
1386                 fst_gen = 0;
1387                 fst_fl  = LU_XATTR_CREATE;
1388         }
1389
1390         snd_lmm = snd_buf->lb_buf;
1391         snd_gen = le16_to_cpu(snd_lmm->lmm_layout_gen);
1392
1393         /* increase the generation layout numbers */
1394         snd_gen++;
1395         fst_gen++;
1396
1397         /* set the file specific informations in lmm */
1398         if (fst_lmm != NULL) {
1399                 saved_oi = &info->mti_oa.o_oi;
1400
1401                 *saved_oi = fst_lmm->lmm_oi;
1402                 fst_lmm->lmm_layout_gen = cpu_to_le16(snd_gen);
1403                 fst_lmm->lmm_oi = snd_lmm->lmm_oi;
1404                 snd_lmm->lmm_oi = *saved_oi;
1405         } else {
1406                 if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1))
1407                         snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
1408                 else if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3))
1409                         snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
1410                 else
1411                         GOTO(stop, rc = -EPROTO);
1412         }
1413         snd_lmm->lmm_layout_gen = cpu_to_le16(fst_gen);
1414
1415         /* Prepare HSM attribute if it's required */
1416         if (flags & SWAP_LAYOUTS_MDS_HSM) {
1417                 const int buflen = sizeof(struct hsm_attrs);
1418
1419                 lu_buf_alloc(fst_hsm_buf, buflen);
1420                 lu_buf_alloc(snd_hsm_buf, buflen);
1421                 if (fst_hsm_buf->lb_buf == NULL || snd_hsm_buf->lb_buf == NULL)
1422                         GOTO(stop, rc = -ENOMEM);
1423
1424                 /* Read HSM attribute */
1425                 rc = mdo_xattr_get(env, fst_o, fst_hsm_buf, XATTR_NAME_HSM,
1426                                    BYPASS_CAPA);
1427                 if (rc < 0)
1428                         GOTO(stop, rc);
1429
1430                 rc = mdo_xattr_get(env, snd_o, snd_hsm_buf, XATTR_NAME_HSM,
1431                                    BYPASS_CAPA);
1432                 if (rc < 0)
1433                         GOTO(stop, rc);
1434
1435                 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_hsm_buf,
1436                                            XATTR_NAME_HSM, LU_XATTR_REPLACE,
1437                                            handle);
1438                 if (rc < 0)
1439                         GOTO(stop, rc);
1440
1441                 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_hsm_buf,
1442                                            XATTR_NAME_HSM, LU_XATTR_REPLACE,
1443                                            handle);
1444                 if (rc < 0)
1445                         GOTO(stop, rc);
1446         }
1447
1448         /* prepare transaction */
1449         rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
1450                                    fst_fl, handle);
1451         if (rc != 0)
1452                 GOTO(stop, rc);
1453
1454         if (fst_buf->lb_buf != NULL)
1455                 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
1456                                            XATTR_NAME_LOV, LU_XATTR_REPLACE,
1457                                            handle);
1458         else
1459                 rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
1460                                            handle);
1461         if (rc != 0)
1462                 GOTO(stop, rc);
1463
1464         rc = mdd_trans_start(env, mdd, handle);
1465         if (rc != 0)
1466                 GOTO(stop, rc);
1467
1468         if (flags & SWAP_LAYOUTS_MDS_HSM) {
1469                 rc = mdd_xattr_hsm_replace(env, fst_o, snd_hsm_buf, handle);
1470                 if (rc < 0)
1471                         GOTO(stop, rc);
1472
1473                 rc = mdd_xattr_hsm_replace(env, snd_o, fst_hsm_buf, handle);
1474                 if (rc < 0) {
1475                         rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
1476                                                     handle);
1477                         if (rc2 < 0)
1478                                 CERROR("%s: restore "DFID" HSM error: %d/%d\n",
1479                                        mdd_obj_dev_name(fst_o),
1480                                        PFID(mdo2fid(fst_o)), rc, rc2);
1481                         GOTO(stop, rc);
1482                 }
1483         }
1484
1485         rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle,
1486                            mdd_object_capa(env, fst_o));
1487         if (rc != 0)
1488                 GOTO(stop, rc);
1489
1490         if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_SWAP_LAYOUTS))) {
1491                 rc = -EOPNOTSUPP;
1492         } else {
1493                 if (fst_buf->lb_buf != NULL)
1494                         rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
1495                                            LU_XATTR_REPLACE, handle,
1496                                            mdd_object_capa(env, snd_o));
1497                 else
1498                         rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle,
1499                                            mdd_object_capa(env, snd_o));
1500         }
1501
1502         if (rc != 0) {
1503                 int steps = 0;
1504
1505                 /* failure on second file, but first was done, so we have
1506                  * to roll back first. */
1507                 if (fst_buf->lb_buf != NULL) {
1508                         fst_lmm->lmm_oi = *saved_oi;
1509                         fst_lmm->lmm_layout_gen = cpu_to_le16(fst_gen - 1);
1510                         rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
1511                                             LU_XATTR_REPLACE, handle,
1512                                             mdd_object_capa(env, fst_o));
1513                 } else {
1514                         rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle,
1515                                             mdd_object_capa(env, fst_o));
1516                 }
1517                 if (rc2 < 0)
1518                         goto do_lbug;
1519
1520                 ++steps;
1521                 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
1522                 if (rc2 < 0)
1523                         goto do_lbug;
1524
1525                 ++steps;
1526                 rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
1527
1528         do_lbug:
1529                 if (rc2 < 0) {
1530                         /* very bad day */
1531                         CERROR("%s: unable to roll back layout swap. FIDs: "
1532                                DFID" and "DFID "error: %d/%d, steps: %d\n",
1533                                mdd_obj_dev_name(fst_o),
1534                                PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
1535                                rc, rc2, steps);
1536                         /* a solution to avoid journal commit is to panic,
1537                          * but it has strong consequences so we use LBUG to
1538                          * allow sysdamin to choose to panic or not
1539                          */
1540                         LBUG();
1541                 }
1542                 GOTO(stop, rc);
1543         }
1544
1545         /* Issue one changelog record per file */
1546         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
1547         if (rc)
1548                 GOTO(stop, rc);
1549
1550         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
1551         if (rc)
1552                 GOTO(stop, rc);
1553         EXIT;
1554
1555 stop:
1556         mdd_trans_stop(env, mdd, rc, handle);
1557         mdd_write_unlock(env, snd_o);
1558         mdd_write_unlock(env, fst_o);
1559
1560         lu_buf_free(fst_buf);
1561         lu_buf_free(snd_buf);
1562         lu_buf_free(fst_hsm_buf);
1563         lu_buf_free(snd_hsm_buf);
1564         return rc;
1565 }
1566
1567 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
1568                           struct mdd_object *child, const struct lu_attr *attr,
1569                           const struct md_op_spec *spec,
1570                           struct dt_allocation_hint *hint)
1571 {
1572         struct dt_object *np = parent ?  mdd_object_child(parent) : NULL;
1573         struct dt_object *nc = mdd_object_child(child);
1574
1575         memset(hint, 0, sizeof(*hint));
1576
1577         /* For striped directory, give striping EA to lod_ah_init, which will
1578          * decide the stripe_offset and stripe count by it. */
1579         if (S_ISDIR(attr->la_mode) &&
1580             unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1581                 hint->dah_eadata = spec->u.sp_ea.eadata;
1582                 hint->dah_eadata_len = spec->u.sp_ea.eadatalen;
1583         } else {
1584                 hint->dah_eadata = NULL;
1585                 hint->dah_eadata_len = 0;
1586         }
1587
1588         CDEBUG(D_INFO, DFID" eadata %p len %d\n", PFID(mdd_object_fid(child)),
1589                hint->dah_eadata, hint->dah_eadata_len);
1590         /* @hint will be initialized by underlying device. */
1591         nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
1592 }
1593
1594 /*
1595  * do NOT or the MAY_*'s, you'll get the weakest
1596  */
1597 int accmode(const struct lu_env *env, const struct lu_attr *la, int flags)
1598 {
1599         int res = 0;
1600
1601         /* Sadly, NFSD reopens a file repeatedly during operation, so the
1602          * "acc_mode = 0" allowance for newly-created files isn't honoured.
1603          * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1604          * owner can write to a file even if it is marked readonly to hide
1605          * its brokenness. (bug 5781) */
1606         if (flags & MDS_OPEN_OWNEROVERRIDE) {
1607                 struct lu_ucred *uc = lu_ucred_check(env);
1608
1609                 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
1610                         return 0;
1611         }
1612
1613         if (flags & FMODE_READ)
1614                 res |= MAY_READ;
1615         if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1616                 res |= MAY_WRITE;
1617         if (flags & MDS_FMODE_EXEC)
1618                 res = MAY_EXEC;
1619         return res;
1620 }
1621
1622 static int mdd_open_sanity_check(const struct lu_env *env,
1623                                 struct mdd_object *obj,
1624                                 const struct lu_attr *attr, int flag)
1625 {
1626         int mode, rc;
1627         ENTRY;
1628
1629         /* EEXIST check */
1630         if (mdd_is_dead_obj(obj))
1631                 RETURN(-ENOENT);
1632
1633         if (S_ISLNK(attr->la_mode))
1634                 RETURN(-ELOOP);
1635
1636         mode = accmode(env, attr, flag);
1637
1638         if (S_ISDIR(attr->la_mode) && (mode & MAY_WRITE))
1639                 RETURN(-EISDIR);
1640
1641         if (!(flag & MDS_OPEN_CREATED)) {
1642                 rc = mdd_permission_internal(env, obj, attr, mode);
1643                 if (rc)
1644                         RETURN(rc);
1645         }
1646
1647         if (S_ISFIFO(attr->la_mode) || S_ISSOCK(attr->la_mode) ||
1648             S_ISBLK(attr->la_mode) || S_ISCHR(attr->la_mode))
1649                 flag &= ~MDS_OPEN_TRUNC;
1650
1651         /* For writing append-only file must open it with append mode. */
1652         if (mdd_is_append(obj)) {
1653                 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1654                         RETURN(-EPERM);
1655                 if (flag & MDS_OPEN_TRUNC)
1656                         RETURN(-EPERM);
1657         }
1658
1659 #if 0
1660         /*
1661          * Now, flag -- O_NOATIME does not be packed by client.
1662          */
1663         if (flag & O_NOATIME) {
1664                 struct lu_ucred *uc = lu_ucred(env);
1665
1666                 if (uc && ((uc->uc_valid == UCRED_OLD) ||
1667                            (uc->uc_valid == UCRED_NEW)) &&
1668                     (uc->uc_fsuid != attr->la_uid) &&
1669                     !md_capable(uc, CFS_CAP_FOWNER))
1670                         RETURN(-EPERM);
1671         }
1672 #endif
1673
1674         RETURN(0);
1675 }
1676
1677 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1678                     int flags)
1679 {
1680         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1681         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1682         int rc = 0;
1683
1684         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1685
1686         rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1687         if (rc)
1688                 RETURN(rc);
1689
1690         rc = mdd_open_sanity_check(env, mdd_obj, attr, flags);
1691         if (rc == 0)
1692                 mdd_obj->mod_count++;
1693
1694         mdd_write_unlock(env, mdd_obj);
1695         return rc;
1696 }
1697
1698 int mdd_declare_object_kill(const struct lu_env *env, struct mdd_object *obj,
1699                             struct md_attr *ma, struct thandle *handle)
1700 {
1701         return mdo_declare_destroy(env, obj, handle);
1702 }
1703
1704 /* return md_attr back,
1705  * if it is last unlink then return lov ea + llog cookie*/
1706 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
1707                     struct md_attr *ma, struct thandle *handle)
1708 {
1709         int rc;
1710         ENTRY;
1711
1712         rc = mdo_destroy(env, obj, handle);
1713
1714         RETURN(rc);
1715 }
1716
1717 static int mdd_declare_close(const struct lu_env *env,
1718                              struct mdd_object *obj,
1719                              struct md_attr *ma,
1720                              struct thandle *handle)
1721 {
1722         int rc;
1723
1724         rc = orph_declare_index_delete(env, obj, handle);
1725         if (rc)
1726                 return rc;
1727
1728         return mdo_declare_destroy(env, obj, handle);
1729 }
1730
1731 /*
1732  * No permission check is needed.
1733  */
1734 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1735                      struct md_attr *ma, int mode)
1736 {
1737         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1738         struct mdd_device *mdd = mdo2mdd(obj);
1739         struct thandle    *handle = NULL;
1740         int rc, is_orphan = 0;
1741         ENTRY;
1742
1743         if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
1744                 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1745                 mdd_obj->mod_count--;
1746                 mdd_write_unlock(env, mdd_obj);
1747
1748                 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
1749                         CDEBUG(D_HA, "Object "DFID" is retained in orphan "
1750                                "list\n", PFID(mdd_object_fid(mdd_obj)));
1751                 RETURN(0);
1752         }
1753
1754         /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
1755          * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
1756         /* check without any lock */
1757         is_orphan = mdd_obj->mod_count == 1 &&
1758                     (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
1759
1760 again:
1761         if (is_orphan) {
1762                 handle = mdd_trans_create(env, mdo2mdd(obj));
1763                 if (IS_ERR(handle))
1764                         RETURN(PTR_ERR(handle));
1765
1766                 rc = mdd_declare_close(env, mdd_obj, ma, handle);
1767                 if (rc)
1768                         GOTO(stop, rc);
1769
1770                 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1771                 if (rc)
1772                         GOTO(stop, rc);
1773
1774                 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1775                 if (rc)
1776                         GOTO(stop, rc);
1777         }
1778
1779         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1780         rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
1781                         mdd_object_capa(env, mdd_obj));
1782         if (rc != 0) {
1783                 CERROR("Failed to get lu_attr of "DFID": %d\n",
1784                        PFID(mdd_object_fid(mdd_obj)), rc);
1785                 GOTO(out, rc);
1786         }
1787
1788         /* check again with lock */
1789         is_orphan = (mdd_obj->mod_count == 1) &&
1790                     ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
1791                      ma->ma_attr.la_nlink == 0);
1792
1793         if (is_orphan && handle == NULL) {
1794                 mdd_write_unlock(env, mdd_obj);
1795                 goto again;
1796         }
1797
1798         mdd_obj->mod_count--; /*release open count */
1799
1800         if (!is_orphan)
1801                 GOTO(out, rc = 0);
1802
1803         /* Orphan object */
1804         /* NB: Object maybe not in orphan list originally, it is rare case for
1805          * mdd_finish_unlink() failure, in that case, the object doesn't have
1806          * ORPHAN_OBJ flag */
1807         if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
1808                 /* remove link to object from orphan index */
1809                 LASSERT(handle != NULL);
1810                 rc = __mdd_orphan_del(env, mdd_obj, handle);
1811                 if (rc != 0) {
1812                         CERROR("%s: unable to delete "DFID" from orphan list: "
1813                                "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1814                                PFID(mdd_object_fid(mdd_obj)), rc);
1815                         /* If object was not deleted from orphan list, do not
1816                          * destroy OSS objects, which will be done when next
1817                          * recovery. */
1818                         GOTO(out, rc);
1819                 }
1820
1821                 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
1822                        "list, OSS objects to be destroyed.\n",
1823                        PFID(mdd_object_fid(mdd_obj)));
1824         }
1825
1826         rc = mdo_destroy(env, mdd_obj, handle);
1827
1828         if (rc != 0) {
1829                 CERROR("%s: unable to delete "DFID" from orphan list: "
1830                        "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1831                        PFID(mdd_object_fid(mdd_obj)), rc);
1832         }
1833         EXIT;
1834
1835 out:
1836         mdd_write_unlock(env, mdd_obj);
1837
1838         if (rc == 0 &&
1839             (mode & (FMODE_WRITE | MDS_OPEN_APPEND | MDS_OPEN_TRUNC)) &&
1840             !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
1841                 if (handle == NULL) {
1842                         handle = mdd_trans_create(env, mdo2mdd(obj));
1843                         if (IS_ERR(handle))
1844                                 GOTO(stop, rc = PTR_ERR(handle));
1845
1846                         rc = mdd_declare_changelog_store(env, mdd, NULL,
1847                                                          handle);
1848                         if (rc)
1849                                 GOTO(stop, rc);
1850
1851                         rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1852                         if (rc)
1853                                 GOTO(stop, rc);
1854                 }
1855
1856                 mdd_changelog_data_store(env, mdd, CL_CLOSE, mode,
1857                                          mdd_obj, handle);
1858         }
1859
1860 stop:
1861         if (handle != NULL && !IS_ERR(handle))
1862                 mdd_trans_stop(env, mdd, rc, handle);
1863
1864         return rc;
1865 }
1866
1867 /*
1868  * Permission check is done when open,
1869  * no need check again.
1870  */
1871 static int mdd_readpage_sanity_check(const struct lu_env *env,
1872                                      struct mdd_object *obj)
1873 {
1874         struct dt_object *next = mdd_object_child(obj);
1875         int rc;
1876         ENTRY;
1877
1878         if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1879                 rc = 0;
1880         else
1881                 rc = -ENOTDIR;
1882
1883         RETURN(rc);
1884 }
1885
1886 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
1887                               int nob, const struct dt_it_ops *iops,
1888                               struct dt_it *it, __u32 attr, void *arg)
1889 {
1890         struct lu_dirpage       *dp = &lp->lp_dir;
1891         void                    *area = dp;
1892         int                      result;
1893         __u64                    hash = 0;
1894         struct lu_dirent        *ent;
1895         struct lu_dirent        *last = NULL;
1896         int                      first = 1;
1897
1898         memset(area, 0, sizeof (*dp));
1899         area += sizeof (*dp);
1900         nob  -= sizeof (*dp);
1901
1902         ent  = area;
1903         do {
1904                 int    len;
1905                 int    recsize;
1906
1907                 len = iops->key_size(env, it);
1908
1909                 /* IAM iterator can return record with zero len. */
1910                 if (len == 0)
1911                         goto next;
1912
1913                 hash = iops->store(env, it);
1914                 if (unlikely(first)) {
1915                         first = 0;
1916                         dp->ldp_hash_start = cpu_to_le64(hash);
1917                 }
1918
1919                 /* calculate max space required for lu_dirent */
1920                 recsize = lu_dirent_calc_size(len, attr);
1921
1922                 if (nob >= recsize) {
1923                         result = iops->rec(env, it, (struct dt_rec *)ent, attr);
1924                         if (result == -ESTALE)
1925                                 goto next;
1926                         if (result != 0)
1927                                 goto out;
1928
1929                         /* osd might not able to pack all attributes,
1930                          * so recheck rec length */
1931                         recsize = le16_to_cpu(ent->lde_reclen);
1932                 } else {
1933                         result = (last != NULL) ? 0 :-EINVAL;
1934                         goto out;
1935                 }
1936                 last = ent;
1937                 ent = (void *)ent + recsize;
1938                 nob -= recsize;
1939
1940 next:
1941                 result = iops->next(env, it);
1942                 if (result == -ESTALE)
1943                         goto next;
1944         } while (result == 0);
1945
1946 out:
1947         dp->ldp_hash_end = cpu_to_le64(hash);
1948         if (last != NULL) {
1949                 if (last->lde_hash == dp->ldp_hash_end)
1950                         dp->ldp_flags |= cpu_to_le32(LDF_COLLIDE);
1951                 last->lde_reclen = 0; /* end mark */
1952         }
1953         if (result > 0)
1954                 /* end of directory */
1955                 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1956         else if (result < 0)
1957                 CWARN("build page failed: %d!\n", result);
1958         return result;
1959 }
1960
1961 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1962                  const struct lu_rdpg *rdpg)
1963 {
1964         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1965         int rc;
1966         ENTRY;
1967
1968         if (mdd_object_exists(mdd_obj) == 0) {
1969                 CERROR("%s: object "DFID" not found: rc = -2\n",
1970                        mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
1971                 return -ENOENT;
1972         }
1973
1974         mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
1975         rc = mdd_readpage_sanity_check(env, mdd_obj);
1976         if (rc)
1977                 GOTO(out_unlock, rc);
1978
1979         if (mdd_is_dead_obj(mdd_obj)) {
1980                 struct page *pg;
1981                 struct lu_dirpage *dp;
1982
1983                 /*
1984                  * According to POSIX, please do not return any entry to client:
1985                  * even dot and dotdot should not be returned.
1986                  */
1987                 CDEBUG(D_INODE, "readdir from dead object: "DFID"\n",
1988                        PFID(mdd_object_fid(mdd_obj)));
1989
1990                 if (rdpg->rp_count <= 0)
1991                         GOTO(out_unlock, rc = -EFAULT);
1992                 LASSERT(rdpg->rp_pages != NULL);
1993
1994                 pg = rdpg->rp_pages[0];
1995                 dp = (struct lu_dirpage *)kmap(pg);
1996                 memset(dp, 0 , sizeof(struct lu_dirpage));
1997                 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
1998                 dp->ldp_hash_end   = cpu_to_le64(MDS_DIR_END_OFF);
1999                 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
2000                 kunmap(pg);
2001                 GOTO(out_unlock, rc = LU_PAGE_SIZE);
2002         }
2003
2004         rc = dt_index_walk(env, mdd_object_child(mdd_obj), rdpg,
2005                            mdd_dir_page_build, NULL);
2006         if (rc >= 0) {
2007                 struct lu_dirpage       *dp;
2008
2009                 dp = kmap(rdpg->rp_pages[0]);
2010                 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
2011                 if (rc == 0) {
2012                         /*
2013                          * No pages were processed, mark this for first page
2014                          * and send back.
2015                          */
2016                         dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
2017                         rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
2018                 }
2019                 kunmap(rdpg->rp_pages[0]);
2020         }
2021
2022         GOTO(out_unlock, rc);
2023 out_unlock:
2024         mdd_read_unlock(env, mdd_obj);
2025         return rc;
2026 }
2027
2028 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
2029 {
2030         struct mdd_object *mdd_obj = md2mdd_obj(obj);
2031
2032         if (mdd_object_exists(mdd_obj) == 0) {
2033                 CERROR("%s: object "DFID" not found: rc = -2\n",
2034                        mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
2035                 return -ENOENT;
2036         }
2037         return dt_object_sync(env, mdd_object_child(mdd_obj));
2038 }
2039
2040 static int mdd_object_lock(const struct lu_env *env,
2041                            struct md_object *obj,
2042                            struct lustre_handle *lh,
2043                            struct ldlm_enqueue_info *einfo,
2044                            ldlm_policy_data_t *policy)
2045 {
2046         struct mdd_object *mdd_obj = md2mdd_obj(obj);
2047         return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
2048                               einfo, policy);
2049 }
2050
2051 static int mdd_object_unlock(const struct lu_env *env,
2052                              struct md_object *obj,
2053                              struct ldlm_enqueue_info *einfo,
2054                              ldlm_policy_data_t *policy)
2055 {
2056         struct mdd_object *mdd_obj = md2mdd_obj(obj);
2057         return dt_object_unlock(env, mdd_object_child(mdd_obj), einfo, policy);
2058 }
2059
2060 const struct md_object_operations mdd_obj_ops = {
2061         .moo_permission         = mdd_permission,
2062         .moo_attr_get           = mdd_attr_get,
2063         .moo_attr_set           = mdd_attr_set,
2064         .moo_xattr_get          = mdd_xattr_get,
2065         .moo_xattr_set          = mdd_xattr_set,
2066         .moo_xattr_list         = mdd_xattr_list,
2067         .moo_xattr_del          = mdd_xattr_del,
2068         .moo_swap_layouts       = mdd_swap_layouts,
2069         .moo_open               = mdd_open,
2070         .moo_close              = mdd_close,
2071         .moo_readpage           = mdd_readpage,
2072         .moo_readlink           = mdd_readlink,
2073         .moo_changelog          = mdd_changelog,
2074         .moo_capa_get           = mdd_capa_get,
2075         .moo_object_sync        = mdd_object_sync,
2076         .moo_object_lock        = mdd_object_lock,
2077         .moo_object_unlock      = mdd_object_unlock,
2078 };