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