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