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;
269 OBD_ALLOC(cookie, LLU_SYSIO_COOKIE_SIZE(maxpages));
272 cookie->lsc_inode = inode;
273 cookie->lsc_maxpages = maxpages;
274 cookie->lsc_llap = (struct ll_async_page *)(cookie + 1);
275 cookie->lsc_pages = (struct page *) (cookie->lsc_llap + maxpages);
277 osic_init(&cookie->lsc_osic);
284 void put_sysio_cookie(struct llu_sysio_cookie *cookie)
286 struct lov_stripe_md *lsm = llu_i2info(cookie->lsc_inode)->lli_smd;
287 struct obd_export *exp = llu_i2obdexp(cookie->lsc_inode);
288 struct ll_async_page *llap = cookie->lsc_llap;
289 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
290 struct page *pages = cookie->lsc_pages;
294 for (i = 0; i< cookie->lsc_maxpages; i++) {
295 if (llap[i].llap_cookie)
296 obd_teardown_async_page(exp, lsm, NULL,
297 llap[i].llap_cookie);
298 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
299 if (pages[i]._managed) {
301 pages[i]._managed = 0;
306 I_RELE(cookie->lsc_inode);
308 osic_release(cookie->lsc_osic);
309 OBD_FREE(cookie, LLU_SYSIO_COOKIE_SIZE(cookie->lsc_maxpages));
312 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
313 /* Note: these code should be removed finally, don't need
317 int prepare_unaligned_write(struct llu_sysio_cookie *cookie)
319 struct inode *inode = cookie->lsc_inode;
320 struct llu_inode_info *lli = llu_i2info(inode);
321 struct lov_stripe_md *lsm = lli->lli_smd;
323 struct page *pages = cookie->lsc_pages;
324 int i, pgidx[2] = {0, cookie->lsc_npages-1};
328 for (i = 0; i < 2; i++) {
329 struct page *oldpage = &pages[pgidx[i]];
334 if (i == 0 && pgidx[0] == pgidx[1])
337 LASSERT(oldpage->_offset + oldpage->_count <= PAGE_CACHE_SIZE);
339 if (oldpage->_count == PAGE_CACHE_SIZE)
342 if (oldpage->index << PAGE_CACHE_SHIFT >=
346 newbuf = malloc(PAGE_CACHE_SIZE);
350 newpage.index = oldpage->index;
351 newpage.addr = newbuf;
354 pg.off = ((obd_off)newpage.index << PAGE_CACHE_SHIFT);
355 if (pg.off + PAGE_CACHE_SIZE > lli->lli_st_size)
356 pg.count = lli->lli_st_size % PAGE_CACHE_SIZE;
358 pg.count = PAGE_CACHE_SIZE;
361 oa.o_id = lsm->lsm_object_id;
362 oa.o_mode = lli->lli_st_mode;
363 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLTYPE;
366 rc = obd_brw(OBD_BRW_READ, llu_i2obdexp(inode), &oa, lsm, 1, &pg, NULL);
372 /* copy page content, and reset page params */
373 memcpy(newbuf + oldpage->_offset,
374 (char*)oldpage->addr + oldpage->_offset,
377 oldpage->addr = newbuf;
378 if ((((obd_off)oldpage->index << PAGE_CACHE_SHIFT) +
379 oldpage->_offset + oldpage->_count) > lli->lli_st_size)
380 oldpage->_count += oldpage->_offset;
382 oldpage->_count = PAGE_CACHE_SIZE;
383 oldpage->_offset = 0;
384 oldpage->_managed = 1;
392 int llu_prep_async_io(struct llu_sysio_cookie *cookie, int cmd,
393 char *buf, loff_t pos, size_t count)
395 struct llu_inode_info *lli = llu_i2info(cookie->lsc_inode);
396 struct lov_stripe_md *lsm = lli->lli_smd;
397 struct obd_export *exp = llu_i2obdexp(cookie->lsc_inode);
398 struct page *pages = cookie->lsc_pages;
399 struct ll_async_page *llap = cookie->lsc_llap;
400 int i, rc, npages = 0;
406 /* prepare the pages array */
408 unsigned long index, offset, bytes;
410 offset = (pos & ~PAGE_CACHE_MASK);
411 index = pos >> PAGE_CACHE_SHIFT;
412 bytes = PAGE_CACHE_SIZE - offset;
416 /* prevent read beyond file range */
417 if ((cmd == OBD_BRW_READ) &&
418 (pos + bytes) >= lli->lli_st_size) {
419 if (pos >= lli->lli_st_size)
421 bytes = lli->lli_st_size - pos;
424 /* prepare page for this index */
425 pages[npages].index = index;
426 pages[npages].addr = buf - offset;
428 pages[npages]._offset = offset;
429 pages[npages]._count = bytes;
436 cookie->lsc_rwcount += bytes;
439 cookie->lsc_npages = npages;
441 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
442 if (cmd == OBD_BRW_WRITE) {
443 rc = prepare_unaligned_write(cookie);
449 for (i = 0; i < npages; i++) {
450 llap[i].llap_magic = LLAP_MAGIC;
451 rc = obd_prep_async_page(exp, lsm, NULL, &pages[i],
452 (obd_off)pages[i].index << PAGE_SHIFT,
454 &llap[i], &llap[i].llap_cookie);
456 llap[i].llap_cookie = NULL;
459 CDEBUG(D_CACHE, "llap %p page %p cookie %p obj off "LPU64"\n",
460 &llap[i], &pages[i], llap[i].llap_cookie,
461 (obd_off)pages[i].index << PAGE_SHIFT);
462 pages[i].private = (unsigned long)&llap[i];
463 llap[i].llap_page = &pages[i];
464 llap[i].llap_inode = cookie->lsc_inode;
466 rc = obd_queue_sync_io(exp, lsm, NULL, cookie->lsc_osic,
467 llap[i].llap_cookie, cmd,
468 pages[i]._offset, pages[i]._count, 0);
472 llap[i].llap_queued = 1;
479 int llu_start_async_io(struct llu_sysio_cookie *cookie)
481 struct lov_stripe_md *lsm = llu_i2info(cookie->lsc_inode)->lli_smd;
482 struct obd_export *exp = llu_i2obdexp(cookie->lsc_inode);
484 return obd_trigger_sync_io(exp, lsm, NULL, cookie->lsc_osic);
488 * read/write a continuous buffer for an inode (zero-copy)
490 struct llu_sysio_cookie*
491 llu_rw(int cmd, struct inode *inode, char *buf, size_t count, loff_t pos)
493 struct llu_sysio_cookie *cookie;
497 max_pages = (count >> PAGE_SHIFT) + 2;
499 cookie = get_sysio_cookie(inode, max_pages);
501 RETURN(ERR_PTR(-ENOMEM));
503 rc = llu_prep_async_io(cookie, cmd, buf, pos, count);
505 GOTO(out_cleanup, rc);
507 rc = llu_start_async_io(cookie);
509 GOTO(out_cleanup, rc);
512 rc = osic_wait(&osic);
514 CERROR("file i/o error!\n");
521 put_sysio_cookie(cookie);
525 struct llu_sysio_callback_args*
526 llu_file_write(struct inode *inode, const struct iovec *iovec,
527 size_t iovlen, loff_t pos)
529 struct llu_inode_info *lli = llu_i2info(inode);
530 struct ll_file_data *fd = lli->lli_file_data;
531 struct lustre_handle lockh = {0};
532 struct lov_stripe_md *lsm = lli->lli_smd;
533 struct llu_sysio_callback_args *lsca;
534 struct llu_sysio_cookie *cookie;
535 struct ldlm_extent extent;
540 /* XXX consider other types later */
541 if (!S_ISREG(lli->lli_st_mode))
544 LASSERT(iovlen <= MAX_IOVEC);
546 OBD_ALLOC(lsca, sizeof(*lsca));
548 RETURN(ERR_PTR(-ENOMEM));
550 /* FIXME optimize the following extent locking */
551 for (iovidx = 0; iovidx < iovlen; iovidx++) {
552 char *buf = (char*)iovec[iovidx].iov_base;
553 size_t count = iovec[iovidx].iov_len;
558 /* FIXME libsysio haven't handle O_APPEND */
560 extent.end = pos + count - 1;
562 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
563 if ((pos & ~PAGE_CACHE_MASK) == 0 &&
564 (count & ~PAGE_CACHE_MASK) == 0)
565 err = llu_extent_lock_no_validate(fd, inode, lsm,
566 LCK_PW, &extent, &lockh, 0);
568 err = llu_extent_lock(fd, inode, lsm, LCK_PW,
571 /* server will handle partial write, so we don't
572 * care for file size here */
573 err = llu_extent_lock_no_validate(fd, inode, lsm, LCK_PW,
577 GOTO(err_out, err = -ENOLCK);
579 CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
580 lli->lli_st_ino, count, pos);
582 cookie = llu_rw(OBD_BRW_WRITE, inode, buf, count, pos);
583 if (!IS_ERR(cookie)) {
585 lsca->cookies[lsca->ncookies++] = cookie;
587 /* file size grow. XXX should be done here? */
588 if (pos > lli->lli_st_size) {
589 lli->lli_st_size = pos;
590 set_bit(LLI_F_PREFER_EXTENDED_SIZE,
594 llu_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
595 GOTO(err_out, err = PTR_ERR(cookie));
599 err = llu_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
601 CERROR("extent unlock error %d\n", err);
607 /* teardown all async stuff */
608 while (lsca->ncookies--) {
609 put_sysio_cookie(lsca->cookies[lsca->ncookies]);
611 OBD_FREE(lsca, sizeof(*lsca));
613 RETURN(ERR_PTR(err));
617 static void llu_update_atime(struct inode *inode)
619 struct llu_inode_info *lli = llu_i2info(inode);
624 attr.ia_atime = LTIME_S(CURRENT_TIME);
625 attr.ia_valid = ATTR_ATIME;
627 if (lli->lli_st_atime == attr.ia_atime) return;
628 if (IS_RDONLY(inode)) return;
629 if (IS_NOATIME(inode)) return;
631 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
632 llu_inode_setattr(inode, &attr, 0);
634 /* update atime, but don't explicitly write it out just this change */
635 inode->i_atime = CURRENT_TIME;
640 struct llu_sysio_callback_args*
641 llu_file_read(struct inode *inode, const struct iovec *iovec,
642 size_t iovlen, loff_t pos)
644 struct llu_inode_info *lli = llu_i2info(inode);
645 struct ll_file_data *fd = lli->lli_file_data;
646 struct lov_stripe_md *lsm = lli->lli_smd;
647 struct lustre_handle lockh = { 0 };
648 struct ldlm_extent extent;
649 struct llu_sysio_callback_args *lsca;
650 struct llu_sysio_cookie *cookie;
656 OBD_ALLOC(lsca, sizeof(*lsca));
658 RETURN(ERR_PTR(-ENOMEM));
660 for (iovidx = 0; iovidx < iovlen; iovidx++) {
661 char *buf = iovec[iovidx].iov_base;
662 size_t count = iovec[iovidx].iov_len;
664 /* "If nbyte is 0, read() will return 0 and have no other results."
665 * -- Single Unix Spec */
670 extent.end = pos + count - 1;
672 err = llu_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
674 GOTO(err_out, err = -ENOLCK);
676 CDEBUG(D_INFO, "Reading inode %lu, "LPSZ" bytes, offset %Ld\n",
677 lli->lli_st_ino, count, pos);
679 if (pos >= lli->lli_st_size) {
680 llu_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
684 cookie = llu_rw(OBD_BRW_READ, inode, buf, count, pos);
685 if (!IS_ERR(cookie)) {
687 lsca->cookies[lsca->ncookies++] = cookie;
690 llu_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
691 GOTO(err_out, err = PTR_ERR(cookie));
695 err = llu_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
697 CERROR("extent_unlock fail: %d\n", err);
701 llu_update_atime(inode);
706 /* teardown all async stuff */
707 while (lsca->ncookies--) {
708 put_sysio_cookie(lsca->cookies[lsca->ncookies]);
710 OBD_FREE(lsca, sizeof(*lsca));
712 RETURN(ERR_PTR(err));
715 int llu_iop_iodone(struct ioctx *ioctxp)
717 struct llu_sysio_callback_args *lsca = ioctxp->ioctx_private;
718 struct llu_sysio_cookie *cookie;
719 int i, err = 0, rc = 0;
722 /* write/read(fd, buf, 0) */
724 ioctxp->ioctx_cc = 0;
728 LASSERT(!IS_ERR(lsca));
730 for (i = 0; i < lsca->ncookies; i++) {
731 cookie = lsca->cookies[i];
733 err = osic_wait(cookie->lsc_osic);
737 ioctxp->ioctx_cc += cookie->lsc_rwcount;
738 put_sysio_cookie(cookie);
744 ioctxp->ioctx_cc = -1;
745 ioctxp->ioctx_errno = -rc;
748 OBD_FREE(lsca, sizeof(*lsca));
749 ioctxp->ioctx_private = NULL;