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