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