1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Light block IO
6 * Copyright (c) 2002-2004 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_LLITE
30 #include <sys/types.h>
31 #include <sys/queue.h>
47 #include "llite_lib.h"
51 struct obd_io_group *lig_oig;
52 struct inode *lig_inode;
56 struct ll_async_page *lig_llap;
57 struct page *lig_pages;
60 #define LLU_IO_GROUP_SIZE(x) \
61 (sizeof(struct llu_io_group) + \
62 sizeof(struct ll_async_page) * (x) + \
63 sizeof(struct page) * (x))
67 struct inode *lis_inode;
71 struct llu_io_group *lis_groups[0];
73 #define LLU_IO_SESSION_SIZE(x) \
74 (sizeof(struct llu_io_session) + (x) * 2 * sizeof(void *))
77 typedef ssize_t llu_file_piov_t(const struct iovec *iovec, int iovlen,
78 _SYSIO_OFF_T pos, ssize_t len,
81 static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
83 struct llu_inode_info *lli = llu_i2info(inode);
84 struct lov_stripe_md *lsm = lli->lli_smd;
85 struct obd_export *exp = llu_i2dtexp(inode);
88 struct ldlm_lock *lock;
89 struct lov_stripe_md *lsm;
90 } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
91 __u32 stripe, vallen = sizeof(stripe);
95 if (lsm->lsm_stripe_count == 1)
98 /* get our offset in the lov */
99 rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
101 CERROR("obd_get_info: rc = %d\n", rc);
104 LASSERT(stripe < lsm->lsm_stripe_count);
108 static int llu_extent_lock_callback(struct ldlm_lock *lock,
109 struct ldlm_lock_desc *new, void *data,
112 struct lustre_handle lockh = { 0 };
116 if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
117 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
122 case LDLM_CB_BLOCKING:
123 ldlm_lock2handle(lock, &lockh);
124 rc = ldlm_cli_cancel(&lockh);
126 CERROR("ldlm_cli_cancel failed: %d\n", rc);
128 case LDLM_CB_CANCELING: {
130 struct llu_inode_info *lli;
131 struct lov_stripe_md *lsm;
135 /* This lock wasn't granted, don't try to evict pages */
136 if (lock->l_req_mode != lock->l_granted_mode)
139 inode = llu_inode_from_lock(lock);
142 lli= llu_i2info(inode);
149 stripe = llu_lock_to_stripe_offset(inode, lock);
150 #warning "fix l_lock() using here!"
151 // l_lock(&lock->l_resource->lr_namespace->ns_lock);
152 kms = ldlm_extent_shift_kms(lock,
153 lsm->lsm_oinfo[stripe].loi_kms);
154 // l_unlock(&lock->l_resource->lr_namespace->ns_lock);
155 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
156 LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
157 lsm->lsm_oinfo[stripe].loi_kms, kms);
158 lsm->lsm_oinfo[stripe].loi_kms = kms;
170 static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
172 struct ptlrpc_request *req = reqp;
173 struct inode *inode = llu_inode_from_lock(lock);
174 struct llu_inode_info *lli;
176 int rc, size = sizeof(*lvb), stripe = 0;
180 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
181 lli = llu_i2info(inode);
183 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
184 if (lli->lli_smd == NULL)
185 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
187 /* First, find out which stripe index this lock corresponds to. */
188 if (lli->lli_smd->lsm_stripe_count > 1)
189 stripe = llu_lock_to_stripe_offset(inode, lock);
191 rc = lustre_pack_reply(req, 1, &size, NULL);
193 CERROR("lustre_pack_reply: %d\n", rc);
197 lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
198 lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
200 LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
201 lli->lli_st_size, stripe, lvb->lvb_size);
205 /* These errors are normal races, so we don't want to fill the console
206 * with messages by calling ptlrpc_error() */
207 if (rc == -ELDLM_NO_LOCK_DATA)
208 lustre_pack_reply(req, 0, NULL, NULL);
214 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms_only);
215 __u64 lov_merge_blocks(struct lov_stripe_md *lsm);
216 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
218 /* NB: lov_merge_size will prefer locally cached writes if they extend the
219 * file (because it prefers KMS over RSS when larger) */
220 int llu_glimpse_size(struct inode *inode)
222 struct llu_inode_info *lli = llu_i2info(inode);
223 struct llu_sb_info *sbi = llu_i2sbi(inode);
224 ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
225 struct lustre_handle lockh = { 0 };
226 int rc, flags = LDLM_FL_HAS_INTENT;
229 CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", lli->lli_st_ino);
231 rc = obd_enqueue(sbi->ll_dt_exp, lli->lli_smd, LDLM_EXTENT, &policy,
232 LCK_PR, &flags, llu_extent_lock_callback,
233 ldlm_completion_ast, llu_glimpse_callback, inode,
234 sizeof(struct ost_lvb), lustre_swab_ost_lvb, &lockh);
236 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
237 RETURN(rc > 0 ? -EIO : rc);
240 lli->lli_st_size = lov_merge_size(lli->lli_smd, 0);
241 lli->lli_st_blocks = lov_merge_blocks(lli->lli_smd);
242 //lli->lli_st_mtime = lov_merge_mtime(lli->lli_smd, inode->i_mtime);
244 CDEBUG(D_DLMTRACE, "glimpse: size: %llu, blocks: %lu\n",
245 lli->lli_st_size, lli->lli_st_blocks);
247 obd_cancel(sbi->ll_dt_exp, lli->lli_smd, LCK_PR, &lockh);
252 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
253 struct lov_stripe_md *lsm, int mode,
254 ldlm_policy_data_t *policy, struct lustre_handle *lockh,
257 struct llu_sb_info *sbi = llu_i2sbi(inode);
258 struct llu_inode_info *lli = llu_i2info(inode);
262 LASSERT(lockh->cookie == 0);
264 /* XXX phil: can we do this? won't it screw the file size up? */
265 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
266 (sbi->ll_flags & LL_SBI_NOLCK))
269 CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
270 lli->lli_st_ino, policy->l_extent.start, policy->l_extent.end);
272 rc = obd_enqueue(sbi->ll_dt_exp, lsm, LDLM_EXTENT, policy, mode,
273 &ast_flags, llu_extent_lock_callback,
274 ldlm_completion_ast, llu_glimpse_callback, inode,
275 sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
279 if (policy->l_extent.start == 0 &&
280 policy->l_extent.end == OBD_OBJECT_EOF)
281 lli->lli_st_size = lov_merge_size(lsm, 1);
283 //inode->i_mtime = lov_merge_mtime(lsm, inode->i_mtime);
288 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
289 struct lov_stripe_md *lsm, int mode,
290 struct lustre_handle *lockh)
292 struct llu_sb_info *sbi = llu_i2sbi(inode);
296 /* XXX phil: can we do this? won't it screw the file size up? */
297 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
298 (sbi->ll_flags & LL_SBI_NOLCK))
301 rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
306 #define LLAP_MAGIC 12346789
308 struct ll_async_page {
312 struct page *llap_page;
313 struct inode *llap_inode;
316 static void llu_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
318 struct ll_async_page *llap;
320 struct lov_stripe_md *lsm;
321 obd_valid valid_flags;
324 llap = LLAP_FROM_COOKIE(data);
325 inode = llap->llap_inode;
326 lsm = llu_i2info(inode)->lli_smd;
328 oa->o_id = lsm->lsm_object_id;
329 oa->o_valid = OBD_MD_FLID;
330 valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
331 if (cmd == OBD_BRW_WRITE)
332 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
334 obdo_from_inode(oa, inode, valid_flags);
338 /* called for each page in a completed rpc.*/
339 static void llu_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
341 struct ll_async_page *llap;
344 llap = LLAP_FROM_COOKIE(data);
345 llap->llap_queued = 0;
346 page = llap->llap_page;
349 if (cmd == OBD_BRW_WRITE)
350 CERROR("writeback error on page %p index %ld: %d\n",
351 page, page->index, rc);
356 static struct obd_async_page_ops llu_async_page_ops = {
357 .ap_make_ready = NULL,
358 .ap_refresh_count = NULL,
359 .ap_fill_obdo = llu_ap_fill_obdo,
360 .ap_completion = llu_ap_completion,
363 static int llu_queue_pio(int cmd, struct llu_io_group *group,
364 char *buf, size_t count, loff_t pos)
366 struct llu_inode_info *lli = llu_i2info(group->lig_inode);
367 struct lov_stripe_md *lsm = lli->lli_smd;
368 struct obd_export *exp = llu_i2dtexp(group->lig_inode);
369 struct page *pages = &group->lig_pages[group->lig_npages];
370 struct ll_async_page *llap = &group->lig_llap[group->lig_npages];
371 int i, rc, npages = 0, ret_bytes = 0;
377 /* prepare the pages array */
379 unsigned long index, offset, bytes;
381 offset = (pos & ~PAGE_CACHE_MASK);
382 index = pos >> PAGE_CACHE_SHIFT;
383 bytes = PAGE_CACHE_SIZE - offset;
387 /* prevent read beyond file range */
388 if ((cmd == OBD_BRW_READ) &&
389 (pos + bytes) >= lli->lli_st_size) {
390 if (pos >= lli->lli_st_size)
392 bytes = lli->lli_st_size - pos;
395 /* prepare page for this index */
396 pages[npages].index = index;
397 pages[npages].addr = buf - offset;
399 pages[npages]._offset = offset;
400 pages[npages]._count = bytes;
407 group->lig_rwcount += bytes;
411 group->lig_npages += npages;
413 for (i = 0; i < npages; i++) {
414 llap[i].llap_magic = LLAP_MAGIC;
415 rc = obd_prep_async_page(exp, lsm, NULL, &pages[i],
416 (obd_off)pages[i].index << PAGE_SHIFT,
418 &llap[i], &llap[i].llap_cookie);
421 llap[i].llap_cookie = NULL;
424 CDEBUG(D_CACHE, "llap %p page %p group %p obj off "LPU64"\n",
425 &llap[i], &pages[i], llap[i].llap_cookie,
426 (obd_off)pages[i].index << PAGE_SHIFT);
427 pages[i].private = (unsigned long)&llap[i];
428 llap[i].llap_page = &pages[i];
429 llap[i].llap_inode = group->lig_inode;
431 rc = obd_queue_group_io(exp, lsm, NULL, group->lig_oig,
432 llap[i].llap_cookie, cmd,
433 pages[i]._offset, pages[i]._count, 0,
434 ASYNC_READY | ASYNC_URGENT |
435 ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
441 llap[i].llap_queued = 1;
448 struct llu_io_group * get_io_group(struct inode *inode, int maxpages)
450 struct llu_io_group *group;
453 OBD_ALLOC(group, LLU_IO_GROUP_SIZE(maxpages));
455 return ERR_PTR(-ENOMEM);
458 group->lig_inode = inode;
459 group->lig_maxpages = maxpages;
460 group->lig_llap = (struct ll_async_page *)(group + 1);
461 group->lig_pages = (struct page *) (group->lig_llap + maxpages);
463 rc = oig_init(&group->lig_oig);
465 OBD_FREE(group, LLU_IO_GROUP_SIZE(maxpages));
472 static int max_io_pages(ssize_t len, int iovlen)
474 return (((len + PAGE_SIZE -1) / PAGE_SIZE) + 2 + iovlen - 1);
478 void put_io_group(struct llu_io_group *group)
480 struct lov_stripe_md *lsm = llu_i2info(group->lig_inode)->lli_smd;
481 struct obd_export *exp = llu_i2dtexp(group->lig_inode);
482 struct ll_async_page *llap = group->lig_llap;
485 for (i = 0; i< group->lig_npages; i++) {
486 if (llap[i].llap_cookie)
487 obd_teardown_async_page(exp, lsm, NULL,
488 llap[i].llap_cookie);
491 I_RELE(group->lig_inode);
493 oig_release(group->lig_oig);
494 OBD_FREE(group, LLU_IO_GROUP_SIZE(group->lig_maxpages));
498 ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
499 _SYSIO_OFF_T pos, ssize_t len,
502 struct llu_io_session *session = (struct llu_io_session *) private;
503 struct inode *inode = session->lis_inode;
504 struct llu_inode_info *lli = llu_i2info(inode);
505 struct ll_file_data *fd = lli->lli_file_data;
506 struct lustre_handle lockh = {0};
507 struct lov_stripe_md *lsm = lli->lli_smd;
508 struct obd_export *exp = NULL;
509 ldlm_policy_data_t policy;
510 struct llu_io_group *iogroup;
511 int astflag = (lli->lli_open_flags & O_NONBLOCK) ?
512 LDLM_FL_BLOCK_NOWAIT : 0;
514 int err, is_read, lock_mode, iovidx, ret;
517 /* in a large iov read/write we'll be repeatedly called.
518 * so give a chance to answer cancel ast here
520 liblustre_wait_event(0);
522 exp = llu_i2dtexp(inode);
526 if (len == 0 || iovlen == 0)
529 if (pos + len > lli->lli_maxbytes)
532 iogroup = get_io_group(inode, max_io_pages(len, iovlen));
534 RETURN(PTR_ERR(iogroup));
536 is_read = session->lis_cmd == OBD_BRW_READ;
537 lock_mode = is_read ? LCK_PR : LCK_PW;
539 if (!is_read && (lli->lli_open_flags & O_APPEND)) {
540 policy.l_extent.start = 0;
541 policy.l_extent.end = OBD_OBJECT_EOF;
543 policy.l_extent.start = pos;
544 policy.l_extent.end = pos + len - 1;
547 err = llu_extent_lock(fd, inode, lsm, lock_mode, &policy,
553 kms = lov_merge_size(lsm, 1);
554 if (policy.l_extent.end > kms) {
555 /* A glimpse is necessary to determine whether we
556 * return a short read or some zeroes at the end of
558 if ((err = llu_glimpse_size(inode))) {
559 llu_extent_unlock(fd, inode, lsm,
564 lli->lli_st_size = kms;
567 if (lli->lli_open_flags & O_APPEND)
568 pos = lli->lli_st_size;
571 for (iovidx = 0; iovidx < iovlen; iovidx++) {
572 char *buf = (char *) iovec[iovidx].iov_base;
573 size_t count = iovec[iovidx].iov_len;
579 if (IS_BAD_PTR(buf) || IS_BAD_PTR(buf + count)) {
580 llu_extent_unlock(fd, inode, lsm, lock_mode, &lockh);
581 GOTO(err_put, err = -EFAULT);
585 if (pos >= lli->lli_st_size)
588 if (pos >= lli->lli_maxbytes) {
589 llu_extent_unlock(fd, inode, lsm, lock_mode,
591 GOTO(err_put, err = -EFBIG);
593 if (pos + count >= lli->lli_maxbytes)
594 count = lli->lli_maxbytes - pos;
597 ret = llu_queue_pio(session->lis_cmd, iogroup, buf, count, pos);
599 llu_extent_unlock(fd, inode, lsm, lock_mode, &lockh);
600 GOTO(err_put, err = ret);
604 LASSERT(ret == count);
605 obd_adjust_kms(exp, lsm, pos, 0);
606 /* file size grow immediately */
607 if (pos > lli->lli_st_size)
608 lli->lli_st_size = pos;
615 LASSERT(len == 0 || is_read); /* libsysio should guarantee this */
617 err = llu_extent_unlock(fd, inode, lsm, lock_mode, &lockh);
619 CERROR("extent unlock error %d\n", err);
621 err = obd_trigger_group_io(exp, lsm, NULL, iogroup->lig_oig);
625 session->lis_groups[session->lis_ngroups++] = iogroup;
628 put_io_group(iogroup);
629 RETURN((ssize_t)err);
633 struct llu_io_session *get_io_session(struct inode *ino, int ngroups, int cmd)
635 struct llu_io_session *session;
637 OBD_ALLOC(session, LLU_IO_SESSION_SIZE(ngroups));
642 session->lis_inode = ino;
643 session->lis_max_groups = ngroups;
644 session->lis_cmd = cmd;
648 static void put_io_session(struct llu_io_session *session)
652 for (i = 0; i < session->lis_ngroups; i++) {
653 if (session->lis_groups[i]) {
654 put_io_group(session->lis_groups[i]);
655 session->lis_groups[i] = NULL;
659 I_RELE(session->lis_inode);
660 OBD_FREE(session, LLU_IO_SESSION_SIZE(session->lis_max_groups));
663 static int llu_file_rwx(struct inode *ino,
667 struct llu_io_session *session;
669 int cmd = read ? OBD_BRW_READ : OBD_BRW_WRITE;
672 LASSERT(ioctx->ioctx_xtvlen >= 0);
673 LASSERT(ioctx->ioctx_iovlen >= 0);
675 liblustre_wait_event(0);
677 if (!ioctx->ioctx_xtvlen)
680 /* XXX consider other types later */
681 LASSERT(S_ISREG(llu_i2info(ino)->lli_st_mode));
683 session = get_io_session(ino, ioctx->ioctx_xtvlen * 2, cmd);
687 cc = _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
688 ioctx->ioctx_iov, ioctx->ioctx_iovlen,
689 llu_file_prwv, session);
692 LASSERT(!ioctx->ioctx_cc);
693 ioctx->ioctx_private = session;
696 put_io_session(session);
701 int llu_iop_read(struct inode *ino,
704 return llu_file_rwx(ino, ioctx, 1);
707 int llu_iop_write(struct inode *ino,
713 memset(&iattr, 0, sizeof(iattr));
714 iattr.ia_mtime = iattr.ia_atime = CURRENT_TIME;
715 iattr.ia_valid = ATTR_MTIME | ATTR_ATIME | ATTR_RAW;
717 liblustre_wait_event(0);
718 rc = llu_setattr_raw(ino, &iattr);
720 CERROR("failed to set mtime/atime during write: %d", rc);
721 /* XXX should continue or return error? */
724 return llu_file_rwx(ino, ioctx, 0);
727 int llu_iop_iodone(struct ioctx *ioctx)
729 struct llu_io_session *session;
730 struct llu_io_group *group;
731 int i, err = 0, rc = 0;
734 liblustre_wait_event(0);
736 session = (struct llu_io_session *) ioctx->ioctx_private;
738 LASSERT(!IS_ERR(session));
740 for (i = 0; i < session->lis_ngroups; i++) {
741 group = session->lis_groups[i];
744 err = oig_wait(group->lig_oig);
749 ioctx->ioctx_cc += group->lig_rwcount;
751 session->lis_groups[i] = NULL;
757 ioctx->ioctx_cc = -1;
758 ioctx->ioctx_errno = -rc;
761 put_io_session(session);
762 ioctx->ioctx_private = NULL;