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