Whamcloud - gitweb
- landing of b_hd_cleanup_merge to HEAD.
[fs/lustre-release.git] / lustre / liblustre / rw.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light block IO
5  *
6  *  Copyright (c) 2002-2004 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
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.
13  *
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.
18  *
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.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <sys/queue.h>
32 #include <fcntl.h>
33 #include <sys/uio.h>
34
35 #include <xtio.h>
36 #include <sysio.h>
37 #include <fs.h>
38 #include <mount.h>
39 #include <inode.h>
40 #include <file.h>
41
42 #undef LIST_HEAD
43
44 #include "llite_lib.h"
45
46 struct llu_io_group
47 {
48         struct obd_io_group    *lig_oig;
49         struct inode           *lig_inode;
50         int                     lig_maxpages;
51         int                     lig_npages;
52         __u64                   lig_rwcount;
53         struct ll_async_page   *lig_llap;
54         struct page            *lig_pages;
55 };
56
57 #define LLU_IO_GROUP_SIZE(x) \
58         (sizeof(struct llu_io_group) + \
59          sizeof(struct ll_async_page) * (x) + \
60          sizeof(struct page) * (x))
61
62 struct llu_io_session
63 {
64         struct inode           *lis_inode;
65         int                     lis_cmd;
66         int                     lis_max_groups;
67         int                     lis_ngroups;
68         struct llu_io_group    *lis_groups[0];
69 };
70 #define LLU_IO_SESSION_SIZE(x)  \
71         (sizeof(struct llu_io_session) + (x) * 2 * sizeof(void *))
72
73
74 typedef ssize_t llu_file_piov_t(const struct iovec *iovec, int iovlen,
75                                 _SYSIO_OFF_T pos, ssize_t len,
76                                 void *private);
77
78 static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
79 {
80         struct llu_inode_info *lli = llu_i2info(inode);
81         struct lov_stripe_md *lsm = lli->lli_smd;
82         struct obd_export *exp = llu_i2obdexp(inode);
83         struct {
84                 char name[16];
85                 struct ldlm_lock *lock;
86                 struct lov_stripe_md *lsm;
87         } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
88         __u32 stripe, vallen = sizeof(stripe);
89         int rc;
90         ENTRY;
91
92         if (lsm->lsm_stripe_count == 1)
93                 RETURN(0);
94
95         /* get our offset in the lov */
96         rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
97         if (rc != 0) {
98                 CERROR("obd_get_info: rc = %d\n", rc);
99                 LBUG();
100         }
101         LASSERT(stripe < lsm->lsm_stripe_count);
102         RETURN(stripe);
103 }
104
105 static int llu_extent_lock_callback(struct ldlm_lock *lock,
106                                     struct ldlm_lock_desc *new, void *data,
107                                     int flag)
108 {
109         struct lustre_handle lockh = { 0 };
110         int rc;
111         ENTRY;
112
113         if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
114                 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
115                 LBUG();
116         }
117
118         switch (flag) {
119         case LDLM_CB_BLOCKING:
120                 ldlm_lock2handle(lock, &lockh);
121                 rc = ldlm_cli_cancel(&lockh);
122                 if (rc != ELDLM_OK)
123                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
124                 break;
125         case LDLM_CB_CANCELING: {
126                 struct inode *inode;
127                 struct llu_inode_info *lli;
128                 struct lov_stripe_md *lsm;
129                 __u32 stripe;
130                 __u64 kms;
131
132                 /* This lock wasn't granted, don't try to evict pages */
133                 if (lock->l_req_mode != lock->l_granted_mode)
134                         RETURN(0);
135
136                 inode = llu_inode_from_lock(lock);
137                 if (!inode)
138                         RETURN(0);
139                 lli= llu_i2info(inode);
140                 if (!lli)
141                         goto iput;
142                 if (!lli->lli_smd)
143                         goto iput;
144                 lsm = lli->lli_smd;
145
146                 stripe = llu_lock_to_stripe_offset(inode, lock);
147                 l_lock(&lock->l_resource->lr_namespace->ns_lock);
148                 kms = ldlm_extent_shift_kms(lock,
149                                             lsm->lsm_oinfo[stripe].loi_kms);
150                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
151                 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
152                         LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
153                                    lsm->lsm_oinfo[stripe].loi_kms, kms);
154                 lsm->lsm_oinfo[stripe].loi_kms = kms;
155 iput:
156                 I_RELE(inode);
157                 break;
158         }
159         default:
160                 LBUG();
161         }
162
163         RETURN(0);
164 }
165
166 static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
167 {
168         struct ptlrpc_request *req = reqp;
169         struct inode *inode = llu_inode_from_lock(lock);
170         struct llu_inode_info *lli;
171         struct ost_lvb *lvb;
172         int rc, size = sizeof(*lvb), stripe = 0;
173         ENTRY;
174
175         if (inode == NULL)
176                 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
177         lli = llu_i2info(inode);
178         if (lli == NULL)
179                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
180         if (lli->lli_smd == NULL)
181                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
182
183         /* First, find out which stripe index this lock corresponds to. */
184         if (lli->lli_smd->lsm_stripe_count > 1)
185                 stripe = llu_lock_to_stripe_offset(inode, lock);
186
187         rc = lustre_pack_reply(req, 1, &size, NULL);
188         if (rc) {
189                 CERROR("lustre_pack_reply: %d\n", rc);
190                 GOTO(iput, rc);
191         }
192
193         lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
194         lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
195
196         LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
197                    lli->lli_st_size, stripe, lvb->lvb_size);
198  iput:
199         I_RELE(inode);
200  out:
201         /* These errors are normal races, so we don't want to fill the console
202          * with messages by calling ptlrpc_error() */
203         if (rc == -ELDLM_NO_LOCK_DATA)
204                 lustre_pack_reply(req, 0, NULL, NULL);
205
206         req->rq_status = rc;
207         return rc;
208 }
209
210 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms_only);
211 __u64 lov_merge_blocks(struct lov_stripe_md *lsm);
212 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
213
214 /* NB: lov_merge_size will prefer locally cached writes if they extend the
215  * file (because it prefers KMS over RSS when larger) */
216 int llu_glimpse_size(struct inode *inode)
217 {
218         struct llu_inode_info *lli = llu_i2info(inode);
219         struct llu_sb_info *sbi = llu_i2sbi(inode);
220         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
221         struct lustre_handle lockh = { 0 };
222         int rc, flags = LDLM_FL_HAS_INTENT;
223         ENTRY;
224
225         CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", lli->lli_st_ino);
226
227         rc = obd_enqueue(sbi->ll_osc_exp, lli->lli_smd, LDLM_EXTENT, &policy,
228                          LCK_PR, &flags, llu_extent_lock_callback,
229                          ldlm_completion_ast, llu_glimpse_callback, inode,
230                          sizeof(struct ost_lvb), lustre_swab_ost_lvb, &lockh);
231         if (rc) {
232                 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
233                 RETURN(rc > 0 ? -EIO : rc);
234         }
235
236         lli->lli_st_size = lov_merge_size(lli->lli_smd, 0);
237         lli->lli_st_blocks = lov_merge_blocks(lli->lli_smd);
238         //lli->lli_st_mtime = lov_merge_mtime(lli->lli_smd, inode->i_mtime);
239
240         CDEBUG(D_DLMTRACE, "glimpse: size: %llu, blocks: %lu\n",
241                lli->lli_st_size, lli->lli_st_blocks);
242
243         obd_cancel(sbi->ll_osc_exp, lli->lli_smd, LCK_PR, &lockh);
244
245         RETURN(rc);
246 }
247
248 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
249                     struct lov_stripe_md *lsm, int mode,
250                     ldlm_policy_data_t *policy, struct lustre_handle *lockh,
251                     int ast_flags)
252 {
253         struct llu_sb_info *sbi = llu_i2sbi(inode);
254         struct llu_inode_info *lli = llu_i2info(inode);
255         int rc;
256         ENTRY;
257
258         LASSERT(lockh->cookie == 0);
259
260         /* XXX phil: can we do this?  won't it screw the file size up? */
261         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
262             (sbi->ll_flags & LL_SBI_NOLCK))
263                 RETURN(0);
264
265         CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
266                lli->lli_st_ino, policy->l_extent.start, policy->l_extent.end);
267
268         rc = obd_enqueue(sbi->ll_osc_exp, lsm, LDLM_EXTENT, policy, mode,
269                          &ast_flags, llu_extent_lock_callback,
270                          ldlm_completion_ast, llu_glimpse_callback, inode,
271                          sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
272         if (rc > 0)
273                 rc = -EIO;
274
275         if (policy->l_extent.start == 0 &&
276             policy->l_extent.end == OBD_OBJECT_EOF)
277                 lli->lli_st_size = lov_merge_size(lsm, 1);
278
279         //inode->i_mtime = lov_merge_mtime(lsm, inode->i_mtime);
280
281         RETURN(rc);
282 }
283
284 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
285                 struct lov_stripe_md *lsm, int mode,
286                 struct lustre_handle *lockh)
287 {
288         struct llu_sb_info *sbi = llu_i2sbi(inode);
289         int rc;
290         ENTRY;
291
292         /* XXX phil: can we do this?  won't it screw the file size up? */
293         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
294             (sbi->ll_flags & LL_SBI_NOLCK))
295                 RETURN(0);
296
297         rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
298
299         RETURN(rc);
300 }
301
302 #define LLAP_MAGIC 12346789
303
304 struct ll_async_page {
305         int             llap_magic;
306         void           *llap_cookie;
307         int             llap_queued;
308         struct page    *llap_page;
309         struct inode   *llap_inode;
310 };
311
312 static struct ll_async_page *llap_from_cookie(void *cookie)
313 {
314         struct ll_async_page *llap = cookie;
315         if (llap->llap_magic != LLAP_MAGIC)
316                 return ERR_PTR(-EINVAL);
317         return llap;
318 };
319
320 static void llu_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
321 {
322         struct ll_async_page *llap;
323         struct inode *inode;
324         struct lov_stripe_md *lsm;
325         obd_flag valid_flags;
326         ENTRY;
327
328         llap = llap_from_cookie(data);
329         if (IS_ERR(llap)) {
330                 EXIT;
331                 return;
332         }
333
334         inode = llap->llap_inode;
335         lsm = llu_i2info(inode)->lli_smd;
336
337         oa->o_id = lsm->lsm_object_id;
338         oa->o_valid = OBD_MD_FLID;
339         valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
340         if (cmd == OBD_BRW_WRITE)
341                 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
342
343         obdo_from_inode(oa, inode, valid_flags);
344         EXIT;
345 }
346
347 /* called for each page in a completed rpc.*/
348 static void llu_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
349 {
350         struct ll_async_page *llap;
351         struct page *page;
352
353         llap = llap_from_cookie(data);
354         if (IS_ERR(llap)) {
355                 EXIT;
356                 return;
357         }
358
359         llap->llap_queued = 0;
360         page = llap->llap_page;
361
362         if (rc != 0) {
363                 if (cmd == OBD_BRW_WRITE)
364                         CERROR("writeback error on page %p index %ld: %d\n", 
365                                page, page->index, rc);
366         }
367         EXIT;
368 }
369
370 static struct obd_async_page_ops llu_async_page_ops = {
371         .ap_make_ready =        NULL,
372         .ap_refresh_count =     NULL,
373         .ap_fill_obdo =         llu_ap_fill_obdo,
374         .ap_completion =        llu_ap_completion,
375 };
376
377 static int llu_queue_pio(int cmd, struct llu_io_group *group,
378                          char *buf, size_t count, loff_t pos)
379 {
380         struct llu_inode_info *lli = llu_i2info(group->lig_inode);
381         struct lov_stripe_md *lsm = lli->lli_smd;
382         struct obd_export *exp = llu_i2obdexp(group->lig_inode);
383         struct page *pages = &group->lig_pages[group->lig_npages];
384         struct ll_async_page *llap = &group->lig_llap[group->lig_npages];
385         int i, rc, npages = 0, ret_bytes = 0;
386         ENTRY;
387
388         if (!exp)
389                 RETURN(-EINVAL);
390
391         /* prepare the pages array */
392         do {
393                 unsigned long index, offset, bytes;
394
395                 offset = (pos & ~PAGE_CACHE_MASK);
396                 index = pos >> PAGE_CACHE_SHIFT;
397                 bytes = PAGE_CACHE_SIZE - offset;
398                 if (bytes > count)
399                         bytes = count;
400
401                 /* prevent read beyond file range */
402                 if ((cmd == OBD_BRW_READ) &&
403                     (pos + bytes) >= lli->lli_st_size) {
404                         if (pos >= lli->lli_st_size)
405                                 break;
406                         bytes = lli->lli_st_size - pos;
407                 }
408
409                 /* prepare page for this index */
410                 pages[npages].index = index;
411                 pages[npages].addr = buf - offset;
412
413                 pages[npages]._offset = offset;
414                 pages[npages]._count = bytes;
415
416                 npages++;
417                 count -= bytes;
418                 pos += bytes;
419                 buf += bytes;
420
421                 group->lig_rwcount += bytes;
422                 ret_bytes += bytes;
423         } while (count);
424
425         group->lig_npages += npages;
426
427         for (i = 0; i < npages; i++) {
428                 llap[i].llap_magic = LLAP_MAGIC;
429                 rc = obd_prep_async_page(exp, lsm, NULL, &pages[i],
430                                          (obd_off)pages[i].index << PAGE_SHIFT,
431                                          &llu_async_page_ops,
432                                          &llap[i], &llap[i].llap_cookie);
433                 if (rc) {
434                         LASSERT(rc < 0);
435                         llap[i].llap_cookie = NULL;
436                         RETURN(rc);
437                 }
438                 CDEBUG(D_CACHE, "llap %p page %p group %p obj off "LPU64"\n",
439                        &llap[i], &pages[i], llap[i].llap_cookie,
440                        (obd_off)pages[i].index << PAGE_SHIFT);
441                 pages[i].private = (unsigned long)&llap[i];
442                 llap[i].llap_page = &pages[i];
443                 llap[i].llap_inode = group->lig_inode;
444
445                 rc = obd_queue_group_io(exp, lsm, NULL, group->lig_oig,
446                                         llap[i].llap_cookie, cmd,
447                                         pages[i]._offset, pages[i]._count, 0,
448                                         ASYNC_READY | ASYNC_URGENT |
449                                         ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
450                 if (rc) {
451                         LASSERT(rc < 0);
452                         RETURN(rc);
453                 }
454
455                 llap[i].llap_queued = 1;
456         }
457
458         RETURN(ret_bytes);
459 }
460
461 static
462 struct llu_io_group * get_io_group(struct inode *inode, int maxpages)
463 {
464         struct llu_io_group *group;
465         int rc;
466
467         OBD_ALLOC(group, LLU_IO_GROUP_SIZE(maxpages));
468         if (!group)
469                 return ERR_PTR(-ENOMEM);
470
471         I_REF(inode);
472         group->lig_inode = inode;
473         group->lig_maxpages = maxpages;
474         group->lig_llap = (struct ll_async_page *)(group + 1);
475         group->lig_pages = (struct page *) (group->lig_llap + maxpages);
476
477         rc = oig_init(&group->lig_oig);
478         if (rc) {
479                 OBD_FREE(group, LLU_IO_GROUP_SIZE(maxpages));
480                 return ERR_PTR(rc);
481         }
482
483         return group;
484 }
485
486 static int max_io_pages(ssize_t len, int iovlen)
487 {
488         return (((len + PAGE_SIZE -1) / PAGE_SIZE) + 2 + iovlen - 1);
489 }
490
491 static
492 void put_io_group(struct llu_io_group *group)
493 {
494         struct lov_stripe_md *lsm = llu_i2info(group->lig_inode)->lli_smd;
495         struct obd_export *exp = llu_i2obdexp(group->lig_inode);
496         struct ll_async_page *llap = group->lig_llap;
497         int i;
498
499         for (i = 0; i< group->lig_npages; i++) {
500                 if (llap[i].llap_cookie)
501                         obd_teardown_async_page(exp, lsm, NULL,
502                                                 llap[i].llap_cookie);
503         }
504
505         I_RELE(group->lig_inode);
506
507         oig_release(group->lig_oig);
508         OBD_FREE(group, LLU_IO_GROUP_SIZE(group->lig_maxpages));
509 }
510
511 void lov_increase_kms(struct obd_export *exp, struct lov_stripe_md *lsm,
512                       obd_off size);
513
514 static
515 ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
516                         _SYSIO_OFF_T pos, ssize_t len,
517                         void *private)
518 {
519         struct llu_io_session *session = (struct llu_io_session *) private;
520         struct inode *inode = session->lis_inode;
521         struct llu_inode_info *lli = llu_i2info(inode);
522         struct ll_file_data *fd = lli->lli_file_data;
523         struct lustre_handle lockh = {0};
524         struct lov_stripe_md *lsm = lli->lli_smd;
525         struct obd_export *exp = NULL;
526         ldlm_policy_data_t policy;
527         struct llu_io_group *iogroup;
528         int astflag = (lli->lli_open_flags & O_NONBLOCK) ?
529                        LDLM_FL_BLOCK_NOWAIT : 0;
530         __u64 kms;
531         int err, is_read, lock_mode, iovidx, ret;
532         ENTRY;
533
534         /* in a large iov read/write we'll be repeatedly called.
535          * so give a chance to answer cancel ast here
536          */
537         liblustre_wait_event(0);
538
539         exp = llu_i2obdexp(inode);
540         if (exp == NULL)
541                 RETURN(-EINVAL);
542
543         if (len == 0 || iovlen == 0)
544                 RETURN(0);
545
546         if (pos + len > lli->lli_maxbytes)
547                 RETURN(-ERANGE);
548
549         iogroup = get_io_group(inode, max_io_pages(len, iovlen));
550         if (IS_ERR(iogroup))
551                 RETURN(PTR_ERR(iogroup));
552
553         is_read = session->lis_cmd == OBD_BRW_READ;
554         lock_mode = is_read ? LCK_PR : LCK_PW;
555
556         if (!is_read && (lli->lli_open_flags & O_APPEND)) {
557                 policy.l_extent.start = 0;
558                 policy.l_extent.end = OBD_OBJECT_EOF;
559         } else {
560                 policy.l_extent.start = pos;
561                 policy.l_extent.end = pos + len - 1;
562         }
563
564         err = llu_extent_lock(fd, inode, lsm, lock_mode, &policy,
565                               &lockh, astflag);
566         if (err != ELDLM_OK)
567                 GOTO(err_put, err);
568
569         if (is_read) {
570                 kms = lov_merge_size(lsm, 1);
571                 if (policy.l_extent.end > kms) {
572                         /* A glimpse is necessary to determine whether we
573                          * return a short read or some zeroes at the end of
574                          * the buffer */
575                         if ((err = llu_glimpse_size(inode))) {
576                                 llu_extent_unlock(fd, inode, lsm,
577                                                   lock_mode, &lockh);
578                                 GOTO(err_put, err);
579                         }
580                 } else {
581                         lli->lli_st_size = kms;
582                 }
583         } else {
584                 if (lli->lli_open_flags & O_APPEND)
585                         pos = lli->lli_st_size;
586         }
587
588         for (iovidx = 0; iovidx < iovlen; iovidx++) {
589                 char *buf = (char *) iovec[iovidx].iov_base;
590                 size_t count = iovec[iovidx].iov_len;
591
592                 if (!count)
593                         continue;
594                 if (len < count)
595                         count = len;
596                 if (IS_BAD_PTR(buf) || IS_BAD_PTR(buf + count)) {
597                         llu_extent_unlock(fd, inode, lsm, lock_mode, &lockh);
598                         GOTO(err_put, err = -EFAULT);
599                 }
600
601                 if (is_read) {
602                         if (pos >= lli->lli_st_size)
603                                 break;
604                 } else {
605                         if (pos >= lli->lli_maxbytes) {
606                                 llu_extent_unlock(fd, inode, lsm, lock_mode,
607                                                   &lockh);
608                                 GOTO(err_put, err = -EFBIG);
609                         }
610                         if (pos + count >= lli->lli_maxbytes)
611                                 count = lli->lli_maxbytes - pos;
612                 }
613
614                 ret = llu_queue_pio(session->lis_cmd, iogroup, buf, count, pos);
615                 if (ret < 0) {
616                         llu_extent_unlock(fd, inode, lsm, lock_mode, &lockh);
617                         GOTO(err_put, err = ret);
618                 } else {
619                         pos += ret;
620                         if (!is_read) {
621                                 LASSERT(ret == count);
622                                 lov_increase_kms(exp, lsm, pos);
623                                 /* file size grow immediately */
624                                 if (pos > lli->lli_st_size)
625                                         lli->lli_st_size = pos;
626                         }
627                         len -= ret;
628                         if (!len)
629                                 break;
630                 }
631         }
632         LASSERT(len == 0 || is_read); /* libsysio should guarantee this */
633
634         err = llu_extent_unlock(fd, inode, lsm, lock_mode, &lockh);
635         if (err)
636                 CERROR("extent unlock error %d\n", err);
637
638         err = obd_trigger_group_io(exp, lsm, NULL, iogroup->lig_oig);
639         if (err)
640                 GOTO(err_put, err);
641
642         session->lis_groups[session->lis_ngroups++] = iogroup;
643         RETURN(0);
644 err_put:
645         put_io_group(iogroup);
646         RETURN((ssize_t)err);
647 }
648
649 static
650 struct llu_io_session *get_io_session(struct inode *ino, int ngroups, int cmd)
651 {
652         struct llu_io_session *session;
653
654         OBD_ALLOC(session, LLU_IO_SESSION_SIZE(ngroups));
655         if (!session)
656                 return NULL;
657
658         I_REF(ino);
659         session->lis_inode = ino;
660         session->lis_max_groups = ngroups;
661         session->lis_cmd = cmd;
662         return session;
663 }
664
665 static void put_io_session(struct llu_io_session *session)
666 {
667         int i;
668
669         for (i = 0; i < session->lis_ngroups; i++) {
670                 if (session->lis_groups[i]) {
671                         put_io_group(session->lis_groups[i]);
672                         session->lis_groups[i] = NULL;
673                 }
674         }
675
676         I_RELE(session->lis_inode);
677         OBD_FREE(session, LLU_IO_SESSION_SIZE(session->lis_max_groups));
678 }
679
680 static int llu_file_rwx(struct inode *ino,
681                         struct ioctx *ioctx,
682                         int read)
683 {
684         struct llu_io_session *session;
685         ssize_t cc;
686         int cmd = read ? OBD_BRW_READ : OBD_BRW_WRITE;
687         ENTRY;
688
689         LASSERT(ioctx->ioctx_xtvlen >= 0);
690         LASSERT(ioctx->ioctx_iovlen >= 0);
691
692         liblustre_wait_event(0);
693
694         if (!ioctx->ioctx_xtvlen)
695                 RETURN(0);
696
697         /* XXX consider other types later */
698         LASSERT(S_ISREG(llu_i2info(ino)->lli_st_mode));
699
700         session = get_io_session(ino, ioctx->ioctx_xtvlen * 2, cmd);
701         if (!session)
702                 RETURN(-ENOMEM);
703
704         cc = _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
705                                       ioctx->ioctx_iov, ioctx->ioctx_iovlen,
706                                       llu_file_prwv, session);
707
708         if (cc >= 0) {
709                 LASSERT(!ioctx->ioctx_cc);
710                 ioctx->ioctx_private = session;
711                 RETURN(0);
712         } else {
713                 put_io_session(session);
714                 RETURN(cc);
715         }
716 }
717
718 int llu_iop_read(struct inode *ino,
719                  struct ioctx *ioctx)
720 {
721         return llu_file_rwx(ino, ioctx, 1);
722 }
723
724 int llu_iop_write(struct inode *ino,
725                   struct ioctx *ioctx)
726 {
727         struct iattr iattr;
728         int rc;
729
730         memset(&iattr, 0, sizeof(iattr));
731         iattr.ia_mtime = iattr.ia_atime = CURRENT_TIME;
732         iattr.ia_valid = ATTR_MTIME | ATTR_ATIME | ATTR_RAW;
733
734         liblustre_wait_event(0);
735         rc = llu_setattr_raw(ino, &iattr);
736         if (rc) {
737                 CERROR("failed to set mtime/atime during write: %d", rc);
738                 /* XXX should continue or return error? */
739         }
740
741         return llu_file_rwx(ino, ioctx, 0);
742 }
743
744 int llu_iop_iodone(struct ioctx *ioctx)
745 {
746         struct llu_io_session *session;
747         struct llu_io_group *group;
748         int i, err = 0, rc = 0;
749         ENTRY;
750
751         liblustre_wait_event(0);
752
753         session = (struct llu_io_session *) ioctx->ioctx_private;
754         LASSERT(session);
755         LASSERT(!IS_ERR(session));
756
757         for (i = 0; i < session->lis_ngroups; i++) {
758                 group = session->lis_groups[i];
759                 if (group) {
760                         if (!rc) {
761                                 err = oig_wait(group->lig_oig);
762                                 if (err)
763                                         rc = err;
764                         }
765                         if (!rc)
766                                 ioctx->ioctx_cc += group->lig_rwcount;
767                         put_io_group(group);
768                         session->lis_groups[i] = NULL;
769                 }
770         }
771
772         if (rc) {
773                 LASSERT(rc < 0);
774                 ioctx->ioctx_cc = -1;
775                 ioctx->ioctx_errno = -rc;
776         }
777
778         put_io_session(session);
779         ioctx->ioctx_private = NULL;
780
781         RETURN(1);
782 }