2 * Copyright (c) 2007 Cluster File Systems, Inc.
3 * Author: Nikita Danilov <nikita@clusterfs.com>
5 * This file is part of Lustre, http://www.lustre.org.
7 * Lustre is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU General Public
9 * License as published by the Free Software Foundation.
11 * Lustre is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Lustre; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Copyright (c) 2011, 2013, Intel Corporation.
23 #define DEBUG_SUBSYSTEM S_LLITE
29 #include <sys/types.h>
32 #include <sys/queue.h>
34 # include <sys/statvfs.h>
36 # include <sys/statfs.h>
39 #include <liblustre.h>
42 #include <obd_support.h>
43 #include <lustre_fid.h>
44 #include <lustre_lite.h>
45 #include <lustre_dlm.h>
46 #include <lustre_ver.h>
47 #include <lustre_mdc.h>
48 #include <cl_object.h>
50 #include "llite_lib.h"
53 * slp_ prefix stands for "Sysio Library Posix". It corresponds to historical
57 static int slp_type_init (struct lu_device_type *t);
58 static void slp_type_fini (struct lu_device_type *t);
60 static int slp_page_init(const struct lu_env *env, struct cl_object *obj,
61 struct cl_page *page, cfs_page_t *vmpage);
62 static int slp_attr_get (const struct lu_env *env, struct cl_object *obj,
63 struct cl_attr *attr);
65 static struct lu_device *slp_device_alloc(const struct lu_env *env,
66 struct lu_device_type *t,
67 struct lustre_cfg *cfg);
69 static int slp_io_init(const struct lu_env *env, struct cl_object *obj,
71 static struct slp_io *cl2slp_io(const struct lu_env *env,
72 const struct cl_io_slice *slice);
75 static void llu_free_user_page(struct page *page);
77 static const struct lu_object_operations slp_lu_obj_ops;
78 static const struct lu_device_operations slp_lu_ops;
79 static const struct cl_device_operations slp_cl_ops;
80 static const struct cl_io_operations ccc_io_ops;
81 static const struct lu_device_type_operations slp_device_type_ops;
82 //struct lu_device_type slp_device_type;
83 static const struct cl_page_operations slp_page_ops;
84 static const struct cl_page_operations slp_transient_page_ops;
85 static const struct cl_lock_operations slp_lock_ops;
88 /*****************************************************************************
90 * Slp device and device type functions.
94 void *slp_session_key_init(const struct lu_context *ctx,
95 struct lu_context_key *key)
97 struct slp_session *session;
99 OBD_ALLOC_PTR(session);
101 session = ERR_PTR(-ENOMEM);
105 void slp_session_key_fini(const struct lu_context *ctx,
106 struct lu_context_key *key, void *data)
108 struct slp_session *session = data;
109 OBD_FREE_PTR(session);
112 struct lu_context_key slp_session_key = {
113 .lct_tags = LCT_SESSION,
114 .lct_init = slp_session_key_init,
115 .lct_fini = slp_session_key_fini
118 /* type constructor/destructor: slp_type_{init,fini,start,stop}(). */
119 LU_TYPE_INIT_FINI(slp, &ccc_key, &ccc_session_key, &slp_session_key);
121 static struct lu_device *slp_device_alloc(const struct lu_env *env,
122 struct lu_device_type *t,
123 struct lustre_cfg *cfg)
125 return ccc_device_alloc(env, t, cfg, &slp_lu_ops, &slp_cl_ops);
128 static int slp_lock_init(const struct lu_env *env,
129 struct cl_object *obj, struct cl_lock *lock,
130 const struct cl_io *io)
132 return ccc_lock_init(env, obj, lock, io, &slp_lock_ops);
135 static const struct cl_object_operations slp_ops = {
136 .coo_page_init = slp_page_init,
137 .coo_lock_init = slp_lock_init,
138 .coo_io_init = slp_io_init,
139 .coo_attr_get = slp_attr_get,
140 .coo_attr_set = ccc_attr_set,
141 .coo_conf_set = ccc_conf_set,
142 .coo_glimpse = ccc_object_glimpse
145 static int slp_object_print(const struct lu_env *env, void *cookie,
146 lu_printer_t p, const struct lu_object *o)
148 struct ccc_object *obj = lu2ccc(o);
149 struct inode *inode = obj->cob_inode;
150 struct intnl_stat *st = NULL;
153 st = llu_i2stat(inode);
155 return (*p)(env, cookie, LUSTRE_SLP_NAME"-object@%p(%p:%lu/%u)",
157 st ? (unsigned long)st->st_ino : 0UL,
158 inode ? (unsigned int)llu_i2info(inode)->lli_st_generation
162 static const struct lu_object_operations slp_lu_obj_ops = {
163 .loo_object_init = ccc_object_init,
164 .loo_object_start = NULL,
165 .loo_object_delete = NULL,
166 .loo_object_release = NULL,
167 .loo_object_free = ccc_object_free,
168 .loo_object_print = slp_object_print,
169 .loo_object_invariant = NULL
172 static struct lu_object *slp_object_alloc(const struct lu_env *env,
173 const struct lu_object_header *hdr,
174 struct lu_device *dev)
176 return ccc_object_alloc(env, hdr, dev, &slp_ops, &slp_lu_obj_ops);
179 static const struct lu_device_operations slp_lu_ops = {
180 .ldo_object_alloc = slp_object_alloc
183 static const struct cl_device_operations slp_cl_ops = {
184 .cdo_req_init = ccc_req_init
187 static const struct lu_device_type_operations slp_device_type_ops = {
188 .ldto_init = slp_type_init,
189 .ldto_fini = slp_type_fini,
191 .ldto_start = slp_type_start,
192 .ldto_stop = slp_type_stop,
194 .ldto_device_alloc = slp_device_alloc,
195 .ldto_device_free = ccc_device_free,
196 .ldto_device_init = ccc_device_init,
197 .ldto_device_fini = ccc_device_fini
200 struct lu_device_type slp_device_type = {
201 .ldt_tags = LU_DEVICE_CL,
202 .ldt_name = LUSTRE_SLP_NAME,
203 .ldt_ops = &slp_device_type_ops,
204 .ldt_ctx_tags = LCT_CL_THREAD
207 int slp_global_init(void)
211 result = ccc_global_init(&slp_device_type);
215 void slp_global_fini(void)
217 ccc_global_fini(&slp_device_type);
220 /*****************************************************************************
226 static int slp_page_init(const struct lu_env *env, struct cl_object *obj,
227 struct cl_page *page, cfs_page_t *vmpage)
229 struct ccc_page *cpg = cl_object_page_slice(obj, page);
231 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
233 cpg->cpg_page = vmpage;
235 if (page->cp_type == CPT_CACHEABLE) {
238 struct ccc_object *clobj = cl2ccc(obj);
240 cl_page_slice_add(page, &cpg->cpg_cl, obj,
241 &slp_transient_page_ops);
242 clobj->cob_transient_pages++;
248 static int slp_io_init(const struct lu_env *env, struct cl_object *obj,
251 struct ccc_io *vio = ccc_env_io(env);
254 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
256 cl_io_slice_add(io, &vio->cui_cl, obj, &ccc_io_ops);
257 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
260 count = io->u.ci_rw.crw_count;
261 /* "If nbyte is 0, read() will return 0 and have no other
262 * results." -- Single Unix Spec */
266 vio->cui_tot_count = count;
267 vio->cui_tot_nrsegs = 0;
274 static int slp_attr_get(const struct lu_env *env, struct cl_object *obj,
275 struct cl_attr *attr)
277 struct inode *inode = ccc_object_inode(obj);
278 struct intnl_stat *st = llu_i2stat(inode);
280 attr->cat_size = st->st_size;
281 attr->cat_blocks = st->st_blocks;
282 attr->cat_mtime = st->st_mtime;
283 attr->cat_atime = st->st_atime;
284 attr->cat_ctime = st->st_ctime;
285 /* KMS is not known by this layer */
286 return 0; /* layers below have to fill in the rest */
289 /*****************************************************************************
295 static void slp_page_fini_common(struct ccc_page *cp)
297 cfs_page_t *vmpage = cp->cpg_page;
299 LASSERT(vmpage != NULL);
300 llu_free_user_page(vmpage);
304 static void slp_page_completion_common(const struct lu_env *env,
305 struct ccc_page *cp, int ioret)
307 LASSERT(cp->cpg_cl.cpl_page->cp_sync_io != NULL);
310 static void slp_page_completion_read(const struct lu_env *env,
311 const struct cl_page_slice *slice,
314 struct ccc_page *cp = cl2ccc_page(slice);
317 slp_page_completion_common(env, cp, ioret);
322 static void slp_page_completion_write_common(const struct lu_env *env,
323 const struct cl_page_slice *slice,
326 struct ccc_page *cp = cl2ccc_page(slice);
329 cp->cpg_write_queued = 0;
331 * Only ioret == 0, write succeed, then this page could be
332 * deleted from the pending_writing count.
335 slp_page_completion_common(env, cp, ioret);
338 static int slp_page_is_vmlocked(const struct lu_env *env,
339 const struct cl_page_slice *slice)
344 static void slp_transient_page_fini(const struct lu_env *env,
345 struct cl_page_slice *slice)
347 struct ccc_page *cp = cl2ccc_page(slice);
348 struct cl_page *clp = slice->cpl_page;
349 struct ccc_object *clobj = cl2ccc(clp->cp_obj);
351 slp_page_fini_common(cp);
352 clobj->cob_transient_pages--;
356 static const struct cl_page_operations slp_transient_page_ops = {
357 .cpo_own = ccc_transient_page_own,
358 .cpo_assume = ccc_transient_page_assume,
359 .cpo_unassume = ccc_transient_page_unassume,
360 .cpo_disown = ccc_transient_page_disown,
361 .cpo_discard = ccc_transient_page_discard,
362 .cpo_vmpage = ccc_page_vmpage,
363 .cpo_is_vmlocked = slp_page_is_vmlocked,
364 .cpo_fini = slp_transient_page_fini,
365 .cpo_is_under_lock = ccc_page_is_under_lock,
368 .cpo_completion = slp_page_completion_read,
371 .cpo_completion = slp_page_completion_write_common,
376 /*****************************************************************************
382 static int slp_lock_enqueue(const struct lu_env *env,
383 const struct cl_lock_slice *slice,
384 struct cl_io *unused, __u32 enqflags)
386 CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
388 liblustre_wait_event(0);
392 static const struct cl_lock_operations slp_lock_ops = {
393 .clo_delete = ccc_lock_delete,
394 .clo_fini = ccc_lock_fini,
395 .clo_enqueue = slp_lock_enqueue,
396 .clo_wait = ccc_lock_wait,
397 .clo_unuse = ccc_lock_unuse,
398 .clo_fits_into = ccc_lock_fits_into,
401 /*****************************************************************************
407 static int slp_io_rw_lock(const struct lu_env *env,
408 const struct cl_io_slice *ios)
410 struct ccc_io *cio = ccc_env_io(env);
411 struct cl_io *io = ios->cis_io;
415 if (cl_io_is_append(io)) {
417 end = OBD_OBJECT_EOF;
419 start = io->u.ci_wr.wr.crw_pos;
420 end = start + io->u.ci_wr.wr.crw_count - 1;
423 ccc_io_update_iov(env, cio, io);
426 * This acquires real DLM lock only in O_APPEND case, because of
427 * the io->ci_lockreq setting in llu_io_init().
429 LASSERT(ergo(cl_io_is_append(io), io->ci_lockreq == CILR_MANDATORY));
430 LASSERT(ergo(!cl_io_is_append(io), io->ci_lockreq == CILR_NEVER));
431 return ccc_io_one_lock(env, io, 0,
432 io->ci_type == CIT_READ ? CLM_READ : CLM_WRITE,
437 static int slp_io_setattr_iter_init(const struct lu_env *env,
438 const struct cl_io_slice *ios)
443 static int slp_io_setattr_start(const struct lu_env *env,
444 const struct cl_io_slice *ios)
449 static struct page *llu_get_user_page(int index, void *addr, int offset,
459 page->_offset = offset;
460 page->_count = count;
462 CFS_INIT_LIST_HEAD(&page->list);
463 CFS_INIT_LIST_HEAD(&page->_node);
468 static void llu_free_user_page(struct page *page)
474 static int llu_queue_pio(const struct lu_env *env, struct cl_io *io,
475 struct llu_io_group *group,
476 char *buf, size_t count, loff_t pos)
478 struct cl_object *obj = io->ci_obj;
479 struct inode *inode = ccc_object_inode(obj);
480 struct intnl_stat *st = llu_i2stat(inode);
481 struct obd_export *exp = llu_i2obdexp(inode);
483 int rc = 0, ret_bytes = 0;
485 struct cl_2queue *queue;
491 queue = &io->ci_queue;
492 cl_2queue_init(queue);
495 /* prepare the pages array */
497 unsigned long index, offset, bytes;
499 offset = (pos & ~CFS_PAGE_MASK);
500 index = pos >> CFS_PAGE_SHIFT;
501 bytes = CFS_PAGE_SIZE - offset;
505 /* prevent read beyond file range */
506 if (/* local_lock && */
507 io->ci_type == CIT_READ && pos + bytes >= st->st_size) {
508 if (pos >= st->st_size)
510 bytes = st->st_size - pos;
513 /* prepare page for this index */
514 page = llu_get_user_page(index, buf - offset, offset, bytes);
520 clp = cl_page_find(env, obj,
522 page, CPT_TRANSIENT);
529 rc = cl_page_own(env, io, clp);
531 LASSERT(clp->cp_state == CPS_FREEING);
532 cl_page_put(env, clp);
536 cl_2queue_add(queue, clp);
538 /* drop the reference count for cl_page_find, so that the page
539 * will be freed in cl_2queue_fini. */
540 cl_page_put(env, clp);
542 cl_page_clip(env, clp, offset, offset+bytes);
548 group->lig_rwcount += bytes;
554 enum cl_req_type iot;
555 iot = io->ci_type == CIT_READ ? CRT_READ : CRT_WRITE;
556 rc = cl_io_submit_sync(env, io, iot, queue, 0);
561 cl_2queue_discard(env, io, queue);
562 cl_2queue_disown(env, io, queue);
563 cl_2queue_fini(env, queue);
569 struct llu_io_group * get_io_group(struct inode *inode, int maxpages,
570 struct lustre_rw_params *params)
572 struct llu_io_group *group;
574 OBD_ALLOC_PTR(group);
576 return ERR_PTR(-ENOMEM);
578 group->lig_params = params;
583 static int max_io_pages(ssize_t len, int iovlen)
585 return (((len + CFS_PAGE_SIZE -1) / CFS_PAGE_SIZE) + 2 + iovlen - 1);
588 void put_io_group(struct llu_io_group *group)
594 * True, if \a io is a normal io, False for sendfile() / splice_{read|write}
596 int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
601 static int slp_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
603 struct ccc_io *cio = cl2ccc_io(env, ios);
604 struct cl_io *io = ios->cis_io;
605 struct cl_object *obj = io->ci_obj;
606 struct inode *inode = ccc_object_inode(obj);
610 struct llu_io_group *iogroup;
611 struct lustre_rw_params p = {0};
613 struct intnl_stat *st = llu_i2stat(inode);
614 struct llu_inode_info *lli = llu_i2info(inode);
615 struct llu_io_session *session = cl2slp_io(env, ios)->sio_session;
616 int write = io->ci_type == CIT_WRITE;
619 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
622 pos = io->u.ci_wr.wr.crw_pos;
623 cnt = io->u.ci_wr.wr.crw_count;
625 pos = io->u.ci_rd.rd.crw_pos;
626 cnt = io->u.ci_rd.rd.crw_count;
628 if (io->u.ci_wr.wr_append) {
629 p.lrp_lock_mode = LCK_PW;
631 p.lrp_brw_flags = OBD_BRW_SRVLOCK;
632 p.lrp_lock_mode = LCK_NL;
635 iogroup = get_io_group(inode, max_io_pages(cnt, cio->cui_nrsegs), &p);
637 RETURN(PTR_ERR(iogroup));
639 err = ccc_prep_size(env, obj, io, pos, cnt, &exceed);
640 if (err != 0 || (write == 0 && exceed != 0))
644 "%s ino %lu, %lu bytes, offset "LPU64", i_size "LPU64"\n",
645 write ? "Write" : "Read", (unsigned long)st->st_ino,
646 cnt, (__u64)pos, (__u64)st->st_size);
648 if (write && io->u.ci_wr.wr_append)
649 pos = io->u.ci_wr.wr.crw_pos = st->st_size; /* XXX? Do we need to change io content too here? */
650 /* XXX What about if one write syscall writes at 2 different offsets? */
652 for (iovidx = 0; iovidx < cio->cui_nrsegs; iovidx++) {
653 char *buf = (char *) cio->cui_iov[iovidx].iov_base;
654 long count = cio->cui_iov[iovidx].iov_len;
660 if (IS_BAD_PTR(buf) || IS_BAD_PTR(buf + count)) {
661 GOTO(out, err = -EFAULT);
664 if (io->ci_type == CIT_READ) {
665 if (/* local_lock && */ pos >= st->st_size)
667 } else if (io->ci_type == CIT_WRITE) {
668 if (pos >= lli->lli_maxbytes) {
669 GOTO(out, err = -EFBIG);
671 if (pos + count >= lli->lli_maxbytes)
672 count = lli->lli_maxbytes - pos;
677 ret = llu_queue_pio(env, io, iogroup, buf, count, pos);
679 GOTO(out, err = ret);
684 if (io->ci_type == CIT_WRITE) {
685 // obd_adjust_kms(exp, lsm, pos, 0); // XXX
686 if (pos > st->st_size)
693 LASSERT(cnt == 0 || io->ci_type == CIT_READ); /* libsysio should guarantee this */
695 if (!iogroup->lig_rc)
696 session->lis_rwcount += iogroup->lig_rwcount;
697 else if (!session->lis_rc)
698 session->lis_rc = iogroup->lig_rc;
702 put_io_group(iogroup);
706 static const struct cl_io_operations ccc_io_ops = {
709 .cio_fini = ccc_io_fini,
710 .cio_lock = slp_io_rw_lock,
711 .cio_start = slp_io_start,
712 .cio_end = ccc_io_end,
713 .cio_advance = ccc_io_advance
716 .cio_fini = ccc_io_fini,
717 .cio_lock = slp_io_rw_lock,
718 .cio_start = slp_io_start,
719 .cio_end = ccc_io_end,
720 .cio_advance = ccc_io_advance
723 .cio_fini = ccc_io_fini,
724 .cio_iter_init = slp_io_setattr_iter_init,
725 .cio_start = slp_io_setattr_start
728 .cio_fini = ccc_io_fini
733 static struct slp_io *cl2slp_io(const struct lu_env *env,
734 const struct cl_io_slice *slice)
736 /* We call it just for assertion here */
737 cl2ccc_io(env, slice);
739 return slp_env_io(env);
742 /*****************************************************************************
744 * Temporary prototype thing: mirror obd-devices into cl devices.
748 int cl_sb_init(struct llu_sb_info *sbi)
750 struct cl_device *cl;
755 env = cl_env_get(&refcheck);
757 RETURN(PTR_ERR(env));
759 cl = cl_type_setup(env, NULL, &slp_device_type,
760 sbi->ll_dt_exp->exp_obd->obd_lu_dev);
762 GOTO(out, rc = PTR_ERR(cl));
765 sbi->ll_site = cl2lu_dev(cl)->ld_site;
767 cl_env_put(env, &refcheck);
771 int cl_sb_fini(struct llu_sb_info *sbi)
778 env = cl_env_get(&refcheck);
780 RETURN(PTR_ERR(env));
782 if (sbi->ll_cl != NULL) {
783 cl_stack_fini(env, sbi->ll_cl);
787 cl_env_put(env, &refcheck);
789 * If mount failed (sbi->ll_cl == NULL), and this there are no other
790 * mounts, stop device types manually (this usually happens
791 * automatically when last device is destroyed).