1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2007 Cluster File Systems, Inc.
5 * Author: Nikita Danilov <nikita@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 struct cl_page * slp_page_init(const struct lu_env *env,
61 struct cl_object *obj,
62 struct cl_page *page, cfs_page_t *vmpage);
63 static int slp_attr_get (const struct lu_env *env, struct cl_object *obj,
64 struct cl_attr *attr);
66 static struct lu_device *slp_device_alloc(const struct lu_env *env,
67 struct lu_device_type *t,
68 struct lustre_cfg *cfg);
70 static int slp_io_init(const struct lu_env *env, struct cl_object *obj,
72 static struct slp_io *cl2slp_io(const struct lu_env *env,
73 const struct cl_io_slice *slice);
76 static void llu_free_user_page(struct page *page);
78 static const struct lu_object_operations slp_lu_obj_ops;
79 static const struct lu_device_operations slp_lu_ops;
80 static const struct cl_device_operations slp_cl_ops;
81 static const struct cl_io_operations ccc_io_ops;
82 static const struct lu_device_type_operations slp_device_type_ops;
83 //struct lu_device_type slp_device_type;
84 static const struct cl_page_operations slp_page_ops;
85 static const struct cl_page_operations slp_transient_page_ops;
86 static const struct cl_lock_operations slp_lock_ops;
89 /*****************************************************************************
91 * Slp device and device type functions.
95 void *slp_session_key_init(const struct lu_context *ctx,
96 struct lu_context_key *key)
98 struct slp_session *session;
100 OBD_ALLOC_PTR(session);
102 session = ERR_PTR(-ENOMEM);
106 void slp_session_key_fini(const struct lu_context *ctx,
107 struct lu_context_key *key, void *data)
109 struct slp_session *session = data;
110 OBD_FREE_PTR(session);
113 struct lu_context_key slp_session_key = {
114 .lct_tags = LCT_SESSION,
115 .lct_init = slp_session_key_init,
116 .lct_fini = slp_session_key_fini
119 /* type constructor/destructor: slp_type_{init,fini,start,stop}(). */
120 LU_TYPE_INIT_FINI(slp, &ccc_key, &ccc_session_key, &slp_session_key);
122 static struct lu_device *slp_device_alloc(const struct lu_env *env,
123 struct lu_device_type *t,
124 struct lustre_cfg *cfg)
126 return ccc_device_alloc(env, t, cfg, &slp_lu_ops, &slp_cl_ops);
129 static int slp_lock_init(const struct lu_env *env,
130 struct cl_object *obj, struct cl_lock *lock,
131 const struct cl_io *io)
133 return ccc_lock_init(env, obj, lock, io, &slp_lock_ops);
136 static const struct cl_object_operations slp_ops = {
137 .coo_page_init = slp_page_init,
138 .coo_lock_init = slp_lock_init,
139 .coo_io_init = slp_io_init,
140 .coo_attr_get = slp_attr_get,
141 .coo_attr_set = ccc_attr_set,
142 .coo_conf_set = ccc_conf_set,
143 .coo_glimpse = ccc_object_glimpse
146 static int slp_object_print(const struct lu_env *env, void *cookie,
147 lu_printer_t p, const struct lu_object *o)
149 struct ccc_object *obj = lu2ccc(o);
150 struct inode *inode = obj->cob_inode;
151 struct intnl_stat *st = NULL;
154 st = llu_i2stat(inode);
156 return (*p)(env, cookie, LUSTRE_SLP_NAME"-object@%p(%p:%lu/%u)",
158 st ? (unsigned long)st->st_ino : 0UL,
159 inode ? (unsigned int)llu_i2info(inode)->lli_st_generation
163 static const struct lu_object_operations slp_lu_obj_ops = {
164 .loo_object_init = ccc_object_init,
165 .loo_object_start = NULL,
166 .loo_object_delete = NULL,
167 .loo_object_release = NULL,
168 .loo_object_free = ccc_object_free,
169 .loo_object_print = slp_object_print,
170 .loo_object_invariant = NULL
173 static struct lu_object *slp_object_alloc(const struct lu_env *env,
174 const struct lu_object_header *hdr,
175 struct lu_device *dev)
177 return ccc_object_alloc(env, hdr, dev, &slp_ops, &slp_lu_obj_ops);
180 static const struct lu_device_operations slp_lu_ops = {
181 .ldo_object_alloc = slp_object_alloc
184 static const struct cl_device_operations slp_cl_ops = {
185 .cdo_req_init = ccc_req_init
188 static const struct lu_device_type_operations slp_device_type_ops = {
189 .ldto_init = slp_type_init,
190 .ldto_fini = slp_type_fini,
192 .ldto_start = slp_type_start,
193 .ldto_stop = slp_type_stop,
195 .ldto_device_alloc = slp_device_alloc,
196 .ldto_device_free = ccc_device_free,
197 .ldto_device_init = ccc_device_init,
198 .ldto_device_fini = ccc_device_fini
201 struct lu_device_type slp_device_type = {
202 .ldt_tags = LU_DEVICE_CL,
203 .ldt_name = LUSTRE_SLP_NAME,
204 .ldt_ops = &slp_device_type_ops,
205 .ldt_ctx_tags = LCT_CL_THREAD
208 int slp_global_init(void)
212 result = ccc_global_init(&slp_device_type);
216 void slp_global_fini(void)
218 ccc_global_fini(&slp_device_type);
221 /*****************************************************************************
227 static struct cl_page *slp_page_init(const struct lu_env *env,
228 struct cl_object *obj,
229 struct cl_page *page, cfs_page_t *vmpage)
231 struct ccc_page *cpg;
234 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
238 cpg->cpg_page = vmpage;
240 if (page->cp_type == CPT_CACHEABLE) {
243 struct ccc_object *clobj = cl2ccc(obj);
245 cl_page_slice_add(page, &cpg->cpg_cl, obj,
246 &slp_transient_page_ops);
247 clobj->cob_transient_pages++;
252 return ERR_PTR(result);
255 static int slp_io_init(const struct lu_env *env, struct cl_object *obj,
258 struct ccc_io *vio = ccc_env_io(env);
261 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
263 cl_io_slice_add(io, &vio->cui_cl, obj, &ccc_io_ops);
264 if (io->ci_type == CIT_READ || io->ci_type == CIT_WRITE) {
267 count = io->u.ci_rw.crw_count;
268 /* "If nbyte is 0, read() will return 0 and have no other
269 * results." -- Single Unix Spec */
273 vio->cui_tot_count = count;
274 vio->cui_tot_nrsegs = 0;
281 static int slp_attr_get(const struct lu_env *env, struct cl_object *obj,
282 struct cl_attr *attr)
284 struct inode *inode = ccc_object_inode(obj);
285 struct intnl_stat *st = llu_i2stat(inode);
287 attr->cat_size = st->st_size;
288 attr->cat_blocks = st->st_blocks;
289 attr->cat_mtime = st->st_mtime;
290 attr->cat_atime = st->st_atime;
291 attr->cat_ctime = st->st_ctime;
292 /* KMS is not known by this layer */
293 return 0; /* layers below have to fill in the rest */
296 /*****************************************************************************
302 static void slp_page_fini_common(struct ccc_page *cp)
304 cfs_page_t *vmpage = cp->cpg_page;
306 LASSERT(vmpage != NULL);
307 llu_free_user_page(vmpage);
311 static void slp_page_completion_common(const struct lu_env *env,
312 struct ccc_page *cp, int ioret)
314 LASSERT(cp->cpg_cl.cpl_page->cp_sync_io != NULL);
317 static void slp_page_completion_read(const struct lu_env *env,
318 const struct cl_page_slice *slice,
321 struct ccc_page *cp = cl2ccc_page(slice);
324 slp_page_completion_common(env, cp, ioret);
329 static void slp_page_completion_write_common(const struct lu_env *env,
330 const struct cl_page_slice *slice,
333 struct ccc_page *cp = cl2ccc_page(slice);
336 cp->cpg_write_queued = 0;
338 * Only ioret == 0, write succeed, then this page could be
339 * deleted from the pending_writing count.
342 slp_page_completion_common(env, cp, ioret);
345 static int slp_page_is_vmlocked(const struct lu_env *env,
346 const struct cl_page_slice *slice)
351 static void slp_transient_page_fini(const struct lu_env *env,
352 struct cl_page_slice *slice)
354 struct ccc_page *cp = cl2ccc_page(slice);
355 struct cl_page *clp = slice->cpl_page;
356 struct ccc_object *clobj = cl2ccc(clp->cp_obj);
358 slp_page_fini_common(cp);
359 clobj->cob_transient_pages--;
363 static const struct cl_page_operations slp_transient_page_ops = {
364 .cpo_own = ccc_transient_page_own,
365 .cpo_assume = ccc_transient_page_assume,
366 .cpo_unassume = ccc_transient_page_unassume,
367 .cpo_disown = ccc_transient_page_disown,
368 .cpo_discard = ccc_transient_page_discard,
369 .cpo_vmpage = ccc_page_vmpage,
370 .cpo_is_vmlocked = slp_page_is_vmlocked,
371 .cpo_fini = slp_transient_page_fini,
372 .cpo_is_under_lock = ccc_page_is_under_lock,
375 .cpo_completion = slp_page_completion_read,
378 .cpo_completion = slp_page_completion_write_common,
383 /*****************************************************************************
389 static int slp_lock_enqueue(const struct lu_env *env,
390 const struct cl_lock_slice *slice,
391 struct cl_io *unused, __u32 enqflags)
393 CLOBINVRNT(env, slice->cls_obj, ccc_object_invariant(slice->cls_obj));
395 liblustre_wait_event(0);
399 static const struct cl_lock_operations slp_lock_ops = {
400 .clo_delete = ccc_lock_delete,
401 .clo_fini = ccc_lock_fini,
402 .clo_enqueue = slp_lock_enqueue,
403 .clo_wait = ccc_lock_wait,
404 .clo_unuse = ccc_lock_unuse,
405 .clo_fits_into = ccc_lock_fits_into,
408 /*****************************************************************************
414 static int slp_io_rw_lock(const struct lu_env *env,
415 const struct cl_io_slice *ios)
417 struct ccc_io *cio = ccc_env_io(env);
418 struct cl_io *io = ios->cis_io;
422 if (cl_io_is_append(io)) {
424 end = OBD_OBJECT_EOF;
426 start = io->u.ci_wr.wr.crw_pos;
427 end = start + io->u.ci_wr.wr.crw_count - 1;
430 ccc_io_update_iov(env, cio, io);
433 * This acquires real DLM lock only in O_APPEND case, because of
434 * the io->ci_lockreq setting in llu_io_init().
436 LASSERT(ergo(cl_io_is_append(io), io->ci_lockreq == CILR_MANDATORY));
437 LASSERT(ergo(!cl_io_is_append(io), io->ci_lockreq == CILR_NEVER));
438 return ccc_io_one_lock(env, io, 0,
439 io->ci_type == CIT_READ ? CLM_READ : CLM_WRITE,
444 static int slp_io_setattr_iter_init(const struct lu_env *env,
445 const struct cl_io_slice *ios)
450 static int slp_io_setattr_start(const struct lu_env *env,
451 const struct cl_io_slice *ios)
456 static struct page *llu_get_user_page(int index, void *addr, int offset,
466 page->_offset = offset;
467 page->_count = count;
469 CFS_INIT_LIST_HEAD(&page->list);
470 CFS_INIT_LIST_HEAD(&page->_node);
475 static void llu_free_user_page(struct page *page)
481 static int llu_queue_pio(const struct lu_env *env, struct cl_io *io,
482 struct llu_io_group *group,
483 char *buf, size_t count, loff_t pos)
485 struct cl_object *obj = io->ci_obj;
486 struct inode *inode = ccc_object_inode(obj);
487 struct intnl_stat *st = llu_i2stat(inode);
488 struct obd_export *exp = llu_i2obdexp(inode);
490 int rc = 0, ret_bytes = 0;
493 struct cl_2queue *queue;
499 local_lock = group->lig_params->lrp_lock_mode != LCK_NL;
501 queue = &io->ci_queue;
502 cl_2queue_init(queue);
505 /* prepare the pages array */
507 unsigned long index, offset, bytes;
509 offset = (pos & ~CFS_PAGE_MASK);
510 index = pos >> CFS_PAGE_SHIFT;
511 bytes = CFS_PAGE_SIZE - offset;
515 /* prevent read beyond file range */
516 if (/* local_lock && */
517 io->ci_type == CIT_READ && pos + bytes >= st->st_size) {
518 if (pos >= st->st_size)
520 bytes = st->st_size - pos;
523 /* prepare page for this index */
524 page = llu_get_user_page(index, buf - offset, offset, bytes);
530 clp = cl_page_find(env, obj,
532 page, CPT_TRANSIENT);
539 rc = cl_page_own(env, io, clp);
541 LASSERT(clp->cp_state == CPS_FREEING);
542 cl_page_put(env, clp);
546 cl_2queue_add(queue, clp);
548 /* drop the reference count for cl_page_find, so that the page
549 * will be freed in cl_2queue_fini. */
550 cl_page_put(env, clp);
552 cl_page_clip(env, clp, offset, offset+bytes);
558 group->lig_rwcount += bytes;
564 enum cl_req_type iot;
565 iot = io->ci_type == CIT_READ ? CRT_READ : CRT_WRITE;
566 rc = cl_io_submit_sync(env, io, iot, queue, CRP_NORMAL, 0);
571 cl_2queue_discard(env, io, queue);
572 cl_2queue_disown(env, io, queue);
573 cl_2queue_fini(env, queue);
579 struct llu_io_group * get_io_group(struct inode *inode, int maxpages,
580 struct lustre_rw_params *params)
582 struct llu_io_group *group;
584 OBD_ALLOC_PTR(group);
586 return ERR_PTR(-ENOMEM);
588 group->lig_params = params;
593 static int max_io_pages(ssize_t len, int iovlen)
595 return (((len + CFS_PAGE_SIZE -1) / CFS_PAGE_SIZE) + 2 + iovlen - 1);
598 void put_io_group(struct llu_io_group *group)
604 * True, if \a io is a normal io, False for sendfile() / splice_{read|write}
606 int cl_is_normalio(const struct lu_env *env, const struct cl_io *io)
611 static int slp_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
613 struct ccc_io *cio = cl2ccc_io(env, ios);
614 struct cl_io *io = ios->cis_io;
615 struct cl_object *obj = io->ci_obj;
616 struct inode *inode = ccc_object_inode(obj);
620 struct llu_io_group *iogroup;
621 struct lustre_rw_params p = {0};
623 struct intnl_stat *st = llu_i2stat(inode);
624 struct llu_inode_info *lli = llu_i2info(inode);
625 struct llu_io_session *session = cl2slp_io(env, ios)->sio_session;
626 int write = io->ci_type == CIT_WRITE;
629 CLOBINVRNT(env, obj, ccc_object_invariant(obj));
632 pos = io->u.ci_wr.wr.crw_pos;
633 cnt = io->u.ci_wr.wr.crw_count;
635 pos = io->u.ci_rd.rd.crw_pos;
636 cnt = io->u.ci_rd.rd.crw_count;
638 if (io->u.ci_wr.wr_append) {
639 p.lrp_lock_mode = LCK_PW;
641 p.lrp_brw_flags = OBD_BRW_SRVLOCK;
642 p.lrp_lock_mode = LCK_NL;
645 iogroup = get_io_group(inode, max_io_pages(cnt, cio->cui_nrsegs), &p);
647 RETURN(PTR_ERR(iogroup));
649 err = ccc_prep_size(env, obj, io, pos, cnt, 0, &exceed);
650 if (err != 0 || (write == 0 && exceed != 0))
654 "%s ino %lu, %lu bytes, offset "LPU64", i_size "LPU64"\n",
655 write ? "Write" : "Read", (unsigned long)st->st_ino,
656 cnt, (__u64)pos, (__u64)st->st_size);
658 if (write && io->u.ci_wr.wr_append)
659 pos = io->u.ci_wr.wr.crw_pos = st->st_size; /* XXX? Do we need to change io content too here? */
660 /* XXX What about if one write syscall writes at 2 different offsets? */
662 for (iovidx = 0; iovidx < cio->cui_nrsegs; iovidx++) {
663 char *buf = (char *) cio->cui_iov[iovidx].iov_base;
664 long count = cio->cui_iov[iovidx].iov_len;
670 if (IS_BAD_PTR(buf) || IS_BAD_PTR(buf + count)) {
671 GOTO(out, err = -EFAULT);
674 if (io->ci_type == CIT_READ) {
675 if (/* local_lock && */ pos >= st->st_size)
677 } else if (io->ci_type == CIT_WRITE) {
678 if (pos >= lli->lli_maxbytes) {
679 GOTO(out, err = -EFBIG);
681 if (pos + count >= lli->lli_maxbytes)
682 count = lli->lli_maxbytes - pos;
687 ret = llu_queue_pio(env, io, iogroup, buf, count, pos);
689 GOTO(out, err = ret);
694 if (io->ci_type == CIT_WRITE) {
695 // obd_adjust_kms(exp, lsm, pos, 0); // XXX
696 if (pos > st->st_size)
703 LASSERT(cnt == 0 || io->ci_type == CIT_READ); /* libsysio should guarantee this */
705 if (!iogroup->lig_rc)
706 session->lis_rwcount += iogroup->lig_rwcount;
707 else if (!session->lis_rc)
708 session->lis_rc = iogroup->lig_rc;
712 put_io_group(iogroup);
716 static const struct cl_io_operations ccc_io_ops = {
719 .cio_fini = ccc_io_fini,
720 .cio_lock = slp_io_rw_lock,
721 .cio_start = slp_io_start,
722 .cio_end = ccc_io_end,
723 .cio_advance = ccc_io_advance
726 .cio_fini = ccc_io_fini,
727 .cio_lock = slp_io_rw_lock,
728 .cio_start = slp_io_start,
729 .cio_end = ccc_io_end,
730 .cio_advance = ccc_io_advance
733 .cio_fini = ccc_io_fini,
734 .cio_iter_init = slp_io_setattr_iter_init,
735 .cio_start = slp_io_setattr_start
738 .cio_fini = ccc_io_fini
743 static struct slp_io *cl2slp_io(const struct lu_env *env,
744 const struct cl_io_slice *slice)
746 /* We call it just for assertion here */
747 cl2ccc_io(env, slice);
749 return slp_env_io(env);
752 /*****************************************************************************
754 * Temporary prototype thing: mirror obd-devices into cl devices.
758 int cl_sb_init(struct llu_sb_info *sbi)
760 struct cl_device *cl;
765 env = cl_env_get(&refcheck);
767 RETURN(PTR_ERR(env));
769 cl = cl_type_setup(env, NULL, &slp_device_type,
770 sbi->ll_dt_exp->exp_obd->obd_lu_dev);
772 GOTO(out, rc = PTR_ERR(cl));
775 sbi->ll_site = cl2lu_dev(cl)->ld_site;
777 cl_env_put(env, &refcheck);
781 int cl_sb_fini(struct llu_sb_info *sbi)
788 env = cl_env_get(&refcheck);
790 RETURN(PTR_ERR(env));
792 if (sbi->ll_cl != NULL) {
793 cl_stack_fini(env, sbi->ll_cl);
797 cl_env_put(env, &refcheck);
799 * If mount failed (sbi->ll_cl == NULL), and this there are no other
800 * mounts, stop device types manually (this usually happens
801 * automatically when last device is destroyed).
804 cl_env_cache_purge(~0);