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