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