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