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