4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * cl code shared between vvp and liblustre (and other Lustre clients in the
39 * Author: Nikita Danilov <nikita.danilov@sun.com>
42 #define DEBUG_SUBSYSTEM S_LLITE
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>
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>
64 #include "../llite/llite_internal.h"
66 static const struct cl_req_operations ccc_req_ops;
69 * ccc_ prefix stands for "Common Client Code".
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;
78 static struct lu_kmem_descr ccc_caches[] = {
80 .ckd_cache = &ccc_lock_kmem,
81 .ckd_name = "ccc_lock_kmem",
82 .ckd_size = sizeof(struct ccc_lock)
85 .ckd_cache = &ccc_object_kmem,
86 .ckd_name = "ccc_object_kmem",
87 .ckd_size = sizeof(struct ccc_object)
90 .ckd_cache = &ccc_thread_kmem,
91 .ckd_name = "ccc_thread_kmem",
92 .ckd_size = sizeof(struct ccc_thread_info),
95 .ckd_cache = &ccc_session_kmem,
96 .ckd_name = "ccc_session_kmem",
97 .ckd_size = sizeof(struct ccc_session)
100 .ckd_cache = &ccc_req_kmem,
101 .ckd_name = "ccc_req_kmem",
102 .ckd_size = sizeof(struct ccc_req)
109 /*****************************************************************************
111 * Vvp device and device type functions.
115 void *ccc_key_init(const struct lu_context *ctx, struct lu_context_key *key)
117 struct ccc_thread_info *info;
119 OBD_SLAB_ALLOC_PTR_GFP(info, ccc_thread_kmem, GFP_NOFS);
121 info = ERR_PTR(-ENOMEM);
125 void ccc_key_fini(const struct lu_context *ctx,
126 struct lu_context_key *key, void *data)
128 struct ccc_thread_info *info = data;
129 OBD_SLAB_FREE_PTR(info, ccc_thread_kmem);
132 void *ccc_session_key_init(const struct lu_context *ctx,
133 struct lu_context_key *key)
135 struct ccc_session *session;
137 OBD_SLAB_ALLOC_PTR_GFP(session, ccc_session_kmem, GFP_NOFS);
139 session = ERR_PTR(-ENOMEM);
143 void ccc_session_key_fini(const struct lu_context *ctx,
144 struct lu_context_key *key, void *data)
146 struct ccc_session *session = data;
147 OBD_SLAB_FREE_PTR(session, ccc_session_kmem);
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
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
163 /* type constructor/destructor: ccc_type_{init,fini,start,stop}(). */
164 /* LU_TYPE_INIT_FINI(ccc, &ccc_key, &ccc_session_key); */
166 int ccc_device_init(const struct lu_env *env, struct lu_device *d,
167 const char *name, struct lu_device *next)
169 struct ccc_device *vdv;
173 vdv->cdv_next = lu2cl_dev(next);
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);
181 lu_ref_add(&next->ld_reference, "lu-stack", &lu_site_init);
186 struct lu_device *ccc_device_fini(const struct lu_env *env,
189 return cl2lu_dev(lu2ccc_dev(d)->cdv_next);
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)
198 struct ccc_device *vdv;
199 struct lu_device *lud;
200 struct cl_site *site;
205 return ERR_PTR(-ENOMEM);
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;
214 rc = cl_site_init(site, &vdv->cdv_cl);
216 rc = lu_site_init_finish(&site->cs_lu);
218 LASSERT(lud->ld_site == NULL);
219 CERROR("Cannot init lu_site, rc %d.\n", rc);
225 ccc_device_free(env, lud);
231 struct lu_device *ccc_device_free(const struct lu_env *env,
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);
238 if (d->ld_site != NULL) {
242 cl_device_fini(lu2cl_dev(d));
247 int ccc_req_init(const struct lu_env *env, struct cl_device *dev,
253 OBD_SLAB_ALLOC_PTR_GFP(vrq, ccc_req_kmem, GFP_NOFS);
255 cl_req_slice_add(req, &vrq->crq_cl, dev, &ccc_req_ops);
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
267 static struct lu_env *ccc_inode_fini_env = NULL;
270 * A mutex serializing calls to slp_inode_fini() under extreme memory
271 * pressure, when environments cannot be allocated.
273 static DEFINE_MUTEX(ccc_inode_fini_guard);
274 static int dummy_refcheck;
276 int ccc_global_init(struct lu_device_type *device_type)
280 result = lu_kmem_init(ccc_caches);
284 result = lu_device_type_init(device_type);
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);
295 ccc_inode_fini_env->le_ctx.lc_cookie = 0x4;
298 lu_device_type_fini(device_type);
300 lu_kmem_fini(ccc_caches);
304 void ccc_global_fini(struct lu_device_type *device_type)
306 if (ccc_inode_fini_env != NULL) {
307 cl_env_put(ccc_inode_fini_env, &dummy_refcheck);
308 ccc_inode_fini_env = NULL;
310 lu_device_type_fini(device_type);
311 lu_kmem_fini(ccc_caches);
314 /*****************************************************************************
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)
326 struct ccc_object *vob;
327 struct lu_object *obj;
329 OBD_SLAB_ALLOC_PTR_GFP(vob, ccc_object_kmem, GFP_NOFS);
331 struct cl_object_header *hdr;
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);
339 vob->cob_cl.co_ops = clops;
346 int ccc_object_init0(const struct lu_env *env,
347 struct ccc_object *vob,
348 const struct cl_object_conf *conf)
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));
356 int ccc_object_init(const struct lu_env *env, struct lu_object *obj,
357 const struct lu_object_conf *conf)
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;
365 under = &dev->cdv_next->cd_lu_dev;
366 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header, under);
368 const struct cl_object_conf *cconf;
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);
379 void ccc_object_free(const struct lu_env *env, struct lu_object *obj)
381 struct ccc_object *vob = lu2ccc(obj);
384 lu_object_header_fini(obj->lo_header);
385 OBD_SLAB_FREE_PTR(vob, ccc_object_kmem);
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)
393 struct ccc_lock *clk;
396 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
398 OBD_SLAB_ALLOC_PTR_GFP(clk, ccc_lock_kmem, GFP_NOFS);
400 cl_lock_slice_add(lock, &clk->clk_cl, obj, lkops);
407 int ccc_attr_set(const struct lu_env *env, struct cl_object *obj,
408 const struct cl_attr *attr, unsigned valid)
413 int ccc_object_glimpse(const struct lu_env *env,
414 const struct cl_object *obj, struct ost_lvb *lvb)
416 struct inode *inode = ccc_object_inode(obj);
418 lvb->lvb_mtime = cl_inode_mtime(inode);
419 lvb->lvb_atime = cl_inode_atime(inode);
420 lvb->lvb_ctime = cl_inode_ctime(inode);
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
426 if (lvb->lvb_size > 0 && lvb->lvb_blocks == 0)
427 lvb->lvb_blocks = dirty_cnt(inode);
433 int ccc_conf_set(const struct lu_env *env, struct cl_object *obj,
434 const struct cl_object_conf *conf)
436 /* TODO: destroy all pages attached to this object. */
440 static void ccc_object_size_lock(struct cl_object *obj)
442 struct inode *inode = ccc_object_inode(obj);
444 cl_isize_lock(inode);
445 cl_object_attr_lock(obj);
448 static void ccc_object_size_unlock(struct cl_object *obj)
450 struct inode *inode = ccc_object_inode(obj);
452 cl_object_attr_unlock(obj);
453 cl_isize_unlock(inode);
456 /*****************************************************************************
462 struct page *ccc_page_vmpage(const struct lu_env *env,
463 const struct cl_page_slice *slice)
465 return cl2vm_page(slice);
468 int ccc_page_is_under_lock(const struct lu_env *env,
469 const struct cl_page_slice *slice,
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;
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)
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,
495 int ccc_fail(const struct lu_env *env, const struct cl_page_slice *slice)
504 void ccc_transient_page_verify(const struct cl_page *page)
508 int ccc_transient_page_own(const struct lu_env *env,
509 const struct cl_page_slice *slice,
510 struct cl_io *unused,
513 ccc_transient_page_verify(slice->cpl_page);
517 void ccc_transient_page_assume(const struct lu_env *env,
518 const struct cl_page_slice *slice,
519 struct cl_io *unused)
521 ccc_transient_page_verify(slice->cpl_page);
524 void ccc_transient_page_unassume(const struct lu_env *env,
525 const struct cl_page_slice *slice,
526 struct cl_io *unused)
528 ccc_transient_page_verify(slice->cpl_page);
531 void ccc_transient_page_disown(const struct lu_env *env,
532 const struct cl_page_slice *slice,
533 struct cl_io *unused)
535 ccc_transient_page_verify(slice->cpl_page);
538 void ccc_transient_page_discard(const struct lu_env *env,
539 const struct cl_page_slice *slice,
540 struct cl_io *unused)
542 struct cl_page *page = slice->cpl_page;
544 ccc_transient_page_verify(slice->cpl_page);
547 * For transient pages, remove it from the radix tree.
549 cl_page_delete(env, page);
552 int ccc_transient_page_prep(const struct lu_env *env,
553 const struct cl_page_slice *slice,
554 struct cl_io *unused)
556 /* transient page should always be sent. */
560 /*****************************************************************************
566 void ccc_lock_delete(const struct lu_env *env,
567 const struct cl_lock_slice *slice)
569 CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
572 void ccc_lock_fini(const struct lu_env *env, struct cl_lock_slice *slice)
574 struct ccc_lock *clk = cl2ccc_lock(slice);
575 OBD_SLAB_FREE_PTR(clk, ccc_lock_kmem);
578 int ccc_lock_enqueue(const struct lu_env *env,
579 const struct cl_lock_slice *slice,
580 struct cl_io *unused, __u32 enqflags)
582 CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
586 int ccc_lock_unuse(const struct lu_env *env, const struct cl_lock_slice *slice)
588 CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
592 int ccc_lock_wait(const struct lu_env *env, const struct cl_lock_slice *slice)
594 CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
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.
604 * \param slice lock to be checked
605 * \param io IO that wants a lock.
607 * \see lov_lock_fits_into().
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)
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);
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.
626 if (cio->cui_glimpse)
627 result = descr->cld_mode != CLM_WRITE;
630 * Also, don't match incomplete write locks for read, otherwise read
631 * would enqueue missing sub-locks in the write mode.
633 else if (need->cld_mode != descr->cld_mode)
634 result = lock->cll_state >= CLS_ENQUEUED;
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.
645 void ccc_lock_state(const struct lu_env *env,
646 const struct cl_lock_slice *slice,
647 enum cl_lock_state state)
649 struct cl_lock *lock = slice->cls_lock;
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.
656 if (state == CLS_HELD && lock->cll_state < CLS_HELD) {
657 struct cl_object *obj;
660 obj = slice->cls_obj;
661 inode = ccc_object_inode(obj);
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);
678 /*****************************************************************************
684 void ccc_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
686 struct cl_io *io = ios->cis_io;
688 CLOBINVRNT(env, io->ci_obj, ccc_object_invariant(io->ci_obj));
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)
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;
699 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
701 CDEBUG(D_VFSTRACE, "lock: %d [%lu, %lu]\n", mode, start, end);
703 memset(&cio->cui_link, 0, sizeof(cio->cui_link));
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;
709 descr->cld_mode = mode;
711 descr->cld_obj = obj;
712 descr->cld_start = start;
713 descr->cld_end = end;
714 descr->cld_enq_flags = enqflags;
716 cl_io_lock_add(env, io, &cio->cui_link);
720 void ccc_io_update_iov(const struct lu_env *env,
721 struct ccc_io *cio, struct cl_io *io)
723 size_t size = io->u.ci_rw.crw_count;
725 if (!cl_is_normalio(env, io) || cio->cui_iter == NULL)
728 iov_iter_truncate(cio->cui_iter, size);
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)
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));
740 void ccc_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
742 CLOBINVRNT(env, ios->cis_io->ci_obj,
743 ccc_object_invariant(ios->cis_io->ci_obj));
746 void ccc_io_advance(const struct lu_env *env,
747 const struct cl_io_slice *ios,
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;
754 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
756 if (!cl_is_normalio(env, io))
759 iov_iter_reexpand(cio->cui_iter, cio->cui_tot_count -= nob);
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.
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.
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)
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;
783 * Consistency guarantees: following possibilities exist for the
784 * relation between region being accessed and real file size at this
787 * (A): the region is completely inside of the file;
789 * (B-x): x bytes of region are inside of the file, the rest is
792 * (C): the region is completely outside of the file.
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.
801 ccc_object_size_lock(obj);
802 result = cl_object_attr_get(env, obj, attr);
807 * A glimpse is necessary to determine whether we
808 * return a short read (B) or some zeroes at the end
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.
819 loff_t size = cl_isize_read(inode);
820 unsigned long cur_index = start >> PAGE_CACHE_SHIFT;
822 if ((size == 0 && cur_index != 0) ||
823 (((size - 1) >> PAGE_CACHE_SHIFT) < cur_index))
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.
838 if (cl_isize_read(inode) < kms) {
839 cl_isize_write_nolock(inode, kms);
841 DFID" updating i_size "LPU64"\n",
842 PFID(lu_object_fid(&obj->co_lu)),
843 (__u64)cl_isize_read(inode));
848 ccc_object_size_unlock(obj);
852 /*****************************************************************************
854 * Transfer operations.
858 void ccc_req_completion(const struct lu_env *env,
859 const struct cl_req_slice *slice, int ioret)
864 cl_stats_tally(slice->crs_dev, slice->crs_req->crq_type, ioret);
866 vrq = cl2ccc_req(slice);
867 OBD_SLAB_FREE_PTR(vrq, ccc_req_kmem);
871 * Implementation of struct cl_req_operations::cro_attr_set() for ccc
872 * layer. ccc is responsible for
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)
897 obd_flag valid_flags;
900 inode = ccc_object_inode(obj);
901 valid_flags = OBD_MD_FLTYPE;
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);
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;
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);
923 static const struct cl_req_operations ccc_req_ops = {
924 .cro_attr_set = ccc_req_attr_set,
925 .cro_completion = ccc_req_completion
928 int cl_setattr_ost(struct inode *inode, const struct iattr *attr,
929 struct obd_capa *capa)
936 env = cl_env_get(&refcheck);
940 io = ccc_env_thread_io(env);
941 io->ci_obj = cl_i2info(inode)->lli_clob;
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;
951 if (cl_io_init(env, io, CIT_SETATTR, io->ci_obj) == 0) {
952 struct ccc_io *cio = ccc_env_io(env);
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);
959 result = cl_io_loop(env, io);
961 result = io->ci_result;
964 if (unlikely(io->ci_need_restart))
966 /* HSM import case: file is released, cannot be restored
967 * no need to fail except if restore registration failed
969 if (result == -ENODATA && io->ci_restore_needed &&
970 io->ci_result != -ENODATA)
972 cl_env_put(env, &refcheck);
976 /*****************************************************************************
982 struct lu_device *ccc2lu_dev(struct ccc_device *vdv)
984 return &vdv->cdv_cl.cd_lu_dev;
987 struct ccc_device *lu2ccc_dev(const struct lu_device *d)
989 return container_of0(d, struct ccc_device, cdv_cl.cd_lu_dev);
992 struct ccc_device *cl2ccc_dev(const struct cl_device *d)
994 return container_of0(d, struct ccc_device, cdv_cl);
997 struct lu_object *ccc2lu(struct ccc_object *vob)
999 return &vob->cob_cl.co_lu;
1002 struct ccc_object *lu2ccc(const struct lu_object *obj)
1004 return container_of0(obj, struct ccc_object, cob_cl.co_lu);
1007 struct ccc_object *cl2ccc(const struct cl_object *obj)
1009 return container_of0(obj, struct ccc_object, cob_cl);
1012 struct ccc_lock *cl2ccc_lock(const struct cl_lock_slice *slice)
1014 return container_of(slice, struct ccc_lock, clk_cl);
1017 struct ccc_io *cl2ccc_io(const struct lu_env *env,
1018 const struct cl_io_slice *slice)
1022 cio = container_of(slice, struct ccc_io, cui_cl);
1023 LASSERT(cio == ccc_env_io(env));
1027 struct ccc_req *cl2ccc_req(const struct cl_req_slice *slice)
1029 return container_of0(slice, struct ccc_req, crq_cl);
1032 struct page *cl2vm_page(const struct cl_page_slice *slice)
1034 return cl2ccc_page(slice)->cpg_page;
1037 /*****************************************************************************
1042 int ccc_object_invariant(const struct cl_object *obj)
1044 struct inode *inode = ccc_object_inode(obj);
1045 struct cl_inode_info *lli = cl_i2info(inode);
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;
1052 struct inode *ccc_object_inode(const struct cl_object *obj)
1054 return cl2ccc(obj)->cob_inode;
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.
1062 struct cl_page *ccc_vmpage_page_transient(struct page *vmpage)
1064 KLASSERT(PageLocked(vmpage));
1065 return (struct cl_page *)vmpage->private;
1069 * Initialize or update CLIO structures for regular files when new
1070 * meta-data arrives from the server.
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.
1077 int cl_file_inode_init(struct inode *inode, struct lustre_md *md)
1080 struct cl_inode_info *lli;
1081 struct cl_object *clob;
1082 struct lu_site *site;
1084 struct cl_object_conf conf = {
1093 LASSERT(md->body->valid & OBD_MD_FLID);
1094 LASSERT(S_ISREG(cl_inode_mode(inode)));
1096 env = cl_env_get(&refcheck);
1098 return PTR_ERR(env);
1100 site = cl_i2sbi(inode)->ll_site;
1101 lli = cl_i2info(inode);
1102 fid = &lli->lli_fid;
1103 LASSERT(fid_is_sane(fid));
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),
1114 if (!IS_ERR(clob)) {
1116 * No locking is necessary, as new inode is
1117 * locked by I_NEW bit.
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);
1123 result = PTR_ERR(clob);
1125 result = cl_conf_set(env, lli->lli_clob, &conf);
1128 cl_env_put(env, &refcheck);
1131 CERROR("Failure to initialize cl object "DFID": %d\n",
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.
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.
1145 static void cl_object_put_last(struct lu_env *env, struct cl_object *obj)
1147 struct lu_object_header *header = obj->co_lu.lo_header;
1148 wait_queue_t waiter;
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;
1154 bkt = lu_site_bkt_from_fid(site, &header->loh_fid);
1156 init_waitqueue_entry(&waiter, current);
1157 add_wait_queue(&bkt->lsb_marche_funebre, &waiter);
1160 set_current_state(TASK_UNINTERRUPTIBLE);
1161 if (atomic_read(&header->loh_ref) == 1)
1166 set_current_state(TASK_RUNNING);
1167 remove_wait_queue(&bkt->lsb_marche_funebre, &waiter);
1170 cl_object_put(env, obj);
1173 void cl_inode_fini(struct inode *inode)
1176 struct cl_inode_info *lli = cl_i2info(inode);
1177 struct cl_object *clob = lli->lli_clob;
1184 cookie = cl_env_reenter();
1185 env = cl_env_get(&refcheck);
1186 emergency = IS_ERR(env);
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;
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.
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;
1203 cl_env_unplant(ccc_inode_fini_env, &refcheck);
1204 mutex_unlock(&ccc_inode_fini_guard);
1206 cl_env_put(env, &refcheck);
1207 cl_env_reexit(cookie);
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.
1216 __u16 ll_dirent_type_get(struct lu_dirent *ent)
1219 struct luda_type *lt;
1222 if (le32_to_cpu(ent->lde_attrs) & LUDA_TYPE) {
1223 const unsigned align = sizeof(struct luda_type) - 1;
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));
1234 * build inode number from passed @fid */
1235 __u64 cl_fid_build_ino(const struct lu_fid *fid, int api32)
1237 if (BITS_PER_LONG == 32 || api32)
1238 return fid_flatten32(fid);
1240 return fid_flatten(fid);
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)
1250 if (fid_is_igif(fid)) {
1251 gen = lu_igif_gen(fid);
1255 gen = (fid_flatten(fid) >> 32);
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.
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)
1269 return lov_lsm_get(cl_i2info(inode)->lli_clob);
1272 void inline ccc_inode_lsm_put(struct inode *inode, struct lov_stripe_md *lsm)
1274 lov_lsm_put(cl_i2info(inode)->lli_clob, lsm);