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