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