Whamcloud - gitweb
LU-10308 misc: update Intel copyright messages for 2017
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * lustre/osp/osp_object.c
30  *
31  * Lustre OST Proxy Device (OSP) is the agent on the local MDT for the OST
32  * or remote MDT.
33  *
34  * OSP object attributes cache
35  * ---------------------------
36  * OSP object is the stub of the remote OST-object or MDT-object. Both the
37  * attribute and the extended attributes are stored on the peer side remotely.
38  * It is inefficient to send RPC to peer to fetch those attributes when every
39  * get_attr()/get_xattr() called. For a large system, the LFSCK synchronous
40  * mode scanning is prohibitively inefficient.
41  *
42  * So the OSP maintains the OSP object attributes cache to cache some
43  * attributes on the local MDT. The cache is organized against the OSP
44  * object as follows:
45  *
46  * struct osp_xattr_entry {
47  *      struct list_head         oxe_list;
48  *      atomic_t                 oxe_ref;
49  *      void                    *oxe_value;
50  *      int                      oxe_buflen;
51  *      int                      oxe_namelen;
52  *      int                      oxe_vallen;
53  *      unsigned int             oxe_exist:1,
54  *                               oxe_ready:1;
55  *      char                     oxe_buf[0];
56  * };
57  *
58  * struct osp_object {
59  *      ...
60  *      struct lu_attr          opo_attr;
61  *      struct list_head        opo_xattr_list;
62  *      spinlock_t              opo_lock;
63  *      ...
64  * };
65  *
66  * The basic attributes, such as owner/mode/flags, are stored in the
67  * osp_object::opo_attr. The extended attributes will be stored
68  * as osp_xattr_entry. Every extended attribute has an independent
69  * osp_xattr_entry, and all the osp_xattr_entry are linked into the
70  * osp_object::opo_xattr_list. The OSP object attributes cache
71  * is protected by the osp_object::opo_lock.
72  *
73  * Not all OSP objects have an attributes cache because maintaining
74  * the cache requires some resources. Currently, the OSP object
75  * attributes cache will be initialized when the attributes or the
76  * extended attributes are pre-fetched via osp_declare_attr_get()
77  * or osp_declare_xattr_get(). That is usually for LFSCK purpose,
78  * but it also can be shared by others.
79  *
80  *
81  * XXX: NOT prepare out RPC for remote transaction. ((please refer to the
82  *      comment of osp_trans_create() for remote transaction)
83  *
84  * According to our current transaction/dt_object_lock framework (to make
85  * the cross-MDTs modification for DNE1 to be workable), the transaction
86  * sponsor will start the transaction firstly, then try to acquire related
87  * dt_object_lock if needed. Under such rules, if we want to prepare the
88  * OUT RPC in the transaction declare phase, then related attr/xattr
89  * should be known without dt_object_lock. But such condition maybe not
90  * true for some remote transaction case. For example:
91  *
92  * For linkEA repairing (by LFSCK) case, before the LFSCK thread obtained
93  * the dt_object_lock on the target MDT-object, it cannot know whether
94  * the MDT-object has linkEA or not, neither invalid or not.
95  *
96  * Since the LFSCK thread cannot hold dt_object_lock before the remote
97  * transaction start (otherwise there will be some potential deadlock),
98  * it cannot prepare related OUT RPC for repairing during the declare
99  * phase as other normal transactions do.
100  *
101  * To resolve the trouble, we will make OSP to prepare related OUT RPC
102  * after remote transaction started, and trigger the remote updating
103  * (send RPC) when trans_stop. Then the up layer users, such as LFSCK,
104  * can follow the general rule to handle trans_start/dt_object_lock
105  * for repairing linkEA inconsistency without distinguishing remote
106  * MDT-object.
107  *
108  * In fact, above solution for remote transaction should be the normal
109  * model without considering DNE1. The trouble brought by DNE1 will be
110  * resolved in DNE2. At that time, this patch can be removed.
111  *
112  *
113  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
114  * Author: Mikhail Pershin <mike.tappro@intel.com>
115  */
116
117 #define DEBUG_SUBSYSTEM S_MDS
118
119 #include <lustre_obdo.h>
120 #include <lustre_swab.h>
121
122 #include "osp_internal.h"
123
124 static inline __u32 osp_dev2node(struct osp_device *osp)
125 {
126         return osp->opd_storage->dd_lu_dev.ld_site->ld_seq_site->ss_node_id;
127 }
128
129 static inline const char *osp_dto2name(struct osp_object *obj)
130 {
131         return obj->opo_obj.do_lu.lo_dev->ld_obd->obd_name;
132 }
133
134 static inline bool is_ost_obj(struct lu_object *lo)
135 {
136         return !lu2osp_dev(lo->lo_dev)->opd_connect_mdt;
137 }
138
139 static inline void __osp_oac_xattr_assignment(struct osp_object *obj,
140                                               struct osp_xattr_entry *oxe,
141                                               const struct lu_buf *buf)
142 {
143         if (buf->lb_len > 0)
144                 memcpy(oxe->oxe_value, buf->lb_buf, buf->lb_len);
145
146         oxe->oxe_vallen = buf->lb_len;
147         oxe->oxe_exist = 1;
148         oxe->oxe_ready = 1;
149 }
150
151 /**
152  * Assign FID to the OST object.
153  *
154  * This function will assign the FID to the OST object of a striped file.
155  *
156  * \param[in] env       pointer to the thread context
157  * \param[in] d         pointer to the OSP device
158  * \param[in] o         pointer to the OSP object that the FID will be
159  *                      assigned to
160  */
161 static void osp_object_assign_fid(const struct lu_env *env,
162                                   struct osp_device *d, struct osp_object *o)
163 {
164         struct osp_thread_info *osi = osp_env_info(env);
165
166         LASSERT(fid_is_zero(lu_object_fid(&o->opo_obj.do_lu)));
167         LASSERT(o->opo_reserved);
168         o->opo_reserved = 0;
169
170         osp_precreate_get_fid(env, d, &osi->osi_fid);
171
172         lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
173 }
174
175 #define OXE_DEFAULT_LEN 16
176
177 /**
178  * Release reference from the OSP object extended attribute entry.
179  *
180  * If it is the last reference, then free the entry.
181  *
182  * \param[in] oxe       pointer to the OSP object extended attribute entry.
183  */
184 static inline void osp_oac_xattr_put(struct osp_xattr_entry *oxe)
185 {
186         if (atomic_dec_and_test(&oxe->oxe_ref)) {
187                 LASSERT(list_empty(&oxe->oxe_list));
188
189                 OBD_FREE(oxe, oxe->oxe_buflen);
190         }
191 }
192
193 /**
194  * Find the named extended attribute in the OSP object attributes cache.
195  *
196  * The caller should take the osp_object::opo_lock before calling
197  * this function.
198  *
199  * \param[in] obj       pointer to the OSP object
200  * \param[in] name      the name of the extended attribute
201  * \param[in] namelen   the name length of the extended attribute
202  *
203  * \retval              pointer to the found extended attribute entry
204  * \retval              NULL if the specified extended attribute is not
205  *                      in the cache
206  */
207 static struct osp_xattr_entry *
208 osp_oac_xattr_find_locked(struct osp_object *obj, const char *name,
209                           size_t namelen)
210 {
211         struct osp_xattr_entry *oxe;
212
213         list_for_each_entry(oxe, &obj->opo_xattr_list, oxe_list) {
214                 if (namelen == oxe->oxe_namelen &&
215                     strncmp(name, oxe->oxe_buf, namelen) == 0)
216                         return oxe;
217         }
218
219         return NULL;
220 }
221
222 /**
223  * Find the named extended attribute in the OSP object attributes cache.
224  *
225  * Call osp_oac_xattr_find_locked() with the osp_object::opo_lock held.
226  *
227  * \param[in] obj       pointer to the OSP object
228  * \param[in] name      the name of the extended attribute
229  * \param[in] unlink    true if the extended attribute entry is to be removed
230  *                      from the cache
231  *
232  * \retval              pointer to the found extended attribute entry
233  * \retval              NULL if the specified extended attribute is not
234  *                      in the cache
235  */
236 static struct osp_xattr_entry *osp_oac_xattr_find(struct osp_object *obj,
237                                                   const char *name, bool unlink)
238 {
239         struct osp_xattr_entry *oxe = NULL;
240
241         spin_lock(&obj->opo_lock);
242         oxe = osp_oac_xattr_find_locked(obj, name, strlen(name));
243         if (oxe) {
244                 if (unlink)
245                         list_del_init(&oxe->oxe_list);
246                 else
247                         atomic_inc(&oxe->oxe_ref);
248         }
249         spin_unlock(&obj->opo_lock);
250
251         return oxe;
252 }
253
254 /**
255  * Find the named extended attribute in the OSP object attributes cache.
256  *
257  * If it is not in the cache, then add an empty entry (that will be
258  * filled later) to cache with the given name.
259  *
260  * \param[in] obj       pointer to the OSP object
261  * \param[in] name      the name of the extended attribute
262  * \param[in] len       the length of the extended attribute value
263  *
264  * \retval              pointer to the found or new-created extended
265  *                      attribute entry
266  * \retval              NULL if the specified extended attribute is not in the
267  *                      cache or fail to add new empty entry to the cache.
268  */
269 static struct osp_xattr_entry *
270 osp_oac_xattr_find_or_add(struct osp_object *obj, const char *name, size_t len)
271 {
272         struct osp_xattr_entry *oxe;
273         struct osp_xattr_entry *tmp = NULL;
274         size_t namelen = strlen(name);
275         size_t size = sizeof(*oxe) + namelen + 1 +
276                       (len ? len : OXE_DEFAULT_LEN);
277
278         oxe = osp_oac_xattr_find(obj, name, false);
279         if (oxe)
280                 return oxe;
281
282         OBD_ALLOC(oxe, size);
283         if (unlikely(!oxe))
284                 return NULL;
285
286         INIT_LIST_HEAD(&oxe->oxe_list);
287         oxe->oxe_buflen = size;
288         oxe->oxe_namelen = namelen;
289         memcpy(oxe->oxe_buf, name, namelen);
290         oxe->oxe_value = oxe->oxe_buf + namelen + 1;
291         /* One ref is for the caller, the other is for the entry on the list. */
292         atomic_set(&oxe->oxe_ref, 2);
293
294         spin_lock(&obj->opo_lock);
295         tmp = osp_oac_xattr_find_locked(obj, name, namelen);
296         if (!tmp)
297                 list_add_tail(&oxe->oxe_list, &obj->opo_xattr_list);
298         else
299                 atomic_inc(&tmp->oxe_ref);
300         spin_unlock(&obj->opo_lock);
301
302         if (tmp) {
303                 OBD_FREE(oxe, size);
304                 oxe = tmp;
305         }
306
307         return oxe;
308 }
309
310 /**
311  * Assign the cached OST-object's EA with the given value.
312  *
313  * If the current EA entry in cache has not enough space to hold the new
314  * value, remove it, create a new one, then assign with the given value.
315  *
316  * \param[in] obj       pointer to the OSP object
317  * \param[in] oxe       pointer to the cached EA entry to be assigned
318  * \param[in] buf       pointer to the buffer with new EA value
319  *
320  * \retval              pointer to the new created EA entry in cache if
321  *                      current entry is not big enough; otherwise, the
322  *                      input 'oxe' will be returned.
323  */
324 static struct osp_xattr_entry *
325 osp_oac_xattr_assignment(struct osp_object *obj, struct osp_xattr_entry *oxe,
326                          const struct lu_buf *buf)
327 {
328         struct osp_xattr_entry *new = NULL;
329         struct osp_xattr_entry *old = NULL;
330         int namelen = oxe->oxe_namelen;
331         size_t size = sizeof(*oxe) + namelen + 1 + buf->lb_len;
332         bool unlink_only = false;
333
334         if (oxe->oxe_buflen < size) {
335                 OBD_ALLOC(new, size);
336                 if (likely(new)) {
337                         INIT_LIST_HEAD(&new->oxe_list);
338                         new->oxe_buflen = size;
339                         new->oxe_namelen = namelen;
340                         memcpy(new->oxe_buf, oxe->oxe_buf, namelen);
341                         new->oxe_value = new->oxe_buf + namelen + 1;
342                         /* One ref is for the caller,
343                          * the other is for the entry on the list. */
344                         atomic_set(&new->oxe_ref, 2);
345                         __osp_oac_xattr_assignment(obj, new, buf);
346                 } else {
347                         unlink_only = true;
348                         CWARN("%s: cannot update cached xattr %.*s of "DFID"\n",
349                               osp_dto2name(obj), namelen, oxe->oxe_buf,
350                               PFID(lu_object_fid(&obj->opo_obj.do_lu)));
351                 }
352         }
353
354         spin_lock(&obj->opo_lock);
355         old = osp_oac_xattr_find_locked(obj, oxe->oxe_buf, namelen);
356         if (likely(old)) {
357                 if (new) {
358                         /* Unlink the 'old'. */
359                         list_del_init(&old->oxe_list);
360
361                         /* Drop the ref for 'old' on list. */
362                         osp_oac_xattr_put(old);
363
364                         /* Drop the ref for current using. */
365                         osp_oac_xattr_put(oxe);
366                         oxe = new;
367
368                         /* Insert 'new' into list. */
369                         list_add_tail(&new->oxe_list, &obj->opo_xattr_list);
370                 } else if (unlink_only) {
371                         /* Unlink the 'old'. */
372                         list_del_init(&old->oxe_list);
373
374                         /* Drop the ref for 'old' on list. */
375                         osp_oac_xattr_put(old);
376                 } else {
377                         __osp_oac_xattr_assignment(obj, oxe, buf);
378                 }
379         } else if (new) {
380                 /* Drop the ref for current using. */
381                 osp_oac_xattr_put(oxe);
382                 oxe = new;
383
384                 /* Someone unlinked the 'old' by race,
385                  * insert the 'new' one into list. */
386                 list_add_tail(&new->oxe_list, &obj->opo_xattr_list);
387         }
388         spin_unlock(&obj->opo_lock);
389
390         return oxe;
391 }
392
393 /**
394  * Parse the OSP object attribute from the RPC reply.
395  *
396  * If the attribute is valid, then it will be added to the OSP object
397  * attributes cache.
398  *
399  * \param[in] env       pointer to the thread context
400  * \param[in] reply     pointer to the RPC reply
401  * \param[in] req       pointer to the RPC request
402  * \param[out] attr     pointer to buffer to hold the output attribute
403  * \param[in] obj       pointer to the OSP object
404  * \param[in] index     the index of the attribute buffer in the reply
405  *
406  * \retval              0 for success
407  * \retval              negative error number on failure
408  */
409 static int osp_get_attr_from_reply(const struct lu_env *env,
410                                    struct object_update_reply *reply,
411                                    struct ptlrpc_request *req,
412                                    struct lu_attr *attr,
413                                    struct osp_object *obj, int index)
414 {
415         struct osp_thread_info  *osi    = osp_env_info(env);
416         struct lu_buf           *rbuf   = &osi->osi_lb2;
417         struct obdo             *lobdo  = &osi->osi_obdo;
418         struct obdo             *wobdo;
419         int                     rc;
420
421         rc = object_update_result_data_get(reply, rbuf, index);
422         if (rc < 0)
423                 return rc;
424
425         wobdo = rbuf->lb_buf;
426         if (rbuf->lb_len != sizeof(*wobdo))
427                 return -EPROTO;
428
429         LASSERT(req != NULL);
430         if (ptlrpc_req_need_swab(req))
431                 lustre_swab_obdo(wobdo);
432
433         lustre_get_wire_obdo(NULL, lobdo, wobdo);
434         spin_lock(&obj->opo_lock);
435         la_from_obdo(&obj->opo_attr, lobdo, lobdo->o_valid);
436         if (attr != NULL)
437                 *attr = obj->opo_attr;
438         spin_unlock(&obj->opo_lock);
439
440         return 0;
441 }
442
443 /**
444  * Interpreter function for getting OSP object attribute asynchronously.
445  *
446  * Called to interpret the result of an async mode RPC for getting the
447  * OSP object attribute.
448  *
449  * \param[in] env       pointer to the thread context
450  * \param[in] reply     pointer to the RPC reply
451  * \param[in] req       pointer to the RPC request
452  * \param[in] obj       pointer to the OSP object
453  * \param[out] data     pointer to buffer to hold the output attribute
454  * \param[in] index     the index of the attribute buffer in the reply
455  * \param[in] rc        the result for handling the RPC
456  *
457  * \retval              0 for success
458  * \retval              negative error number on failure
459  */
460 static int osp_attr_get_interpterer(const struct lu_env *env,
461                                     struct object_update_reply *reply,
462                                     struct ptlrpc_request *req,
463                                     struct osp_object *obj,
464                                     void *data, int index, int rc)
465 {
466         struct lu_attr *attr = data;
467
468         if (rc == 0) {
469                 osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
470                 obj->opo_non_exist = 0;
471
472                 return osp_get_attr_from_reply(env, reply, req, NULL, obj,
473                                                index);
474         } else {
475                 if (rc == -ENOENT) {
476                         osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
477                         obj->opo_non_exist = 1;
478                 }
479
480                 spin_lock(&obj->opo_lock);
481                 attr->la_valid = 0;
482                 spin_unlock(&obj->opo_lock);
483         }
484
485         return 0;
486 }
487
488 /**
489  * Implement OSP layer dt_object_operations::do_declare_attr_get() interface.
490  *
491  * Declare that the caller will get attribute from the specified OST object.
492  *
493  * This function adds an Object Unified Target (OUT) sub-request to the per-OSP
494  * based shared asynchronous request queue. The osp_attr_get_interpterer()
495  * is registered as the interpreter function to handle the result of this
496  * sub-request.
497  *
498  * \param[in] env       pointer to the thread context
499  * \param[in] dt        pointer to the OSP layer dt_object
500  *
501  * \retval              0 for success
502  * \retval              negative error number on failure
503  */
504 static int osp_declare_attr_get(const struct lu_env *env, struct dt_object *dt)
505 {
506         struct osp_object       *obj    = dt2osp_obj(dt);
507         struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
508         int                      rc     = 0;
509
510         mutex_lock(&osp->opd_async_requests_mutex);
511         rc = osp_insert_async_request(env, OUT_ATTR_GET, obj, 0, NULL, NULL,
512                                       &obj->opo_attr, sizeof(struct obdo),
513                                       osp_attr_get_interpterer);
514         mutex_unlock(&osp->opd_async_requests_mutex);
515
516         return rc;
517 }
518
519 /**
520  * Implement OSP layer dt_object_operations::do_attr_get() interface.
521  *
522  * Get attribute from the specified MDT/OST object.
523  *
524  * If the attribute is in the OSP object attributes cache, then return
525  * the cached attribute directly. Otherwise it will trigger an OUT RPC
526  * to the peer to get the attribute synchronously, if successful, add it
527  * to the OSP attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
528  *
529  * \param[in] env       pointer to the thread context
530  * \param[in] dt        pointer to the OSP layer dt_object
531  * \param[out] attr     pointer to the buffer to hold the output attribute
532  *
533  * \retval              0 for success
534  * \retval              negative error number on failure
535  */
536 int osp_attr_get(const struct lu_env *env, struct dt_object *dt,
537                  struct lu_attr *attr)
538 {
539         struct osp_device               *osp = lu2osp_dev(dt->do_lu.lo_dev);
540         struct osp_object               *obj = dt2osp_obj(dt);
541         struct dt_device                *dev = &osp->opd_dt_dev;
542         struct osp_update_request       *update;
543         struct object_update_reply      *reply;
544         struct ptlrpc_request           *req = NULL;
545         int                             rc = 0;
546         ENTRY;
547
548         if (is_ost_obj(&dt->do_lu) && obj->opo_non_exist)
549                 RETURN(-ENOENT);
550
551         spin_lock(&obj->opo_lock);
552         if (obj->opo_attr.la_valid != 0 && !obj->opo_stale) {
553                 *attr = obj->opo_attr;
554                 spin_unlock(&obj->opo_lock);
555
556                 RETURN(0);
557         }
558         spin_unlock(&obj->opo_lock);
559
560         update = osp_update_request_create(dev);
561         if (IS_ERR(update))
562                 RETURN(PTR_ERR(update));
563
564         rc = osp_update_rpc_pack(env, attr_get, update, OUT_ATTR_GET,
565                                  lu_object_fid(&dt->do_lu));
566         if (rc != 0) {
567                 CERROR("%s: Insert update error "DFID": rc = %d\n",
568                        dev->dd_lu_dev.ld_obd->obd_name,
569                        PFID(lu_object_fid(&dt->do_lu)), rc);
570
571                 GOTO(out, rc);
572         }
573
574         rc = osp_remote_sync(env, osp, update, &req);
575         if (rc != 0) {
576                 if (rc == -ENOENT) {
577                         osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
578                         obj->opo_non_exist = 1;
579                 } else {
580                         CERROR("%s:osp_attr_get update error "DFID": rc = %d\n",
581                                dev->dd_lu_dev.ld_obd->obd_name,
582                                PFID(lu_object_fid(&dt->do_lu)), rc);
583                 }
584
585                 GOTO(out, rc);
586         }
587
588         osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
589         obj->opo_non_exist = 0;
590         reply = req_capsule_server_sized_get(&req->rq_pill,
591                                              &RMF_OUT_UPDATE_REPLY,
592                                              OUT_UPDATE_REPLY_SIZE);
593         if (reply == NULL || reply->ourp_magic != UPDATE_REPLY_MAGIC)
594                 GOTO(out, rc = -EPROTO);
595
596         rc = osp_get_attr_from_reply(env, reply, req, attr, obj, 0);
597         if (rc != 0)
598                 GOTO(out, rc);
599
600         spin_lock(&obj->opo_lock);
601         obj->opo_stale = 0;
602         spin_unlock(&obj->opo_lock);
603
604         GOTO(out, rc);
605
606 out:
607         if (req != NULL)
608                 ptlrpc_req_finished(req);
609
610         osp_update_request_destroy(env, update);
611
612         return rc;
613 }
614
615 /**
616  * Implement OSP layer dt_object_operations::do_declare_attr_set() interface.
617  *
618  * If the transaction is not remote one, then declare the credits that will
619  * be used for the subsequent llog record for the object's attributes.
620  *
621  * \param[in] env       pointer to the thread context
622  * \param[in] dt        pointer to the OSP layer dt_object
623  * \param[in] attr      pointer to the attribute to be set
624  * \param[in] th        pointer to the transaction handler
625  *
626  * \retval              0 for success
627  * \retval              negative error number on failure
628  */
629 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
630                                 const struct lu_attr *attr, struct thandle *th)
631 {
632         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
633         struct osp_object       *o = dt2osp_obj(dt);
634         int                     rc;
635
636         if (is_only_remote_trans(th))
637                 return osp_md_declare_attr_set(env, dt, attr, th);
638         /*
639          * Usually we don't allow server stack to manipulate size
640          * but there is a special case when striping is created
641          * late, after stripeless file got truncated to non-zero.
642          *
643          * In this case we do the following:
644          *
645          * 1) grab id in declare - this can lead to leaked OST objects
646          *    but we don't currently have proper mechanism and the only
647          *    options we have are to do truncate RPC holding transaction
648          *    open (very bad) or to grab id in declare at cost of leaked
649          *    OST object in same very rare unfortunate case (just bad)
650          *    notice 1.6-2.0 do assignment outside of running transaction
651          *    all the time, meaning many more chances for leaked objects.
652          *
653          * 2) send synchronous truncate RPC with just assigned id
654          */
655
656         /* there are few places in MDD code still passing NULL
657          * XXX: to be fixed soon */
658         if (attr == NULL)
659                 RETURN(0);
660
661         if (attr->la_valid & LA_SIZE && attr->la_size > 0 &&
662             fid_is_zero(lu_object_fid(&o->opo_obj.do_lu))) {
663                 LASSERT(!dt_object_exists(dt));
664                 osp_object_assign_fid(env, d, o);
665                 rc = osp_object_truncate(env, dt, attr->la_size);
666                 if (rc != 0)
667                         RETURN(rc);
668         }
669
670         if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
671                 RETURN(0);
672
673         /* track all UID/GID, projid, and layout version changes via llog */
674         rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
675
676         return 0;
677 }
678
679 /**
680  * Implement OSP layer dt_object_operations::do_attr_set() interface.
681  *
682  * Set attribute to the specified OST object.
683  *
684  * If the transaction is a remote one, then add OUT_ATTR_SET sub-request
685  * in the OUT RPC that will be flushed when the remote transaction stop.
686  * Otherwise, it will generate a MDS_SETATTR64_REC record in the llog that
687  * will be handled by a dedicated thread asynchronously.
688  *
689  * If the attribute entry exists in the OSP object attributes cache,
690  * then update the cached attribute according to given attribute.
691  *
692  * \param[in] env       pointer to the thread context
693  * \param[in] dt        pointer to the OSP layer dt_object
694  * \param[in] attr      pointer to the attribute to be set
695  * \param[in] th        pointer to the transaction handler
696  *
697  * \retval              0 for success
698  * \retval              negative error number on failure
699  */
700 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
701                         const struct lu_attr *attr, struct thandle *th)
702 {
703         struct osp_object       *o = dt2osp_obj(dt);
704         int                      rc = 0;
705         ENTRY;
706
707         /* we're interested in uid/gid/projid/layout version changes only */
708         if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
709                 RETURN(0);
710
711         if (!is_only_remote_trans(th)) {
712                 rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
713                 /* XXX: send new uid/gid to OST ASAP? */
714         } else {
715                 struct lu_attr  *la;
716
717                 /* It is for OST-object attr_set directly without updating
718                  * local MDT-object attribute. It is usually used by LFSCK. */
719                 rc = osp_md_attr_set(env, dt, attr, th);
720                 CDEBUG(D_INFO, "(1) set attr "DFID": rc = %d\n",
721                        PFID(&dt->do_lu.lo_header->loh_fid), rc);
722
723                 if (rc != 0)
724                         RETURN(rc);
725
726                 /* Update the OSP object attributes cache. */
727                 la = &o->opo_attr;
728                 spin_lock(&o->opo_lock);
729                 if (attr->la_valid & LA_UID) {
730                         la->la_uid = attr->la_uid;
731                         la->la_valid |= LA_UID;
732                 }
733
734                 if (attr->la_valid & LA_GID) {
735                         la->la_gid = attr->la_gid;
736                         la->la_valid |= LA_GID;
737                 }
738                 if (attr->la_valid & LA_PROJID) {
739                         la->la_projid = attr->la_projid;
740                         la->la_valid |= LA_PROJID;
741                 }
742                 spin_unlock(&o->opo_lock);
743         }
744
745         RETURN(rc);
746 }
747
748 /**
749  * Interpreter function for getting OSP object extended attribute asynchronously
750  *
751  * Called to interpret the result of an async mode RPC for getting the
752  * OSP object extended attribute.
753  *
754  * \param[in] env       pointer to the thread context
755  * \param[in] reply     pointer to the RPC reply
756  * \param[in] req       pointer to the RPC request
757  * \param[in] obj       pointer to the OSP object
758  * \param[out] data     pointer to OSP object attributes cache
759  * \param[in] index     the index of the attribute buffer in the reply
760  * \param[in] rc        the result for handling the RPC
761  *
762  * \retval              0 for success
763  * \retval              negative error number on failure
764  */
765 static int osp_xattr_get_interpterer(const struct lu_env *env,
766                                      struct object_update_reply *reply,
767                                      struct ptlrpc_request *req,
768                                      struct osp_object *obj,
769                                      void *data, int index, int rc)
770 {
771         struct osp_xattr_entry *oxe = data;
772         struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
773
774         if (!rc) {
775                 size_t len = sizeof(*oxe) + oxe->oxe_namelen + 1;
776
777                 rc = object_update_result_data_get(reply, rbuf, index);
778                 spin_lock(&obj->opo_lock);
779                 if (rc < 0 || rbuf->lb_len == 0 ||
780                     rbuf->lb_len > (oxe->oxe_buflen - len)) {
781                         if (unlikely(rc == -ENODATA)) {
782                                 oxe->oxe_exist = 0;
783                                 oxe->oxe_ready = 1;
784                         } else {
785                                 oxe->oxe_ready = 0;
786                         }
787                         spin_unlock(&obj->opo_lock);
788                         /* Put the reference obtained in the
789                          * osp_declare_xattr_get(). */
790                         osp_oac_xattr_put(oxe);
791
792                         return rc < 0 ? rc : -ERANGE;
793                 }
794
795                 __osp_oac_xattr_assignment(obj, oxe, rbuf);
796                 spin_unlock(&obj->opo_lock);
797         } else if (rc == -ENOENT || rc == -ENODATA) {
798                 spin_lock(&obj->opo_lock);
799                 oxe->oxe_exist = 0;
800                 oxe->oxe_ready = 1;
801                 spin_unlock(&obj->opo_lock);
802         } else {
803                 spin_lock(&obj->opo_lock);
804                 oxe->oxe_ready = 0;
805                 spin_unlock(&obj->opo_lock);
806         }
807
808         /* Put the reference obtained in the osp_declare_xattr_get(). */
809         osp_oac_xattr_put(oxe);
810
811         return 0;
812 }
813
814 /**
815  * Implement OSP dt_object_operations::do_declare_xattr_get() interface.
816  *
817  * Declare that the caller will get extended attribute from the specified
818  * OST object.
819  *
820  * This function will add an OUT_XATTR_GET sub-request to the per OSP
821  * based shared asynchronous request queue with the interpreter function:
822  * osp_xattr_get_interpterer().
823  *
824  * \param[in] env       pointer to the thread context
825  * \param[in] dt        pointer to the OSP layer dt_object
826  * \param[out] buf      pointer to the lu_buf to hold the extended attribute
827  * \param[in] name      the name for the expected extended attribute
828  *
829  * \retval              0 for success
830  * \retval              negative error number on failure
831  */
832 static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
833                                  struct lu_buf *buf, const char *name)
834 {
835         struct osp_object       *obj     = dt2osp_obj(dt);
836         struct osp_device       *osp     = lu2osp_dev(dt->do_lu.lo_dev);
837         struct osp_xattr_entry  *oxe;
838         __u16 namelen;
839         int                      rc      = 0;
840
841         LASSERT(buf != NULL);
842         LASSERT(name != NULL);
843
844         if (unlikely(buf->lb_len == 0))
845                 return -EINVAL;
846
847         oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
848         if (oxe == NULL)
849                 return -ENOMEM;
850
851         namelen = strlen(name);
852         mutex_lock(&osp->opd_async_requests_mutex);
853         rc = osp_insert_async_request(env, OUT_XATTR_GET, obj, 1,
854                                       &namelen, (const void **)&name,
855                                       oxe, buf->lb_len,
856                                       osp_xattr_get_interpterer);
857         if (rc != 0) {
858                 mutex_unlock(&osp->opd_async_requests_mutex);
859                 osp_oac_xattr_put(oxe);
860         } else {
861                 struct osp_update_request *our;
862                 struct osp_update_request_sub *ours;
863
864                 /* XXX: Currently, we trigger the batched async OUT
865                  *      RPC via dt_declare_xattr_get(). It is not
866                  *      perfect solution, but works well now.
867                  *
868                  *      We will improve it in the future. */
869                 our = osp->opd_async_requests;
870                 ours = osp_current_object_update_request(our);
871                 if (ours != NULL && ours->ours_req != NULL &&
872                     ours->ours_req->ourq_count > 0) {
873                         osp->opd_async_requests = NULL;
874                         mutex_unlock(&osp->opd_async_requests_mutex);
875                         rc = osp_unplug_async_request(env, osp, our);
876                 } else {
877                         mutex_unlock(&osp->opd_async_requests_mutex);
878                 }
879         }
880
881         return rc;
882 }
883
884 /**
885  * Implement OSP layer dt_object_operations::do_xattr_get() interface.
886  *
887  * Get extended attribute from the specified MDT/OST object.
888  *
889  * If the extended attribute is in the OSP object attributes cache, then
890  * return the cached extended attribute directly. Otherwise it will get
891  * the extended attribute synchronously, if successful, add it to the OSP
892  * attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
893  *
894  * There is a race condition: some other thread has added the named extended
895  * attributed entry to the OSP object attributes cache during the current
896  * OUT_XATTR_GET handling. If such case happens, the OSP will replace the
897  * (just) existing extended attribute entry with the new replied one.
898  *
899  * \param[in] env       pointer to the thread context
900  * \param[in] dt        pointer to the OSP layer dt_object
901  * \param[out] buf      pointer to the lu_buf to hold the extended attribute
902  * \param[in] name      the name for the expected extended attribute
903  *
904  * \retval              0 for success
905  * \retval              negative error number on failure
906  */
907 int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
908                   struct lu_buf *buf, const char *name)
909 {
910         struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
911         struct osp_object       *obj    = dt2osp_obj(dt);
912         struct dt_device        *dev    = &osp->opd_dt_dev;
913         struct lu_buf           *rbuf   = &osp_env_info(env)->osi_lb2;
914         struct osp_update_request *update = NULL;
915         struct ptlrpc_request   *req    = NULL;
916         struct object_update_reply *reply;
917         struct osp_xattr_entry  *oxe    = NULL;
918         const char *dname = osp_dto2name(obj);
919         int rc = 0;
920         ENTRY;
921
922         LASSERT(buf != NULL);
923         LASSERT(name != NULL);
924
925         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NETWORK) &&
926             osp->opd_index == cfs_fail_val) {
927                 if (is_ost_obj(&dt->do_lu)) {
928                         if (osp_dev2node(osp) == cfs_fail_val)
929                                 RETURN(-ENOTCONN);
930                 } else {
931                         if (strcmp(name, XATTR_NAME_LINK) == 0)
932                                 RETURN(-ENOTCONN);
933                 }
934         }
935
936         if (unlikely(obj->opo_non_exist))
937                 RETURN(-ENOENT);
938
939         oxe = osp_oac_xattr_find(obj, name, false);
940         if (oxe != NULL) {
941                 spin_lock(&obj->opo_lock);
942                 if (oxe->oxe_ready) {
943                         if (!oxe->oxe_exist)
944                                 GOTO(unlock, rc = -ENODATA);
945
946                         if (buf->lb_buf == NULL)
947                                 GOTO(unlock, rc = oxe->oxe_vallen);
948
949                         if (buf->lb_len < oxe->oxe_vallen)
950                                 GOTO(unlock, rc = -ERANGE);
951
952                         memcpy(buf->lb_buf, oxe->oxe_value,
953                                oxe->oxe_vallen);
954
955                         GOTO(unlock, rc = oxe->oxe_vallen);
956
957 unlock:
958                         spin_unlock(&obj->opo_lock);
959                         osp_oac_xattr_put(oxe);
960
961                         return rc;
962                 }
963                 spin_unlock(&obj->opo_lock);
964         }
965         update = osp_update_request_create(dev);
966         if (IS_ERR(update))
967                 GOTO(out, rc = PTR_ERR(update));
968
969         rc = osp_update_rpc_pack(env, xattr_get, update, OUT_XATTR_GET,
970                                  lu_object_fid(&dt->do_lu), name, buf->lb_len);
971         if (rc != 0) {
972                 CERROR("%s: Insert update error "DFID": rc = %d\n",
973                        dname, PFID(lu_object_fid(&dt->do_lu)), rc);
974                 GOTO(out, rc);
975         }
976
977         rc = osp_remote_sync(env, osp, update, &req);
978         if (rc < 0) {
979                 if (rc == -ENOENT) {
980                         dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
981                         obj->opo_non_exist = 1;
982                 }
983
984                 if (oxe == NULL)
985                         oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
986
987                 if (oxe == NULL) {
988                         CWARN("%s: Fail to add xattr (%s) to cache for "
989                               DFID" (1): rc = %d\n", dname, name,
990                               PFID(lu_object_fid(&dt->do_lu)), rc);
991
992                         GOTO(out, rc);
993                 }
994
995                 spin_lock(&obj->opo_lock);
996                 if (rc == -ENOENT || rc == -ENODATA) {
997                         oxe->oxe_exist = 0;
998                         oxe->oxe_ready = 1;
999                 } else {
1000                         oxe->oxe_ready = 0;
1001                 }
1002                 spin_unlock(&obj->opo_lock);
1003
1004                 GOTO(out, rc);
1005         }
1006
1007         reply = req_capsule_server_sized_get(&req->rq_pill,
1008                                              &RMF_OUT_UPDATE_REPLY,
1009                                              OUT_UPDATE_REPLY_SIZE);
1010         if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1011                 CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
1012                        dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
1013                        PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
1014
1015                 GOTO(out, rc = -EPROTO);
1016         }
1017
1018         rc = object_update_result_data_get(reply, rbuf, 0);
1019         if (rc < 0 || rbuf->lb_len == 0) {
1020                 if (oxe == NULL && rc == -ENODATA) {
1021                         oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
1022                         if (oxe == NULL) {
1023                                 rc = -ENOMEM;
1024                                 CWARN("%s: Fail to add xattr (%s) to cache for "
1025                                       DFID" (1): rc = %d\n", dname, name,
1026                                       PFID(lu_object_fid(&dt->do_lu)), rc);
1027                                 GOTO(out, rc);
1028                         }
1029                 }
1030
1031                 if (oxe) {
1032                         spin_lock(&obj->opo_lock);
1033                         if (unlikely(rc == -ENODATA)) {
1034                                 oxe->oxe_exist = 0;
1035                                 oxe->oxe_ready = 1;
1036                         } else {
1037                                 oxe->oxe_ready = 0;
1038                         }
1039                         spin_unlock(&obj->opo_lock);
1040                 }
1041
1042                 GOTO(out, rc);
1043         }
1044
1045         /* For detecting EA size. */
1046         if (!buf->lb_buf)
1047                 GOTO(out, rc);
1048
1049         if (!oxe) {
1050                 oxe = osp_oac_xattr_find_or_add(obj, name, rbuf->lb_len);
1051                 if (!oxe) {
1052                         CWARN("%s: Fail to add xattr (%s) to "
1053                               "cache for "DFID" (2): rc = %d\n",
1054                               dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1055
1056                         GOTO(out, rc);
1057                 }
1058         }
1059
1060         oxe = osp_oac_xattr_assignment(obj, oxe, rbuf);
1061
1062         GOTO(out, rc);
1063
1064 out:
1065         if (rc > 0 && buf->lb_buf) {
1066                 if (unlikely(buf->lb_len < rbuf->lb_len))
1067                         rc = -ERANGE;
1068                 else
1069                         memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
1070         }
1071
1072         if (req)
1073                 ptlrpc_req_finished(req);
1074
1075         if (update && !IS_ERR(update))
1076                 osp_update_request_destroy(env, update);
1077
1078         if (oxe)
1079                 osp_oac_xattr_put(oxe);
1080
1081         return rc;
1082 }
1083
1084 /**
1085  * Implement OSP layer dt_object_operations::do_declare_xattr_set() interface.
1086  *
1087  * Declare that the caller will set extended attribute to the specified
1088  * MDT/OST object.
1089  *
1090  * If it is non-remote transaction, it will add an OUT_XATTR_SET sub-request
1091  * to the OUT RPC that will be flushed when the transaction start. And if the
1092  * OSP attributes cache is initialized, then check whether the name extended
1093  * attribute entry exists in the cache or not. If yes, replace it; otherwise,
1094  * add the extended attribute to the cache.
1095  *
1096  * \param[in] env       pointer to the thread context
1097  * \param[in] dt        pointer to the OSP layer dt_object
1098  * \param[in] buf       pointer to the lu_buf to hold the extended attribute
1099  * \param[in] name      the name of the extended attribute to be set
1100  * \param[in] flag      to indicate the detailed set operation: LU_XATTR_CREATE
1101  *                      or LU_XATTR_REPLACE or others
1102  * \param[in] th        pointer to the transaction handler
1103  *
1104  * \retval              0 for success
1105  * \retval              negative error number on failure
1106  */
1107 int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
1108                           const struct lu_buf *buf, const char *name,
1109                           int flag, struct thandle *th)
1110 {
1111         return osp_trans_update_request_create(th);
1112 }
1113
1114 /**
1115  * Implement OSP layer dt_object_operations::do_xattr_set() interface.
1116  *
1117  * Set extended attribute to the specified MDT/OST object.
1118  *
1119  * Add an OUT_XATTR_SET sub-request into the OUT RPC that will be flushed in
1120  * the transaction stop. And if the OSP attributes cache is initialized, then
1121  * check whether the name extended attribute entry exists in the cache or not.
1122  * If yes, replace it; otherwise, add the extended attribute to the cache.
1123  *
1124  * \param[in] env       pointer to the thread context
1125  * \param[in] dt        pointer to the OSP layer dt_object
1126  * \param[in] buf       pointer to the lu_buf to hold the extended attribute
1127  * \param[in] name      the name of the extended attribute to be set
1128  * \param[in] fl        to indicate the detailed set operation: LU_XATTR_CREATE
1129  *                      or LU_XATTR_REPLACE or others
1130  * \param[in] th        pointer to the transaction handler
1131  *
1132  * \retval              0 for success
1133  * \retval              negative error number on failure
1134  */
1135 int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
1136                   const struct lu_buf *buf, const char *name, int fl,
1137                   struct thandle *th)
1138 {
1139         struct osp_object       *o = dt2osp_obj(dt);
1140         struct osp_update_request *update;
1141         struct osp_xattr_entry  *oxe;
1142         int                     rc;
1143         ENTRY;
1144
1145         update = thandle_to_osp_update_request(th);
1146         LASSERT(update != NULL);
1147
1148         CDEBUG(D_INODE, DFID" set xattr '%s' with size %zd\n",
1149                PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
1150
1151         rc = osp_update_rpc_pack(env, xattr_set, update, OUT_XATTR_SET,
1152                                  lu_object_fid(&dt->do_lu), buf, name, fl);
1153         if (rc != 0)
1154                 RETURN(rc);
1155
1156         /* Do not cache linkEA that may be self-adjusted by peers
1157          * under EA overflow case. */
1158         if (strcmp(name, XATTR_NAME_LINK) == 0) {
1159                 oxe = osp_oac_xattr_find(o, name, true);
1160                 if (oxe != NULL)
1161                         osp_oac_xattr_put(oxe);
1162
1163                 RETURN(0);
1164         }
1165
1166         oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
1167         if (oxe == NULL) {
1168                 CWARN("%s: cannot cache xattr '%s' of "DFID"\n",
1169                       osp_dto2name(o), name, PFID(lu_object_fid(&dt->do_lu)));
1170
1171                 RETURN(0);
1172         }
1173
1174         oxe = osp_oac_xattr_assignment(o, oxe, buf);
1175         if (oxe)
1176                 osp_oac_xattr_put(oxe);
1177
1178         RETURN(0);
1179 }
1180
1181 /**
1182  * Implement OSP layer dt_object_operations::do_declare_xattr_del() interface.
1183  *
1184  * Declare that the caller will delete extended attribute on the specified
1185  * MDT/OST object.
1186  *
1187  * If it is non-remote transaction, it will add an OUT_XATTR_DEL sub-request
1188  * to the OUT RPC that will be flushed when the transaction start. And if the
1189  * name extended attribute entry exists in the OSP attributes cache, then remove
1190  * it from the cache.
1191  *
1192  * \param[in] env       pointer to the thread context
1193  * \param[in] dt        pointer to the OSP layer dt_object
1194  * \param[in] name      the name of the extended attribute to be set
1195  * \param[in] th        pointer to the transaction handler
1196  *
1197  * \retval              0 for success
1198  * \retval              negative error number on failure
1199  */
1200 int osp_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
1201                           const char *name, struct thandle *th)
1202 {
1203         return osp_trans_update_request_create(th);
1204 }
1205
1206 /**
1207  * Implement OSP layer dt_object_operations::do_xattr_del() interface.
1208  *
1209  * Delete extended attribute on the specified MDT/OST object.
1210  *
1211  * If it is remote transaction, it will add an OUT_XATTR_DEL sub-request into
1212  * the OUT RPC that will be flushed when the transaction stop. And if the name
1213  * extended attribute entry exists in the OSP attributes cache, then remove it
1214  * from the cache.
1215  *
1216  * \param[in] env       pointer to the thread context
1217  * \param[in] dt        pointer to the OSP layer dt_object
1218  * \param[in] name      the name of the extended attribute to be set
1219  * \param[in] th        pointer to the transaction handler
1220  *
1221  * \retval              0 for success
1222  * \retval              negative error number on failure
1223  */
1224 int osp_xattr_del(const struct lu_env *env, struct dt_object *dt,
1225                   const char *name, struct thandle *th)
1226 {
1227         struct osp_update_request *update;
1228         const struct lu_fid      *fid = lu_object_fid(&dt->do_lu);
1229         struct osp_object        *o     = dt2osp_obj(dt);
1230         struct osp_xattr_entry   *oxe;
1231         int                       rc;
1232
1233         update = thandle_to_osp_update_request(th);
1234         LASSERT(update != NULL);
1235
1236         rc = osp_update_rpc_pack(env, xattr_del, update, OUT_XATTR_DEL,
1237                                  fid, name);
1238         if (rc != 0)
1239                 return rc;
1240
1241         oxe = osp_oac_xattr_find(o, name, true);
1242         if (oxe != NULL)
1243                 /* Drop the ref for entry on list. */
1244                 osp_oac_xattr_put(oxe);
1245
1246         return 0;
1247 }
1248
1249 void osp_obj_invalidate_cache(struct osp_object *obj)
1250 {
1251         struct osp_xattr_entry *oxe;
1252         struct osp_xattr_entry *tmp;
1253
1254         spin_lock(&obj->opo_lock);
1255         list_for_each_entry_safe(oxe, tmp, &obj->opo_xattr_list, oxe_list) {
1256                 oxe->oxe_ready = 0;
1257                 list_del_init(&oxe->oxe_list);
1258                 osp_oac_xattr_put(oxe);
1259         }
1260         obj->opo_attr.la_valid = 0;
1261         spin_unlock(&obj->opo_lock);
1262 }
1263
1264 /**
1265  * Implement OSP layer dt_object_operations::do_invalidate() interface.
1266  *
1267  * Invalidate attributes cached on the specified MDT/OST object.
1268  *
1269  * \param[in] env       pointer to the thread context
1270  * \param[in] dt        pointer to the OSP layer dt_object
1271  *
1272  * \retval              0 for success
1273  * \retval              negative error number on failure
1274  */
1275 int osp_invalidate(const struct lu_env *env, struct dt_object *dt)
1276 {
1277         struct osp_object *obj = dt2osp_obj(dt);
1278         ENTRY;
1279
1280         CDEBUG(D_HA, "Invalidate osp_object "DFID"\n",
1281                PFID(lu_object_fid(&dt->do_lu)));
1282         osp_obj_invalidate_cache(obj);
1283
1284         spin_lock(&obj->opo_lock);
1285         obj->opo_stale = 1;
1286         spin_unlock(&obj->opo_lock);
1287
1288         RETURN(0);
1289 }
1290
1291 /**
1292  * Implement OSP layer dt_object_operations::do_declare_create() interface.
1293  *
1294  * Declare that the caller will create the OST object.
1295  *
1296  * If the transaction is a remote transaction and the FID for the OST-object
1297  * has been assigned already, then handle it as creating (remote) MDT object
1298  * via osp_md_declare_create(). This function is usually used for LFSCK
1299  * to re-create the lost OST object. Otherwise, if it is not replay case, the
1300  * OSP will reserve pre-created object for the subsequent create operation;
1301  * if the MDT side cached pre-created objects are less than some threshold,
1302  * then it will wakeup the pre-create thread.
1303  *
1304  * \param[in] env       pointer to the thread context
1305  * \param[in] dt        pointer to the OSP layer dt_object
1306  * \param[in] attr      the attribute for the object to be created
1307  * \param[in] hint      pointer to the hint for creating the object, such as
1308  *                      the parent object
1309  * \param[in] dof       pointer to the dt_object_format for help the creation
1310  * \param[in] th        pointer to the transaction handler
1311  *
1312  * \retval              0 for success
1313  * \retval              negative error number on failure
1314  */
1315 static int osp_declare_create(const struct lu_env *env, struct dt_object *dt,
1316                               struct lu_attr *attr,
1317                               struct dt_allocation_hint *hint,
1318                               struct dt_object_format *dof, struct thandle *th)
1319 {
1320         struct osp_thread_info  *osi = osp_env_info(env);
1321         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
1322         struct osp_object       *o = dt2osp_obj(dt);
1323         const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
1324         struct thandle          *local_th;
1325         int                      rc = 0;
1326
1327         ENTRY;
1328
1329         if (is_only_remote_trans(th) && !fid_is_zero(fid)) {
1330                 LASSERT(fid_is_sane(fid));
1331
1332                 rc = osp_md_declare_create(env, dt, attr, hint, dof, th);
1333
1334                 RETURN(rc);
1335         }
1336
1337         /* should happen to non-0 OSP only so that at least one object
1338          * has been already declared in the scenario and LOD should
1339          * cleanup that */
1340         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
1341                 RETURN(-ENOSPC);
1342
1343         LASSERT(d->opd_last_used_oid_file);
1344
1345         /*
1346          * There can be gaps in precreated ids and record to unlink llog
1347          * XXX: we do not handle gaps yet, implemented before solution
1348          *      was found to be racy, so we disabled that. there is no
1349          *      point in making useless but expensive llog declaration.
1350          */
1351         /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
1352
1353         local_th = osp_get_storage_thandle(env, th, d);
1354         if (IS_ERR(local_th))
1355                 RETURN(PTR_ERR(local_th));
1356
1357         if (unlikely(!fid_is_zero(fid))) {
1358                 /* replay case: caller knows fid */
1359                 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1360                 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1361                 osi->osi_lb.lb_buf = NULL;
1362
1363                 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1364                                              &osi->osi_lb, osi->osi_off,
1365                                              local_th);
1366                 RETURN(rc);
1367         }
1368
1369         /*
1370          * in declaration we need to reserve object so that we don't block
1371          * awaiting precreation RPC to complete
1372          */
1373         rc = osp_precreate_reserve(env, d);
1374         /*
1375          * we also need to declare update to local "last used id" file for
1376          * recovery if object isn't used for a reason, we need to release
1377          * reservation, this can be made in osd_object_release()
1378          */
1379         if (rc == 0) {
1380                 /* mark id is reserved: in create we don't want to talk
1381                  * to OST */
1382                 LASSERT(o->opo_reserved == 0);
1383                 o->opo_reserved = 1;
1384
1385                 /* common for all OSPs file hystorically */
1386                 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1387                 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1388                 osi->osi_lb.lb_buf = NULL;
1389                 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1390                                              &osi->osi_lb, osi->osi_off,
1391                                              local_th);
1392         } else {
1393                 /* not needed in the cache anymore */
1394                 set_bit(LU_OBJECT_HEARD_BANSHEE,
1395                             &dt->do_lu.lo_header->loh_flags);
1396         }
1397         RETURN(rc);
1398 }
1399
1400 /**
1401  * Implement OSP layer dt_object_operations::do_create() interface.
1402  *
1403  * Create the OST object.
1404  *
1405  * If the transaction is a remote transaction and the FID for the OST-object
1406  * has been assigned already, then handle it as handling MDT object via the
1407  * osp_md_create(). For other cases, the OSP will assign FID to the
1408  * object to be created, and update last_used Object ID (OID) file.
1409  *
1410  * \param[in] env       pointer to the thread context
1411  * \param[in] dt        pointer to the OSP layer dt_object
1412  * \param[in] attr      the attribute for the object to be created
1413  * \param[in] hint      pointer to the hint for creating the object, such as
1414  *                      the parent object
1415  * \param[in] dof       pointer to the dt_object_format for help the creation
1416  * \param[in] th        pointer to the transaction handler
1417  *
1418  * \retval              0 for success
1419  * \retval              negative error number on failure
1420  */
1421 static int osp_create(const struct lu_env *env, struct dt_object *dt,
1422                       struct lu_attr *attr, struct dt_allocation_hint *hint,
1423                       struct dt_object_format *dof, struct thandle *th)
1424 {
1425         struct osp_thread_info  *osi = osp_env_info(env);
1426         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
1427         struct osp_object       *o = dt2osp_obj(dt);
1428         int                     rc = 0;
1429         struct lu_fid           *fid = &osi->osi_fid;
1430         struct thandle          *local_th;
1431         struct lu_fid           *last_fid = &d->opd_last_used_fid;
1432         ENTRY;
1433
1434         if (is_only_remote_trans(th) &&
1435             !fid_is_zero(lu_object_fid(&dt->do_lu))) {
1436                 LASSERT(fid_is_sane(lu_object_fid(&dt->do_lu)));
1437
1438                 rc = osp_md_create(env, dt, attr, hint, dof, th);
1439                 if (rc == 0)
1440                         o->opo_non_exist = 0;
1441
1442                 RETURN(rc);
1443         }
1444
1445         o->opo_non_exist = 0;
1446         if (o->opo_reserved) {
1447                 /* regular case, fid is assigned holding transaction open */
1448                  osp_object_assign_fid(env, d, o);
1449         }
1450
1451         memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
1452
1453         LASSERTF(fid_is_sane(fid), "fid for osp_object %p is insane"DFID"!\n",
1454                  o, PFID(fid));
1455
1456         if (!o->opo_reserved) {
1457                 /* special case, id was assigned outside of transaction
1458                  * see comments in osp_declare_attr_set */
1459                 LASSERT(d->opd_pre != NULL);
1460                 spin_lock(&d->opd_pre_lock);
1461                 osp_update_last_fid(d, fid);
1462                 spin_unlock(&d->opd_pre_lock);
1463         }
1464
1465         CDEBUG(D_INODE, "fid for osp_object %p is "DFID"\n", o, PFID(fid));
1466
1467         /* If the precreate ends, it means it will be ready to rollover to
1468          * the new sequence soon, all the creation should be synchronized,
1469          * otherwise during replay, the replay fid will be inconsistent with
1470          * last_used/create fid */
1471         if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
1472                 th->th_sync = 1;
1473
1474         local_th = osp_get_storage_thandle(env, th, d);
1475         if (IS_ERR(local_th))
1476                 RETURN(PTR_ERR(local_th));
1477         /*
1478          * it's OK if the import is inactive by this moment - id was created
1479          * by OST earlier, we just need to maintain it consistently on the disk
1480          * once import is reconnected, OSP will claim this and other objects
1481          * used and OST either keep them, if they exist or recreate
1482          */
1483
1484         /* we might have lost precreated objects */
1485         if (unlikely(d->opd_gap_count) > 0) {
1486                 LASSERT(d->opd_pre != NULL);
1487                 spin_lock(&d->opd_pre_lock);
1488                 if (d->opd_gap_count > 0) {
1489                         int count = d->opd_gap_count;
1490
1491                         rc = ostid_set_id(&osi->osi_oi,
1492                                           fid_oid(&d->opd_gap_start_fid));
1493                         if (rc) {
1494                                 spin_unlock(&d->opd_pre_lock);
1495                                 RETURN(rc);
1496                         }
1497                         d->opd_gap_count = 0;
1498                         spin_unlock(&d->opd_pre_lock);
1499
1500                         CDEBUG(D_HA, "Writing gap "DFID"+%d in llog\n",
1501                                PFID(&d->opd_gap_start_fid), count);
1502                         /* real gap handling is disabled intil ORI-692 will be
1503                          * fixed, now we only report gaps */
1504                 } else {
1505                         spin_unlock(&d->opd_pre_lock);
1506                 }
1507         }
1508
1509         /* Only need update last_used oid file, seq file will only be update
1510          * during seq rollover */
1511         if (fid_is_idif((last_fid)))
1512                 osi->osi_id = fid_idif_id(fid_seq(last_fid),
1513                                           fid_oid(last_fid), fid_ver(last_fid));
1514         else
1515                 osi->osi_id = fid_oid(last_fid);
1516         osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
1517                            &osi->osi_id, d->opd_index);
1518
1519         rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
1520                              &osi->osi_off, local_th);
1521
1522         CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
1523                d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
1524
1525         RETURN(rc);
1526 }
1527
1528 /**
1529  * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1530  *
1531  * Declare that the caller will destroy the specified OST object.
1532  *
1533  * The OST object destroy will be handled via llog asynchronously. This
1534  * function will declare the credits for generating MDS_UNLINK64_REC llog.
1535  *
1536  * \param[in] env       pointer to the thread context
1537  * \param[in] dt        pointer to the OSP layer dt_object to be destroyed
1538  * \param[in] th        pointer to the transaction handler
1539  *
1540  * \retval              0 for success
1541  * \retval              negative error number on failure
1542  */
1543 int osp_declare_destroy(const struct lu_env *env, struct dt_object *dt,
1544                         struct thandle *th)
1545 {
1546         struct osp_object       *o = dt2osp_obj(dt);
1547         struct osp_device       *osp = lu2osp_dev(dt->do_lu.lo_dev);
1548         int                      rc = 0;
1549
1550         ENTRY;
1551
1552         LASSERT(!osp->opd_connect_mdt);
1553         rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
1554
1555         RETURN(rc);
1556 }
1557
1558 /**
1559  * Implement OSP layer dt_object_operations::do_destroy() interface.
1560  *
1561  * Destroy the specified OST object.
1562  *
1563  * The OSP generates a MDS_UNLINK64_REC record in the llog. There
1564  * will be some dedicated thread to handle the llog asynchronously.
1565  *
1566  * It also marks the object as non-cached.
1567  *
1568  * \param[in] env       pointer to the thread context
1569  * \param[in] dt        pointer to the OSP layer dt_object to be destroyed
1570  * \param[in] th        pointer to the transaction handler
1571  *
1572  * \retval              0 for success
1573  * \retval              negative error number on failure
1574  */
1575 static int osp_destroy(const struct lu_env *env, struct dt_object *dt,
1576                        struct thandle *th)
1577 {
1578         struct osp_object       *o = dt2osp_obj(dt);
1579         struct osp_device       *osp = lu2osp_dev(dt->do_lu.lo_dev);
1580         int                      rc = 0;
1581
1582         ENTRY;
1583
1584         o->opo_non_exist = 1;
1585
1586         LASSERT(!osp->opd_connect_mdt);
1587         /* once transaction is committed put proper command on
1588          * the queue going to our OST. */
1589         rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
1590         if (rc < 0)
1591                 RETURN(rc);
1592
1593         /* not needed in cache any more */
1594         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1595
1596         RETURN(rc);
1597 }
1598
1599 static int osp_orphan_index_lookup(const struct lu_env *env,
1600                                    struct dt_object *dt,
1601                                    struct dt_rec *rec,
1602                                    const struct dt_key *key)
1603 {
1604         return -EOPNOTSUPP;
1605 }
1606
1607 static int osp_orphan_index_declare_insert(const struct lu_env *env,
1608                                            struct dt_object *dt,
1609                                            const struct dt_rec *rec,
1610                                            const struct dt_key *key,
1611                                            struct thandle *handle)
1612 {
1613         return -EOPNOTSUPP;
1614 }
1615
1616 static int osp_orphan_index_insert(const struct lu_env *env,
1617                                    struct dt_object *dt,
1618                                    const struct dt_rec *rec,
1619                                    const struct dt_key *key,
1620                                    struct thandle *handle,
1621                                    int ignore_quota)
1622 {
1623         return -EOPNOTSUPP;
1624 }
1625
1626 static int osp_orphan_index_declare_delete(const struct lu_env *env,
1627                                            struct dt_object *dt,
1628                                            const struct dt_key *key,
1629                                            struct thandle *handle)
1630 {
1631         return -EOPNOTSUPP;
1632 }
1633
1634 static int osp_orphan_index_delete(const struct lu_env *env,
1635                                    struct dt_object *dt,
1636                                    const struct dt_key *key,
1637                                    struct thandle *handle)
1638 {
1639         return -EOPNOTSUPP;
1640 }
1641
1642 /**
1643  * Initialize the OSP layer index iteration.
1644  *
1645  * \param[in] env       pointer to the thread context
1646  * \param[in] dt        pointer to the index object to be iterated
1647  * \param[in] attr      unused
1648  *
1649  * \retval              pointer to the iteration structure
1650  * \retval              negative error number on failure
1651  */
1652 struct dt_it *osp_it_init(const struct lu_env *env, struct dt_object *dt,
1653                           __u32 attr)
1654 {
1655         struct osp_it *it;
1656
1657         OBD_ALLOC_PTR(it);
1658         if (it == NULL)
1659                 return ERR_PTR(-ENOMEM);
1660
1661         it->ooi_pos_ent = -1;
1662         it->ooi_obj = dt;
1663         it->ooi_attr = attr;
1664
1665         return (struct dt_it *)it;
1666 }
1667
1668 /**
1669  * Finalize the OSP layer index iteration.
1670  *
1671  * \param[in] env       pointer to the thread context
1672  * \param[in] di        pointer to the iteration structure
1673  */
1674 void osp_it_fini(const struct lu_env *env, struct dt_it *di)
1675 {
1676         struct osp_it   *it = (struct osp_it *)di;
1677         struct page     **pages = it->ooi_pages;
1678         int             npages = it->ooi_total_npages;
1679         int             i;
1680
1681         if (pages != NULL) {
1682                 for (i = 0; i < npages; i++) {
1683                         if (pages[i] != NULL) {
1684                                 if (pages[i] == it->ooi_cur_page) {
1685                                         kunmap(pages[i]);
1686                                         it->ooi_cur_page = NULL;
1687                                 }
1688                                 __free_page(pages[i]);
1689                         }
1690                 }
1691                 OBD_FREE(pages, npages * sizeof(*pages));
1692         }
1693         OBD_FREE_PTR(it);
1694 }
1695
1696 /**
1697  * Get more records for the iteration from peer.
1698  *
1699  * The new records will be filled in an array of pages. The OSP side
1700  * allows 1MB bulk data to be transferred.
1701  *
1702  * \param[in] env       pointer to the thread context
1703  * \param[in] it        pointer to the iteration structure
1704  *
1705  * \retval              0 for success
1706  * \retval              negative error number on failure
1707  */
1708 static int osp_it_fetch(const struct lu_env *env, struct osp_it *it)
1709 {
1710         struct lu_device         *dev   = it->ooi_obj->do_lu.lo_dev;
1711         struct osp_device        *osp   = lu2osp_dev(dev);
1712         struct page             **pages;
1713         struct ptlrpc_request    *req   = NULL;
1714         struct ptlrpc_bulk_desc  *desc;
1715         struct idx_info          *ii;
1716         int                       npages;
1717         int                       rc;
1718         int                       i;
1719         ENTRY;
1720
1721         /* 1MB bulk */
1722         npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
1723         npages /= PAGE_SIZE;
1724
1725         OBD_ALLOC(pages, npages * sizeof(*pages));
1726         if (pages == NULL)
1727                 RETURN(-ENOMEM);
1728
1729         it->ooi_pages = pages;
1730         it->ooi_total_npages = npages;
1731         for (i = 0; i < npages; i++) {
1732                 pages[i] = alloc_page(GFP_NOFS);
1733                 if (pages[i] == NULL)
1734                         RETURN(-ENOMEM);
1735         }
1736
1737         req = ptlrpc_request_alloc(osp->opd_obd->u.cli.cl_import,
1738                                    &RQF_OBD_IDX_READ);
1739         if (req == NULL)
1740                 RETURN(-ENOMEM);
1741
1742         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, OBD_IDX_READ);
1743         if (rc != 0) {
1744                 ptlrpc_request_free(req);
1745                 RETURN(rc);
1746         }
1747
1748         osp_set_req_replay(osp, req);
1749         req->rq_request_portal = OUT_PORTAL;
1750         ii = req_capsule_client_get(&req->rq_pill, &RMF_IDX_INFO);
1751         memset(ii, 0, sizeof(*ii));
1752         if (fid_is_last_id(lu_object_fid(&it->ooi_obj->do_lu))) {
1753                 /* LFSCK will iterate orphan object[FID_SEQ_LAYOUT_BTREE,
1754                  * ost_index, 0] with LAST_ID FID, so it needs to replace
1755                  * the FID with orphan FID here */
1756                 ii->ii_fid.f_seq = FID_SEQ_LAYOUT_RBTREE;
1757                 ii->ii_fid.f_oid = osp->opd_index;
1758                 ii->ii_fid.f_ver = 0;
1759                 ii->ii_flags = II_FL_NOHASH;
1760                 ii->ii_attrs = osp_dev2node(osp);
1761         } else {
1762                 ii->ii_fid = *lu_object_fid(&it->ooi_obj->do_lu);
1763                 ii->ii_flags = II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
1764                                II_FL_VARREC;
1765                 ii->ii_attrs = it->ooi_attr;
1766         }
1767         ii->ii_magic = IDX_INFO_MAGIC;
1768         ii->ii_count = npages * LU_PAGE_COUNT;
1769         ii->ii_hash_start = it->ooi_next;
1770
1771         ptlrpc_at_set_req_timeout(req);
1772
1773         desc = ptlrpc_prep_bulk_imp(req, npages, 1,
1774                                     PTLRPC_BULK_PUT_SINK | PTLRPC_BULK_BUF_KIOV,
1775                                     MDS_BULK_PORTAL,
1776                                     &ptlrpc_bulk_kiov_pin_ops);
1777         if (desc == NULL)
1778                 GOTO(out, rc = -ENOMEM);
1779
1780         for (i = 0; i < npages; i++)
1781                 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1782                                                  PAGE_SIZE);
1783
1784         ptlrpc_request_set_replen(req);
1785         rc = ptlrpc_queue_wait(req);
1786         if (rc != 0)
1787                 GOTO(out, rc);
1788
1789         rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1790                                           req->rq_bulk->bd_nob_transferred);
1791         if (rc < 0)
1792                 GOTO(out, rc);
1793         rc = 0;
1794
1795         ii = req_capsule_server_get(&req->rq_pill, &RMF_IDX_INFO);
1796         if (ii->ii_magic != IDX_INFO_MAGIC)
1797                  GOTO(out, rc = -EPROTO);
1798
1799         npages = (ii->ii_count + LU_PAGE_COUNT - 1) >>
1800                  (PAGE_SHIFT - LU_PAGE_SHIFT);
1801         if (npages > it->ooi_total_npages) {
1802                 CERROR("%s: returned more pages than expected, %u > %u\n",
1803                        osp->opd_obd->obd_name, npages, it->ooi_total_npages);
1804                 GOTO(out, rc = -EINVAL);
1805         }
1806
1807         it->ooi_rec_size = ii->ii_recsize;
1808         it->ooi_valid_npages = npages;
1809         if (ptlrpc_rep_need_swab(req))
1810                 it->ooi_swab = 1;
1811
1812         it->ooi_next = ii->ii_hash_end;
1813
1814 out:
1815         ptlrpc_req_finished(req);
1816
1817         return rc;
1818 }
1819
1820 /**
1821  * Move the iteration cursor to the next lu_page.
1822  *
1823  * One system page (PAGE_SIZE) may contain multiple lu_page (4KB),
1824  * that depends on the LU_PAGE_COUNT. If it is not the last lu_page
1825  * in current system page, then move the iteration cursor to the next
1826  * lu_page in current system page. Otherwise, if there are more system
1827  * pages in the cache, then move the iteration cursor to the next system
1828  * page. If all the cached records (pages) have been iterated, then fetch
1829  * more records via osp_it_fetch().
1830  *
1831  * \param[in] env       pointer to the thread context
1832  * \param[in] di        pointer to the iteration structure
1833  *
1834  * \retval              positive for end of the directory
1835  * \retval              0 for success
1836  * \retval              negative error number on failure
1837  */
1838 int osp_it_next_page(const struct lu_env *env, struct dt_it *di)
1839 {
1840         struct osp_it           *it = (struct osp_it *)di;
1841         struct lu_idxpage       *idxpage;
1842         struct page             **pages;
1843         int                     rc;
1844         int                     i;
1845         ENTRY;
1846
1847 again2:
1848         idxpage = it->ooi_cur_idxpage;
1849         if (idxpage != NULL) {
1850                 if (idxpage->lip_nr == 0)
1851                         RETURN(1);
1852
1853                 if (it->ooi_pos_ent < idxpage->lip_nr) {
1854                         CDEBUG(D_INFO, "ooi_pos %d nr %d\n",
1855                                (int)it->ooi_pos_ent, (int)idxpage->lip_nr);
1856                         RETURN(0);
1857                 }
1858                 it->ooi_cur_idxpage = NULL;
1859                 it->ooi_pos_lu_page++;
1860
1861 again1:
1862                 if (it->ooi_pos_lu_page < LU_PAGE_COUNT) {
1863                         it->ooi_cur_idxpage = (void *)it->ooi_cur_page +
1864                                          LU_PAGE_SIZE * it->ooi_pos_lu_page;
1865                         if (it->ooi_swab)
1866                                 lustre_swab_lip_header(it->ooi_cur_idxpage);
1867                         if (it->ooi_cur_idxpage->lip_magic != LIP_MAGIC) {
1868                                 struct osp_device *osp =
1869                                         lu2osp_dev(it->ooi_obj->do_lu.lo_dev);
1870
1871                                 CERROR("%s: invalid magic (%x != %x) for page "
1872                                        "%d/%d while read layout orphan index\n",
1873                                        osp->opd_obd->obd_name,
1874                                        it->ooi_cur_idxpage->lip_magic,
1875                                        LIP_MAGIC, it->ooi_pos_page,
1876                                        it->ooi_pos_lu_page);
1877                                 /* Skip this lu_page next time. */
1878                                 it->ooi_pos_ent = idxpage->lip_nr - 1;
1879                                 RETURN(-EINVAL);
1880                         }
1881                         it->ooi_pos_ent = -1;
1882                         goto again2;
1883                 }
1884
1885                 kunmap(it->ooi_cur_page);
1886                 it->ooi_cur_page = NULL;
1887                 it->ooi_pos_page++;
1888
1889 again0:
1890                 pages = it->ooi_pages;
1891                 if (it->ooi_pos_page < it->ooi_valid_npages) {
1892                         it->ooi_cur_page = kmap(pages[it->ooi_pos_page]);
1893                         it->ooi_pos_lu_page = 0;
1894                         goto again1;
1895                 }
1896
1897                 for (i = 0; i < it->ooi_total_npages; i++) {
1898                         if (pages[i] != NULL)
1899                                 __free_page(pages[i]);
1900                 }
1901                 OBD_FREE(pages, it->ooi_total_npages * sizeof(*pages));
1902
1903                 it->ooi_pos_page = 0;
1904                 it->ooi_total_npages = 0;
1905                 it->ooi_valid_npages = 0;
1906                 it->ooi_swab = 0;
1907                 it->ooi_ent = NULL;
1908                 it->ooi_cur_page = NULL;
1909                 it->ooi_cur_idxpage = NULL;
1910                 it->ooi_pages = NULL;
1911         }
1912
1913         if (it->ooi_next == II_END_OFF)
1914                 RETURN(1);
1915
1916         rc = osp_it_fetch(env, it);
1917         if (rc == 0)
1918                 goto again0;
1919
1920         RETURN(rc);
1921 }
1922
1923 /**
1924  * Move the iteration cursor to the next record.
1925  *
1926  * If there are more records in the lu_page, then move the iteration
1927  * cursor to the next record directly. Otherwise, move the iteration
1928  * cursor to the record in the next lu_page via osp_it_next_page()
1929  *
1930  * \param[in] env       pointer to the thread context
1931  * \param[in] di        pointer to the iteration structure
1932  *
1933  * \retval              positive for end of the directory
1934  * \retval              0 for success
1935  * \retval              negative error number on failure
1936  */
1937 static int osp_orphan_it_next(const struct lu_env *env, struct dt_it *di)
1938 {
1939         struct osp_it           *it = (struct osp_it *)di;
1940         struct lu_idxpage       *idxpage;
1941         int                     rc;
1942         ENTRY;
1943
1944 again:
1945         idxpage = it->ooi_cur_idxpage;
1946         if (idxpage != NULL) {
1947                 if (idxpage->lip_nr == 0)
1948                         RETURN(1);
1949
1950                 it->ooi_pos_ent++;
1951                 if (it->ooi_pos_ent < idxpage->lip_nr) {
1952                         if (it->ooi_rec_size ==
1953                                         sizeof(struct lu_orphan_rec_v2)) {
1954                                 it->ooi_ent =
1955                                 (struct lu_orphan_ent_v2 *)idxpage->lip_entries+
1956                                                         it->ooi_pos_ent;
1957                                 if (it->ooi_swab)
1958                                         lustre_swab_orphan_ent_v2(it->ooi_ent);
1959                         } else {
1960                                 it->ooi_ent =
1961                                 (struct lu_orphan_ent *)idxpage->lip_entries +
1962                                                         it->ooi_pos_ent;
1963                                 if (it->ooi_swab)
1964                                         lustre_swab_orphan_ent(it->ooi_ent);
1965                         }
1966                         RETURN(0);
1967                 }
1968         }
1969
1970         rc = osp_it_next_page(env, di);
1971         if (rc == 0)
1972                 goto again;
1973
1974         RETURN(rc);
1975 }
1976
1977 int osp_it_get(const struct lu_env *env, struct dt_it *di,
1978                const struct dt_key *key)
1979 {
1980         return 1;
1981 }
1982
1983 void osp_it_put(const struct lu_env *env, struct dt_it *di)
1984 {
1985 }
1986
1987 static struct dt_key *osp_orphan_it_key(const struct lu_env *env,
1988                                         const struct dt_it *di)
1989 {
1990         struct osp_it   *it  = (struct osp_it *)di;
1991         struct lu_orphan_ent    *ent = (struct lu_orphan_ent *)it->ooi_ent;
1992
1993         if (likely(ent != NULL))
1994                 return (struct dt_key *)(&ent->loe_key);
1995
1996         return NULL;
1997 }
1998
1999 static int osp_orphan_it_key_size(const struct lu_env *env,
2000                                   const struct dt_it *di)
2001 {
2002         return sizeof(struct lu_fid);
2003 }
2004
2005 static int osp_orphan_it_rec(const struct lu_env *env, const struct dt_it *di,
2006                              struct dt_rec *rec, __u32 attr)
2007 {
2008         struct osp_it *it = (struct osp_it *)di;
2009
2010         if (likely(it->ooi_ent)) {
2011                 if (it->ooi_rec_size == sizeof(struct lu_orphan_rec_v2)) {
2012                         struct lu_orphan_ent_v2 *ent =
2013                                 (struct lu_orphan_ent_v2 *)it->ooi_ent;
2014
2015                         *(struct lu_orphan_rec_v2 *)rec = ent->loe_rec;
2016                 } else {
2017                         struct lu_orphan_ent *ent =
2018                                 (struct lu_orphan_ent *)it->ooi_ent;
2019
2020                         *(struct lu_orphan_rec *)rec = ent->loe_rec;
2021                 }
2022                 return 0;
2023         }
2024
2025         return -EINVAL;
2026 }
2027
2028 __u64 osp_it_store(const struct lu_env *env, const struct dt_it *di)
2029 {
2030         struct osp_it   *it = (struct osp_it *)di;
2031
2032         return it->ooi_next;
2033 }
2034
2035 /**
2036  * Locate the iteration cursor to the specified position (cookie).
2037  *
2038  * \param[in] env       pointer to the thread context
2039  * \param[in] di        pointer to the iteration structure
2040  * \param[in] hash      the specified position
2041  *
2042  * \retval              positive number for locating to the exactly position
2043  *                      or the next
2044  * \retval              0 for arriving at the end of the iteration
2045  * \retval              negative error number on failure
2046  */
2047 int osp_orphan_it_load(const struct lu_env *env, const struct dt_it *di,
2048                        __u64 hash)
2049 {
2050         struct osp_it   *it     = (struct osp_it *)di;
2051         int              rc;
2052
2053         it->ooi_next = hash;
2054         rc = osp_orphan_it_next(env, (struct dt_it *)di);
2055         if (rc == 1)
2056                 return 0;
2057
2058         if (rc == 0)
2059                 return 1;
2060
2061         return rc;
2062 }
2063
2064 int osp_it_key_rec(const struct lu_env *env, const struct dt_it *di,
2065                    void *key_rec)
2066 {
2067         return 0;
2068 }
2069
2070 static const struct dt_index_operations osp_orphan_index_ops = {
2071         .dio_lookup             = osp_orphan_index_lookup,
2072         .dio_declare_insert     = osp_orphan_index_declare_insert,
2073         .dio_insert             = osp_orphan_index_insert,
2074         .dio_declare_delete     = osp_orphan_index_declare_delete,
2075         .dio_delete             = osp_orphan_index_delete,
2076         .dio_it = {
2077                 .init           = osp_it_init,
2078                 .fini           = osp_it_fini,
2079                 .next           = osp_orphan_it_next,
2080                 .get            = osp_it_get,
2081                 .put            = osp_it_put,
2082                 .key            = osp_orphan_it_key,
2083                 .key_size       = osp_orphan_it_key_size,
2084                 .rec            = osp_orphan_it_rec,
2085                 .store          = osp_it_store,
2086                 .load           = osp_orphan_it_load,
2087                 .key_rec        = osp_it_key_rec,
2088         }
2089 };
2090
2091 /**
2092  * Implement OSP layer dt_object_operations::do_index_try() interface.
2093  *
2094  * Negotiate the index type.
2095  *
2096  * If the target index is an IDIF object, then use osp_orphan_index_ops.
2097  * Otherwise, assign osp_md_index_ops to the dt_object::do_index_ops.
2098  * (\see lustre/include/lustre_fid.h for IDIF.)
2099  *
2100  * \param[in] env       pointer to the thread context
2101  * \param[in] dt        pointer to the OSP layer dt_object
2102  * \param[in] feat      unused
2103  *
2104  * \retval              0 for success
2105  */
2106 static int osp_index_try(const struct lu_env *env,
2107                          struct dt_object *dt,
2108                          const struct dt_index_features *feat)
2109 {
2110         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2111
2112         if (fid_is_last_id(fid) && fid_is_idif(fid))
2113                 dt->do_index_ops = &osp_orphan_index_ops;
2114         else
2115                 dt->do_index_ops = &osp_md_index_ops;
2116         return 0;
2117 }
2118
2119 static struct dt_object_operations osp_obj_ops = {
2120         .do_declare_attr_get    = osp_declare_attr_get,
2121         .do_attr_get            = osp_attr_get,
2122         .do_declare_attr_set    = osp_declare_attr_set,
2123         .do_attr_set            = osp_attr_set,
2124         .do_declare_xattr_get   = osp_declare_xattr_get,
2125         .do_xattr_get           = osp_xattr_get,
2126         .do_declare_xattr_set   = osp_declare_xattr_set,
2127         .do_xattr_set           = osp_xattr_set,
2128         .do_declare_create      = osp_declare_create,
2129         .do_create              = osp_create,
2130         .do_declare_destroy     = osp_declare_destroy,
2131         .do_destroy             = osp_destroy,
2132         .do_index_try           = osp_index_try,
2133 };
2134
2135 /**
2136  * Implement OSP layer lu_object_operations::loo_object_init() interface.
2137  *
2138  * Initialize the object.
2139  *
2140  * If it is a remote MDT object, then call do_attr_get() to fetch
2141  * the attribute from the peer.
2142  *
2143  * \param[in] env       pointer to the thread context
2144  * \param[in] o         pointer to the OSP layer lu_object
2145  * \param[in] conf      unused
2146  *
2147  * \retval              0 for success
2148  * \retval              negative error number on failure
2149  */
2150 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
2151                            const struct lu_object_conf *conf)
2152 {
2153         struct osp_object       *po = lu2osp_obj(o);
2154         int                     rc = 0;
2155         ENTRY;
2156
2157         spin_lock_init(&po->opo_lock);
2158         o->lo_header->loh_attr |= LOHA_REMOTE;
2159         INIT_LIST_HEAD(&po->opo_xattr_list);
2160         INIT_LIST_HEAD(&po->opo_invalidate_cb_list);
2161
2162         if (is_ost_obj(o)) {
2163                 po->opo_obj.do_ops = &osp_obj_ops;
2164         } else {
2165                 struct lu_attr *la = &osp_env_info(env)->osi_attr;
2166
2167                 po->opo_obj.do_ops = &osp_md_obj_ops;
2168                 po->opo_obj.do_body_ops = &osp_md_body_ops;
2169
2170                 if (conf != NULL && conf->loc_flags & LOC_F_NEW) {
2171                         po->opo_non_exist = 1;
2172                 } else {
2173                         rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o),
2174                                                              la);
2175                         if (rc == 0)
2176                                 o->lo_header->loh_attr |=
2177                                         LOHA_EXISTS | (la->la_mode & S_IFMT);
2178                         if (rc == -ENOENT) {
2179                                 po->opo_non_exist = 1;
2180                                 rc = 0;
2181                         }
2182                 }
2183                 init_rwsem(&po->opo_sem);
2184         }
2185         RETURN(rc);
2186 }
2187
2188 /**
2189  * Implement OSP layer lu_object_operations::loo_object_free() interface.
2190  *
2191  * Finalize the object.
2192  *
2193  * If the OSP object has attributes cache, then destroy the cache.
2194  * Free the object finally.
2195  *
2196  * \param[in] env       pointer to the thread context
2197  * \param[in] o         pointer to the OSP layer lu_object
2198  */
2199 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
2200 {
2201         struct osp_object       *obj = lu2osp_obj(o);
2202         struct lu_object_header *h = o->lo_header;
2203         struct osp_xattr_entry *oxe;
2204         struct osp_xattr_entry *tmp;
2205         int                     count;
2206
2207         dt_object_fini(&obj->opo_obj);
2208         lu_object_header_fini(h);
2209         list_for_each_entry_safe(oxe, tmp, &obj->opo_xattr_list, oxe_list) {
2210                 list_del(&oxe->oxe_list);
2211                 count = atomic_read(&oxe->oxe_ref);
2212                 LASSERTF(count == 1,
2213                          "Still has %d users on the xattr entry %.*s\n",
2214                          count-1, (int)oxe->oxe_namelen, oxe->oxe_buf);
2215
2216                 OBD_FREE(oxe, oxe->oxe_buflen);
2217         }
2218         OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
2219 }
2220
2221 /**
2222  * Implement OSP layer lu_object_operations::loo_object_release() interface.
2223  *
2224  * Cleanup (not free) the object.
2225  *
2226  * If it is a reserved object but failed to be created, or it is an OST
2227  * object, then mark the object as non-cached.
2228  *
2229  * \param[in] env       pointer to the thread context
2230  * \param[in] o         pointer to the OSP layer lu_object
2231  */
2232 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
2233 {
2234         struct osp_object       *po = lu2osp_obj(o);
2235         struct osp_device       *d  = lu2osp_dev(o->lo_dev);
2236
2237         ENTRY;
2238
2239         /*
2240          * release reservation if object was declared but not created
2241          * this may require lu_object_put() in LOD
2242          */
2243         if (unlikely(po->opo_reserved)) {
2244                 LASSERT(d->opd_pre != NULL);
2245                 LASSERT(d->opd_pre_reserved > 0);
2246                 spin_lock(&d->opd_pre_lock);
2247                 d->opd_pre_reserved--;
2248                 spin_unlock(&d->opd_pre_lock);
2249
2250                 /* not needed in cache any more */
2251                 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2252         }
2253
2254         if (is_ost_obj(o))
2255                 /* XXX: Currently, NOT cache OST-object on MDT because:
2256                  *      1. it is not often accessed on MDT.
2257                  *      2. avoid up layer (such as LFSCK) to load too many
2258                  *         once-used OST-objects. */
2259                 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2260
2261         EXIT;
2262 }
2263
2264 static int osp_object_print(const struct lu_env *env, void *cookie,
2265                             lu_printer_t p, const struct lu_object *l)
2266 {
2267         const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
2268
2269         return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
2270 }
2271
2272 static int osp_object_invariant(const struct lu_object *o)
2273 {
2274         LBUG();
2275 }
2276
2277 struct lu_object_operations osp_lu_obj_ops = {
2278         .loo_object_init        = osp_object_init,
2279         .loo_object_free        = osp_object_free,
2280         .loo_object_release     = osp_object_release,
2281         .loo_object_print       = osp_object_print,
2282         .loo_object_invariant   = osp_object_invariant
2283 };