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