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