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