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