Whamcloud - gitweb
1098ae46e14c93c23c378ce6eb172cc26c3301db
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_handler.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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * Copyright (c) 2011 Whamcloud, Inc.
37  */
38 /*
39  * This file is part of Lustre, http://www.lustre.org/
40  * Lustre is a trademark of Sun Microsystems, Inc.
41  *
42  * lustre/osd/osd_handler.c
43  *
44  * Top-level entry points into osd module
45  *
46  * Author: Nikita Danilov <nikita@clusterfs.com>
47  *         Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
48  */
49
50 #ifndef EXPORT_SYMTAB
51 # define EXPORT_SYMTAB
52 #endif
53 #define DEBUG_SUBSYSTEM S_MDS
54
55 #include <linux/module.h>
56
57 /* LUSTRE_VERSION_CODE */
58 #include <lustre_ver.h>
59 /* prerequisite for linux/xattr.h */
60 #include <linux/types.h>
61 /* prerequisite for linux/xattr.h */
62 #include <linux/fs.h>
63 /* XATTR_{REPLACE,CREATE} */
64 #include <linux/xattr.h>
65 /* simple_mkdir() */
66 #include <lvfs.h>
67
68 /*
69  * struct OBD_{ALLOC,FREE}*()
70  * OBD_FAIL_CHECK
71  */
72 #include <obd_support.h>
73 /* struct ptlrpc_thread */
74 #include <lustre_net.h>
75
76 /* fid_is_local() */
77 #include <lustre_fid.h>
78
79 #include "osd_internal.h"
80 #include "osd_igif.h"
81
82 /* llo_* api support */
83 #include <md_object.h>
84
85 #ifdef HAVE_LDISKFS_PDO
86 int ldiskfs_pdo = 1;
87 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
88                 "ldiskfs with parallel directory operations");
89 #else
90 int ldiskfs_pdo = 0;
91 #endif
92
93 static const char dot[] = ".";
94 static const char dotdot[] = "..";
95 static const char remote_obj_dir[] = "REM_OBJ_DIR";
96
97 struct osd_directory {
98         struct iam_container od_container;
99         struct iam_descr     od_descr;
100 };
101
102 struct osd_object {
103         struct dt_object       oo_dt;
104         /**
105          * Inode for file system object represented by this osd_object. This
106          * inode is pinned for the whole duration of lu_object life.
107          *
108          * Not modified concurrently (either setup early during object
109          * creation, or assigned by osd_object_create() under write lock).
110          */
111         struct inode          *oo_inode;
112         /**
113          * to protect index ops.
114          */
115         struct htree_lock_head *oo_hl_head;
116         cfs_rw_semaphore_t     oo_ext_idx_sem;
117         cfs_rw_semaphore_t     oo_sem;
118         struct osd_directory  *oo_dir;
119         /** protects inode attributes. */
120         cfs_spinlock_t         oo_guard;
121         /**
122          * Following two members are used to indicate the presence of dot and
123          * dotdot in the given directory. This is required for interop mode
124          * (b11826).
125          */
126         int                    oo_compat_dot_created;
127         int                    oo_compat_dotdot_created;
128
129         const struct lu_env   *oo_owner;
130 #ifdef CONFIG_LOCKDEP
131         struct lockdep_map     oo_dep_map;
132 #endif
133 };
134
135 static const struct lu_object_operations      osd_lu_obj_ops;
136 static const struct lu_device_operations      osd_lu_ops;
137 static       struct lu_context_key            osd_key;
138 static const struct dt_object_operations      osd_obj_ops;
139 static const struct dt_object_operations      osd_obj_ea_ops;
140 static const struct dt_body_operations        osd_body_ops;
141 static const struct dt_body_operations        osd_body_ops_new;
142 static const struct dt_index_operations       osd_index_iam_ops;
143 static const struct dt_index_operations       osd_index_ea_ops;
144
145 #define OSD_TRACK_DECLARES
146 #ifdef OSD_TRACK_DECLARES
147 #define OSD_DECLARE_OP(oh, op)   {                               \
148         LASSERT(oh->ot_handle == NULL);                          \
149         ((oh)->ot_declare_ ##op)++; }
150 #define OSD_EXEC_OP(handle, op)     {                            \
151         struct osd_thandle *oh;                                  \
152         oh = container_of0(handle, struct osd_thandle, ot_super);\
153         if (((oh)->ot_declare_ ##op) > 0) {                      \
154                 ((oh)->ot_declare_ ##op)--;                      \
155         }                                                        \
156         }
157 #else
158 #define OSD_DECLARE_OP(oh, op)
159 #define OSD_EXEC_OP(oh, op)
160 #endif
161
162 /* There are at most 10 uid/gids are affected in a transaction, and
163  * that's rename case:
164  * - 2 for source parent uid & gid;
165  * - 2 for source child uid & gid ('..' entry update when the child
166  *   is directory);
167  * - 2 for target parent uid & gid;
168  * - 2 for target child uid & gid (if the target child exists);
169  * - 2 for root uid & gid (last_rcvd, llog, etc);
170  *
171  * The 0 to (OSD_MAX_UGID_CNT - 1) bits of ot_id_type is for indicating
172  * the id type of each id in the ot_id_array.
173  */
174 #define OSD_MAX_UGID_CNT        10
175
176 struct osd_thandle {
177         struct thandle          ot_super;
178         handle_t               *ot_handle;
179         struct journal_callback ot_jcb;
180         cfs_list_t              ot_dcb_list;
181         /* Link to the device, for debugging. */
182         struct lu_ref_link     *ot_dev_link;
183         unsigned short          ot_credits;
184         unsigned short          ot_id_cnt;
185         unsigned short          ot_id_type;
186         uid_t                   ot_id_array[OSD_MAX_UGID_CNT];
187
188 #ifdef OSD_TRACK_DECLARES
189         unsigned char           ot_declare_attr_set;
190         unsigned char           ot_declare_punch;
191         unsigned char           ot_declare_xattr_set;
192         unsigned char           ot_declare_create;
193         unsigned char           ot_declare_destroy;
194         unsigned char           ot_declare_ref_add;
195         unsigned char           ot_declare_ref_del;
196         unsigned char           ot_declare_write;
197         unsigned char           ot_declare_insert;
198         unsigned char           ot_declare_delete;
199 #endif
200
201 #if OSD_THANDLE_STATS
202         /** time when this handle was allocated */
203         cfs_time_t oth_alloced;
204
205         /** time when this thanle was started */
206         cfs_time_t oth_started;
207 #endif
208 };
209
210 /**
211  * Basic transaction credit op
212  */
213 enum dt_txn_op {
214         DTO_INDEX_INSERT,
215         DTO_INDEX_DELETE,
216         DTO_INDEX_UPDATE,
217         DTO_OBJECT_CREATE,
218         DTO_OBJECT_DELETE,
219         DTO_ATTR_SET_BASE,
220         DTO_XATTR_SET,
221         DTO_LOG_REC, /**< XXX temporary: dt layer knows nothing about llog. */
222         DTO_WRITE_BASE,
223         DTO_WRITE_BLOCK,
224         DTO_ATTR_SET_CHOWN,
225
226         DTO_NR
227 };
228
229 /*
230  * Helpers.
231  */
232 static int lu_device_is_osd(const struct lu_device *d)
233 {
234         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osd_lu_ops);
235 }
236
237 static struct osd_device *osd_dt_dev(const struct dt_device *d)
238 {
239         LASSERT(lu_device_is_osd(&d->dd_lu_dev));
240         return container_of0(d, struct osd_device, od_dt_dev);
241 }
242
243 static struct osd_device *osd_dev(const struct lu_device *d)
244 {
245         LASSERT(lu_device_is_osd(d));
246         return osd_dt_dev(container_of0(d, struct dt_device, dd_lu_dev));
247 }
248
249 static struct osd_device *osd_obj2dev(const struct osd_object *o)
250 {
251         return osd_dev(o->oo_dt.do_lu.lo_dev);
252 }
253
254 static struct super_block *osd_sb(const struct osd_device *dev)
255 {
256         return dev->od_mount->lmi_mnt->mnt_sb;
257 }
258
259 static int osd_object_is_root(const struct osd_object *obj)
260 {
261         return osd_sb(osd_obj2dev(obj))->s_root->d_inode == obj->oo_inode;
262 }
263
264 static struct osd_object *osd_obj(const struct lu_object *o)
265 {
266         LASSERT(lu_device_is_osd(o->lo_dev));
267         return container_of0(o, struct osd_object, oo_dt.do_lu);
268 }
269
270 static struct osd_object *osd_dt_obj(const struct dt_object *d)
271 {
272         return osd_obj(&d->do_lu);
273 }
274
275 static struct lu_device *osd2lu_dev(struct osd_device *osd)
276 {
277         return &osd->od_dt_dev.dd_lu_dev;
278 }
279
280 static journal_t *osd_journal(const struct osd_device *dev)
281 {
282         return LDISKFS_SB(osd_sb(dev))->s_journal;
283 }
284
285 static int osd_has_index(const struct osd_object *obj)
286 {
287         return obj->oo_dt.do_index_ops != NULL;
288 }
289
290 static int osd_object_invariant(const struct lu_object *l)
291 {
292         return osd_invariant(osd_obj(l));
293 }
294
295 #ifdef HAVE_QUOTA_SUPPORT
296 static inline void
297 osd_push_ctxt(const struct lu_env *env, struct osd_ctxt *save)
298 {
299         struct md_ucred    *uc = md_ucred(env);
300         struct cred        *tc;
301
302         LASSERT(uc != NULL);
303
304         save->oc_uid = current_fsuid();
305         save->oc_gid = current_fsgid();
306         save->oc_cap = current_cap();
307         if ((tc = prepare_creds())) {
308                 tc->fsuid         = uc->mu_fsuid;
309                 tc->fsgid         = uc->mu_fsgid;
310                 commit_creds(tc);
311         }
312         /* XXX not suboptimal */
313         cfs_curproc_cap_unpack(uc->mu_cap);
314 }
315
316 static inline void
317 osd_pop_ctxt(struct osd_ctxt *save)
318 {
319         struct cred *tc;
320
321         if ((tc = prepare_creds())) {
322                 tc->fsuid         = save->oc_uid;
323                 tc->fsgid         = save->oc_gid;
324                 tc->cap_effective = save->oc_cap;
325                 commit_creds(tc);
326         }
327 }
328 #endif
329
330 static inline struct osd_thread_info *osd_oti_get(const struct lu_env *env)
331 {
332         return lu_context_key_get(&env->le_ctx, &osd_key);
333 }
334
335 /*
336  * Concurrency: doesn't matter
337  */
338 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
339 {
340         return osd_oti_get(env)->oti_r_locks > 0;
341 }
342
343 /*
344  * Concurrency: doesn't matter
345  */
346 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
347 {
348         struct osd_thread_info *oti = osd_oti_get(env);
349         return oti->oti_w_locks > 0 && o->oo_owner == env;
350 }
351
352 /*
353  * Concurrency: doesn't access mutable data
354  */
355 static int osd_root_get(const struct lu_env *env,
356                         struct dt_device *dev, struct lu_fid *f)
357 {
358         struct inode *inode;
359
360         inode = osd_sb(osd_dt_dev(dev))->s_root->d_inode;
361         LU_IGIF_BUILD(f, inode->i_ino, inode->i_generation);
362         return 0;
363 }
364
365 static inline int osd_qid_type(struct osd_thandle *oh, int i)
366 {
367         return (oh->ot_id_type & (1 << i)) ? GRPQUOTA : USRQUOTA;
368 }
369
370 static inline void osd_qid_set_type(struct osd_thandle *oh, int i, int type)
371 {
372         oh->ot_id_type |= ((type == GRPQUOTA) ? (1 << i) : 0);
373 }
374
375 static void osd_declare_qid(struct dt_object *dt, struct osd_thandle *oh,
376                             int type, uid_t id, struct inode *inode)
377 {
378 #ifdef CONFIG_QUOTA
379         int i, allocated = 0;
380         struct osd_object *obj;
381
382         LASSERT(dt != NULL);
383         LASSERT(oh != NULL);
384         LASSERTF(oh->ot_id_cnt <= OSD_MAX_UGID_CNT, "count=%u",
385                  oh->ot_id_cnt);
386
387         /* id entry is allocated in the quota file */
388         if (inode && inode->i_dquot[type] && inode->i_dquot[type]->dq_off)
389                 allocated = 1;
390
391         for (i = 0; i < oh->ot_id_cnt; i++) {
392                 if (oh->ot_id_array[i] == id && osd_qid_type(oh, i) == type)
393                         return;
394         }
395
396         if (unlikely(i >= OSD_MAX_UGID_CNT)) {
397                 CERROR("more than %d uid/gids for a transaction?\n", i);
398                 return;
399         }
400
401         oh->ot_id_array[i] = id;
402         osd_qid_set_type(oh, i, type);
403         oh->ot_id_cnt++;
404         obj = osd_dt_obj(dt);
405         oh->ot_credits += (allocated || id == 0) ?
406                 1 : LDISKFS_QUOTA_INIT_BLOCKS(osd_sb(osd_obj2dev(obj)));
407 #endif
408 }
409
410 /*
411  * OSD object methods.
412  */
413
414 /*
415  * Concurrency: no concurrent access is possible that early in object
416  * life-cycle.
417  */
418 static struct lu_object *osd_object_alloc(const struct lu_env *env,
419                                           const struct lu_object_header *hdr,
420                                           struct lu_device *d)
421 {
422         struct osd_object *mo;
423
424         OBD_ALLOC_PTR(mo);
425         if (mo != NULL) {
426                 struct lu_object *l;
427
428                 l = &mo->oo_dt.do_lu;
429                 dt_object_init(&mo->oo_dt, NULL, d);
430                 if (osd_dev(d)->od_iop_mode)
431                         mo->oo_dt.do_ops = &osd_obj_ea_ops;
432                 else
433                         mo->oo_dt.do_ops = &osd_obj_ops;
434
435                 l->lo_ops = &osd_lu_obj_ops;
436                 cfs_init_rwsem(&mo->oo_sem);
437                 cfs_init_rwsem(&mo->oo_ext_idx_sem);
438                 cfs_spin_lock_init(&mo->oo_guard);
439                 return l;
440         } else {
441                 return NULL;
442         }
443 }
444
445 /*
446  * retrieve object from backend ext fs.
447  **/
448 static struct inode *osd_iget(struct osd_thread_info *info,
449                               struct osd_device *dev,
450                               const struct osd_inode_id *id)
451 {
452         struct inode *inode = NULL;
453
454 #ifdef HAVE_EXT4_LDISKFS
455         inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
456         if (IS_ERR(inode))
457         /* Newer kernels return an error instead of a NULL pointer */
458                 inode = NULL;
459 #else
460         inode = iget(osd_sb(dev), id->oii_ino);
461 #endif
462         if (inode == NULL) {
463                 CERROR("no inode\n");
464                 inode = ERR_PTR(-EACCES);
465         } else if (id->oii_gen != OSD_OII_NOGEN &&
466                    inode->i_generation != id->oii_gen) {
467                 iput(inode);
468                 inode = ERR_PTR(-ESTALE);
469         } else if (inode->i_nlink == 0) {
470                 /* due to parallel readdir and unlink,
471                 * we can have dead inode here. */
472                 CWARN("stale inode\n");
473                 make_bad_inode(inode);
474                 iput(inode);
475                 inode = ERR_PTR(-ESTALE);
476         } else if (is_bad_inode(inode)) {
477                 CERROR("bad inode %lx\n",inode->i_ino);
478                 iput(inode);
479                 inode = ERR_PTR(-ENOENT);
480         } else {
481                 /* Do not update file c/mtime in ldiskfs.
482                  * NB: we don't have any lock to protect this because we don't
483                  * have reference on osd_object now, but contention with
484                  * another lookup + attr_set can't happen in the tiny window
485                  * between if (...) and set S_NOCMTIME. */
486                 if (!(inode->i_flags & S_NOCMTIME))
487                         inode->i_flags |= S_NOCMTIME;
488         }
489         return inode;
490 }
491
492 static int osd_fid_lookup(const struct lu_env *env,
493                           struct osd_object *obj, const struct lu_fid *fid)
494 {
495         struct osd_thread_info *info;
496         struct lu_device       *ldev = obj->oo_dt.do_lu.lo_dev;
497         struct osd_device      *dev;
498         struct osd_inode_id    *id;
499         struct inode           *inode;
500         int                     result;
501
502         LINVRNT(osd_invariant(obj));
503         LASSERT(obj->oo_inode == NULL);
504         LASSERTF(fid_is_sane(fid) || osd_fid_is_root(fid), DFID, PFID(fid));
505         /*
506          * This assertion checks that osd layer sees only local
507          * fids. Unfortunately it is somewhat expensive (does a
508          * cache-lookup). Disabling it for production/acceptance-testing.
509          */
510         LASSERT(1 || fid_is_local(env, ldev->ld_site, fid));
511
512         ENTRY;
513
514         info = osd_oti_get(env);
515         dev  = osd_dev(ldev);
516         id   = &info->oti_id;
517
518         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
519                 RETURN(-ENOENT);
520
521         result = osd_oi_lookup(info, osd_fid2oi(dev, fid), fid, id);
522         if (result != 0) {
523                 if (result == -ENOENT)
524                         result = 0;
525                 goto out;
526         }
527
528         inode = osd_iget(info, dev, id);
529         if (IS_ERR(inode)) {
530                 /*
531                  * If fid wasn't found in oi, inode-less object is
532                  * created, for which lu_object_exists() returns
533                  * false. This is used in a (frequent) case when
534                  * objects are created as locking anchors or
535                  * place holders for objects yet to be created.
536                  */
537                 result = PTR_ERR(inode);
538                 goto out;
539         }
540
541         obj->oo_inode = inode;
542         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
543         if (dev->od_iop_mode) {
544                 obj->oo_compat_dot_created = 1;
545                 obj->oo_compat_dotdot_created = 1;
546         }
547
548         if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
549                 goto out;
550
551         LASSERT(obj->oo_hl_head == NULL);
552         obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
553         if (obj->oo_hl_head == NULL) {
554                 obj->oo_inode = NULL;
555                 iput(inode);
556                 result = -ENOMEM;
557         }
558 out:
559         LINVRNT(osd_invariant(obj));
560
561         RETURN(result);
562 }
563
564 /*
565  * Concurrency: shouldn't matter.
566  */
567 static void osd_object_init0(struct osd_object *obj)
568 {
569         LASSERT(obj->oo_inode != NULL);
570         obj->oo_dt.do_body_ops = &osd_body_ops;
571         obj->oo_dt.do_lu.lo_header->loh_attr |=
572                 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
573 }
574
575 /*
576  * Concurrency: no concurrent access is possible that early in object
577  * life-cycle.
578  */
579 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
580                            const struct lu_object_conf *unused)
581 {
582         struct osd_object *obj = osd_obj(l);
583         int result;
584
585         LINVRNT(osd_invariant(obj));
586
587         result = osd_fid_lookup(env, obj, lu_object_fid(l));
588         obj->oo_dt.do_body_ops = &osd_body_ops_new;
589         if (result == 0) {
590                 if (obj->oo_inode != NULL)
591                         osd_object_init0(obj);
592         }
593         LINVRNT(osd_invariant(obj));
594         return result;
595 }
596
597 /*
598  * Concurrency: no concurrent access is possible that late in object
599  * life-cycle.
600  */
601 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
602 {
603         struct osd_object *obj = osd_obj(l);
604
605         LINVRNT(osd_invariant(obj));
606
607         dt_object_fini(&obj->oo_dt);
608         if (obj->oo_hl_head != NULL)
609                 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
610         OBD_FREE_PTR(obj);
611 }
612
613 /**
614  * IAM Iterator
615  */
616 static struct iam_path_descr *osd_it_ipd_get(const struct lu_env *env,
617                                              const struct iam_container *bag)
618 {
619         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
620                                            osd_oti_get(env)->oti_it_ipd);
621 }
622
623 static struct iam_path_descr *osd_idx_ipd_get(const struct lu_env *env,
624                                               const struct iam_container *bag)
625 {
626         return bag->ic_descr->id_ops->id_ipd_alloc(bag,
627                                            osd_oti_get(env)->oti_idx_ipd);
628 }
629
630 static void osd_ipd_put(const struct lu_env *env,
631                         const struct iam_container *bag,
632                         struct iam_path_descr *ipd)
633 {
634         bag->ic_descr->id_ops->id_ipd_free(ipd);
635 }
636
637 /*
638  * Concurrency: no concurrent access is possible that late in object
639  * life-cycle.
640  */
641 static void osd_index_fini(struct osd_object *o)
642 {
643         struct iam_container *bag;
644
645         if (o->oo_dir != NULL) {
646                 bag = &o->oo_dir->od_container;
647                 if (o->oo_inode != NULL) {
648                         if (bag->ic_object == o->oo_inode)
649                                 iam_container_fini(bag);
650                 }
651                 OBD_FREE_PTR(o->oo_dir);
652                 o->oo_dir = NULL;
653         }
654 }
655
656 /*
657  * Concurrency: no concurrent access is possible that late in object
658  * life-cycle (for all existing callers, that is. New callers have to provide
659  * their own locking.)
660  */
661 static int osd_inode_unlinked(const struct inode *inode)
662 {
663         return inode->i_nlink == 0;
664 }
665
666 enum {
667         OSD_TXN_OI_DELETE_CREDITS    = 20,
668         OSD_TXN_INODE_DELETE_CREDITS = 20
669 };
670
671 /*
672  * Journal
673  */
674
675 #if OSD_THANDLE_STATS
676 /**
677  * Set time when the handle is allocated
678  */
679 static void osd_th_alloced(struct osd_thandle *oth)
680 {
681         oth->oth_alloced = cfs_time_current();
682 }
683
684 /**
685  * Set time when the handle started
686  */
687 static void osd_th_started(struct osd_thandle *oth)
688 {
689         oth->oth_started = cfs_time_current();
690 }
691
692 /**
693  * Helper function to convert time interval to microseconds packed in
694  * long int (default time units for the counter in "stats" initialized
695  * by lu_time_init() )
696  */
697 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
698 {
699         struct timeval val;
700
701         cfs_duration_usec(cfs_time_sub(end, start), &val);
702         return val.tv_sec * 1000000 + val.tv_usec;
703 }
704
705 /**
706  * Check whether the we deal with this handle for too long.
707  */
708 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
709                                 cfs_time_t alloced, cfs_time_t started,
710                                 cfs_time_t closed)
711 {
712         cfs_time_t now = cfs_time_current();
713
714         LASSERT(dev != NULL);
715
716         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
717                             interval_to_usec(alloced, started));
718         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
719                             interval_to_usec(started, closed));
720         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
721                             interval_to_usec(closed, now));
722
723         if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
724                 CWARN("transaction handle %p was open for too long: "
725                       "now "CFS_TIME_T" ,"
726                       "alloced "CFS_TIME_T" ,"
727                       "started "CFS_TIME_T" ,"
728                       "closed "CFS_TIME_T"\n",
729                       oth, now, alloced, started, closed);
730                 libcfs_debug_dumpstack(NULL);
731         }
732 }
733
734 #define OSD_CHECK_SLOW_TH(oth, dev, expr)                               \
735 {                                                                       \
736         cfs_time_t __closed = cfs_time_current();                       \
737         cfs_time_t __alloced = oth->oth_alloced;                        \
738         cfs_time_t __started = oth->oth_started;                        \
739                                                                         \
740         expr;                                                           \
741         __osd_th_check_slow(oth, dev, __alloced, __started, __closed);  \
742 }
743
744 #else /* OSD_THANDLE_STATS */
745
746 #define osd_th_alloced(h)                  do {} while(0)
747 #define osd_th_started(h)                  do {} while(0)
748 #define OSD_CHECK_SLOW_TH(oth, dev, expr)  expr
749
750 #endif /* OSD_THANDLE_STATS */
751
752 /*
753  * Concurrency: doesn't access mutable data.
754  */
755 static int osd_param_is_sane(const struct osd_device *dev,
756                              const struct thandle *th)
757 {
758         struct osd_thandle *oh;
759         oh = container_of0(th, struct osd_thandle, ot_super);
760         return oh->ot_credits <= osd_journal(dev)->j_max_transaction_buffers;
761 }
762
763 /*
764  * Concurrency: shouldn't matter.
765  */
766 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
767 static void osd_trans_commit_cb(struct super_block *sb,
768                                 struct journal_callback *jcb, int error)
769 #else
770 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
771 #endif
772 {
773         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
774         struct thandle     *th  = &oh->ot_super;
775         struct lu_device   *lud = &th->th_dev->dd_lu_dev;
776         struct dt_txn_commit_cb *dcb, *tmp;
777
778         LASSERT(oh->ot_handle == NULL);
779
780         if (error)
781                 CERROR("transaction @0x%p commit error: %d\n", th, error);
782
783         dt_txn_hook_commit(th);
784
785         /* call per-transaction callbacks if any */
786         cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage)
787                 dcb->dcb_func(NULL, th, dcb, error);
788
789         lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
790         lu_device_put(lud);
791         th->th_dev = NULL;
792
793         lu_context_exit(&th->th_ctx);
794         lu_context_fini(&th->th_ctx);
795         OBD_FREE_PTR(oh);
796 }
797
798 static struct thandle *osd_trans_create(const struct lu_env *env,
799                                         struct dt_device *d)
800 {
801         struct osd_thread_info *oti = osd_oti_get(env);
802         struct osd_thandle     *oh;
803         struct thandle         *th;
804         ENTRY;
805
806         th = ERR_PTR(-ENOMEM);
807         OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
808         if (oh != NULL) {
809                 th = &oh->ot_super;
810                 th->th_dev = d;
811                 th->th_result = 0;
812                 th->th_tags = LCT_TX_HANDLE;
813                 oh->ot_credits = 0;
814                 oti->oti_dev = osd_dt_dev(d);
815                 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
816                 osd_th_alloced(oh);
817         }
818         RETURN(th);
819 }
820
821 /*
822  * Concurrency: shouldn't matter.
823  */
824 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
825                     struct thandle *th)
826 {
827         struct osd_thread_info *oti = osd_oti_get(env);
828         struct osd_device  *dev = osd_dt_dev(d);
829         handle_t           *jh;
830         struct osd_thandle *oh;
831         int rc;
832
833         ENTRY;
834
835         LASSERT(current->journal_info == NULL);
836
837         oh = container_of0(th, struct osd_thandle, ot_super);
838         LASSERT(oh != NULL);
839         LASSERT(oh->ot_handle == NULL);
840
841         rc = dt_txn_hook_start(env, d, th);
842         if (rc != 0)
843                 GOTO(out, rc);
844
845         if (!osd_param_is_sane(dev, th)) {
846                 CWARN("%s: too many transaction credits (%d > %d)\n",
847                       d->dd_lu_dev.ld_obd->obd_name, oh->ot_credits,
848                       osd_journal(dev)->j_max_transaction_buffers);
849                 /* XXX Limit the credits to 'max_transaction_buffers', and
850                  *     let the underlying filesystem to catch the error if
851                  *     we really need so many credits.
852                  *
853                  *     This should be removed when we can calculate the
854                  *     credits precisely. */
855                 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
856 #ifdef OSD_TRACK_DECLARES
857                 CERROR("  attr_set: %d, punch: %d, xattr_set: %d,\n",
858                        oh->ot_declare_attr_set, oh->ot_declare_punch,
859                        oh->ot_declare_xattr_set);
860                 CERROR("  create: %d, ref_add: %d, ref_del: %d, write: %d\n",
861                        oh->ot_declare_create, oh->ot_declare_ref_add,
862                        oh->ot_declare_ref_del, oh->ot_declare_write);
863                 CERROR("  insert: %d, delete: %d, destroy: %d\n",
864                        oh->ot_declare_insert, oh->ot_declare_delete,
865                        oh->ot_declare_destroy);
866 #endif
867         }
868
869         /*
870          * XXX temporary stuff. Some abstraction layer should
871          * be used.
872          */
873         jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
874         osd_th_started(oh);
875         if (!IS_ERR(jh)) {
876                 oh->ot_handle = jh;
877                 LASSERT(oti->oti_txns == 0);
878                 lu_context_init(&th->th_ctx, th->th_tags);
879                 lu_context_enter(&th->th_ctx);
880
881                 lu_device_get(&d->dd_lu_dev);
882                 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
883                                              "osd-tx", th);
884
885                 /*
886                  * XXX: current rule is that we first start tx,
887                  *      then lock object(s), but we can't use
888                  *      this rule for data (due to locking specifics
889                  *      in ldiskfs). also in long-term we'd like to
890                  *      use usually-used (locks;tx) ordering. so,
891                  *      UGLY thing is that we'll use one ordering for
892                  *      data (ofd) and reverse ordering for metadata
893                  *      (mdd). then at some point we'll fix the latter
894                  */
895                 if (lu_device_is_md(&d->dd_lu_dev)) {
896                         LASSERT(oti->oti_r_locks == 0);
897                         LASSERT(oti->oti_w_locks == 0);
898                 }
899
900                 oti->oti_txns++;
901                 rc = 0;
902         } else {
903                 rc = PTR_ERR(jh);
904         }
905 out:
906         RETURN(rc);
907 }
908
909 /*
910  * Concurrency: shouldn't matter.
911  */
912 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
913 {
914         int                     rc = 0;
915         struct osd_thandle     *oh;
916         struct osd_thread_info *oti = osd_oti_get(env);
917
918         ENTRY;
919
920         oh = container_of0(th, struct osd_thandle, ot_super);
921
922         if (oh->ot_handle != NULL) {
923                 handle_t *hdl = oh->ot_handle;
924
925                 hdl->h_sync = th->th_sync;
926
927                 /*
928                  * add commit callback
929                  * notice we don't do this in osd_trans_start()
930                  * as underlying transaction can change during truncate
931                  */
932                 osd_journal_callback_set(hdl, osd_trans_commit_cb,
933                                          &oh->ot_jcb);
934
935                 LASSERT(oti->oti_txns == 1);
936                 oti->oti_txns--;
937                 /*
938                  * XXX: current rule is that we first start tx,
939                  *      then lock object(s), but we can't use
940                  *      this rule for data (due to locking specifics
941                  *      in ldiskfs). also in long-term we'd like to
942                  *      use usually-used (locks;tx) ordering. so,
943                  *      UGLY thing is that we'll use one ordering for
944                  *      data (ofd) and reverse ordering for metadata
945                  *      (mdd). then at some point we'll fix the latter
946                  */
947                 if (lu_device_is_md(&th->th_dev->dd_lu_dev)) {
948                         LASSERT(oti->oti_r_locks == 0);
949                         LASSERT(oti->oti_w_locks == 0);
950                 }
951                 rc = dt_txn_hook_stop(env, th);
952                 if (rc != 0)
953                         CERROR("Failure in transaction hook: %d\n", rc);
954                 oh->ot_handle = NULL;
955                 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
956                                   rc = ldiskfs_journal_stop(hdl));
957                 if (rc != 0)
958                         CERROR("Failure to stop transaction: %d\n", rc);
959         } else {
960                 OBD_FREE_PTR(oh);
961         }
962
963         RETURN(rc);
964 }
965
966 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
967 {
968         struct osd_thandle *oh = container_of0(th, struct osd_thandle,
969                                                ot_super);
970
971         cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
972
973         return 0;
974 }
975
976 /*
977  * Called just before object is freed. Releases all resources except for
978  * object itself (that is released by osd_object_free()).
979  *
980  * Concurrency: no concurrent access is possible that late in object
981  * life-cycle.
982  */
983 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
984 {
985         struct osd_object *obj   = osd_obj(l);
986         struct inode      *inode = obj->oo_inode;
987
988         LINVRNT(osd_invariant(obj));
989
990         /*
991          * If object is unlinked remove fid->ino mapping from object index.
992          */
993
994         osd_index_fini(obj);
995         if (inode != NULL) {
996                 iput(inode);
997                 obj->oo_inode = NULL;
998         }
999 }
1000
1001 /*
1002  * Concurrency: ->loo_object_release() is called under site spin-lock.
1003  */
1004 static void osd_object_release(const struct lu_env *env,
1005                                struct lu_object *l)
1006 {
1007 }
1008
1009 /*
1010  * Concurrency: shouldn't matter.
1011  */
1012 static int osd_object_print(const struct lu_env *env, void *cookie,
1013                             lu_printer_t p, const struct lu_object *l)
1014 {
1015         struct osd_object *o = osd_obj(l);
1016         struct iam_descr  *d;
1017
1018         if (o->oo_dir != NULL)
1019                 d = o->oo_dir->od_container.ic_descr;
1020         else
1021                 d = NULL;
1022         return (*p)(env, cookie, LUSTRE_OSD_NAME"-object@%p(i:%p:%lu/%u)[%s]",
1023                     o, o->oo_inode,
1024                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
1025                     o->oo_inode ? o->oo_inode->i_generation : 0,
1026                     d ? d->id_ops->id_name : "plain");
1027 }
1028
1029 /*
1030  * Concurrency: shouldn't matter.
1031  */
1032 int osd_statfs(const struct lu_env *env, struct dt_device *d,
1033                cfs_kstatfs_t *sfs)
1034 {
1035         struct osd_device *osd = osd_dt_dev(d);
1036         struct super_block *sb = osd_sb(osd);
1037         int result = 0;
1038
1039         cfs_spin_lock(&osd->od_osfs_lock);
1040         /* cache 1 second */
1041         if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
1042                 result = ll_do_statfs(sb, &osd->od_kstatfs);
1043                 if (likely(result == 0)) /* N.B. statfs can't really fail */
1044                         osd->od_osfs_age = cfs_time_current_64();
1045         }
1046
1047         if (likely(result == 0))
1048                 *sfs = osd->od_kstatfs;
1049         cfs_spin_unlock(&osd->od_osfs_lock);
1050
1051         return result;
1052 }
1053
1054 /*
1055  * Concurrency: doesn't access mutable data.
1056  */
1057 static void osd_conf_get(const struct lu_env *env,
1058                          const struct dt_device *dev,
1059                          struct dt_device_param *param)
1060 {
1061         struct super_block *sb = osd_sb(osd_dt_dev(dev));
1062
1063         /*
1064          * XXX should be taken from not-yet-existing fs abstraction layer.
1065          */
1066         param->ddp_max_name_len = LDISKFS_NAME_LEN;
1067         param->ddp_max_nlink    = LDISKFS_LINK_MAX;
1068         param->ddp_block_shift  = osd_sb(osd_dt_dev(dev))->s_blocksize_bits;
1069         param->ddp_mntopts      = 0;
1070         if (test_opt(sb, XATTR_USER))
1071                 param->ddp_mntopts |= MNTOPT_USERXATTR;
1072         if (test_opt(sb, POSIX_ACL))
1073                 param->ddp_mntopts |= MNTOPT_ACL;
1074
1075 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1076         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1077                 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1078         else
1079 #endif
1080                 param->ddp_max_ea_size = sb->s_blocksize;
1081
1082 }
1083
1084 /**
1085  * Helper function to get and fill the buffer with input values.
1086  */
1087 static struct lu_buf *osd_buf_get(const struct lu_env *env, void *area, ssize_t len)
1088 {
1089         struct lu_buf *buf;
1090
1091         buf = &osd_oti_get(env)->oti_buf;
1092         buf->lb_buf = area;
1093         buf->lb_len = len;
1094         return buf;
1095 }
1096
1097 /*
1098  * Concurrency: shouldn't matter.
1099  */
1100 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1101 {
1102         CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_NAME);
1103         return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1104 }
1105
1106 /**
1107  * Start commit for OSD device.
1108  *
1109  * An implementation of dt_commit_async method for OSD device.
1110  * Asychronously starts underlayng fs sync and thereby a transaction
1111  * commit.
1112  *
1113  * \param env environment
1114  * \param d dt device
1115  *
1116  * \see dt_device_operations
1117  */
1118 static int osd_commit_async(const struct lu_env *env,
1119                             struct dt_device *d)
1120 {
1121         struct super_block *s = osd_sb(osd_dt_dev(d));
1122         ENTRY;
1123
1124         CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_NAME);
1125         RETURN(s->s_op->sync_fs(s, 0));
1126 }
1127
1128 /*
1129  * Concurrency: shouldn't matter.
1130  */
1131
1132 static void osd_ro(const struct lu_env *env, struct dt_device *d)
1133 {
1134         struct super_block *sb = osd_sb(osd_dt_dev(d));
1135         ENTRY;
1136
1137         CERROR("*** setting device %s read-only ***\n", LUSTRE_OSD_NAME);
1138
1139         __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1140         EXIT;
1141 }
1142
1143 /*
1144  * Concurrency: serialization provided by callers.
1145  */
1146 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1147                               int mode, unsigned long timeout, __u32 alg,
1148                               struct lustre_capa_key *keys)
1149 {
1150         struct osd_device *dev = osd_dt_dev(d);
1151         ENTRY;
1152
1153         dev->od_fl_capa = mode;
1154         dev->od_capa_timeout = timeout;
1155         dev->od_capa_alg = alg;
1156         dev->od_capa_keys = keys;
1157         RETURN(0);
1158 }
1159
1160 /**
1161  * Concurrency: serialization provided by callers.
1162  */
1163 static void osd_init_quota_ctxt(const struct lu_env *env, struct dt_device *d,
1164                                struct dt_quota_ctxt *ctxt, void *data)
1165 {
1166         struct obd_device *obd = (void *)ctxt;
1167         struct vfsmount *mnt = (struct vfsmount *)data;
1168         ENTRY;
1169
1170         obd->u.obt.obt_sb = mnt->mnt_root->d_inode->i_sb;
1171         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1172         obd->obd_lvfs_ctxt.pwdmnt = mnt;
1173         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
1174         obd->obd_lvfs_ctxt.fs = get_ds();
1175
1176         EXIT;
1177 }
1178
1179 /**
1180  * Note: we do not count into QUOTA here.
1181  * If we mount with --data_journal we may need more.
1182  */
1183 static const int osd_dto_credits_noquota[DTO_NR] = {
1184         /**
1185          * Insert/Delete.
1186          * INDEX_EXTRA_TRANS_BLOCKS(8) +
1187          * SINGLEDATA_TRANS_BLOCKS(8)
1188          * XXX Note: maybe iam need more, since iam have more level than
1189          *           EXT3 htree.
1190          */
1191         [DTO_INDEX_INSERT]  = 16,
1192         [DTO_INDEX_DELETE]  = 16,
1193         /**
1194          * Unused now
1195          */
1196         [DTO_INDEX_UPDATE]  = 16,
1197         /**
1198          * Create a object. The same as create object in EXT3.
1199          * DATA_TRANS_BLOCKS(14) +
1200          * INDEX_EXTRA_BLOCKS(8) +
1201          * 3(inode bits, groups, GDT)
1202          */
1203         [DTO_OBJECT_CREATE] = 25,
1204         /**
1205          * XXX: real credits to be fixed
1206          */
1207         [DTO_OBJECT_DELETE] = 25,
1208         /**
1209          * Attr set credits (inode)
1210          */
1211         [DTO_ATTR_SET_BASE] = 1,
1212         /**
1213          * Xattr set. The same as xattr of EXT3.
1214          * DATA_TRANS_BLOCKS(14)
1215          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1216          * are also counted in. Do not know why?
1217          */
1218         [DTO_XATTR_SET]     = 14,
1219         [DTO_LOG_REC]       = 14,
1220         /**
1221          * credits for inode change during write.
1222          */
1223         [DTO_WRITE_BASE]    = 3,
1224         /**
1225          * credits for single block write.
1226          */
1227         [DTO_WRITE_BLOCK]   = 14,
1228         /**
1229          * Attr set credits for chown.
1230          * This is extra credits for setattr, and it is null without quota
1231          */
1232         [DTO_ATTR_SET_CHOWN]= 0
1233 };
1234
1235 static const struct dt_device_operations osd_dt_ops = {
1236         .dt_root_get       = osd_root_get,
1237         .dt_statfs         = osd_statfs,
1238         .dt_trans_create   = osd_trans_create,
1239         .dt_trans_start    = osd_trans_start,
1240         .dt_trans_stop     = osd_trans_stop,
1241         .dt_trans_cb_add   = osd_trans_cb_add,
1242         .dt_conf_get       = osd_conf_get,
1243         .dt_sync           = osd_sync,
1244         .dt_ro             = osd_ro,
1245         .dt_commit_async   = osd_commit_async,
1246         .dt_init_capa_ctxt = osd_init_capa_ctxt,
1247         .dt_init_quota_ctxt= osd_init_quota_ctxt,
1248 };
1249
1250 static void osd_object_read_lock(const struct lu_env *env,
1251                                  struct dt_object *dt, unsigned role)
1252 {
1253         struct osd_object *obj = osd_dt_obj(dt);
1254         struct osd_thread_info *oti = osd_oti_get(env);
1255
1256         LINVRNT(osd_invariant(obj));
1257
1258         LASSERT(obj->oo_owner != env);
1259         cfs_down_read_nested(&obj->oo_sem, role);
1260
1261         LASSERT(obj->oo_owner == NULL);
1262         oti->oti_r_locks++;
1263 }
1264
1265 static void osd_object_write_lock(const struct lu_env *env,
1266                                   struct dt_object *dt, unsigned role)
1267 {
1268         struct osd_object *obj = osd_dt_obj(dt);
1269         struct osd_thread_info *oti = osd_oti_get(env);
1270
1271         LINVRNT(osd_invariant(obj));
1272
1273         LASSERT(obj->oo_owner != env);
1274         cfs_down_write_nested(&obj->oo_sem, role);
1275
1276         LASSERT(obj->oo_owner == NULL);
1277         obj->oo_owner = env;
1278         oti->oti_w_locks++;
1279 }
1280
1281 static void osd_object_read_unlock(const struct lu_env *env,
1282                                    struct dt_object *dt)
1283 {
1284         struct osd_object *obj = osd_dt_obj(dt);
1285         struct osd_thread_info *oti = osd_oti_get(env);
1286
1287         LINVRNT(osd_invariant(obj));
1288
1289         LASSERT(oti->oti_r_locks > 0);
1290         oti->oti_r_locks--;
1291         cfs_up_read(&obj->oo_sem);
1292 }
1293
1294 static void osd_object_write_unlock(const struct lu_env *env,
1295                                     struct dt_object *dt)
1296 {
1297         struct osd_object *obj = osd_dt_obj(dt);
1298         struct osd_thread_info *oti = osd_oti_get(env);
1299
1300         LINVRNT(osd_invariant(obj));
1301
1302         LASSERT(obj->oo_owner == env);
1303         LASSERT(oti->oti_w_locks > 0);
1304         oti->oti_w_locks--;
1305         obj->oo_owner = NULL;
1306         cfs_up_write(&obj->oo_sem);
1307 }
1308
1309 static int osd_object_write_locked(const struct lu_env *env,
1310                                    struct dt_object *dt)
1311 {
1312         struct osd_object *obj = osd_dt_obj(dt);
1313
1314         LINVRNT(osd_invariant(obj));
1315
1316         return obj->oo_owner == env;
1317 }
1318
1319 static int capa_is_sane(const struct lu_env *env,
1320                         struct osd_device *dev,
1321                         struct lustre_capa *capa,
1322                         struct lustre_capa_key *keys)
1323 {
1324         struct osd_thread_info *oti = osd_oti_get(env);
1325         struct lustre_capa *tcapa = &oti->oti_capa;
1326         struct obd_capa *oc;
1327         int i, rc = 0;
1328         ENTRY;
1329
1330         oc = capa_lookup(dev->od_capa_hash, capa, 0);
1331         if (oc) {
1332                 if (capa_is_expired(oc)) {
1333                         DEBUG_CAPA(D_ERROR, capa, "expired");
1334                         rc = -ESTALE;
1335                 }
1336                 capa_put(oc);
1337                 RETURN(rc);
1338         }
1339
1340         if (capa_is_expired_sec(capa)) {
1341                 DEBUG_CAPA(D_ERROR, capa, "expired");
1342                 RETURN(-ESTALE);
1343         }
1344
1345         cfs_spin_lock(&capa_lock);
1346         for (i = 0; i < 2; i++) {
1347                 if (keys[i].lk_keyid == capa->lc_keyid) {
1348                         oti->oti_capa_key = keys[i];
1349                         break;
1350                 }
1351         }
1352         cfs_spin_unlock(&capa_lock);
1353
1354         if (i == 2) {
1355                 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1356                 RETURN(-ESTALE);
1357         }
1358
1359         rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1360         if (rc)
1361                 RETURN(rc);
1362
1363         if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1364                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1365                 RETURN(-EACCES);
1366         }
1367
1368         oc = capa_add(dev->od_capa_hash, capa);
1369         capa_put(oc);
1370
1371         RETURN(0);
1372 }
1373
1374 static int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1375                            struct lustre_capa *capa, __u64 opc)
1376 {
1377         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1378         struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1379         struct md_capainfo *ci;
1380         int rc;
1381
1382         if (!dev->od_fl_capa)
1383                 return 0;
1384
1385         if (capa == BYPASS_CAPA)
1386                 return 0;
1387
1388         ci = md_capainfo(env);
1389         if (unlikely(!ci))
1390                 return 0;
1391
1392         if (ci->mc_auth == LC_ID_NONE)
1393                 return 0;
1394
1395         if (!capa) {
1396                 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1397                 return -EACCES;
1398         }
1399
1400         if (!lu_fid_eq(fid, &capa->lc_fid)) {
1401                 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1402                            PFID(fid));
1403                 return -EACCES;
1404         }
1405
1406         if (!capa_opc_supported(capa, opc)) {
1407                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1408                 return -EACCES;
1409         }
1410
1411         if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1412                 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1413                 return -EACCES;
1414         }
1415
1416         return 0;
1417 }
1418
1419 static struct timespec *osd_inode_time(const struct lu_env *env,
1420                                        struct inode *inode, __u64 seconds)
1421 {
1422         struct osd_thread_info *oti = osd_oti_get(env);
1423         struct timespec        *t   = &oti->oti_time;
1424
1425         t->tv_sec  = seconds;
1426         t->tv_nsec = 0;
1427         *t = timespec_trunc(*t, get_sb_time_gran(inode->i_sb));
1428         return t;
1429 }
1430
1431
1432 static void osd_inode_getattr(const struct lu_env *env,
1433                               struct inode *inode, struct lu_attr *attr)
1434 {
1435         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1436                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1437                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1438
1439         attr->la_atime      = LTIME_S(inode->i_atime);
1440         attr->la_mtime      = LTIME_S(inode->i_mtime);
1441         attr->la_ctime      = LTIME_S(inode->i_ctime);
1442         attr->la_mode       = inode->i_mode;
1443         attr->la_size       = i_size_read(inode);
1444         attr->la_blocks     = inode->i_blocks;
1445         attr->la_uid        = inode->i_uid;
1446         attr->la_gid        = inode->i_gid;
1447         attr->la_flags      = LDISKFS_I(inode)->i_flags;
1448         attr->la_nlink      = inode->i_nlink;
1449         attr->la_rdev       = inode->i_rdev;
1450         attr->la_blksize    = ll_inode_blksize(inode);
1451         attr->la_blkbits    = inode->i_blkbits;
1452 }
1453
1454 static int osd_attr_get(const struct lu_env *env,
1455                         struct dt_object *dt,
1456                         struct lu_attr *attr,
1457                         struct lustre_capa *capa)
1458 {
1459         struct osd_object *obj = osd_dt_obj(dt);
1460
1461         LASSERT(dt_object_exists(dt));
1462         LINVRNT(osd_invariant(obj));
1463
1464         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1465                 return -EACCES;
1466
1467         cfs_spin_lock(&obj->oo_guard);
1468         osd_inode_getattr(env, obj->oo_inode, attr);
1469         cfs_spin_unlock(&obj->oo_guard);
1470         return 0;
1471 }
1472
1473 static int osd_declare_attr_set(const struct lu_env *env,
1474                                 struct dt_object *dt,
1475                                 const struct lu_attr *attr,
1476                                 struct thandle *handle)
1477 {
1478         struct osd_thandle *oh;
1479         struct osd_object *obj;
1480
1481         LASSERT(dt != NULL);
1482         LASSERT(handle != NULL);
1483
1484         obj = osd_dt_obj(dt);
1485         LASSERT(osd_invariant(obj));
1486
1487         oh = container_of0(handle, struct osd_thandle, ot_super);
1488         LASSERT(oh->ot_handle == NULL);
1489
1490         OSD_DECLARE_OP(oh, attr_set);
1491         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
1492
1493         if (attr && attr->la_valid & LA_UID) {
1494                 if (obj->oo_inode)
1495                         osd_declare_qid(dt, oh, USRQUOTA, obj->oo_inode->i_uid,
1496                                         obj->oo_inode);
1497                 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1498         }
1499         if (attr && attr->la_valid & LA_GID) {
1500                 if (obj->oo_inode)
1501                         osd_declare_qid(dt, oh, GRPQUOTA, obj->oo_inode->i_gid,
1502                                         obj->oo_inode);
1503                 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1504         }
1505
1506         return 0;
1507 }
1508
1509 static int osd_inode_setattr(const struct lu_env *env,
1510                              struct inode *inode, const struct lu_attr *attr)
1511 {
1512         __u64 bits;
1513
1514         bits = attr->la_valid;
1515
1516         LASSERT(!(bits & LA_TYPE)); /* Huh? You want too much. */
1517
1518 #ifdef HAVE_QUOTA_SUPPORT
1519         if ((bits & LA_UID && attr->la_uid != inode->i_uid) ||
1520             (bits & LA_GID && attr->la_gid != inode->i_gid)) {
1521                 struct osd_ctxt *save = &osd_oti_get(env)->oti_ctxt;
1522                 struct iattr iattr;
1523                 int rc;
1524
1525                 iattr.ia_valid = 0;
1526                 if (bits & LA_UID)
1527                         iattr.ia_valid |= ATTR_UID;
1528                 if (bits & LA_GID)
1529                         iattr.ia_valid |= ATTR_GID;
1530                 iattr.ia_uid = attr->la_uid;
1531                 iattr.ia_gid = attr->la_gid;
1532                 osd_push_ctxt(env, save);
1533                 rc = ll_vfs_dq_transfer(inode, &iattr) ? -EDQUOT : 0;
1534                 osd_pop_ctxt(save);
1535                 if (rc != 0)
1536                         return rc;
1537         }
1538 #endif
1539
1540         if (bits & LA_ATIME)
1541                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
1542         if (bits & LA_CTIME)
1543                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
1544         if (bits & LA_MTIME)
1545                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
1546         if (bits & LA_SIZE) {
1547                 LDISKFS_I(inode)->i_disksize = attr->la_size;
1548                 i_size_write(inode, attr->la_size);
1549         }
1550
1551 #if 0
1552         /* OSD should not change "i_blocks" which is used by quota.
1553          * "i_blocks" should be changed by ldiskfs only. */
1554         if (bits & LA_BLOCKS)
1555                 inode->i_blocks = attr->la_blocks;
1556 #endif
1557         if (bits & LA_MODE)
1558                 inode->i_mode   = (inode->i_mode & S_IFMT) |
1559                         (attr->la_mode & ~S_IFMT);
1560         if (bits & LA_UID)
1561                 inode->i_uid    = attr->la_uid;
1562         if (bits & LA_GID)
1563                 inode->i_gid    = attr->la_gid;
1564         if (bits & LA_NLINK)
1565                 inode->i_nlink  = attr->la_nlink;
1566         if (bits & LA_RDEV)
1567                 inode->i_rdev   = attr->la_rdev;
1568
1569         if (bits & LA_FLAGS) {
1570                 /* always keep S_NOCMTIME */
1571                 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1572                                  S_NOCMTIME;
1573         }
1574         return 0;
1575 }
1576
1577 static int osd_attr_set(const struct lu_env *env,
1578                         struct dt_object *dt,
1579                         const struct lu_attr *attr,
1580                         struct thandle *handle,
1581                         struct lustre_capa *capa)
1582 {
1583         struct osd_object *obj = osd_dt_obj(dt);
1584         int rc;
1585
1586         LASSERT(handle != NULL);
1587         LASSERT(dt_object_exists(dt));
1588         LASSERT(osd_invariant(obj));
1589
1590         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1591                 return -EACCES;
1592
1593         OSD_EXEC_OP(handle, attr_set);
1594
1595         cfs_spin_lock(&obj->oo_guard);
1596         rc = osd_inode_setattr(env, obj->oo_inode, attr);
1597         cfs_spin_unlock(&obj->oo_guard);
1598
1599         if (!rc)
1600                 obj->oo_inode->i_sb->s_op->dirty_inode(obj->oo_inode);
1601         return rc;
1602 }
1603
1604 /*
1605  * Object creation.
1606  *
1607  * XXX temporary solution.
1608  */
1609 static int osd_create_pre(struct osd_thread_info *info, struct osd_object *obj,
1610                           struct lu_attr *attr, struct thandle *th)
1611 {
1612         return 0;
1613 }
1614
1615 static int osd_create_post(struct osd_thread_info *info, struct osd_object *obj,
1616                            struct lu_attr *attr, struct thandle *th)
1617 {
1618         osd_object_init0(obj);
1619         if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1620                 unlock_new_inode(obj->oo_inode);
1621         return 0;
1622 }
1623
1624 static struct dentry * osd_child_dentry_get(const struct lu_env *env,
1625                                             struct osd_object *obj,
1626                                             const char *name,
1627                                             const int namelen)
1628 {
1629         struct osd_thread_info *info   = osd_oti_get(env);
1630         struct dentry *child_dentry = &info->oti_child_dentry;
1631         struct dentry *obj_dentry = &info->oti_obj_dentry;
1632
1633         obj_dentry->d_inode = obj->oo_inode;
1634         obj_dentry->d_sb = osd_sb(osd_obj2dev(obj));
1635         obj_dentry->d_name.hash = 0;
1636
1637         child_dentry->d_name.hash = 0;
1638         child_dentry->d_parent = obj_dentry;
1639         child_dentry->d_name.name = name;
1640         child_dentry->d_name.len = namelen;
1641         return child_dentry;
1642 }
1643
1644
1645 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1646                       cfs_umode_t mode,
1647                       struct dt_allocation_hint *hint,
1648                       struct thandle *th)
1649 {
1650         int result;
1651         struct osd_device  *osd = osd_obj2dev(obj);
1652         struct osd_thandle *oth;
1653         struct dt_object   *parent;
1654         struct inode       *inode;
1655 #ifdef HAVE_QUOTA_SUPPORT
1656         struct osd_ctxt    *save = &info->oti_ctxt;
1657 #endif
1658
1659         LINVRNT(osd_invariant(obj));
1660         LASSERT(obj->oo_inode == NULL);
1661         LASSERT(obj->oo_hl_head == NULL);
1662
1663         if (S_ISDIR(mode) && ldiskfs_pdo) {
1664                 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1665                 if (obj->oo_hl_head == NULL)
1666                         return -ENOMEM;
1667         }
1668
1669         oth = container_of(th, struct osd_thandle, ot_super);
1670         LASSERT(oth->ot_handle->h_transaction != NULL);
1671
1672         if (hint && hint->dah_parent)
1673                 parent = hint->dah_parent;
1674         else
1675                 parent = osd->od_obj_area;
1676
1677         LASSERT(parent != NULL);
1678         LASSERT(osd_dt_obj(parent)->oo_inode->i_op != NULL);
1679
1680 #ifdef HAVE_QUOTA_SUPPORT
1681         osd_push_ctxt(info->oti_env, save);
1682 #endif
1683         inode = ldiskfs_create_inode(oth->ot_handle,
1684                                      osd_dt_obj(parent)->oo_inode, mode);
1685 #ifdef HAVE_QUOTA_SUPPORT
1686         osd_pop_ctxt(save);
1687 #endif
1688         if (!IS_ERR(inode)) {
1689                 /* Do not update file c/mtime in ldiskfs.
1690                  * NB: don't need any lock because no contention at this
1691                  * early stage */
1692                 inode->i_flags |= S_NOCMTIME;
1693                 obj->oo_inode = inode;
1694                 result = 0;
1695         } else {
1696                 if (obj->oo_hl_head != NULL) {
1697                         ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1698                         obj->oo_hl_head = NULL;
1699                 }
1700                 result = PTR_ERR(inode);
1701         }
1702         LINVRNT(osd_invariant(obj));
1703         return result;
1704 }
1705
1706 enum {
1707         OSD_NAME_LEN = 255
1708 };
1709
1710 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1711                      struct lu_attr *attr,
1712                      struct dt_allocation_hint *hint,
1713                      struct dt_object_format *dof,
1714                      struct thandle *th)
1715 {
1716         int result;
1717         struct osd_thandle *oth;
1718         struct osd_device *osd = osd_obj2dev(obj);
1719         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1720
1721         LASSERT(S_ISDIR(attr->la_mode));
1722
1723         oth = container_of(th, struct osd_thandle, ot_super);
1724         LASSERT(oth->ot_handle->h_transaction != NULL);
1725         result = osd_mkfile(info, obj, mode, hint, th);
1726         if (result == 0 && osd->od_iop_mode == 0) {
1727                 LASSERT(obj->oo_inode != NULL);
1728                 /*
1729                  * XXX uh-oh... call low-level iam function directly.
1730                  */
1731
1732                 result = iam_lvar_create(obj->oo_inode, OSD_NAME_LEN, 4,
1733                                          sizeof (struct osd_fid_pack),
1734                                          oth->ot_handle);
1735         }
1736         return result;
1737 }
1738
1739 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1740                         struct lu_attr *attr,
1741                         struct dt_allocation_hint *hint,
1742                         struct dt_object_format *dof,
1743                         struct thandle *th)
1744 {
1745         int result;
1746         struct osd_thandle *oth;
1747         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1748
1749         __u32 mode = (attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX));
1750
1751         LASSERT(S_ISREG(attr->la_mode));
1752
1753         oth = container_of(th, struct osd_thandle, ot_super);
1754         LASSERT(oth->ot_handle->h_transaction != NULL);
1755
1756         result = osd_mkfile(info, obj, mode, hint, th);
1757         if (result == 0) {
1758                 LASSERT(obj->oo_inode != NULL);
1759                 if (feat->dif_flags & DT_IND_VARKEY)
1760                         result = iam_lvar_create(obj->oo_inode,
1761                                                  feat->dif_keysize_max,
1762                                                  feat->dif_ptrsize,
1763                                                  feat->dif_recsize_max,
1764                                                  oth->ot_handle);
1765                 else
1766                         result = iam_lfix_create(obj->oo_inode,
1767                                                  feat->dif_keysize_max,
1768                                                  feat->dif_ptrsize,
1769                                                  feat->dif_recsize_max,
1770                                                  oth->ot_handle);
1771
1772         }
1773         return result;
1774 }
1775
1776 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1777                      struct lu_attr *attr,
1778                      struct dt_allocation_hint *hint,
1779                      struct dt_object_format *dof,
1780                      struct thandle *th)
1781 {
1782         LASSERT(S_ISREG(attr->la_mode));
1783         return osd_mkfile(info, obj, (attr->la_mode &
1784                                (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1785 }
1786
1787 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1788                      struct lu_attr *attr,
1789                      struct dt_allocation_hint *hint,
1790                      struct dt_object_format *dof,
1791                      struct thandle *th)
1792 {
1793         LASSERT(S_ISLNK(attr->la_mode));
1794         return osd_mkfile(info, obj, (attr->la_mode &
1795                               (S_IFMT | S_IRWXUGO | S_ISVTX)), hint, th);
1796 }
1797
1798 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1799                      struct lu_attr *attr,
1800                      struct dt_allocation_hint *hint,
1801                      struct dt_object_format *dof,
1802                      struct thandle *th)
1803 {
1804         cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IRWXUGO | S_ISVTX);
1805         int result;
1806
1807         LINVRNT(osd_invariant(obj));
1808         LASSERT(obj->oo_inode == NULL);
1809         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1810                 S_ISFIFO(mode) || S_ISSOCK(mode));
1811
1812         result = osd_mkfile(info, obj, mode, hint, th);
1813         if (result == 0) {
1814                 LASSERT(obj->oo_inode != NULL);
1815                 init_special_inode(obj->oo_inode, mode, attr->la_rdev);
1816         }
1817         LINVRNT(osd_invariant(obj));
1818         return result;
1819 }
1820
1821 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1822                               struct lu_attr *,
1823                               struct dt_allocation_hint *hint,
1824                               struct dt_object_format *dof,
1825                               struct thandle *);
1826
1827 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1828 {
1829         osd_obj_type_f result;
1830
1831         switch (type) {
1832         case DFT_DIR:
1833                 result = osd_mkdir;
1834                 break;
1835         case DFT_REGULAR:
1836                 result = osd_mkreg;
1837                 break;
1838         case DFT_SYM:
1839                 result = osd_mksym;
1840                 break;
1841         case DFT_NODE:
1842                 result = osd_mknod;
1843                 break;
1844         case DFT_INDEX:
1845                 result = osd_mk_index;
1846                 break;
1847
1848         default:
1849                 LBUG();
1850                 break;
1851         }
1852         return result;
1853 }
1854
1855
1856 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1857                         struct dt_object *parent, cfs_umode_t child_mode)
1858 {
1859         LASSERT(ah);
1860
1861         memset(ah, 0, sizeof(*ah));
1862         ah->dah_parent = parent;
1863         ah->dah_mode = child_mode;
1864 }
1865
1866 /**
1867  * Helper function for osd_object_create()
1868  *
1869  * \retval 0, on success
1870  */
1871 static int __osd_object_create(struct osd_thread_info *info,
1872                                struct osd_object *obj, struct lu_attr *attr,
1873                                struct dt_allocation_hint *hint,
1874                                struct dt_object_format *dof,
1875                                struct thandle *th)
1876 {
1877
1878         int result;
1879
1880         result = osd_create_pre(info, obj, attr, th);
1881         if (result == 0) {
1882                 result = osd_create_type_f(dof->dof_type)(info, obj,
1883                                            attr, hint, dof, th);
1884                 if (result == 0)
1885                         result = osd_create_post(info, obj, attr, th);
1886         }
1887         return result;
1888 }
1889
1890 /**
1891  * Helper function for osd_object_create()
1892  *
1893  * \retval 0, on success
1894  */
1895 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1896                            const struct lu_fid *fid, struct thandle *th)
1897 {
1898         struct osd_thread_info *info = osd_oti_get(env);
1899         struct osd_inode_id    *id   = &info->oti_id;
1900         struct osd_device      *osd  = osd_obj2dev(obj);
1901         struct md_ucred        *uc   = md_ucred(env);
1902
1903         LASSERT(obj->oo_inode != NULL);
1904         LASSERT(uc != NULL);
1905
1906         id->oii_ino = obj->oo_inode->i_ino;
1907         id->oii_gen = obj->oo_inode->i_generation;
1908
1909         return osd_oi_insert(info, osd_fid2oi(osd, fid), fid, id, th,
1910                              uc->mu_cap & CFS_CAP_SYS_RESOURCE_MASK);
1911 }
1912
1913 static int osd_declare_object_create(const struct lu_env *env,
1914                                      struct dt_object *dt,
1915                                      struct lu_attr *attr,
1916                                      struct dt_allocation_hint *hint,
1917                                      struct dt_object_format *dof,
1918                                      struct thandle *handle)
1919 {
1920         struct osd_thandle *oh;
1921
1922         LASSERT(handle != NULL);
1923
1924         oh = container_of0(handle, struct osd_thandle, ot_super);
1925         LASSERT(oh->ot_handle == NULL);
1926
1927         OSD_DECLARE_OP(oh, create);
1928         oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_CREATE];
1929         /* XXX: So far, only normal fid needs be inserted into the oi,
1930          *      things could be changed later. Revise following code then. */
1931         if (fid_is_norm(lu_object_fid(&dt->do_lu))) {
1932                 OSD_DECLARE_OP(oh, insert);
1933                 oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
1934         }
1935         /* If this is directory, then we expect . and .. to be inserted as
1936          * well. The one directory block always needs to be created for the
1937          * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
1938          * block), there is no danger of needing a tree for the first block.
1939          */
1940         if (attr && S_ISDIR(attr->la_mode)) {
1941                 OSD_DECLARE_OP(oh, insert);
1942                 OSD_DECLARE_OP(oh, insert);
1943                 oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BASE];
1944         }
1945
1946         if (attr) {
1947                 osd_declare_qid(dt, oh, USRQUOTA, attr->la_uid, NULL);
1948                 osd_declare_qid(dt, oh, GRPQUOTA, attr->la_gid, NULL);
1949         }
1950         return 0;
1951 }
1952
1953 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
1954                              struct lu_attr *attr,
1955                              struct dt_allocation_hint *hint,
1956                              struct dt_object_format *dof,
1957                              struct thandle *th)
1958 {
1959         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
1960         struct osd_object      *obj    = osd_dt_obj(dt);
1961         struct osd_thread_info *info   = osd_oti_get(env);
1962         int result;
1963
1964         ENTRY;
1965
1966         LINVRNT(osd_invariant(obj));
1967         LASSERT(!dt_object_exists(dt));
1968         LASSERT(osd_write_locked(env, obj));
1969         LASSERT(th != NULL);
1970
1971         OSD_EXEC_OP(th, create);
1972
1973         result = __osd_object_create(info, obj, attr, hint, dof, th);
1974         if (result == 0)
1975                 result = __osd_oi_insert(env, obj, fid, th);
1976
1977         LASSERT(ergo(result == 0, dt_object_exists(dt)));
1978         LASSERT(osd_invariant(obj));
1979         RETURN(result);
1980 }
1981
1982 /**
1983  * Called to destroy on-disk representation of the object
1984  *
1985  * Concurrency: must be locked
1986  */
1987 static int osd_declare_object_destroy(const struct lu_env *env,
1988                                       struct dt_object *dt,
1989                                       struct thandle *th)
1990 {
1991         struct osd_object      *obj = osd_dt_obj(dt);
1992         struct inode           *inode = obj->oo_inode;
1993         struct osd_thandle     *oh;
1994         ENTRY;
1995
1996         oh = container_of0(th, struct osd_thandle, ot_super);
1997         LASSERT(oh->ot_handle == NULL);
1998         LASSERT(inode);
1999         LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2000
2001         OSD_DECLARE_OP(oh, destroy);
2002         OSD_DECLARE_OP(oh, delete);
2003         oh->ot_credits += osd_dto_credits_noquota[DTO_OBJECT_DELETE];
2004         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
2005
2006         osd_declare_qid(dt, oh, USRQUOTA, inode->i_uid, inode);
2007         osd_declare_qid(dt, oh, GRPQUOTA, inode->i_gid, inode);
2008
2009         RETURN(0);
2010 }
2011
2012 static int osd_object_destroy(const struct lu_env *env,
2013                               struct dt_object *dt,
2014                               struct thandle *th)
2015 {
2016         const struct lu_fid    *fid = lu_object_fid(&dt->do_lu);
2017         struct osd_object      *obj = osd_dt_obj(dt);
2018         struct inode           *inode = obj->oo_inode;
2019         struct osd_device      *osd = osd_obj2dev(obj);
2020         struct osd_thandle     *oh;
2021         int                     result;
2022         ENTRY;
2023
2024         oh = container_of0(th, struct osd_thandle, ot_super);
2025         LASSERT(oh->ot_handle);
2026         LASSERT(inode);
2027
2028         if (S_ISDIR(inode->i_mode)) {
2029                 LASSERT(osd_inode_unlinked(inode) ||
2030                         inode->i_nlink == 1);
2031                 cfs_spin_lock(&obj->oo_guard);
2032                 inode->i_nlink = 0;
2033                 cfs_spin_unlock(&obj->oo_guard);
2034                 inode->i_sb->s_op->dirty_inode(inode);
2035         } else {
2036                 LASSERT(osd_inode_unlinked(inode));
2037         }
2038
2039         OSD_EXEC_OP(th, destroy);
2040
2041         result = osd_oi_delete(osd_oti_get(env),
2042                                osd_fid2oi(osd, fid), fid, th);
2043
2044         /* XXX: add to ext3 orphan list */
2045         /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2046
2047         /* not needed in the cache anymore */
2048         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2049
2050         RETURN(0);
2051 }
2052
2053 /**
2054  * Helper function for osd_xattr_set()
2055  */
2056 static int __osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2057                            const struct lu_buf *buf, const char *name, int fl)
2058 {
2059         struct osd_object      *obj      = osd_dt_obj(dt);
2060         struct inode           *inode    = obj->oo_inode;
2061         struct osd_thread_info *info     = osd_oti_get(env);
2062         struct dentry          *dentry   = &info->oti_child_dentry;
2063         int                     fs_flags = 0;
2064         int  rc;
2065
2066         LASSERT(dt_object_exists(dt));
2067         LASSERT(inode->i_op != NULL && inode->i_op->setxattr != NULL);
2068         LASSERT(osd_write_locked(env, obj));
2069
2070         if (fl & LU_XATTR_REPLACE)
2071                 fs_flags |= XATTR_REPLACE;
2072
2073         if (fl & LU_XATTR_CREATE)
2074                 fs_flags |= XATTR_CREATE;
2075
2076         dentry->d_inode = inode;
2077         rc = inode->i_op->setxattr(dentry, name, buf->lb_buf,
2078                                    buf->lb_len, fs_flags);
2079         return rc;
2080 }
2081
2082 /**
2083  * Put the fid into lustre_mdt_attrs, and then place the structure
2084  * inode's ea. This fid should not be altered during the life time
2085  * of the inode.
2086  *
2087  * \retval +ve, on success
2088  * \retval -ve, on error
2089  *
2090  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2091  */
2092 static int osd_ea_fid_set(const struct lu_env *env, struct dt_object *dt,
2093                           const struct lu_fid *fid)
2094 {
2095         struct osd_thread_info  *info      = osd_oti_get(env);
2096         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2097
2098         lustre_lma_init(mdt_attrs, fid);
2099         lustre_lma_swab(mdt_attrs);
2100         return __osd_xattr_set(env, dt,
2101                                osd_buf_get(env, mdt_attrs, sizeof *mdt_attrs),
2102                                XATTR_NAME_LMA, LU_XATTR_CREATE);
2103
2104 }
2105
2106 /**
2107  * Helper function to form igif
2108  */
2109 static inline void osd_igif_get(const struct lu_env *env, struct inode  *inode,
2110                                 struct lu_fid *fid)
2111 {
2112         LU_IGIF_BUILD(fid, inode->i_ino, inode->i_generation);
2113 }
2114
2115 /**
2116  * Helper function to pack the fid, ldiskfs stores fid in packed format.
2117  */
2118 void osd_fid_pack(struct osd_fid_pack *pack, const struct dt_rec *fid,
2119                   struct lu_fid *befider)
2120 {
2121         fid_cpu_to_be(befider, (struct lu_fid *)fid);
2122         memcpy(pack->fp_area, befider, sizeof(*befider));
2123         pack->fp_len =  sizeof(*befider) + 1;
2124 }
2125
2126 /**
2127  * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2128  * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2129  * To have compatilibility with 1.8 ldiskfs driver we need to have
2130  * magic number at start of fid data.
2131  * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2132  * its inmemory API.
2133  */
2134 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2135                                   const struct dt_rec *fid)
2136 {
2137         param->edp_magic = LDISKFS_LUFID_MAGIC;
2138         param->edp_len =  sizeof(struct lu_fid) + 1;
2139
2140         fid_cpu_to_be((struct lu_fid *)param->edp_data,
2141                       (struct lu_fid *)fid);
2142 }
2143
2144 int osd_fid_unpack(struct lu_fid *fid, const struct osd_fid_pack *pack)
2145 {
2146         int result;
2147
2148         result = 0;
2149         switch (pack->fp_len) {
2150         case sizeof *fid + 1:
2151                 memcpy(fid, pack->fp_area, sizeof *fid);
2152                 fid_be_to_cpu(fid, fid);
2153                 break;
2154         default:
2155                 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
2156                 result = -EIO;
2157         }
2158         return result;
2159 }
2160
2161 /**
2162  * Try to read the fid from inode ea into dt_rec, if return value
2163  * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2164  *
2165  * \param fid object fid.
2166  *
2167  * \retval 0 on success
2168  */
2169 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2170                           __u32 ino, struct lu_fid *fid)
2171 {
2172         struct osd_thread_info  *info      = osd_oti_get(env);
2173         struct lustre_mdt_attrs *mdt_attrs = &info->oti_mdt_attrs;
2174         struct lu_device        *ldev   = obj->oo_dt.do_lu.lo_dev;
2175         struct dentry           *dentry = &info->oti_child_dentry;
2176         struct osd_inode_id     *id     = &info->oti_id;
2177         struct osd_device       *dev;
2178         struct inode            *inode;
2179         int                      rc;
2180
2181         ENTRY;
2182         dev  = osd_dev(ldev);
2183
2184         id->oii_ino = ino;
2185         id->oii_gen = OSD_OII_NOGEN;
2186
2187         inode = osd_iget(info, dev, id);
2188         if (IS_ERR(inode)) {
2189                 rc = PTR_ERR(inode);
2190                 GOTO(out,rc);
2191         }
2192         dentry->d_inode = inode;
2193
2194         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2195         rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA, (void *)mdt_attrs,
2196                                    sizeof *mdt_attrs);
2197
2198         /* Check LMA compatibility */
2199         if (rc > 0 &&
2200             (mdt_attrs->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP))) {
2201                 CWARN("Inode %lx: Unsupported incompat LMA feature(s) %#x\n",
2202                       inode->i_ino, le32_to_cpu(mdt_attrs->lma_incompat) &
2203                       ~LMA_INCOMPAT_SUPP);
2204                 return -ENOSYS;
2205         }
2206
2207         if (rc > 0) {
2208                 lustre_lma_swab(mdt_attrs);
2209                 memcpy(fid, &mdt_attrs->lma_self_fid, sizeof(*fid));
2210                 rc = 0;
2211         } else if (rc == -ENODATA) {
2212                 osd_igif_get(env, inode, fid);
2213                 rc = 0;
2214         }
2215         iput(inode);
2216 out:
2217         RETURN(rc);
2218 }
2219
2220 /**
2221  * OSD layer object create function for interoperability mode (b11826).
2222  * This is mostly similar to osd_object_create(). Only difference being, fid is
2223  * inserted into inode ea here.
2224  *
2225  * \retval   0, on success
2226  * \retval -ve, on error
2227  */
2228 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2229                              struct lu_attr *attr,
2230                              struct dt_allocation_hint *hint,
2231                              struct dt_object_format *dof,
2232                              struct thandle *th)
2233 {
2234         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
2235         struct osd_object      *obj    = osd_dt_obj(dt);
2236         struct osd_thread_info *info   = osd_oti_get(env);
2237         int result;
2238
2239         ENTRY;
2240
2241         LASSERT(osd_invariant(obj));
2242         LASSERT(!dt_object_exists(dt));
2243         LASSERT(osd_write_locked(env, obj));
2244         LASSERT(th != NULL);
2245
2246         OSD_EXEC_OP(th, create);
2247
2248         result = __osd_object_create(info, obj, attr, hint, dof, th);
2249
2250         /* objects under osd root shld have igif fid, so dont add fid EA */
2251         if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL)
2252                 result = osd_ea_fid_set(env, dt, fid);
2253
2254         if (result == 0)
2255                 result = __osd_oi_insert(env, obj, fid, th);
2256
2257         LASSERT(ergo(result == 0, dt_object_exists(dt)));
2258         LINVRNT(osd_invariant(obj));
2259         RETURN(result);
2260 }
2261
2262 static int osd_declare_object_ref_add(const struct lu_env *env,
2263                                struct dt_object *dt,
2264                                struct thandle *handle)
2265 {
2266         struct osd_thandle *oh;
2267
2268         /* it's possible that object doesn't exist yet */
2269         LASSERT(handle != NULL);
2270
2271         oh = container_of0(handle, struct osd_thandle, ot_super);
2272         LASSERT(oh->ot_handle == NULL);
2273
2274         OSD_DECLARE_OP(oh, ref_add);
2275         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2276
2277         return 0;
2278 }
2279
2280 /*
2281  * Concurrency: @dt is write locked.
2282  */
2283 static int osd_object_ref_add(const struct lu_env *env,
2284                               struct dt_object *dt,
2285                               struct thandle *th)
2286 {
2287         struct osd_object *obj = osd_dt_obj(dt);
2288         struct inode *inode = obj->oo_inode;
2289
2290         LINVRNT(osd_invariant(obj));
2291         LASSERT(dt_object_exists(dt));
2292         LASSERT(osd_write_locked(env, obj));
2293         LASSERT(th != NULL);
2294
2295         OSD_EXEC_OP(th, ref_add);
2296
2297         /*
2298          * DIR_NLINK feature is set for compatibility reasons if:
2299          * 1) nlinks > LDISKFS_LINK_MAX, or
2300          * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2301          *
2302          * It is easier to always set this flag (rather than check and set),
2303          * since it has less overhead, and the superblock will be dirtied
2304          * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2305          * do not actually care whether this flag is set or not.
2306          */
2307         cfs_spin_lock(&obj->oo_guard);
2308         inode->i_nlink++;
2309         if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2310                 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2311                     inode->i_nlink == 2)
2312                         inode->i_nlink = 1;
2313         }
2314         LASSERT(inode->i_nlink < LDISKFS_LINK_MAX);
2315         cfs_spin_unlock(&obj->oo_guard);
2316         inode->i_sb->s_op->dirty_inode(inode);
2317         LINVRNT(osd_invariant(obj));
2318
2319         return 0;
2320 }
2321
2322 static int osd_declare_object_ref_del(const struct lu_env *env,
2323                                struct dt_object *dt,
2324                                struct thandle *handle)
2325 {
2326         struct osd_thandle *oh;
2327
2328         LASSERT(dt_object_exists(dt));
2329         LASSERT(handle != NULL);
2330
2331         oh = container_of0(handle, struct osd_thandle, ot_super);
2332         LASSERT(oh->ot_handle == NULL);
2333
2334         OSD_DECLARE_OP(oh, ref_del);
2335         oh->ot_credits += osd_dto_credits_noquota[DTO_ATTR_SET_BASE];
2336
2337         return 0;
2338 }
2339
2340 /*
2341  * Concurrency: @dt is write locked.
2342  */
2343 static int osd_object_ref_del(const struct lu_env *env,
2344                               struct dt_object *dt,
2345                               struct thandle *th)
2346 {
2347         struct osd_object *obj = osd_dt_obj(dt);
2348         struct inode *inode = obj->oo_inode;
2349
2350         LINVRNT(osd_invariant(obj));
2351         LASSERT(dt_object_exists(dt));
2352         LASSERT(osd_write_locked(env, obj));
2353         LASSERT(th != NULL);
2354
2355         OSD_EXEC_OP(th, ref_del);
2356
2357         cfs_spin_lock(&obj->oo_guard);
2358         LASSERT(inode->i_nlink > 0);
2359         inode->i_nlink--;
2360         /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2361          * then the nlink count is 1. Don't let it be set to 0 or the directory
2362          * inode will be deleted incorrectly. */
2363         if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2364                 inode->i_nlink++;
2365         cfs_spin_unlock(&obj->oo_guard);
2366         inode->i_sb->s_op->dirty_inode(inode);
2367         LINVRNT(osd_invariant(obj));
2368
2369         return 0;
2370 }
2371
2372 /*
2373  * Get the 64-bit version for an inode.
2374  */
2375 static int osd_object_version_get(const struct lu_env *env,
2376                                   struct dt_object *dt, dt_obj_version_t *ver)
2377 {
2378         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2379
2380         CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2381                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2382         *ver = LDISKFS_I(inode)->i_fs_version;
2383         return 0;
2384 }
2385
2386 /*
2387  * Concurrency: @dt is read locked.
2388  */
2389 static int osd_xattr_get(const struct lu_env *env,
2390                          struct dt_object *dt,
2391                          struct lu_buf *buf,
2392                          const char *name,
2393                          struct lustre_capa *capa)
2394 {
2395         struct osd_object      *obj    = osd_dt_obj(dt);
2396         struct inode           *inode  = obj->oo_inode;
2397         struct osd_thread_info *info   = osd_oti_get(env);
2398         struct dentry          *dentry = &info->oti_obj_dentry;
2399
2400         /* version get is not real XATTR but uses xattr API */
2401         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2402                 /* for version we are just using xattr API but change inode
2403                  * field instead */
2404                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2405                 osd_object_version_get(env, dt, buf->lb_buf);
2406                 return sizeof(dt_obj_version_t);
2407         }
2408
2409         LASSERT(dt_object_exists(dt));
2410         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2411         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2412
2413         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2414                 return -EACCES;
2415
2416         dentry->d_inode = inode;
2417         return inode->i_op->getxattr(dentry, name, buf->lb_buf, buf->lb_len);
2418 }
2419
2420
2421 static int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
2422                                  const struct lu_buf *buf, const char *name,
2423                                  int fl, struct thandle *handle)
2424 {
2425         struct osd_thandle *oh;
2426
2427         LASSERT(handle != NULL);
2428
2429         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2430                 /* no credits for version */
2431                 return 0;
2432         }
2433
2434         oh = container_of0(handle, struct osd_thandle, ot_super);
2435         LASSERT(oh->ot_handle == NULL);
2436
2437         OSD_DECLARE_OP(oh, xattr_set);
2438         oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2439
2440         return 0;
2441 }
2442
2443 /*
2444  * Set the 64-bit version for object
2445  */
2446 static void osd_object_version_set(const struct lu_env *env,
2447                                    struct dt_object *dt,
2448                                    dt_obj_version_t *new_version)
2449 {
2450         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2451
2452         CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2453                *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2454
2455         LDISKFS_I(inode)->i_fs_version = *new_version;
2456         /** Version is set after all inode operations are finished,
2457          *  so we should mark it dirty here */
2458         inode->i_sb->s_op->dirty_inode(inode);
2459 }
2460
2461 /*
2462  * Concurrency: @dt is write locked.
2463  */
2464 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2465                          const struct lu_buf *buf, const char *name, int fl,
2466                          struct thandle *handle, struct lustre_capa *capa)
2467 {
2468         LASSERT(handle != NULL);
2469
2470         /* version set is not real XATTR */
2471         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2472                 /* for version we are just using xattr API but change inode
2473                  * field instead */
2474                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2475                 osd_object_version_set(env, dt, buf->lb_buf);
2476                 return sizeof(dt_obj_version_t);
2477         }
2478
2479         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2480                 return -EACCES;
2481
2482         OSD_EXEC_OP(handle, xattr_set);
2483         return __osd_xattr_set(env, dt, buf, name, fl);
2484 }
2485
2486 /*
2487  * Concurrency: @dt is read locked.
2488  */
2489 static int osd_xattr_list(const struct lu_env *env,
2490                           struct dt_object *dt,
2491                           struct lu_buf *buf,
2492                           struct lustre_capa *capa)
2493 {
2494         struct osd_object      *obj    = osd_dt_obj(dt);
2495         struct inode           *inode  = obj->oo_inode;
2496         struct osd_thread_info *info   = osd_oti_get(env);
2497         struct dentry          *dentry = &info->oti_obj_dentry;
2498
2499         LASSERT(dt_object_exists(dt));
2500         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2501         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2502
2503         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2504                 return -EACCES;
2505
2506         dentry->d_inode = inode;
2507         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2508 }
2509
2510 static int osd_declare_xattr_del(const struct lu_env *env,
2511                                  struct dt_object *dt,
2512                                 const char *name,
2513                                 struct thandle *handle)
2514 {
2515         struct osd_thandle *oh;
2516
2517         LASSERT(dt_object_exists(dt));
2518         LASSERT(handle != NULL);
2519
2520         oh = container_of0(handle, struct osd_thandle, ot_super);
2521         LASSERT(oh->ot_handle == NULL);
2522
2523         OSD_DECLARE_OP(oh, xattr_set);
2524         oh->ot_credits += osd_dto_credits_noquota[DTO_XATTR_SET];
2525
2526         return 0;
2527 }
2528
2529 /*
2530  * Concurrency: @dt is write locked.
2531  */
2532 static int osd_xattr_del(const struct lu_env *env,
2533                          struct dt_object *dt,
2534                          const char *name,
2535                          struct thandle *handle,
2536                          struct lustre_capa *capa)
2537 {
2538         struct osd_object      *obj    = osd_dt_obj(dt);
2539         struct inode           *inode  = obj->oo_inode;
2540         struct osd_thread_info *info   = osd_oti_get(env);
2541         struct dentry          *dentry = &info->oti_obj_dentry;
2542         int                     rc;
2543
2544         LASSERT(dt_object_exists(dt));
2545         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2546         LASSERT(osd_write_locked(env, obj));
2547         LASSERT(handle != NULL);
2548
2549         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2550                 return -EACCES;
2551
2552         OSD_EXEC_OP(handle, xattr_set);
2553
2554         dentry->d_inode = inode;
2555         rc = inode->i_op->removexattr(dentry, name);
2556         return rc;
2557 }
2558
2559 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2560                                      struct dt_object *dt,
2561                                      struct lustre_capa *old,
2562                                      __u64 opc)
2563 {
2564         struct osd_thread_info *info = osd_oti_get(env);
2565         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2566         struct osd_object *obj = osd_dt_obj(dt);
2567         struct osd_device *dev = osd_obj2dev(obj);
2568         struct lustre_capa_key *key = &info->oti_capa_key;
2569         struct lustre_capa *capa = &info->oti_capa;
2570         struct obd_capa *oc;
2571         struct md_capainfo *ci;
2572         int rc;
2573         ENTRY;
2574
2575         if (!dev->od_fl_capa)
2576                 RETURN(ERR_PTR(-ENOENT));
2577
2578         LASSERT(dt_object_exists(dt));
2579         LINVRNT(osd_invariant(obj));
2580
2581         /* renewal sanity check */
2582         if (old && osd_object_auth(env, dt, old, opc))
2583                 RETURN(ERR_PTR(-EACCES));
2584
2585         ci = md_capainfo(env);
2586         if (unlikely(!ci))
2587                 RETURN(ERR_PTR(-ENOENT));
2588
2589         switch (ci->mc_auth) {
2590         case LC_ID_NONE:
2591                 RETURN(NULL);
2592         case LC_ID_PLAIN:
2593                 capa->lc_uid = obj->oo_inode->i_uid;
2594                 capa->lc_gid = obj->oo_inode->i_gid;
2595                 capa->lc_flags = LC_ID_PLAIN;
2596                 break;
2597         case LC_ID_CONVERT: {
2598                 __u32 d[4], s[4];
2599
2600                 s[0] = obj->oo_inode->i_uid;
2601                 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2602                 s[2] = obj->oo_inode->i_gid;
2603                 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2604                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2605                 if (unlikely(rc))
2606                         RETURN(ERR_PTR(rc));
2607
2608                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2609                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2610                 capa->lc_flags = LC_ID_CONVERT;
2611                 break;
2612         }
2613         default:
2614                 RETURN(ERR_PTR(-EINVAL));
2615         }
2616
2617         capa->lc_fid = *fid;
2618         capa->lc_opc = opc;
2619         capa->lc_flags |= dev->od_capa_alg << 24;
2620         capa->lc_timeout = dev->od_capa_timeout;
2621         capa->lc_expiry = 0;
2622
2623         oc = capa_lookup(dev->od_capa_hash, capa, 1);
2624         if (oc) {
2625                 LASSERT(!capa_is_expired(oc));
2626                 RETURN(oc);
2627         }
2628
2629         cfs_spin_lock(&capa_lock);
2630         *key = dev->od_capa_keys[1];
2631         cfs_spin_unlock(&capa_lock);
2632
2633         capa->lc_keyid = key->lk_keyid;
2634         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2635
2636         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2637         if (rc) {
2638                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2639                 RETURN(ERR_PTR(rc));
2640         }
2641
2642         oc = capa_add(dev->od_capa_hash, capa);
2643         RETURN(oc);
2644 }
2645
2646 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2647 {
2648         int rc;
2649         struct osd_object      *obj    = osd_dt_obj(dt);
2650         struct inode           *inode  = obj->oo_inode;
2651         struct osd_thread_info *info   = osd_oti_get(env);
2652         struct dentry          *dentry = &info->oti_obj_dentry;
2653         struct file            *file   = &info->oti_file;
2654         ENTRY;
2655
2656         dentry->d_inode = inode;
2657         file->f_dentry = dentry;
2658         file->f_mapping = inode->i_mapping;
2659         file->f_op = inode->i_fop;
2660         LOCK_INODE_MUTEX(inode);
2661         rc = file->f_op->fsync(file, dentry, 0);
2662         UNLOCK_INODE_MUTEX(inode);
2663         RETURN(rc);
2664 }
2665
2666 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2667                         void **data)
2668 {
2669         struct osd_object *obj = osd_dt_obj(dt);
2670         ENTRY;
2671
2672         *data = (void *)obj->oo_inode;
2673         RETURN(0);
2674 }
2675
2676 /*
2677  * Index operations.
2678  */
2679
2680 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2681                            const struct dt_index_features *feat)
2682 {
2683         struct iam_descr *descr;
2684
2685         if (osd_object_is_root(o))
2686                 return feat == &dt_directory_features;
2687
2688         LASSERT(o->oo_dir != NULL);
2689
2690         descr = o->oo_dir->od_container.ic_descr;
2691         if (feat == &dt_directory_features) {
2692                 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2693                         return 1;
2694                 else
2695                         return 0;
2696         } else {
2697                 return
2698                         feat->dif_keysize_min <= descr->id_key_size &&
2699                         descr->id_key_size <= feat->dif_keysize_max &&
2700                         feat->dif_recsize_min <= descr->id_rec_size &&
2701                         descr->id_rec_size <= feat->dif_recsize_max &&
2702                         !(feat->dif_flags & (DT_IND_VARKEY |
2703                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
2704                         ergo(feat->dif_flags & DT_IND_UPDATE,
2705                              1 /* XXX check that object (and file system) is
2706                                 * writable */);
2707         }
2708 }
2709
2710 static int osd_iam_container_init(const struct lu_env *env,
2711                                   struct osd_object *obj,
2712                                   struct osd_directory *dir)
2713 {
2714         struct iam_container *bag = &dir->od_container;
2715         int result;
2716
2717         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2718         if (result != 0)
2719                 return result;
2720
2721         result = iam_container_setup(bag);
2722         if (result != 0)
2723                 goto out;
2724
2725         if (osd_obj2dev(obj)->od_iop_mode) {
2726                 u32 ptr = bag->ic_descr->id_ops->id_root_ptr(bag);
2727
2728                 bag->ic_root_bh = ldiskfs_bread(NULL, obj->oo_inode,
2729                                                 ptr, 0, &result);
2730         }
2731
2732  out:
2733         if (result == 0)
2734                 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2735         else
2736                 iam_container_fini(bag);
2737
2738         return result;
2739 }
2740
2741
2742 /*
2743  * Concurrency: no external locking is necessary.
2744  */
2745 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2746                          const struct dt_index_features *feat)
2747 {
2748         int result;
2749         int ea_dir = 0;
2750         struct osd_object *obj = osd_dt_obj(dt);
2751         struct osd_device *osd = osd_obj2dev(obj);
2752
2753         LINVRNT(osd_invariant(obj));
2754         LASSERT(dt_object_exists(dt));
2755
2756         if (osd_object_is_root(obj)) {
2757                 dt->do_index_ops = &osd_index_ea_ops;
2758                 result = 0;
2759         } else if (feat == &dt_directory_features && osd->od_iop_mode) {
2760                 dt->do_index_ops = &osd_index_ea_ops;
2761                 if (S_ISDIR(obj->oo_inode->i_mode))
2762                         result = 0;
2763                 else
2764                         result = -ENOTDIR;
2765                 ea_dir = 1;
2766         } else if (!osd_has_index(obj)) {
2767                 struct osd_directory *dir;
2768
2769                 OBD_ALLOC_PTR(dir);
2770                 if (dir != NULL) {
2771
2772                         cfs_spin_lock(&obj->oo_guard);
2773                         if (obj->oo_dir == NULL)
2774                                 obj->oo_dir = dir;
2775                         else
2776                                 /*
2777                                  * Concurrent thread allocated container data.
2778                                  */
2779                                 OBD_FREE_PTR(dir);
2780                         cfs_spin_unlock(&obj->oo_guard);
2781                         /*
2782                          * Now, that we have container data, serialize its
2783                          * initialization.
2784                          */
2785                         cfs_down_write(&obj->oo_ext_idx_sem);
2786                         /*
2787                          * recheck under lock.
2788                          */
2789                         if (!osd_has_index(obj))
2790                                 result = osd_iam_container_init(env, obj, dir);
2791                         else
2792                                 result = 0;
2793                         cfs_up_write(&obj->oo_ext_idx_sem);
2794                 } else {
2795                         result = -ENOMEM;
2796                 }
2797         } else {
2798                 result = 0;
2799         }
2800
2801         if (result == 0 && ea_dir == 0) {
2802                 if (!osd_iam_index_probe(env, obj, feat))
2803                         result = -ENOTDIR;
2804         }
2805         LINVRNT(osd_invariant(obj));
2806
2807         return result;
2808 }
2809
2810 static const struct dt_object_operations osd_obj_ops = {
2811         .do_read_lock         = osd_object_read_lock,
2812         .do_write_lock        = osd_object_write_lock,
2813         .do_read_unlock       = osd_object_read_unlock,
2814         .do_write_unlock      = osd_object_write_unlock,
2815         .do_write_locked      = osd_object_write_locked,
2816         .do_attr_get          = osd_attr_get,
2817         .do_declare_attr_set  = osd_declare_attr_set,
2818         .do_attr_set          = osd_attr_set,
2819         .do_ah_init           = osd_ah_init,
2820         .do_declare_create    = osd_declare_object_create,
2821         .do_create            = osd_object_create,
2822         .do_declare_destroy   = osd_declare_object_destroy,
2823         .do_destroy           = osd_object_destroy,
2824         .do_index_try         = osd_index_try,
2825         .do_declare_ref_add   = osd_declare_object_ref_add,
2826         .do_ref_add           = osd_object_ref_add,
2827         .do_declare_ref_del   = osd_declare_object_ref_del,
2828         .do_ref_del           = osd_object_ref_del,
2829         .do_xattr_get         = osd_xattr_get,
2830         .do_declare_xattr_set = osd_declare_xattr_set,
2831         .do_xattr_set         = osd_xattr_set,
2832         .do_declare_xattr_del = osd_declare_xattr_del,
2833         .do_xattr_del         = osd_xattr_del,
2834         .do_xattr_list        = osd_xattr_list,
2835         .do_capa_get          = osd_capa_get,
2836         .do_object_sync       = osd_object_sync,
2837         .do_data_get          = osd_data_get,
2838 };
2839
2840 /**
2841  * dt_object_operations for interoperability mode
2842  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2843  */
2844 static const struct dt_object_operations osd_obj_ea_ops = {
2845         .do_read_lock         = osd_object_read_lock,
2846         .do_write_lock        = osd_object_write_lock,
2847         .do_read_unlock       = osd_object_read_unlock,
2848         .do_write_unlock      = osd_object_write_unlock,
2849         .do_write_locked      = osd_object_write_locked,
2850         .do_attr_get          = osd_attr_get,
2851         .do_declare_attr_set  = osd_declare_attr_set,
2852         .do_attr_set          = osd_attr_set,
2853         .do_ah_init           = osd_ah_init,
2854         .do_declare_create    = osd_declare_object_create,
2855         .do_create            = osd_object_ea_create,
2856         .do_declare_destroy   = osd_declare_object_destroy,
2857         .do_destroy           = osd_object_destroy,
2858         .do_index_try         = osd_index_try,
2859         .do_declare_ref_add   = osd_declare_object_ref_add,
2860         .do_ref_add           = osd_object_ref_add,
2861         .do_declare_ref_del   = osd_declare_object_ref_del,
2862         .do_ref_del           = osd_object_ref_del,
2863         .do_xattr_get         = osd_xattr_get,
2864         .do_declare_xattr_set = osd_declare_xattr_set,
2865         .do_xattr_set         = osd_xattr_set,
2866         .do_declare_xattr_del = osd_declare_xattr_del,
2867         .do_xattr_del         = osd_xattr_del,
2868         .do_xattr_list        = osd_xattr_list,
2869         .do_capa_get          = osd_capa_get,
2870         .do_object_sync       = osd_object_sync,
2871         .do_data_get          = osd_data_get,
2872 };
2873
2874 /*
2875  * Body operations.
2876  */
2877
2878 /*
2879  * XXX: Another layering violation for now.
2880  *
2881  * We don't want to use ->f_op->read methods, because generic file write
2882  *
2883  *         - serializes on ->i_sem, and
2884  *
2885  *         - does a lot of extra work like balance_dirty_pages(),
2886  *
2887  * which doesn't work for globally shared files like /last-received.
2888  */
2889 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
2890 {
2891         struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
2892
2893         memcpy(buffer, (char*)ei->i_data, buflen);
2894
2895         return  buflen;
2896 }
2897
2898 static int osd_ldiskfs_read(struct inode *inode, void *buf, int size,
2899                             loff_t *offs)
2900 {
2901         struct buffer_head *bh;
2902         unsigned long block;
2903         int osize = size;
2904         int blocksize;
2905         int csize;
2906         int boffs;
2907         int err;
2908
2909         /* prevent reading after eof */
2910         spin_lock(&inode->i_lock);
2911         if (i_size_read(inode) < *offs + size) {
2912                 size = i_size_read(inode) - *offs;
2913                 spin_unlock(&inode->i_lock);
2914                 if (size < 0) {
2915                         CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
2916                                i_size_read(inode), *offs);
2917                         return -EBADR;
2918                 } else if (size == 0) {
2919                         return 0;
2920                 }
2921         } else {
2922                 spin_unlock(&inode->i_lock);
2923         }
2924
2925         blocksize = 1 << inode->i_blkbits;
2926
2927         while (size > 0) {
2928                 block = *offs >> inode->i_blkbits;
2929                 boffs = *offs & (blocksize - 1);
2930                 csize = min(blocksize - boffs, size);
2931                 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
2932                 if (!bh) {
2933                         CERROR("can't read block: %d\n", err);
2934                         return err;
2935                 }
2936
2937                 memcpy(buf, bh->b_data + boffs, csize);
2938                 brelse(bh);
2939
2940                 *offs += csize;
2941                 buf += csize;
2942                 size -= csize;
2943         }
2944         return osize;
2945 }
2946
2947 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
2948                         struct lu_buf *buf, loff_t *pos,
2949                         struct lustre_capa *capa)
2950 {
2951         struct osd_object      *obj    = osd_dt_obj(dt);
2952         struct inode           *inode  = obj->oo_inode;
2953         int rc;
2954
2955         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
2956                 RETURN(-EACCES);
2957
2958         /* Read small symlink from inode body as we need to maintain correct
2959          * on-disk symlinks for ldiskfs.
2960          */
2961         if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
2962             (buf->lb_len <= sizeof (LDISKFS_I(inode)->i_data)))
2963                 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
2964         else
2965                 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
2966
2967         return rc;
2968 }
2969
2970 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
2971 {
2972
2973         memcpy((char*)&LDISKFS_I(inode)->i_data, (char *)buffer,
2974                buflen);
2975         LDISKFS_I(inode)->i_disksize = buflen;
2976         i_size_write(inode, buflen);
2977         inode->i_sb->s_op->dirty_inode(inode);
2978
2979         return 0;
2980 }
2981
2982 static int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
2983                                     loff_t *offs, handle_t *handle)
2984 {
2985         struct buffer_head *bh = NULL;
2986         loff_t offset = *offs;
2987         loff_t new_size = i_size_read(inode);
2988         unsigned long block;
2989         int blocksize = 1 << inode->i_blkbits;
2990         int err = 0;
2991         int size;
2992         int boffs;
2993         int dirty_inode = 0;
2994
2995         while (bufsize > 0) {
2996                 if (bh != NULL)
2997                         brelse(bh);
2998
2999                 block = offset >> inode->i_blkbits;
3000                 boffs = offset & (blocksize - 1);
3001                 size = min(blocksize - boffs, bufsize);
3002                 bh = ldiskfs_bread(handle, inode, block, 1, &err);
3003                 if (!bh) {
3004                         CERROR("can't read/create block: %d\n", err);
3005                         break;
3006                 }
3007
3008                 err = ldiskfs_journal_get_write_access(handle, bh);
3009                 if (err) {
3010                         CERROR("journal_get_write_access() returned error %d\n",
3011                                err);
3012                         break;
3013                 }
3014                 LASSERTF(boffs + size <= bh->b_size,
3015                          "boffs %d size %d bh->b_size %lu",
3016                          boffs, size, (unsigned long)bh->b_size);
3017                 memcpy(bh->b_data + boffs, buf, size);
3018                 err = ldiskfs_journal_dirty_metadata(handle, bh);
3019                 if (err)
3020                         break;
3021
3022                 if (offset + size > new_size)
3023                         new_size = offset + size;
3024                 offset += size;
3025                 bufsize -= size;
3026                 buf += size;
3027         }
3028         if (bh)
3029                 brelse(bh);
3030
3031         /* correct in-core and on-disk sizes */
3032         if (new_size > i_size_read(inode)) {
3033                 spin_lock(&inode->i_lock);
3034                 if (new_size > i_size_read(inode))
3035                         i_size_write(inode, new_size);
3036                 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
3037                         LDISKFS_I(inode)->i_disksize = i_size_read(inode);
3038                         dirty_inode = 1;
3039                 }
3040                 spin_unlock(&inode->i_lock);
3041                 if (dirty_inode)
3042                         inode->i_sb->s_op->dirty_inode(inode);
3043         }
3044
3045         if (err == 0)
3046                 *offs = offset;
3047         return err;
3048 }
3049
3050 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
3051                                  const loff_t size, loff_t pos,
3052                                  struct thandle *handle)
3053 {
3054         struct osd_thandle *oh;
3055
3056         LASSERT(handle != NULL);
3057
3058         oh = container_of0(handle, struct osd_thandle, ot_super);
3059         LASSERT(oh->ot_handle == NULL);
3060
3061         OSD_DECLARE_OP(oh, write);
3062         oh->ot_credits += osd_dto_credits_noquota[DTO_WRITE_BLOCK];
3063
3064         if (osd_dt_obj(dt)->oo_inode == NULL)
3065                 return 0;
3066
3067         osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3068                         osd_dt_obj(dt)->oo_inode);
3069         osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3070                         osd_dt_obj(dt)->oo_inode);
3071         return 0;
3072 }
3073
3074 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
3075                          const struct lu_buf *buf, loff_t *pos,
3076                          struct thandle *handle, struct lustre_capa *capa,
3077                          int ignore_quota)
3078 {
3079         struct osd_object  *obj   = osd_dt_obj(dt);
3080         struct inode       *inode = obj->oo_inode;
3081         struct osd_thandle *oh;
3082         ssize_t            result = 0;
3083 #ifdef HAVE_QUOTA_SUPPORT
3084         cfs_cap_t           save = cfs_curproc_cap_pack();
3085 #endif
3086
3087         LASSERT(handle != NULL);
3088
3089         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
3090                 RETURN(-EACCES);
3091
3092         oh = container_of(handle, struct osd_thandle, ot_super);
3093         LASSERT(oh->ot_handle->h_transaction != NULL);
3094 #ifdef HAVE_QUOTA_SUPPORT
3095         if (ignore_quota)
3096                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3097         else
3098                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3099 #endif
3100         /* Write small symlink to inode body as we need to maintain correct
3101          * on-disk symlinks for ldiskfs.
3102          */
3103         if(S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr) &&
3104            (buf->lb_len < sizeof (LDISKFS_I(inode)->i_data)))
3105                 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
3106         else
3107                 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
3108                                                   buf->lb_len, pos,
3109                                                   oh->ot_handle);
3110 #ifdef HAVE_QUOTA_SUPPORT
3111         cfs_curproc_cap_unpack(save);
3112 #endif
3113         if (result == 0)
3114                 result = buf->lb_len;
3115         return result;
3116 }
3117
3118 /*
3119  * in some cases we may need declare methods for objects being created
3120  * e.g., when we create symlink
3121  */
3122 static const struct dt_body_operations osd_body_ops_new = {
3123         .dbo_declare_write = osd_declare_write,
3124 };
3125
3126 static const struct dt_body_operations osd_body_ops = {
3127         .dbo_read          = osd_read,
3128         .dbo_declare_write = osd_declare_write,
3129         .dbo_write         = osd_write
3130 };
3131
3132 static int osd_index_declare_iam_delete(const struct lu_env *env,
3133                                         struct dt_object *dt,
3134                                         const struct dt_key *key,
3135                                         struct thandle *handle)
3136 {
3137         struct osd_thandle    *oh;
3138
3139         oh = container_of0(handle, struct osd_thandle, ot_super);
3140         LASSERT(oh->ot_handle == NULL);
3141
3142         OSD_DECLARE_OP(oh, delete);
3143         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3144
3145         return 0;
3146 }
3147
3148 /**
3149  *      delete a (key, value) pair from index \a dt specified by \a key
3150  *
3151  *      \param  dt      osd index object
3152  *      \param  key     key for index
3153  *      \param  rec     record reference
3154  *      \param  handle  transaction handler
3155  *
3156  *      \retval  0  success
3157  *      \retval -ve   failure
3158  */
3159
3160 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3161                                 const struct dt_key *key, struct thandle *handle,
3162                                 struct lustre_capa *capa)
3163 {
3164         struct osd_object     *obj = osd_dt_obj(dt);
3165         struct osd_thandle    *oh;
3166         struct iam_path_descr *ipd;
3167         struct iam_container  *bag = &obj->oo_dir->od_container;
3168         int rc;
3169
3170         ENTRY;
3171
3172         LINVRNT(osd_invariant(obj));
3173         LASSERT(dt_object_exists(dt));
3174         LASSERT(bag->ic_object == obj->oo_inode);
3175         LASSERT(handle != NULL);
3176
3177         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3178                 RETURN(-EACCES);
3179
3180         OSD_EXEC_OP(handle, delete);
3181
3182         ipd = osd_idx_ipd_get(env, bag);
3183         if (unlikely(ipd == NULL))
3184                 RETURN(-ENOMEM);
3185
3186         oh = container_of0(handle, struct osd_thandle, ot_super);
3187         LASSERT(oh->ot_handle != NULL);
3188         LASSERT(oh->ot_handle->h_transaction != NULL);
3189
3190         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3191         osd_ipd_put(env, bag, ipd);
3192         LINVRNT(osd_invariant(obj));
3193         RETURN(rc);
3194 }
3195
3196 static int osd_index_declare_ea_delete(const struct lu_env *env,
3197                                        struct dt_object *dt,
3198                                        const struct dt_key *key,
3199                                        struct thandle *handle)
3200 {
3201         struct osd_thandle *oh;
3202
3203         LASSERT(dt_object_exists(dt));
3204         LASSERT(handle != NULL);
3205
3206         oh = container_of0(handle, struct osd_thandle, ot_super);
3207         LASSERT(oh->ot_handle == NULL);
3208
3209         OSD_DECLARE_OP(oh, delete);
3210         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_DELETE];
3211
3212         LASSERT(osd_dt_obj(dt)->oo_inode);
3213         osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3214                         osd_dt_obj(dt)->oo_inode);
3215         osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3216                         osd_dt_obj(dt)->oo_inode);
3217
3218         return 0;
3219 }
3220
3221 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3222                                           struct dt_rec *fid)
3223 {
3224         struct osd_fid_pack *rec;
3225         int rc = -ENODATA;
3226
3227         if (de->file_type & LDISKFS_DIRENT_LUFID) {
3228                 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3229                 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3230         }
3231         RETURN(rc);
3232 }
3233
3234 /**
3235  * Index delete function for interoperability mode (b11826).
3236  * It will remove the directory entry added by osd_index_ea_insert().
3237  * This entry is needed to maintain name->fid mapping.
3238  *
3239  * \param key,  key i.e. file entry to be deleted
3240  *
3241  * \retval   0, on success
3242  * \retval -ve, on error
3243  */
3244 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3245                                const struct dt_key *key, struct thandle *handle,
3246                                struct lustre_capa *capa)
3247 {
3248         struct osd_object          *obj    = osd_dt_obj(dt);
3249         struct inode               *dir    = obj->oo_inode;
3250         struct dentry              *dentry;
3251         struct osd_thandle         *oh;
3252         struct ldiskfs_dir_entry_2 *de;
3253         struct buffer_head         *bh;
3254         struct htree_lock          *hlock = NULL;
3255
3256         int rc;
3257
3258         ENTRY;
3259
3260         LINVRNT(osd_invariant(obj));
3261         LASSERT(dt_object_exists(dt));
3262         LASSERT(handle != NULL);
3263
3264         OSD_EXEC_OP(handle, delete);
3265
3266         oh = container_of(handle, struct osd_thandle, ot_super);
3267         LASSERT(oh->ot_handle != NULL);
3268         LASSERT(oh->ot_handle->h_transaction != NULL);
3269
3270         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3271                 RETURN(-EACCES);
3272
3273         dentry = osd_child_dentry_get(env, obj,
3274                                       (char *)key, strlen((char *)key));
3275
3276         if (obj->oo_hl_head != NULL) {
3277                 hlock = osd_oti_get(env)->oti_hlock;
3278                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3279                                    dir, LDISKFS_HLOCK_DEL);
3280         } else {
3281                 cfs_down_write(&obj->oo_ext_idx_sem);
3282         }
3283
3284         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3285         if (bh) {
3286                 rc = ldiskfs_delete_entry(oh->ot_handle,
3287                                           dir, de, bh);
3288                 brelse(bh);
3289         } else {
3290                 rc = -ENOENT;
3291         }
3292         if (hlock != NULL)
3293                 ldiskfs_htree_unlock(hlock);
3294         else
3295                 cfs_up_write(&obj->oo_ext_idx_sem);
3296
3297         LASSERT(osd_invariant(obj));
3298         RETURN(rc);
3299 }
3300
3301 /**
3302  *      Lookup index for \a key and copy record to \a rec.
3303  *
3304  *      \param  dt      osd index object
3305  *      \param  key     key for index
3306  *      \param  rec     record reference
3307  *
3308  *      \retval  +ve  success : exact mach
3309  *      \retval  0    return record with key not greater than \a key
3310  *      \retval -ve   failure
3311  */
3312 static int osd_index_iam_lookup(const struct lu_env *env, struct dt_object *dt,
3313                                 struct dt_rec *rec, const struct dt_key *key,
3314                                 struct lustre_capa *capa)
3315 {
3316         struct osd_object     *obj = osd_dt_obj(dt);
3317         struct iam_path_descr *ipd;
3318         struct iam_container  *bag = &obj->oo_dir->od_container;
3319         struct osd_thread_info *oti = osd_oti_get(env);
3320         struct iam_iterator    *it = &oti->oti_idx_it;
3321         struct iam_rec *iam_rec;
3322         int rc;
3323         ENTRY;
3324
3325         LASSERT(osd_invariant(obj));
3326         LASSERT(dt_object_exists(dt));
3327         LASSERT(bag->ic_object == obj->oo_inode);
3328
3329         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_LOOKUP))
3330                 RETURN(-EACCES);
3331
3332         ipd = osd_idx_ipd_get(env, bag);
3333         if (IS_ERR(ipd))
3334                 RETURN(-ENOMEM);
3335
3336         /* got ipd now we can start iterator. */
3337         iam_it_init(it, bag, 0, ipd);
3338
3339         rc = iam_it_get(it, (struct iam_key *)key);
3340         if (rc >= 0) {
3341                 if (S_ISDIR(obj->oo_inode->i_mode))
3342                         iam_rec = (struct iam_rec *)oti->oti_ldp;
3343                 else
3344                         iam_rec = (struct iam_rec *) rec;
3345
3346                 iam_reccpy(&it->ii_path.ip_leaf, (struct iam_rec *)iam_rec);
3347                 if (S_ISDIR(obj->oo_inode->i_mode))
3348                         osd_fid_unpack((struct lu_fid *) rec,
3349                                        (struct osd_fid_pack *)iam_rec);
3350         }
3351         iam_it_put(it);
3352         iam_it_fini(it);
3353         osd_ipd_put(env, bag, ipd);
3354
3355         LINVRNT(osd_invariant(obj));
3356
3357         RETURN(rc);
3358 }
3359
3360 static int osd_index_declare_iam_insert(const struct lu_env *env,
3361                                         struct dt_object *dt,
3362                                         const struct dt_rec *rec,
3363                                         const struct dt_key *key,
3364                                         struct thandle *handle)
3365 {
3366         struct osd_thandle *oh;
3367
3368         LASSERT(dt_object_exists(dt));
3369         LASSERT(handle != NULL);
3370
3371         oh = container_of0(handle, struct osd_thandle, ot_super);
3372         LASSERT(oh->ot_handle == NULL);
3373
3374         OSD_DECLARE_OP(oh, insert);
3375         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3376
3377         return 0;
3378 }
3379
3380 /**
3381  *      Inserts (key, value) pair in \a dt index object.
3382  *
3383  *      \param  dt      osd index object
3384  *      \param  key     key for index
3385  *      \param  rec     record reference
3386  *      \param  th      transaction handler
3387  *
3388  *      \retval  0  success
3389  *      \retval -ve failure
3390  */
3391 static int osd_index_iam_insert(const struct lu_env *env, struct dt_object *dt,
3392                                 const struct dt_rec *rec, const struct dt_key *key,
3393                                 struct thandle *th, struct lustre_capa *capa,
3394                                 int ignore_quota)
3395 {
3396         struct osd_object     *obj = osd_dt_obj(dt);
3397         struct iam_path_descr *ipd;
3398         struct osd_thandle    *oh;
3399         struct iam_container  *bag = &obj->oo_dir->od_container;
3400 #ifdef HAVE_QUOTA_SUPPORT
3401         cfs_cap_t              save = cfs_curproc_cap_pack();
3402 #endif
3403         struct osd_thread_info *oti = osd_oti_get(env);
3404         struct iam_rec *iam_rec = (struct iam_rec *)oti->oti_ldp;
3405         int rc;
3406
3407         ENTRY;
3408
3409         LINVRNT(osd_invariant(obj));
3410         LASSERT(dt_object_exists(dt));
3411         LASSERT(bag->ic_object == obj->oo_inode);
3412         LASSERT(th != NULL);
3413
3414         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3415                 return -EACCES;
3416
3417         OSD_EXEC_OP(th, insert);
3418
3419         ipd = osd_idx_ipd_get(env, bag);
3420         if (unlikely(ipd == NULL))
3421                 RETURN(-ENOMEM);
3422
3423         oh = container_of0(th, struct osd_thandle, ot_super);
3424         LASSERT(oh->ot_handle != NULL);
3425         LASSERT(oh->ot_handle->h_transaction != NULL);
3426 #ifdef HAVE_QUOTA_SUPPORT
3427         if (ignore_quota)
3428                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3429         else
3430                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3431 #endif
3432         if (S_ISDIR(obj->oo_inode->i_mode))
3433                 osd_fid_pack((struct osd_fid_pack *)iam_rec, rec, &oti->oti_fid);
3434         else
3435                 iam_rec = (struct iam_rec *) rec;
3436         rc = iam_insert(oh->ot_handle, bag, (const struct iam_key *)key,
3437                         iam_rec, ipd);
3438 #ifdef HAVE_QUOTA_SUPPORT
3439         cfs_curproc_cap_unpack(save);
3440 #endif
3441         osd_ipd_put(env, bag, ipd);
3442         LINVRNT(osd_invariant(obj));
3443         RETURN(rc);
3444 }
3445
3446 /**
3447  * Calls ldiskfs_add_entry() to add directory entry
3448  * into the directory. This is required for
3449  * interoperability mode (b11826)
3450  *
3451  * \retval   0, on success
3452  * \retval -ve, on error
3453  */
3454 static int __osd_ea_add_rec(struct osd_thread_info *info,
3455                             struct osd_object *pobj,
3456                             struct inode  *cinode,
3457                             const char *name,
3458                             const struct dt_rec *fid,
3459                             struct htree_lock *hlock,
3460                             struct thandle *th)
3461 {
3462         struct ldiskfs_dentry_param *ldp;
3463         struct dentry      *child;
3464         struct osd_thandle *oth;
3465         int rc;
3466
3467         oth = container_of(th, struct osd_thandle, ot_super);
3468         LASSERT(oth->ot_handle != NULL);
3469         LASSERT(oth->ot_handle->h_transaction != NULL);
3470
3471         child = osd_child_dentry_get(info->oti_env, pobj, name, strlen(name));
3472
3473         if (fid_is_igif((struct lu_fid *)fid) ||
3474             fid_is_norm((struct lu_fid *)fid)) {
3475                 ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3476                 osd_get_ldiskfs_dirent_param(ldp, fid);
3477                 child->d_fsdata = (void*) ldp;
3478         } else
3479                 child->d_fsdata = NULL;
3480         rc = osd_ldiskfs_add_entry(oth->ot_handle, child, cinode, hlock);
3481
3482         RETURN(rc);
3483 }
3484
3485 /**
3486  * Calls ldiskfs_add_dot_dotdot() to add dot and dotdot entries
3487  * into the directory.Also sets flags into osd object to
3488  * indicate dot and dotdot are created. This is required for
3489  * interoperability mode (b11826)
3490  *
3491  * \param dir   directory for dot and dotdot fixup.
3492  * \param obj   child object for linking
3493  *
3494  * \retval   0, on success
3495  * \retval -ve, on error
3496  */
3497 static int osd_add_dot_dotdot(struct osd_thread_info *info,
3498                               struct osd_object *dir,
3499                               struct inode  *parent_dir, const char *name,
3500                               const struct dt_rec *dot_fid,
3501                               const struct dt_rec *dot_dot_fid,
3502                               struct thandle *th)
3503 {
3504         struct inode            *inode  = dir->oo_inode;
3505         struct ldiskfs_dentry_param *dot_ldp;
3506         struct ldiskfs_dentry_param *dot_dot_ldp;
3507         struct osd_thandle      *oth;
3508         int result = 0;
3509
3510         oth = container_of(th, struct osd_thandle, ot_super);
3511         LASSERT(oth->ot_handle->h_transaction != NULL);
3512         LASSERT(S_ISDIR(dir->oo_inode->i_mode));
3513
3514         if (strcmp(name, dot) == 0) {
3515                 if (dir->oo_compat_dot_created) {
3516                         result = -EEXIST;
3517                 } else {
3518                         LASSERT(inode == parent_dir);
3519                         dir->oo_compat_dot_created = 1;
3520                         result = 0;
3521                 }
3522         } else if(strcmp(name, dotdot) == 0) {
3523                 dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
3524                 dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
3525
3526                 if (!dir->oo_compat_dot_created)
3527                         return -EINVAL;
3528                 if (fid_seq((struct lu_fid *)dot_fid) >= FID_SEQ_NORMAL) {
3529                         osd_get_ldiskfs_dirent_param(dot_ldp, dot_fid);
3530                         osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
3531                 } else {
3532                         dot_ldp = NULL;
3533                         dot_dot_ldp = NULL;
3534                 }
3535                 /* in case of rename, dotdot is already created */
3536                 if (dir->oo_compat_dotdot_created) {
3537                         return __osd_ea_add_rec(info, dir, parent_dir, name,
3538                                                 dot_dot_fid, NULL, th);
3539                 }
3540
3541                 result = ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
3542                                                 inode, dot_ldp, dot_dot_ldp);
3543                 if (result == 0)
3544                        dir->oo_compat_dotdot_created = 1;
3545         }
3546
3547         return result;
3548 }
3549
3550
3551 /**
3552  * It will call the appropriate osd_add* function and return the
3553  * value, return by respective functions.
3554  */
3555 static int osd_ea_add_rec(const struct lu_env *env,
3556                           struct osd_object *pobj,
3557                           struct inode *cinode,
3558                           const char *name,
3559                           const struct dt_rec *fid,
3560                           struct thandle *th)
3561 {
3562         struct osd_thread_info    *info   = osd_oti_get(env);
3563         struct htree_lock         *hlock;
3564         int rc;
3565
3566         hlock = pobj->oo_hl_head != NULL ? info->oti_hlock : NULL;
3567
3568         if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' &&
3569                                                    name[2] =='\0'))) {
3570                 if (hlock != NULL) {
3571                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3572                                            pobj->oo_inode, 0);
3573                 } else {
3574                         cfs_down_write(&pobj->oo_ext_idx_sem);
3575                 }
3576                 rc = osd_add_dot_dotdot(info, pobj, cinode, name,
3577                      (struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
3578                                         fid, th);
3579         } else {
3580                 if (hlock != NULL) {
3581                         ldiskfs_htree_lock(hlock, pobj->oo_hl_head,
3582                                            pobj->oo_inode, LDISKFS_HLOCK_ADD);
3583                 } else {
3584                         cfs_down_write(&pobj->oo_ext_idx_sem);
3585                 }
3586
3587                 rc = __osd_ea_add_rec(info, pobj, cinode, name, fid,
3588                                       hlock, th);
3589         }
3590         if (hlock != NULL)
3591                 ldiskfs_htree_unlock(hlock);
3592         else
3593                 cfs_up_write(&pobj->oo_ext_idx_sem);
3594
3595         return rc;
3596 }
3597
3598 /**
3599  * Calls ->lookup() to find dentry. From dentry get inode and
3600  * read inode's ea to get fid. This is required for  interoperability
3601  * mode (b11826)
3602  *
3603  * \retval   0, on success
3604  * \retval -ve, on error
3605  */
3606 static int osd_ea_lookup_rec(const struct lu_env *env, struct osd_object *obj,
3607                              struct dt_rec *rec, const struct dt_key *key)
3608 {
3609         struct inode               *dir    = obj->oo_inode;
3610         struct dentry              *dentry;
3611         struct ldiskfs_dir_entry_2 *de;
3612         struct buffer_head         *bh;
3613         struct lu_fid              *fid = (struct lu_fid *) rec;
3614         struct htree_lock          *hlock = NULL;
3615         int ino;
3616         int rc;
3617
3618         LASSERT(dir->i_op != NULL && dir->i_op->lookup != NULL);
3619
3620         dentry = osd_child_dentry_get(env, obj,
3621                                       (char *)key, strlen((char *)key));
3622
3623         if (obj->oo_hl_head != NULL) {
3624                 hlock = osd_oti_get(env)->oti_hlock;
3625                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3626                                    dir, LDISKFS_HLOCK_LOOKUP);
3627         } else {
3628                 cfs_down_read(&obj->oo_ext_idx_sem);
3629         }
3630
3631         bh = osd_ldiskfs_find_entry(dir, dentry, &de, hlock);
3632         if (bh) {
3633                 ino = le32_to_cpu(de->inode);
3634                 rc = osd_get_fid_from_dentry(de, rec);
3635
3636                 /* done with de, release bh */
3637                 brelse(bh);
3638                 if (rc != 0)
3639                         rc = osd_ea_fid_get(env, obj, ino, fid);
3640         } else {
3641                 rc = -ENOENT;
3642         }
3643
3644         if (hlock != NULL)
3645                 ldiskfs_htree_unlock(hlock);
3646         else
3647                 cfs_up_read(&obj->oo_ext_idx_sem);
3648         RETURN (rc);
3649 }
3650
3651 /**
3652  * Find the osd object for given fid.
3653  *
3654  * \param fid need to find the osd object having this fid
3655  *
3656  * \retval osd_object on success
3657  * \retval        -ve on error
3658  */
3659 struct osd_object *osd_object_find(const struct lu_env *env,
3660                                    struct dt_object *dt,
3661                                    const struct lu_fid *fid)
3662 {
3663         struct lu_device         *ludev = dt->do_lu.lo_dev;
3664         struct osd_object        *child = NULL;
3665         struct lu_object         *luch;
3666         struct lu_object         *lo;
3667
3668         luch = lu_object_find(env, ludev, fid, NULL);
3669         if (!IS_ERR(luch)) {
3670                 if (lu_object_exists(luch)) {
3671                         lo = lu_object_locate(luch->lo_header, ludev->ld_type);
3672                         if (lo != NULL)
3673                                 child = osd_obj(lo);
3674                         else
3675                                 LU_OBJECT_DEBUG(D_ERROR, env, luch,
3676                                                 "lu_object can't be located"
3677                                                 ""DFID"\n", PFID(fid));
3678
3679                         if (child == NULL) {
3680                                 lu_object_put(env, luch);
3681                                 CERROR("Unable to get osd_object\n");
3682                                 child = ERR_PTR(-ENOENT);
3683                         }
3684                 } else {
3685                         LU_OBJECT_DEBUG(D_ERROR, env, luch,
3686                                         "lu_object does not exists "DFID"\n",
3687                                         PFID(fid));
3688                         child = ERR_PTR(-ENOENT);
3689                 }
3690         } else
3691                 child = (void *)luch;
3692
3693         return child;
3694 }
3695
3696 /**
3697  * Put the osd object once done with it.
3698  *
3699  * \param obj osd object that needs to be put
3700  */
3701 static inline void osd_object_put(const struct lu_env *env,
3702                                   struct osd_object *obj)
3703 {
3704         lu_object_put(env, &obj->oo_dt.do_lu);
3705 }
3706
3707 static int osd_index_declare_ea_insert(const struct lu_env *env,
3708                                        struct dt_object *dt,
3709                                        const struct dt_rec *rec,
3710                                        const struct dt_key *key,
3711                                        struct thandle *handle)
3712 {
3713         struct osd_thandle *oh;
3714
3715         LASSERT(dt_object_exists(dt));
3716         LASSERT(handle != NULL);
3717
3718         oh = container_of0(handle, struct osd_thandle, ot_super);
3719         LASSERT(oh->ot_handle == NULL);
3720
3721         OSD_DECLARE_OP(oh, insert);
3722         oh->ot_credits += osd_dto_credits_noquota[DTO_INDEX_INSERT];
3723
3724         LASSERT(osd_dt_obj(dt)->oo_inode);
3725         osd_declare_qid(dt, oh, USRQUOTA, osd_dt_obj(dt)->oo_inode->i_uid,
3726                         osd_dt_obj(dt)->oo_inode);
3727         osd_declare_qid(dt, oh, GRPQUOTA, osd_dt_obj(dt)->oo_inode->i_gid,
3728                         osd_dt_obj(dt)->oo_inode);
3729
3730         return 0;
3731 }
3732
3733 /**
3734  * Index add function for interoperability mode (b11826).
3735  * It will add the directory entry.This entry is needed to
3736  * maintain name->fid mapping.
3737  *
3738  * \param key it is key i.e. file entry to be inserted
3739  * \param rec it is value of given key i.e. fid
3740  *
3741  * \retval   0, on success
3742  * \retval -ve, on error
3743  */
3744 static int osd_index_ea_insert(const struct lu_env *env, struct dt_object *dt,
3745                                const struct dt_rec *rec,
3746                                const struct dt_key *key, struct thandle *th,
3747                                struct lustre_capa *capa, int ignore_quota)
3748 {
3749         struct osd_object        *obj   = osd_dt_obj(dt);
3750         struct lu_fid            *fid   = (struct lu_fid *) rec;
3751         const char               *name  = (const char *)key;
3752         struct osd_object        *child;
3753 #ifdef HAVE_QUOTA_SUPPORT
3754         cfs_cap_t                 save  = cfs_curproc_cap_pack();
3755 #endif
3756         int rc;
3757
3758         ENTRY;
3759
3760         LASSERT(osd_invariant(obj));
3761         LASSERT(dt_object_exists(dt));
3762         LASSERT(th != NULL);
3763
3764         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_INSERT))
3765                 RETURN(-EACCES);
3766
3767         child = osd_object_find(env, dt, fid);
3768         if (!IS_ERR(child)) {
3769 #ifdef HAVE_QUOTA_SUPPORT
3770                 if (ignore_quota)
3771                         cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
3772                 else
3773                         cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
3774 #endif
3775                 rc = osd_ea_add_rec(env, obj, child->oo_inode, name, rec, th);
3776 #ifdef HAVE_QUOTA_SUPPORT
3777                 cfs_curproc_cap_unpack(save);
3778 #endif
3779                 osd_object_put(env, child);
3780         } else {
3781                 rc = PTR_ERR(child);
3782         }
3783
3784         LASSERT(osd_invariant(obj));
3785         RETURN(rc);
3786 }
3787
3788 /**
3789  *  Initialize osd Iterator for given osd index object.
3790  *
3791  *  \param  dt      osd index object
3792  */
3793
3794 static struct dt_it *osd_it_iam_init(const struct lu_env *env,
3795                                      struct dt_object *dt,
3796                                      __u32 unused,
3797                                      struct lustre_capa *capa)
3798 {
3799         struct osd_it_iam         *it;
3800         struct osd_thread_info *oti = osd_oti_get(env);
3801         struct osd_object     *obj = osd_dt_obj(dt);
3802         struct lu_object      *lo  = &dt->do_lu;
3803         struct iam_path_descr *ipd;
3804         struct iam_container  *bag = &obj->oo_dir->od_container;
3805
3806         LASSERT(lu_object_exists(lo));
3807
3808         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
3809                 return ERR_PTR(-EACCES);
3810