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