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