]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/lustre/lustre/lclient/lcommon_cl.c
Merge remote-tracking branch 'staging/staging-next'
[karo-tx-linux.git] / drivers / staging / lustre / lustre / lclient / lcommon_cl.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * cl code shared between vvp and liblustre (and other Lustre clients in the
37  * future).
38  *
39  *   Author: Nikita Danilov <nikita.danilov@sun.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LLITE
43
44 # include <linux/libcfs/libcfs.h>
45 # include <linux/fs.h>
46 # include <linux/sched.h>
47 # include <linux/mm.h>
48 # include <linux/quotaops.h>
49 # include <linux/highmem.h>
50 # include <linux/pagemap.h>
51 # include <linux/rbtree.h>
52
53 #include <obd.h>
54 #include <obd_support.h>
55 #include <lustre_fid.h>
56 #include <lustre_lite.h>
57 #include <lustre_dlm.h>
58 #include <lustre_ver.h>
59 #include <lustre_mdc.h>
60 #include <cl_object.h>
61
62 #include <lclient.h>
63
64 #include "../llite/llite_internal.h"
65
66 static const struct cl_req_operations ccc_req_ops;
67
68 /*
69  * ccc_ prefix stands for "Common Client Code".
70  */
71
72 static struct kmem_cache *ccc_lock_kmem;
73 static struct kmem_cache *ccc_object_kmem;
74 static struct kmem_cache *ccc_thread_kmem;
75 static struct kmem_cache *ccc_session_kmem;
76 static struct kmem_cache *ccc_req_kmem;
77
78 static struct lu_kmem_descr ccc_caches[] = {
79         {
80                 .ckd_cache = &ccc_lock_kmem,
81                 .ckd_name  = "ccc_lock_kmem",
82                 .ckd_size  = sizeof(struct ccc_lock)
83         },
84         {
85                 .ckd_cache = &ccc_object_kmem,
86                 .ckd_name  = "ccc_object_kmem",
87                 .ckd_size  = sizeof(struct ccc_object)
88         },
89         {
90                 .ckd_cache = &ccc_thread_kmem,
91                 .ckd_name  = "ccc_thread_kmem",
92                 .ckd_size  = sizeof(struct ccc_thread_info),
93         },
94         {
95                 .ckd_cache = &ccc_session_kmem,
96                 .ckd_name  = "ccc_session_kmem",
97                 .ckd_size  = sizeof(struct ccc_session)
98         },
99         {
100                 .ckd_cache = &ccc_req_kmem,
101                 .ckd_name  = "ccc_req_kmem",
102                 .ckd_size  = sizeof(struct ccc_req)
103         },
104         {
105                 .ckd_cache = NULL
106         }
107 };
108
109 /*****************************************************************************
110  *
111  * Vvp device and device type functions.
112  *
113  */
114
115 void *ccc_key_init(const struct lu_context *ctx, struct lu_context_key *key)
116 {
117         struct ccc_thread_info *info;
118
119         OBD_SLAB_ALLOC_PTR_GFP(info, ccc_thread_kmem, GFP_NOFS);
120         if (info == NULL)
121                 info = ERR_PTR(-ENOMEM);
122         return info;
123 }
124
125 void ccc_key_fini(const struct lu_context *ctx,
126                          struct lu_context_key *key, void *data)
127 {
128         struct ccc_thread_info *info = data;
129         OBD_SLAB_FREE_PTR(info, ccc_thread_kmem);
130 }
131
132 void *ccc_session_key_init(const struct lu_context *ctx,
133                                   struct lu_context_key *key)
134 {
135         struct ccc_session *session;
136
137         OBD_SLAB_ALLOC_PTR_GFP(session, ccc_session_kmem, GFP_NOFS);
138         if (session == NULL)
139                 session = ERR_PTR(-ENOMEM);
140         return session;
141 }
142
143 void ccc_session_key_fini(const struct lu_context *ctx,
144                                  struct lu_context_key *key, void *data)
145 {
146         struct ccc_session *session = data;
147         OBD_SLAB_FREE_PTR(session, ccc_session_kmem);
148 }
149
150 struct lu_context_key ccc_key = {
151         .lct_tags = LCT_CL_THREAD,
152         .lct_init = ccc_key_init,
153         .lct_fini = ccc_key_fini
154 };
155
156 struct lu_context_key ccc_session_key = {
157         .lct_tags = LCT_SESSION,
158         .lct_init = ccc_session_key_init,
159         .lct_fini = ccc_session_key_fini
160 };
161
162
163 /* type constructor/destructor: ccc_type_{init,fini,start,stop}(). */
164 /* LU_TYPE_INIT_FINI(ccc, &ccc_key, &ccc_session_key); */
165
166 int ccc_device_init(const struct lu_env *env, struct lu_device *d,
167                            const char *name, struct lu_device *next)
168 {
169         struct ccc_device  *vdv;
170         int rc;
171
172         vdv = lu2ccc_dev(d);
173         vdv->cdv_next = lu2cl_dev(next);
174
175         LASSERT(d->ld_site != NULL && next->ld_type != NULL);
176         next->ld_site = d->ld_site;
177         rc = next->ld_type->ldt_ops->ldto_device_init(
178                         env, next, next->ld_type->ldt_name, NULL);
179         if (rc == 0) {
180                 lu_device_get(next);
181                 lu_ref_add(&next->ld_reference, "lu-stack", &lu_site_init);
182         }
183         return rc;
184 }
185
186 struct lu_device *ccc_device_fini(const struct lu_env *env,
187                                          struct lu_device *d)
188 {
189         return cl2lu_dev(lu2ccc_dev(d)->cdv_next);
190 }
191
192 struct lu_device *ccc_device_alloc(const struct lu_env *env,
193                                    struct lu_device_type *t,
194                                    struct lustre_cfg *cfg,
195                                    const struct lu_device_operations *luops,
196                                    const struct cl_device_operations *clops)
197 {
198         struct ccc_device *vdv;
199         struct lu_device  *lud;
200         struct cl_site    *site;
201         int rc;
202
203         OBD_ALLOC_PTR(vdv);
204         if (vdv == NULL)
205                 return ERR_PTR(-ENOMEM);
206
207         lud = &vdv->cdv_cl.cd_lu_dev;
208         cl_device_init(&vdv->cdv_cl, t);
209         ccc2lu_dev(vdv)->ld_ops = luops;
210         vdv->cdv_cl.cd_ops = clops;
211
212         OBD_ALLOC_PTR(site);
213         if (site != NULL) {
214                 rc = cl_site_init(site, &vdv->cdv_cl);
215                 if (rc == 0)
216                         rc = lu_site_init_finish(&site->cs_lu);
217                 else {
218                         LASSERT(lud->ld_site == NULL);
219                         CERROR("Cannot init lu_site, rc %d.\n", rc);
220                         OBD_FREE_PTR(site);
221                 }
222         } else
223                 rc = -ENOMEM;
224         if (rc != 0) {
225                 ccc_device_free(env, lud);
226                 lud = ERR_PTR(rc);
227         }
228         return lud;
229 }
230
231 struct lu_device *ccc_device_free(const struct lu_env *env,
232                                          struct lu_device *d)
233 {
234         struct ccc_device *vdv  = lu2ccc_dev(d);
235         struct cl_site    *site = lu2cl_site(d->ld_site);
236         struct lu_device  *next = cl2lu_dev(vdv->cdv_next);
237
238         if (d->ld_site != NULL) {
239                 cl_site_fini(site);
240                 OBD_FREE_PTR(site);
241         }
242         cl_device_fini(lu2cl_dev(d));
243         OBD_FREE_PTR(vdv);
244         return next;
245 }
246
247 int ccc_req_init(const struct lu_env *env, struct cl_device *dev,
248                         struct cl_req *req)
249 {
250         struct ccc_req *vrq;
251         int result;
252
253         OBD_SLAB_ALLOC_PTR_GFP(vrq, ccc_req_kmem, GFP_NOFS);
254         if (vrq != NULL) {
255                 cl_req_slice_add(req, &vrq->crq_cl, dev, &ccc_req_ops);
256                 result = 0;
257         } else
258                 result = -ENOMEM;
259         return result;
260 }
261
262 /**
263  * An `emergency' environment used by ccc_inode_fini() when cl_env_get()
264  * fails. Access to this environment is serialized by ccc_inode_fini_guard
265  * mutex.
266  */
267 static struct lu_env *ccc_inode_fini_env = NULL;
268
269 /**
270  * A mutex serializing calls to slp_inode_fini() under extreme memory
271  * pressure, when environments cannot be allocated.
272  */
273 static DEFINE_MUTEX(ccc_inode_fini_guard);
274 static int dummy_refcheck;
275
276 int ccc_global_init(struct lu_device_type *device_type)
277 {
278         int result;
279
280         result = lu_kmem_init(ccc_caches);
281         if (result)
282                 return result;
283
284         result = lu_device_type_init(device_type);
285         if (result)
286                 goto out_kmem;
287
288         ccc_inode_fini_env = cl_env_alloc(&dummy_refcheck,
289                                           LCT_REMEMBER|LCT_NOREF);
290         if (IS_ERR(ccc_inode_fini_env)) {
291                 result = PTR_ERR(ccc_inode_fini_env);
292                 goto out_device;
293         }
294
295         ccc_inode_fini_env->le_ctx.lc_cookie = 0x4;
296         return 0;
297 out_device:
298         lu_device_type_fini(device_type);
299 out_kmem:
300         lu_kmem_fini(ccc_caches);
301         return result;
302 }
303
304 void ccc_global_fini(struct lu_device_type *device_type)
305 {
306         if (ccc_inode_fini_env != NULL) {
307                 cl_env_put(ccc_inode_fini_env, &dummy_refcheck);
308                 ccc_inode_fini_env = NULL;
309         }
310         lu_device_type_fini(device_type);
311         lu_kmem_fini(ccc_caches);
312 }
313
314 /*****************************************************************************
315  *
316  * Object operations.
317  *
318  */
319
320 struct lu_object *ccc_object_alloc(const struct lu_env *env,
321                                    const struct lu_object_header *unused,
322                                    struct lu_device *dev,
323                                    const struct cl_object_operations *clops,
324                                    const struct lu_object_operations *luops)
325 {
326         struct ccc_object *vob;
327         struct lu_object  *obj;
328
329         OBD_SLAB_ALLOC_PTR_GFP(vob, ccc_object_kmem, GFP_NOFS);
330         if (vob != NULL) {
331                 struct cl_object_header *hdr;
332
333                 obj = ccc2lu(vob);
334                 hdr = &vob->cob_header;
335                 cl_object_header_init(hdr);
336                 lu_object_init(obj, &hdr->coh_lu, dev);
337                 lu_object_add_top(&hdr->coh_lu, obj);
338
339                 vob->cob_cl.co_ops = clops;
340                 obj->lo_ops = luops;
341         } else
342                 obj = NULL;
343         return obj;
344 }
345
346 int ccc_object_init0(const struct lu_env *env,
347                             struct ccc_object *vob,
348                             const struct cl_object_conf *conf)
349 {
350         vob->cob_inode = conf->coc_inode;
351         vob->cob_transient_pages = 0;
352         cl_object_page_init(&vob->cob_cl, sizeof(struct ccc_page));
353         return 0;
354 }
355
356 int ccc_object_init(const struct lu_env *env, struct lu_object *obj,
357                            const struct lu_object_conf *conf)
358 {
359         struct ccc_device *dev = lu2ccc_dev(obj->lo_dev);
360         struct ccc_object *vob = lu2ccc(obj);
361         struct lu_object  *below;
362         struct lu_device  *under;
363         int result;
364
365         under = &dev->cdv_next->cd_lu_dev;
366         below = under->ld_ops->ldo_object_alloc(env, obj->lo_header, under);
367         if (below != NULL) {
368                 const struct cl_object_conf *cconf;
369
370                 cconf = lu2cl_conf(conf);
371                 INIT_LIST_HEAD(&vob->cob_pending_list);
372                 lu_object_add(obj, below);
373                 result = ccc_object_init0(env, vob, cconf);
374         } else
375                 result = -ENOMEM;
376         return result;
377 }
378
379 void ccc_object_free(const struct lu_env *env, struct lu_object *obj)
380 {
381         struct ccc_object *vob = lu2ccc(obj);
382
383         lu_object_fini(obj);
384         lu_object_header_fini(obj->lo_header);
385         OBD_SLAB_FREE_PTR(vob, ccc_object_kmem);
386 }
387
388 int ccc_lock_init(const struct lu_env *env,
389                   struct cl_object *obj, struct cl_lock *lock,
390                   const struct cl_io *unused,
391                   const struct cl_lock_operations *lkops)
392 {
393         struct ccc_lock *clk;
394         int result;
395
396         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
397
398         OBD_SLAB_ALLOC_PTR_GFP(clk, ccc_lock_kmem, GFP_NOFS);
399         if (clk != NULL) {
400                 cl_lock_slice_add(lock, &clk->clk_cl, obj, lkops);
401                 result = 0;
402         } else
403                 result = -ENOMEM;
404         return result;
405 }
406
407 int ccc_attr_set(const struct lu_env *env, struct cl_object *obj,
408                  const struct cl_attr *attr, unsigned valid)
409 {
410         return 0;
411 }
412
413 int ccc_object_glimpse(const struct lu_env *env,
414                        const struct cl_object *obj, struct ost_lvb *lvb)
415 {
416         struct inode *inode = ccc_object_inode(obj);
417
418         lvb->lvb_mtime = cl_inode_mtime(inode);
419         lvb->lvb_atime = cl_inode_atime(inode);
420         lvb->lvb_ctime = cl_inode_ctime(inode);
421         /*
422          * LU-417: Add dirty pages block count lest i_blocks reports 0, some
423          * "cp" or "tar" on remote node may think it's a completely sparse file
424          * and skip it.
425          */
426         if (lvb->lvb_size > 0 && lvb->lvb_blocks == 0)
427                 lvb->lvb_blocks = dirty_cnt(inode);
428         return 0;
429 }
430
431
432
433 int ccc_conf_set(const struct lu_env *env, struct cl_object *obj,
434                         const struct cl_object_conf *conf)
435 {
436         /* TODO: destroy all pages attached to this object. */
437         return 0;
438 }
439
440 static void ccc_object_size_lock(struct cl_object *obj)
441 {
442         struct inode *inode = ccc_object_inode(obj);
443
444         cl_isize_lock(inode);
445         cl_object_attr_lock(obj);
446 }
447
448 static void ccc_object_size_unlock(struct cl_object *obj)
449 {
450         struct inode *inode = ccc_object_inode(obj);
451
452         cl_object_attr_unlock(obj);
453         cl_isize_unlock(inode);
454 }
455
456 /*****************************************************************************
457  *
458  * Page operations.
459  *
460  */
461
462 struct page *ccc_page_vmpage(const struct lu_env *env,
463                             const struct cl_page_slice *slice)
464 {
465         return cl2vm_page(slice);
466 }
467
468 int ccc_page_is_under_lock(const struct lu_env *env,
469                            const struct cl_page_slice *slice,
470                            struct cl_io *io)
471 {
472         struct ccc_io   *cio  = ccc_env_io(env);
473         struct cl_lock_descr *desc = &ccc_env_info(env)->cti_descr;
474         struct cl_page       *page = slice->cpl_page;
475
476         int result;
477
478         if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
479             io->ci_type == CIT_FAULT) {
480                 if (cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED)
481                         result = -EBUSY;
482                 else {
483                         desc->cld_start = page->cp_index;
484                         desc->cld_end   = page->cp_index;
485                         desc->cld_obj   = page->cp_obj;
486                         desc->cld_mode  = CLM_READ;
487                         result = cl_queue_match(&io->ci_lockset.cls_done,
488                                                 desc) ? -EBUSY : 0;
489                 }
490         } else
491                 result = 0;
492         return result;
493 }
494
495 int ccc_fail(const struct lu_env *env, const struct cl_page_slice *slice)
496 {
497         /*
498          * Cached read?
499          */
500         LBUG();
501         return 0;
502 }
503
504 void ccc_transient_page_verify(const struct cl_page *page)
505 {
506 }
507
508 int ccc_transient_page_own(const struct lu_env *env,
509                                    const struct cl_page_slice *slice,
510                                    struct cl_io *unused,
511                                    int nonblock)
512 {
513         ccc_transient_page_verify(slice->cpl_page);
514         return 0;
515 }
516
517 void ccc_transient_page_assume(const struct lu_env *env,
518                                       const struct cl_page_slice *slice,
519                                       struct cl_io *unused)
520 {
521         ccc_transient_page_verify(slice->cpl_page);
522 }
523
524 void ccc_transient_page_unassume(const struct lu_env *env,
525                                         const struct cl_page_slice *slice,
526                                         struct cl_io *unused)
527 {
528         ccc_transient_page_verify(slice->cpl_page);
529 }
530
531 void ccc_transient_page_disown(const struct lu_env *env,
532                                       const struct cl_page_slice *slice,
533                                       struct cl_io *unused)
534 {
535         ccc_transient_page_verify(slice->cpl_page);
536 }
537
538 void ccc_transient_page_discard(const struct lu_env *env,
539                                        const struct cl_page_slice *slice,
540                                        struct cl_io *unused)
541 {
542         struct cl_page *page = slice->cpl_page;
543
544         ccc_transient_page_verify(slice->cpl_page);
545
546         /*
547          * For transient pages, remove it from the radix tree.
548          */
549         cl_page_delete(env, page);
550 }
551
552 int ccc_transient_page_prep(const struct lu_env *env,
553                                    const struct cl_page_slice *slice,
554                                    struct cl_io *unused)
555 {
556         /* transient page should always be sent. */
557         return 0;
558 }
559
560 /*****************************************************************************
561  *
562  * Lock operations.
563  *
564  */
565
566 void ccc_lock_delete(const struct lu_env *env,
567                      const struct cl_lock_slice *slice)
568 {
569         CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
570 }
571
572 void ccc_lock_fini(const struct lu_env *env, struct cl_lock_slice *slice)
573 {
574         struct ccc_lock *clk = cl2ccc_lock(slice);
575         OBD_SLAB_FREE_PTR(clk, ccc_lock_kmem);
576 }
577
578 int ccc_lock_enqueue(const struct lu_env *env,
579                      const struct cl_lock_slice *slice,
580                      struct cl_io *unused, __u32 enqflags)
581 {
582         CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
583         return 0;
584 }
585
586 int ccc_lock_unuse(const struct lu_env *env, const struct cl_lock_slice *slice)
587 {
588         CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
589         return 0;
590 }
591
592 int ccc_lock_wait(const struct lu_env *env, const struct cl_lock_slice *slice)
593 {
594         CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
595         return 0;
596 }
597
598 /**
599  * Implementation of cl_lock_operations::clo_fits_into() methods for ccc
600  * layer. This function is executed every time io finds an existing lock in
601  * the lock cache while creating new lock. This function has to decide whether
602  * cached lock "fits" into io.
603  *
604  * \param slice lock to be checked
605  * \param io    IO that wants a lock.
606  *
607  * \see lov_lock_fits_into().
608  */
609 int ccc_lock_fits_into(const struct lu_env *env,
610                        const struct cl_lock_slice *slice,
611                        const struct cl_lock_descr *need,
612                        const struct cl_io *io)
613 {
614         const struct cl_lock       *lock  = slice->cls_lock;
615         const struct cl_lock_descr *descr = &lock->cll_descr;
616         const struct ccc_io     *cio   = ccc_env_io(env);
617         int                      result;
618
619         /*
620          * Work around DLM peculiarity: it assumes that glimpse
621          * (LDLM_FL_HAS_INTENT) lock is always LCK_PR, and returns reads lock
622          * when asked for LCK_PW lock with LDLM_FL_HAS_INTENT flag set. Make
623          * sure that glimpse doesn't get CLM_WRITE top-lock, so that it
624          * doesn't enqueue CLM_WRITE sub-locks.
625          */
626         if (cio->cui_glimpse)
627                 result = descr->cld_mode != CLM_WRITE;
628
629         /*
630          * Also, don't match incomplete write locks for read, otherwise read
631          * would enqueue missing sub-locks in the write mode.
632          */
633         else if (need->cld_mode != descr->cld_mode)
634                 result = lock->cll_state >= CLS_ENQUEUED;
635         else
636                 result = 1;
637         return result;
638 }
639
640 /**
641  * Implements cl_lock_operations::clo_state() method for ccc layer, invoked
642  * whenever lock state changes. Transfers object attributes, that might be
643  * updated as a result of lock acquiring into inode.
644  */
645 void ccc_lock_state(const struct lu_env *env,
646                     const struct cl_lock_slice *slice,
647                     enum cl_lock_state state)
648 {
649         struct cl_lock *lock = slice->cls_lock;
650
651         /*
652          * Refresh inode attributes when the lock is moving into CLS_HELD
653          * state, and only when this is a result of real enqueue, rather than
654          * of finding lock in the cache.
655          */
656         if (state == CLS_HELD && lock->cll_state < CLS_HELD) {
657                 struct cl_object *obj;
658                 struct inode     *inode;
659
660                 obj   = slice->cls_obj;
661                 inode = ccc_object_inode(obj);
662
663                 /* vmtruncate() sets the i_size
664                  * under both a DLM lock and the
665                  * ll_inode_size_lock().  If we don't get the
666                  * ll_inode_size_lock() here we can match the DLM lock and
667                  * reset i_size.  generic_file_write can then trust the
668                  * stale i_size when doing appending writes and effectively
669                  * cancel the result of the truncate.  Getting the
670                  * ll_inode_size_lock() after the enqueue maintains the DLM
671                  * -> ll_inode_size_lock() acquiring order. */
672                 if (lock->cll_descr.cld_start == 0 &&
673                     lock->cll_descr.cld_end == CL_PAGE_EOF)
674                         cl_merge_lvb(env, inode);
675         }
676 }
677
678 /*****************************************************************************
679  *
680  * io operations.
681  *
682  */
683
684 void ccc_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
685 {
686         struct cl_io *io = ios->cis_io;
687
688         CLOBINVRNT(env, io->ci_obj, ccc_object_invariant(io->ci_obj));
689 }
690
691 int ccc_io_one_lock_index(const struct lu_env *env, struct cl_io *io,
692                           __u32 enqflags, enum cl_lock_mode mode,
693                           pgoff_t start, pgoff_t end)
694 {
695         struct ccc_io     *cio   = ccc_env_io(env);
696         struct cl_lock_descr   *descr = &cio->cui_link.cill_descr;
697         struct cl_object       *obj   = io->ci_obj;
698
699         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
700
701         CDEBUG(D_VFSTRACE, "lock: %d [%lu, %lu]\n", mode, start, end);
702
703         memset(&cio->cui_link, 0, sizeof(cio->cui_link));
704
705         if (cio->cui_fd && (cio->cui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
706                 descr->cld_mode = CLM_GROUP;
707                 descr->cld_gid  = cio->cui_fd->fd_grouplock.cg_gid;
708         } else {
709                 descr->cld_mode  = mode;
710         }
711         descr->cld_obj   = obj;
712         descr->cld_start = start;
713         descr->cld_end   = end;
714         descr->cld_enq_flags = enqflags;
715
716         cl_io_lock_add(env, io, &cio->cui_link);
717         return 0;
718 }
719
720 void ccc_io_update_iov(const struct lu_env *env,
721                        struct ccc_io *cio, struct cl_io *io)
722 {
723         size_t size = io->u.ci_rw.crw_count;
724
725         if (!cl_is_normalio(env, io) || cio->cui_iter == NULL)
726                 return;
727
728         iov_iter_truncate(cio->cui_iter, size);
729 }
730
731 int ccc_io_one_lock(const struct lu_env *env, struct cl_io *io,
732                     __u32 enqflags, enum cl_lock_mode mode,
733                     loff_t start, loff_t end)
734 {
735         struct cl_object *obj = io->ci_obj;
736         return ccc_io_one_lock_index(env, io, enqflags, mode,
737                                      cl_index(obj, start), cl_index(obj, end));
738 }
739
740 void ccc_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
741 {
742         CLOBINVRNT(env, ios->cis_io->ci_obj,
743                    ccc_object_invariant(ios->cis_io->ci_obj));
744 }
745
746 void ccc_io_advance(const struct lu_env *env,
747                     const struct cl_io_slice *ios,
748                     size_t nob)
749 {
750         struct ccc_io    *cio = cl2ccc_io(env, ios);
751         struct cl_io     *io  = ios->cis_io;
752         struct cl_object *obj = ios->cis_io->ci_obj;
753
754         CLOBINVRNT(env, obj, ccc_object_invariant(obj));
755
756         if (!cl_is_normalio(env, io))
757                 return;
758
759         iov_iter_reexpand(cio->cui_iter, cio->cui_tot_count  -= nob);
760 }
761
762 /**
763  * Helper function that if necessary adjusts file size (inode->i_size), when
764  * position at the offset \a pos is accessed. File size can be arbitrary stale
765  * on a Lustre client, but client at least knows KMS. If accessed area is
766  * inside [0, KMS], set file size to KMS, otherwise glimpse file size.
767  *
768  * Locking: cl_isize_lock is used to serialize changes to inode size and to
769  * protect consistency between inode size and cl_object
770  * attributes. cl_object_size_lock() protects consistency between cl_attr's of
771  * top-object and sub-objects.
772  */
773 int ccc_prep_size(const struct lu_env *env, struct cl_object *obj,
774                   struct cl_io *io, loff_t start, size_t count, int *exceed)
775 {
776         struct cl_attr *attr  = ccc_env_thread_attr(env);
777         struct inode   *inode = ccc_object_inode(obj);
778         loff_t    pos   = start + count - 1;
779         loff_t kms;
780         int result;
781
782         /*
783          * Consistency guarantees: following possibilities exist for the
784          * relation between region being accessed and real file size at this
785          * moment:
786          *
787          *  (A): the region is completely inside of the file;
788          *
789          *  (B-x): x bytes of region are inside of the file, the rest is
790          *  outside;
791          *
792          *  (C): the region is completely outside of the file.
793          *
794          * This classification is stable under DLM lock already acquired by
795          * the caller, because to change the class, other client has to take
796          * DLM lock conflicting with our lock. Also, any updates to ->i_size
797          * by other threads on this client are serialized by
798          * ll_inode_size_lock(). This guarantees that short reads are handled
799          * correctly in the face of concurrent writes and truncates.
800          */
801         ccc_object_size_lock(obj);
802         result = cl_object_attr_get(env, obj, attr);
803         if (result == 0) {
804                 kms = attr->cat_kms;
805                 if (pos > kms) {
806                         /*
807                          * A glimpse is necessary to determine whether we
808                          * return a short read (B) or some zeroes at the end
809                          * of the buffer (C)
810                          */
811                         ccc_object_size_unlock(obj);
812                         result = cl_glimpse_lock(env, io, inode, obj, 0);
813                         if (result == 0 && exceed != NULL) {
814                                 /* If objective page index exceed end-of-file
815                                  * page index, return directly. Do not expect
816                                  * kernel will check such case correctly.
817                                  * linux-2.6.18-128.1.1 miss to do that.
818                                  * --bug 17336 */
819                                 loff_t size = cl_isize_read(inode);
820                                 unsigned long cur_index = start >> PAGE_CACHE_SHIFT;
821
822                                 if ((size == 0 && cur_index != 0) ||
823                                     (((size - 1) >> PAGE_CACHE_SHIFT) < cur_index))
824                                 *exceed = 1;
825                         }
826                         return result;
827                 } else {
828                         /*
829                          * region is within kms and, hence, within real file
830                          * size (A). We need to increase i_size to cover the
831                          * read region so that generic_file_read() will do its
832                          * job, but that doesn't mean the kms size is
833                          * _correct_, it is only the _minimum_ size. If
834                          * someone does a stat they will get the correct size
835                          * which will always be >= the kms value here.
836                          * b=11081
837                          */
838                         if (cl_isize_read(inode) < kms) {
839                                 cl_isize_write_nolock(inode, kms);
840                                 CDEBUG(D_VFSTRACE,
841                                        DFID" updating i_size "LPU64"\n",
842                                        PFID(lu_object_fid(&obj->co_lu)),
843                                        (__u64)cl_isize_read(inode));
844
845                         }
846                 }
847         }
848         ccc_object_size_unlock(obj);
849         return result;
850 }
851
852 /*****************************************************************************
853  *
854  * Transfer operations.
855  *
856  */
857
858 void ccc_req_completion(const struct lu_env *env,
859                         const struct cl_req_slice *slice, int ioret)
860 {
861         struct ccc_req *vrq;
862
863         if (ioret > 0)
864                 cl_stats_tally(slice->crs_dev, slice->crs_req->crq_type, ioret);
865
866         vrq = cl2ccc_req(slice);
867         OBD_SLAB_FREE_PTR(vrq, ccc_req_kmem);
868 }
869
870 /**
871  * Implementation of struct cl_req_operations::cro_attr_set() for ccc
872  * layer. ccc is responsible for
873  *
874  *    - o_[mac]time
875  *
876  *    - o_mode
877  *
878  *    - o_parent_seq
879  *
880  *    - o_[ug]id
881  *
882  *    - o_parent_oid
883  *
884  *    - o_parent_ver
885  *
886  *    - o_ioepoch,
887  *
888  *  and capability.
889  */
890 void ccc_req_attr_set(const struct lu_env *env,
891                       const struct cl_req_slice *slice,
892                       const struct cl_object *obj,
893                       struct cl_req_attr *attr, obd_valid flags)
894 {
895         struct inode *inode;
896         struct obdo  *oa;
897         obd_flag      valid_flags;
898
899         oa = attr->cra_oa;
900         inode = ccc_object_inode(obj);
901         valid_flags = OBD_MD_FLTYPE;
902
903         if ((flags & OBD_MD_FLOSSCAPA) != 0) {
904                 LASSERT(attr->cra_capa == NULL);
905                 attr->cra_capa = cl_capa_lookup(inode,
906                                                 slice->crs_req->crq_type);
907         }
908
909         if (slice->crs_req->crq_type == CRT_WRITE) {
910                 if (flags & OBD_MD_FLEPOCH) {
911                         oa->o_valid |= OBD_MD_FLEPOCH;
912                         oa->o_ioepoch = cl_i2info(inode)->lli_ioepoch;
913                         valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME |
914                                        OBD_MD_FLUID | OBD_MD_FLGID;
915                 }
916         }
917         obdo_from_inode(oa, inode, valid_flags & flags);
918         obdo_set_parent_fid(oa, &cl_i2info(inode)->lli_fid);
919         memcpy(attr->cra_jobid, cl_i2info(inode)->lli_jobid,
920                JOBSTATS_JOBID_SIZE);
921 }
922
923 static const struct cl_req_operations ccc_req_ops = {
924         .cro_attr_set   = ccc_req_attr_set,
925         .cro_completion = ccc_req_completion
926 };
927
928 int cl_setattr_ost(struct inode *inode, const struct iattr *attr,
929                    struct obd_capa *capa)
930 {
931         struct lu_env *env;
932         struct cl_io  *io;
933         int         result;
934         int         refcheck;
935
936         env = cl_env_get(&refcheck);
937         if (IS_ERR(env))
938                 return PTR_ERR(env);
939
940         io = ccc_env_thread_io(env);
941         io->ci_obj = cl_i2info(inode)->lli_clob;
942
943         io->u.ci_setattr.sa_attr.lvb_atime = LTIME_S(attr->ia_atime);
944         io->u.ci_setattr.sa_attr.lvb_mtime = LTIME_S(attr->ia_mtime);
945         io->u.ci_setattr.sa_attr.lvb_ctime = LTIME_S(attr->ia_ctime);
946         io->u.ci_setattr.sa_attr.lvb_size = attr->ia_size;
947         io->u.ci_setattr.sa_valid = attr->ia_valid;
948         io->u.ci_setattr.sa_capa = capa;
949
950 again:
951         if (cl_io_init(env, io, CIT_SETATTR, io->ci_obj) == 0) {
952                 struct ccc_io *cio = ccc_env_io(env);
953
954                 if (attr->ia_valid & ATTR_FILE)
955                         /* populate the file descriptor for ftruncate to honor
956                          * group lock - see LU-787 */
957                         cio->cui_fd = cl_iattr2fd(inode, attr);
958
959                 result = cl_io_loop(env, io);
960         } else {
961                 result = io->ci_result;
962         }
963         cl_io_fini(env, io);
964         if (unlikely(io->ci_need_restart))
965                 goto again;
966         /* HSM import case: file is released, cannot be restored
967          * no need to fail except if restore registration failed
968          * with -ENODATA */
969         if (result == -ENODATA && io->ci_restore_needed &&
970             io->ci_result != -ENODATA)
971                 result = 0;
972         cl_env_put(env, &refcheck);
973         return result;
974 }
975
976 /*****************************************************************************
977  *
978  * Type conversions.
979  *
980  */
981
982 struct lu_device *ccc2lu_dev(struct ccc_device *vdv)
983 {
984         return &vdv->cdv_cl.cd_lu_dev;
985 }
986
987 struct ccc_device *lu2ccc_dev(const struct lu_device *d)
988 {
989         return container_of0(d, struct ccc_device, cdv_cl.cd_lu_dev);
990 }
991
992 struct ccc_device *cl2ccc_dev(const struct cl_device *d)
993 {
994         return container_of0(d, struct ccc_device, cdv_cl);
995 }
996
997 struct lu_object *ccc2lu(struct ccc_object *vob)
998 {
999         return &vob->cob_cl.co_lu;
1000 }
1001
1002 struct ccc_object *lu2ccc(const struct lu_object *obj)
1003 {
1004         return container_of0(obj, struct ccc_object, cob_cl.co_lu);
1005 }
1006
1007 struct ccc_object *cl2ccc(const struct cl_object *obj)
1008 {
1009         return container_of0(obj, struct ccc_object, cob_cl);
1010 }
1011
1012 struct ccc_lock *cl2ccc_lock(const struct cl_lock_slice *slice)
1013 {
1014         return container_of(slice, struct ccc_lock, clk_cl);
1015 }
1016
1017 struct ccc_io *cl2ccc_io(const struct lu_env *env,
1018                          const struct cl_io_slice *slice)
1019 {
1020         struct ccc_io *cio;
1021
1022         cio = container_of(slice, struct ccc_io, cui_cl);
1023         LASSERT(cio == ccc_env_io(env));
1024         return cio;
1025 }
1026
1027 struct ccc_req *cl2ccc_req(const struct cl_req_slice *slice)
1028 {
1029         return container_of0(slice, struct ccc_req, crq_cl);
1030 }
1031
1032 struct page *cl2vm_page(const struct cl_page_slice *slice)
1033 {
1034         return cl2ccc_page(slice)->cpg_page;
1035 }
1036
1037 /*****************************************************************************
1038  *
1039  * Accessors.
1040  *
1041  */
1042 int ccc_object_invariant(const struct cl_object *obj)
1043 {
1044         struct inode     *inode = ccc_object_inode(obj);
1045         struct cl_inode_info *lli   = cl_i2info(inode);
1046
1047         return (S_ISREG(cl_inode_mode(inode)) ||
1048                 /* i_mode of unlinked inode is zeroed. */
1049                 cl_inode_mode(inode) == 0) && lli->lli_clob == obj;
1050 }
1051
1052 struct inode *ccc_object_inode(const struct cl_object *obj)
1053 {
1054         return cl2ccc(obj)->cob_inode;
1055 }
1056
1057 /**
1058  * Returns a pointer to cl_page associated with \a vmpage, without acquiring
1059  * additional reference to the resulting page. This is an unsafe version of
1060  * cl_vmpage_page() that can only be used under vmpage lock.
1061  */
1062 struct cl_page *ccc_vmpage_page_transient(struct page *vmpage)
1063 {
1064         KLASSERT(PageLocked(vmpage));
1065         return (struct cl_page *)vmpage->private;
1066 }
1067
1068 /**
1069  * Initialize or update CLIO structures for regular files when new
1070  * meta-data arrives from the server.
1071  *
1072  * \param inode regular file inode
1073  * \param md    new file metadata from MDS
1074  * - allocates cl_object if necessary,
1075  * - updated layout, if object was already here.
1076  */
1077 int cl_file_inode_init(struct inode *inode, struct lustre_md *md)
1078 {
1079         struct lu_env   *env;
1080         struct cl_inode_info *lli;
1081         struct cl_object     *clob;
1082         struct lu_site       *site;
1083         struct lu_fid   *fid;
1084         struct cl_object_conf conf = {
1085                 .coc_inode = inode,
1086                 .u = {
1087                         .coc_md    = md
1088                 }
1089         };
1090         int result = 0;
1091         int refcheck;
1092
1093         LASSERT(md->body->valid & OBD_MD_FLID);
1094         LASSERT(S_ISREG(cl_inode_mode(inode)));
1095
1096         env = cl_env_get(&refcheck);
1097         if (IS_ERR(env))
1098                 return PTR_ERR(env);
1099
1100         site = cl_i2sbi(inode)->ll_site;
1101         lli  = cl_i2info(inode);
1102         fid  = &lli->lli_fid;
1103         LASSERT(fid_is_sane(fid));
1104
1105         if (lli->lli_clob == NULL) {
1106                 /* clob is slave of inode, empty lli_clob means for new inode,
1107                  * there is no clob in cache with the given fid, so it is
1108                  * unnecessary to perform lookup-alloc-lookup-insert, just
1109                  * alloc and insert directly. */
1110                 LASSERT(inode->i_state & I_NEW);
1111                 conf.coc_lu.loc_flags = LOC_F_NEW;
1112                 clob = cl_object_find(env, lu2cl_dev(site->ls_top_dev),
1113                                       fid, &conf);
1114                 if (!IS_ERR(clob)) {
1115                         /*
1116                          * No locking is necessary, as new inode is
1117                          * locked by I_NEW bit.
1118                          */
1119                         lli->lli_clob = clob;
1120                         lli->lli_has_smd = lsm_has_objects(md->lsm);
1121                         lu_object_ref_add(&clob->co_lu, "inode", inode);
1122                 } else
1123                         result = PTR_ERR(clob);
1124         } else {
1125                 result = cl_conf_set(env, lli->lli_clob, &conf);
1126         }
1127
1128         cl_env_put(env, &refcheck);
1129
1130         if (result != 0)
1131                 CERROR("Failure to initialize cl object "DFID": %d\n",
1132                        PFID(fid), result);
1133         return result;
1134 }
1135
1136 /**
1137  * Wait for others drop their references of the object at first, then we drop
1138  * the last one, which will lead to the object be destroyed immediately.
1139  * Must be called after cl_object_kill() against this object.
1140  *
1141  * The reason we want to do this is: destroying top object will wait for sub
1142  * objects being destroyed first, so we can't let bottom layer (e.g. from ASTs)
1143  * to initiate top object destroying which may deadlock. See bz22520.
1144  */
1145 static void cl_object_put_last(struct lu_env *env, struct cl_object *obj)
1146 {
1147         struct lu_object_header *header = obj->co_lu.lo_header;
1148         wait_queue_t       waiter;
1149
1150         if (unlikely(atomic_read(&header->loh_ref) != 1)) {
1151                 struct lu_site *site = obj->co_lu.lo_dev->ld_site;
1152                 struct lu_site_bkt_data *bkt;
1153
1154                 bkt = lu_site_bkt_from_fid(site, &header->loh_fid);
1155
1156                 init_waitqueue_entry(&waiter, current);
1157                 add_wait_queue(&bkt->lsb_marche_funebre, &waiter);
1158
1159                 while (1) {
1160                         set_current_state(TASK_UNINTERRUPTIBLE);
1161                         if (atomic_read(&header->loh_ref) == 1)
1162                                 break;
1163                         schedule();
1164                 }
1165
1166                 set_current_state(TASK_RUNNING);
1167                 remove_wait_queue(&bkt->lsb_marche_funebre, &waiter);
1168         }
1169
1170         cl_object_put(env, obj);
1171 }
1172
1173 void cl_inode_fini(struct inode *inode)
1174 {
1175         struct lu_env      *env;
1176         struct cl_inode_info    *lli  = cl_i2info(inode);
1177         struct cl_object        *clob = lli->lli_clob;
1178         int refcheck;
1179         int emergency;
1180
1181         if (clob != NULL) {
1182                 void                *cookie;
1183
1184                 cookie = cl_env_reenter();
1185                 env = cl_env_get(&refcheck);
1186                 emergency = IS_ERR(env);
1187                 if (emergency) {
1188                         mutex_lock(&ccc_inode_fini_guard);
1189                         LASSERT(ccc_inode_fini_env != NULL);
1190                         cl_env_implant(ccc_inode_fini_env, &refcheck);
1191                         env = ccc_inode_fini_env;
1192                 }
1193                 /*
1194                  * cl_object cache is a slave to inode cache (which, in turn
1195                  * is a slave to dentry cache), don't keep cl_object in memory
1196                  * when its master is evicted.
1197                  */
1198                 cl_object_kill(env, clob);
1199                 lu_object_ref_del(&clob->co_lu, "inode", inode);
1200                 cl_object_put_last(env, clob);
1201                 lli->lli_clob = NULL;
1202                 if (emergency) {
1203                         cl_env_unplant(ccc_inode_fini_env, &refcheck);
1204                         mutex_unlock(&ccc_inode_fini_guard);
1205                 } else
1206                         cl_env_put(env, &refcheck);
1207                 cl_env_reexit(cookie);
1208         }
1209 }
1210
1211 /**
1212  * return IF_* type for given lu_dirent entry.
1213  * IF_* flag shld be converted to particular OS file type in
1214  * platform llite module.
1215  */
1216 __u16 ll_dirent_type_get(struct lu_dirent *ent)
1217 {
1218         __u16 type = 0;
1219         struct luda_type *lt;
1220         int len = 0;
1221
1222         if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
1223                 const unsigned align = sizeof(struct luda_type) - 1;
1224
1225                 len = le16_to_cpu(ent->lde_namelen);
1226                 len = (len + align) & ~align;
1227                 lt = (void *)ent->lde_name + len;
1228                 type = IFTODT(le16_to_cpu(lt->lt_type));
1229         }
1230         return type;
1231 }
1232
1233 /**
1234  * build inode number from passed @fid */
1235 __u64 cl_fid_build_ino(const struct lu_fid *fid, int api32)
1236 {
1237         if (BITS_PER_LONG == 32 || api32)
1238                 return fid_flatten32(fid);
1239         else
1240                 return fid_flatten(fid);
1241 }
1242
1243 /**
1244  * build inode generation from passed @fid.  If our FID overflows the 32-bit
1245  * inode number then return a non-zero generation to distinguish them. */
1246 __u32 cl_fid_build_gen(const struct lu_fid *fid)
1247 {
1248         __u32 gen;
1249
1250         if (fid_is_igif(fid)) {
1251                 gen = lu_igif_gen(fid);
1252                 return gen;
1253         }
1254
1255         gen = (fid_flatten(fid) >> 32);
1256         return gen;
1257 }
1258
1259 /* lsm is unreliable after hsm implementation as layout can be changed at
1260  * any time. This is only to support old, non-clio-ized interfaces. It will
1261  * cause deadlock if clio operations are called with this extra layout refcount
1262  * because in case the layout changed during the IO, ll_layout_refresh() will
1263  * have to wait for the refcount to become zero to destroy the older layout.
1264  *
1265  * Notice that the lsm returned by this function may not be valid unless called
1266  * inside layout lock - MDS_INODELOCK_LAYOUT. */
1267 struct lov_stripe_md *ccc_inode_lsm_get(struct inode *inode)
1268 {
1269         return lov_lsm_get(cl_i2info(inode)->lli_clob);
1270 }
1271
1272 void inline ccc_inode_lsm_put(struct inode *inode, struct lov_stripe_md *lsm)
1273 {
1274         lov_lsm_put(cl_i2info(inode)->lli_clob, lsm);
1275 }