Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / mdd / mdd_object.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_MDS
47
48 #include <linux/module.h>
49 #include <linux/jbd.h>
50 #include <obd.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lprocfs_status.h>
54 /* fid_be_cpu(), fid_cpu_to_be(). */
55 #include <lustre_fid.h>
56
57 #include <linux/ldiskfs_fs.h>
58 #include <lustre_mds.h>
59 #include <lustre/lustre_idl.h>
60
61 #include "mdd_internal.h"
62
63 static struct lu_object_operations mdd_lu_obj_ops;
64
65 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
66                struct lu_attr *la, struct lustre_capa *capa)
67 {
68         LASSERTF(mdd_object_exists(obj), "FID is "DFID"\n",
69                  PFID(mdd_object_fid(obj)));
70         return mdo_attr_get(env, obj, la, capa);
71 }
72
73 static void mdd_flags_xlate(struct mdd_object *obj, __u32 flags)
74 {
75         obj->mod_flags &= ~(APPEND_OBJ|IMMUTE_OBJ);
76
77         if (flags & LUSTRE_APPEND_FL)
78                 obj->mod_flags |= APPEND_OBJ;
79
80         if (flags & LUSTRE_IMMUTABLE_FL)
81                 obj->mod_flags |= IMMUTE_OBJ;
82 }
83
84 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
85 {
86         struct lu_buf *buf;
87
88         buf = &mdd_env_info(env)->mti_buf;
89         buf->lb_buf = area;
90         buf->lb_len = len;
91         return buf;
92 }
93
94 struct llog_cookie *mdd_max_cookie_get(const struct lu_env *env,
95                                        struct mdd_device *mdd)
96 {
97         struct mdd_thread_info *mti = mdd_env_info(env);
98         int                     max_cookie_size;
99
100         max_cookie_size = mdd_lov_cookiesize(env, mdd);
101         if (unlikely(mti->mti_max_cookie_size < max_cookie_size)) {
102                 if (mti->mti_max_cookie)
103                         OBD_FREE(mti->mti_max_cookie, mti->mti_max_cookie_size);
104                 mti->mti_max_cookie = NULL;
105                 mti->mti_max_cookie_size = 0;
106         }
107         if (unlikely(mti->mti_max_cookie == NULL)) {
108                 OBD_ALLOC(mti->mti_max_cookie, max_cookie_size);
109                 if (unlikely(mti->mti_max_cookie != NULL))
110                         mti->mti_max_cookie_size = max_cookie_size;
111         }
112         return mti->mti_max_cookie;
113 }
114
115 struct lov_mds_md *mdd_max_lmm_get(const struct lu_env *env,
116                                    struct mdd_device *mdd)
117 {
118         struct mdd_thread_info *mti = mdd_env_info(env);
119         int                     max_lmm_size;
120
121         max_lmm_size = mdd_lov_mdsize(env, mdd);
122         if (unlikely(mti->mti_max_lmm_size < max_lmm_size)) {
123                 if (mti->mti_max_lmm)
124                         OBD_FREE(mti->mti_max_lmm, mti->mti_max_lmm_size);
125                 mti->mti_max_lmm = NULL;
126                 mti->mti_max_lmm_size = 0;
127         }
128         if (unlikely(mti->mti_max_lmm == NULL)) {
129                 OBD_ALLOC(mti->mti_max_lmm, max_lmm_size);
130                 if (unlikely(mti->mti_max_lmm != NULL))
131                         mti->mti_max_lmm_size = max_lmm_size;
132         }
133         return mti->mti_max_lmm;
134 }
135
136 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
137                                        const void *area, ssize_t len)
138 {
139         struct lu_buf *buf;
140
141         buf = &mdd_env_info(env)->mti_buf;
142         buf->lb_buf = (void *)area;
143         buf->lb_len = len;
144         return buf;
145 }
146
147 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
148 {
149         struct mdd_thread_info *info;
150
151         info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
152         LASSERT(info != NULL);
153         return info;
154 }
155
156 struct lu_object *mdd_object_alloc(const struct lu_env *env,
157                                    const struct lu_object_header *hdr,
158                                    struct lu_device *d)
159 {
160         struct mdd_object *mdd_obj;
161
162         OBD_ALLOC_PTR(mdd_obj);
163         if (mdd_obj != NULL) {
164                 struct lu_object *o;
165
166                 o = mdd2lu_obj(mdd_obj);
167                 lu_object_init(o, NULL, d);
168                 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
169                 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
170                 mdd_obj->mod_count = 0;
171                 o->lo_ops = &mdd_lu_obj_ops;
172                 return o;
173         } else {
174                 return NULL;
175         }
176 }
177
178 static int mdd_object_init(const struct lu_env *env, struct lu_object *o)
179 {
180         struct mdd_device *d = lu2mdd_dev(o->lo_dev);
181         struct lu_object  *below;
182         struct lu_device  *under;
183         ENTRY;
184
185         under = &d->mdd_child->dd_lu_dev;
186         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
187         mdd_pdlock_init(lu2mdd_obj(o));
188         if (below == NULL)
189                 RETURN(-ENOMEM);
190
191         lu_object_add(o, below);
192         RETURN(0);
193 }
194
195 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
196 {
197         if (lu_object_exists(o))
198                 return mdd_get_flags(env, lu2mdd_obj(o));
199         else
200                 return 0;
201 }
202
203 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
204 {
205         struct mdd_object *mdd = lu2mdd_obj(o);
206         
207         lu_object_fini(o);
208         OBD_FREE_PTR(mdd);
209 }
210
211 static int mdd_object_print(const struct lu_env *env, void *cookie,
212                             lu_printer_t p, const struct lu_object *o)
213 {
214         return (*p)(env, cookie, LUSTRE_MDD_NAME"-object@%p", o);
215 }
216
217 /* orphan handling is here */
218 static void mdd_object_delete(const struct lu_env *env,
219                                struct lu_object *o)
220 {
221         struct mdd_object *mdd_obj = lu2mdd_obj(o);
222         struct thandle *handle = NULL;
223         ENTRY;
224
225         if (lu2mdd_dev(o->lo_dev)->mdd_orphans == NULL)
226                 return;
227
228         if (mdd_obj->mod_flags & ORPHAN_OBJ) {
229                 mdd_txn_param_build(env, lu2mdd_dev(o->lo_dev),
230                                     MDD_TXN_INDEX_DELETE_OP);
231                 handle = mdd_trans_start(env, lu2mdd_dev(o->lo_dev));
232                 if (IS_ERR(handle))
233                         CERROR("Cannot get thandle\n");
234                 else {
235                         mdd_write_lock(env, mdd_obj);
236                         /* let's remove obj from the orphan list */
237                         __mdd_orphan_del(env, mdd_obj, handle);
238                         mdd_write_unlock(env, mdd_obj);
239                         mdd_trans_stop(env, lu2mdd_dev(o->lo_dev),
240                                        0, handle);
241                 }
242         }
243 }
244
245 static struct lu_object_operations mdd_lu_obj_ops = {
246         .loo_object_init    = mdd_object_init,
247         .loo_object_start   = mdd_object_start,
248         .loo_object_free    = mdd_object_free,
249         .loo_object_print   = mdd_object_print,
250         .loo_object_delete  = mdd_object_delete
251 };
252
253 struct mdd_object *mdd_object_find(const struct lu_env *env,
254                                    struct mdd_device *d,
255                                    const struct lu_fid *f)
256 {
257         struct lu_object *o, *lo;
258         struct mdd_object *m;
259         ENTRY;
260
261         o = lu_object_find(env, mdd2lu_dev(d)->ld_site, f);
262         if (IS_ERR(o))
263                 m = (struct mdd_object *)o;
264         else {
265                 lo = lu_object_locate(o->lo_header, mdd2lu_dev(d)->ld_type);
266                 /* remote object can't be located and should be put then */
267                 if (lo == NULL)
268                         lu_object_put(env, o);
269                 m = lu2mdd_obj(lo);
270         }
271         RETURN(m);
272 }
273
274 int mdd_get_flags(const struct lu_env *env, struct mdd_object *obj)
275 {
276         struct lu_attr *la = &mdd_env_info(env)->mti_la;
277         int rc;
278
279         ENTRY;
280         rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
281         if (rc == 0) {
282                 mdd_flags_xlate(obj, la->la_flags);
283                 if (S_ISDIR(la->la_mode) && la->la_nlink == 1)
284                         obj->mod_flags |= MNLINK_OBJ;
285         }
286         RETURN(rc);
287 }
288
289 /* get only inode attributes */
290 int mdd_iattr_get(const struct lu_env *env, struct mdd_object *mdd_obj,
291                   struct md_attr *ma)
292 {
293         int rc = 0;
294         ENTRY;
295
296         if (ma->ma_valid & MA_INODE)
297                 RETURN(0);
298
299         rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
300                           mdd_object_capa(env, mdd_obj));
301         if (rc == 0)
302                 ma->ma_valid |= MA_INODE;
303         RETURN(rc);
304 }
305
306 static int mdd_get_default_md(struct mdd_object *mdd_obj,
307                 struct lov_mds_md *lmm, int *size)
308 {
309         struct lov_desc *ldesc;
310         struct mdd_device *mdd = mdo2mdd(&mdd_obj->mod_obj);
311         ENTRY;
312
313         ldesc = &mdd->mdd_obd_dev->u.mds.mds_lov_desc;
314         LASSERT(ldesc != NULL);
315
316         if (!lmm)
317                 RETURN(0);
318
319         lmm->lmm_magic = LOV_MAGIC_V1;
320         lmm->lmm_object_gr = LOV_OBJECT_GROUP_DEFAULT;
321         lmm->lmm_pattern = ldesc->ld_pattern;
322         lmm->lmm_stripe_size = ldesc->ld_default_stripe_size;
323         lmm->lmm_stripe_count = ldesc->ld_default_stripe_count;
324         *size = sizeof(struct lov_mds_md);
325
326         RETURN(sizeof(struct lov_mds_md));
327 }
328
329 /* get lov EA only */
330 static int __mdd_lmm_get(const struct lu_env *env,
331                          struct mdd_object *mdd_obj, struct md_attr *ma)
332 {
333         int rc;
334         ENTRY;
335
336         if (ma->ma_valid & MA_LOV)
337                 RETURN(0);
338
339         rc = mdd_get_md(env, mdd_obj, ma->ma_lmm, &ma->ma_lmm_size,
340                         MDS_LOV_MD_NAME);
341
342         if (rc == 0 && (ma->ma_need & MA_LOV_DEF)) {
343                 rc = mdd_get_default_md(mdd_obj, ma->ma_lmm,
344                                 &ma->ma_lmm_size);
345         }
346
347         if (rc > 0) {
348                 ma->ma_valid |= MA_LOV;
349                 rc = 0;
350         }
351         RETURN(rc);
352 }
353
354 int mdd_lmm_get_locked(const struct lu_env *env, struct mdd_object *mdd_obj,
355                        struct md_attr *ma)
356 {
357         int rc;
358         ENTRY;
359
360         mdd_read_lock(env, mdd_obj);
361         rc = __mdd_lmm_get(env, mdd_obj, ma);
362         mdd_read_unlock(env, mdd_obj);
363         RETURN(rc);
364 }
365
366 /* get lmv EA only*/
367 static int __mdd_lmv_get(const struct lu_env *env,
368                          struct mdd_object *mdd_obj, struct md_attr *ma)
369 {
370         int rc;
371         ENTRY;
372
373         if (ma->ma_valid & MA_LMV)
374                 RETURN(0);
375
376         rc = mdd_get_md(env, mdd_obj, ma->ma_lmv, &ma->ma_lmv_size,
377                         MDS_LMV_MD_NAME);
378         if (rc > 0) {
379                 ma->ma_valid |= MA_LMV;
380                 rc = 0;
381         }
382         RETURN(rc);
383 }
384
385 static int mdd_attr_get_internal(const struct lu_env *env,
386                                  struct mdd_object *mdd_obj,
387                                  struct md_attr *ma)
388 {
389         int rc = 0;
390         ENTRY;
391
392         if (ma->ma_need & MA_INODE)
393                 rc = mdd_iattr_get(env, mdd_obj, ma);
394
395         if (rc == 0 && ma->ma_need & MA_LOV) {
396                 if (S_ISREG(mdd_object_type(mdd_obj)) ||
397                     S_ISDIR(mdd_object_type(mdd_obj)))
398                         rc = __mdd_lmm_get(env, mdd_obj, ma);
399         }
400         if (rc == 0 && ma->ma_need & MA_LMV) {
401                 if (S_ISDIR(mdd_object_type(mdd_obj)))
402                         rc = __mdd_lmv_get(env, mdd_obj, ma);
403         }
404 #ifdef CONFIG_FS_POSIX_ACL
405         if (rc == 0 && ma->ma_need & MA_ACL_DEF) {
406                 if (S_ISDIR(mdd_object_type(mdd_obj)))
407                         rc = mdd_acl_def_get(env, mdd_obj, ma);
408         }
409 #endif
410         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64"\n",
411                rc, ma->ma_valid);
412         RETURN(rc);
413 }
414
415 int mdd_attr_get_internal_locked(const struct lu_env *env,
416                                  struct mdd_object *mdd_obj, struct md_attr *ma)
417 {
418         int rc;
419         int needlock = ma->ma_need & (MA_LOV | MA_LMV | MA_ACL_DEF);
420
421         if (needlock)
422                 mdd_read_lock(env, mdd_obj);
423         rc = mdd_attr_get_internal(env, mdd_obj, ma);
424         if (needlock)
425                 mdd_read_unlock(env, mdd_obj);
426         return rc;
427 }
428
429 /*
430  * No permission check is needed.
431  */
432 static int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
433                         struct md_attr *ma)
434 {
435         struct mdd_object *mdd_obj = md2mdd_obj(obj);
436         int                rc;
437
438         ENTRY;
439         rc = mdd_attr_get_internal_locked(env, mdd_obj, ma);
440         RETURN(rc);
441 }
442
443 /*
444  * No permission check is needed.
445  */
446 static int mdd_xattr_get(const struct lu_env *env,
447                          struct md_object *obj, struct lu_buf *buf,
448                          const char *name)
449 {
450         struct mdd_object *mdd_obj = md2mdd_obj(obj);
451         int rc;
452
453         ENTRY;
454
455         LASSERT(mdd_object_exists(mdd_obj));
456
457         mdd_read_lock(env, mdd_obj);
458         rc = mdo_xattr_get(env, mdd_obj, buf, name,
459                            mdd_object_capa(env, mdd_obj));
460         mdd_read_unlock(env, mdd_obj);
461
462         RETURN(rc);
463 }
464
465 /*
466  * Permission check is done when open,
467  * no need check again.
468  */
469 static int mdd_readlink(const struct lu_env *env, struct md_object *obj,
470                         struct lu_buf *buf)
471 {
472         struct mdd_object *mdd_obj = md2mdd_obj(obj);
473         struct dt_object  *next;
474         loff_t             pos = 0;
475         int                rc;
476         ENTRY;
477
478         LASSERT(mdd_object_exists(mdd_obj));
479
480         next = mdd_object_child(mdd_obj);
481         mdd_read_lock(env, mdd_obj);
482         rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
483                                          mdd_object_capa(env, mdd_obj));
484         mdd_read_unlock(env, mdd_obj);
485         RETURN(rc);
486 }
487
488 /*
489  * No permission check is needed.
490  */
491 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
492                           struct lu_buf *buf)
493 {
494         struct mdd_object *mdd_obj = md2mdd_obj(obj);
495         int rc;
496
497         ENTRY;
498
499         mdd_read_lock(env, mdd_obj);
500         rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
501         mdd_read_unlock(env, mdd_obj);
502
503         RETURN(rc);
504 }
505
506 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
507                                struct mdd_object *c, struct md_attr *ma,
508                                struct thandle *handle)
509 {
510         struct lu_attr *attr = &ma->ma_attr;
511         struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
512         int rc;
513         ENTRY;
514
515         if (!mdd_object_exists(c)) {
516                 struct dt_object *next = mdd_object_child(c);
517                 LASSERT(next);
518
519                 /* @hint will be initialized by underlying device. */
520                 next->do_ops->do_ah_init(env, hint,
521                                          p ? mdd_object_child(p) : NULL,
522                                          attr->la_mode & S_IFMT);
523                 rc = mdo_create_obj(env, c, attr, hint, handle);
524                 LASSERT(ergo(rc == 0, mdd_object_exists(c)));
525         } else
526                 rc = -EEXIST;
527
528         RETURN(rc);
529 }
530
531 /**
532  * Make sure the ctime is increased only.
533  */
534 static inline int mdd_attr_check(const struct lu_env *env,
535                                  struct mdd_object *obj,
536                                  struct lu_attr *attr)
537 {
538         struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
539         int rc;
540         ENTRY;
541
542         if (attr->la_valid & LA_CTIME) {
543                 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
544                 if (rc)
545                         RETURN(rc);
546
547                 if (attr->la_ctime < tmp_la->la_ctime)
548                         attr->la_valid &= ~(LA_MTIME | LA_CTIME);
549                 else if (attr->la_valid == LA_CTIME &&
550                          attr->la_ctime == tmp_la->la_ctime)
551                         attr->la_valid &= ~LA_CTIME;
552         }
553         RETURN(0);
554 }
555
556 int mdd_attr_set_internal(const struct lu_env *env,
557                           struct mdd_object *obj,
558                           struct lu_attr *attr,
559                           struct thandle *handle,
560                           int needacl)
561 {
562         int rc;
563         ENTRY;
564
565         rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
566 #ifdef CONFIG_FS_POSIX_ACL
567         if (!rc && (attr->la_valid & LA_MODE) && needacl)
568                 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
569 #endif
570         RETURN(rc);
571 }
572
573 int mdd_attr_check_set_internal(const struct lu_env *env,
574                                 struct mdd_object *obj,
575                                 struct lu_attr *attr,
576                                 struct thandle *handle,
577                                 int needacl)
578 {
579         int rc;
580         ENTRY;
581
582         rc = mdd_attr_check(env, obj, attr);
583         if (rc)
584                 RETURN(rc);
585
586         if (attr->la_valid)
587                 rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
588         RETURN(rc);
589 }
590
591 static int mdd_attr_set_internal_locked(const struct lu_env *env,
592                                         struct mdd_object *obj,
593                                         struct lu_attr *attr,
594                                         struct thandle *handle,
595                                         int needacl)
596 {
597         int rc;
598         ENTRY;
599
600         needacl = needacl && (attr->la_valid & LA_MODE);
601         if (needacl)
602                 mdd_write_lock(env, obj);
603         rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
604         if (needacl)
605                 mdd_write_unlock(env, obj);
606         RETURN(rc);
607 }
608
609 int mdd_attr_check_set_internal_locked(const struct lu_env *env,
610                                        struct mdd_object *obj,
611                                        struct lu_attr *attr,
612                                        struct thandle *handle,
613                                        int needacl)
614 {
615         int rc;
616         ENTRY;
617
618         needacl = needacl && (attr->la_valid & LA_MODE);
619         if (needacl)
620                 mdd_write_lock(env, obj);
621         rc = mdd_attr_check_set_internal(env, obj, attr, handle, needacl);
622         if (needacl)
623                 mdd_write_unlock(env, obj);
624         RETURN(rc);
625 }
626
627 static int __mdd_xattr_set(const struct lu_env *env, struct mdd_object *obj,
628                            const struct lu_buf *buf, const char *name,
629                            int fl, struct thandle *handle)
630 {
631         struct lustre_capa *capa = mdd_object_capa(env, obj);
632         int rc = -EINVAL;
633         ENTRY;
634
635         if (buf->lb_buf && buf->lb_len > 0)
636                 rc = mdo_xattr_set(env, obj, buf, name, 0, handle, capa);
637         else if (buf->lb_buf == NULL && buf->lb_len == 0)
638                 rc = mdo_xattr_del(env, obj, name, handle, capa);
639
640         RETURN(rc);
641 }
642
643 /*
644  * This gives the same functionality as the code between
645  * sys_chmod and inode_setattr
646  * chown_common and inode_setattr
647  * utimes and inode_setattr
648  * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
649  */
650 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
651                         struct lu_attr *la, const struct md_attr *ma)
652 {
653         struct lu_attr   *tmp_la     = &mdd_env_info(env)->mti_la;
654         struct md_ucred  *uc         = md_ucred(env);
655         int               rc;
656         ENTRY;
657
658         if (!la->la_valid)
659                 RETURN(0);
660
661         /* Do not permit change file type */
662         if (la->la_valid & LA_TYPE)
663                 RETURN(-EPERM);
664
665         /* They should not be processed by setattr */
666         if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
667                 RETURN(-EPERM);
668
669         rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
670         if (rc)
671                 RETURN(rc);
672
673         if (la->la_valid == LA_CTIME) {
674                 if (!(ma->ma_attr_flags & MDS_PERM_BYPASS))
675                         /* This is only for set ctime when rename's source is
676                          * on remote MDS. */
677                         rc = mdd_may_delete(env, NULL, obj,
678                                             (struct md_attr *)ma, 1, 0);
679                 if (rc == 0 && la->la_ctime <= tmp_la->la_ctime)
680                         la->la_valid &= ~LA_CTIME;
681                 RETURN(rc);
682         }
683
684         if (la->la_valid == LA_ATIME) {
685                 /* This is atime only set for read atime update on close. */
686                 if (la->la_atime <= tmp_la->la_atime +
687                                     mdd_obj2mdd_dev(obj)->mdd_atime_diff)
688                         la->la_valid &= ~LA_ATIME;
689                 RETURN(0);
690         }
691  
692         /* Check if flags change. */
693         if (la->la_valid & LA_FLAGS) {
694                 unsigned int oldflags = 0;
695                 unsigned int newflags = la->la_flags &
696                                 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
697
698                 if ((uc->mu_fsuid != tmp_la->la_uid) &&
699                     !mdd_capable(uc, CAP_FOWNER))
700                         RETURN(-EPERM);
701
702                 /* XXX: the IMMUTABLE and APPEND_ONLY flags can
703                  * only be changed by the relevant capability. */
704                 if (mdd_is_immutable(obj))
705                         oldflags |= LUSTRE_IMMUTABLE_FL;
706                 if (mdd_is_append(obj))
707                         oldflags |= LUSTRE_APPEND_FL; 
708                 if ((oldflags ^ newflags) &&
709                     !mdd_capable(uc, CAP_LINUX_IMMUTABLE))
710                         RETURN(-EPERM);
711
712                 if (!S_ISDIR(tmp_la->la_mode))
713                         la->la_flags &= ~LUSTRE_DIRSYNC_FL;
714         }
715
716         if ((mdd_is_immutable(obj) || mdd_is_append(obj)) &&
717             (la->la_valid & ~LA_FLAGS) &&
718             !(ma->ma_attr_flags & MDS_PERM_BYPASS))
719                 RETURN(-EPERM);
720
721         /* Check for setting the obj time. */
722         if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
723             !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
724                 if ((uc->mu_fsuid != tmp_la->la_uid) &&
725                     !mdd_capable(uc, CAP_FOWNER)) {
726                         rc = mdd_permission_internal_locked(env, obj, tmp_la,
727                                                             MAY_WRITE);
728                         if (rc)
729                                 RETURN(rc);
730                 }
731         }
732
733         /* Make sure a caller can chmod. */
734         if (la->la_valid & LA_MODE) {
735                 /* Bypass la_vaild == LA_MODE,
736                  * this is for changing file with SUID or SGID. */
737                 if ((la->la_valid & ~LA_MODE) &&
738                     (uc->mu_fsuid != tmp_la->la_uid) &&
739                     !mdd_capable(uc, CAP_FOWNER))
740                         RETURN(-EPERM);
741
742                 if (la->la_mode == (umode_t) -1)
743                         la->la_mode = tmp_la->la_mode;
744                 else
745                         la->la_mode = (la->la_mode & S_IALLUGO) |
746                                       (tmp_la->la_mode & ~S_IALLUGO);
747
748                 /* Also check the setgid bit! */
749                 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ? la->la_gid :
750                                 tmp_la->la_gid) && !mdd_capable(uc, CAP_FSETID))
751                         la->la_mode &= ~S_ISGID;
752         } else {
753                la->la_mode = tmp_la->la_mode;
754         }
755
756         /* Make sure a caller can chown. */
757         if (la->la_valid & LA_UID) {
758                 if (la->la_uid == (uid_t) -1)
759                         la->la_uid = tmp_la->la_uid;
760                 if (((uc->mu_fsuid != tmp_la->la_uid) ||
761                     (la->la_uid != tmp_la->la_uid)) &&
762                     !mdd_capable(uc, CAP_CHOWN))
763                         RETURN(-EPERM);
764
765                 /* If the user or group of a non-directory has been
766                  * changed by a non-root user, remove the setuid bit.
767                  * 19981026 David C Niemi <niemi@tux.org>
768                  *
769                  * Changed this to apply to all users, including root,
770                  * to avoid some races. This is the behavior we had in
771                  * 2.0. The check for non-root was definitely wrong
772                  * for 2.2 anyway, as it should have been using
773                  * CAP_FSETID rather than fsuid -- 19990830 SD. */
774                 if (((tmp_la->la_mode & S_ISUID) == S_ISUID) &&
775                     !S_ISDIR(tmp_la->la_mode)) {
776                         la->la_mode &= ~S_ISUID;
777                         la->la_valid |= LA_MODE;
778                 }
779         }
780
781         /* Make sure caller can chgrp. */
782         if (la->la_valid & LA_GID) {
783                 if (la->la_gid == (gid_t) -1)
784                         la->la_gid = tmp_la->la_gid;
785                 if (((uc->mu_fsuid != tmp_la->la_uid) ||
786                     ((la->la_gid != tmp_la->la_gid) &&
787                     !lustre_in_group_p(uc, la->la_gid))) &&
788                     !mdd_capable(uc, CAP_CHOWN))
789                         RETURN(-EPERM);
790
791                 /* Likewise, if the user or group of a non-directory
792                  * has been changed by a non-root user, remove the
793                  * setgid bit UNLESS there is no group execute bit
794                  * (this would be a file marked for mandatory
795                  * locking).  19981026 David C Niemi <niemi@tux.org>
796                  *
797                  * Removed the fsuid check (see the comment above) --
798                  * 19990830 SD. */
799                 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
800                      (S_ISGID | S_IXGRP)) && !S_ISDIR(tmp_la->la_mode)) {
801                         la->la_mode &= ~S_ISGID;
802                         la->la_valid |= LA_MODE;
803                 }
804         }
805
806         /* For both Size-on-MDS case and truncate case,
807          * "la->la_valid & (LA_SIZE | LA_BLOCKS)" are ture.
808          * We distinguish them by "ma->ma_attr_flags & MDS_SOM".
809          * For SOM case, it is true, the MAY_WRITE perm has been checked
810          * when open, no need check again. For truncate case, it is false,
811          * the MAY_WRITE perm should be checked here. */
812         if (ma->ma_attr_flags & MDS_SOM) {
813                 /* For the "Size-on-MDS" setattr update, merge coming
814                  * attributes with the set in the inode. BUG 10641 */
815                 if ((la->la_valid & LA_ATIME) &&
816                     (la->la_atime <= tmp_la->la_atime))
817                         la->la_valid &= ~LA_ATIME;
818
819                 /* OST attributes do not have a priority over MDS attributes,
820                  * so drop times if ctime is equal. */
821                 if ((la->la_valid & LA_CTIME) &&
822                     (la->la_ctime <= tmp_la->la_ctime))
823                         la->la_valid &= ~(LA_MTIME | LA_CTIME);
824         } else {
825                 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
826                         if (!((ma->ma_attr_flags & MDS_OPEN_OWNEROVERRIDE) &&
827                               (uc->mu_fsuid == tmp_la->la_uid)) &&
828                             !(ma->ma_attr_flags & MDS_PERM_BYPASS)) {
829                                 rc = mdd_permission_internal_locked(env, obj,
830                                                             tmp_la, MAY_WRITE);
831                                 if (rc)
832                                         RETURN(rc);
833                         }
834                 }
835                 if (la->la_valid & LA_CTIME) {
836                         /* The pure setattr, it has the priority over what is
837                          * already set, do not drop it if ctime is equal. */
838                         if (la->la_ctime < tmp_la->la_ctime)
839                                 la->la_valid &= ~(LA_ATIME | LA_MTIME |
840                                                   LA_CTIME);
841                 }
842         }
843
844         RETURN(0);
845 }
846
847 /* set attr and LOV EA at once, return updated attr */
848 static int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
849                         const struct md_attr *ma)
850 {
851         struct mdd_object *mdd_obj = md2mdd_obj(obj);
852         struct mdd_device *mdd = mdo2mdd(obj);
853         struct thandle *handle;
854         struct lov_mds_md *lmm = NULL;
855         struct llog_cookie *logcookies = NULL;
856         int  rc, lmm_size = 0, cookie_size = 0;
857         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
858         ENTRY;
859
860         mdd_txn_param_build(env, mdd, MDD_TXN_ATTR_SET_OP);
861         handle = mdd_trans_start(env, mdd);
862         if (IS_ERR(handle))
863                 RETURN(PTR_ERR(handle));
864         /*TODO: add lock here*/
865         /* start a log jounal handle if needed */
866         if (S_ISREG(mdd_object_type(mdd_obj)) &&
867             ma->ma_attr.la_valid & (LA_UID | LA_GID)) {
868                 lmm_size = mdd_lov_mdsize(env, mdd);
869                 lmm = mdd_max_lmm_get(env, mdd);
870                 if (lmm == NULL)
871                         GOTO(cleanup, rc = -ENOMEM);
872
873                 rc = mdd_get_md_locked(env, mdd_obj, lmm, &lmm_size,
874                                 MDS_LOV_MD_NAME);
875
876                 if (rc < 0)
877                         GOTO(cleanup, rc);
878         }
879
880         if (ma->ma_attr.la_valid & (ATTR_MTIME | ATTR_CTIME))
881                 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
882                        ma->ma_attr.la_mtime, ma->ma_attr.la_ctime);
883
884         *la_copy = ma->ma_attr;
885         rc = mdd_fix_attr(env, mdd_obj, la_copy, ma);
886         if (rc)
887                 GOTO(cleanup, rc);
888
889         if (la_copy->la_valid & LA_FLAGS) {
890                 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
891                                                   handle, 1);
892                 if (rc == 0)
893                         mdd_flags_xlate(mdd_obj, la_copy->la_flags);
894         } else if (la_copy->la_valid) {            /* setattr */
895                 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
896                                                   handle, 1);
897                 /* journal chown/chgrp in llog, just like unlink */
898                 if (rc == 0 && lmm_size){
899                         cookie_size = mdd_lov_cookiesize(env, mdd);
900                         logcookies = mdd_max_cookie_get(env, mdd);
901                         if (logcookies == NULL)
902                                 GOTO(cleanup, rc = -ENOMEM);
903
904                         if (mdd_setattr_log(env, mdd, ma, lmm, lmm_size,
905                                             logcookies, cookie_size) <= 0)
906                                 logcookies = NULL;
907                 }
908         }
909
910         if (rc == 0 && ma->ma_valid & MA_LOV) {
911                 umode_t mode;
912
913                 mode = mdd_object_type(mdd_obj);
914                 if (S_ISREG(mode) || S_ISDIR(mode)) {
915                         rc = mdd_lsm_sanity_check(env, mdd_obj);
916                         if (rc)
917                                 GOTO(cleanup, rc);
918
919                         rc = mdd_lov_set_md(env, NULL, mdd_obj, ma->ma_lmm,
920                                             ma->ma_lmm_size, handle, 1);
921                 }
922
923         }
924 cleanup:
925         mdd_trans_stop(env, mdd, rc, handle);
926         if (rc == 0 && (lmm != NULL && lmm_size > 0 )) {
927                 /*set obd attr, if needed*/
928                 rc = mdd_lov_setattr_async(env, mdd_obj, lmm, lmm_size,
929                                            logcookies);
930         }
931         RETURN(rc);
932 }
933
934 int mdd_xattr_set_txn(const struct lu_env *env, struct mdd_object *obj,
935                       const struct lu_buf *buf, const char *name, int fl,
936                       struct thandle *handle)
937 {
938         int  rc;
939         ENTRY;
940
941         mdd_write_lock(env, obj);
942         rc = __mdd_xattr_set(env, obj, buf, name, fl, handle);
943         mdd_write_unlock(env, obj);
944
945         RETURN(rc);
946 }
947
948 static int mdd_xattr_sanity_check(const struct lu_env *env,
949                                   struct mdd_object *obj)
950 {
951         struct lu_attr  *tmp_la = &mdd_env_info(env)->mti_la;
952         struct md_ucred *uc     = md_ucred(env);
953         int rc;
954         ENTRY;
955
956         if (mdd_is_immutable(obj) || mdd_is_append(obj))
957                 RETURN(-EPERM);
958
959         rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
960         if (rc)
961                 RETURN(rc);
962
963         if ((uc->mu_fsuid != tmp_la->la_uid) && !mdd_capable(uc, CAP_FOWNER))
964                 RETURN(-EPERM);
965
966         RETURN(rc);
967 }
968
969 /**
970  * The caller should guarantee to update the object ctime
971  * after xattr_set if needed.
972  */
973 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
974                          const struct lu_buf *buf, const char *name,
975                          int fl)
976 {
977         struct mdd_object *mdd_obj = md2mdd_obj(obj);
978         struct mdd_device *mdd = mdo2mdd(obj);
979         struct thandle *handle;
980         int  rc;
981         ENTRY;
982
983         rc = mdd_xattr_sanity_check(env, mdd_obj);
984         if (rc)
985                 RETURN(rc);
986
987         mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
988         handle = mdd_trans_start(env, mdd);
989         if (IS_ERR(handle))
990                 RETURN(PTR_ERR(handle));
991
992         rc = mdd_xattr_set_txn(env, mdd_obj, buf, name, fl, handle);
993         mdd_trans_stop(env, mdd, rc, handle);
994
995         RETURN(rc);
996 }
997
998 /**
999  * The caller should guarantee to update the object ctime
1000  * after xattr_set if needed.
1001  */
1002 int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1003                   const char *name)
1004 {
1005         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1006         struct mdd_device *mdd = mdo2mdd(obj);
1007         struct thandle *handle;
1008         int  rc;
1009         ENTRY;
1010
1011         rc = mdd_xattr_sanity_check(env, mdd_obj);
1012         if (rc)
1013                 RETURN(rc);
1014
1015         mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
1016         handle = mdd_trans_start(env, mdd);
1017         if (IS_ERR(handle))
1018                 RETURN(PTR_ERR(handle));
1019
1020         mdd_write_lock(env, mdd_obj);
1021         rc = mdo_xattr_del(env, mdd_obj, name, handle,
1022                            mdd_object_capa(env, mdd_obj));
1023         mdd_write_unlock(env, mdd_obj);
1024         mdd_trans_stop(env, mdd, rc, handle);
1025
1026         RETURN(rc);
1027 }
1028
1029 /* partial unlink */
1030 static int mdd_ref_del(const struct lu_env *env, struct md_object *obj,
1031                        struct md_attr *ma)
1032 {
1033         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1034         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1035         struct mdd_device *mdd = mdo2mdd(obj);
1036         struct thandle *handle;
1037         int rc;
1038         ENTRY;
1039
1040         /*
1041          * Check -ENOENT early here because we need to get object type
1042          * to calculate credits before transaction start
1043          */
1044         if (!mdd_object_exists(mdd_obj))
1045                 RETURN(-ENOENT);
1046
1047         LASSERT(mdd_object_exists(mdd_obj) > 0);
1048
1049         rc = mdd_log_txn_param_build(env, obj, ma, MDD_TXN_UNLINK_OP);
1050         if (rc)
1051                 RETURN(rc);
1052
1053         handle = mdd_trans_start(env, mdd);
1054         if (IS_ERR(handle))
1055                 RETURN(-ENOMEM);
1056
1057         mdd_write_lock(env, mdd_obj);
1058
1059         rc = mdd_unlink_sanity_check(env, NULL, mdd_obj, ma);
1060         if (rc)
1061                 GOTO(cleanup, rc);
1062
1063         __mdd_ref_del(env, mdd_obj, handle, 0);
1064
1065         if (S_ISDIR(lu_object_attr(&obj->mo_lu))) {
1066                 /* unlink dot */
1067                 __mdd_ref_del(env, mdd_obj, handle, 1);
1068         }
1069
1070         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1071         la_copy->la_ctime = ma->ma_attr.la_ctime;
1072
1073         la_copy->la_valid = LA_CTIME;
1074         rc = mdd_attr_check_set_internal(env, mdd_obj, la_copy, handle, 0);
1075         if (rc)
1076                 GOTO(cleanup, rc);
1077
1078         rc = mdd_finish_unlink(env, mdd_obj, ma, handle);
1079
1080         EXIT;
1081 cleanup:
1082         mdd_write_unlock(env, mdd_obj);
1083         mdd_trans_stop(env, mdd, rc, handle);
1084         return rc;
1085 }
1086
1087 /* partial operation */
1088 static int mdd_oc_sanity_check(const struct lu_env *env,
1089                                struct mdd_object *obj,
1090                                struct md_attr *ma)
1091 {
1092         int rc;
1093         ENTRY;
1094
1095         switch (ma->ma_attr.la_mode & S_IFMT) {
1096         case S_IFREG:
1097         case S_IFDIR:
1098         case S_IFLNK:
1099         case S_IFCHR:
1100         case S_IFBLK:
1101         case S_IFIFO:
1102         case S_IFSOCK:
1103                 rc = 0;
1104                 break;
1105         default:
1106                 rc = -EINVAL;
1107                 break;
1108         }
1109         RETURN(rc);
1110 }
1111
1112 static int mdd_object_create(const struct lu_env *env,
1113                              struct md_object *obj,
1114                              const struct md_op_spec *spec,
1115                              struct md_attr *ma)
1116 {
1117
1118         struct mdd_device *mdd = mdo2mdd(obj);
1119         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1120         const struct lu_fid *pfid = spec->u.sp_pfid;
1121         struct thandle *handle;
1122         int rc;
1123         ENTRY;
1124
1125         mdd_txn_param_build(env, mdd, MDD_TXN_OBJECT_CREATE_OP);
1126         handle = mdd_trans_start(env, mdd);
1127         if (IS_ERR(handle))
1128                 RETURN(PTR_ERR(handle));
1129
1130         mdd_write_lock(env, mdd_obj);
1131         rc = mdd_oc_sanity_check(env, mdd_obj, ma);
1132         if (rc)
1133                 GOTO(unlock, rc);
1134
1135         rc = mdd_object_create_internal(env, NULL, mdd_obj, ma, handle);
1136         if (rc)
1137                 GOTO(unlock, rc);
1138
1139         if (spec->sp_cr_flags & MDS_CREATE_SLAVE_OBJ) {
1140                 /* If creating the slave object, set slave EA here. */
1141                 int lmv_size = spec->u.sp_ea.eadatalen;
1142                 struct lmv_stripe_md *lmv;
1143
1144                 lmv = (struct lmv_stripe_md *)spec->u.sp_ea.eadata;
1145                 LASSERT(lmv != NULL && lmv_size > 0);
1146
1147                 rc = __mdd_xattr_set(env, mdd_obj,
1148                                      mdd_buf_get_const(env, lmv, lmv_size),
1149                                      MDS_LMV_MD_NAME, 0, handle);
1150                 if (rc)
1151                         GOTO(unlock, rc);
1152
1153                 rc = mdd_attr_set_internal(env, mdd_obj, &ma->ma_attr,
1154                                            handle, 0);
1155         } else {
1156 #ifdef CONFIG_FS_POSIX_ACL
1157                 if (spec->sp_cr_flags & MDS_CREATE_RMT_ACL) {
1158                         struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1159
1160                         buf->lb_buf = (void *)spec->u.sp_ea.eadata;
1161                         buf->lb_len = spec->u.sp_ea.eadatalen;
1162                         if ((buf->lb_len > 0) && (buf->lb_buf != NULL)) {
1163                                 rc = __mdd_acl_init(env, mdd_obj, buf,
1164                                                     &ma->ma_attr.la_mode,
1165                                                     handle);
1166                                 if (rc)
1167                                         GOTO(unlock, rc);
1168                                 else
1169                                         ma->ma_attr.la_valid |= LA_MODE;
1170                         }
1171
1172                         pfid = spec->u.sp_ea.fid;
1173                 }
1174 #endif
1175                 rc = mdd_object_initialize(env, pfid, mdd_obj, ma, handle);
1176         }
1177         EXIT;
1178 unlock:
1179         mdd_write_unlock(env, mdd_obj);
1180         if (rc == 0)
1181                 rc = mdd_attr_get_internal_locked(env, mdd_obj, ma);
1182
1183         mdd_trans_stop(env, mdd, rc, handle);
1184         return rc;
1185 }
1186
1187 /* partial link */
1188 static int mdd_ref_add(const struct lu_env *env, struct md_object *obj,
1189                        const struct md_attr *ma)
1190 {
1191         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1192         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1193         struct mdd_device *mdd = mdo2mdd(obj);
1194         struct thandle *handle;
1195         int rc;
1196         ENTRY;
1197
1198         mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
1199         handle = mdd_trans_start(env, mdd);
1200         if (IS_ERR(handle))
1201                 RETURN(-ENOMEM);
1202
1203         mdd_write_lock(env, mdd_obj);
1204         rc = mdd_link_sanity_check(env, NULL, NULL, mdd_obj);
1205         if (rc == 0)
1206                 __mdd_ref_add(env, mdd_obj, handle);
1207         mdd_write_unlock(env, mdd_obj);
1208         if (rc == 0) {
1209                 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1210                 la_copy->la_ctime = ma->ma_attr.la_ctime;
1211
1212                 la_copy->la_valid = LA_CTIME;
1213                 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la_copy,
1214                                                         handle, 0);
1215         }
1216         mdd_trans_stop(env, mdd, 0, handle);
1217
1218         RETURN(rc);
1219 }
1220
1221 /*
1222  * do NOT or the MAY_*'s, you'll get the weakest
1223  */
1224 int accmode(const struct lu_env *env, struct lu_attr *la, int flags)
1225 {
1226         int res = 0;
1227
1228         /* Sadly, NFSD reopens a file repeatedly during operation, so the
1229          * "acc_mode = 0" allowance for newly-created files isn't honoured.
1230          * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1231          * owner can write to a file even if it is marked readonly to hide
1232          * its brokenness. (bug 5781) */
1233         if (flags & MDS_OPEN_OWNEROVERRIDE) {
1234                 struct md_ucred *uc = md_ucred(env);
1235
1236                 if ((uc == NULL) || (uc->mu_valid == UCRED_INIT) ||
1237                     (la->la_uid == uc->mu_fsuid))
1238                         return 0;
1239         }
1240
1241         if (flags & FMODE_READ)
1242                 res |= MAY_READ;
1243         if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1244                 res |= MAY_WRITE;
1245         if (flags & MDS_FMODE_EXEC)
1246                 res |= MAY_EXEC;
1247         return res;
1248 }
1249
1250 static int mdd_open_sanity_check(const struct lu_env *env,
1251                                  struct mdd_object *obj, int flag)
1252 {
1253         struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
1254         int mode, rc;
1255         ENTRY;
1256
1257         /* EEXIST check */
1258         if (mdd_is_dead_obj(obj))
1259                 RETURN(-ENOENT);
1260
1261         rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
1262         if (rc)
1263                RETURN(rc);
1264
1265         if (S_ISLNK(tmp_la->la_mode))
1266                 RETURN(-ELOOP);
1267
1268         mode = accmode(env, tmp_la, flag);
1269
1270         if (S_ISDIR(tmp_la->la_mode) && (mode & MAY_WRITE))
1271                 RETURN(-EISDIR);
1272
1273         if (!(flag & MDS_OPEN_CREATED)) {
1274                 rc = mdd_permission_internal(env, obj, tmp_la, mode);
1275                 if (rc)
1276                         RETURN(rc);
1277         }
1278
1279         if (S_ISFIFO(tmp_la->la_mode) || S_ISSOCK(tmp_la->la_mode) ||
1280             S_ISBLK(tmp_la->la_mode) || S_ISCHR(tmp_la->la_mode))
1281                 flag &= ~MDS_OPEN_TRUNC;
1282
1283         /* For writing append-only file must open it with append mode. */
1284         if (mdd_is_append(obj)) {
1285                 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1286                         RETURN(-EPERM);
1287                 if (flag & MDS_OPEN_TRUNC)
1288                         RETURN(-EPERM);
1289         }
1290
1291 #if 0
1292         /*
1293          * Now, flag -- O_NOATIME does not be packed by client.
1294          */
1295         if (flag & O_NOATIME) {
1296                 struct md_ucred *uc = md_ucred(env);
1297
1298                 if (uc && ((uc->mu_valid == UCRED_OLD) ||
1299                     (uc->mu_valid == UCRED_NEW)) &&
1300                     (uc->mu_fsuid != tmp_la->la_uid) &&
1301                     !mdd_capable(uc, CAP_FOWNER))
1302                         RETURN(-EPERM);
1303         }
1304 #endif
1305
1306         RETURN(0);
1307 }
1308
1309 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1310                     int flags)
1311 {
1312         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1313         int rc = 0;
1314
1315         mdd_write_lock(env, mdd_obj);
1316
1317         rc = mdd_open_sanity_check(env, mdd_obj, flags);
1318         if (rc == 0)
1319                 mdd_obj->mod_count++;
1320
1321         mdd_write_unlock(env, mdd_obj);
1322         return rc;
1323 }
1324
1325 /* return md_attr back,
1326  * if it is last unlink then return lov ea + llog cookie*/
1327 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
1328                     struct md_attr *ma)
1329 {
1330         int rc = 0;
1331         ENTRY;
1332
1333         if (S_ISREG(mdd_object_type(obj))) {
1334                 /* Return LOV & COOKIES unconditionally here. We clean evth up.
1335                  * Caller must be ready for that. */
1336                 rc = __mdd_lmm_get(env, obj, ma);
1337                 if ((ma->ma_valid & MA_LOV))
1338                         rc = mdd_unlink_log(env, mdo2mdd(&obj->mod_obj),
1339                                             obj, ma);
1340         }
1341         RETURN(rc);
1342 }
1343
1344 /*
1345  * No permission check is needed.
1346  */
1347 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1348                      struct md_attr *ma)
1349 {
1350         int rc;
1351         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1352         struct thandle    *handle;
1353         ENTRY;
1354
1355         rc = mdd_log_txn_param_build(env, obj, ma, MDD_TXN_UNLINK_OP);
1356         if (rc)
1357                 RETURN(rc);
1358         handle = mdd_trans_start(env, mdo2mdd(obj));
1359         if (IS_ERR(handle))
1360                 RETURN(PTR_ERR(handle));
1361
1362         mdd_write_lock(env, mdd_obj);
1363         /* release open count */
1364         mdd_obj->mod_count --;
1365
1366         rc = mdd_iattr_get(env, mdd_obj, ma);
1367         if (rc == 0 && mdd_obj->mod_count == 0 && ma->ma_attr.la_nlink == 0)
1368                 rc = mdd_object_kill(env, mdd_obj, ma);
1369         else
1370                 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
1371         
1372         mdd_write_unlock(env, mdd_obj);
1373         mdd_trans_stop(env, mdo2mdd(obj), rc, handle);
1374         RETURN(rc);
1375 }
1376
1377 /*
1378  * Permission check is done when open,
1379  * no need check again.
1380  */
1381 static int mdd_readpage_sanity_check(const struct lu_env *env,
1382                                      struct mdd_object *obj)
1383 {
1384         struct dt_object *next = mdd_object_child(obj);
1385         int rc;
1386         ENTRY;
1387
1388         if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1389                 rc = 0;
1390         else
1391                 rc = -ENOTDIR;
1392
1393         RETURN(rc);
1394 }
1395
1396 static int mdd_dir_page_build(const struct lu_env *env, int first,
1397                               void *area, int nob, struct dt_it_ops *iops,
1398                               struct dt_it *it, __u64 *start, __u64 *end,
1399                               struct lu_dirent **last)
1400 {
1401         struct lu_fid          *fid  = &mdd_env_info(env)->mti_fid2;
1402         struct mdd_thread_info *info = mdd_env_info(env);
1403         struct lu_fid_pack     *pack = &info->mti_pack;
1404         int                     result;
1405         struct lu_dirent       *ent;
1406
1407         if (first) {
1408                 memset(area, 0, sizeof (struct lu_dirpage));
1409                 area += sizeof (struct lu_dirpage);
1410                 nob  -= sizeof (struct lu_dirpage);
1411         }
1412
1413         LASSERT(nob > sizeof *ent);
1414
1415         ent  = area;
1416         result = 0;
1417         do {
1418                 char  *name;
1419                 int    len;
1420                 int    recsize;
1421                 __u64  hash;
1422
1423                 name = (char *)iops->key(env, it);
1424                 len  = iops->key_size(env, it);
1425
1426                 pack = (struct lu_fid_pack *)iops->rec(env, it);
1427                 result = fid_unpack(pack, fid);
1428                 if (result != 0)
1429                         break;
1430
1431                 recsize = (sizeof(*ent) + len + 7) & ~7;
1432                 hash = iops->store(env, it);
1433                 *end = hash;
1434
1435                 CDEBUG(D_INFO, "%p %p %d "DFID": "LPU64" (%d) \"%*.*s\"\n",
1436                        name, ent, nob, PFID(fid), hash, len, len, len, name);
1437
1438                 if (nob >= recsize) {
1439                         ent->lde_fid = *fid;
1440                         fid_cpu_to_le(&ent->lde_fid, &ent->lde_fid);
1441                         ent->lde_hash = hash;
1442                         ent->lde_namelen = cpu_to_le16(len);
1443                         ent->lde_reclen  = cpu_to_le16(recsize);
1444                         memcpy(ent->lde_name, name, len);
1445                         if (first && ent == area)
1446                                 *start = hash;
1447                         *last = ent;
1448                         ent = (void *)ent + recsize;
1449                         nob -= recsize;
1450                         result = iops->next(env, it);
1451                 } else {
1452                         /*
1453                          * record doesn't fit into page, enlarge previous one.
1454                          */
1455                         LASSERT(*last != NULL);
1456                         (*last)->lde_reclen =
1457                                 cpu_to_le16(le16_to_cpu((*last)->lde_reclen) +
1458                                             nob);
1459                         break;
1460                 }
1461         } while (result == 0);
1462
1463         return result;
1464 }
1465
1466 static int __mdd_readpage(const struct lu_env *env, struct mdd_object *obj,
1467                           const struct lu_rdpg *rdpg)
1468 {
1469         struct dt_it      *it;
1470         struct dt_object  *next = mdd_object_child(obj);
1471         struct dt_it_ops  *iops;
1472         struct page       *pg;
1473         struct lu_dirent  *last = NULL;
1474         int i;
1475         int rc;
1476         int nob;
1477         __u64 hash_start;
1478         __u64 hash_end = 0;
1479
1480         LASSERT(rdpg->rp_pages != NULL);
1481         LASSERT(next->do_index_ops != NULL);
1482
1483         if (rdpg->rp_count <= 0)
1484                 return -EFAULT;
1485
1486         /*
1487          * iterate through directory and fill pages from @rdpg
1488          */
1489         iops = &next->do_index_ops->dio_it;
1490         it = iops->init(env, next, 0, mdd_object_capa(env, obj));
1491         if (IS_ERR(it))
1492                 return PTR_ERR(it);
1493
1494         rc = iops->load(env, it, rdpg->rp_hash);
1495
1496         if (rc == 0)
1497                 /*
1498                  * Iterator didn't find record with exactly the key requested.
1499                  *
1500                  * It is currently either
1501                  *
1502                  *     - positioned above record with key less than
1503                  *     requested---skip it.
1504                  *
1505                  *     - or not positioned at all (is in IAM_IT_SKEWED
1506                  *     state)---position it on the next item.
1507                  */
1508                 rc = iops->next(env, it);
1509         else if (rc > 0)
1510                 rc = 0;
1511
1512         /*
1513          * At this point and across for-loop:
1514          *
1515          *  rc == 0 -> ok, proceed.
1516          *  rc >  0 -> end of directory.
1517          *  rc <  0 -> error.
1518          */
1519         for (i = 0, nob = rdpg->rp_count; rc == 0 && nob > 0;
1520              i++, nob -= CFS_PAGE_SIZE) {
1521                 LASSERT(i < rdpg->rp_npages);
1522                 pg = rdpg->rp_pages[i];
1523                 rc = mdd_dir_page_build(env, !i, cfs_kmap(pg),
1524                                         min_t(int, nob, CFS_PAGE_SIZE), iops,
1525                                         it, &hash_start, &hash_end, &last);
1526                 if (rc != 0 || i == rdpg->rp_npages - 1)
1527                         last->lde_reclen = 0;
1528                 cfs_kunmap(pg);
1529         }
1530         if (rc > 0) {
1531                 /*
1532                  * end of directory.
1533                  */
1534                 hash_end = DIR_END_OFF;
1535                 rc = 0;
1536         }
1537         if (rc == 0) {
1538                 struct lu_dirpage *dp;
1539
1540                 dp = cfs_kmap(rdpg->rp_pages[0]);
1541                 dp->ldp_hash_start = rdpg->rp_hash;
1542                 dp->ldp_hash_end   = hash_end;
1543                 if (i == 0)
1544                         /*
1545                          * No pages were processed, mark this.
1546                          */
1547                         dp->ldp_flags |= LDF_EMPTY;
1548                 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
1549                 cfs_kunmap(rdpg->rp_pages[0]);
1550         }
1551         iops->put(env, it);
1552         iops->fini(env, it);
1553
1554         return rc;
1555 }
1556
1557 static int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1558                         const struct lu_rdpg *rdpg)
1559 {
1560         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1561         int rc;
1562         ENTRY;
1563
1564         LASSERT(mdd_object_exists(mdd_obj));
1565
1566         mdd_read_lock(env, mdd_obj);
1567         rc = mdd_readpage_sanity_check(env, mdd_obj);
1568         if (rc)
1569                 GOTO(out_unlock, rc);
1570
1571         if (mdd_is_dead_obj(mdd_obj)) {
1572                 struct page *pg;
1573                 struct lu_dirpage *dp;
1574
1575                 /*
1576                  * According to POSIX, please do not return any entry to client:
1577                  * even dot and dotdot should not be returned.
1578                  */
1579                 CWARN("readdir from dead object: "DFID"\n",
1580                         PFID(mdd_object_fid(mdd_obj)));
1581
1582                 if (rdpg->rp_count <= 0)
1583                         GOTO(out_unlock, rc = -EFAULT);
1584                 LASSERT(rdpg->rp_pages != NULL);
1585
1586                 pg = rdpg->rp_pages[0];
1587                 dp = (struct lu_dirpage*)cfs_kmap(pg);
1588                 memset(dp, 0 , sizeof(struct lu_dirpage));
1589                 dp->ldp_hash_start = rdpg->rp_hash;
1590                 dp->ldp_hash_end   = DIR_END_OFF;
1591                 dp->ldp_flags |= LDF_EMPTY;
1592                 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
1593                 cfs_kunmap(pg);
1594                 GOTO(out_unlock, rc = 0);
1595         }
1596
1597         rc = __mdd_readpage(env, mdd_obj, rdpg);
1598
1599         EXIT;
1600 out_unlock:
1601         mdd_read_unlock(env, mdd_obj);
1602         return rc;
1603 }
1604
1605 struct md_object_operations mdd_obj_ops = {
1606         .moo_permission    = mdd_permission,
1607         .moo_attr_get      = mdd_attr_get,
1608         .moo_attr_set      = mdd_attr_set,
1609         .moo_xattr_get     = mdd_xattr_get,
1610         .moo_xattr_set     = mdd_xattr_set,
1611         .moo_xattr_list    = mdd_xattr_list,
1612         .moo_xattr_del     = mdd_xattr_del,
1613         .moo_object_create = mdd_object_create,
1614         .moo_ref_add       = mdd_ref_add,
1615         .moo_ref_del       = mdd_ref_del,
1616         .moo_open          = mdd_open,
1617         .moo_close         = mdd_close,
1618         .moo_readpage      = mdd_readpage,
1619         .moo_readlink      = mdd_readlink,
1620         .moo_capa_get      = mdd_capa_get
1621 };