1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Light Super operations
6 * Copyright (c) 2002, 2003 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>
41 #include "llite_lib.h"
43 static int llu_extent_lock_callback(struct ldlm_lock *lock,
44 struct ldlm_lock_desc *new, void *data,
47 struct lustre_handle lockh = { 0 };
52 if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
53 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
58 case LDLM_CB_BLOCKING:
59 ldlm_lock2handle(lock, &lockh);
60 rc = ldlm_cli_cancel(&lockh);
62 CERROR("ldlm_cli_cancel failed: %d\n", rc);
64 case LDLM_CB_CANCELING: {
65 struct inode *inode = llu_inode_from_lock(lock);
66 struct llu_inode_info *lli;
70 lli= llu_i2info(inode);
81 ll_pgcache_remove_extent(inode, lli->lli_smd, lock);
94 int llu_extent_lock_no_validate(struct ll_file_data *fd,
96 struct lov_stripe_md *lsm,
98 struct ldlm_extent *extent,
99 struct lustre_handle *lockh,
102 struct llu_sb_info *sbi = llu_i2sbi(inode);
103 struct llu_inode_info *lli = llu_i2info(inode);
107 LASSERT(lockh->cookie == 0);
109 /* XXX phil: can we do this? won't it screw the file size up? */
110 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
111 (sbi->ll_flags & LL_SBI_NOLCK))
114 CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
115 lli->lli_st_ino, extent->start, extent->end);
117 rc = obd_enqueue(sbi->ll_osc_exp, lsm, NULL, LDLM_EXTENT, extent,
118 sizeof(extent), mode, &ast_flags,
119 llu_extent_lock_callback, inode, lockh);
125 * this grabs a lock and manually implements behaviour that makes it look like
126 * the OST is returning the file size with each lock acquisition.
128 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
129 struct lov_stripe_md *lsm, int mode,
130 struct ldlm_extent *extent, struct lustre_handle *lockh)
132 struct llu_inode_info *lli = llu_i2info(inode);
133 struct obd_export *exp = llu_i2obdexp(inode);
134 struct ldlm_extent size_lock;
135 struct lustre_handle match_lockh = {0};
136 int flags, rc, matched;
139 rc = llu_extent_lock_no_validate(fd, inode, lsm, mode, extent, lockh, 0);
143 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
146 rc = llu_inode_getattr(inode, lsm);
148 llu_extent_unlock(fd, inode, lsm, mode, lockh);
152 size_lock.start = lli->lli_st_size;
153 size_lock.end = OBD_OBJECT_EOF;
155 /* XXX I bet we should be checking the lock ignore flags.. */
156 flags = LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
157 matched = obd_match(exp, lsm, LDLM_EXTENT, &size_lock,
158 sizeof(size_lock), LCK_PR, &flags, inode,
161 /* hey, alright, we hold a size lock that covers the size we
162 * just found, its not going to change for a while.. */
164 set_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags);
165 obd_cancel(exp, lsm, LCK_PR, &match_lockh);
171 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
172 struct lov_stripe_md *lsm, int mode,
173 struct lustre_handle *lockh)
175 struct llu_sb_info *sbi = llu_i2sbi(inode);
179 /* XXX phil: can we do this? won't it screw the file size up? */
180 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
181 (sbi->ll_flags & LL_SBI_NOLCK))
184 rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
189 #define LLAP_MAGIC 12346789
191 struct ll_async_page {
195 struct page *llap_page;
196 struct inode *llap_inode;
199 static struct ll_async_page *llap_from_cookie(void *cookie)
201 struct ll_async_page *llap = cookie;
202 if (llap->llap_magic != LLAP_MAGIC)
203 return ERR_PTR(-EINVAL);
207 static void llu_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
209 struct ll_async_page *llap;
211 struct lov_stripe_md *lsm;
212 obd_flag valid_flags;
215 llap = llap_from_cookie(data);
221 inode = llap->llap_inode;
222 lsm = llu_i2info(inode)->lli_smd;
224 oa->o_id = lsm->lsm_object_id;
225 oa->o_valid = OBD_MD_FLID;
226 valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
227 if (cmd == OBD_BRW_WRITE)
228 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
230 obdo_from_inode(oa, inode, valid_flags);
234 /* called for each page in a completed rpc.*/
235 static void llu_ap_completion(void *data, int cmd, int rc)
237 struct ll_async_page *llap;
240 llap = llap_from_cookie(data);
246 llap->llap_queued = 0;
247 page = llap->llap_page;
250 if (cmd == OBD_BRW_WRITE)
251 CERROR("writeback error on page %p index %ld: %d\n",
252 page, page->index, rc);
257 static struct obd_async_page_ops llu_async_page_ops = {
258 .ap_make_ready = NULL,
259 .ap_refresh_count = NULL,
260 .ap_fill_obdo = llu_ap_fill_obdo,
261 .ap_completion = llu_ap_completion,
265 struct llu_sysio_cookie* get_sysio_cookie(struct inode *inode, int maxpages)
267 struct llu_sysio_cookie *cookie;
270 OBD_ALLOC(cookie, LLU_SYSIO_COOKIE_SIZE(maxpages));
275 cookie->lsc_inode = inode;
276 cookie->lsc_maxpages = maxpages;
277 cookie->lsc_llap = (struct ll_async_page *)(cookie + 1);
278 cookie->lsc_pages = (struct page *) (cookie->lsc_llap + maxpages);
280 rc = oig_init(&cookie->lsc_oig);
282 OBD_FREE(cookie, LLU_SYSIO_COOKIE_SIZE(maxpages));
291 void put_sysio_cookie(struct llu_sysio_cookie *cookie)
293 struct lov_stripe_md *lsm = llu_i2info(cookie->lsc_inode)->lli_smd;
294 struct obd_export *exp = llu_i2obdexp(cookie->lsc_inode);
295 struct ll_async_page *llap = cookie->lsc_llap;
296 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
297 struct page *pages = cookie->lsc_pages;
301 for (i = 0; i< cookie->lsc_maxpages; i++) {
302 if (llap[i].llap_cookie)
303 obd_teardown_async_page(exp, lsm, NULL,
304 llap[i].llap_cookie);
305 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
306 if (pages[i]._managed) {
308 pages[i]._managed = 0;
313 I_RELE(cookie->lsc_inode);
315 oig_release(cookie->lsc_oig);
316 OBD_FREE(cookie, LLU_SYSIO_COOKIE_SIZE(cookie->lsc_maxpages));
319 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
320 /* Note: these code should be removed finally, don't need
324 int prepare_unaligned_write(struct llu_sysio_cookie *cookie)
326 struct inode *inode = cookie->lsc_inode;
327 struct llu_inode_info *lli = llu_i2info(inode);
328 struct lov_stripe_md *lsm = lli->lli_smd;
330 struct page *pages = cookie->lsc_pages;
331 int i, pgidx[2] = {0, cookie->lsc_npages-1};
335 for (i = 0; i < 2; i++) {
336 struct page *oldpage = &pages[pgidx[i]];
341 if (i == 0 && pgidx[0] == pgidx[1])
344 LASSERT(oldpage->_offset + oldpage->_count <= PAGE_CACHE_SIZE);
346 if (oldpage->_count == PAGE_CACHE_SIZE)
349 if (oldpage->index << PAGE_CACHE_SHIFT >=
353 newbuf = malloc(PAGE_CACHE_SIZE);
357 newpage.index = oldpage->index;
358 newpage.addr = newbuf;
361 pg.off = ((obd_off)newpage.index << PAGE_CACHE_SHIFT);
362 if (pg.off + PAGE_CACHE_SIZE > lli->lli_st_size)
363 pg.count = lli->lli_st_size % PAGE_CACHE_SIZE;
365 pg.count = PAGE_CACHE_SIZE;
368 oa.o_id = lsm->lsm_object_id;
369 oa.o_mode = lli->lli_st_mode;
370 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
373 rc = obd_brw(OBD_BRW_READ, llu_i2obdexp(inode), &oa, lsm, 1, &pg, NULL);
379 /* copy page content, and reset page params */
380 memcpy(newbuf + oldpage->_offset,
381 (char*)oldpage->addr + oldpage->_offset,
384 oldpage->addr = newbuf;
385 if ((((obd_off)oldpage->index << PAGE_CACHE_SHIFT) +
386 oldpage->_offset + oldpage->_count) > lli->lli_st_size)
387 oldpage->_count += oldpage->_offset;
389 oldpage->_count = PAGE_CACHE_SIZE;
390 oldpage->_offset = 0;
391 oldpage->_managed = 1;
399 int llu_prep_async_io(struct llu_sysio_cookie *cookie, int cmd,
400 char *buf, loff_t pos, size_t count)
402 struct llu_inode_info *lli = llu_i2info(cookie->lsc_inode);
403 struct lov_stripe_md *lsm = lli->lli_smd;
404 struct obd_export *exp = llu_i2obdexp(cookie->lsc_inode);
405 struct page *pages = cookie->lsc_pages;
406 struct ll_async_page *llap = cookie->lsc_llap;
407 int i, rc, npages = 0;
413 /* prepare the pages array */
415 unsigned long index, offset, bytes;
417 offset = (pos & ~PAGE_CACHE_MASK);
418 index = pos >> PAGE_CACHE_SHIFT;
419 bytes = PAGE_CACHE_SIZE - offset;
423 /* prevent read beyond file range */
424 if ((cmd == OBD_BRW_READ) &&
425 (pos + bytes) >= lli->lli_st_size) {
426 if (pos >= lli->lli_st_size)
428 bytes = lli->lli_st_size - pos;
431 /* prepare page for this index */
432 pages[npages].index = index;
433 pages[npages].addr = buf - offset;
435 pages[npages]._offset = offset;
436 pages[npages]._count = bytes;
443 cookie->lsc_rwcount += bytes;
446 cookie->lsc_npages = npages;
448 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
449 if (cmd == OBD_BRW_WRITE) {
450 rc = prepare_unaligned_write(cookie);
456 for (i = 0; i < npages; i++) {
457 llap[i].llap_magic = LLAP_MAGIC;
458 rc = obd_prep_async_page(exp, lsm, NULL, &pages[i],
459 (obd_off)pages[i].index << PAGE_SHIFT,
461 &llap[i], &llap[i].llap_cookie);
463 llap[i].llap_cookie = NULL;
466 CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n",
467 &llap[i], &pages[i], llap[i].llap_cookie,
468 (obd_off)pages[i].index << PAGE_SHIFT);
469 pages[i].private = (unsigned long)&llap[i];
470 llap[i].llap_page = &pages[i];
471 llap[i].llap_inode = cookie->lsc_inode;
473 rc = obd_queue_group_io(exp, lsm, NULL, cookie->lsc_oig,
474 llap[i].llap_cookie, cmd,
475 pages[i]._offset, pages[i]._count, 0,
476 ASYNC_READY | ASYNC_URGENT |
477 ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
481 llap[i].llap_queued = 1;
488 int llu_start_async_io(struct llu_sysio_cookie *cookie)
490 struct lov_stripe_md *lsm = llu_i2info(cookie->lsc_inode)->lli_smd;
491 struct obd_export *exp = llu_i2obdexp(cookie->lsc_inode);
493 return obd_trigger_group_io(exp, lsm, NULL, cookie->lsc_oig);
497 * read/write a continuous buffer for an inode (zero-copy)
499 struct llu_sysio_cookie*
500 llu_rw(int cmd, struct inode *inode, char *buf, size_t count, loff_t pos)
502 struct llu_sysio_cookie *cookie;
506 max_pages = (count >> PAGE_SHIFT) + 2;
508 cookie = get_sysio_cookie(inode, max_pages);
510 RETURN(ERR_PTR(-ENOMEM));
512 rc = llu_prep_async_io(cookie, cmd, buf, pos, count);
514 GOTO(out_cleanup, rc);
516 rc = llu_start_async_io(cookie);
518 GOTO(out_cleanup, rc);
523 CERROR("file i/o error!\n");
530 put_sysio_cookie(cookie);
534 struct llu_sysio_callback_args*
535 llu_file_write(struct inode *inode, const struct iovec *iovec,
536 size_t iovlen, loff_t pos)
538 struct llu_inode_info *lli = llu_i2info(inode);
539 struct ll_file_data *fd = lli->lli_file_data;
540 struct lustre_handle lockh = {0};
541 struct lov_stripe_md *lsm = lli->lli_smd;
542 struct llu_sysio_callback_args *lsca;
543 struct llu_sysio_cookie *cookie;
544 struct ldlm_extent extent;
549 /* XXX consider other types later */
550 if (!S_ISREG(lli->lli_st_mode))
553 LASSERT(iovlen <= MAX_IOVEC);
555 OBD_ALLOC(lsca, sizeof(*lsca));
557 RETURN(ERR_PTR(-ENOMEM));
559 /* FIXME optimize the following extent locking */
560 for (iovidx = 0; iovidx < iovlen; iovidx++) {
561 char *buf = (char*)iovec[iovidx].iov_base;
562 size_t count = iovec[iovidx].iov_len;
567 /* FIXME libsysio haven't handle O_APPEND */
569 extent.end = pos + count - 1;
571 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
572 if ((pos & ~PAGE_CACHE_MASK) == 0 &&
573 (count & ~PAGE_CACHE_MASK) == 0)
574 err = llu_extent_lock_no_validate(fd, inode, lsm,
575 LCK_PW, &extent, &lockh, 0);
577 err = llu_extent_lock(fd, inode, lsm, LCK_PW,
580 /* server will handle partial write, so we don't
581 * care for file size here */
582 err = llu_extent_lock_no_validate(fd, inode, lsm, LCK_PW,
586 GOTO(err_out, err = -ENOLCK);
588 CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
589 lli->lli_st_ino, count, pos);
591 cookie = llu_rw(OBD_BRW_WRITE, inode, buf, count, pos);
592 if (!IS_ERR(cookie)) {
594 lsca->cookies[lsca->ncookies++] = cookie;
596 /* file size grow. XXX should be done here? */
597 if (pos > lli->lli_st_size) {
598 lli->lli_st_size = pos;
599 set_bit(LLI_F_PREFER_EXTENDED_SIZE,
603 llu_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
604 GOTO(err_out, err = PTR_ERR(cookie));
608 err = llu_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
610 CERROR("extent unlock error %d\n", err);
616 /* teardown all async stuff */
617 while (lsca->ncookies--) {
618 put_sysio_cookie(lsca->cookies[lsca->ncookies]);
620 OBD_FREE(lsca, sizeof(*lsca));
622 RETURN(ERR_PTR(err));
626 static void llu_update_atime(struct inode *inode)
628 struct llu_inode_info *lli = llu_i2info(inode);
633 attr.ia_atime = LTIME_S(CURRENT_TIME);
634 attr.ia_valid = ATTR_ATIME;
636 if (lli->lli_st_atime == attr.ia_atime) return;
637 if (IS_RDONLY(inode)) return;
638 if (IS_NOATIME(inode)) return;
640 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
641 llu_inode_setattr(inode, &attr, 0);
643 /* update atime, but don't explicitly write it out just this change */
644 inode->i_atime = CURRENT_TIME;
649 struct llu_sysio_callback_args*
650 llu_file_read(struct inode *inode, const struct iovec *iovec,
651 size_t iovlen, loff_t pos)
653 struct llu_inode_info *lli = llu_i2info(inode);
654 struct ll_file_data *fd = lli->lli_file_data;
655 struct lov_stripe_md *lsm = lli->lli_smd;
656 struct lustre_handle lockh = { 0 };
657 struct ldlm_extent extent;
658 struct llu_sysio_callback_args *lsca;
659 struct llu_sysio_cookie *cookie;
665 OBD_ALLOC(lsca, sizeof(*lsca));
667 RETURN(ERR_PTR(-ENOMEM));
669 for (iovidx = 0; iovidx < iovlen; iovidx++) {
670 char *buf = iovec[iovidx].iov_base;
671 size_t count = iovec[iovidx].iov_len;
673 /* "If nbyte is 0, read() will return 0 and have no other results."
674 * -- Single Unix Spec */
679 extent.end = pos + count - 1;
681 err = llu_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
683 GOTO(err_out, err = -ENOLCK);
685 CDEBUG(D_INFO, "Reading inode %lu, "LPSZ" bytes, offset %Ld\n",
686 lli->lli_st_ino, count, pos);
688 if (pos >= lli->lli_st_size) {
689 llu_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
693 cookie = llu_rw(OBD_BRW_READ, inode, buf, count, pos);
694 if (!IS_ERR(cookie)) {
696 lsca->cookies[lsca->ncookies++] = cookie;
699 llu_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
700 GOTO(err_out, err = PTR_ERR(cookie));
704 err = llu_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
706 CERROR("extent_unlock fail: %d\n", err);
710 llu_update_atime(inode);
715 /* teardown all async stuff */
716 while (lsca->ncookies--) {
717 put_sysio_cookie(lsca->cookies[lsca->ncookies]);
719 OBD_FREE(lsca, sizeof(*lsca));
721 RETURN(ERR_PTR(err));
724 int llu_iop_iodone(struct ioctx *ioctxp)
726 struct llu_sysio_callback_args *lsca = ioctxp->ioctx_private;
727 struct llu_sysio_cookie *cookie;
728 int i, err = 0, rc = 0;
731 /* write/read(fd, buf, 0) */
733 ioctxp->ioctx_cc = 0;
737 LASSERT(!IS_ERR(lsca));
739 for (i = 0; i < lsca->ncookies; i++) {
740 cookie = lsca->cookies[i];
742 err = oig_wait(cookie->lsc_oig);
746 ioctxp->ioctx_cc += cookie->lsc_rwcount;
747 put_sysio_cookie(cookie);
753 ioctxp->ioctx_cc = -1;
754 ioctxp->ioctx_errno = -rc;
757 OBD_FREE(lsca, sizeof(*lsca));
758 ioctxp->ioctx_private = NULL;