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