Whamcloud - gitweb
2835bbb6a66fbff63b156600e93d162d65685a6d
[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 (o->opo_new)
627                 /* no need in logging for new objects being created */
628                 RETURN(0);
629
630         if (!(attr->la_valid & (LA_UID | LA_GID)))
631                 RETURN(0);
632
633         if (!is_only_remote_trans(th))
634                 /*
635                  * track all UID/GID changes via llog
636                  */
637                 rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
638         else
639                 /* It is for OST-object attr_set directly without updating
640                  * local MDT-object attribute. It is usually used by LFSCK. */
641                 rc = osp_md_declare_attr_set(env, dt, attr, th);
642
643         if (rc != 0 || o->opo_ooa == NULL)
644                 RETURN(rc);
645
646         /* Update the OSP object attributes cache. */
647         la = &o->opo_ooa->ooa_attr;
648         spin_lock(&o->opo_lock);
649         if (attr->la_valid & LA_UID) {
650                 la->la_uid = attr->la_uid;
651                 la->la_valid |= LA_UID;
652         }
653
654         if (attr->la_valid & LA_GID) {
655                 la->la_gid = attr->la_gid;
656                 la->la_valid |= LA_GID;
657         }
658         spin_unlock(&o->opo_lock);
659
660         RETURN(0);
661 }
662
663 /**
664  * Implement OSP layer dt_object_operations::do_declare_attr_set() interface.
665  * XXX: NOT prepare set_{attr,xattr} RPC for remote transaction.
666  *
667  * According to our current transaction/dt_object_lock framework (to make
668  * the cross-MDTs modification for DNE1 to be workable), the transaction
669  * sponsor will start the transaction firstly, then try to acquire related
670  * dt_object_lock if needed. Under such rules, if we want to prepare the
671  * set_{attr,xattr} RPC in the RPC declare phase, then related attr/xattr
672  * should be known without dt_object_lock. But such condition maybe not
673  * true for some remote transaction case. For example:
674  *
675  * For linkEA repairing (by LFSCK) case, before the LFSCK thread obtained
676  * the dt_object_lock on the target MDT-object, it cannot know whether
677  * the MDT-object has linkEA or not, neither invalid or not.
678  *
679  * Since the LFSCK thread cannot hold dt_object_lock before the (remote)
680  * transaction start (otherwise there will be some potential deadlock),
681  * it cannot prepare related RPC for repairing during the declare phase
682  * as other normal transactions do.
683  *
684  * To resolve the trouble, we will make OSP to prepare related RPC
685  * (set_attr/set_xattr/del_xattr) after remote transaction started,
686  * and trigger the remote updating (RPC sending) when trans_stop.
687  * Then the up layer users, such as LFSCK, can follow the general
688  * rule to handle trans_start/dt_object_lock for repairing linkEA
689  * inconsistency without distinguishing remote MDT-object.
690  *
691  * In fact, above solution for remote transaction should be the normal
692  * model without considering DNE1. The trouble brought by DNE1 will be
693  * resolved in DNE2. At that time, this patch can be removed.
694  *
695  * \param[in] env       pointer to the thread context
696  * \param[in] dt        pointer to the OSP layer dt_object
697  * \param[in] attr      pointer to the attribute to be set
698  * \param[in] th        pointer to the transaction handler
699  *
700  * \retval              0 for success
701  * \retval              negative error number on failure
702  */
703 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
704                                 const struct lu_attr *attr, struct thandle *th)
705 {
706         int rc = 0;
707
708         if (!is_only_remote_trans(th))
709                 rc = __osp_attr_set(env, dt, attr, th);
710
711         return rc;
712 }
713
714 /**
715  * Implement OSP layer dt_object_operations::do_attr_set() interface.
716  *
717  * Set attribute to the specified OST object.
718  *
719  * If the transaction is a remote transaction, then related modification
720  * sub-request has been added in the declare phase and related OUT RPC
721  * has been triggered at transaction start. Otherwise it will generate
722  * a MDS_SETATTR64_REC record in the llog. There is a dedicated thread
723  * to handle the llog asynchronously.
724  *
725  * If the attribute entry exists in the OSP object attributes cache,
726  * then update the cached attribute according to given attribute.
727  *
728  * \param[in] env       pointer to the thread context
729  * \param[in] dt        pointer to the OSP layer dt_object
730  * \param[in] attr      pointer to the attribute to be set
731  * \param[in] th        pointer to the transaction handler
732  * \param[in] capa      the capability for this operation
733  *
734  * \retval              0 for success
735  * \retval              negative error number on failure
736  */
737 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
738                         const struct lu_attr *attr, struct thandle *th,
739                         struct lustre_capa *capa)
740 {
741         struct osp_object       *o = dt2osp_obj(dt);
742         int                      rc = 0;
743         ENTRY;
744
745         if (is_only_remote_trans(th)) {
746                 rc = __osp_attr_set(env, dt, attr, th);
747                 if (rc != 0)
748                         RETURN(rc);
749         }
750
751         /* we're interested in uid/gid changes only */
752         if (!(attr->la_valid & (LA_UID | LA_GID)))
753                 RETURN(0);
754
755         /* new object, the very first ->attr_set()
756          * initializing attributes needs no logging
757          * all subsequent one are subject to the
758          * logging and synchronization with OST */
759         if (o->opo_new) {
760                 o->opo_new = 0;
761                 RETURN(0);
762         }
763
764         if (!is_only_remote_trans(th))
765                 /*
766                  * once transaction is committed put proper command on
767                  * the queue going to our OST
768                  */
769                 rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
770                 /* XXX: send new uid/gid to OST ASAP? */
771         else
772                 /* It is for OST-object attr_set directly without updating
773                  * local MDT-object attribute. It is usually used by LFSCK. */
774                 rc = osp_md_attr_set(env, dt, attr, th, capa);
775
776         RETURN(rc);
777 }
778
779 /**
780  * Interpreter function for getting OSP object extended attribute asynchronously
781  *
782  * Called to interpret the result of an async mode RPC for getting the
783  * OSP object extended attribute.
784  *
785  * \param[in] env       pointer to the thread context
786  * \param[in] reply     pointer to the RPC reply
787  * \param[in] req       pointer to the RPC request
788  * \param[in] obj       pointer to the OSP object
789  * \param[out] data     pointer to OSP object attributes cache
790  * \param[in] index     the index of the attribute buffer in the reply
791  * \param[in] rc        the result for handling the RPC
792  *
793  * \retval              0 for success
794  * \retval              negative error number on failure
795  */
796 static int osp_xattr_get_interpterer(const struct lu_env *env,
797                                      struct object_update_reply *reply,
798                                      struct ptlrpc_request *req,
799                                      struct osp_object *obj,
800                                      void *data, int index, int rc)
801 {
802         struct osp_object_attr  *ooa  = obj->opo_ooa;
803         struct osp_xattr_entry  *oxe  = data;
804         struct lu_buf           *rbuf = &osp_env_info(env)->osi_lb2;
805
806         LASSERT(ooa != NULL);
807
808         if (rc == 0) {
809                 size_t len = sizeof(*oxe) + oxe->oxe_namelen + 1;
810
811                 rc = object_update_result_data_get(reply, rbuf, index);
812                 if (rc < 0 || rbuf->lb_len > (oxe->oxe_buflen - len)) {
813                         spin_lock(&obj->opo_lock);
814                         oxe->oxe_ready = 0;
815                         spin_unlock(&obj->opo_lock);
816                         osp_oac_xattr_put(oxe);
817
818                         return rc < 0 ? rc : -ERANGE;
819                 }
820
821                 spin_lock(&obj->opo_lock);
822                 oxe->oxe_vallen = rbuf->lb_len;
823                 memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
824                 oxe->oxe_exist = 1;
825                 oxe->oxe_ready = 1;
826                 spin_unlock(&obj->opo_lock);
827         } else if (rc == -ENOENT || rc == -ENODATA) {
828                 spin_lock(&obj->opo_lock);
829                 oxe->oxe_exist = 0;
830                 oxe->oxe_ready = 1;
831                 spin_unlock(&obj->opo_lock);
832         } else {
833                 spin_lock(&obj->opo_lock);
834                 oxe->oxe_ready = 0;
835                 spin_unlock(&obj->opo_lock);
836         }
837
838         osp_oac_xattr_put(oxe);
839
840         return 0;
841 }
842
843 /**
844  * Implement OSP dt_object_operations::do_declare_xattr_get() interface.
845  *
846  * Declare that the caller will get extended attribute from the specified
847  * OST object.
848  *
849  * This function will add an OUT_XATTR_GET sub-request to the per OSP
850  * based shared asynchronous request queue with the interpreter function:
851  * osp_xattr_get_interpterer().
852  *
853  * \param[in] env       pointer to the thread context
854  * \param[in] dt        pointer to the OSP layer dt_object
855  * \param[out] buf      pointer to the lu_buf to hold the extended attribute
856  * \param[in] name      the name for the expected extended attribute
857  * \param[in] capa      the capability for this operation
858  *
859  * \retval              0 for success
860  * \retval              negative error number on failure
861  */
862 static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
863                                  struct lu_buf *buf, const char *name,
864                                  struct lustre_capa *capa)
865 {
866         struct osp_object       *obj     = dt2osp_obj(dt);
867         struct osp_device       *osp     = lu2osp_dev(dt->do_lu.lo_dev);
868         struct osp_xattr_entry  *oxe;
869         __u16                    namelen = strlen(name);
870         int                      rc      = 0;
871
872         LASSERT(buf != NULL);
873         LASSERT(name != NULL);
874
875         /* If only for xattr size, return directly. */
876         if (unlikely(buf->lb_len == 0))
877                 return 0;
878
879         if (obj->opo_ooa == NULL) {
880                 rc = osp_oac_init(obj);
881                 if (rc != 0)
882                         return rc;
883         }
884
885         oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
886         if (oxe == NULL)
887                 return -ENOMEM;
888
889         mutex_lock(&osp->opd_async_requests_mutex);
890         rc = osp_insert_async_request(env, OUT_XATTR_GET, obj, 1,
891                                       &namelen, (const void **)&name, oxe,
892                                       osp_xattr_get_interpterer);
893         if (rc != 0) {
894                 mutex_unlock(&osp->opd_async_requests_mutex);
895                 osp_oac_xattr_put(oxe);
896         } else {
897                 struct dt_update_request *update;
898
899                 /* XXX: Currently, we trigger the batched async OUT
900                  *      RPC via dt_declare_xattr_get(). It is not
901                  *      perfect solution, but works well now.
902                  *
903                  *      We will improve it in the future. */
904                 update = osp->opd_async_requests;
905                 if (update != NULL && update->dur_buf.ub_req != NULL &&
906                     update->dur_buf.ub_req->ourq_count > 0) {
907                         osp->opd_async_requests = NULL;
908                         mutex_unlock(&osp->opd_async_requests_mutex);
909                         rc = osp_unplug_async_request(env, osp, update);
910                 } else {
911                         mutex_unlock(&osp->opd_async_requests_mutex);
912                 }
913         }
914
915         return rc;
916 }
917
918 /**
919  * Implement OSP layer dt_object_operations::do_xattr_get() interface.
920  *
921  * Get extended attribute from the specified MDT/OST object.
922  *
923  * If the extended attribute is in the OSP object attributes cache, then
924  * return the cached extended attribute directly. Otherwise it will get
925  * the extended attribute synchronously, if successful, add it to the OSP
926  * attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
927  *
928  * There is a race condition: some other thread has added the named extended
929  * attributed entry to the OSP object attributes cache during the current
930  * OUT_XATTR_GET handling. If such case happens, the OSP will replace the
931  * (just) existing extended attribute entry with the new replied one.
932  *
933  * \param[in] env       pointer to the thread context
934  * \param[in] dt        pointer to the OSP layer dt_object
935  * \param[out] buf      pointer to the lu_buf to hold the extended attribute
936  * \param[in] name      the name for the expected extended attribute
937  * \param[in] capa      the capability for this operation
938  *
939  * \retval              0 for success
940  * \retval              negative error number on failure
941  */
942 int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
943                   struct lu_buf *buf, const char *name,
944                   struct lustre_capa *capa)
945 {
946         struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
947         struct osp_object       *obj    = dt2osp_obj(dt);
948         struct dt_device        *dev    = &osp->opd_dt_dev;
949         struct lu_buf           *rbuf   = &osp_env_info(env)->osi_lb2;
950         struct dt_update_request *update = NULL;
951         struct ptlrpc_request   *req    = NULL;
952         struct object_update_reply *reply;
953         struct osp_xattr_entry  *oxe    = NULL;
954         const char              *dname  = dt->do_lu.lo_dev->ld_obd->obd_name;
955         int                      rc     = 0;
956         ENTRY;
957
958         LASSERT(buf != NULL);
959         LASSERT(name != NULL);
960
961         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NETWORK) &&
962             osp->opd_index == cfs_fail_val) {
963                 if (is_ost_obj(&dt->do_lu)) {
964                         if (osp_dev2node(osp) == cfs_fail_val)
965                                 RETURN(-ENOTCONN);
966                 } else {
967                         if (strcmp(name, XATTR_NAME_LINK) == 0)
968                                 RETURN(-ENOTCONN);
969                 }
970         }
971
972         if (unlikely(obj->opo_non_exist))
973                 RETURN(-ENOENT);
974
975         oxe = osp_oac_xattr_find(obj, name, false);
976         if (oxe != NULL) {
977                 spin_lock(&obj->opo_lock);
978                 if (oxe->oxe_ready) {
979                         if (!oxe->oxe_exist)
980                                 GOTO(unlock, rc = -ENODATA);
981
982                         if (buf->lb_buf == NULL)
983                                 GOTO(unlock, rc = oxe->oxe_vallen);
984
985                         if (buf->lb_len < oxe->oxe_vallen)
986                                 GOTO(unlock, rc = -ERANGE);
987
988                         memcpy(buf->lb_buf, oxe->oxe_value, oxe->oxe_vallen);
989
990                         GOTO(unlock, rc = oxe->oxe_vallen);
991
992 unlock:
993                         spin_unlock(&obj->opo_lock);
994                         osp_oac_xattr_put(oxe);
995
996                         return rc;
997                 }
998                 spin_unlock(&obj->opo_lock);
999         }
1000
1001         update = dt_update_request_create(dev);
1002         if (IS_ERR(update))
1003                 GOTO(out, rc = PTR_ERR(update));
1004
1005         rc = out_xattr_get_pack(env, &update->dur_buf,
1006                                 lu_object_fid(&dt->do_lu), name);
1007         if (rc != 0) {
1008                 CERROR("%s: Insert update error "DFID": rc = %d\n",
1009                        dname, PFID(lu_object_fid(&dt->do_lu)), rc);
1010                 GOTO(out, rc);
1011         }
1012
1013         rc = out_remote_sync(env, osp->opd_obd->u.cli.cl_import, update, &req);
1014         if (rc != 0) {
1015                 if (rc == -ENOENT) {
1016                         dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
1017                         obj->opo_non_exist = 1;
1018                 }
1019
1020                 if (obj->opo_ooa == NULL)
1021                         GOTO(out, rc);
1022
1023                 if (oxe == NULL)
1024                         oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
1025
1026                 if (oxe == NULL) {
1027                         CWARN("%s: Fail to add xattr (%s) to cache for "
1028                               DFID" (1): rc = %d\n", dname, name,
1029                               PFID(lu_object_fid(&dt->do_lu)), rc);
1030
1031                         GOTO(out, rc);
1032                 }
1033
1034                 spin_lock(&obj->opo_lock);
1035                 if (rc == -ENOENT || rc == -ENODATA) {
1036                         oxe->oxe_exist = 0;
1037                         oxe->oxe_ready = 1;
1038                 } else {
1039                         oxe->oxe_ready = 0;
1040                 }
1041                 spin_unlock(&obj->opo_lock);
1042
1043                 GOTO(out, rc);
1044         }
1045
1046         reply = req_capsule_server_sized_get(&req->rq_pill,
1047                                              &RMF_OUT_UPDATE_REPLY,
1048                                              OUT_UPDATE_REPLY_SIZE);
1049         if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
1050                 CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
1051                        dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
1052                        PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
1053
1054                 GOTO(out, rc = -EPROTO);
1055         }
1056
1057         rc = object_update_result_data_get(reply, rbuf, 0);
1058         if (rc < 0)
1059                 GOTO(out, rc);
1060
1061         if (buf->lb_buf == NULL)
1062                 GOTO(out, rc = rbuf->lb_len);
1063
1064         if (unlikely(buf->lb_len < rbuf->lb_len))
1065                 GOTO(out, rc = -ERANGE);
1066
1067         memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
1068         rc = rbuf->lb_len;
1069         if (obj->opo_ooa == NULL)
1070                 GOTO(out, rc);
1071
1072         if (oxe == NULL) {
1073                 oxe = osp_oac_xattr_find_or_add(obj, name, rbuf->lb_len);
1074                 if (oxe == NULL) {
1075                         CWARN("%s: Fail to add xattr (%s) to "
1076                               "cache for "DFID" (2): rc = %d\n",
1077                               dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1078
1079                         GOTO(out, rc);
1080                 }
1081         }
1082
1083         if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < rbuf->lb_len) {
1084                 struct osp_xattr_entry *old = oxe;
1085                 struct osp_xattr_entry *tmp;
1086
1087                 tmp = osp_oac_xattr_replace(obj, &old, rbuf->lb_len);
1088                 osp_oac_xattr_put(oxe);
1089                 oxe = tmp;
1090                 if (tmp == NULL) {
1091                         CWARN("%s: Fail to update xattr (%s) to "
1092                               "cache for "DFID": rc = %d\n",
1093                               dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
1094                         spin_lock(&obj->opo_lock);
1095                         old->oxe_ready = 0;
1096                         spin_unlock(&obj->opo_lock);
1097
1098                         GOTO(out, rc);
1099                 }
1100
1101                 /* Drop the ref for entry on list. */
1102                 osp_oac_xattr_put(old);
1103         }
1104
1105         spin_lock(&obj->opo_lock);
1106         oxe->oxe_vallen = rbuf->lb_len;
1107         memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
1108         oxe->oxe_exist = 1;
1109         oxe->oxe_ready = 1;
1110         spin_unlock(&obj->opo_lock);
1111
1112         GOTO(out, rc);
1113
1114 out:
1115         if (req != NULL)
1116                 ptlrpc_req_finished(req);
1117
1118         if (update != NULL && !IS_ERR(update))
1119                 dt_update_request_destroy(update);
1120
1121         if (oxe != NULL)
1122                 osp_oac_xattr_put(oxe);
1123
1124         return rc;
1125 }
1126
1127 static int __osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
1128                            const struct lu_buf *buf, const char *name,
1129                            int flag, struct thandle *th)
1130 {
1131         struct osp_object       *o = dt2osp_obj(dt);
1132         struct dt_update_request *update;
1133         struct osp_xattr_entry  *oxe;
1134         int                     rc;
1135         ENTRY;
1136
1137         LASSERT(buf->lb_len > 0 && buf->lb_buf != NULL);
1138
1139         update = dt_update_request_find_or_create(th, dt);
1140         if (IS_ERR(update)) {
1141                 CERROR("%s: Get OSP update buf failed "DFID": rc = %d\n",
1142                        dt->do_lu.lo_dev->ld_obd->obd_name,
1143                        PFID(lu_object_fid(&dt->do_lu)),
1144                        (int)PTR_ERR(update));
1145
1146                 RETURN(PTR_ERR(update));
1147         }
1148
1149         rc = out_xattr_set_pack(env, &update->dur_buf,
1150                                 lu_object_fid(&dt->do_lu),
1151                                 buf, name, flag, update->dur_batchid);
1152         if (rc != 0 || o->opo_ooa == NULL)
1153                 RETURN(rc);
1154
1155         oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
1156         if (oxe == NULL) {
1157                 CWARN("%s: Fail to add xattr (%s) to cache for "DFID,
1158                       dt->do_lu.lo_dev->ld_obd->obd_name,
1159                       name, PFID(lu_object_fid(&dt->do_lu)));
1160
1161                 RETURN(0);
1162         }
1163
1164         if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < buf->lb_len) {
1165                 struct osp_xattr_entry *old = oxe;
1166                 struct osp_xattr_entry *tmp;
1167
1168                 tmp = osp_oac_xattr_replace(o, &old, buf->lb_len);
1169                 osp_oac_xattr_put(oxe);
1170                 oxe = tmp;
1171                 if (tmp == NULL) {
1172                         CWARN("%s: Fail to update xattr (%s) to cache for "DFID,
1173                               dt->do_lu.lo_dev->ld_obd->obd_name,
1174                               name, PFID(lu_object_fid(&dt->do_lu)));
1175                         spin_lock(&o->opo_lock);
1176                         old->oxe_ready = 0;
1177                         spin_unlock(&o->opo_lock);
1178
1179                         RETURN(0);
1180                 }
1181
1182                 /* Drop the ref for entry on list. */
1183                 osp_oac_xattr_put(old);
1184         }
1185
1186         spin_lock(&o->opo_lock);
1187         oxe->oxe_vallen = buf->lb_len;
1188         memcpy(oxe->oxe_value, buf->lb_buf, buf->lb_len);
1189         oxe->oxe_exist = 1;
1190         oxe->oxe_ready = 1;
1191         spin_unlock(&o->opo_lock);
1192         osp_oac_xattr_put(oxe);
1193
1194         RETURN(0);
1195 }
1196
1197 /**
1198  * Implement OSP layer dt_object_operations::do_declare_xattr_set() interface.
1199  *
1200  * Declare that the caller will set extended attribute to the specified
1201  * MDT/OST object.
1202  *
1203  * This function will add an OUT_XATTR_SET sub-request to the per
1204  * OSP-transaction based request queue which will be flushed when
1205  * the transaction starts.
1206  *
1207  * \param[in] env       pointer to the thread context
1208  * \param[in] dt        pointer to the OSP layer dt_object
1209  * \param[in] buf       pointer to the lu_buf to hold the extended attribute
1210  * \param[in] name      the name of the extended attribute to be set
1211  * \param[in] flag      to indicate the detailed set operation: LU_XATTR_CREATE
1212  *                      or LU_XATTR_REPLACE or others
1213  * \param[in] th        pointer to the transaction handler
1214  *
1215  * \retval              0 for success
1216  * \retval              negative error number on failure
1217  */
1218 int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
1219                           const struct lu_buf *buf, const char *name,
1220                           int flag, struct thandle *th)
1221 {
1222         int rc = 0;
1223
1224         /* Please check the comment in osp_attr_set() for handling
1225          * remote transaction. */
1226         if (!is_only_remote_trans(th))
1227                 rc = __osp_xattr_set(env, dt, buf, name, flag, th);
1228
1229         return rc;
1230 }
1231
1232 /**
1233  * Implement OSP layer dt_object_operations::do_xattr_set() interface.
1234  *
1235  * Set extended attribute to the specified MDT/OST object.
1236  *
1237  * The real modification sub-request has been added in the declare phase
1238  * and related (OUT) RPC has been triggered when transaction start.
1239  *
1240  * If the OSP attributes cache is initialized, then check whether the name
1241  * extended attribute entry exists in the cache or not. If yes, replace it;
1242  * otherwise, add the extended attribute to the cache.
1243  *
1244  * \param[in] env       pointer to the thread context
1245  * \param[in] dt        pointer to the OSP layer dt_object
1246  * \param[in] buf       pointer to the lu_buf to hold the extended attribute
1247  * \param[in] name      the name of the extended attribute to be set
1248  * \param[in] fl        to indicate the detailed set operation: LU_XATTR_CREATE
1249  *                      or LU_XATTR_REPLACE or others
1250  * \param[in] th        pointer to the transaction handler
1251  * \param[in] capa      the capability for this operation
1252  *
1253  * \retval              0 for success
1254  * \retval              negative error number on failure
1255  */
1256 int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
1257                   const struct lu_buf *buf, const char *name, int fl,
1258                   struct thandle *th, struct lustre_capa *capa)
1259 {
1260         int rc = 0;
1261
1262         CDEBUG(D_INFO, "xattr %s set object "DFID"\n", name,
1263                PFID(&dt->do_lu.lo_header->loh_fid));
1264
1265         /* Please check the comment in osp_attr_set() for handling
1266          * remote transaction. */
1267         if (is_only_remote_trans(th))
1268                 rc = __osp_xattr_set(env, dt, buf, name, fl, th);
1269
1270         return rc;
1271 }
1272
1273 static int __osp_xattr_del(const struct lu_env *env, struct dt_object *dt,
1274                            const char *name, struct thandle *th)
1275 {
1276         struct dt_update_request *update;
1277         const struct lu_fid      *fid;
1278         struct osp_object        *o     = dt2osp_obj(dt);
1279         struct osp_xattr_entry   *oxe;
1280         int                       rc;
1281
1282         update = dt_update_request_find_or_create(th, dt);
1283         if (IS_ERR(update))
1284                 return PTR_ERR(update);
1285
1286         fid = lu_object_fid(&dt->do_lu);
1287
1288         rc = out_xattr_del_pack(env, &update->dur_buf, fid, name,
1289                                 update->dur_batchid);
1290
1291         if (rc != 0 || o->opo_ooa == NULL)
1292                 return rc;
1293
1294         oxe = osp_oac_xattr_find(o, name, true);
1295         if (oxe != NULL)
1296                 /* Drop the ref for entry on list. */
1297                 osp_oac_xattr_put(oxe);
1298
1299         return 0;
1300 }
1301
1302 /**
1303  * Implement OSP layer dt_object_operations::do_declare_xattr_del() interface.
1304  *
1305  * Declare that the caller will delete extended attribute on the specified
1306  * MDT/OST object.
1307  *
1308  * This function will add an OUT_XATTR_DEL sub-request to the per
1309  * OSP-transaction based request queue which will be flushed when
1310  * transaction start.
1311  *
1312  * \param[in] env       pointer to the thread context
1313  * \param[in] dt        pointer to the OSP layer dt_object
1314  * \param[in] name      the name of the extended attribute to be set
1315  * \param[in] th        pointer to the transaction handler
1316  *
1317  * \retval              0 for success
1318  * \retval              negative error number on failure
1319  */
1320 int osp_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
1321                           const char *name, struct thandle *th)
1322 {
1323         int rc = 0;
1324
1325         /* Please check the comment in osp_attr_set() for handling
1326          * remote transaction. */
1327         if (!is_only_remote_trans(th))
1328                 rc = __osp_xattr_del(env, dt, name, th);
1329
1330         return rc;
1331 }
1332
1333 /**
1334  * Implement OSP layer dt_object_operations::do_xattr_del() interface.
1335  *
1336  * Delete extended attribute on the specified MDT/OST object.
1337  *
1338  * The real modification sub-request has been added in the declare phase
1339  * and related (OUT) RPC has been triggered when transaction start.
1340  *
1341  * If the name extended attribute entry exists in the OSP attributes
1342  * cache, then remove it from the cache.
1343  *
1344  * \param[in] env       pointer to the thread context
1345  * \param[in] dt        pointer to the OSP layer dt_object
1346  * \param[in] name      the name of the extended attribute to be set
1347  * \param[in] th        pointer to the transaction handler
1348  * \param[in] capa      the capability for this operation
1349  *
1350  * \retval              0 for success
1351  * \retval              negative error number on failure
1352  */
1353 int osp_xattr_del(const struct lu_env *env, struct dt_object *dt,
1354                   const char *name, struct thandle *th,
1355                   struct lustre_capa *capa)
1356 {
1357         int rc = 0;
1358
1359         CDEBUG(D_INFO, "xattr %s del object "DFID"\n", name,
1360                PFID(&dt->do_lu.lo_header->loh_fid));
1361
1362         /* Please check the comment in osp_attr_set() for handling
1363          * remote transaction. */
1364         if (is_only_remote_trans(th))
1365                 rc = __osp_xattr_del(env, dt, name, th);
1366
1367         return rc;
1368 }
1369
1370 /**
1371  * Implement OSP layer dt_object_operations::do_declare_create() interface.
1372  *
1373  * Declare that the caller will create the OST object.
1374  *
1375  * If the transaction is a remote transaction (please refer to the
1376  * comment of osp_trans_create() for remote transaction), then the FID
1377  * for the OST object has been assigned already, and will be handled
1378  * as create (remote) MDT object via osp_md_declare_object_create().
1379  * This function is usually used for LFSCK to re-create the lost OST
1380  * object. Otherwise, if it is not replay case, the OSP will reserve
1381  * pre-created object for the subsequent create operation; if the MDT
1382  * side cached pre-created objects are less than some threshold, then
1383  * it will wakeup the pre-create thread.
1384  *
1385  * \param[in] env       pointer to the thread context
1386  * \param[in] dt        pointer to the OSP layer dt_object
1387  * \param[in] attr      the attribute for the object to be created
1388  * \param[in] hint      pointer to the hint for creating the object, such as
1389  *                      the parent object
1390  * \param[in] dof       pointer to the dt_object_format for help the creation
1391  * \param[in] th        pointer to the transaction handler
1392  *
1393  * \retval              0 for success
1394  * \retval              negative error number on failure
1395  */
1396 static int osp_declare_object_create(const struct lu_env *env,
1397                                      struct dt_object *dt,
1398                                      struct lu_attr *attr,
1399                                      struct dt_allocation_hint *hint,
1400                                      struct dt_object_format *dof,
1401                                      struct thandle *th)
1402 {
1403         struct osp_thread_info  *osi = osp_env_info(env);
1404         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
1405         struct osp_object       *o = dt2osp_obj(dt);
1406         const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
1407         int                      rc = 0;
1408
1409         ENTRY;
1410
1411         if (is_only_remote_trans(th)) {
1412                 LASSERT(fid_is_sane(fid));
1413
1414                 rc = osp_md_declare_object_create(env, dt, attr, hint, dof, th);
1415
1416                 RETURN(rc);
1417         }
1418
1419         /* should happen to non-0 OSP only so that at least one object
1420          * has been already declared in the scenario and LOD should
1421          * cleanup that */
1422         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
1423                 RETURN(-ENOSPC);
1424
1425         LASSERT(d->opd_last_used_oid_file);
1426
1427         /*
1428          * There can be gaps in precreated ids and record to unlink llog
1429          * XXX: we do not handle gaps yet, implemented before solution
1430          *      was found to be racy, so we disabled that. there is no
1431          *      point in making useless but expensive llog declaration.
1432          */
1433         /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
1434
1435         if (unlikely(!fid_is_zero(fid))) {
1436                 /* replay case: caller knows fid */
1437                 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1438                 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1439                 osi->osi_lb.lb_buf = NULL;
1440                 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1441                                              &osi->osi_lb, osi->osi_off, th);
1442                 RETURN(rc);
1443         }
1444
1445         /*
1446          * in declaration we need to reserve object so that we don't block
1447          * awaiting precreation RPC to complete
1448          */
1449         rc = osp_precreate_reserve(env, d);
1450         /*
1451          * we also need to declare update to local "last used id" file for
1452          * recovery if object isn't used for a reason, we need to release
1453          * reservation, this can be made in osd_object_release()
1454          */
1455         if (rc == 0) {
1456                 /* mark id is reserved: in create we don't want to talk
1457                  * to OST */
1458                 LASSERT(o->opo_reserved == 0);
1459                 o->opo_reserved = 1;
1460
1461                 /* common for all OSPs file hystorically */
1462                 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
1463                 osi->osi_lb.lb_len = sizeof(osi->osi_id);
1464                 osi->osi_lb.lb_buf = NULL;
1465                 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
1466                                              &osi->osi_lb, osi->osi_off, th);
1467         } else {
1468                 /* not needed in the cache anymore */
1469                 set_bit(LU_OBJECT_HEARD_BANSHEE,
1470                             &dt->do_lu.lo_header->loh_flags);
1471         }
1472         RETURN(rc);
1473 }
1474
1475 /**
1476  * Implement OSP layer dt_object_operations::do_create() interface.
1477  *
1478  * Create the OST object.
1479  *
1480  * For remote transaction case, the real create sub-request has been
1481  * added in the declare phase and related (OUT) RPC has been triggered
1482  * at transaction start. Here, like creating (remote) MDT object, the
1483  * OSP will mark the object existence via osp_md_object_create().
1484  *
1485  * For non-remote transaction case, the OSP will assign FID to the
1486  * object to be created, and update last_used Object ID (OID) file.
1487  *
1488  * \param[in] env       pointer to the thread context
1489  * \param[in] dt        pointer to the OSP layer dt_object
1490  * \param[in] attr      the attribute for the object to be created
1491  * \param[in] hint      pointer to the hint for creating the object, such as
1492  *                      the parent object
1493  * \param[in] dof       pointer to the dt_object_format for help the creation
1494  * \param[in] th        pointer to the transaction handler
1495  *
1496  * \retval              0 for success
1497  * \retval              negative error number on failure
1498  */
1499 static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
1500                              struct lu_attr *attr,
1501                              struct dt_allocation_hint *hint,
1502                              struct dt_object_format *dof, struct thandle *th)
1503 {
1504         struct osp_thread_info  *osi = osp_env_info(env);
1505         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
1506         struct osp_object       *o = dt2osp_obj(dt);
1507         int                     rc = 0;
1508         struct lu_fid           *fid = &osi->osi_fid;
1509         ENTRY;
1510
1511         if (is_only_remote_trans(th)) {
1512                 LASSERT(fid_is_sane(lu_object_fid(&dt->do_lu)));
1513
1514                 rc = osp_md_object_create(env, dt, attr, hint, dof, th);
1515                 if (rc == 0)
1516                         o->opo_non_exist = 0;
1517
1518                 RETURN(rc);
1519         }
1520
1521         o->opo_non_exist = 0;
1522         if (o->opo_reserved) {
1523                 /* regular case, fid is assigned holding transaction open */
1524                  osp_object_assign_fid(env, d, o);
1525         }
1526
1527         memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
1528
1529         LASSERTF(fid_is_sane(fid), "fid for osp_object %p is insane"DFID"!\n",
1530                  o, PFID(fid));
1531
1532         if (!o->opo_reserved) {
1533                 /* special case, id was assigned outside of transaction
1534                  * see comments in osp_declare_attr_set */
1535                 LASSERT(d->opd_pre != NULL);
1536                 spin_lock(&d->opd_pre_lock);
1537                 osp_update_last_fid(d, fid);
1538                 spin_unlock(&d->opd_pre_lock);
1539         }
1540
1541         CDEBUG(D_INODE, "fid for osp_object %p is "DFID"\n", o, PFID(fid));
1542
1543         /* If the precreate ends, it means it will be ready to rollover to
1544          * the new sequence soon, all the creation should be synchronized,
1545          * otherwise during replay, the replay fid will be inconsistent with
1546          * last_used/create fid */
1547         if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
1548                 th->th_sync = 1;
1549
1550         /*
1551          * it's OK if the import is inactive by this moment - id was created
1552          * by OST earlier, we just need to maintain it consistently on the disk
1553          * once import is reconnected, OSP will claim this and other objects
1554          * used and OST either keep them, if they exist or recreate
1555          */
1556
1557         /* we might have lost precreated objects */
1558         if (unlikely(d->opd_gap_count) > 0) {
1559                 LASSERT(d->opd_pre != NULL);
1560                 spin_lock(&d->opd_pre_lock);
1561                 if (d->opd_gap_count > 0) {
1562                         int count = d->opd_gap_count;
1563
1564                         ostid_set_id(&osi->osi_oi,
1565                                      fid_oid(&d->opd_gap_start_fid));
1566                         d->opd_gap_count = 0;
1567                         spin_unlock(&d->opd_pre_lock);
1568
1569                         CDEBUG(D_HA, "Writing gap "DFID"+%d in llog\n",
1570                                PFID(&d->opd_gap_start_fid), count);
1571                         /* real gap handling is disabled intil ORI-692 will be
1572                          * fixed, now we only report gaps */
1573                 } else {
1574                         spin_unlock(&d->opd_pre_lock);
1575                 }
1576         }
1577
1578         /* new object, the very first ->attr_set()
1579          * initializing attributes needs no logging */
1580         o->opo_new = 1;
1581
1582         /* Only need update last_used oid file, seq file will only be update
1583          * during seq rollover */
1584         osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
1585                            &d->opd_last_used_fid.f_oid, d->opd_index);
1586
1587         rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
1588                              &osi->osi_off, th);
1589
1590         CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
1591                d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
1592
1593         RETURN(rc);
1594 }
1595
1596 /**
1597  * Implement OSP layer dt_object_operations::do_declare_destroy() interface.
1598  *
1599  * Declare that the caller will destroy the specified OST object.
1600  *
1601  * The OST object destroy will be handled via llog asynchronously. This
1602  * function will declare the credits for generating MDS_UNLINK64_REC llog.
1603  *
1604  * \param[in] env       pointer to the thread context
1605  * \param[in] dt        pointer to the OSP layer dt_object to be destroyed
1606  * \param[in] th        pointer to the transaction handler
1607  *
1608  * \retval              0 for success
1609  * \retval              negative error number on failure
1610  */
1611 int osp_declare_object_destroy(const struct lu_env *env,
1612                                struct dt_object *dt, struct thandle *th)
1613 {
1614         struct osp_object       *o = dt2osp_obj(dt);
1615         int                      rc = 0;
1616
1617         ENTRY;
1618
1619         /*
1620          * track objects to be destroyed via llog
1621          */
1622         rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
1623
1624         RETURN(rc);
1625 }
1626
1627 /**
1628  * Implement OSP layer dt_object_operations::do_destroy() interface.
1629  *
1630  * Destroy the specified OST object.
1631  *
1632  * The OSP generates a MDS_UNLINK64_REC record in the llog. There
1633  * will be some dedicated thread to handle the llog asynchronously.
1634  *
1635  * It also marks the object as non-cached.
1636  *
1637  * \param[in] env       pointer to the thread context
1638  * \param[in] dt        pointer to the OSP layer dt_object to be destroyed
1639  * \param[in] th        pointer to the transaction handler
1640  *
1641  * \retval              0 for success
1642  * \retval              negative error number on failure
1643  */
1644 int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
1645                        struct thandle *th)
1646 {
1647         struct osp_object       *o = dt2osp_obj(dt);
1648         int                      rc = 0;
1649
1650         ENTRY;
1651
1652         o->opo_non_exist = 1;
1653         /*
1654          * once transaction is committed put proper command on
1655          * the queue going to our OST
1656          */
1657         rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
1658
1659         /* not needed in cache any more */
1660         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
1661
1662         RETURN(rc);
1663 }
1664
1665 static int osp_orphan_index_lookup(const struct lu_env *env,
1666                                    struct dt_object *dt,
1667                                    struct dt_rec *rec,
1668                                    const struct dt_key *key,
1669                                    struct lustre_capa *capa)
1670 {
1671         return -EOPNOTSUPP;
1672 }
1673
1674 static int osp_orphan_index_declare_insert(const struct lu_env *env,
1675                                            struct dt_object *dt,
1676                                            const struct dt_rec *rec,
1677                                            const struct dt_key *key,
1678                                            struct thandle *handle)
1679 {
1680         return -EOPNOTSUPP;
1681 }
1682
1683 static int osp_orphan_index_insert(const struct lu_env *env,
1684                                    struct dt_object *dt,
1685                                    const struct dt_rec *rec,
1686                                    const struct dt_key *key,
1687                                    struct thandle *handle,
1688                                    struct lustre_capa *capa,
1689                                    int ignore_quota)
1690 {
1691         return -EOPNOTSUPP;
1692 }
1693
1694 static int osp_orphan_index_declare_delete(const struct lu_env *env,
1695                                            struct dt_object *dt,
1696                                            const struct dt_key *key,
1697                                            struct thandle *handle)
1698 {
1699         return -EOPNOTSUPP;
1700 }
1701
1702 static int osp_orphan_index_delete(const struct lu_env *env,
1703                                    struct dt_object *dt,
1704                                    const struct dt_key *key,
1705                                    struct thandle *handle,
1706                                    struct lustre_capa *capa)
1707 {
1708         return -EOPNOTSUPP;
1709 }
1710
1711 /**
1712  * Initialize the OSP layer index iteration.
1713  *
1714  * \param[in] env       pointer to the thread context
1715  * \param[in] dt        pointer to the index object to be iterated
1716  * \param[in] attr      unused
1717  * \param[in] capa      the capability for this operation
1718  *
1719  * \retval              pointer to the iteration structure
1720  * \retval              negative error number on failure
1721  */
1722 struct dt_it *osp_it_init(const struct lu_env *env, struct dt_object *dt,
1723                           __u32 attr, struct lustre_capa *capa)
1724 {
1725         struct osp_it *it;
1726
1727         OBD_ALLOC_PTR(it);
1728         if (it == NULL)
1729                 return ERR_PTR(-ENOMEM);
1730
1731         it->ooi_pos_ent = -1;
1732         it->ooi_obj = dt;
1733
1734         return (struct dt_it *)it;
1735 }
1736
1737 /**
1738  * Finalize the OSP layer index iteration.
1739  *
1740  * \param[in] env       pointer to the thread context
1741  * \param[in] di        pointer to the iteration structure
1742  */
1743 void osp_it_fini(const struct lu_env *env, struct dt_it *di)
1744 {
1745         struct osp_it   *it = (struct osp_it *)di;
1746         struct page     **pages = it->ooi_pages;
1747         int             npages = it->ooi_total_npages;
1748         int             i;
1749
1750         if (pages != NULL) {
1751                 for (i = 0; i < npages; i++) {
1752                         if (pages[i] != NULL) {
1753                                 if (pages[i] == it->ooi_cur_page) {
1754                                         kunmap(pages[i]);
1755                                         it->ooi_cur_page = NULL;
1756                                 }
1757                                 __free_page(pages[i]);
1758                         }
1759                 }
1760                 OBD_FREE(pages, npages * sizeof(*pages));
1761         }
1762         OBD_FREE_PTR(it);
1763 }
1764
1765 /**
1766  * Get more records for the iteration from peer.
1767  *
1768  * The new records will be filled in an array of pages. The OSP side
1769  * allows 1MB bulk data to be transfered.
1770  *
1771  * \param[in] env       pointer to the thread context
1772  * \param[in] it        pointer to the iteration structure
1773  *
1774  * \retval              0 for success
1775  * \retval              negative error number on failure
1776  */
1777 static int osp_it_fetch(const struct lu_env *env, struct osp_it *it)
1778 {
1779         struct lu_device         *dev   = it->ooi_obj->do_lu.lo_dev;
1780         struct osp_device        *osp   = lu2osp_dev(dev);
1781         struct page             **pages;
1782         struct ptlrpc_request    *req   = NULL;
1783         struct ptlrpc_bulk_desc  *desc;
1784         struct idx_info          *ii;
1785         int                       npages;
1786         int                       rc;
1787         int                       i;
1788         ENTRY;
1789
1790         /* 1MB bulk */
1791         npages = min_t(unsigned int, OFD_MAX_BRW_SIZE, 1 << 20);
1792         npages /= PAGE_CACHE_SIZE;
1793
1794         OBD_ALLOC(pages, npages * sizeof(*pages));
1795         if (pages == NULL)
1796                 RETURN(-ENOMEM);
1797
1798         it->ooi_pages = pages;
1799         it->ooi_total_npages = npages;
1800         for (i = 0; i < npages; i++) {
1801                 pages[i] = alloc_page(GFP_IOFS);
1802                 if (pages[i] == NULL)
1803                         RETURN(-ENOMEM);
1804         }
1805
1806         req = ptlrpc_request_alloc(osp->opd_obd->u.cli.cl_import,
1807                                    &RQF_OBD_IDX_READ);
1808         if (req == NULL)
1809                 RETURN(-ENOMEM);
1810
1811         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, OBD_IDX_READ);
1812         if (rc != 0) {
1813                 ptlrpc_request_free(req);
1814                 RETURN(rc);
1815         }
1816
1817         req->rq_request_portal = OUT_PORTAL;
1818         ii = req_capsule_client_get(&req->rq_pill, &RMF_IDX_INFO);
1819         memset(ii, 0, sizeof(*ii));
1820         if (fid_is_last_id(lu_object_fid(&it->ooi_obj->do_lu))) {
1821                 /* LFSCK will iterate orphan object[FID_SEQ_LAYOUT_BTREE,
1822                  * ost_index, 0] with LAST_ID FID, so it needs to replace
1823                  * the FID with orphan FID here */
1824                 ii->ii_fid.f_seq = FID_SEQ_LAYOUT_RBTREE;
1825                 ii->ii_fid.f_oid = osp->opd_index;
1826                 ii->ii_fid.f_ver = 0;
1827                 ii->ii_flags = II_FL_NOHASH;
1828         } else {
1829                 ii->ii_fid = *lu_object_fid(&it->ooi_obj->do_lu);
1830                 ii->ii_flags = II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
1831                                II_FL_VARREC;
1832         }
1833         ii->ii_magic = IDX_INFO_MAGIC;
1834         ii->ii_count = npages * LU_PAGE_COUNT;
1835         ii->ii_hash_start = it->ooi_next;
1836         ii->ii_attrs = osp_dev2node(osp);
1837
1838         ptlrpc_at_set_req_timeout(req);
1839
1840         desc = ptlrpc_prep_bulk_imp(req, npages, 1, BULK_PUT_SINK,
1841                                     MDS_BULK_PORTAL);
1842         if (desc == NULL) {
1843                 ptlrpc_request_free(req);
1844                 RETURN(-ENOMEM);
1845         }
1846
1847         for (i = 0; i < npages; i++)
1848                 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1849
1850         ptlrpc_request_set_replen(req);
1851         rc = ptlrpc_queue_wait(req);
1852         if (rc != 0)
1853                 GOTO(out, rc);
1854
1855         rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1856                                           req->rq_bulk->bd_nob_transferred);
1857         if (rc < 0)
1858                 GOTO(out, rc);
1859         rc = 0;
1860
1861         ii = req_capsule_server_get(&req->rq_pill, &RMF_IDX_INFO);
1862         if (ii->ii_magic != IDX_INFO_MAGIC)
1863                  GOTO(out, rc = -EPROTO);
1864
1865         npages = (ii->ii_count + LU_PAGE_COUNT - 1) >>
1866                  (PAGE_CACHE_SHIFT - LU_PAGE_SHIFT);
1867         if (npages > it->ooi_total_npages) {
1868                 CERROR("%s: returned more pages than expected, %u > %u\n",
1869                        osp->opd_obd->obd_name, npages, it->ooi_total_npages);
1870                 GOTO(out, rc = -EINVAL);
1871         }
1872
1873         it->ooi_valid_npages = npages;
1874         if (ptlrpc_rep_need_swab(req))
1875                 it->ooi_swab = 1;
1876
1877         it->ooi_next = ii->ii_hash_end;
1878
1879 out:
1880         ptlrpc_req_finished(req);
1881
1882         return rc;
1883 }
1884
1885 /**
1886  * Move the iteration cursor to the next lu_page.
1887  *
1888  * One system page (PAGE_SIZE) may contain multiple lu_page (4KB),
1889  * that depends on the LU_PAGE_COUNT. If it is not the last lu_page
1890  * in current system page, then move the iteration cursor to the next
1891  * lu_page in current system page. Otherwise, if there are more system
1892  * pages in the cache, then move the iteration cursor to the next system
1893  * page. If all the cached records (pages) have been iterated, then fetch
1894  * more records via osp_it_fetch().
1895  *
1896  * \param[in] env       pointer to the thread context
1897  * \param[in] di        pointer to the iteration structure
1898  *
1899  * \retval              0 for success
1900  * \retval              negative error number on failure
1901  */
1902 int osp_it_next_page(const struct lu_env *env, struct dt_it *di)
1903 {
1904         struct osp_it           *it = (struct osp_it *)di;
1905         struct lu_idxpage       *idxpage;
1906         struct page             **pages;
1907         int                     rc;
1908         int                     i;
1909         ENTRY;
1910
1911 again2:
1912         idxpage = it->ooi_cur_idxpage;
1913         if (idxpage != NULL) {
1914                 if (idxpage->lip_nr == 0)
1915                         RETURN(1);
1916
1917                 if (it->ooi_pos_ent < idxpage->lip_nr) {
1918                         CDEBUG(D_INFO, "ooi_pos %d nr %d\n",
1919                                (int)it->ooi_pos_ent, (int)idxpage->lip_nr);
1920                         RETURN(0);
1921                 }
1922                 it->ooi_cur_idxpage = NULL;
1923                 it->ooi_pos_lu_page++;
1924 again1:
1925                 if (it->ooi_pos_lu_page < LU_PAGE_COUNT) {
1926                         it->ooi_cur_idxpage = (void *)it->ooi_cur_page +
1927                                          LU_PAGE_SIZE * it->ooi_pos_lu_page;
1928                         if (it->ooi_swab)
1929                                 lustre_swab_lip_header(it->ooi_cur_idxpage);
1930                         if (it->ooi_cur_idxpage->lip_magic != LIP_MAGIC) {
1931                                 struct osp_device *osp =
1932                                         lu2osp_dev(it->ooi_obj->do_lu.lo_dev);
1933
1934                                 CERROR("%s: invalid magic (%x != %x) for page "
1935                                        "%d/%d while read layout orphan index\n",
1936                                        osp->opd_obd->obd_name,
1937                                        it->ooi_cur_idxpage->lip_magic,
1938                                        LIP_MAGIC, it->ooi_pos_page,
1939                                        it->ooi_pos_lu_page);
1940                                 /* Skip this lu_page next time. */
1941                                 it->ooi_pos_ent = idxpage->lip_nr - 1;
1942                                 RETURN(-EINVAL);
1943                         }
1944                         it->ooi_pos_ent = -1;
1945                         goto again2;
1946                 }
1947
1948                 kunmap(it->ooi_cur_page);
1949                 it->ooi_cur_page = NULL;
1950                 it->ooi_pos_page++;
1951
1952 again0:
1953                 pages = it->ooi_pages;
1954                 if (it->ooi_pos_page < it->ooi_valid_npages) {
1955                         it->ooi_cur_page = kmap(pages[it->ooi_pos_page]);
1956                         it->ooi_pos_lu_page = 0;
1957                         goto again1;
1958                 }
1959
1960                 for (i = 0; i < it->ooi_total_npages; i++) {
1961                         if (pages[i] != NULL)
1962                                 __free_page(pages[i]);
1963                 }
1964                 OBD_FREE(pages, it->ooi_total_npages * sizeof(*pages));
1965
1966                 it->ooi_pos_page = 0;
1967                 it->ooi_total_npages = 0;
1968                 it->ooi_valid_npages = 0;
1969                 it->ooi_swab = 0;
1970                 it->ooi_ent = NULL;
1971                 it->ooi_cur_page = NULL;
1972                 it->ooi_cur_idxpage = NULL;
1973                 it->ooi_pages = NULL;
1974         }
1975
1976         if (it->ooi_next == II_END_OFF)
1977                 RETURN(1);
1978
1979         rc = osp_it_fetch(env, it);
1980         if (rc == 0)
1981                 goto again0;
1982
1983         RETURN(rc);
1984 }
1985
1986 /**
1987  * Move the iteration cursor to the next record.
1988  *
1989  * If there are more records in the lu_page, then move the iteration
1990  * cursor to the next record directly. Otherwise, move the iteration
1991  * cursor to the record in the next lu_page via osp_it_next_page()
1992  *
1993  * \param[in] env       pointer to the thread context
1994  * \param[in] di        pointer to the iteration structure
1995  *
1996  * \retval              0 for success
1997  * \retval              negative error number on failure
1998  */
1999 int osp_orphan_it_next(const struct lu_env *env, struct dt_it *di)
2000 {
2001         struct osp_it           *it = (struct osp_it *)di;
2002         struct lu_idxpage       *idxpage;
2003         int                     rc;
2004         ENTRY;
2005
2006 again:
2007         idxpage = it->ooi_cur_idxpage;
2008         if (idxpage != NULL) {
2009                 if (idxpage->lip_nr == 0)
2010                         RETURN(1);
2011
2012                 it->ooi_pos_ent++;
2013                 if (it->ooi_pos_ent < idxpage->lip_nr) {
2014                         it->ooi_ent =
2015                                 (struct lu_orphan_ent *)idxpage->lip_entries +
2016                                                         it->ooi_pos_ent;
2017                         if (it->ooi_swab)
2018                                 lustre_swab_orphan_ent(it->ooi_ent);
2019                         RETURN(0);
2020                 }
2021         }
2022
2023         rc = osp_it_next_page(env, di);
2024         if (rc == 0)
2025                 goto again;
2026
2027         RETURN(rc);
2028 }
2029
2030 int osp_it_get(const struct lu_env *env, struct dt_it *di,
2031                const struct dt_key *key)
2032 {
2033         return 1;
2034 }
2035
2036 void osp_it_put(const struct lu_env *env, struct dt_it *di)
2037 {
2038 }
2039
2040 struct dt_key *osp_orphan_it_key(const struct lu_env *env,
2041                                  const struct dt_it *di)
2042 {
2043         struct osp_it   *it  = (struct osp_it *)di;
2044         struct lu_orphan_ent    *ent = (struct lu_orphan_ent *)it->ooi_ent;
2045
2046         if (likely(ent != NULL))
2047                 return (struct dt_key *)(&ent->loe_key);
2048
2049         return NULL;
2050 }
2051
2052 int osp_orphan_it_key_size(const struct lu_env *env, const struct dt_it *di)
2053 {
2054         return sizeof(struct lu_fid);
2055 }
2056
2057 int osp_orphan_it_rec(const struct lu_env *env, const struct dt_it *di,
2058                       struct dt_rec *rec, __u32 attr)
2059 {
2060         struct osp_it   *it  = (struct osp_it *)di;
2061         struct lu_orphan_ent    *ent = (struct lu_orphan_ent *)it->ooi_ent;
2062
2063         if (likely(ent != NULL)) {
2064                 *(struct lu_orphan_rec *)rec = ent->loe_rec;
2065                 return 0;
2066         }
2067
2068         return -EINVAL;
2069 }
2070
2071 __u64 osp_it_store(const struct lu_env *env, const struct dt_it *di)
2072 {
2073         struct osp_it   *it = (struct osp_it *)di;
2074
2075         return it->ooi_next;
2076 }
2077
2078 /**
2079  * Locate the iteration cursor to the specified position (cookie).
2080  *
2081  * \param[in] env       pointer to the thread context
2082  * \param[in] di        pointer to the iteration structure
2083  * \param[in] hash      the specified position
2084  *
2085  * \retval              positive number for locating to the exactly position
2086  *                      or the next
2087  * \retval              0 for arriving at the end of the iteration
2088  * \retval              negative error number on failure
2089  */
2090 int osp_orphan_it_load(const struct lu_env *env, const struct dt_it *di,
2091                        __u64 hash)
2092 {
2093         struct osp_it   *it     = (struct osp_it *)di;
2094         int              rc;
2095
2096         it->ooi_next = hash;
2097         rc = osp_orphan_it_next(env, (struct dt_it *)di);
2098         if (rc == 1)
2099                 return 0;
2100
2101         if (rc == 0)
2102                 return 1;
2103
2104         return rc;
2105 }
2106
2107 int osp_it_key_rec(const struct lu_env *env, const struct dt_it *di,
2108                    void *key_rec)
2109 {
2110         return 0;
2111 }
2112
2113 static const struct dt_index_operations osp_orphan_index_ops = {
2114         .dio_lookup             = osp_orphan_index_lookup,
2115         .dio_declare_insert     = osp_orphan_index_declare_insert,
2116         .dio_insert             = osp_orphan_index_insert,
2117         .dio_declare_delete     = osp_orphan_index_declare_delete,
2118         .dio_delete             = osp_orphan_index_delete,
2119         .dio_it = {
2120                 .init           = osp_it_init,
2121                 .fini           = osp_it_fini,
2122                 .next           = osp_orphan_it_next,
2123                 .get            = osp_it_get,
2124                 .put            = osp_it_put,
2125                 .key            = osp_orphan_it_key,
2126                 .key_size       = osp_orphan_it_key_size,
2127                 .rec            = osp_orphan_it_rec,
2128                 .store          = osp_it_store,
2129                 .load           = osp_orphan_it_load,
2130                 .key_rec        = osp_it_key_rec,
2131         }
2132 };
2133
2134 /**
2135  * Implement OSP layer dt_object_operations::do_index_try() interface.
2136  *
2137  * Negotiate the index type.
2138  *
2139  * If the target index is an IDIF object, then use osp_orphan_index_ops.
2140  * Otherwise, assign osp_md_index_ops to the dt_object::do_index_ops.
2141  * (\see lustre/include/lustre_fid.h for IDIF.)
2142  *
2143  * \param[in] env       pointer to the thread context
2144  * \param[in] dt        pointer to the OSP layer dt_object
2145  * \param[in] feat      unused
2146  *
2147  * \retval              0 for success
2148  */
2149 static int osp_index_try(const struct lu_env *env,
2150                          struct dt_object *dt,
2151                          const struct dt_index_features *feat)
2152 {
2153         const struct lu_fid *fid = lu_object_fid(&dt->do_lu);
2154
2155         if (fid_is_last_id(fid) && fid_is_idif(fid))
2156                 dt->do_index_ops = &osp_orphan_index_ops;
2157         else
2158                 dt->do_index_ops = &osp_md_index_ops;
2159         return 0;
2160 }
2161
2162 struct dt_object_operations osp_obj_ops = {
2163         .do_declare_attr_get    = osp_declare_attr_get,
2164         .do_attr_get            = osp_attr_get,
2165         .do_declare_attr_set    = osp_declare_attr_set,
2166         .do_attr_set            = osp_attr_set,
2167         .do_declare_xattr_get   = osp_declare_xattr_get,
2168         .do_xattr_get           = osp_xattr_get,
2169         .do_declare_xattr_set   = osp_declare_xattr_set,
2170         .do_xattr_set           = osp_xattr_set,
2171         .do_declare_create      = osp_declare_object_create,
2172         .do_create              = osp_object_create,
2173         .do_declare_destroy     = osp_declare_object_destroy,
2174         .do_destroy             = osp_object_destroy,
2175         .do_index_try           = osp_index_try,
2176 };
2177
2178 /**
2179  * Implement OSP layer lu_object_operations::loo_object_init() interface.
2180  *
2181  * Initialize the object.
2182  *
2183  * If it is a remote MDT object, then call do_attr_get() to fetch
2184  * the attribute from the peer.
2185  *
2186  * \param[in] env       pointer to the thread context
2187  * \param[in] o         pointer to the OSP layer lu_object
2188  * \param[in] conf      unused
2189  *
2190  * \retval              0 for success
2191  * \retval              negative error number on failure
2192  */
2193 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
2194                            const struct lu_object_conf *conf)
2195 {
2196         struct osp_object       *po = lu2osp_obj(o);
2197         int                     rc = 0;
2198         ENTRY;
2199
2200         spin_lock_init(&po->opo_lock);
2201         o->lo_header->loh_attr |= LOHA_REMOTE;
2202
2203         if (is_ost_obj(o)) {
2204                 po->opo_obj.do_ops = &osp_obj_ops;
2205         } else {
2206                 struct lu_attr *la = &osp_env_info(env)->osi_attr;
2207
2208                 po->opo_obj.do_ops = &osp_md_obj_ops;
2209                 po->opo_obj.do_body_ops = &osp_md_body_ops;
2210                 rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o),
2211                                                      la, NULL);
2212                 if (rc == 0)
2213                         o->lo_header->loh_attr |=
2214                                 LOHA_EXISTS | (la->la_mode & S_IFMT);
2215                 if (rc == -ENOENT) {
2216                         po->opo_non_exist = 1;
2217                         rc = 0;
2218                 }
2219                 init_rwsem(&po->opo_sem);
2220         }
2221         RETURN(rc);
2222 }
2223
2224 /**
2225  * Implement OSP layer lu_object_operations::loo_object_free() interface.
2226  *
2227  * Finalize the object.
2228  *
2229  * If the OSP object has attributes cache, then destroy the cache.
2230  * Free the object finally.
2231  *
2232  * \param[in] env       pointer to the thread context
2233  * \param[in] o         pointer to the OSP layer lu_object
2234  */
2235 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
2236 {
2237         struct osp_object       *obj = lu2osp_obj(o);
2238         struct lu_object_header *h = o->lo_header;
2239
2240         dt_object_fini(&obj->opo_obj);
2241         lu_object_header_fini(h);
2242         if (obj->opo_ooa != NULL) {
2243                 struct osp_xattr_entry *oxe;
2244                 struct osp_xattr_entry *tmp;
2245                 int                     count;
2246
2247                 list_for_each_entry_safe(oxe, tmp,
2248                                          &obj->opo_ooa->ooa_xattr_list,
2249                                          oxe_list) {
2250                         list_del(&oxe->oxe_list);
2251                         count = atomic_read(&oxe->oxe_ref);
2252                         LASSERTF(count == 1,
2253                                  "Still has %d users on the xattr entry %.*s\n",
2254                                  count-1, (int)oxe->oxe_namelen, oxe->oxe_buf);
2255
2256                         OBD_FREE(oxe, oxe->oxe_buflen);
2257                 }
2258                 OBD_FREE_PTR(obj->opo_ooa);
2259         }
2260         OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
2261 }
2262
2263 /**
2264  * Implement OSP layer lu_object_operations::loo_object_release() interface.
2265  *
2266  * Cleanup (not free) the object.
2267  *
2268  * If it is a reserved object but failed to be created, or it is an OST
2269  * object, then mark the object as non-cached.
2270  *
2271  * \param[in] env       pointer to the thread context
2272  * \param[in] o         pointer to the OSP layer lu_object
2273  */
2274 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
2275 {
2276         struct osp_object       *po = lu2osp_obj(o);
2277         struct osp_device       *d  = lu2osp_dev(o->lo_dev);
2278
2279         ENTRY;
2280
2281         /*
2282          * release reservation if object was declared but not created
2283          * this may require lu_object_put() in LOD
2284          */
2285         if (unlikely(po->opo_reserved)) {
2286                 LASSERT(d->opd_pre != NULL);
2287                 LASSERT(d->opd_pre_reserved > 0);
2288                 spin_lock(&d->opd_pre_lock);
2289                 d->opd_pre_reserved--;
2290                 spin_unlock(&d->opd_pre_lock);
2291
2292                 /* not needed in cache any more */
2293                 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2294         }
2295
2296         if (is_ost_obj(o))
2297                 /* XXX: Currently, NOT cache OST-object on MDT because:
2298                  *      1. it is not often accessed on MDT.
2299                  *      2. avoid up layer (such as LFSCK) to load too many
2300                  *         once-used OST-objects. */
2301                 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
2302
2303         EXIT;
2304 }
2305
2306 static int osp_object_print(const struct lu_env *env, void *cookie,
2307                             lu_printer_t p, const struct lu_object *l)
2308 {
2309         const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
2310
2311         return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
2312 }
2313
2314 static int osp_object_invariant(const struct lu_object *o)
2315 {
2316         LBUG();
2317 }
2318
2319 struct lu_object_operations osp_lu_obj_ops = {
2320         .loo_object_init        = osp_object_init,
2321         .loo_object_free        = osp_object_free,
2322         .loo_object_release     = osp_object_release,
2323         .loo_object_print       = osp_object_print,
2324         .loo_object_invariant   = osp_object_invariant
2325 };