-struct llu_io_group
-{
- struct obd_io_group *lig_oig;
- struct inode *lig_inode;
- struct lustre_rw_params *lig_params;
- int lig_maxpages;
- int lig_npages;
- __u64 lig_rwcount;
- struct ll_async_page *lig_llaps;
- struct page *lig_pages;
- void *lig_llap_cookies;
-};
-
-#define LLU_IO_GROUP_SIZE(x) \
- (sizeof(struct llu_io_group) + \
- (sizeof(struct ll_async_page) + \
- sizeof(struct page) + \
- llap_cookie_size) * (x))
-
-struct llu_io_session
-{
- struct inode *lis_inode;
- int lis_cmd;
- int lis_max_groups;
- int lis_ngroups;
- struct llu_io_group *lis_groups[0];
-};
-#define LLU_IO_SESSION_SIZE(x) \
- (sizeof(struct llu_io_session) + (x) * 2 * sizeof(void *))
-
-
-typedef ssize_t llu_file_piov_t(const struct iovec *iovec, int iovlen,
- _SYSIO_OFF_T pos, ssize_t len,
- void *private);
-
-size_t llap_cookie_size;
-
-static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
-{
- struct llu_inode_info *lli = llu_i2info(inode);
- struct lov_stripe_md *lsm = lli->lli_smd;
- struct obd_export *exp = llu_i2obdexp(inode);
- struct {
- char name[16];
- struct ldlm_lock *lock;
- struct lov_stripe_md *lsm;
- } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
- __u32 stripe, vallen = sizeof(stripe);
- int rc;
- ENTRY;
-
- if (lsm->lsm_stripe_count == 1)
- RETURN(0);
-
- /* get our offset in the lov */
- rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
- if (rc != 0) {
- CERROR("obd_get_info: rc = %d\n", rc);
- LBUG();
- }
- LASSERT(stripe < lsm->lsm_stripe_count);
- RETURN(stripe);
-}
-
-static int llu_extent_lock_callback(struct ldlm_lock *lock,
- struct ldlm_lock_desc *new, void *data,
- int flag)
-{
- struct lustre_handle lockh = { 0 };
- int rc;
- ENTRY;
-
- if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
- LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
- LBUG();
- }
-
- switch (flag) {
- case LDLM_CB_BLOCKING:
- ldlm_lock2handle(lock, &lockh);
- rc = ldlm_cli_cancel(&lockh);
- if (rc != ELDLM_OK)
- CERROR("ldlm_cli_cancel failed: %d\n", rc);
- break;
- case LDLM_CB_CANCELING: {
- struct inode *inode;
- struct llu_inode_info *lli;
- struct lov_stripe_md *lsm;
- __u32 stripe;
- __u64 kms;
-
- /* This lock wasn't granted, don't try to evict pages */
- if (lock->l_req_mode != lock->l_granted_mode)
- RETURN(0);
-
- inode = llu_inode_from_lock(lock);
- if (!inode)
- RETURN(0);
- lli= llu_i2info(inode);
- if (!lli)
- goto iput;
- if (!lli->lli_smd)
- goto iput;
- lsm = lli->lli_smd;
-
- stripe = llu_lock_to_stripe_offset(inode, lock);
- lock_res_and_lock(lock);
- kms = ldlm_extent_shift_kms(lock,
- lsm->lsm_oinfo[stripe]->loi_kms);
- unlock_res_and_lock(lock);
- if (lsm->lsm_oinfo[stripe]->loi_kms != kms)
- LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
- lsm->lsm_oinfo[stripe]->loi_kms, kms);
- lsm->lsm_oinfo[stripe]->loi_kms = kms;
-iput:
- I_RELE(inode);
- break;
- }
- default:
- LBUG();
- }
-
- RETURN(0);
-}
-
-static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
-{
- struct ptlrpc_request *req = reqp;
- struct inode *inode = llu_inode_from_lock(lock);
- struct llu_inode_info *lli;
- struct ost_lvb *lvb;
- int size[2] = { sizeof(struct ptlrpc_body), sizeof(*lvb) };
- int rc, stripe = 0;
- ENTRY;
-
- if (inode == NULL)
- GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
- lli = llu_i2info(inode);
- if (lli == NULL)
- GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
- if (lli->lli_smd == NULL)
- GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
-
- /* First, find out which stripe index this lock corresponds to. */
- if (lli->lli_smd->lsm_stripe_count > 1)
- stripe = llu_lock_to_stripe_offset(inode, lock);
-
- rc = lustre_pack_reply(req, 2, size, NULL);
- if (rc) {
- CERROR("lustre_pack_reply: %d\n", rc);
- GOTO(iput, rc);
- }
-
- lvb = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*lvb));
- lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe]->loi_kms;
-
- LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
- (__u64)llu_i2stat(inode)->st_size, stripe,lvb->lvb_size);
- iput:
- I_RELE(inode);
- out:
- /* These errors are normal races, so we don't want to fill the console
- * with messages by calling ptlrpc_error() */
- if (rc == -ELDLM_NO_LOCK_DATA)
- lustre_pack_reply(req, 1, NULL, NULL);
-
- req->rq_status = rc;
- return rc;
-}
-
-static void llu_merge_lvb(struct inode *inode)
-{
- struct llu_inode_info *lli = llu_i2info(inode);
- struct llu_sb_info *sbi = llu_i2sbi(inode);
- struct intnl_stat *st = llu_i2stat(inode);
- struct ost_lvb lvb;
- ENTRY;
-
- inode_init_lvb(inode, &lvb);
- obd_merge_lvb(sbi->ll_dt_exp, lli->lli_smd, &lvb, 0);
- st->st_size = lvb.lvb_size;
- st->st_blocks = lvb.lvb_blocks;
- st->st_mtime = lvb.lvb_mtime;
- st->st_atime = lvb.lvb_atime;
- st->st_ctime = lvb.lvb_ctime;
- EXIT;
-}
-
-int llu_local_size(struct inode *inode)
-{
- ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
- struct llu_inode_info *lli = llu_i2info(inode);
- struct llu_sb_info *sbi = llu_i2sbi(inode);
- struct lustre_handle lockh = { 0 };
- int flags = 0;
- int rc;
- ENTRY;
-
- if (lli->lli_smd->lsm_stripe_count == 0)
- RETURN(0);
-
- rc = obd_match(sbi->ll_dt_exp, lli->lli_smd, LDLM_EXTENT,
- &policy, LCK_PR | LCK_PW, &flags, inode, &lockh);
- if (rc < 0)
- RETURN(rc);
- else if (rc == 0)
- RETURN(-ENODATA);
-
- llu_merge_lvb(inode);
- obd_cancel(sbi->ll_dt_exp, lli->lli_smd, LCK_PR, &lockh);
- RETURN(0);
-}
-
-/* NB: lov_merge_size will prefer locally cached writes if they extend the
- * file (because it prefers KMS over RSS when larger) */
-int llu_glimpse_size(struct inode *inode)
-{
- struct llu_inode_info *lli = llu_i2info(inode);
- struct intnl_stat *st = llu_i2stat(inode);
- struct llu_sb_info *sbi = llu_i2sbi(inode);
- struct lustre_handle lockh = { 0 };
- struct ldlm_enqueue_info einfo = { 0 };
- struct obd_info oinfo = { { { 0 } } };
- int rc;
- ENTRY;
-
- /* If size is cached on the mds, skip glimpse. */
- if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
- RETURN(0);
-
- CDEBUG(D_DLMTRACE, "Glimpsing inode "LPU64"\n", (__u64)st->st_ino);
-
- if (!lli->lli_smd) {
- CDEBUG(D_DLMTRACE, "No objects for inode "LPU64"\n",
- (__u64)st->st_ino);
- RETURN(0);
- }
-
- einfo.ei_type = LDLM_EXTENT;
- einfo.ei_mode = LCK_PR;
- einfo.ei_cb_bl = llu_extent_lock_callback;
- einfo.ei_cb_cp = ldlm_completion_ast;
- einfo.ei_cb_gl = llu_glimpse_callback;
- einfo.ei_cbdata = inode;
-
- oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
- oinfo.oi_lockh = &lockh;
- oinfo.oi_md = lli->lli_smd;
- oinfo.oi_flags = LDLM_FL_HAS_INTENT;
-
- rc = obd_enqueue_rqset(sbi->ll_dt_exp, &oinfo, &einfo);
- if (rc) {
- CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
- RETURN(rc > 0 ? -EIO : rc);
- }
-
- llu_merge_lvb(inode);
- CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64", blocks: "LPU64"\n",
- (__u64)st->st_size, (__u64)st->st_blocks);
-
- RETURN(rc);
-}
-
-int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
- struct lov_stripe_md *lsm, int mode,
- ldlm_policy_data_t *policy, struct lustre_handle *lockh,
- int ast_flags)
-{
- struct llu_sb_info *sbi = llu_i2sbi(inode);
- struct intnl_stat *st = llu_i2stat(inode);
- struct ldlm_enqueue_info einfo = { 0 };
- struct obd_info oinfo = { { { 0 } } };
- struct ost_lvb lvb;
- int rc;
- ENTRY;
-
- LASSERT(!lustre_handle_is_used(lockh));
- CLASSERT(ELDLM_OK == 0);
-
- /* XXX phil: can we do this? won't it screw the file size up? */
- if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
- (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
- RETURN(0);
-
- CDEBUG(D_DLMTRACE, "Locking inode %llu, start "LPU64" end "LPU64"\n",
- (__u64)st->st_ino, policy->l_extent.start,
- policy->l_extent.end);
-
- einfo.ei_type = LDLM_EXTENT;
- einfo.ei_mode = mode;
- einfo.ei_cb_bl = llu_extent_lock_callback;
- einfo.ei_cb_cp = ldlm_completion_ast;
- einfo.ei_cb_gl = llu_glimpse_callback;
- einfo.ei_cbdata = inode;
-
- oinfo.oi_policy = *policy;
- oinfo.oi_lockh = lockh;
- oinfo.oi_md = lsm;
- oinfo.oi_flags = ast_flags;
-
- rc = obd_enqueue(sbi->ll_dt_exp, &oinfo, &einfo, NULL);
- *policy = oinfo.oi_policy;
- if (rc > 0)
- rc = -EIO;
-
- inode_init_lvb(inode, &lvb);
- obd_merge_lvb(sbi->ll_dt_exp, lsm, &lvb, 1);
- if (policy->l_extent.start == 0 &&
- policy->l_extent.end == OBD_OBJECT_EOF)
- st->st_size = lvb.lvb_size;
-
- if (rc == 0) {
- st->st_mtime = lvb.lvb_mtime;
- st->st_atime = lvb.lvb_atime;
- st->st_ctime = lvb.lvb_ctime;
- }
-
- RETURN(rc);
-}
-
-int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
- struct lov_stripe_md *lsm, int mode,
- struct lustre_handle *lockh)
-{
- struct llu_sb_info *sbi = llu_i2sbi(inode);
- int rc;
- ENTRY;
-
- CLASSERT(ELDLM_OK == 0);
-
- /* XXX phil: can we do this? won't it screw the file size up? */
- if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
- (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
- RETURN(0);
-
- rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
-
- RETURN(rc);
-}
-
-#define LLAP_MAGIC 12346789
-
-struct ll_async_page {
- int llap_magic;
- void *llap_cookie;
- int llap_queued;
- struct page *llap_page;
- struct inode *llap_inode;
-};
-
-static void llu_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
-{
- struct ll_async_page *llap;
- struct inode *inode;
- struct lov_stripe_md *lsm;
- obd_flag valid_flags;
- ENTRY;
-
- llap = LLAP_FROM_COOKIE(data);
- inode = llap->llap_inode;
- lsm = llu_i2info(inode)->lli_smd;
-
- oa->o_id = lsm->lsm_object_id;
- oa->o_valid = OBD_MD_FLID;
- valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
- if (cmd & OBD_BRW_WRITE)
- valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME |
- OBD_MD_FLUID | OBD_MD_FLGID |
- OBD_MD_FLFID | OBD_MD_FLGENER;
-
- obdo_from_inode(oa, inode, valid_flags);
- EXIT;
-}
-
-static void llu_ap_update_obdo(void *data, int cmd, struct obdo *oa,
- obd_valid valid)
-{
- struct ll_async_page *llap;
- ENTRY;
-
- llap = LLAP_FROM_COOKIE(data);
- obdo_from_inode(oa, llap->llap_inode, valid);
-
- EXIT;
-}
-
-/* called for each page in a completed rpc.*/
-static int llu_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
-{
- struct ll_async_page *llap;
- struct page *page;
- ENTRY;
-
- llap = LLAP_FROM_COOKIE(data);
- llap->llap_queued = 0;
- page = llap->llap_page;
-
- if (rc != 0) {
- if (cmd & OBD_BRW_WRITE)
- CERROR("writeback error on page %p index %ld: %d\n",
- page, page->index, rc);
- }
- RETURN(0);
-}
-
-static struct obd_async_page_ops llu_async_page_ops = {
- .ap_make_ready = NULL,
- .ap_refresh_count = NULL,
- .ap_fill_obdo = llu_ap_fill_obdo,
- .ap_update_obdo = llu_ap_update_obdo,
- .ap_completion = llu_ap_completion,
-};
-
-static int llu_queue_pio(int cmd, struct llu_io_group *group,
- char *buf, size_t count, loff_t pos)
-{
- struct llu_inode_info *lli = llu_i2info(group->lig_inode);
- struct intnl_stat *st = llu_i2stat(group->lig_inode);
- struct lov_stripe_md *lsm = lli->lli_smd;
- struct obd_export *exp = llu_i2obdexp(group->lig_inode);
- struct page *pages = &group->lig_pages[group->lig_npages],*page = pages;
- struct ll_async_page *llap = &group->lig_llaps[group->lig_npages];
- void *llap_cookie = group->lig_llap_cookies +
- llap_cookie_size * group->lig_npages;
- int i, rc, npages = 0, ret_bytes = 0;
- int local_lock;
- ENTRY;
-
- if (!exp)
- RETURN(-EINVAL);
-
- local_lock = group->lig_params->lrp_lock_mode != LCK_NL;
- /* prepare the pages array */
- do {
- unsigned long index, offset, bytes;
-
- offset = (pos & ~CFS_PAGE_MASK);
- index = pos >> CFS_PAGE_SHIFT;
- bytes = CFS_PAGE_SIZE - offset;
- if (bytes > count)
- bytes = count;
-
- /* prevent read beyond file range */
- if (/* local_lock && */
- cmd == OBD_BRW_READ && pos + bytes >= st->st_size) {
- if (pos >= st->st_size)
- break;
- bytes = st->st_size - pos;
- }
-
- /* prepare page for this index */
- page->index = index;
- page->addr = buf - offset;
-
- page->_offset = offset;
- page->_count = bytes;
-
- page++;
- npages++;
- count -= bytes;
- pos += bytes;
- buf += bytes;
-
- group->lig_rwcount += bytes;
- ret_bytes += bytes;
- } while (count);
-
- group->lig_npages += npages;
-
- for (i = 0, page = pages; i < npages;
- i++, page++, llap++, llap_cookie += llap_cookie_size){
- llap->llap_magic = LLAP_MAGIC;
- llap->llap_cookie = llap_cookie;
- rc = obd_prep_async_page(exp, lsm, NULL, page,
- (obd_off)page->index << CFS_PAGE_SHIFT,
- &llu_async_page_ops,
- llap, &llap->llap_cookie);
- if (rc) {
- LASSERT(rc < 0);
- llap->llap_cookie = NULL;
- RETURN(rc);
- }
- CDEBUG(D_CACHE, "llap %p page %p group %p obj off "LPU64"\n",
- llap, page, llap->llap_cookie,
- (obd_off)pages->index << CFS_PAGE_SHIFT);
- page->private = (unsigned long)llap;
- llap->llap_page = page;
- llap->llap_inode = group->lig_inode;
-
- rc = obd_queue_group_io(exp, lsm, NULL, group->lig_oig,
- llap->llap_cookie, cmd,
- page->_offset, page->_count,
- group->lig_params->lrp_brw_flags,
- ASYNC_READY | ASYNC_URGENT |
- ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
- if (!local_lock && cmd == OBD_BRW_READ) {
- /*
- * In OST-side locking case short reads cannot be
- * detected properly.
- *
- * The root of the problem is that
- *
- * kms = lov_merge_size(lsm, 1);
- * if (end >= kms)
- * glimpse_size(inode);
- * else
- * st->st_size = kms;
- *
- * logic in the read code (both llite and liblustre)
- * only works correctly when client holds DLM lock on
- * [start, end]. Without DLM lock KMS can be
- * completely out of date, and client can either make
- * spurious short-read (missing concurrent write), or
- * return stale data (missing concurrent
- * truncate). For llite client this is fatal, because
- * incorrect data are cached and can be later sent
- * back to the server (vide bug 5047). This is hard to
- * fix by handling short-reads on the server, as there
- * is no easy way to communicate file size (or amount
- * of bytes read/written) back to the client,
- * _especially_ because OSC pages can be sliced and
- * dices into multiple RPCs arbitrary. Fortunately,
- * liblustre doesn't cache data and the worst case is
- * that we get race with concurrent write or truncate.
- */
- }
- if (rc) {
- LASSERT(rc < 0);
- RETURN(rc);
- }
-
- llap->llap_queued = 1;
- }
-
- RETURN(ret_bytes);
-}
-
-static
-struct llu_io_group * get_io_group(struct inode *inode, int maxpages,
- struct lustre_rw_params *params)
-{
- struct llu_io_group *group;
- int rc;
-
- if (!llap_cookie_size)
- llap_cookie_size = obd_prep_async_page(llu_i2obdexp(inode),
- NULL, NULL, NULL, 0,
- NULL, NULL, NULL);
-
- OBD_ALLOC(group, LLU_IO_GROUP_SIZE(maxpages));
- if (!group)
- return ERR_PTR(-ENOMEM);
-
- I_REF(inode);
- group->lig_inode = inode;
- group->lig_maxpages = maxpages;
- group->lig_params = params;
- group->lig_llaps = (struct ll_async_page *)(group + 1);
- group->lig_pages = (struct page *)(&group->lig_llaps[maxpages]);
- group->lig_llap_cookies = (void *)(&group->lig_pages[maxpages]);
-
- rc = oig_init(&group->lig_oig);
- if (rc) {
- OBD_FREE(group, LLU_IO_GROUP_SIZE(maxpages));
- return ERR_PTR(rc);
- }
-
- return group;
-}
-
-static int max_io_pages(ssize_t len, int iovlen)