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