Whamcloud - gitweb
LU-1187 osd: add agent/local inode for remote directory
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_handler.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osd/osd_handler.c
37  *
38  * Top-level entry points into osd module
39  *
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  *         Pravin Shelar <pravin.shelar@sun.com> : Added fid in dirent
42  */
43
44 #define DEBUG_SUBSYSTEM S_MDS
45
46 #include <linux/module.h>
47
48 /* LUSTRE_VERSION_CODE */
49 #include <lustre_ver.h>
50 /* prerequisite for linux/xattr.h */
51 #include <linux/types.h>
52 /* prerequisite for linux/xattr.h */
53 #include <linux/fs.h>
54 /* XATTR_{REPLACE,CREATE} */
55 #include <linux/xattr.h>
56 /* simple_mkdir() */
57 #include <lvfs.h>
58
59 /*
60  * struct OBD_{ALLOC,FREE}*()
61  * OBD_FAIL_CHECK
62  */
63 #include <obd_support.h>
64 /* struct ptlrpc_thread */
65 #include <lustre_net.h>
66 #include <lustre_fid.h>
67
68 #include "osd_internal.h"
69
70 /* llo_* api support */
71 #include <md_object.h>
72 #include <lustre_quota.h>
73
74 int ldiskfs_pdo = 1;
75 CFS_MODULE_PARM(ldiskfs_pdo, "i", int, 0644,
76                 "ldiskfs with parallel directory operations");
77
78 static const char dot[] = ".";
79 static const char dotdot[] = "..";
80 static const char remote_obj_dir[] = "REM_OBJ_DIR";
81
82 static const struct lu_object_operations      osd_lu_obj_ops;
83 static const struct dt_object_operations      osd_obj_ops;
84 static const struct dt_object_operations      osd_obj_ea_ops;
85 static const struct dt_object_operations      osd_obj_otable_it_ops;
86 static const struct dt_index_operations       osd_index_iam_ops;
87 static const struct dt_index_operations       osd_index_ea_ops;
88
89 #ifdef OSD_TRACK_DECLARES
90 int osd_trans_declare_op2rb[] = {
91         [OSD_OT_ATTR_SET]       = OSD_OT_ATTR_SET,
92         [OSD_OT_PUNCH]          = OSD_OT_MAX,
93         [OSD_OT_XATTR_SET]      = OSD_OT_XATTR_SET,
94         [OSD_OT_CREATE]         = OSD_OT_DESTROY,
95         [OSD_OT_DESTROY]        = OSD_OT_CREATE,
96         [OSD_OT_REF_ADD]        = OSD_OT_REF_DEL,
97         [OSD_OT_REF_DEL]        = OSD_OT_REF_ADD,
98         [OSD_OT_WRITE]          = OSD_OT_WRITE,
99         [OSD_OT_INSERT]         = OSD_OT_DELETE,
100         [OSD_OT_DELETE]         = OSD_OT_INSERT,
101         [OSD_OT_QUOTA]          = OSD_OT_MAX,
102 };
103 #endif
104
105 static int osd_has_index(const struct osd_object *obj)
106 {
107         return obj->oo_dt.do_index_ops != NULL;
108 }
109
110 static int osd_object_invariant(const struct lu_object *l)
111 {
112         return osd_invariant(osd_obj(l));
113 }
114
115 /*
116  * Concurrency: doesn't matter
117  */
118 static int osd_read_locked(const struct lu_env *env, struct osd_object *o)
119 {
120         return osd_oti_get(env)->oti_r_locks > 0;
121 }
122
123 /*
124  * Concurrency: doesn't matter
125  */
126 static int osd_write_locked(const struct lu_env *env, struct osd_object *o)
127 {
128         struct osd_thread_info *oti = osd_oti_get(env);
129         return oti->oti_w_locks > 0 && o->oo_owner == env;
130 }
131
132 /*
133  * Concurrency: doesn't access mutable data
134  */
135 static int osd_root_get(const struct lu_env *env,
136                         struct dt_device *dev, struct lu_fid *f)
137 {
138         lu_local_obj_fid(f, OSD_FS_ROOT_OID);
139         return 0;
140 }
141
142 /*
143  * OSD object methods.
144  */
145
146 /*
147  * Concurrency: no concurrent access is possible that early in object
148  * life-cycle.
149  */
150 static struct lu_object *osd_object_alloc(const struct lu_env *env,
151                                           const struct lu_object_header *hdr,
152                                           struct lu_device *d)
153 {
154         struct osd_object *mo;
155
156         OBD_ALLOC_PTR(mo);
157         if (mo != NULL) {
158                 struct lu_object *l;
159
160                 l = &mo->oo_dt.do_lu;
161                 dt_object_init(&mo->oo_dt, NULL, d);
162                 mo->oo_dt.do_ops = &osd_obj_ea_ops;
163                 l->lo_ops = &osd_lu_obj_ops;
164                 init_rwsem(&mo->oo_sem);
165                 init_rwsem(&mo->oo_ext_idx_sem);
166                 spin_lock_init(&mo->oo_guard);
167                 return l;
168         } else {
169                 return NULL;
170         }
171 }
172
173 static inline int __osd_xattr_get(struct inode *inode, struct dentry *dentry,
174                                   const char *name, void *buf, int len)
175 {
176         dentry->d_inode = inode;
177         return inode->i_op->getxattr(dentry, name, buf, len);
178 }
179
180 int osd_get_lma(struct osd_thread_info *info, struct inode *inode,
181                 struct dentry *dentry, struct lustre_mdt_attrs *lma)
182 {
183         int rc;
184
185         rc = __osd_xattr_get(inode, dentry, XATTR_NAME_LMA, (void *)lma,
186                              sizeof(*lma));
187         if (rc == -ERANGE) {
188                 /* try with old lma size */
189                 rc = inode->i_op->getxattr(dentry, XATTR_NAME_LMA,
190                                            info->oti_mdt_attrs_old,
191                                            LMA_OLD_SIZE);
192                 if (rc > 0)
193                         memcpy(lma, info->oti_mdt_attrs_old, sizeof(*lma));
194         }
195         if (rc > 0) {
196                 /* Check LMA compatibility */
197                 if (lma->lma_incompat & ~cpu_to_le32(LMA_INCOMPAT_SUPP)) {
198                         CWARN("%.16s: unsupported incompat LMA feature(s) "
199                               "%lx/%#x\n",
200                               LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
201                               inode->i_ino, le32_to_cpu(lma->lma_incompat) &
202                                                         ~LMA_INCOMPAT_SUPP);
203                         rc = -ENOSYS;
204                 } else {
205                         lustre_lma_swab(lma);
206                         rc = 0;
207                 }
208         } else if (rc == 0) {
209                 rc = -ENODATA;
210         }
211
212         return rc;
213 }
214
215 /*
216  * retrieve object from backend ext fs.
217  **/
218 struct inode *osd_iget(struct osd_thread_info *info, struct osd_device *dev,
219                        struct osd_inode_id *id)
220 {
221         struct inode *inode = NULL;
222
223         inode = ldiskfs_iget(osd_sb(dev), id->oii_ino);
224         if (IS_ERR(inode)) {
225                 CDEBUG(D_INODE, "no inode: ino = %u, rc = %ld\n",
226                        id->oii_ino, PTR_ERR(inode));
227         } else if (id->oii_gen != OSD_OII_NOGEN &&
228                    inode->i_generation != id->oii_gen) {
229                 CDEBUG(D_INODE, "unmatched inode: ino = %u, gen0 = %u, "
230                        "gen1 = %u\n",
231                        id->oii_ino, id->oii_gen, inode->i_generation);
232                 iput(inode);
233                 inode = ERR_PTR(-ESTALE);
234         } else if (inode->i_nlink == 0) {
235                 /* due to parallel readdir and unlink,
236                 * we can have dead inode here. */
237                 CDEBUG(D_INODE, "stale inode: ino = %u\n", id->oii_ino);
238                 make_bad_inode(inode);
239                 iput(inode);
240                 inode = ERR_PTR(-ESTALE);
241         } else if (is_bad_inode(inode)) {
242                 CWARN("%.16s: bad inode: ino = %u\n",
243                 LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, id->oii_ino);
244                 iput(inode);
245                 inode = ERR_PTR(-ENOENT);
246         } else {
247                 if (id->oii_gen == OSD_OII_NOGEN)
248                         osd_id_gen(id, inode->i_ino, inode->i_generation);
249
250                 /* Do not update file c/mtime in ldiskfs.
251                  * NB: we don't have any lock to protect this because we don't
252                  * have reference on osd_object now, but contention with
253                  * another lookup + attr_set can't happen in the tiny window
254                  * between if (...) and set S_NOCMTIME. */
255                 if (!(inode->i_flags & S_NOCMTIME))
256                         inode->i_flags |= S_NOCMTIME;
257         }
258         return inode;
259 }
260
261 static struct inode *
262 osd_iget_fid(struct osd_thread_info *info, struct osd_device *dev,
263              struct osd_inode_id *id, struct lu_fid *fid)
264 {
265         struct lustre_mdt_attrs *lma   = &info->oti_mdt_attrs;
266         struct inode            *inode;
267         int                      rc;
268
269         inode = osd_iget(info, dev, id);
270         if (IS_ERR(inode))
271                 return inode;
272
273         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
274         if (rc == 0) {
275                 *fid = lma->lma_self_fid;
276         } else if (rc == -ENODATA) {
277                 if (unlikely(inode == osd_sb(dev)->s_root->d_inode))
278                         lu_local_obj_fid(fid, OSD_FS_ROOT_OID);
279                 else
280                         lu_igif_build(fid, inode->i_ino, inode->i_generation);
281         } else {
282                 iput(inode);
283                 inode = ERR_PTR(rc);
284         }
285         return inode;
286 }
287
288 static struct inode *
289 osd_iget_verify(struct osd_thread_info *info, struct osd_device *dev,
290                 struct osd_inode_id *id, const struct lu_fid *fid)
291 {
292         struct lustre_mdt_attrs *lma   = &info->oti_mdt_attrs;
293         struct inode            *inode;
294         int                      rc;
295
296         inode = osd_iget(info, dev, id);
297         if (IS_ERR(inode))
298                 return inode;
299
300         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
301         if (rc == -ENODATA)
302                 return inode;
303
304         if (rc != 0) {
305                 iput(inode);
306                 return ERR_PTR(rc);
307         }
308
309         if (!lu_fid_eq(fid, &lma->lma_self_fid)) {
310                 CDEBUG(D_LFSCK, "inconsistent obj: "DFID", %lu, "DFID"\n",
311                        PFID(&lma->lma_self_fid), inode->i_ino, PFID(fid));
312                 iput(inode);
313                 return ERR_PTR(-EREMCHG);
314         }
315
316         return inode;
317 }
318
319 static int osd_fid_lookup(const struct lu_env *env, struct osd_object *obj,
320                           const struct lu_fid *fid,
321                           const struct lu_object_conf *conf)
322 {
323         struct osd_thread_info *info;
324         struct lu_device       *ldev   = obj->oo_dt.do_lu.lo_dev;
325         struct osd_device      *dev;
326         struct osd_idmap_cache *oic;
327         struct osd_inode_id    *id;
328         struct inode           *inode;
329         struct osd_scrub       *scrub;
330         struct scrub_file      *sf;
331         int                     result;
332         int                     verify = 0;
333         ENTRY;
334
335         LINVRNT(osd_invariant(obj));
336         LASSERT(obj->oo_inode == NULL);
337         LASSERTF(fid_is_sane(fid) || fid_is_idif(fid), DFID, PFID(fid));
338
339         dev = osd_dev(ldev);
340         scrub = &dev->od_scrub;
341         sf = &scrub->os_file;
342         info = osd_oti_get(env);
343         LASSERT(info);
344         oic = &info->oti_cache;
345
346         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT))
347                 RETURN(-ENOENT);
348
349         /* Search order: 1. per-thread cache. */
350         if (lu_fid_eq(fid, &oic->oic_fid)) {
351                 id = &oic->oic_lid;
352                 goto iget;
353         }
354
355         id = &info->oti_id;
356         if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
357                 /* Search order: 2. OI scrub pending list. */
358                 result = osd_oii_lookup(dev, fid, id);
359                 if (result == 0)
360                         goto iget;
361         }
362
363         if (sf->sf_flags & SF_INCONSISTENT)
364                 verify = 1;
365
366         /*
367          * Objects are created as locking anchors or place holders for objects
368          * yet to be created. No need to osd_oi_lookup() at here because FID
369          * shouldn't never be re-used, if it's really a duplicate FID from
370          * unexpected reason, we should be able to detect it later by calling
371          * do_create->osd_oi_insert()
372          */
373         if (conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)
374                 GOTO(out, result = 0);
375
376         /* Search order: 3. OI files. */
377         result = osd_oi_lookup(info, dev, fid, id, true);
378         if (result == -ENOENT) {
379                 if (!fid_is_norm(fid) ||
380                     !ldiskfs_test_bit(osd_oi_fid2idx(dev,fid),
381                                       sf->sf_oi_bitmap))
382                         GOTO(out, result = 0);
383
384                 goto trigger;
385         }
386
387         if (result != 0)
388                 GOTO(out, result);
389
390 iget:
391         if (verify == 0)
392                 inode = osd_iget(info, dev, id);
393         else
394                 inode = osd_iget_verify(info, dev, id, fid);
395         if (IS_ERR(inode)) {
396                 result = PTR_ERR(inode);
397                 if (result == -ENOENT || result == -ESTALE) {
398                         fid_zero(&oic->oic_fid);
399                         result = 0;
400                 } else if (result == -EREMCHG) {
401
402 trigger:
403                         if (thread_is_running(&scrub->os_thread)) {
404                                 result = -EINPROGRESS;
405                         } else if (!dev->od_noscrub) {
406                                 result = osd_scrub_start(dev);
407                                 LCONSOLE_ERROR("%.16s: trigger OI scrub by RPC "
408                                                "for "DFID", rc = %d [1]\n",
409                                                LDISKFS_SB(osd_sb(dev))->s_es->\
410                                                s_volume_name,PFID(fid), result);
411                                 if (result == 0 || result == -EALREADY)
412                                         result = -EINPROGRESS;
413                                 else
414                                         result = -EREMCHG;
415                         }
416                 }
417
418                 GOTO(out, result);
419         }
420
421         obj->oo_inode = inode;
422         LASSERT(obj->oo_inode->i_sb == osd_sb(dev));
423
424         obj->oo_compat_dot_created = 1;
425         obj->oo_compat_dotdot_created = 1;
426
427         if (!S_ISDIR(inode->i_mode) || !ldiskfs_pdo) /* done */
428                 GOTO(out, result = 0);
429
430         LASSERT(obj->oo_hl_head == NULL);
431         obj->oo_hl_head = ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
432         if (obj->oo_hl_head == NULL) {
433                 obj->oo_inode = NULL;
434                 iput(inode);
435                 GOTO(out, result = -ENOMEM);
436         }
437         GOTO(out, result = 0);
438
439 out:
440         LINVRNT(osd_invariant(obj));
441         return result;
442 }
443
444 /*
445  * Concurrency: shouldn't matter.
446  */
447 static void osd_object_init0(struct osd_object *obj)
448 {
449         LASSERT(obj->oo_inode != NULL);
450         obj->oo_dt.do_body_ops = &osd_body_ops;
451         obj->oo_dt.do_lu.lo_header->loh_attr |=
452                 (LOHA_EXISTS | (obj->oo_inode->i_mode & S_IFMT));
453 }
454
455 /*
456  * Concurrency: no concurrent access is possible that early in object
457  * life-cycle.
458  */
459 static int osd_object_init(const struct lu_env *env, struct lu_object *l,
460                            const struct lu_object_conf *conf)
461 {
462         struct osd_object *obj = osd_obj(l);
463         int result;
464
465         LINVRNT(osd_invariant(obj));
466
467         if (fid_is_otable_it(&l->lo_header->loh_fid)) {
468                 obj->oo_dt.do_ops = &osd_obj_otable_it_ops;
469                 l->lo_header->loh_attr |= LOHA_EXISTS;
470                 return 0;
471         }
472
473         result = osd_fid_lookup(env, obj, lu_object_fid(l), conf);
474         obj->oo_dt.do_body_ops = &osd_body_ops_new;
475         if (result == 0 && obj->oo_inode != NULL)
476                 osd_object_init0(obj);
477
478         LINVRNT(osd_invariant(obj));
479         return result;
480 }
481
482 /*
483  * Concurrency: no concurrent access is possible that late in object
484  * life-cycle.
485  */
486 static void osd_object_free(const struct lu_env *env, struct lu_object *l)
487 {
488         struct osd_object *obj = osd_obj(l);
489
490         LINVRNT(osd_invariant(obj));
491
492         dt_object_fini(&obj->oo_dt);
493         if (obj->oo_hl_head != NULL)
494                 ldiskfs_htree_lock_head_free(obj->oo_hl_head);
495         OBD_FREE_PTR(obj);
496 }
497
498 /*
499  * Concurrency: no concurrent access is possible that late in object
500  * life-cycle.
501  */
502 static void osd_index_fini(struct osd_object *o)
503 {
504         struct iam_container *bag;
505
506         if (o->oo_dir != NULL) {
507                 bag = &o->oo_dir->od_container;
508                 if (o->oo_inode != NULL) {
509                         if (bag->ic_object == o->oo_inode)
510                                 iam_container_fini(bag);
511                 }
512                 OBD_FREE_PTR(o->oo_dir);
513                 o->oo_dir = NULL;
514         }
515 }
516
517 /*
518  * Concurrency: no concurrent access is possible that late in object
519  * life-cycle (for all existing callers, that is. New callers have to provide
520  * their own locking.)
521  */
522 static int osd_inode_unlinked(const struct inode *inode)
523 {
524         return inode->i_nlink == 0;
525 }
526
527 enum {
528         OSD_TXN_OI_DELETE_CREDITS    = 20,
529         OSD_TXN_INODE_DELETE_CREDITS = 20
530 };
531
532 /*
533  * Journal
534  */
535
536 #if OSD_THANDLE_STATS
537 /**
538  * Set time when the handle is allocated
539  */
540 static void osd_th_alloced(struct osd_thandle *oth)
541 {
542         oth->oth_alloced = cfs_time_current();
543 }
544
545 /**
546  * Set time when the handle started
547  */
548 static void osd_th_started(struct osd_thandle *oth)
549 {
550         oth->oth_started = cfs_time_current();
551 }
552
553 /**
554  * Helper function to convert time interval to microseconds packed in
555  * long int.
556  */
557 static long interval_to_usec(cfs_time_t start, cfs_time_t end)
558 {
559         struct timeval val;
560
561         cfs_duration_usec(cfs_time_sub(end, start), &val);
562         return val.tv_sec * 1000000 + val.tv_usec;
563 }
564
565 /**
566  * Check whether the we deal with this handle for too long.
567  */
568 static void __osd_th_check_slow(void *oth, struct osd_device *dev,
569                                 cfs_time_t alloced, cfs_time_t started,
570                                 cfs_time_t closed)
571 {
572         cfs_time_t now = cfs_time_current();
573
574         LASSERT(dev != NULL);
575
576         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_STARTING,
577                             interval_to_usec(alloced, started));
578         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_OPEN,
579                             interval_to_usec(started, closed));
580         lprocfs_counter_add(dev->od_stats, LPROC_OSD_THANDLE_CLOSING,
581                             interval_to_usec(closed, now));
582
583         if (cfs_time_before(cfs_time_add(alloced, cfs_time_seconds(30)), now)) {
584                 CWARN("transaction handle %p was open for too long: "
585                       "now "CFS_TIME_T" ,"
586                       "alloced "CFS_TIME_T" ,"
587                       "started "CFS_TIME_T" ,"
588                       "closed "CFS_TIME_T"\n",
589                       oth, now, alloced, started, closed);
590                 libcfs_debug_dumpstack(NULL);
591         }
592 }
593
594 #define OSD_CHECK_SLOW_TH(oth, dev, expr)                               \
595 {                                                                       \
596         cfs_time_t __closed = cfs_time_current();                       \
597         cfs_time_t __alloced = oth->oth_alloced;                        \
598         cfs_time_t __started = oth->oth_started;                        \
599                                                                         \
600         expr;                                                           \
601         __osd_th_check_slow(oth, dev, __alloced, __started, __closed);  \
602 }
603
604 #else /* OSD_THANDLE_STATS */
605
606 #define osd_th_alloced(h)                  do {} while(0)
607 #define osd_th_started(h)                  do {} while(0)
608 #define OSD_CHECK_SLOW_TH(oth, dev, expr)  expr
609
610 #endif /* OSD_THANDLE_STATS */
611
612 /*
613  * Concurrency: doesn't access mutable data.
614  */
615 static int osd_param_is_not_sane(const struct osd_device *dev,
616                                  const struct thandle *th)
617 {
618         struct osd_thandle *oh = container_of(th, typeof(*oh), ot_super);
619
620         return oh->ot_credits > osd_journal(dev)->j_max_transaction_buffers;
621 }
622
623 /*
624  * Concurrency: shouldn't matter.
625  */
626 #ifdef HAVE_LDISKFS_JOURNAL_CALLBACK_ADD
627 static void osd_trans_commit_cb(struct super_block *sb,
628                                 struct journal_callback *jcb, int error)
629 #else
630 static void osd_trans_commit_cb(struct journal_callback *jcb, int error)
631 #endif
632 {
633         struct osd_thandle *oh = container_of0(jcb, struct osd_thandle, ot_jcb);
634         struct thandle     *th  = &oh->ot_super;
635         struct lu_device   *lud = &th->th_dev->dd_lu_dev;
636         struct dt_txn_commit_cb *dcb, *tmp;
637
638         LASSERT(oh->ot_handle == NULL);
639
640         if (error)
641                 CERROR("transaction @0x%p commit error: %d\n", th, error);
642
643         dt_txn_hook_commit(th);
644
645         /* call per-transaction callbacks if any */
646         cfs_list_for_each_entry_safe(dcb, tmp, &oh->ot_dcb_list, dcb_linkage) {
647                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
648                          "commit callback entry: magic=%x name='%s'\n",
649                          dcb->dcb_magic, dcb->dcb_name);
650                 cfs_list_del_init(&dcb->dcb_linkage);
651                 dcb->dcb_func(NULL, th, dcb, error);
652         }
653
654         lu_ref_del_at(&lud->ld_reference, oh->ot_dev_link, "osd-tx", th);
655         lu_device_put(lud);
656         th->th_dev = NULL;
657
658         lu_context_exit(&th->th_ctx);
659         lu_context_fini(&th->th_ctx);
660         OBD_FREE_PTR(oh);
661 }
662
663 static struct thandle *osd_trans_create(const struct lu_env *env,
664                                         struct dt_device *d)
665 {
666         struct osd_thread_info *oti = osd_oti_get(env);
667         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
668         struct osd_thandle     *oh;
669         struct thandle         *th;
670         ENTRY;
671
672         /* on pending IO in this thread should left from prev. request */
673         LASSERT(cfs_atomic_read(&iobuf->dr_numreqs) == 0);
674
675         th = ERR_PTR(-ENOMEM);
676         OBD_ALLOC_GFP(oh, sizeof *oh, CFS_ALLOC_IO);
677         if (oh != NULL) {
678                 oh->ot_quota_trans = &oti->oti_quota_trans;
679                 memset(oh->ot_quota_trans, 0, sizeof(*oh->ot_quota_trans));
680                 th = &oh->ot_super;
681                 th->th_dev = d;
682                 th->th_result = 0;
683                 th->th_tags = LCT_TX_HANDLE;
684                 oh->ot_credits = 0;
685                 oti->oti_dev = osd_dt_dev(d);
686                 CFS_INIT_LIST_HEAD(&oh->ot_dcb_list);
687                 osd_th_alloced(oh);
688
689                 memset(oti->oti_declare_ops, 0, OSD_OT_MAX);
690                 memset(oti->oti_declare_ops_rb, 0, OSD_OT_MAX);
691                 memset(oti->oti_declare_ops_cred, 0, OSD_OT_MAX);
692                 oti->oti_rollback = false;
693         }
694         RETURN(th);
695 }
696
697 /*
698  * Concurrency: shouldn't matter.
699  */
700 int osd_trans_start(const struct lu_env *env, struct dt_device *d,
701                     struct thandle *th)
702 {
703         struct osd_thread_info *oti = osd_oti_get(env);
704         struct osd_device  *dev = osd_dt_dev(d);
705         handle_t           *jh;
706         struct osd_thandle *oh;
707         int rc;
708
709         ENTRY;
710
711         LASSERT(current->journal_info == NULL);
712
713         oh = container_of0(th, struct osd_thandle, ot_super);
714         LASSERT(oh != NULL);
715         LASSERT(oh->ot_handle == NULL);
716
717         rc = dt_txn_hook_start(env, d, th);
718         if (rc != 0)
719                 GOTO(out, rc);
720
721         if (unlikely(osd_param_is_not_sane(dev, th))) {
722                 static unsigned long last_printed;
723                 static int last_credits;
724
725                 CWARN("%.16s: too many transaction credits (%d > %d)\n",
726                       LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name,
727                       oh->ot_credits,
728                       osd_journal(dev)->j_max_transaction_buffers);
729 #ifdef OSD_TRACK_DECLARES
730                 CWARN("  create: %u/%u, delete: %u/%u, destroy: %u/%u\n",
731                       oti->oti_declare_ops[OSD_OT_CREATE],
732                       oti->oti_declare_ops_cred[OSD_OT_CREATE],
733                       oti->oti_declare_ops[OSD_OT_DELETE],
734                       oti->oti_declare_ops_cred[OSD_OT_DELETE],
735                       oti->oti_declare_ops[OSD_OT_DESTROY],
736                       oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
737                 CWARN("  attr_set: %u/%u, xattr_set: %u/%u\n",
738                       oti->oti_declare_ops[OSD_OT_ATTR_SET],
739                       oti->oti_declare_ops_cred[OSD_OT_ATTR_SET],
740                       oti->oti_declare_ops[OSD_OT_XATTR_SET],
741                       oti->oti_declare_ops_cred[OSD_OT_XATTR_SET]);
742                 CWARN("  write: %u/%u, punch: %u/%u, quota %u/%u\n",
743                       oti->oti_declare_ops[OSD_OT_WRITE],
744                       oti->oti_declare_ops_cred[OSD_OT_WRITE],
745                       oti->oti_declare_ops[OSD_OT_PUNCH],
746                       oti->oti_declare_ops_cred[OSD_OT_PUNCH],
747                       oti->oti_declare_ops[OSD_OT_QUOTA],
748                       oti->oti_declare_ops_cred[OSD_OT_QUOTA]);
749                 CWARN("  insert: %u/%u, delete: %u/%u\n",
750                       oti->oti_declare_ops[OSD_OT_INSERT],
751                       oti->oti_declare_ops_cred[OSD_OT_INSERT],
752                       oti->oti_declare_ops[OSD_OT_DESTROY],
753                       oti->oti_declare_ops_cred[OSD_OT_DESTROY]);
754                 CWARN("  ref_add: %u/%u, ref_del: %u/%u\n",
755                       oti->oti_declare_ops[OSD_OT_REF_ADD],
756                       oti->oti_declare_ops_cred[OSD_OT_REF_ADD],
757                       oti->oti_declare_ops[OSD_OT_REF_DEL],
758                       oti->oti_declare_ops_cred[OSD_OT_REF_DEL]);
759
760                 if (last_credits != oh->ot_credits &&
761                     time_after(jiffies, last_printed + 60 * HZ)) {
762                         libcfs_debug_dumpstack(NULL);
763                         last_credits = oh->ot_credits;
764                         last_printed = jiffies;
765                 }
766 #endif
767                 /* XXX Limit the credits to 'max_transaction_buffers', and
768                  *     let the underlying filesystem to catch the error if
769                  *     we really need so many credits.
770                  *
771                  *     This should be removed when we can calculate the
772                  *     credits precisely. */
773                 oh->ot_credits = osd_journal(dev)->j_max_transaction_buffers;
774         }
775
776         /*
777          * XXX temporary stuff. Some abstraction layer should
778          * be used.
779          */
780         jh = ldiskfs_journal_start_sb(osd_sb(dev), oh->ot_credits);
781         osd_th_started(oh);
782         if (!IS_ERR(jh)) {
783                 oh->ot_handle = jh;
784                 LASSERT(oti->oti_txns == 0);
785                 lu_context_init(&th->th_ctx, th->th_tags);
786                 lu_context_enter(&th->th_ctx);
787
788                 lu_device_get(&d->dd_lu_dev);
789                 oh->ot_dev_link = lu_ref_add(&d->dd_lu_dev.ld_reference,
790                                              "osd-tx", th);
791                 oti->oti_txns++;
792                 rc = 0;
793         } else {
794                 rc = PTR_ERR(jh);
795         }
796 out:
797         RETURN(rc);
798 }
799
800 /*
801  * Concurrency: shouldn't matter.
802  */
803 static int osd_trans_stop(const struct lu_env *env, struct thandle *th)
804 {
805         int                     rc = 0;
806         struct osd_thandle     *oh;
807         struct osd_thread_info *oti = osd_oti_get(env);
808         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
809         struct qsd_instance    *qsd = oti->oti_dev->od_quota_slave;
810         ENTRY;
811
812         oh = container_of0(th, struct osd_thandle, ot_super);
813
814         if (qsd != NULL)
815                 /* inform the quota slave device that the transaction is
816                  * stopping */
817                 qsd_op_end(env, qsd, oh->ot_quota_trans);
818         oh->ot_quota_trans = NULL;
819
820         if (oh->ot_handle != NULL) {
821                 handle_t *hdl = oh->ot_handle;
822
823                 /*
824                  * add commit callback
825                  * notice we don't do this in osd_trans_start()
826                  * as underlying transaction can change during truncate
827                  */
828                 osd_journal_callback_set(hdl, osd_trans_commit_cb,
829                                          &oh->ot_jcb);
830
831                 LASSERT(oti->oti_txns == 1);
832                 oti->oti_txns--;
833                 rc = dt_txn_hook_stop(env, th);
834                 if (rc != 0)
835                         CERROR("Failure in transaction hook: %d\n", rc);
836
837                 /* hook functions might modify th_sync */
838                 hdl->h_sync = th->th_sync;
839
840                 oh->ot_handle = NULL;
841                 OSD_CHECK_SLOW_TH(oh, oti->oti_dev,
842                                   rc = ldiskfs_journal_stop(hdl));
843                 if (rc != 0)
844                         CERROR("Failure to stop transaction: %d\n", rc);
845         } else {
846                 OBD_FREE_PTR(oh);
847         }
848
849         /* as we want IO to journal and data IO be concurrent, we don't block
850          * awaiting data IO completion in osd_do_bio(), instead we wait here
851          * once transaction is submitted to the journal. all reqular requests
852          * don't do direct IO (except read/write), thus this wait_event becomes
853          * no-op for them.
854          *
855          * IMPORTANT: we have to wait till any IO submited by the thread is
856          * completed otherwise iobuf may be corrupted by different request
857          */
858         cfs_wait_event(iobuf->dr_wait,
859                        cfs_atomic_read(&iobuf->dr_numreqs) == 0);
860         if (!rc)
861                 rc = iobuf->dr_error;
862
863         RETURN(rc);
864 }
865
866 static int osd_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
867 {
868         struct osd_thandle *oh = container_of0(th, struct osd_thandle,
869                                                ot_super);
870
871         LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
872         LASSERT(&dcb->dcb_func != NULL);
873         cfs_list_add(&dcb->dcb_linkage, &oh->ot_dcb_list);
874
875         return 0;
876 }
877
878 /*
879  * Called just before object is freed. Releases all resources except for
880  * object itself (that is released by osd_object_free()).
881  *
882  * Concurrency: no concurrent access is possible that late in object
883  * life-cycle.
884  */
885 static void osd_object_delete(const struct lu_env *env, struct lu_object *l)
886 {
887         struct osd_object *obj   = osd_obj(l);
888         struct inode      *inode = obj->oo_inode;
889
890         LINVRNT(osd_invariant(obj));
891
892         /*
893          * If object is unlinked remove fid->ino mapping from object index.
894          */
895
896         osd_index_fini(obj);
897         if (inode != NULL) {
898                 struct qsd_instance     *qsd = osd_obj2dev(obj)->od_quota_slave;
899                 qid_t                    uid = inode->i_uid;
900                 qid_t                    gid = inode->i_gid;
901
902                 iput(inode);
903                 obj->oo_inode = NULL;
904
905                 if (qsd != NULL) {
906                         struct osd_thread_info  *info = osd_oti_get(env);
907                         struct lquota_id_info   *qi = &info->oti_qi;
908
909                         /* Release granted quota to master if necessary */
910                         qi->lqi_id.qid_uid = uid;
911                         qsd_op_adjust(env, qsd, &qi->lqi_id, USRQUOTA);
912
913                         qi->lqi_id.qid_uid = gid;
914                         qsd_op_adjust(env, qsd, &qi->lqi_id, GRPQUOTA);
915                 }
916         }
917 }
918
919 /*
920  * Concurrency: ->loo_object_release() is called under site spin-lock.
921  */
922 static void osd_object_release(const struct lu_env *env,
923                                struct lu_object *l)
924 {
925 }
926
927 /*
928  * Concurrency: shouldn't matter.
929  */
930 static int osd_object_print(const struct lu_env *env, void *cookie,
931                             lu_printer_t p, const struct lu_object *l)
932 {
933         struct osd_object *o = osd_obj(l);
934         struct iam_descr  *d;
935
936         if (o->oo_dir != NULL)
937                 d = o->oo_dir->od_container.ic_descr;
938         else
939                 d = NULL;
940         return (*p)(env, cookie,
941                     LUSTRE_OSD_LDISKFS_NAME"-object@%p(i:%p:%lu/%u)[%s]",
942                     o, o->oo_inode,
943                     o->oo_inode ? o->oo_inode->i_ino : 0UL,
944                     o->oo_inode ? o->oo_inode->i_generation : 0,
945                     d ? d->id_ops->id_name : "plain");
946 }
947
948 /*
949  * Concurrency: shouldn't matter.
950  */
951 int osd_statfs(const struct lu_env *env, struct dt_device *d,
952                struct obd_statfs *sfs)
953 {
954         struct osd_device  *osd = osd_dt_dev(d);
955         struct super_block *sb = osd_sb(osd);
956         struct kstatfs     *ksfs;
957         int result = 0;
958
959         if (unlikely(osd->od_mnt == NULL))
960                 return -EINPROGRESS;
961
962         /* osd_lproc.c call this without env, allocate ksfs for that case */
963         if (unlikely(env == NULL)) {
964                 OBD_ALLOC_PTR(ksfs);
965                 if (ksfs == NULL)
966                         return -ENOMEM;
967         } else {
968                 ksfs = &osd_oti_get(env)->oti_ksfs;
969         }
970
971         spin_lock(&osd->od_osfs_lock);
972         /* cache 1 second */
973         if (cfs_time_before_64(osd->od_osfs_age, cfs_time_shift_64(-1))) {
974                 result = sb->s_op->statfs(sb->s_root, ksfs);
975                 if (likely(result == 0)) { /* N.B. statfs can't really fail */
976                         osd->od_osfs_age = cfs_time_current_64();
977                         statfs_pack(&osd->od_statfs, ksfs);
978                         if (sb->s_flags & MS_RDONLY)
979                                 sfs->os_state = OS_STATE_READONLY;
980                 }
981         }
982
983         if (likely(result == 0))
984                 *sfs = osd->od_statfs;
985         spin_unlock(&osd->od_osfs_lock);
986
987         if (unlikely(env == NULL))
988                 OBD_FREE_PTR(ksfs);
989
990         return result;
991 }
992
993 /**
994  * Estimate space needed for file creations. We assume the largest filename
995  * which is 2^64 - 1, hence a filename of 20 chars.
996  * This is 28 bytes per object which is 28MB for 1M objects ... no so bad.
997  */
998 #ifdef __LDISKFS_DIR_REC_LEN
999 #define PER_OBJ_USAGE __LDISKFS_DIR_REC_LEN(20)
1000 #else
1001 #define PER_OBJ_USAGE LDISKFS_DIR_REC_LEN(20)
1002 #endif
1003
1004 /*
1005  * Concurrency: doesn't access mutable data.
1006  */
1007 static void osd_conf_get(const struct lu_env *env,
1008                          const struct dt_device *dev,
1009                          struct dt_device_param *param)
1010 {
1011         struct super_block *sb = osd_sb(osd_dt_dev(dev));
1012
1013         /*
1014          * XXX should be taken from not-yet-existing fs abstraction layer.
1015          */
1016         param->ddp_mnt = osd_dt_dev(dev)->od_mnt;
1017         param->ddp_max_name_len = LDISKFS_NAME_LEN;
1018         param->ddp_max_nlink    = LDISKFS_LINK_MAX;
1019         param->ddp_block_shift  = sb->s_blocksize_bits;
1020         param->ddp_mount_type     = LDD_MT_LDISKFS;
1021         param->ddp_maxbytes       = sb->s_maxbytes;
1022         /* Overhead estimate should be fairly accurate, so we really take a tiny
1023          * error margin which also avoids fragmenting the filesystem too much */
1024         param->ddp_grant_reserved = 2; /* end up to be 1.9% after conversion */
1025         /* inode are statically allocated, so per-inode space consumption
1026          * is the space consumed by the directory entry */
1027         param->ddp_inodespace     = PER_OBJ_USAGE;
1028         /* per-fragment overhead to be used by the client code */
1029         param->ddp_grant_frag     = 6 * LDISKFS_BLOCK_SIZE(sb);
1030         param->ddp_mntopts      = 0;
1031         if (test_opt(sb, XATTR_USER))
1032                 param->ddp_mntopts |= MNTOPT_USERXATTR;
1033         if (test_opt(sb, POSIX_ACL))
1034                 param->ddp_mntopts |= MNTOPT_ACL;
1035
1036 #if defined(LDISKFS_FEATURE_INCOMPAT_EA_INODE)
1037         if (LDISKFS_HAS_INCOMPAT_FEATURE(sb, LDISKFS_FEATURE_INCOMPAT_EA_INODE))
1038                 param->ddp_max_ea_size = LDISKFS_XATTR_MAX_LARGE_EA_SIZE;
1039         else
1040 #endif
1041                 param->ddp_max_ea_size = sb->s_blocksize;
1042
1043 }
1044
1045 /*
1046  * Concurrency: shouldn't matter.
1047  */
1048 static int osd_sync(const struct lu_env *env, struct dt_device *d)
1049 {
1050         CDEBUG(D_HA, "syncing OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1051         return ldiskfs_force_commit(osd_sb(osd_dt_dev(d)));
1052 }
1053
1054 /**
1055  * Start commit for OSD device.
1056  *
1057  * An implementation of dt_commit_async method for OSD device.
1058  * Asychronously starts underlayng fs sync and thereby a transaction
1059  * commit.
1060  *
1061  * \param env environment
1062  * \param d dt device
1063  *
1064  * \see dt_device_operations
1065  */
1066 static int osd_commit_async(const struct lu_env *env,
1067                             struct dt_device *d)
1068 {
1069         struct super_block *s = osd_sb(osd_dt_dev(d));
1070         ENTRY;
1071
1072         CDEBUG(D_HA, "async commit OSD %s\n", LUSTRE_OSD_LDISKFS_NAME);
1073         RETURN(s->s_op->sync_fs(s, 0));
1074 }
1075
1076 /*
1077  * Concurrency: shouldn't matter.
1078  */
1079
1080 static int osd_ro(const struct lu_env *env, struct dt_device *d)
1081 {
1082         struct super_block *sb = osd_sb(osd_dt_dev(d));
1083         int rc;
1084         ENTRY;
1085
1086         CERROR("*** setting %s read-only ***\n", osd_dt_dev(d)->od_svname);
1087
1088         rc = __lvfs_set_rdonly(sb->s_bdev, LDISKFS_SB(sb)->journal_bdev);
1089         RETURN(rc);
1090 }
1091
1092 /*
1093  * Concurrency: serialization provided by callers.
1094  */
1095 static int osd_init_capa_ctxt(const struct lu_env *env, struct dt_device *d,
1096                               int mode, unsigned long timeout, __u32 alg,
1097                               struct lustre_capa_key *keys)
1098 {
1099         struct osd_device *dev = osd_dt_dev(d);
1100         ENTRY;
1101
1102         dev->od_fl_capa = mode;
1103         dev->od_capa_timeout = timeout;
1104         dev->od_capa_alg = alg;
1105         dev->od_capa_keys = keys;
1106         RETURN(0);
1107 }
1108
1109 /**
1110  * Note: we do not count into QUOTA here.
1111  * If we mount with --data_journal we may need more.
1112  */
1113 const int osd_dto_credits_noquota[DTO_NR] = {
1114         /**
1115          * Insert/Delete.
1116          * INDEX_EXTRA_TRANS_BLOCKS(8) +
1117          * SINGLEDATA_TRANS_BLOCKS(8)
1118          * XXX Note: maybe iam need more, since iam have more level than
1119          *           EXT3 htree.
1120          */
1121         [DTO_INDEX_INSERT]  = 16,
1122         [DTO_INDEX_DELETE]  = 16,
1123         /**
1124          * Used for OI scrub
1125          */
1126         [DTO_INDEX_UPDATE]  = 16,
1127         /**
1128          * Create a object. The same as create object in EXT3.
1129          * DATA_TRANS_BLOCKS(14) +
1130          * INDEX_EXTRA_BLOCKS(8) +
1131          * 3(inode bits, groups, GDT)
1132          */
1133         [DTO_OBJECT_CREATE] = 25,
1134         /**
1135          * XXX: real credits to be fixed
1136          */
1137         [DTO_OBJECT_DELETE] = 25,
1138         /**
1139          * Attr set credits (inode)
1140          */
1141         [DTO_ATTR_SET_BASE] = 1,
1142         /**
1143          * Xattr set. The same as xattr of EXT3.
1144          * DATA_TRANS_BLOCKS(14)
1145          * XXX Note: in original MDS implmentation INDEX_EXTRA_TRANS_BLOCKS
1146          * are also counted in. Do not know why?
1147          */
1148         [DTO_XATTR_SET]     = 14,
1149         [DTO_LOG_REC]       = 14,
1150         /**
1151          * credits for inode change during write.
1152          */
1153         [DTO_WRITE_BASE]    = 3,
1154         /**
1155          * credits for single block write.
1156          */
1157         [DTO_WRITE_BLOCK]   = 14,
1158         /**
1159          * Attr set credits for chown.
1160          * This is extra credits for setattr, and it is null without quota
1161          */
1162         [DTO_ATTR_SET_CHOWN]= 0
1163 };
1164
1165 static const struct dt_device_operations osd_dt_ops = {
1166         .dt_root_get       = osd_root_get,
1167         .dt_statfs         = osd_statfs,
1168         .dt_trans_create   = osd_trans_create,
1169         .dt_trans_start    = osd_trans_start,
1170         .dt_trans_stop     = osd_trans_stop,
1171         .dt_trans_cb_add   = osd_trans_cb_add,
1172         .dt_conf_get       = osd_conf_get,
1173         .dt_sync           = osd_sync,
1174         .dt_ro             = osd_ro,
1175         .dt_commit_async   = osd_commit_async,
1176         .dt_init_capa_ctxt = osd_init_capa_ctxt,
1177 };
1178
1179 static void osd_object_read_lock(const struct lu_env *env,
1180                                  struct dt_object *dt, unsigned role)
1181 {
1182         struct osd_object *obj = osd_dt_obj(dt);
1183         struct osd_thread_info *oti = osd_oti_get(env);
1184
1185         LINVRNT(osd_invariant(obj));
1186
1187         LASSERT(obj->oo_owner != env);
1188         down_read_nested(&obj->oo_sem, role);
1189
1190         LASSERT(obj->oo_owner == NULL);
1191         oti->oti_r_locks++;
1192 }
1193
1194 static void osd_object_write_lock(const struct lu_env *env,
1195                                   struct dt_object *dt, unsigned role)
1196 {
1197         struct osd_object *obj = osd_dt_obj(dt);
1198         struct osd_thread_info *oti = osd_oti_get(env);
1199
1200         LINVRNT(osd_invariant(obj));
1201
1202         LASSERT(obj->oo_owner != env);
1203         down_write_nested(&obj->oo_sem, role);
1204
1205         LASSERT(obj->oo_owner == NULL);
1206         obj->oo_owner = env;
1207         oti->oti_w_locks++;
1208 }
1209
1210 static void osd_object_read_unlock(const struct lu_env *env,
1211                                    struct dt_object *dt)
1212 {
1213         struct osd_object *obj = osd_dt_obj(dt);
1214         struct osd_thread_info *oti = osd_oti_get(env);
1215
1216         LINVRNT(osd_invariant(obj));
1217
1218         LASSERT(oti->oti_r_locks > 0);
1219         oti->oti_r_locks--;
1220         up_read(&obj->oo_sem);
1221 }
1222
1223 static void osd_object_write_unlock(const struct lu_env *env,
1224                                     struct dt_object *dt)
1225 {
1226         struct osd_object *obj = osd_dt_obj(dt);
1227         struct osd_thread_info *oti = osd_oti_get(env);
1228
1229         LINVRNT(osd_invariant(obj));
1230
1231         LASSERT(obj->oo_owner == env);
1232         LASSERT(oti->oti_w_locks > 0);
1233         oti->oti_w_locks--;
1234         obj->oo_owner = NULL;
1235         up_write(&obj->oo_sem);
1236 }
1237
1238 static int osd_object_write_locked(const struct lu_env *env,
1239                                    struct dt_object *dt)
1240 {
1241         struct osd_object *obj = osd_dt_obj(dt);
1242
1243         LINVRNT(osd_invariant(obj));
1244
1245         return obj->oo_owner == env;
1246 }
1247
1248 static int capa_is_sane(const struct lu_env *env,
1249                         struct osd_device *dev,
1250                         struct lustre_capa *capa,
1251                         struct lustre_capa_key *keys)
1252 {
1253         struct osd_thread_info *oti = osd_oti_get(env);
1254         struct lustre_capa *tcapa = &oti->oti_capa;
1255         struct obd_capa *oc;
1256         int i, rc = 0;
1257         ENTRY;
1258
1259         oc = capa_lookup(dev->od_capa_hash, capa, 0);
1260         if (oc) {
1261                 if (capa_is_expired(oc)) {
1262                         DEBUG_CAPA(D_ERROR, capa, "expired");
1263                         rc = -ESTALE;
1264                 }
1265                 capa_put(oc);
1266                 RETURN(rc);
1267         }
1268
1269         if (capa_is_expired_sec(capa)) {
1270                 DEBUG_CAPA(D_ERROR, capa, "expired");
1271                 RETURN(-ESTALE);
1272         }
1273
1274         spin_lock(&capa_lock);
1275         for (i = 0; i < 2; i++) {
1276                 if (keys[i].lk_keyid == capa->lc_keyid) {
1277                         oti->oti_capa_key = keys[i];
1278                         break;
1279                 }
1280         }
1281         spin_unlock(&capa_lock);
1282
1283         if (i == 2) {
1284                 DEBUG_CAPA(D_ERROR, capa, "no matched capa key");
1285                 RETURN(-ESTALE);
1286         }
1287
1288         rc = capa_hmac(tcapa->lc_hmac, capa, oti->oti_capa_key.lk_key);
1289         if (rc)
1290                 RETURN(rc);
1291
1292         if (memcmp(tcapa->lc_hmac, capa->lc_hmac, sizeof(capa->lc_hmac))) {
1293                 DEBUG_CAPA(D_ERROR, capa, "HMAC mismatch");
1294                 RETURN(-EACCES);
1295         }
1296
1297         oc = capa_add(dev->od_capa_hash, capa);
1298         capa_put(oc);
1299
1300         RETURN(0);
1301 }
1302
1303 int osd_object_auth(const struct lu_env *env, struct dt_object *dt,
1304                     struct lustre_capa *capa, __u64 opc)
1305 {
1306         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1307         struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1308         struct md_capainfo *ci;
1309         int rc;
1310
1311         if (!dev->od_fl_capa)
1312                 return 0;
1313
1314         if (capa == BYPASS_CAPA)
1315                 return 0;
1316
1317         ci = md_capainfo(env);
1318         if (unlikely(!ci))
1319                 return 0;
1320
1321         if (ci->mc_auth == LC_ID_NONE)
1322                 return 0;
1323
1324         if (!capa) {
1325                 CERROR("no capability is provided for fid "DFID"\n", PFID(fid));
1326                 return -EACCES;
1327         }
1328
1329         if (!lu_fid_eq(fid, &capa->lc_fid)) {
1330                 DEBUG_CAPA(D_ERROR, capa, "fid "DFID" mismatch with",
1331                            PFID(fid));
1332                 return -EACCES;
1333         }
1334
1335         if (!capa_opc_supported(capa, opc)) {
1336                 DEBUG_CAPA(D_ERROR, capa, "opc "LPX64" not supported by", opc);
1337                 return -EACCES;
1338         }
1339
1340         if ((rc = capa_is_sane(env, dev, capa, dev->od_capa_keys))) {
1341                 DEBUG_CAPA(D_ERROR, capa, "insane (rc %d)", rc);
1342                 return -EACCES;
1343         }
1344
1345         return 0;
1346 }
1347
1348 static struct timespec *osd_inode_time(const struct lu_env *env,
1349                                        struct inode *inode, __u64 seconds)
1350 {
1351         struct osd_thread_info  *oti = osd_oti_get(env);
1352         struct timespec         *t   = &oti->oti_time;
1353
1354         t->tv_sec = seconds;
1355         t->tv_nsec = 0;
1356         *t = timespec_trunc(*t, inode->i_sb->s_time_gran);
1357         return t;
1358 }
1359
1360
1361 static void osd_inode_getattr(const struct lu_env *env,
1362                               struct inode *inode, struct lu_attr *attr)
1363 {
1364         attr->la_valid      |= LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1365                                LA_SIZE | LA_BLOCKS | LA_UID | LA_GID |
1366                                LA_FLAGS | LA_NLINK | LA_RDEV | LA_BLKSIZE;
1367
1368         attr->la_atime      = LTIME_S(inode->i_atime);
1369         attr->la_mtime      = LTIME_S(inode->i_mtime);
1370         attr->la_ctime      = LTIME_S(inode->i_ctime);
1371         attr->la_mode       = inode->i_mode;
1372         attr->la_size       = i_size_read(inode);
1373         attr->la_blocks     = inode->i_blocks;
1374         attr->la_uid        = inode->i_uid;
1375         attr->la_gid        = inode->i_gid;
1376         attr->la_flags      = LDISKFS_I(inode)->i_flags;
1377         attr->la_nlink      = inode->i_nlink;
1378         attr->la_rdev       = inode->i_rdev;
1379         attr->la_blksize    = 1 << inode->i_blkbits;
1380         attr->la_blkbits    = inode->i_blkbits;
1381 }
1382
1383 static int osd_attr_get(const struct lu_env *env,
1384                         struct dt_object *dt,
1385                         struct lu_attr *attr,
1386                         struct lustre_capa *capa)
1387 {
1388         struct osd_object *obj = osd_dt_obj(dt);
1389
1390         LASSERT(dt_object_exists(dt));
1391         LINVRNT(osd_invariant(obj));
1392
1393         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
1394                 return -EACCES;
1395
1396         spin_lock(&obj->oo_guard);
1397         osd_inode_getattr(env, obj->oo_inode, attr);
1398         spin_unlock(&obj->oo_guard);
1399         return 0;
1400 }
1401
1402 static int osd_declare_attr_set(const struct lu_env *env,
1403                                 struct dt_object *dt,
1404                                 const struct lu_attr *attr,
1405                                 struct thandle *handle)
1406 {
1407         struct osd_thandle     *oh;
1408         struct osd_object      *obj;
1409         struct osd_thread_info *info = osd_oti_get(env);
1410         struct lquota_id_info  *qi = &info->oti_qi;
1411         long long               bspace;
1412         int                     rc = 0;
1413         bool                    allocated;
1414         ENTRY;
1415
1416         LASSERT(dt != NULL);
1417         LASSERT(handle != NULL);
1418
1419         obj = osd_dt_obj(dt);
1420         LASSERT(osd_invariant(obj));
1421
1422         oh = container_of0(handle, struct osd_thandle, ot_super);
1423         LASSERT(oh->ot_handle == NULL);
1424
1425         osd_trans_declare_op(env, oh, OSD_OT_ATTR_SET,
1426                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
1427
1428         if (attr == NULL || obj->oo_inode == NULL)
1429                 RETURN(rc);
1430
1431         bspace   = obj->oo_inode->i_blocks;
1432         bspace <<= obj->oo_inode->i_sb->s_blocksize_bits;
1433         bspace   = toqb(bspace);
1434
1435         /* Changing ownership is always preformed by super user, it should not
1436          * fail with EDQUOT.
1437          *
1438          * We still need to call the osd_declare_qid() to calculate the journal
1439          * credits for updating quota accounting files and to trigger quota
1440          * space adjustment once the operation is completed.*/
1441         if ((attr->la_valid & LA_UID) != 0 &&
1442              attr->la_uid != obj->oo_inode->i_uid) {
1443                 qi->lqi_type = USRQUOTA;
1444
1445                 /* inode accounting */
1446                 qi->lqi_is_blk = false;
1447
1448                 /* one more inode for the new owner ... */
1449                 qi->lqi_id.qid_uid = attr->la_uid;
1450                 qi->lqi_space      = 1;
1451                 allocated = (attr->la_uid == 0) ? true : false;
1452                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1453                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1454                         rc = 0;
1455                 if (rc)
1456                         RETURN(rc);
1457
1458                 /* and one less inode for the current uid */
1459                 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1460                 qi->lqi_space      = -1;
1461                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1462                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1463                         rc = 0;
1464                 if (rc)
1465                         RETURN(rc);
1466
1467                 /* block accounting */
1468                 qi->lqi_is_blk = true;
1469
1470                 /* more blocks for the new owner ... */
1471                 qi->lqi_id.qid_uid = attr->la_uid;
1472                 qi->lqi_space      = bspace;
1473                 allocated = (attr->la_uid == 0) ? true : false;
1474                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1475                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1476                         rc = 0;
1477                 if (rc)
1478                         RETURN(rc);
1479
1480                 /* and finally less blocks for the current owner */
1481                 qi->lqi_id.qid_uid = obj->oo_inode->i_uid;
1482                 qi->lqi_space      = -bspace;
1483                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1484                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1485                         rc = 0;
1486                 if (rc)
1487                         RETURN(rc);
1488         }
1489
1490         if (attr->la_valid & LA_GID &&
1491             attr->la_gid != obj->oo_inode->i_gid) {
1492                 qi->lqi_type = GRPQUOTA;
1493
1494                 /* inode accounting */
1495                 qi->lqi_is_blk = false;
1496
1497                 /* one more inode for the new group owner ... */
1498                 qi->lqi_id.qid_gid = attr->la_gid;
1499                 qi->lqi_space      = 1;
1500                 allocated = (attr->la_gid == 0) ? true : false;
1501                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1502                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1503                         rc = 0;
1504                 if (rc)
1505                         RETURN(rc);
1506
1507                 /* and one less inode for the current gid */
1508                 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1509                 qi->lqi_space      = -1;
1510                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1511                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1512                         rc = 0;
1513                 if (rc)
1514                         RETURN(rc);
1515
1516                 /* block accounting */
1517                 qi->lqi_is_blk = true;
1518
1519                 /* more blocks for the new owner ... */
1520                 qi->lqi_id.qid_gid = attr->la_gid;
1521                 qi->lqi_space      = bspace;
1522                 allocated = (attr->la_gid == 0) ? true : false;
1523                 rc = osd_declare_qid(env, oh, qi, allocated, NULL);
1524                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1525                         rc = 0;
1526                 if (rc)
1527                         RETURN(rc);
1528
1529                 /* and finally less blocks for the current owner */
1530                 qi->lqi_id.qid_gid = obj->oo_inode->i_gid;
1531                 qi->lqi_space      = -bspace;
1532                 rc = osd_declare_qid(env, oh, qi, true, NULL);
1533                 if (rc == -EDQUOT || rc == -EINPROGRESS)
1534                         rc = 0;
1535                 if (rc)
1536                         RETURN(rc);
1537         }
1538
1539         RETURN(rc);
1540 }
1541
1542 static int osd_inode_setattr(const struct lu_env *env,
1543                              struct inode *inode, const struct lu_attr *attr)
1544 {
1545         __u64 bits;
1546
1547         bits = attr->la_valid;
1548
1549         if (bits & LA_ATIME)
1550                 inode->i_atime  = *osd_inode_time(env, inode, attr->la_atime);
1551         if (bits & LA_CTIME)
1552                 inode->i_ctime  = *osd_inode_time(env, inode, attr->la_ctime);
1553         if (bits & LA_MTIME)
1554                 inode->i_mtime  = *osd_inode_time(env, inode, attr->la_mtime);
1555         if (bits & LA_SIZE) {
1556                 LDISKFS_I(inode)->i_disksize = attr->la_size;
1557                 i_size_write(inode, attr->la_size);
1558         }
1559
1560 #if 0
1561         /* OSD should not change "i_blocks" which is used by quota.
1562          * "i_blocks" should be changed by ldiskfs only. */
1563         if (bits & LA_BLOCKS)
1564                 inode->i_blocks = attr->la_blocks;
1565 #endif
1566         if (bits & LA_MODE)
1567                 inode->i_mode   = (inode->i_mode & S_IFMT) |
1568                         (attr->la_mode & ~S_IFMT);
1569         if (bits & LA_UID)
1570                 inode->i_uid    = attr->la_uid;
1571         if (bits & LA_GID)
1572                 inode->i_gid    = attr->la_gid;
1573         if (bits & LA_NLINK)
1574                 set_nlink(inode, attr->la_nlink);
1575         if (bits & LA_RDEV)
1576                 inode->i_rdev   = attr->la_rdev;
1577
1578         if (bits & LA_FLAGS) {
1579                 /* always keep S_NOCMTIME */
1580                 inode->i_flags = ll_ext_to_inode_flags(attr->la_flags) |
1581                                  S_NOCMTIME;
1582         }
1583         return 0;
1584 }
1585
1586 static int osd_quota_transfer(struct inode *inode, const struct lu_attr *attr)
1587 {
1588         if ((attr->la_valid & LA_UID && attr->la_uid != inode->i_uid) ||
1589             (attr->la_valid & LA_GID && attr->la_gid != inode->i_gid)) {
1590                 struct iattr    iattr;
1591                 int             rc;
1592
1593                 iattr.ia_valid = 0;
1594                 if (attr->la_valid & LA_UID)
1595                         iattr.ia_valid |= ATTR_UID;
1596                 if (attr->la_valid & LA_GID)
1597                         iattr.ia_valid |= ATTR_GID;
1598                 iattr.ia_uid = attr->la_uid;
1599                 iattr.ia_gid = attr->la_gid;
1600
1601                 rc = ll_vfs_dq_transfer(inode, &iattr);
1602                 if (rc) {
1603                         CERROR("%s: quota transfer failed: rc = %d. Is quota "
1604                                "enforcement enabled on the ldiskfs filesystem?",
1605                                inode->i_sb->s_id, rc);
1606                         return rc;
1607                 }
1608         }
1609         return 0;
1610 }
1611
1612 static int osd_attr_set(const struct lu_env *env,
1613                         struct dt_object *dt,
1614                         const struct lu_attr *attr,
1615                         struct thandle *handle,
1616                         struct lustre_capa *capa)
1617 {
1618         struct osd_object *obj = osd_dt_obj(dt);
1619         struct inode      *inode;
1620         int rc;
1621
1622         LASSERT(handle != NULL);
1623         LASSERT(dt_object_exists(dt));
1624         LASSERT(osd_invariant(obj));
1625
1626         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
1627                 return -EACCES;
1628
1629         osd_trans_exec_op(env, handle, OSD_OT_ATTR_SET);
1630
1631         inode = obj->oo_inode;
1632         ll_vfs_dq_init(inode);
1633
1634         rc = osd_quota_transfer(inode, attr);
1635         if (rc)
1636                 return rc;
1637
1638         spin_lock(&obj->oo_guard);
1639         rc = osd_inode_setattr(env, inode, attr);
1640         spin_unlock(&obj->oo_guard);
1641
1642         if (!rc)
1643                 inode->i_sb->s_op->dirty_inode(inode);
1644         return rc;
1645 }
1646
1647 struct dentry *osd_child_dentry_get(const struct lu_env *env,
1648                                     struct osd_object *obj,
1649                                     const char *name, const int namelen)
1650 {
1651         return osd_child_dentry_by_inode(env, obj->oo_inode, name, namelen);
1652 }
1653
1654 static int osd_mkfile(struct osd_thread_info *info, struct osd_object *obj,
1655                       cfs_umode_t mode,
1656                       struct dt_allocation_hint *hint,
1657                       struct thandle *th)
1658 {
1659         int result;
1660         struct osd_device  *osd = osd_obj2dev(obj);
1661         struct osd_thandle *oth;
1662         struct dt_object   *parent = NULL;
1663         struct inode       *inode;
1664
1665         LINVRNT(osd_invariant(obj));
1666         LASSERT(obj->oo_inode == NULL);
1667         LASSERT(obj->oo_hl_head == NULL);
1668
1669         if (S_ISDIR(mode) && ldiskfs_pdo) {
1670                 obj->oo_hl_head =ldiskfs_htree_lock_head_alloc(HTREE_HBITS_DEF);
1671                 if (obj->oo_hl_head == NULL)
1672                         return -ENOMEM;
1673         }
1674
1675         oth = container_of(th, struct osd_thandle, ot_super);
1676         LASSERT(oth->ot_handle->h_transaction != NULL);
1677
1678         if (hint && hint->dah_parent)
1679                 parent = hint->dah_parent;
1680
1681         inode = ldiskfs_create_inode(oth->ot_handle,
1682                                      parent ? osd_dt_obj(parent)->oo_inode :
1683                                               osd_sb(osd)->s_root->d_inode,
1684                                      mode);
1685         if (!IS_ERR(inode)) {
1686                 /* Do not update file c/mtime in ldiskfs.
1687                  * NB: don't need any lock because no contention at this
1688                  * early stage */
1689                 inode->i_flags |= S_NOCMTIME;
1690
1691                 /* For new created object, it must be consistent,
1692                  * and it is unnecessary to scrub against it. */
1693                 ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
1694                 obj->oo_inode = inode;
1695                 result = 0;
1696         } else {
1697                 if (obj->oo_hl_head != NULL) {
1698                         ldiskfs_htree_lock_head_free(obj->oo_hl_head);
1699                         obj->oo_hl_head = NULL;
1700                 }
1701                 result = PTR_ERR(inode);
1702         }
1703         LINVRNT(osd_invariant(obj));
1704         return result;
1705 }
1706
1707 enum {
1708         OSD_NAME_LEN = 255
1709 };
1710
1711 static int osd_mkdir(struct osd_thread_info *info, struct osd_object *obj,
1712                      struct lu_attr *attr,
1713                      struct dt_allocation_hint *hint,
1714                      struct dt_object_format *dof,
1715                      struct thandle *th)
1716 {
1717         int result;
1718         struct osd_thandle *oth;
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
1727         return result;
1728 }
1729
1730 static int osd_mk_index(struct osd_thread_info *info, struct osd_object *obj,
1731                         struct lu_attr *attr,
1732                         struct dt_allocation_hint *hint,
1733                         struct dt_object_format *dof,
1734                         struct thandle *th)
1735 {
1736         int result;
1737         struct osd_thandle *oth;
1738         const struct dt_index_features *feat = dof->u.dof_idx.di_feat;
1739
1740         __u32 mode = (attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX));
1741
1742         LASSERT(S_ISREG(attr->la_mode));
1743
1744         oth = container_of(th, struct osd_thandle, ot_super);
1745         LASSERT(oth->ot_handle->h_transaction != NULL);
1746
1747         result = osd_mkfile(info, obj, mode, hint, th);
1748         if (result == 0) {
1749                 LASSERT(obj->oo_inode != NULL);
1750                 if (feat->dif_flags & DT_IND_VARKEY)
1751                         result = iam_lvar_create(obj->oo_inode,
1752                                                  feat->dif_keysize_max,
1753                                                  feat->dif_ptrsize,
1754                                                  feat->dif_recsize_max,
1755                                                  oth->ot_handle);
1756                 else
1757                         result = iam_lfix_create(obj->oo_inode,
1758                                                  feat->dif_keysize_max,
1759                                                  feat->dif_ptrsize,
1760                                                  feat->dif_recsize_max,
1761                                                  oth->ot_handle);
1762
1763         }
1764         return result;
1765 }
1766
1767 static int osd_mkreg(struct osd_thread_info *info, struct osd_object *obj,
1768                      struct lu_attr *attr,
1769                      struct dt_allocation_hint *hint,
1770                      struct dt_object_format *dof,
1771                      struct thandle *th)
1772 {
1773         LASSERT(S_ISREG(attr->la_mode));
1774         return osd_mkfile(info, obj, (attr->la_mode &
1775                                (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1776 }
1777
1778 static int osd_mksym(struct osd_thread_info *info, struct osd_object *obj,
1779                      struct lu_attr *attr,
1780                      struct dt_allocation_hint *hint,
1781                      struct dt_object_format *dof,
1782                      struct thandle *th)
1783 {
1784         LASSERT(S_ISLNK(attr->la_mode));
1785         return osd_mkfile(info, obj, (attr->la_mode &
1786                               (S_IFMT | S_IALLUGO | S_ISVTX)), hint, th);
1787 }
1788
1789 static int osd_mknod(struct osd_thread_info *info, struct osd_object *obj,
1790                      struct lu_attr *attr,
1791                      struct dt_allocation_hint *hint,
1792                      struct dt_object_format *dof,
1793                      struct thandle *th)
1794 {
1795         cfs_umode_t mode = attr->la_mode & (S_IFMT | S_IALLUGO | S_ISVTX);
1796         int result;
1797
1798         LINVRNT(osd_invariant(obj));
1799         LASSERT(obj->oo_inode == NULL);
1800         LASSERT(S_ISCHR(mode) || S_ISBLK(mode) ||
1801                 S_ISFIFO(mode) || S_ISSOCK(mode));
1802
1803         result = osd_mkfile(info, obj, mode, hint, th);
1804         if (result == 0) {
1805                 LASSERT(obj->oo_inode != NULL);
1806                 /*
1807                  * This inode should be marked dirty for i_rdev.  Currently
1808                  * that is done in the osd_attr_init().
1809                  */
1810                 init_special_inode(obj->oo_inode, obj->oo_inode->i_mode,
1811                                    attr->la_rdev);
1812         }
1813         LINVRNT(osd_invariant(obj));
1814         return result;
1815 }
1816
1817 typedef int (*osd_obj_type_f)(struct osd_thread_info *, struct osd_object *,
1818                               struct lu_attr *,
1819                               struct dt_allocation_hint *hint,
1820                               struct dt_object_format *dof,
1821                               struct thandle *);
1822
1823 static osd_obj_type_f osd_create_type_f(enum dt_format_type type)
1824 {
1825         osd_obj_type_f result;
1826
1827         switch (type) {
1828         case DFT_DIR:
1829                 result = osd_mkdir;
1830                 break;
1831         case DFT_REGULAR:
1832                 result = osd_mkreg;
1833                 break;
1834         case DFT_SYM:
1835                 result = osd_mksym;
1836                 break;
1837         case DFT_NODE:
1838                 result = osd_mknod;
1839                 break;
1840         case DFT_INDEX:
1841                 result = osd_mk_index;
1842                 break;
1843
1844         default:
1845                 LBUG();
1846                 break;
1847         }
1848         return result;
1849 }
1850
1851
1852 static void osd_ah_init(const struct lu_env *env, struct dt_allocation_hint *ah,
1853                         struct dt_object *parent, struct dt_object *child,
1854                         cfs_umode_t child_mode)
1855 {
1856         LASSERT(ah);
1857
1858         memset(ah, 0, sizeof(*ah));
1859         ah->dah_parent = parent;
1860         ah->dah_mode = child_mode;
1861 }
1862
1863 static void osd_attr_init(struct osd_thread_info *info, struct osd_object *obj,
1864                           struct lu_attr *attr, struct dt_object_format *dof)
1865 {
1866         struct inode   *inode = obj->oo_inode;
1867         __u64           valid = attr->la_valid;
1868         int             result;
1869
1870         attr->la_valid &= ~(LA_TYPE | LA_MODE);
1871
1872         if (dof->dof_type != DFT_NODE)
1873                 attr->la_valid &= ~LA_RDEV;
1874         if ((valid & LA_ATIME) && (attr->la_atime == LTIME_S(inode->i_atime)))
1875                 attr->la_valid &= ~LA_ATIME;
1876         if ((valid & LA_CTIME) && (attr->la_ctime == LTIME_S(inode->i_ctime)))
1877                 attr->la_valid &= ~LA_CTIME;
1878         if ((valid & LA_MTIME) && (attr->la_mtime == LTIME_S(inode->i_mtime)))
1879                 attr->la_valid &= ~LA_MTIME;
1880
1881         result = osd_quota_transfer(inode, attr);
1882         if (result)
1883                 return;
1884
1885         if (attr->la_valid != 0) {
1886                 result = osd_inode_setattr(info->oti_env, inode, attr);
1887                 /*
1888                  * The osd_inode_setattr() should always succeed here.  The
1889                  * only error that could be returned is EDQUOT when we are
1890                  * trying to change the UID or GID of the inode. However, this
1891                  * should not happen since quota enforcement is no longer
1892                  * enabled on ldiskfs (lquota takes care of it).
1893                  */
1894                 LASSERTF(result == 0, "%d", result);
1895                 inode->i_sb->s_op->dirty_inode(inode);
1896         }
1897
1898         attr->la_valid = valid;
1899 }
1900
1901 /**
1902  * Helper function for osd_object_create()
1903  *
1904  * \retval 0, on success
1905  */
1906 static int __osd_object_create(struct osd_thread_info *info,
1907                                struct osd_object *obj, struct lu_attr *attr,
1908                                struct dt_allocation_hint *hint,
1909                                struct dt_object_format *dof,
1910                                struct thandle *th)
1911 {
1912         int     result;
1913
1914         result = osd_create_type_f(dof->dof_type)(info, obj, attr, hint, dof,
1915                                                   th);
1916         if (result == 0) {
1917                 osd_attr_init(info, obj, attr, dof);
1918                 osd_object_init0(obj);
1919                 /* bz 24037 */
1920                 if (obj->oo_inode && (obj->oo_inode->i_state & I_NEW))
1921                         unlock_new_inode(obj->oo_inode);
1922         }
1923
1924         return result;
1925 }
1926
1927 /**
1928  * Helper function for osd_object_create()
1929  *
1930  * \retval 0, on success
1931  */
1932 static int __osd_oi_insert(const struct lu_env *env, struct osd_object *obj,
1933                            const struct lu_fid *fid, struct thandle *th)
1934 {
1935         struct osd_thread_info *info = osd_oti_get(env);
1936         struct osd_inode_id    *id   = &info->oti_id;
1937         struct osd_device      *osd  = osd_obj2dev(obj);
1938
1939         LASSERT(obj->oo_inode != NULL);
1940
1941         osd_id_gen(id, obj->oo_inode->i_ino, obj->oo_inode->i_generation);
1942         return osd_oi_insert(info, osd, fid, id, th);
1943 }
1944
1945 int osd_fld_lookup(const struct lu_env *env, struct osd_device *osd,
1946                    const struct lu_fid *fid, struct lu_seq_range *range)
1947 {
1948         struct seq_server_site  *ss = osd_seq_site(osd);
1949         int                     rc;
1950
1951         if (fid_is_igif(fid)) {
1952                 range->lsr_flags = LU_SEQ_RANGE_MDT;
1953                 range->lsr_index = 0;
1954                 return 0;
1955         }
1956
1957         if (fid_is_idif(fid)) {
1958                 range->lsr_flags = LU_SEQ_RANGE_OST;
1959                 range->lsr_index = fid_idif_ost_idx(fid);
1960                 return 0;
1961         }
1962
1963         if (!fid_is_norm(fid)) {
1964                 range->lsr_flags = LU_SEQ_RANGE_MDT;
1965                 if (ss != NULL)
1966                         /* FIXME: If ss is NULL, it suppose not get lsr_index
1967                          * at all */
1968                         range->lsr_index = ss->ss_node_id;
1969                 return 0;
1970         }
1971
1972         LASSERT(ss != NULL);
1973         range->lsr_flags = -1;
1974         rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1975         if (rc != 0) {
1976                 CERROR("%s can not find "DFID": rc = %d\n",
1977                        osd_name(osd), PFID(fid), rc);
1978         }
1979         return rc;
1980 }
1981
1982
1983 static int osd_declare_object_create(const struct lu_env *env,
1984                                      struct dt_object *dt,
1985                                      struct lu_attr *attr,
1986                                      struct dt_allocation_hint *hint,
1987                                      struct dt_object_format *dof,
1988                                      struct thandle *handle)
1989 {
1990         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
1991         struct osd_thandle      *oh;
1992         int                      rc;
1993         ENTRY;
1994
1995         LASSERT(handle != NULL);
1996
1997         oh = container_of0(handle, struct osd_thandle, ot_super);
1998         LASSERT(oh->ot_handle == NULL);
1999
2000         osd_trans_declare_op(env, oh, OSD_OT_CREATE,
2001                              osd_dto_credits_noquota[DTO_OBJECT_CREATE]);
2002         /* XXX: So far, only normal fid needs be inserted into the oi,
2003          *      things could be changed later. Revise following code then. */
2004         if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2005             !fid_is_on_ost(osd_oti_get(env), osd_dt_dev(handle->th_dev),
2006                            lu_object_fid(&dt->do_lu))) {
2007                 /* Reuse idle OI block may cause additional one OI block
2008                  * to be changed. */
2009                 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2010                                 osd_dto_credits_noquota[DTO_INDEX_INSERT] + 1);
2011         }
2012         /* If this is directory, then we expect . and .. to be inserted as
2013          * well. The one directory block always needs to be created for the
2014          * directory, so we could use DTO_WRITE_BASE here (GDT, block bitmap,
2015          * block), there is no danger of needing a tree for the first block.
2016          */
2017         if (attr && S_ISDIR(attr->la_mode)) {
2018                 osd_trans_declare_op(env, oh, OSD_OT_INSERT,
2019                                      osd_dto_credits_noquota[DTO_WRITE_BASE]);
2020                 osd_trans_declare_op(env, oh, OSD_OT_INSERT, 0);
2021         }
2022
2023         if (!attr)
2024                 RETURN(0);
2025
2026         rc = osd_declare_inode_qid(env, attr->la_uid, attr->la_gid, 1, oh,
2027                                    false, false, NULL, false);
2028         if (rc != 0)
2029                 RETURN(rc);
2030
2031         /* It does fld look up inside declare, and the result will be
2032          * added to fld cache, so the following fld lookup inside insert
2033          * does not need send RPC anymore, so avoid send rpc with holding
2034          * transaction */
2035         if (fid_is_norm(lu_object_fid(&dt->do_lu)) &&
2036                 !fid_is_last_id(lu_object_fid(&dt->do_lu)))
2037                 osd_fld_lookup(env, osd_dt_dev(handle->th_dev),
2038                                lu_object_fid(&dt->do_lu), range);
2039
2040
2041         RETURN(rc);
2042 }
2043
2044 static int osd_object_create(const struct lu_env *env, struct dt_object *dt,
2045                              struct lu_attr *attr,
2046                              struct dt_allocation_hint *hint,
2047                              struct dt_object_format *dof,
2048                              struct thandle *th)
2049 {
2050         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
2051         struct osd_object      *obj    = osd_dt_obj(dt);
2052         struct osd_thread_info *info   = osd_oti_get(env);
2053         int result;
2054
2055         ENTRY;
2056
2057         LINVRNT(osd_invariant(obj));
2058         LASSERT(!dt_object_exists(dt));
2059         LASSERT(osd_write_locked(env, obj));
2060         LASSERT(th != NULL);
2061
2062         if (unlikely(fid_is_acct(fid)))
2063                 /* Quota files can't be created from the kernel any more,
2064                  * 'tune2fs -O quota' will take care of creating them */
2065                 RETURN(-EPERM);
2066
2067         osd_trans_exec_op(env, th, OSD_OT_CREATE);
2068         osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2069
2070         result = __osd_object_create(info, obj, attr, hint, dof, th);
2071         if (result == 0)
2072                 result = __osd_oi_insert(env, obj, fid, th);
2073
2074         LASSERT(ergo(result == 0, dt_object_exists(dt)));
2075         LASSERT(osd_invariant(obj));
2076         RETURN(result);
2077 }
2078
2079 /**
2080  * Called to destroy on-disk representation of the object
2081  *
2082  * Concurrency: must be locked
2083  */
2084 static int osd_declare_object_destroy(const struct lu_env *env,
2085                                       struct dt_object *dt,
2086                                       struct thandle *th)
2087 {
2088         struct osd_object  *obj = osd_dt_obj(dt);
2089         struct inode       *inode = obj->oo_inode;
2090         struct osd_thandle *oh;
2091         int                 rc;
2092         ENTRY;
2093
2094         oh = container_of0(th, struct osd_thandle, ot_super);
2095         LASSERT(oh->ot_handle == NULL);
2096         LASSERT(inode);
2097
2098         osd_trans_declare_op(env, oh, OSD_OT_DELETE,
2099                              osd_dto_credits_noquota[DTO_OBJECT_DELETE]);
2100         /* XXX: So far, only normal fid needs to be inserted into the OI,
2101          *      so only normal fid needs to be removed from the OI also.
2102          * Recycle idle OI leaf may cause additional three OI blocks
2103          * to be changed. */
2104         osd_trans_declare_op(env, oh, OSD_OT_DESTROY,
2105                              fid_is_norm(lu_object_fid(&dt->do_lu)) ?
2106                              osd_dto_credits_noquota[DTO_INDEX_DELETE] + 3 : 0);
2107
2108         /* one less inode */
2109         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, -1, oh,
2110                                    false, true, NULL, false);
2111         if (rc)
2112                 RETURN(rc);
2113         /* data to be truncated */
2114         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
2115                                    true, true, NULL, false);
2116         RETURN(rc);
2117 }
2118
2119 static int osd_object_destroy(const struct lu_env *env,
2120                               struct dt_object *dt,
2121                               struct thandle *th)
2122 {
2123         const struct lu_fid    *fid = lu_object_fid(&dt->do_lu);
2124         struct osd_object      *obj = osd_dt_obj(dt);
2125         struct inode           *inode = obj->oo_inode;
2126         struct osd_device      *osd = osd_obj2dev(obj);
2127         struct osd_thandle     *oh;
2128         int                     result;
2129         ENTRY;
2130
2131         oh = container_of0(th, struct osd_thandle, ot_super);
2132         LASSERT(oh->ot_handle);
2133         LASSERT(inode);
2134         LASSERT(!lu_object_is_dying(dt->do_lu.lo_header));
2135
2136         if (unlikely(fid_is_acct(fid)))
2137                 RETURN(-EPERM);
2138
2139         /* Parallel control for OI scrub. For most of cases, there is no
2140          * lock contention. So it will not affect unlink performance. */
2141         mutex_lock(&inode->i_mutex);
2142         if (S_ISDIR(inode->i_mode)) {
2143                 LASSERT(osd_inode_unlinked(inode) || inode->i_nlink == 1);
2144                 /* it will check/delete the agent inode for every dir
2145                  * destory, how to optimize it? unlink performance
2146                  * impaction XXX */
2147                 result = osd_delete_agent_inode(env, osd, obj, oh);
2148                 if (result != 0 && result != -ENOENT) {
2149                         CERROR("%s: delete agent inode "DFID": rc = %d\n",
2150                                osd_name(osd), PFID(fid), result);
2151                 }
2152                 spin_lock(&obj->oo_guard);
2153                 clear_nlink(inode);
2154                 spin_unlock(&obj->oo_guard);
2155                 inode->i_sb->s_op->dirty_inode(inode);
2156         }
2157
2158         osd_trans_exec_op(env, th, OSD_OT_DESTROY);
2159
2160         result = osd_oi_delete(osd_oti_get(env), osd, fid, th);
2161         mutex_unlock(&inode->i_mutex);
2162
2163         /* XXX: add to ext3 orphan list */
2164         /* rc = ext3_orphan_add(handle_t *handle, struct inode *inode) */
2165
2166         /* not needed in the cache anymore */
2167         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
2168
2169         RETURN(0);
2170 }
2171
2172 static inline int __osd_xattr_set(struct osd_thread_info *info,
2173                                   struct inode *inode, const char *name,
2174                                   const void *buf, int buflen, int fl)
2175 {
2176         struct dentry *dentry = &info->oti_child_dentry;
2177
2178         ll_vfs_dq_init(inode);
2179         dentry->d_inode = inode;
2180         return inode->i_op->setxattr(dentry, name, buf, buflen, fl);
2181 }
2182
2183 /**
2184  * Put the fid into lustre_mdt_attrs, and then place the structure
2185  * inode's ea. This fid should not be altered during the life time
2186  * of the inode.
2187  *
2188  * \retval +ve, on success
2189  * \retval -ve, on error
2190  *
2191  * FIXME: It is good to have/use ldiskfs_xattr_set_handle() here
2192  */
2193 int osd_ea_fid_set(struct osd_thread_info *info, struct inode *inode,
2194                    const struct lu_fid *fid)
2195 {
2196         struct lustre_mdt_attrs *lma = &info->oti_mdt_attrs;
2197         int                      rc;
2198
2199         lustre_lma_init(lma, fid);
2200         lustre_lma_swab(lma);
2201
2202         rc = __osd_xattr_set(info, inode, XATTR_NAME_LMA, lma, sizeof(*lma),
2203                              XATTR_CREATE);
2204         /* Someone may created the EA by race. */
2205         if (unlikely(rc == -EEXIST))
2206                 rc = 0;
2207         return rc;
2208 }
2209
2210 /**
2211  * ldiskfs supports fid in dirent, it is passed in dentry->d_fsdata.
2212  * lustre 1.8 also uses d_fsdata for passing other info to ldiskfs.
2213  * To have compatilibility with 1.8 ldiskfs driver we need to have
2214  * magic number at start of fid data.
2215  * \ldiskfs_dentry_param is used only to pass fid from osd to ldiskfs.
2216  * its inmemory API.
2217  */
2218 void osd_get_ldiskfs_dirent_param(struct ldiskfs_dentry_param *param,
2219                                   const struct dt_rec *fid)
2220 {
2221         /* XXX: replace the check with "!fid_is_client_mdt_visible()"
2222          *      when FID in OI file introduced for local object. */
2223         if (!fid_is_norm((const struct lu_fid *)fid) &&
2224             !fid_is_igif((const struct lu_fid *)fid)) {
2225                 param->edp_magic = 0;
2226                 return;
2227         }
2228
2229         param->edp_magic = LDISKFS_LUFID_MAGIC;
2230         param->edp_len =  sizeof(struct lu_fid) + 1;
2231         fid_cpu_to_be((struct lu_fid *)param->edp_data, (struct lu_fid *)fid);
2232 }
2233
2234 /**
2235  * Try to read the fid from inode ea into dt_rec, if return value
2236  * i.e. rc is +ve, then we got fid, otherwise we will have to form igif
2237  *
2238  * \param fid object fid.
2239  *
2240  * \retval 0 on success
2241  */
2242 static int osd_ea_fid_get(const struct lu_env *env, struct osd_object *obj,
2243                           __u32 ino, struct lu_fid *fid,
2244                           struct osd_inode_id *id)
2245 {
2246         struct osd_thread_info *info  = osd_oti_get(env);
2247         struct inode           *inode;
2248         ENTRY;
2249
2250         osd_id_gen(id, ino, OSD_OII_NOGEN);
2251         inode = osd_iget_fid(info, osd_obj2dev(obj), id, fid);
2252         if (IS_ERR(inode))
2253                 RETURN(PTR_ERR(inode));
2254
2255         iput(inode);
2256         RETURN(0);
2257 }
2258
2259 static int osd_add_dot_dotdot_internal(struct osd_thread_info *info,
2260                                         struct inode *dir,
2261                                         struct inode  *parent_dir,
2262                                         const struct dt_rec *dot_fid,
2263                                         const struct dt_rec *dot_dot_fid,
2264                                         struct osd_thandle *oth)
2265 {
2266         struct ldiskfs_dentry_param *dot_ldp;
2267         struct ldiskfs_dentry_param *dot_dot_ldp;
2268
2269         dot_dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp2;
2270         osd_get_ldiskfs_dirent_param(dot_dot_ldp, dot_dot_fid);
2271
2272         dot_ldp = (struct ldiskfs_dentry_param *)info->oti_ldp;
2273         dot_ldp->edp_magic = 0;
2274         return ldiskfs_add_dot_dotdot(oth->ot_handle, parent_dir,
2275                                         dir, dot_ldp, dot_dot_ldp);
2276 }
2277
2278 /**
2279  * Create an local inode for remote entry
2280  */
2281 static struct inode *osd_create_remote_inode(const struct lu_env *env,
2282                                              struct osd_device *osd,
2283                                              struct osd_object *pobj,
2284                                              const struct lu_fid *fid,
2285                                              struct thandle *th)
2286 {
2287         struct osd_thread_info  *info = osd_oti_get(env);
2288         struct inode            *local;
2289         struct osd_thandle      *oh;
2290         int                     rc;
2291         ENTRY;
2292
2293         LASSERT(th);
2294         oh = container_of(th, struct osd_thandle, ot_super);
2295         LASSERT(oh->ot_handle->h_transaction != NULL);
2296
2297 #ifdef HAVE_QUOTA_SUPPORT
2298         osd_push_ctxt(info->oti_env, save);
2299 #endif
2300         /* FIXME: Insert index api needs to know the mode of
2301          * the remote object. Just use S_IFDIR for now */
2302         local = ldiskfs_create_inode(oh->ot_handle, pobj->oo_inode, S_IFDIR);
2303 #ifdef HAVE_QUOTA_SUPPORT
2304         osd_pop_ctxt(info->oti_env, save);
2305 #endif
2306         if (IS_ERR(local)) {
2307                 CERROR("%s: create local error %d\n", osd_name(osd),
2308                        (int)PTR_ERR(local));
2309                 RETURN(local);
2310         }
2311
2312         rc = osd_add_dot_dotdot_internal(info, local, pobj->oo_inode,
2313                 (const struct dt_rec *)lu_object_fid(&pobj->oo_dt.do_lu),
2314                 (const struct dt_rec *)fid, oh);
2315         if (rc != 0) {
2316                 CERROR("%s: "DFID" add dot dotdot error: rc = %d\n",
2317                         osd_name(osd), PFID(fid), rc);
2318                 RETURN(ERR_PTR(rc));
2319         }
2320
2321         RETURN(local);
2322 }
2323
2324 /**
2325  * Delete local inode for remote entry
2326  */
2327 static int osd_delete_remote_inode(const struct lu_env *env,
2328                                    struct osd_device *osd,
2329                                    const struct lu_fid *fid,
2330                                     __u32 ino, struct osd_thandle *oh)
2331 {
2332         struct osd_thread_info  *oti = osd_oti_get(env);
2333         struct osd_inode_id     *id = &oti->oti_id;
2334         struct inode            *inode;
2335         ENTRY;
2336
2337         id->oii_ino = le32_to_cpu(ino);
2338         id->oii_gen = OSD_OII_NOGEN;
2339         inode = osd_iget(oti, osd, id);
2340         if (IS_ERR(inode)) {
2341                 CERROR("%s: iget error "DFID" id %u:%u\n", osd_name(osd),
2342                        PFID(fid), id->oii_ino, id->oii_gen);
2343                 RETURN(PTR_ERR(inode));
2344         }
2345
2346         clear_nlink(inode);
2347         mark_inode_dirty(inode);
2348         CDEBUG(D_INODE, "%s: delete remote inode "DFID" %lu\n",
2349                 osd_name(osd), PFID(fid), inode->i_ino);
2350         iput(inode);
2351         RETURN(0);
2352 }
2353
2354 /**
2355  * OSD layer object create function for interoperability mode (b11826).
2356  * This is mostly similar to osd_object_create(). Only difference being, fid is
2357  * inserted into inode ea here.
2358  *
2359  * \retval   0, on success
2360  * \retval -ve, on error
2361  */
2362 static int osd_object_ea_create(const struct lu_env *env, struct dt_object *dt,
2363                                 struct lu_attr *attr,
2364                                 struct dt_allocation_hint *hint,
2365                                 struct dt_object_format *dof,
2366                                 struct thandle *th)
2367 {
2368         const struct lu_fid    *fid    = lu_object_fid(&dt->do_lu);
2369         struct osd_object      *obj    = osd_dt_obj(dt);
2370         struct osd_thread_info *info   = osd_oti_get(env);
2371         int                     result;
2372
2373         ENTRY;
2374
2375         LASSERT(osd_invariant(obj));
2376         LASSERT(!dt_object_exists(dt));
2377         LASSERT(osd_write_locked(env, obj));
2378         LASSERT(th != NULL);
2379
2380         if (unlikely(fid_is_acct(fid)))
2381                 /* Quota files can't be created from the kernel any more,
2382                  * 'tune2fs -O quota' will take care of creating them */
2383                 RETURN(-EPERM);
2384
2385         osd_trans_exec_op(env, th, OSD_OT_CREATE);
2386         osd_trans_declare_rb(env, th, OSD_OT_REF_ADD);
2387
2388         result = __osd_object_create(info, obj, attr, hint, dof, th);
2389         /* objects under osd root shld have igif fid, so dont add fid EA */
2390         /* For ost object, the fid will be stored during first write */
2391         if (result == 0 && fid_seq(fid) >= FID_SEQ_NORMAL &&
2392             !fid_is_on_ost(info, osd_dt_dev(th->th_dev), fid))
2393                 result = osd_ea_fid_set(info, obj->oo_inode, fid);
2394
2395         if (result == 0)
2396                 result = __osd_oi_insert(env, obj, fid, th);
2397
2398         LASSERT(ergo(result == 0, dt_object_exists(dt)));
2399         LINVRNT(osd_invariant(obj));
2400         RETURN(result);
2401 }
2402
2403 static int osd_declare_object_ref_add(const struct lu_env *env,
2404                                       struct dt_object *dt,
2405                                       struct thandle *handle)
2406 {
2407         struct osd_thandle       *oh;
2408
2409         /* it's possible that object doesn't exist yet */
2410         LASSERT(handle != NULL);
2411
2412         oh = container_of0(handle, struct osd_thandle, ot_super);
2413         LASSERT(oh->ot_handle == NULL);
2414
2415         osd_trans_declare_op(env, oh, OSD_OT_REF_ADD,
2416                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2417
2418         return 0;
2419 }
2420
2421 /*
2422  * Concurrency: @dt is write locked.
2423  */
2424 static int osd_object_ref_add(const struct lu_env *env,
2425                               struct dt_object *dt, struct thandle *th)
2426 {
2427         struct osd_object *obj = osd_dt_obj(dt);
2428         struct inode      *inode = obj->oo_inode;
2429
2430         LINVRNT(osd_invariant(obj));
2431         LASSERT(dt_object_exists(dt));
2432         LASSERT(osd_write_locked(env, obj));
2433         LASSERT(th != NULL);
2434
2435         osd_trans_exec_op(env, th, OSD_OT_REF_ADD);
2436
2437         /*
2438          * DIR_NLINK feature is set for compatibility reasons if:
2439          * 1) nlinks > LDISKFS_LINK_MAX, or
2440          * 2) nlinks == 2, since this indicates i_nlink was previously 1.
2441          *
2442          * It is easier to always set this flag (rather than check and set),
2443          * since it has less overhead, and the superblock will be dirtied
2444          * at some point. Both e2fsprogs and any Lustre-supported ldiskfs
2445          * do not actually care whether this flag is set or not.
2446          */
2447         spin_lock(&obj->oo_guard);
2448         /* inc_nlink from 0 may cause WARN_ON */
2449         if(inode->i_nlink == 0)
2450                 set_nlink(inode, 1);
2451         else
2452                 inc_nlink(inode);
2453         if (S_ISDIR(inode->i_mode) && inode->i_nlink > 1) {
2454                 if (inode->i_nlink >= LDISKFS_LINK_MAX ||
2455                     inode->i_nlink == 2)
2456                         set_nlink(inode, 1);
2457         }
2458         LASSERT(inode->i_nlink <= LDISKFS_LINK_MAX);
2459         spin_unlock(&obj->oo_guard);
2460         inode->i_sb->s_op->dirty_inode(inode);
2461         LINVRNT(osd_invariant(obj));
2462
2463         return 0;
2464 }
2465
2466 static int osd_declare_object_ref_del(const struct lu_env *env,
2467                                       struct dt_object *dt,
2468                                       struct thandle *handle)
2469 {
2470         struct osd_thandle *oh;
2471
2472         LASSERT(dt_object_exists(dt));
2473         LASSERT(handle != NULL);
2474
2475         oh = container_of0(handle, struct osd_thandle, ot_super);
2476         LASSERT(oh->ot_handle == NULL);
2477
2478         osd_trans_declare_op(env, oh, OSD_OT_REF_DEL,
2479                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE]);
2480
2481         return 0;
2482 }
2483
2484 /*
2485  * Concurrency: @dt is write locked.
2486  */
2487 static int osd_object_ref_del(const struct lu_env *env, struct dt_object *dt,
2488                               struct thandle *th)
2489 {
2490         struct osd_object *obj = osd_dt_obj(dt);
2491         struct inode      *inode = obj->oo_inode;
2492
2493         LINVRNT(osd_invariant(obj));
2494         LASSERT(dt_object_exists(dt));
2495         LASSERT(osd_write_locked(env, obj));
2496         LASSERT(th != NULL);
2497
2498         osd_trans_exec_op(env, th, OSD_OT_REF_DEL);
2499
2500         spin_lock(&obj->oo_guard);
2501         LASSERT(inode->i_nlink > 0);
2502         drop_nlink(inode);
2503         /* If this is/was a many-subdir directory (nlink > LDISKFS_LINK_MAX)
2504          * then the nlink count is 1. Don't let it be set to 0 or the directory
2505          * inode will be deleted incorrectly. */
2506         if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
2507                 set_nlink(inode, 1);
2508         spin_unlock(&obj->oo_guard);
2509         inode->i_sb->s_op->dirty_inode(inode);
2510         LINVRNT(osd_invariant(obj));
2511
2512         return 0;
2513 }
2514
2515 /*
2516  * Get the 64-bit version for an inode.
2517  */
2518 static int osd_object_version_get(const struct lu_env *env,
2519                                   struct dt_object *dt, dt_obj_version_t *ver)
2520 {
2521         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2522
2523         CDEBUG(D_INODE, "Get version "LPX64" for inode %lu\n",
2524                LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2525         *ver = LDISKFS_I(inode)->i_fs_version;
2526         return 0;
2527 }
2528
2529 /*
2530  * Concurrency: @dt is read locked.
2531  */
2532 static int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
2533                          struct lu_buf *buf, const char *name,
2534                          struct lustre_capa *capa)
2535 {
2536         struct osd_object      *obj    = osd_dt_obj(dt);
2537         struct inode           *inode  = obj->oo_inode;
2538         struct osd_thread_info *info   = osd_oti_get(env);
2539         struct dentry          *dentry = &info->oti_obj_dentry;
2540
2541         /* version get is not real XATTR but uses xattr API */
2542         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2543                 /* for version we are just using xattr API but change inode
2544                  * field instead */
2545                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2546                 osd_object_version_get(env, dt, buf->lb_buf);
2547                 return sizeof(dt_obj_version_t);
2548         }
2549
2550         LASSERT(dt_object_exists(dt));
2551         LASSERT(inode->i_op != NULL && inode->i_op->getxattr != NULL);
2552
2553         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2554                 return -EACCES;
2555
2556         return __osd_xattr_get(inode, dentry, name, buf->lb_buf, buf->lb_len);
2557 }
2558
2559
2560 static int osd_declare_xattr_set(const struct lu_env *env,
2561                                  struct dt_object *dt,
2562                                  const struct lu_buf *buf, const char *name,
2563                                  int fl, struct thandle *handle)
2564 {
2565         struct osd_thandle *oh;
2566
2567         LASSERT(handle != NULL);
2568
2569         oh = container_of0(handle, struct osd_thandle, ot_super);
2570         LASSERT(oh->ot_handle == NULL);
2571
2572         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2573                              strcmp(name, XATTR_NAME_VERSION) == 0 ?
2574                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE] :
2575                              osd_dto_credits_noquota[DTO_XATTR_SET]);
2576
2577         return 0;
2578 }
2579
2580 /*
2581  * Set the 64-bit version for object
2582  */
2583 static void osd_object_version_set(const struct lu_env *env,
2584                                    struct dt_object *dt,
2585                                    dt_obj_version_t *new_version)
2586 {
2587         struct inode *inode = osd_dt_obj(dt)->oo_inode;
2588
2589         CDEBUG(D_INODE, "Set version "LPX64" (old "LPX64") for inode %lu\n",
2590                *new_version, LDISKFS_I(inode)->i_fs_version, inode->i_ino);
2591
2592         LDISKFS_I(inode)->i_fs_version = *new_version;
2593         /** Version is set after all inode operations are finished,
2594          *  so we should mark it dirty here */
2595         inode->i_sb->s_op->dirty_inode(inode);
2596 }
2597
2598 /*
2599  * Concurrency: @dt is write locked.
2600  */
2601 static int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
2602                          const struct lu_buf *buf, const char *name, int fl,
2603                          struct thandle *handle, struct lustre_capa *capa)
2604 {
2605         struct osd_object      *obj      = osd_dt_obj(dt);
2606         struct inode           *inode    = obj->oo_inode;
2607         struct osd_thread_info *info     = osd_oti_get(env);
2608         int                     fs_flags = 0;
2609
2610         LASSERT(handle != NULL);
2611
2612         /* version set is not real XATTR */
2613         if (strcmp(name, XATTR_NAME_VERSION) == 0) {
2614                 /* for version we are just using xattr API but change inode
2615                  * field instead */
2616                 LASSERT(buf->lb_len == sizeof(dt_obj_version_t));
2617                 osd_object_version_set(env, dt, buf->lb_buf);
2618                 return sizeof(dt_obj_version_t);
2619         }
2620
2621         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2622                 return -EACCES;
2623
2624         osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2625         if (fl & LU_XATTR_REPLACE)
2626                 fs_flags |= XATTR_REPLACE;
2627
2628         if (fl & LU_XATTR_CREATE)
2629                 fs_flags |= XATTR_CREATE;
2630
2631         return __osd_xattr_set(info, inode, name, buf->lb_buf, buf->lb_len,
2632                                fs_flags);
2633 }
2634
2635 /*
2636  * Concurrency: @dt is read locked.
2637  */
2638 static int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
2639                           struct lu_buf *buf, struct lustre_capa *capa)
2640 {
2641         struct osd_object      *obj    = osd_dt_obj(dt);
2642         struct inode           *inode  = obj->oo_inode;
2643         struct osd_thread_info *info   = osd_oti_get(env);
2644         struct dentry          *dentry = &info->oti_obj_dentry;
2645
2646         LASSERT(dt_object_exists(dt));
2647         LASSERT(inode->i_op != NULL && inode->i_op->listxattr != NULL);
2648         LASSERT(osd_read_locked(env, obj) || osd_write_locked(env, obj));
2649
2650         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_READ))
2651                 return -EACCES;
2652
2653         dentry->d_inode = inode;
2654         return inode->i_op->listxattr(dentry, buf->lb_buf, buf->lb_len);
2655 }
2656
2657 static int osd_declare_xattr_del(const struct lu_env *env,
2658                                  struct dt_object *dt, const char *name,
2659                                  struct thandle *handle)
2660 {
2661         struct osd_thandle *oh;
2662
2663         LASSERT(dt_object_exists(dt));
2664         LASSERT(handle != NULL);
2665
2666         oh = container_of0(handle, struct osd_thandle, ot_super);
2667         LASSERT(oh->ot_handle == NULL);
2668
2669         osd_trans_declare_op(env, oh, OSD_OT_XATTR_SET,
2670                              osd_dto_credits_noquota[DTO_XATTR_SET]);
2671
2672         return 0;
2673 }
2674
2675 /*
2676  * Concurrency: @dt is write locked.
2677  */
2678 static int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
2679                          const char *name, struct thandle *handle,
2680                          struct lustre_capa *capa)
2681 {
2682         struct osd_object      *obj    = osd_dt_obj(dt);
2683         struct inode           *inode  = obj->oo_inode;
2684         struct osd_thread_info *info   = osd_oti_get(env);
2685         struct dentry          *dentry = &info->oti_obj_dentry;
2686         int                     rc;
2687
2688         LASSERT(dt_object_exists(dt));
2689         LASSERT(inode->i_op != NULL && inode->i_op->removexattr != NULL);
2690         LASSERT(handle != NULL);
2691
2692         if (osd_object_auth(env, dt, capa, CAPA_OPC_META_WRITE))
2693                 return -EACCES;
2694
2695         osd_trans_exec_op(env, handle, OSD_OT_XATTR_SET);
2696
2697         ll_vfs_dq_init(inode);
2698         dentry->d_inode = inode;
2699         rc = inode->i_op->removexattr(dentry, name);
2700         return rc;
2701 }
2702
2703 static struct obd_capa *osd_capa_get(const struct lu_env *env,
2704                                      struct dt_object *dt,
2705                                      struct lustre_capa *old,
2706                                      __u64 opc)
2707 {
2708         struct osd_thread_info *info = osd_oti_get(env);
2709         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2710         struct osd_object *obj = osd_dt_obj(dt);
2711         struct osd_device *dev = osd_obj2dev(obj);
2712         struct lustre_capa_key *key = &info->oti_capa_key;
2713         struct lustre_capa *capa = &info->oti_capa;
2714         struct obd_capa *oc;
2715         struct md_capainfo *ci;
2716         int rc;
2717         ENTRY;
2718
2719         if (!dev->od_fl_capa)
2720                 RETURN(ERR_PTR(-ENOENT));
2721
2722         LASSERT(dt_object_exists(dt));
2723         LINVRNT(osd_invariant(obj));
2724
2725         /* renewal sanity check */
2726         if (old && osd_object_auth(env, dt, old, opc))
2727                 RETURN(ERR_PTR(-EACCES));
2728
2729         ci = md_capainfo(env);
2730         if (unlikely(!ci))
2731                 RETURN(ERR_PTR(-ENOENT));
2732
2733         switch (ci->mc_auth) {
2734         case LC_ID_NONE:
2735                 RETURN(NULL);
2736         case LC_ID_PLAIN:
2737                 capa->lc_uid = obj->oo_inode->i_uid;
2738                 capa->lc_gid = obj->oo_inode->i_gid;
2739                 capa->lc_flags = LC_ID_PLAIN;
2740                 break;
2741         case LC_ID_CONVERT: {
2742                 __u32 d[4], s[4];
2743
2744                 s[0] = obj->oo_inode->i_uid;
2745                 cfs_get_random_bytes(&(s[1]), sizeof(__u32));
2746                 s[2] = obj->oo_inode->i_gid;
2747                 cfs_get_random_bytes(&(s[3]), sizeof(__u32));
2748                 rc = capa_encrypt_id(d, s, key->lk_key, CAPA_HMAC_KEY_MAX_LEN);
2749                 if (unlikely(rc))
2750                         RETURN(ERR_PTR(rc));
2751
2752                 capa->lc_uid   = ((__u64)d[1] << 32) | d[0];
2753                 capa->lc_gid   = ((__u64)d[3] << 32) | d[2];
2754                 capa->lc_flags = LC_ID_CONVERT;
2755                 break;
2756         }
2757         default:
2758                 RETURN(ERR_PTR(-EINVAL));
2759         }
2760
2761         capa->lc_fid = *fid;
2762         capa->lc_opc = opc;
2763         capa->lc_flags |= dev->od_capa_alg << 24;
2764         capa->lc_timeout = dev->od_capa_timeout;
2765         capa->lc_expiry = 0;
2766
2767         oc = capa_lookup(dev->od_capa_hash, capa, 1);
2768         if (oc) {
2769                 LASSERT(!capa_is_expired(oc));
2770                 RETURN(oc);
2771         }
2772
2773         spin_lock(&capa_lock);
2774         *key = dev->od_capa_keys[1];
2775         spin_unlock(&capa_lock);
2776
2777         capa->lc_keyid = key->lk_keyid;
2778         capa->lc_expiry = cfs_time_current_sec() + dev->od_capa_timeout;
2779
2780         rc = capa_hmac(capa->lc_hmac, capa, key->lk_key);
2781         if (rc) {
2782                 DEBUG_CAPA(D_ERROR, capa, "HMAC failed: %d for", rc);
2783                 RETURN(ERR_PTR(rc));
2784         }
2785
2786         oc = capa_add(dev->od_capa_hash, capa);
2787         RETURN(oc);
2788 }
2789
2790 static int osd_object_sync(const struct lu_env *env, struct dt_object *dt)
2791 {
2792         struct osd_object       *obj    = osd_dt_obj(dt);
2793         struct inode            *inode  = obj->oo_inode;
2794         struct osd_thread_info  *info   = osd_oti_get(env);
2795         struct dentry           *dentry = &info->oti_obj_dentry;
2796         struct file             *file   = &info->oti_file;
2797         int                     rc;
2798
2799         ENTRY;
2800
2801         dentry->d_inode = inode;
2802         file->f_dentry = dentry;
2803         file->f_mapping = inode->i_mapping;
2804         file->f_op = inode->i_fop;
2805         mutex_lock(&inode->i_mutex);
2806         rc = file->f_op->fsync(file, dentry, 0);
2807         mutex_unlock(&inode->i_mutex);
2808         RETURN(rc);
2809 }
2810
2811 static int osd_data_get(const struct lu_env *env, struct dt_object *dt,
2812                         void **data)
2813 {
2814         struct osd_object *obj = osd_dt_obj(dt);
2815         ENTRY;
2816
2817         *data = (void *)obj->oo_inode;
2818         RETURN(0);
2819 }
2820
2821 /*
2822  * Index operations.
2823  */
2824
2825 static int osd_iam_index_probe(const struct lu_env *env, struct osd_object *o,
2826                            const struct dt_index_features *feat)
2827 {
2828         struct iam_descr *descr;
2829
2830         if (osd_object_is_root(o))
2831                 return feat == &dt_directory_features;
2832
2833         LASSERT(o->oo_dir != NULL);
2834
2835         descr = o->oo_dir->od_container.ic_descr;
2836         if (feat == &dt_directory_features) {
2837                 if (descr->id_rec_size == sizeof(struct osd_fid_pack))
2838                         return 1;
2839                 else
2840                         return 0;
2841         } else {
2842                 return
2843                         feat->dif_keysize_min <= descr->id_key_size &&
2844                         descr->id_key_size <= feat->dif_keysize_max &&
2845                         feat->dif_recsize_min <= descr->id_rec_size &&
2846                         descr->id_rec_size <= feat->dif_recsize_max &&
2847                         !(feat->dif_flags & (DT_IND_VARKEY |
2848                                              DT_IND_VARREC | DT_IND_NONUNQ)) &&
2849                         ergo(feat->dif_flags & DT_IND_UPDATE,
2850                              1 /* XXX check that object (and file system) is
2851                                 * writable */);
2852         }
2853 }
2854
2855 static int osd_iam_container_init(const struct lu_env *env,
2856                                   struct osd_object *obj,
2857                                   struct osd_directory *dir)
2858 {
2859         struct iam_container *bag = &dir->od_container;
2860         int result;
2861
2862         result = iam_container_init(bag, &dir->od_descr, obj->oo_inode);
2863         if (result != 0)
2864                 return result;
2865
2866         result = iam_container_setup(bag);
2867         if (result == 0)
2868                 obj->oo_dt.do_index_ops = &osd_index_iam_ops;
2869         else
2870                 iam_container_fini(bag);
2871
2872         return result;
2873 }
2874
2875
2876 /*
2877  * Concurrency: no external locking is necessary.
2878  */
2879 static int osd_index_try(const struct lu_env *env, struct dt_object *dt,
2880                          const struct dt_index_features *feat)
2881 {
2882         int                      result;
2883         int                      skip_iam = 0;
2884         struct osd_object       *obj = osd_dt_obj(dt);
2885
2886         LINVRNT(osd_invariant(obj));
2887         LASSERT(dt_object_exists(dt));
2888
2889         if (osd_object_is_root(obj)) {
2890                 dt->do_index_ops = &osd_index_ea_ops;
2891                 result = 0;
2892         } else if (feat == &dt_directory_features) {
2893                 dt->do_index_ops = &osd_index_ea_ops;
2894                 if (S_ISDIR(obj->oo_inode->i_mode))
2895                         result = 0;
2896                 else
2897                         result = -ENOTDIR;
2898                 skip_iam = 1;
2899         } else if (unlikely(feat == &dt_otable_features)) {
2900                 dt->do_index_ops = &osd_otable_ops;
2901                 return 0;
2902         } else if (unlikely(feat == &dt_acct_features)) {
2903                 dt->do_index_ops = &osd_acct_index_ops;
2904                 result = 0;
2905                 skip_iam = 1;
2906         } else if (!osd_has_index(obj)) {
2907                 struct osd_directory *dir;
2908
2909                 OBD_ALLOC_PTR(dir);
2910                 if (dir != NULL) {
2911
2912                         spin_lock(&obj->oo_guard);
2913                         if (obj->oo_dir == NULL)
2914                                 obj->oo_dir = dir;
2915                         else
2916                                 /*
2917                                  * Concurrent thread allocated container data.
2918                                  */
2919                                 OBD_FREE_PTR(dir);
2920                         spin_unlock(&obj->oo_guard);
2921                         /*
2922                          * Now, that we have container data, serialize its
2923                          * initialization.
2924                          */
2925                         down_write(&obj->oo_ext_idx_sem);
2926                         /*
2927                          * recheck under lock.
2928                          */
2929                         if (!osd_has_index(obj))
2930                                 result = osd_iam_container_init(env, obj, dir);
2931                         else
2932                                 result = 0;
2933                         up_write(&obj->oo_ext_idx_sem);
2934                 } else {
2935                         result = -ENOMEM;
2936                 }
2937         } else {
2938                 result = 0;
2939         }
2940
2941         if (result == 0 && skip_iam == 0) {
2942                 if (!osd_iam_index_probe(env, obj, feat))
2943                         result = -ENOTDIR;
2944         }
2945         LINVRNT(osd_invariant(obj));
2946
2947         if (is_quota_glb_feat(feat))
2948                 result = osd_quota_migration(env, dt, feat);
2949
2950         return result;
2951 }
2952
2953 static int osd_otable_it_attr_get(const struct lu_env *env,
2954                                  struct dt_object *dt,
2955                                  struct lu_attr *attr,
2956                                  struct lustre_capa *capa)
2957 {
2958         attr->la_valid = 0;
2959         return 0;
2960 }
2961
2962 static const struct dt_object_operations osd_obj_ops = {
2963         .do_read_lock         = osd_object_read_lock,
2964         .do_write_lock        = osd_object_write_lock,
2965         .do_read_unlock       = osd_object_read_unlock,
2966         .do_write_unlock      = osd_object_write_unlock,
2967         .do_write_locked      = osd_object_write_locked,
2968         .do_attr_get          = osd_attr_get,
2969         .do_declare_attr_set  = osd_declare_attr_set,
2970         .do_attr_set          = osd_attr_set,
2971         .do_ah_init           = osd_ah_init,
2972         .do_declare_create    = osd_declare_object_create,
2973         .do_create            = osd_object_create,
2974         .do_declare_destroy   = osd_declare_object_destroy,
2975         .do_destroy           = osd_object_destroy,
2976         .do_index_try         = osd_index_try,
2977         .do_declare_ref_add   = osd_declare_object_ref_add,
2978         .do_ref_add           = osd_object_ref_add,
2979         .do_declare_ref_del   = osd_declare_object_ref_del,
2980         .do_ref_del           = osd_object_ref_del,
2981         .do_xattr_get         = osd_xattr_get,
2982         .do_declare_xattr_set = osd_declare_xattr_set,
2983         .do_xattr_set         = osd_xattr_set,
2984         .do_declare_xattr_del = osd_declare_xattr_del,
2985         .do_xattr_del         = osd_xattr_del,
2986         .do_xattr_list        = osd_xattr_list,
2987         .do_capa_get          = osd_capa_get,
2988         .do_object_sync       = osd_object_sync,
2989         .do_data_get          = osd_data_get,
2990 };
2991
2992 /**
2993  * dt_object_operations for interoperability mode
2994  * (i.e. to run 2.0 mds on 1.8 disk) (b11826)
2995  */
2996 static const struct dt_object_operations osd_obj_ea_ops = {
2997         .do_read_lock         = osd_object_read_lock,
2998         .do_write_lock        = osd_object_write_lock,
2999         .do_read_unlock       = osd_object_read_unlock,
3000         .do_write_unlock      = osd_object_write_unlock,
3001         .do_write_locked      = osd_object_write_locked,
3002         .do_attr_get          = osd_attr_get,
3003         .do_declare_attr_set  = osd_declare_attr_set,
3004         .do_attr_set          = osd_attr_set,
3005         .do_ah_init           = osd_ah_init,
3006         .do_declare_create    = osd_declare_object_create,
3007         .do_create            = osd_object_ea_create,
3008         .do_declare_destroy   = osd_declare_object_destroy,
3009         .do_destroy           = osd_object_destroy,
3010         .do_index_try         = osd_index_try,
3011         .do_declare_ref_add   = osd_declare_object_ref_add,
3012         .do_ref_add           = osd_object_ref_add,
3013         .do_declare_ref_del   = osd_declare_object_ref_del,
3014         .do_ref_del           = osd_object_ref_del,
3015         .do_xattr_get         = osd_xattr_get,
3016         .do_declare_xattr_set = osd_declare_xattr_set,
3017         .do_xattr_set         = osd_xattr_set,
3018         .do_declare_xattr_del = osd_declare_xattr_del,
3019         .do_xattr_del         = osd_xattr_del,
3020         .do_xattr_list        = osd_xattr_list,
3021         .do_capa_get          = osd_capa_get,
3022         .do_object_sync       = osd_object_sync,
3023         .do_data_get          = osd_data_get,
3024 };
3025
3026 static const struct dt_object_operations osd_obj_otable_it_ops = {
3027         .do_attr_get    = osd_otable_it_attr_get,
3028         .do_index_try   = osd_index_try,
3029 };
3030
3031 static int osd_index_declare_iam_delete(const struct lu_env *env,
3032                                         struct dt_object *dt,
3033                                         const struct dt_key *key,
3034                                         struct thandle *handle)
3035 {
3036         struct osd_thandle    *oh;
3037
3038         oh = container_of0(handle, struct osd_thandle, ot_super);
3039         LASSERT(oh->ot_handle == NULL);
3040
3041         osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3042                              osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3043
3044         return 0;
3045 }
3046
3047 /**
3048  *      delete a (key, value) pair from index \a dt specified by \a key
3049  *
3050  *      \param  dt      osd index object
3051  *      \param  key     key for index
3052  *      \param  rec     record reference
3053  *      \param  handle  transaction handler
3054  *
3055  *      \retval  0  success
3056  *      \retval -ve   failure
3057  */
3058
3059 static int osd_index_iam_delete(const struct lu_env *env, struct dt_object *dt,
3060                                 const struct dt_key *key,
3061                                 struct thandle *handle,
3062                                 struct lustre_capa *capa)
3063 {
3064         struct osd_thread_info *oti = osd_oti_get(env);
3065         struct osd_object      *obj = osd_dt_obj(dt);
3066         struct osd_thandle     *oh;
3067         struct iam_path_descr  *ipd;
3068         struct iam_container   *bag = &obj->oo_dir->od_container;
3069         int                     rc;
3070
3071         ENTRY;
3072
3073         LINVRNT(osd_invariant(obj));
3074         LASSERT(dt_object_exists(dt));
3075         LASSERT(bag->ic_object == obj->oo_inode);
3076         LASSERT(handle != NULL);
3077
3078         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3079                 RETURN(-EACCES);
3080
3081         osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3082
3083         ipd = osd_idx_ipd_get(env, bag);
3084         if (unlikely(ipd == NULL))
3085                 RETURN(-ENOMEM);
3086
3087         oh = container_of0(handle, struct osd_thandle, ot_super);
3088         LASSERT(oh->ot_handle != NULL);
3089         LASSERT(oh->ot_handle->h_transaction != NULL);
3090
3091         if (fid_is_quota(lu_object_fid(&dt->do_lu))) {
3092                 /* swab quota uid/gid provided by caller */
3093                 oti->oti_quota_id = cpu_to_le64(*((__u64 *)key));
3094                 key = (const struct dt_key *)&oti->oti_quota_id;
3095         }
3096
3097         rc = iam_delete(oh->ot_handle, bag, (const struct iam_key *)key, ipd);
3098         osd_ipd_put(env, bag, ipd);
3099         LINVRNT(osd_invariant(obj));
3100         RETURN(rc);
3101 }
3102
3103 static int osd_index_declare_ea_delete(const struct lu_env *env,
3104                                        struct dt_object *dt,
3105                                        const struct dt_key *key,
3106                                        struct thandle *handle)
3107 {
3108         struct osd_thandle *oh;
3109         struct inode       *inode;
3110         int                 rc;
3111         ENTRY;
3112
3113         LASSERT(dt_object_exists(dt));
3114         LASSERT(handle != NULL);
3115
3116         oh = container_of0(handle, struct osd_thandle, ot_super);
3117         LASSERT(oh->ot_handle == NULL);
3118
3119         osd_trans_declare_op(env, oh, OSD_OT_DELETE,
3120                              osd_dto_credits_noquota[DTO_INDEX_DELETE]);
3121
3122         inode = osd_dt_obj(dt)->oo_inode;
3123         LASSERT(inode);
3124
3125         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
3126                                    true, true, NULL, false);
3127         RETURN(rc);
3128 }
3129
3130 static inline int osd_get_fid_from_dentry(struct ldiskfs_dir_entry_2 *de,
3131                                           struct dt_rec *fid)
3132 {
3133         struct osd_fid_pack *rec;
3134         int                  rc = -ENODATA;
3135
3136         if (de->file_type & LDISKFS_DIRENT_LUFID) {
3137                 rec = (struct osd_fid_pack *) (de->name + de->name_len + 1);
3138                 rc = osd_fid_unpack((struct lu_fid *)fid, rec);
3139         }
3140         RETURN(rc);
3141 }
3142
3143 static int osd_remote_fid(const struct lu_env *env, struct osd_device *osd,
3144                           struct lu_fid *fid)
3145 {
3146         struct lu_seq_range     *range = &osd_oti_get(env)->oti_seq_range;
3147         struct seq_server_site  *ss = osd_seq_site(osd);
3148         int                     rc;
3149         ENTRY;
3150
3151         if ((!fid_is_norm(fid) && !fid_is_igif(fid)) || ss == NULL)
3152                 RETURN(0);
3153
3154         rc = osd_fld_lookup(env, osd, fid, range);
3155         if (rc != 0) {
3156                 CERROR("%s: Can not lookup fld for "DFID"\n",
3157                        osd_name(osd), PFID(fid));
3158                 RETURN(rc);
3159         }
3160
3161         RETURN(ss->ss_node_id != range->lsr_index);
3162 }
3163
3164 /**
3165  * Index delete function for interoperability mode (b11826).
3166  * It will remove the directory entry added by osd_index_ea_insert().
3167  * This entry is needed to maintain name->fid mapping.
3168  *
3169  * \param key,  key i.e. file entry to be deleted
3170  *
3171  * \retval   0, on success
3172  * \retval -ve, on error
3173  */
3174 static int osd_index_ea_delete(const struct lu_env *env, struct dt_object *dt,
3175                                const struct dt_key *key,
3176                                struct thandle *handle,
3177                                struct lustre_capa *capa)
3178 {
3179         struct osd_object          *obj    = osd_dt_obj(dt);
3180         struct inode               *dir    = obj->oo_inode;
3181         struct dentry              *dentry;
3182         struct osd_thandle         *oh;
3183         struct ldiskfs_dir_entry_2 *de = NULL;
3184         struct buffer_head         *bh;
3185         struct htree_lock          *hlock = NULL;
3186         struct lu_fid              *fid = &osd_oti_get(env)->oti_fid;
3187         struct osd_device          *osd = osd_dev(dt->do_lu.lo_dev);
3188         int                        rc;
3189         ENTRY;
3190
3191         LINVRNT(osd_invariant(obj));
3192         LASSERT(dt_object_exists(dt));
3193         LASSERT(handle != NULL);
3194
3195         osd_trans_exec_op(env, handle, OSD_OT_DELETE);
3196
3197         oh = container_of(handle, struct osd_thandle, ot_super);
3198         LASSERT(oh->ot_handle != NULL);
3199         LASSERT(oh->ot_handle->h_transaction != NULL);
3200
3201         if (osd_object_auth(env, dt, capa, CAPA_OPC_INDEX_DELETE))
3202                 RETURN(-EACCES);
3203
3204         ll_vfs_dq_init(dir);
3205         dentry = osd_child_dentry_get(env, obj,
3206                                       (char *)key, strlen((char *)key));
3207
3208         if (obj->oo_hl_head != NULL) {
3209                 hlock = osd_oti_get(env)->oti_hlock;
3210                 ldiskfs_htree_lock(hlock, obj->oo_hl_head,
3211                                    dir, LDISKFS_HLOCK_DEL);
3212         } else {
3213                 down_write(&obj->oo_ext_idx_sem);
3214         }
3215