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