Whamcloud - gitweb
LU-3336 lfsck: regenerate lost layout EA
[fs/lustre-release.git] / lustre / osp / osp_object.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) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osp/osp_object.c
37  *
38  * Lustre OST Proxy Device
39  *
40  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41  * Author: Mikhail Pershin <mike.tappro@intel.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_MDS
45
46 #include "osp_internal.h"
47
48 static inline bool is_ost_obj(struct lu_object *lo)
49 {
50         return !lu2osp_dev(lo->lo_dev)->opd_connect_mdt;
51 }
52
53 static void osp_object_assign_fid(const struct lu_env *env,
54                                  struct osp_device *d, struct osp_object *o)
55 {
56         struct osp_thread_info *osi = osp_env_info(env);
57
58         LASSERT(fid_is_zero(lu_object_fid(&o->opo_obj.do_lu)));
59         LASSERT(o->opo_reserved);
60         o->opo_reserved = 0;
61
62         osp_precreate_get_fid(env, d, &osi->osi_fid);
63
64         lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
65 }
66
67 static int osp_oac_init(struct osp_object *obj)
68 {
69         struct osp_object_attr *ooa;
70
71         OBD_ALLOC_PTR(ooa);
72         if (ooa == NULL)
73                 return -ENOMEM;
74
75         INIT_LIST_HEAD(&ooa->ooa_xattr_list);
76         spin_lock(&obj->opo_lock);
77         if (likely(obj->opo_ooa == NULL)) {
78                 obj->opo_ooa = ooa;
79                 spin_unlock(&obj->opo_lock);
80         } else {
81                 spin_unlock(&obj->opo_lock);
82                 OBD_FREE_PTR(ooa);
83         }
84
85         return 0;
86 }
87
88 static struct osp_xattr_entry *
89 osp_oac_xattr_find_locked(struct osp_object_attr *ooa,
90                           const char *name, int namelen, bool unlink)
91 {
92         struct osp_xattr_entry *oxe;
93
94         list_for_each_entry(oxe, &ooa->ooa_xattr_list, oxe_list) {
95                 if (namelen == oxe->oxe_namelen &&
96                     strncmp(name, oxe->oxe_buf, namelen) == 0) {
97                         if (unlink)
98                                 list_del_init(&oxe->oxe_list);
99                         else
100                                 atomic_inc(&oxe->oxe_ref);
101
102                         return oxe;
103                 }
104         }
105
106         return NULL;
107 }
108
109 static struct osp_xattr_entry *osp_oac_xattr_find(struct osp_object *obj,
110                                                   const char *name)
111 {
112         struct osp_xattr_entry *oxe = NULL;
113
114         spin_lock(&obj->opo_lock);
115         if (obj->opo_ooa != NULL)
116                 oxe = osp_oac_xattr_find_locked(obj->opo_ooa, name,
117                                                 strlen(name), false);
118         spin_unlock(&obj->opo_lock);
119
120         return oxe;
121 }
122
123 static struct osp_xattr_entry *
124 osp_oac_xattr_find_or_add(struct osp_object *obj, const char *name, int len)
125 {
126         struct osp_object_attr *ooa     = obj->opo_ooa;
127         struct osp_xattr_entry *oxe;
128         struct osp_xattr_entry *tmp     = NULL;
129         int                     namelen = strlen(name);
130         int                     size    = sizeof(*oxe) + namelen + 1 + len;
131
132         LASSERT(ooa != NULL);
133
134         oxe = osp_oac_xattr_find(obj, name);
135         if (oxe != NULL)
136                 return oxe;
137
138         OBD_ALLOC(oxe, size);
139         if (unlikely(oxe == NULL))
140                 return NULL;
141
142         INIT_LIST_HEAD(&oxe->oxe_list);
143         oxe->oxe_buflen = size;
144         oxe->oxe_namelen = namelen;
145         memcpy(oxe->oxe_buf, name, namelen);
146         oxe->oxe_value = oxe->oxe_buf + namelen + 1;
147         /* One ref is for the caller, the other is for the entry on the list. */
148         atomic_set(&oxe->oxe_ref, 2);
149
150         spin_lock(&obj->opo_lock);
151         tmp = osp_oac_xattr_find_locked(ooa, name, namelen, false);
152         if (tmp == NULL)
153                 list_add_tail(&oxe->oxe_list, &ooa->ooa_xattr_list);
154         spin_unlock(&obj->opo_lock);
155
156         if (tmp != NULL) {
157                 OBD_FREE(oxe, size);
158                 oxe = tmp;
159         }
160
161         return oxe;
162 }
163
164 static struct osp_xattr_entry *
165 osp_oac_xattr_replace(struct osp_object *obj,
166                       struct osp_xattr_entry **poxe, int len)
167 {
168         struct osp_object_attr *ooa     = obj->opo_ooa;
169         struct osp_xattr_entry *old     = *poxe;
170         struct osp_xattr_entry *oxe;
171         struct osp_xattr_entry *tmp     = NULL;
172         int                     namelen = old->oxe_namelen;
173         int                     size    = sizeof(*oxe) + namelen + 1 + len;
174
175         LASSERT(ooa != NULL);
176
177         OBD_ALLOC(oxe, size);
178         if (unlikely(oxe == NULL))
179                 return NULL;
180
181         INIT_LIST_HEAD(&oxe->oxe_list);
182         oxe->oxe_buflen = size;
183         oxe->oxe_namelen = namelen;
184         memcpy(oxe->oxe_buf, old->oxe_buf, namelen);
185         oxe->oxe_value = oxe->oxe_buf + namelen + 1;
186         /* One ref is for the caller, the other is for the entry on the list. */
187         atomic_set(&oxe->oxe_ref, 2);
188
189         spin_lock(&obj->opo_lock);
190         tmp = osp_oac_xattr_find_locked(ooa, oxe->oxe_buf, namelen, true);
191         list_add_tail(&oxe->oxe_list, &ooa->ooa_xattr_list);
192         spin_unlock(&obj->opo_lock);
193
194         *poxe = tmp;
195         LASSERT(tmp != NULL);
196
197         return oxe;
198 }
199
200 static inline void osp_oac_xattr_put(struct osp_xattr_entry *oxe)
201 {
202         if (atomic_dec_and_test(&oxe->oxe_ref)) {
203                 LASSERT(list_empty(&oxe->oxe_list));
204
205                 OBD_FREE(oxe, oxe->oxe_buflen);
206         }
207 }
208
209 static int osp_get_attr_from_reply(const struct lu_env *env,
210                                    struct update_reply *reply,
211                                    struct lu_attr *attr,
212                                    struct osp_object *obj, int index)
213 {
214         struct osp_thread_info  *osi    = osp_env_info(env);
215         struct lu_buf           *rbuf   = &osi->osi_lb2;
216         struct obdo             *lobdo  = &osi->osi_obdo;
217         struct obdo             *wobdo;
218         int                      rc;
219
220         rc = update_get_reply_buf(reply, rbuf, index);
221         if (rc < 0)
222                 return rc;
223
224         wobdo = rbuf->lb_buf;
225         if (rbuf->lb_len != sizeof(*wobdo))
226                 return -EPROTO;
227
228         obdo_le_to_cpu(wobdo, wobdo);
229         lustre_get_wire_obdo(NULL, lobdo, wobdo);
230         spin_lock(&obj->opo_lock);
231         if (obj->opo_ooa != NULL) {
232                 la_from_obdo(&obj->opo_ooa->ooa_attr, lobdo, lobdo->o_valid);
233                 if (attr != NULL)
234                         *attr = obj->opo_ooa->ooa_attr;
235         } else {
236                 LASSERT(attr != NULL);
237
238                 la_from_obdo(attr, lobdo, lobdo->o_valid);
239         }
240         spin_unlock(&obj->opo_lock);
241
242         return 0;
243 }
244
245 static int osp_attr_get_interpterer(const struct lu_env *env,
246                                     struct update_reply *reply,
247                                     struct osp_object *obj,
248                                     void *data, int index, int rc)
249 {
250         struct lu_attr *attr = data;
251
252         LASSERT(obj->opo_ooa != NULL);
253
254         if (rc == 0) {
255                 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
256                 obj->opo_non_exist = 0;
257
258                 return osp_get_attr_from_reply(env, reply, NULL, obj, index);
259         } else {
260                 if (rc == -ENOENT) {
261                         osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
262                         obj->opo_non_exist = 1;
263                 }
264
265                 spin_lock(&obj->opo_lock);
266                 attr->la_valid = 0;
267                 spin_unlock(&obj->opo_lock);
268         }
269
270         return 0;
271 }
272
273 static int osp_declare_attr_get(const struct lu_env *env, struct dt_object *dt,
274                                 struct lustre_capa *capa)
275 {
276         struct osp_object       *obj    = dt2osp_obj(dt);
277         struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
278         struct update_request   *update;
279         int                      rc     = 0;
280
281         if (obj->opo_ooa == NULL) {
282                 rc = osp_oac_init(obj);
283                 if (rc != 0)
284                         return rc;
285         }
286
287         mutex_lock(&osp->opd_async_requests_mutex);
288         update = osp_find_or_create_async_update_request(osp);
289         if (IS_ERR(update))
290                 rc = PTR_ERR(update);
291         else
292                 rc = osp_insert_async_update(env, update, OBJ_ATTR_GET, obj, 0,
293                                              NULL, NULL,
294                                              &obj->opo_ooa->ooa_attr,
295                                              osp_attr_get_interpterer);
296         mutex_unlock(&osp->opd_async_requests_mutex);
297
298         return rc;
299 }
300
301 int osp_attr_get(const struct lu_env *env, struct dt_object *dt,
302                  struct lu_attr *attr, struct lustre_capa *capa)
303 {
304         struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
305         struct osp_object       *obj    = dt2osp_obj(dt);
306         struct dt_device        *dev    = &osp->opd_dt_dev;
307         struct update_request   *update;
308         struct update_reply     *reply;
309         struct ptlrpc_request   *req    = NULL;
310         int                      rc     = 0;
311         ENTRY;
312
313         if (is_ost_obj(&dt->do_lu) && obj->opo_non_exist)
314                 RETURN(-ENOENT);
315
316         if (obj->opo_ooa != NULL) {
317                 spin_lock(&obj->opo_lock);
318                 if (obj->opo_ooa->ooa_attr.la_valid != 0) {
319                         *attr = obj->opo_ooa->ooa_attr;
320                         spin_unlock(&obj->opo_lock);
321
322                         RETURN(0);
323                 }
324                 spin_unlock(&obj->opo_lock);
325         }
326
327         update = out_create_update_req(dev);
328         if (IS_ERR(update))
329                 RETURN(PTR_ERR(update));
330
331         rc = out_insert_update(env, update, OBJ_ATTR_GET,
332                                lu_object_fid(&dt->do_lu), 0, NULL, NULL);
333         if (rc != 0) {
334                 CERROR("%s: Insert update error "DFID": rc = %d\n",
335                        dev->dd_lu_dev.ld_obd->obd_name,
336                        PFID(lu_object_fid(&dt->do_lu)), rc);
337
338                 GOTO(out, rc);
339         }
340
341         rc = out_remote_sync(env, osp->opd_obd->u.cli.cl_import, update, &req);
342         if (rc != 0) {
343                 if (rc == -ENOENT) {
344                         osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
345                         obj->opo_non_exist = 1;
346                 } else {
347                         CERROR("%s:osp_attr_get update error "DFID": rc = %d\n",
348                                dev->dd_lu_dev.ld_obd->obd_name,
349                                PFID(lu_object_fid(&dt->do_lu)), rc);
350                 }
351
352                 GOTO(out, rc);
353         }
354
355         osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
356         obj->opo_non_exist = 0;
357         reply = req_capsule_server_sized_get(&req->rq_pill, &RMF_UPDATE_REPLY,
358                                              UPDATE_BUFFER_SIZE);
359         if (reply == NULL || reply->ur_version != UPDATE_REPLY_V1)
360                 GOTO(out, rc = -EPROTO);
361
362         rc = osp_get_attr_from_reply(env, reply, attr, obj, 0);
363         if (rc != 0)
364                 GOTO(out, rc);
365
366         if (!is_ost_obj(&dt->do_lu)) {
367                 if (attr->la_flags == 1)
368                         obj->opo_empty = 0;
369                 else
370                         obj->opo_empty = 1;
371         }
372
373         GOTO(out, rc = 0);
374
375 out:
376         if (req != NULL)
377                 ptlrpc_req_finished(req);
378
379         out_destroy_update_req(update);
380
381         return rc;
382 }
383
384 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
385                                 const struct lu_attr *attr, struct thandle *th)
386 {
387         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
388         struct osp_object       *o = dt2osp_obj(dt);
389         struct lu_attr          *la;
390         int                      rc = 0;
391
392         ENTRY;
393
394         /*
395          * Usually we don't allow server stack to manipulate size
396          * but there is a special case when striping is created
397          * late, after stripless file got truncated to non-zero.
398          *
399          * In this case we do the following:
400          *
401          * 1) grab id in declare - this can lead to leaked OST objects
402          *    but we don't currently have proper mechanism and the only
403          *    options we have are to do truncate RPC holding transaction
404          *    open (very bad) or to grab id in declare at cost of leaked
405          *    OST object in same very rare unfortunate case (just bad)
406          *    notice 1.6-2.0 do assignment outside of running transaction
407          *    all the time, meaning many more chances for leaked objects.
408          *
409          * 2) send synchronous truncate RPC with just assigned id
410          */
411
412         /* there are few places in MDD code still passing NULL
413          * XXX: to be fixed soon */
414         if (attr == NULL)
415                 RETURN(0);
416
417         if (attr->la_valid & LA_SIZE && attr->la_size > 0 &&
418             fid_is_zero(lu_object_fid(&o->opo_obj.do_lu))) {
419                 LASSERT(!dt_object_exists(dt));
420                 osp_object_assign_fid(env, d, o);
421                 rc = osp_object_truncate(env, dt, attr->la_size);
422                 if (rc)
423                         RETURN(rc);
424         }
425
426         if (o->opo_new) {
427                 /* no need in logging for new objects being created */
428                 RETURN(0);
429         }
430
431         if (!(attr->la_valid & (LA_UID | LA_GID)))
432                 RETURN(0);
433
434         if (!is_only_remote_trans(th))
435                 /*
436                  * track all UID/GID changes via llog
437                  */
438                 rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
439         else
440                 /* It is for OST-object attr_set directly without updating
441                  * local MDT-object attribute. It is usually used by LFSCK. */
442                 rc = osp_md_declare_attr_set(env, dt, attr, th);
443
444         if (rc != 0 || o->opo_ooa == NULL)
445                 RETURN(rc);
446
447         la = &o->opo_ooa->ooa_attr;
448         spin_lock(&o->opo_lock);
449         if (attr->la_valid & LA_UID) {
450                 la->la_uid = attr->la_uid;
451                 la->la_valid |= LA_UID;
452         }
453
454         if (attr->la_valid & LA_GID) {
455                 la->la_gid = attr->la_gid;
456                 la->la_valid |= LA_GID;
457         }
458         spin_unlock(&o->opo_lock);
459
460         RETURN(0);
461 }
462
463 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
464                         const struct lu_attr *attr, struct thandle *th,
465                         struct lustre_capa *capa)
466 {
467         struct osp_object       *o = dt2osp_obj(dt);
468         int                      rc = 0;
469
470         ENTRY;
471
472         /* we're interested in uid/gid changes only */
473         if (!(attr->la_valid & (LA_UID | LA_GID)))
474                 RETURN(0);
475
476         /* new object, the very first ->attr_set()
477          * initializing attributes needs no logging
478          * all subsequent one are subject to the
479          * logging and synchronization with OST */
480         if (o->opo_new) {
481                 o->opo_new = 0;
482                 RETURN(0);
483         }
484
485         if (!is_only_remote_trans(th))
486                 /*
487                  * once transaction is committed put proper command on
488                  * the queue going to our OST
489                  */
490                 rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
491                 /* XXX: send new uid/gid to OST ASAP? */
492         else
493                 /* It is for OST-object attr_set directly without updating
494                  * local MDT-object attribute. It is usually used by LFSCK. */
495                 rc = osp_md_attr_set(env, dt, attr, th, capa);
496
497         RETURN(rc);
498 }
499
500 static int osp_xattr_get_interpterer(const struct lu_env *env,
501                                      struct update_reply *reply,
502                                      struct osp_object *obj,
503                                      void *data, int index, int rc)
504 {
505         struct osp_object_attr  *ooa  = obj->opo_ooa;
506         struct osp_xattr_entry  *oxe  = data;
507         struct lu_buf           *rbuf = &osp_env_info(env)->osi_lb2;
508
509         LASSERT(ooa != NULL);
510
511         if (rc == 0) {
512                 int len = sizeof(*oxe) + oxe->oxe_namelen + 1;
513
514                 rc = update_get_reply_buf(reply, rbuf, index);
515                 if (rc < 0 || rbuf->lb_len > (oxe->oxe_buflen - len)) {
516                         spin_lock(&obj->opo_lock);
517                         oxe->oxe_ready = 0;
518                         spin_unlock(&obj->opo_lock);
519                         osp_oac_xattr_put(oxe);
520
521                         return rc < 0 ? rc : -ERANGE;
522                 }
523
524                 spin_lock(&obj->opo_lock);
525                 oxe->oxe_vallen = rbuf->lb_len;
526                 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
527                 oxe->oxe_exist = 1;
528                 oxe->oxe_ready = 1;
529                 spin_unlock(&obj->opo_lock);
530         } else if (rc == -ENOENT || rc == -ENODATA) {
531                 spin_lock(&obj->opo_lock);
532                 oxe->oxe_exist = 0;
533                 oxe->oxe_ready = 1;
534                 spin_unlock(&obj->opo_lock);
535         } else {
536                 spin_lock(&obj->opo_lock);
537                 oxe->oxe_ready = 0;
538                 spin_unlock(&obj->opo_lock);
539         }
540
541         osp_oac_xattr_put(oxe);
542
543         return 0;
544 }
545
546 static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
547                                  struct lu_buf *buf, const char *name,
548                                  struct lustre_capa *capa)
549 {
550         struct osp_object       *obj     = dt2osp_obj(dt);
551         struct osp_device       *osp     = lu2osp_dev(dt->do_lu.lo_dev);
552         struct update_request   *update;
553         struct osp_xattr_entry  *oxe;
554         int                      namelen = strlen(name);
555         int                      rc      = 0;
556
557         LASSERT(buf != NULL);
558         LASSERT(name != NULL);
559
560         /* If only for xattr size, return directly. */
561         if (unlikely(buf->lb_len == 0))
562                 return 0;
563
564         if (obj->opo_ooa == NULL) {
565                 rc = osp_oac_init(obj);
566                 if (rc != 0)
567                         return rc;
568         }
569
570         oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
571         if (oxe == NULL)
572                 return -ENOMEM;
573
574         mutex_lock(&osp->opd_async_requests_mutex);
575         update = osp_find_or_create_async_update_request(osp);
576         if (IS_ERR(update)) {
577                 rc = PTR_ERR(update);
578                 mutex_unlock(&osp->opd_async_requests_mutex);
579                 osp_oac_xattr_put(oxe);
580         } else {
581                 rc = osp_insert_async_update(env, update, OBJ_XATTR_GET, obj,
582                                              1, &namelen, &name, oxe,
583                                              osp_xattr_get_interpterer);
584                 if (rc != 0) {
585                         mutex_unlock(&osp->opd_async_requests_mutex);
586                         osp_oac_xattr_put(oxe);
587                 } else {
588                         /* XXX: Currently, we trigger the batched async OUT
589                          *      RPC via dt_declare_xattr_get(). It is not
590                          *      perfect solution, but works well now.
591                          *
592                          *      We will improve it in the future. */
593                         update = osp->opd_async_requests;
594                         if (update != NULL && update->ur_buf != NULL &&
595                             update->ur_buf->ub_count > 0) {
596                                 osp->opd_async_requests = NULL;
597                                 mutex_unlock(&osp->opd_async_requests_mutex);
598                                 rc = osp_unplug_async_update(env, osp, update);
599                         } else {
600                                 mutex_unlock(&osp->opd_async_requests_mutex);
601                         }
602                 }
603         }
604
605         return rc;
606 }
607
608 int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
609                   struct lu_buf *buf, const char *name,
610                   struct lustre_capa *capa)
611 {
612         struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
613         struct osp_object       *obj    = dt2osp_obj(dt);
614         struct dt_device        *dev    = &osp->opd_dt_dev;
615         struct lu_buf           *rbuf   = &osp_env_info(env)->osi_lb2;
616         struct update_request   *update = NULL;
617         struct ptlrpc_request   *req    = NULL;
618         struct update_reply     *reply;
619         struct osp_xattr_entry  *oxe    = NULL;
620         const char              *dname  = dt->do_lu.lo_dev->ld_obd->obd_name;
621         int                      namelen;
622         int                      rc     = 0;
623         ENTRY;
624
625         LASSERT(buf != NULL);
626         LASSERT(name != NULL);
627
628         if (unlikely(obj->opo_non_exist))
629                 RETURN(-ENOENT);
630
631         oxe = osp_oac_xattr_find(obj, name);
632         if (oxe != NULL) {
633                 spin_lock(&obj->opo_lock);
634                 if (oxe->oxe_ready) {
635                         if (!oxe->oxe_exist)
636                                 GOTO(unlock, rc = -ENODATA);
637
638                         if (buf->lb_buf == NULL)
639                                 GOTO(unlock, rc = oxe->oxe_vallen);
640
641                         if (buf->lb_len < oxe->oxe_vallen)
642                                 GOTO(unlock, rc = -ERANGE);
643
644                         memcpy(buf->lb_buf, oxe->oxe_value, oxe->oxe_vallen);
645
646                         GOTO(unlock, rc = oxe->oxe_vallen);
647
648 unlock:
649                         spin_unlock(&obj->opo_lock);
650                         osp_oac_xattr_put(oxe);
651
652                         return rc;
653                 }
654                 spin_unlock(&obj->opo_lock);
655         }
656
657         update = out_create_update_req(dev);
658         if (IS_ERR(update))
659                 GOTO(out, rc = PTR_ERR(update));
660
661         namelen = strlen(name);
662         rc = out_insert_update(env, update, OBJ_XATTR_GET,
663                                lu_object_fid(&dt->do_lu), 1, &namelen, &name);
664         if (rc != 0) {
665                 CERROR("%s: Insert update error "DFID": rc = %d\n",
666                        dname, PFID(lu_object_fid(&dt->do_lu)), rc);
667
668                 GOTO(out, rc);
669         }
670
671         rc = out_remote_sync(env, osp->opd_obd->u.cli.cl_import, update, &req);
672         if (rc != 0) {
673                 if (obj->opo_ooa == NULL)
674                         GOTO(out, rc);
675
676                 if (oxe == NULL)
677                         oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
678
679                 if (oxe == NULL) {
680                         CWARN("%s: Fail to add xattr (%s) to cache for "
681                               DFID" (1): rc = %d\n", dname, name,
682                               PFID(lu_object_fid(&dt->do_lu)), rc);
683
684                         GOTO(out, rc);
685                 }
686
687                 spin_lock(&obj->opo_lock);
688                 if (rc == -ENOENT || rc == -ENODATA) {
689                         oxe->oxe_exist = 0;
690                         oxe->oxe_ready = 1;
691                 } else {
692                         oxe->oxe_ready = 0;
693                 }
694                 spin_unlock(&obj->opo_lock);
695
696                 GOTO(out, rc);
697         }
698
699         reply = req_capsule_server_sized_get(&req->rq_pill, &RMF_UPDATE_REPLY,
700                                             UPDATE_BUFFER_SIZE);
701         if (reply->ur_version != UPDATE_REPLY_V1) {
702                 CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
703                        dname, reply->ur_version, UPDATE_REPLY_V1,
704                        PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
705
706                 GOTO(out, rc = -EPROTO);
707         }
708
709         rc = update_get_reply_buf(reply, rbuf, 0);
710         if (rc < 0)
711                 GOTO(out, rc);
712
713         LASSERT(rbuf->lb_len > 0 && rbuf->lb_len < PAGE_CACHE_SIZE);
714
715         if (buf->lb_buf == NULL)
716                 GOTO(out, rc = rbuf->lb_len);
717
718         if (unlikely(buf->lb_len < rbuf->lb_len))
719                 GOTO(out, rc = -ERANGE);
720
721         memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
722         rc = rbuf->lb_len;
723         if (obj->opo_ooa == NULL)
724                 GOTO(out, rc);
725
726         if (oxe == NULL) {
727                 oxe = osp_oac_xattr_find_or_add(obj, name, rbuf->lb_len);
728                 if (oxe == NULL) {
729                         CWARN("%s: Fail to add xattr (%s) to "
730                               "cache for "DFID" (2): rc = %d\n",
731                               dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
732
733                         GOTO(out, rc);
734                 }
735         }
736
737         if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < rbuf->lb_len) {
738                 struct osp_xattr_entry *old = oxe;
739                 struct osp_xattr_entry *tmp;
740
741                 tmp = osp_oac_xattr_replace(obj, &old, rbuf->lb_len);
742                 osp_oac_xattr_put(oxe);
743                 oxe = tmp;
744                 if (tmp == NULL) {
745                         CWARN("%s: Fail to update xattr (%s) to "
746                               "cache for "DFID": rc = %d\n",
747                               dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
748                         spin_lock(&obj->opo_lock);
749                         oxe->oxe_ready = 0;
750                         spin_unlock(&obj->opo_lock);
751
752                         GOTO(out, rc);
753                 }
754
755                 /* Drop the ref for entry on list. */
756                 osp_oac_xattr_put(old);
757         }
758
759         spin_lock(&obj->opo_lock);
760         oxe->oxe_vallen = rbuf->lb_len;
761         memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
762         oxe->oxe_exist = 1;
763         oxe->oxe_ready = 1;
764         spin_unlock(&obj->opo_lock);
765
766         GOTO(out, rc);
767
768 out:
769         if (req != NULL)
770                 ptlrpc_req_finished(req);
771
772         if (update != NULL && !IS_ERR(update))
773                 out_destroy_update_req(update);
774
775         if (oxe != NULL)
776                 osp_oac_xattr_put(oxe);
777
778         return rc;
779 }
780
781 int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
782                           const struct lu_buf *buf, const char *name,
783                           int flag, struct thandle *th)
784 {
785         struct osp_object       *o       = dt2osp_obj(dt);
786         struct update_request   *update;
787         struct lu_fid           *fid;
788         struct osp_xattr_entry  *oxe;
789         int                     sizes[3] = {strlen(name), buf->lb_len,
790                                             sizeof(int)};
791         char                    *bufs[3] = {(char *)name, (char *)buf->lb_buf };
792         int                     rc;
793
794         LASSERT(buf->lb_len > 0 && buf->lb_buf != NULL);
795
796         update = out_find_create_update_loc(th, dt);
797         if (IS_ERR(update)) {
798                 CERROR("%s: Get OSP update buf failed "DFID": rc = %d\n",
799                        dt->do_lu.lo_dev->ld_obd->obd_name,
800                        PFID(lu_object_fid(&dt->do_lu)),
801                        (int)PTR_ERR(update));
802
803                 return PTR_ERR(update);
804         }
805
806         flag = cpu_to_le32(flag);
807         bufs[2] = (char *)&flag;
808
809         fid = (struct lu_fid *)lu_object_fid(&dt->do_lu);
810         rc = out_insert_update(env, update, OBJ_XATTR_SET, fid,
811                                ARRAY_SIZE(sizes), sizes, (const char **)bufs);
812         if (rc != 0 || o->opo_ooa == NULL)
813                 return rc;
814
815         oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
816         if (oxe == NULL) {
817                 CWARN("%s: Fail to add xattr (%s) to cache for "DFID
818                       ": rc = %d\n", dt->do_lu.lo_dev->ld_obd->obd_name,
819                       name, PFID(lu_object_fid(&dt->do_lu)), rc);
820
821                 return 0;
822         }
823
824         if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < buf->lb_len) {
825                 struct osp_xattr_entry *old = oxe;
826                 struct osp_xattr_entry *tmp;
827
828                 tmp = osp_oac_xattr_replace(o, &old, buf->lb_len);
829                 osp_oac_xattr_put(oxe);
830                 oxe = tmp;
831                 if (tmp == NULL) {
832                         CWARN("%s: Fail to update xattr (%s) to cache for "DFID
833                               ": rc = %d\n", dt->do_lu.lo_dev->ld_obd->obd_name,
834                               name, PFID(lu_object_fid(&dt->do_lu)), rc);
835                         spin_lock(&o->opo_lock);
836                         oxe->oxe_ready = 0;
837                         spin_unlock(&o->opo_lock);
838
839                         return 0;
840                 }
841
842                 /* Drop the ref for entry on list. */
843                 osp_oac_xattr_put(old);
844         }
845
846         spin_lock(&o->opo_lock);
847         oxe->oxe_vallen = buf->lb_len;
848         memcpy(oxe->oxe_value, buf->lb_buf, buf->lb_len);
849         oxe->oxe_exist = 1;
850         oxe->oxe_ready = 1;
851         spin_unlock(&o->opo_lock);
852         osp_oac_xattr_put(oxe);
853
854         return 0;
855 }
856
857 int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
858                   const struct lu_buf *buf, const char *name, int fl,
859                   struct thandle *th, struct lustre_capa *capa)
860 {
861         CDEBUG(D_INFO, "xattr %s set object "DFID"\n", name,
862                PFID(&dt->do_lu.lo_header->loh_fid));
863
864         return 0;
865 }
866
867 static int osp_declare_object_create(const struct lu_env *env,
868                                      struct dt_object *dt,
869                                      struct lu_attr *attr,
870                                      struct dt_allocation_hint *hint,
871                                      struct dt_object_format *dof,
872                                      struct thandle *th)
873 {
874         struct osp_thread_info  *osi = osp_env_info(env);
875         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
876         struct osp_object       *o = dt2osp_obj(dt);
877         const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
878         int                      rc = 0;
879
880         ENTRY;
881
882         if (is_only_remote_trans(th)) {
883                 LASSERT(fid_is_sane(fid));
884
885                 rc = osp_md_declare_object_create(env, dt, attr, hint, dof, th);
886
887                 RETURN(rc);
888         }
889
890         /* should happen to non-0 OSP only so that at least one object
891          * has been already declared in the scenario and LOD should
892          * cleanup that */
893         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
894                 RETURN(-ENOSPC);
895
896         LASSERT(d->opd_last_used_oid_file);
897
898         /*
899          * There can be gaps in precreated ids and record to unlink llog
900          * XXX: we do not handle gaps yet, implemented before solution
901          *      was found to be racy, so we disabled that. there is no
902          *      point in making useless but expensive llog declaration.
903          */
904         /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
905
906         if (unlikely(!fid_is_zero(fid))) {
907                 /* replay case: caller knows fid */
908                 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
909                 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
910                                              sizeof(osi->osi_id), osi->osi_off,
911                                              th);
912                 RETURN(rc);
913         }
914
915         /*
916          * in declaration we need to reserve object so that we don't block
917          * awaiting precreation RPC to complete
918          */
919         rc = osp_precreate_reserve(env, d);
920         /*
921          * we also need to declare update to local "last used id" file for
922          * recovery if object isn't used for a reason, we need to release
923          * reservation, this can be made in osd_object_release()
924          */
925         if (rc == 0) {
926                 /* mark id is reserved: in create we don't want to talk
927                  * to OST */
928                 LASSERT(o->opo_reserved == 0);
929                 o->opo_reserved = 1;
930
931                 /* common for all OSPs file hystorically */
932                 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
933                 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
934                                              sizeof(osi->osi_id), osi->osi_off,
935                                              th);
936         } else {
937                 /* not needed in the cache anymore */
938                 set_bit(LU_OBJECT_HEARD_BANSHEE,
939                             &dt->do_lu.lo_header->loh_flags);
940         }
941         RETURN(rc);
942 }
943
944 static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
945                              struct lu_attr *attr,
946                              struct dt_allocation_hint *hint,
947                              struct dt_object_format *dof, struct thandle *th)
948 {
949         struct osp_thread_info  *osi = osp_env_info(env);
950         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
951         struct osp_object       *o = dt2osp_obj(dt);
952         int                     rc = 0;
953         struct lu_fid           *fid = &osi->osi_fid;
954         ENTRY;
955
956         if (is_only_remote_trans(th)) {
957                 LASSERT(fid_is_sane(lu_object_fid(&dt->do_lu)));
958
959                 rc = osp_md_object_create(env, dt, attr, hint, dof, th);
960                 if (rc == 0)
961                         o->opo_non_exist = 0;
962
963                 RETURN(rc);
964         }
965
966         o->opo_non_exist = 0;
967         if (o->opo_reserved) {
968                 /* regular case, fid is assigned holding trunsaction open */
969                  osp_object_assign_fid(env, d, o);
970         }
971
972         memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
973
974         LASSERTF(fid_is_sane(fid), "fid for osp_object %p is insane"DFID"!\n",
975                  o, PFID(fid));
976
977         if (!o->opo_reserved) {
978                 /* special case, id was assigned outside of transaction
979                  * see comments in osp_declare_attr_set */
980                 LASSERT(d->opd_pre != NULL);
981                 spin_lock(&d->opd_pre_lock);
982                 osp_update_last_fid(d, fid);
983                 spin_unlock(&d->opd_pre_lock);
984         }
985
986         CDEBUG(D_INODE, "fid for osp_object %p is "DFID"\n", o, PFID(fid));
987
988         /* If the precreate ends, it means it will be ready to rollover to
989          * the new sequence soon, all the creation should be synchronized,
990          * otherwise during replay, the replay fid will be inconsistent with
991          * last_used/create fid */
992         if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
993                 th->th_sync = 1;
994
995         /*
996          * it's OK if the import is inactive by this moment - id was created
997          * by OST earlier, we just need to maintain it consistently on the disk
998          * once import is reconnected, OSP will claim this and other objects
999          * used and OST either keep them, if they exist or recreate
1000          */
1001
1002         /* we might have lost precreated objects */
1003         if (unlikely(d->opd_gap_count) > 0) {
1004                 LASSERT(d->opd_pre != NULL);
1005                 spin_lock(&d->opd_pre_lock);
1006                 if (d->opd_gap_count > 0) {
1007                         int count = d->opd_gap_count;
1008
1009                         ostid_set_id(&osi->osi_oi,
1010                                      fid_oid(&d->opd_gap_start_fid));
1011                         d->opd_gap_count = 0;
1012                         spin_unlock(&d->opd_pre_lock);
1013
1014                         CDEBUG(D_HA, "Writting gap "DFID"+%d in llog\n",
1015                                PFID(&d->opd_gap_start_fid), count);
1016                         /* real gap handling is disabled intil ORI-692 will be
1017                          * fixed, now we only report gaps */
1018                 } else {
1019                         spin_unlock(&d->opd_pre_lock);
1020                 }
1021         }
1022
1023         /* new object, the very first ->attr_set()
1024          * initializing attributes needs no logging */
1025         o->opo_new = 1;
1026
1027         /* Only need update last_used oid file, seq file will only be update
1028          * during seq rollover */
1029         osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
1030                            &d->opd_last_used_fid.f_oid, d->opd_index);
1031
1032         rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
1033                              &osi->osi_off, th);
1034
1035         CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
1036                d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
1037
1038         RETURN(rc);
1039 }
1040
1041 int osp_declare_object_destroy(const struct lu_env *env,
1042                                struct dt_object *dt, struct thandle *th)
1043 {
1044         struct osp_object       *o = dt2osp_obj(dt);
1045         int                      rc = 0;
1046
1047         ENTRY;
1048
1049         /*
1050          * track objects to be destroyed via llog
1051          */
1052         rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
1053
1054         RETURN(rc);
1055 }
1056
1057 int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
1058                        struct thandle *th)
1059 {
1060         struct osp_object       *o = dt2osp_obj(dt);
1061         int                      rc = 0;
1062
1063         ENTRY;
1064
1065         o->opo_non_exist = 1;
1066         /*
1067          * once transaction is committed put proper command on
1068          * the queue going to our OST
1069          */
1070         rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
1071
1072         /* not needed in cache any more */
1073         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1074
1075         RETURN(rc);
1076 }
1077
1078 struct osp_orphan_it {
1079         int                       ooi_pos0;
1080         int                       ooi_pos1;
1081         int                       ooi_pos2;
1082         int                       ooi_total_npages;
1083         int                       ooi_valid_npages;
1084         unsigned int              ooi_swab:1;
1085         __u64                     ooi_next;
1086         struct dt_object         *ooi_obj;
1087         struct lu_orphan_ent     *ooi_ent;
1088         struct page              *ooi_cur_page;
1089         struct lu_idxpage        *ooi_cur_idxpage;
1090         struct page             **ooi_pages;
1091 };
1092
1093 static int osp_orphan_index_lookup(const struct lu_env *env,
1094                                    struct dt_object *dt,
1095                                    struct dt_rec *rec,
1096                                    const struct dt_key *key,
1097                                    struct lustre_capa *capa)
1098 {
1099         return -EOPNOTSUPP;
1100 }
1101
1102 static int osp_orphan_index_declare_insert(const struct lu_env *env,
1103                                            struct dt_object *dt,
1104                                            const struct dt_rec *rec,
1105                                            const struct dt_key *key,
1106                                            struct thandle *handle)
1107 {
1108         return -EOPNOTSUPP;
1109 }
1110
1111 static int osp_orphan_index_insert(const struct lu_env *env,
1112                                    struct dt_object *dt,
1113                                    const struct dt_rec *rec,
1114                                    const struct dt_key *key,
1115                                    struct thandle *handle,
1116                                    struct lustre_capa *capa,
1117                                    int ignore_quota)
1118 {
1119         return -EOPNOTSUPP;
1120 }
1121
1122 static int osp_orphan_index_declare_delete(const struct lu_env *env,
1123                                            struct dt_object *dt,
1124                                            const struct dt_key *key,
1125                                            struct thandle *handle)
1126 {
1127         return -EOPNOTSUPP;
1128 }
1129
1130 static int osp_orphan_index_delete(const struct lu_env *env,
1131                                    struct dt_object *dt,
1132                                    const struct dt_key *key,
1133                                    struct thandle *handle,
1134                                    struct lustre_capa *capa)
1135 {
1136         return -EOPNOTSUPP;
1137 }
1138
1139 static struct dt_it *osp_orphan_it_init(const struct lu_env *env,
1140                                         struct dt_object *dt,
1141                                         __u32 attr,
1142                                         struct lustre_capa *capa)
1143 {
1144         struct osp_orphan_it *it;
1145
1146         OBD_ALLOC_PTR(it);
1147         if (it == NULL)
1148                 return ERR_PTR(-ENOMEM);
1149
1150         it->ooi_pos2 = -1;
1151         it->ooi_obj = dt;
1152
1153         return (struct dt_it *)it;
1154 }
1155
1156 static void osp_orphan_it_fini(const struct lu_env *env,
1157                                struct dt_it *di)
1158 {
1159         struct osp_orphan_it     *it            = (struct osp_orphan_it *)di;
1160         struct page             **pages         = it->ooi_pages;
1161         int                       npages        = it->ooi_total_npages;
1162         int                       i;
1163
1164         if (pages != NULL) {
1165                 for (i = 0; i < npages; i++) {
1166                         if (pages[i] != NULL) {
1167                                 if (pages[i] == it->ooi_cur_page) {
1168                                         kunmap(pages[i]);
1169                                         it->ooi_cur_page = NULL;
1170                                 }
1171                                 __free_page(pages[i]);
1172                         }
1173                 }
1174                 OBD_FREE(pages, npages * sizeof(*pages));
1175         }
1176         OBD_FREE_PTR(it);
1177 }
1178
1179 static int osp_orphan_it_fetch(const struct lu_env *env,
1180                                struct osp_orphan_it *it)
1181 {
1182         struct lu_device         *dev   = it->ooi_obj->do_lu.lo_dev;
1183         struct osp_device        *osp   = lu2osp_dev(dev);
1184         struct page             **pages;
1185         struct ptlrpc_request    *req   = NULL;
1186         struct ptlrpc_bulk_desc  *desc;
1187         struct idx_info          *ii;
1188         int                       npages;
1189         int                       rc;
1190         int                       i;
1191         ENTRY;
1192
1193         /* 1MB bulk */
1194         npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
1195         npages /= PAGE_CACHE_SIZE;
1196
1197         OBD_ALLOC(pages, npages * sizeof(*pages));
1198         if (pages == NULL)
1199                 RETURN(-ENOMEM);
1200
1201         it->ooi_pages = pages;
1202         it->ooi_total_npages = npages;
1203         for (i = 0; i < npages; i++) {
1204                 pages[i] = alloc_page(GFP_IOFS);
1205                 if (pages[i] == NULL)
1206                         RETURN(-ENOMEM);
1207         }
1208
1209         req = ptlrpc_request_alloc(osp->opd_obd->u.cli.cl_import,
1210                                    &RQF_OBD_IDX_READ);
1211         if (req == NULL)
1212                 RETURN(-ENOMEM);
1213
1214         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, OBD_IDX_READ);
1215         if (rc != 0) {
1216                 ptlrpc_request_free(req);
1217                 RETURN(rc);
1218         }
1219
1220         req->rq_request_portal = OST_IDX_PORTAL;
1221         ptlrpc_at_set_req_timeout(req);
1222
1223         desc = ptlrpc_prep_bulk_imp(req, npages, 1, BULK_PUT_SINK,
1224                                     MDS_BULK_PORTAL);
1225         if (desc == NULL) {
1226                 ptlrpc_request_free(req);
1227                 RETURN(-ENOMEM);
1228         }
1229
1230         for (i = 0; i < npages; i++)
1231                 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1232
1233         ii = req_capsule_client_get(&req->rq_pill, &RMF_IDX_INFO);
1234         memset(ii, 0, sizeof(*ii));
1235         ii->ii_fid.f_seq = FID_SEQ_LAYOUT_RBTREE;
1236         ii->ii_fid.f_oid = osp->opd_index;
1237         ii->ii_fid.f_ver = 0;
1238         ii->ii_magic = IDX_INFO_MAGIC;
1239         ii->ii_flags = II_FL_NOHASH;
1240         ii->ii_count = npages * LU_PAGE_COUNT;
1241         ii->ii_hash_start = it->ooi_next;
1242         ii->ii_attrs =
1243                 osp->opd_storage->dd_lu_dev.ld_site->ld_seq_site->ss_node_id;
1244
1245         ptlrpc_request_set_replen(req);
1246         rc = ptlrpc_queue_wait(req);
1247         if (rc != 0)
1248                 GOTO(out, rc);
1249
1250         rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1251                                           req->rq_bulk->bd_nob_transferred);
1252         if (rc < 0)
1253                 GOTO(out, rc);
1254
1255         ii = req_capsule_server_get(&req->rq_pill, &RMF_IDX_INFO);
1256         if (ii->ii_magic != IDX_INFO_MAGIC)
1257                  GOTO(out, rc = -EPROTO);
1258
1259         npages = (ii->ii_count + LU_PAGE_COUNT - 1) >>
1260                  (PAGE_CACHE_SHIFT - LU_PAGE_SHIFT);
1261         if (npages > it->ooi_total_npages) {
1262                 CERROR("%s: returned more pages than expected, %u > %u\n",
1263                        osp->opd_obd->obd_name, npages, it->ooi_total_npages);
1264                 GOTO(out, rc = -EINVAL);
1265         }
1266
1267         it->ooi_valid_npages = npages;
1268         if (ptlrpc_rep_need_swab(req))
1269                 it->ooi_swab = 1;
1270
1271         it->ooi_next = ii->ii_hash_end;
1272
1273         GOTO(out, rc = 0);
1274
1275 out:
1276         ptlrpc_req_finished(req);
1277
1278         return rc;
1279 }
1280
1281 static int osp_orphan_it_next(const struct lu_env *env,
1282                               struct dt_it *di)
1283 {
1284         struct osp_orphan_it     *it            = (struct osp_orphan_it *)di;
1285         struct lu_idxpage        *idxpage;
1286         struct page             **pages;
1287         int                       rc;
1288         int                       i;
1289         ENTRY;
1290
1291 again2:
1292         idxpage = it->ooi_cur_idxpage;
1293         if (idxpage != NULL) {
1294                 if (idxpage->lip_nr == 0)
1295                         RETURN(1);
1296
1297                 it->ooi_pos2++;
1298                 if (it->ooi_pos2 < idxpage->lip_nr) {
1299                         it->ooi_ent =
1300                                 (struct lu_orphan_ent *)idxpage->lip_entries +
1301                                 it->ooi_pos2;
1302                         if (it->ooi_swab)
1303                                 lustre_swab_orphan_ent(it->ooi_ent);
1304                         RETURN(0);
1305                 }
1306
1307                 it->ooi_cur_idxpage = NULL;
1308                 it->ooi_pos1++;
1309
1310 again1:
1311                 if (it->ooi_pos1 < LU_PAGE_COUNT) {
1312                         it->ooi_cur_idxpage = (void *)it->ooi_cur_page +
1313                                               LU_PAGE_SIZE * it->ooi_pos1;
1314                         if (it->ooi_swab)
1315                                 lustre_swab_lip_header(it->ooi_cur_idxpage);
1316                         if (it->ooi_cur_idxpage->lip_magic != LIP_MAGIC) {
1317                                 struct osp_device *osp =
1318                                         lu2osp_dev(it->ooi_obj->do_lu.lo_dev);
1319
1320                                 CERROR("%s: invalid magic (%x != %x) for page "
1321                                        "%d/%d while read layout orphan index\n",
1322                                        osp->opd_obd->obd_name,
1323                                        it->ooi_cur_idxpage->lip_magic,
1324                                        LIP_MAGIC, it->ooi_pos0, it->ooi_pos1);
1325                                 /* Skip this lu_page next time. */
1326                                 it->ooi_pos2 = idxpage->lip_nr - 1;
1327                                 RETURN(-EINVAL);
1328                         }
1329                         it->ooi_pos2 = -1;
1330                         goto again2;
1331                 }
1332
1333                 kunmap(it->ooi_cur_page);
1334                 it->ooi_cur_page = NULL;
1335                 it->ooi_pos0++;
1336
1337 again0:
1338                 pages = it->ooi_pages;
1339                 if (it->ooi_pos0 < it->ooi_valid_npages) {
1340                         it->ooi_cur_page = kmap(pages[it->ooi_pos0]);
1341                         it->ooi_pos1 = 0;
1342                         goto again1;
1343                 }
1344
1345                 for (i = 0; i < it->ooi_total_npages; i++) {
1346                         if (pages[i] != NULL)
1347                                 __free_page(pages[i]);
1348                 }
1349                 OBD_FREE(pages, it->ooi_total_npages * sizeof(*pages));
1350
1351                 it->ooi_pos0 = 0;
1352                 it->ooi_total_npages = 0;
1353                 it->ooi_valid_npages = 0;
1354                 it->ooi_swab = 0;
1355                 it->ooi_ent = NULL;
1356                 it->ooi_cur_page = NULL;
1357                 it->ooi_cur_idxpage = NULL;
1358                 it->ooi_pages = NULL;
1359         }
1360
1361         if (it->ooi_next == II_END_OFF)
1362                 RETURN(1);
1363
1364         rc = osp_orphan_it_fetch(env, it);
1365         if (rc == 0)
1366                 goto again0;
1367
1368         RETURN(rc);
1369 }
1370
1371 static int osp_orphan_it_get(const struct lu_env *env,
1372                              struct dt_it *di,
1373                              const struct dt_key *key)
1374 {
1375         return -ENOSYS;
1376 }
1377
1378 static void osp_orphan_it_put(const struct lu_env *env,
1379                               struct dt_it *di)
1380 {
1381 }
1382
1383 static struct dt_key *osp_orphan_it_key(const struct lu_env *env,
1384                                         const struct dt_it *di)
1385 {
1386         struct osp_orphan_it    *it  = (struct osp_orphan_it *)di;
1387         struct lu_orphan_ent    *ent = it->ooi_ent;
1388
1389         if (likely(ent != NULL))
1390                 return (struct dt_key *)(&ent->loe_key);
1391
1392         return NULL;
1393 }
1394
1395 static int osp_orphan_it_key_size(const struct lu_env *env,
1396                                   const struct dt_it *di)
1397 {
1398         return sizeof(struct lu_fid);
1399 }
1400
1401 static int osp_orphan_it_rec(const struct lu_env *env,
1402                              const struct dt_it *di,
1403                              struct dt_rec *rec,
1404                              __u32 attr)
1405 {
1406         struct osp_orphan_it    *it  = (struct osp_orphan_it *)di;
1407         struct lu_orphan_ent    *ent = it->ooi_ent;
1408
1409         if (likely(ent != NULL)) {
1410                 *(struct lu_orphan_rec *)rec = ent->loe_rec;
1411                 return 0;
1412         }
1413
1414         return -EINVAL;
1415 }
1416
1417 static __u64 osp_orphan_it_store(const struct lu_env *env,
1418                                  const struct dt_it *di)
1419 {
1420         struct osp_orphan_it    *it     = (struct osp_orphan_it *)di;
1421
1422         return it->ooi_next;
1423 }
1424
1425 /**
1426  * \retval       +1: locate to the exactly position
1427  * \retval        0: cannot locate to the exactly position,
1428  *                   call next() to move to a valid position.
1429  * \retval      -ve: on error
1430  */
1431 static int osp_orphan_it_load(const struct lu_env *env,
1432                               const struct dt_it *di,
1433                               __u64 hash)
1434 {
1435         struct osp_orphan_it    *it     = (struct osp_orphan_it *)di;
1436         int                      rc;
1437
1438         it->ooi_next = hash;
1439         rc = osp_orphan_it_next(env, (struct dt_it *)di);
1440         if (rc == 1)
1441                 return 0;
1442
1443         if (rc == 0)
1444                 return 1;
1445
1446         return rc;
1447 }
1448
1449 static int osp_orphan_it_key_rec(const struct lu_env *env,
1450                                 const struct dt_it *di,
1451                                 void *key_rec)
1452 {
1453         return 0;
1454 }
1455
1456 static const struct dt_index_operations osp_orphan_index_ops = {
1457         .dio_lookup             = osp_orphan_index_lookup,
1458         .dio_declare_insert     = osp_orphan_index_declare_insert,
1459         .dio_insert             = osp_orphan_index_insert,
1460         .dio_declare_delete     = osp_orphan_index_declare_delete,
1461         .dio_delete             = osp_orphan_index_delete,
1462         .dio_it = {
1463                 .init           = osp_orphan_it_init,
1464                 .fini           = osp_orphan_it_fini,
1465                 .next           = osp_orphan_it_next,
1466                 .get            = osp_orphan_it_get,
1467                 .put            = osp_orphan_it_put,
1468                 .key            = osp_orphan_it_key,
1469                 .key_size       = osp_orphan_it_key_size,
1470                 .rec            = osp_orphan_it_rec,
1471                 .store          = osp_orphan_it_store,
1472                 .load           = osp_orphan_it_load,
1473                 .key_rec        = osp_orphan_it_key_rec,
1474         }
1475 };
1476
1477 static int osp_index_try(const struct lu_env *env,
1478                          struct dt_object *dt,
1479                          const struct dt_index_features *feat)
1480 {
1481         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
1482
1483         if (fid_is_last_id(fid) && fid_is_idif(fid)) {
1484                 dt->do_index_ops = &osp_orphan_index_ops;
1485
1486                 return 0;
1487         }
1488
1489         return -EINVAL;
1490 }
1491
1492 struct dt_object_operations osp_obj_ops = {
1493         .do_declare_attr_get    = osp_declare_attr_get,
1494         .do_attr_get            = osp_attr_get,
1495         .do_declare_attr_set    = osp_declare_attr_set,
1496         .do_attr_set            = osp_attr_set,
1497         .do_declare_xattr_get   = osp_declare_xattr_get,
1498         .do_xattr_get           = osp_xattr_get,
1499         .do_declare_xattr_set   = osp_declare_xattr_set,
1500         .do_xattr_set           = osp_xattr_set,
1501         .do_declare_create      = osp_declare_object_create,
1502         .do_create              = osp_object_create,
1503         .do_declare_destroy     = osp_declare_object_destroy,
1504         .do_destroy             = osp_object_destroy,
1505         .do_index_try           = osp_index_try,
1506 };
1507
1508 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
1509                            const struct lu_object_conf *conf)
1510 {
1511         struct osp_object       *po = lu2osp_obj(o);
1512         int                     rc = 0;
1513         ENTRY;
1514
1515         spin_lock_init(&po->opo_lock);
1516         o->lo_header->loh_attr |= LOHA_REMOTE;
1517
1518         if (is_ost_obj(o)) {
1519                 po->opo_obj.do_ops = &osp_obj_ops;
1520         } else {
1521                 struct lu_attr *la = &osp_env_info(env)->osi_attr;
1522
1523                 po->opo_obj.do_ops = &osp_md_obj_ops;
1524                 rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o),
1525                                                      la, NULL);
1526                 if (rc == 0)
1527                         o->lo_header->loh_attr |=
1528                                 LOHA_EXISTS | (la->la_mode & S_IFMT);
1529                 if (rc == -ENOENT) {
1530                         po->opo_non_exist = 1;
1531                         rc = 0;
1532                 }
1533                 init_rwsem(&po->opo_sem);
1534         }
1535         RETURN(rc);
1536 }
1537
1538 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
1539 {
1540         struct osp_object       *obj = lu2osp_obj(o);
1541         struct lu_object_header *h = o->lo_header;
1542
1543         dt_object_fini(&obj->opo_obj);
1544         lu_object_header_fini(h);
1545         if (obj->opo_ooa != NULL) {
1546                 struct osp_xattr_entry *oxe;
1547                 struct osp_xattr_entry *tmp;
1548                 int                     count;
1549
1550                 list_for_each_entry_safe(oxe, tmp,
1551                                          &obj->opo_ooa->ooa_xattr_list,
1552                                          oxe_list) {
1553                         list_del(&oxe->oxe_list);
1554                         count = atomic_read(&oxe->oxe_ref);
1555                         LASSERTF(count == 1,
1556                                  "Still has %d users on the xattr entry %.*s\n",
1557                                  count - 1, oxe->oxe_namelen, oxe->oxe_buf);
1558
1559                         OBD_FREE(oxe, oxe->oxe_buflen);
1560                 }
1561                 OBD_FREE_PTR(obj->opo_ooa);
1562         }
1563         OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
1564 }
1565
1566 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
1567 {
1568         struct osp_object       *po = lu2osp_obj(o);
1569         struct osp_device       *d  = lu2osp_dev(o->lo_dev);
1570
1571         ENTRY;
1572
1573         /*
1574          * release reservation if object was declared but not created
1575          * this may require lu_object_put() in LOD
1576          */
1577         if (unlikely(po->opo_reserved)) {
1578                 LASSERT(d->opd_pre != NULL);
1579                 LASSERT(d->opd_pre_reserved > 0);
1580                 spin_lock(&d->opd_pre_lock);
1581                 d->opd_pre_reserved--;
1582                 spin_unlock(&d->opd_pre_lock);
1583
1584                 /* not needed in cache any more */
1585                 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
1586         }
1587
1588         if (is_ost_obj(o))
1589                 /* XXX: Currently, NOT cache OST-object on MDT because:
1590                  *      1. it is not often accessed on MDT.
1591                  *      2. avoid up layer (such as LFSCK) to load too many
1592                  *         once-used OST-objects. */
1593                 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
1594
1595         EXIT;
1596 }
1597
1598 static int osp_object_print(const struct lu_env *env, void *cookie,
1599                             lu_printer_t p, const struct lu_object *l)
1600 {
1601         const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
1602
1603         return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
1604 }
1605
1606 static int osp_object_invariant(const struct lu_object *o)
1607 {
1608         LBUG();
1609 }
1610
1611 struct lu_object_operations osp_lu_obj_ops = {
1612         .loo_object_init        = osp_object_init,
1613         .loo_object_free        = osp_object_free,
1614         .loo_object_release     = osp_object_release,
1615         .loo_object_print       = osp_object_print,
1616         .loo_object_invariant   = osp_object_invariant
1617 };