Whamcloud - gitweb
543be8a4317bfa932916c006e03068203a25f404
[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 #ifdef HAVE_XTIO_H
36 #include <xtio.h>
37 #endif
38 #include <sysio.h>
39 #include <mount.h>
40 #include <inode.h>
41 #ifdef HAVE_FILE_H
42 #include <file.h>
43 #endif
44
45 #undef LIST_HEAD
46
47 #include "llite_lib.h"
48
49 struct llu_io_group
50 {
51         struct obd_io_group    *lig_oig;
52         struct inode           *lig_inode;
53         int                     lig_maxpages;
54         int                     lig_npages;
55         __u64                   lig_rwcount;
56         struct ll_async_page   *lig_llap;
57         struct page            *lig_pages;
58 };
59
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))
64
65 struct llu_io_session
66 {
67         struct inode           *lis_inode;
68         int                     lis_cmd;
69         int                     lis_max_groups;
70         int                     lis_ngroups;
71         struct llu_io_group    *lis_groups[0];
72 };
73 #define LLU_IO_SESSION_SIZE(x)  \
74         (sizeof(struct llu_io_session) + (x) * 2 * sizeof(void *))
75
76
77 typedef ssize_t llu_file_piov_t(const struct iovec *iovec, int iovlen,
78                                 _SYSIO_OFF_T pos, ssize_t len,
79                                 void *private);
80
81 static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
82 {
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);
86         struct {
87                 char name[16];
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);
92         int rc;
93         ENTRY;
94
95         if (lsm->lsm_stripe_count == 1)
96                 RETURN(0);
97
98         /* get our offset in the lov */
99         rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
100         if (rc != 0) {
101                 CERROR("obd_get_info: rc = %d\n", rc);
102                 LBUG();
103         }
104         LASSERT(stripe < lsm->lsm_stripe_count);
105         RETURN(stripe);
106 }
107
108 static int llu_extent_lock_callback(struct ldlm_lock *lock,
109                                     struct ldlm_lock_desc *new, void *data,
110                                     int flag)
111 {
112         struct lustre_handle lockh = { 0 };
113         int rc;
114         ENTRY;
115
116         if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
117                 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
118                 LBUG();
119         }
120
121         switch (flag) {
122         case LDLM_CB_BLOCKING:
123                 ldlm_lock2handle(lock, &lockh);
124                 rc = ldlm_cli_cancel(&lockh);
125                 if (rc != ELDLM_OK)
126                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
127                 break;
128         case LDLM_CB_CANCELING: {
129                 struct inode *inode;
130                 struct llu_inode_info *lli;
131                 struct lov_stripe_md *lsm;
132                 __u32 stripe;
133                 __u64 kms;
134
135                 /* This lock wasn't granted, don't try to evict pages */
136                 if (lock->l_req_mode != lock->l_granted_mode)
137                         RETURN(0);
138
139                 inode = llu_inode_from_lock(lock);
140                 if (!inode)
141                         RETURN(0);
142                 lli= llu_i2info(inode);
143                 if (!lli)
144                         goto iput;
145                 if (!lli->lli_smd)
146                         goto iput;
147                 lsm = lli->lli_smd;
148
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;
159 iput:
160                 I_RELE(inode);
161                 break;
162         }
163         default:
164                 LBUG();
165         }
166
167         RETURN(0);
168 }
169
170 static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
171 {
172         struct ptlrpc_request *req = reqp;
173         struct inode *inode = llu_inode_from_lock(lock);
174         struct llu_inode_info *lli;
175         struct ost_lvb *lvb;
176         int rc, size = sizeof(*lvb), stripe = 0;
177         ENTRY;
178
179         if (inode == NULL)
180                 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
181         lli = llu_i2info(inode);
182         if (lli == NULL)
183                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
184         if (lli->lli_smd == NULL)
185                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
186
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);
190
191         rc = lustre_pack_reply(req, 1, &size, NULL);
192         if (rc) {
193                 CERROR("lustre_pack_reply: %d\n", rc);
194                 GOTO(iput, rc);
195         }
196
197         lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
198         lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
199
200         LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
201                    lli->lli_st_size, stripe, lvb->lvb_size);
202  iput:
203         I_RELE(inode);
204  out:
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);
209
210         req->rq_status = rc;
211         return rc;
212 }
213
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);
217
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)
221 {
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;
227         ENTRY;
228
229         CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", lli->lli_st_ino);
230
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);
235         if (rc) {
236                 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
237                 RETURN(rc > 0 ? -EIO : rc);
238         }
239
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);
243
244         CDEBUG(D_DLMTRACE, "glimpse: size: %llu, blocks: %lu\n",
245                lli->lli_st_size, lli->lli_st_blocks);
246
247         obd_cancel(sbi->ll_dt_exp, lli->lli_smd, LCK_PR, &lockh);
248
249         RETURN(rc);
250 }
251
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,
255                     int ast_flags)
256 {
257         struct llu_sb_info *sbi = llu_i2sbi(inode);
258         struct llu_inode_info *lli = llu_i2info(inode);
259         int rc;
260         ENTRY;
261
262         LASSERT(lockh->cookie == 0);
263
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))
267                 RETURN(0);
268
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);
271
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);
276         if (rc > 0)
277                 rc = -EIO;
278
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);
282
283         //inode->i_mtime = lov_merge_mtime(lsm, inode->i_mtime);
284
285         RETURN(rc);
286 }
287
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)
291 {
292         struct llu_sb_info *sbi = llu_i2sbi(inode);
293         int rc;
294         ENTRY;
295
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))
299                 RETURN(0);
300
301         rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
302
303         RETURN(rc);
304 }
305
306 #define LLAP_MAGIC 12346789
307
308 struct ll_async_page {
309         int             llap_magic;
310         void           *llap_cookie;
311         int             llap_queued;
312         struct page    *llap_page;
313         struct inode   *llap_inode;
314 };
315
316 static void llu_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
317 {
318         struct ll_async_page *llap;
319         struct inode *inode;
320         struct lov_stripe_md *lsm;
321         obd_valid valid_flags;
322         ENTRY;
323
324         llap = LLAP_FROM_COOKIE(data);
325         inode = llap->llap_inode;
326         lsm = llu_i2info(inode)->lli_smd;
327
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;
333
334         obdo_from_inode(oa, inode, valid_flags);
335         EXIT;
336 }
337
338 /* called for each page in a completed rpc.*/
339 static void llu_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
340 {
341         struct ll_async_page *llap;
342         struct page *page;
343
344         llap = LLAP_FROM_COOKIE(data);
345         llap->llap_queued = 0;
346         page = llap->llap_page;
347
348         if (rc != 0) {
349                 if (cmd == OBD_BRW_WRITE)
350                         CERROR("writeback error on page %p index %ld: %d\n", 
351                                page, page->index, rc);
352         }
353         EXIT;
354 }
355
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,
361 };
362
363 static int llu_queue_pio(int cmd, struct llu_io_group *group,
364                          char *buf, size_t count, loff_t pos)
365 {
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;
372         ENTRY;
373
374         if (!exp)
375                 RETURN(-EINVAL);
376
377         /* prepare the pages array */
378         do {
379                 unsigned long index, offset, bytes;
380
381                 offset = (pos & ~PAGE_CACHE_MASK);
382                 index = pos >> PAGE_CACHE_SHIFT;
383                 bytes = PAGE_CACHE_SIZE - offset;
384                 if (bytes > count)
385                         bytes = count;
386
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)
391                                 break;
392                         bytes = lli->lli_st_size - pos;
393                 }
394
395                 /* prepare page for this index */
396                 pages[npages].index = index;
397                 pages[npages].addr = buf - offset;
398
399                 pages[npages]._offset = offset;
400                 pages[npages]._count = bytes;
401
402                 npages++;
403                 count -= bytes;
404                 pos += bytes;
405                 buf += bytes;
406
407                 group->lig_rwcount += bytes;
408                 ret_bytes += bytes;
409         } while (count);
410
411         group->lig_npages += npages;
412
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,
417                                          &llu_async_page_ops,
418                                          &llap[i], &llap[i].llap_cookie);
419                 if (rc) {
420                         LASSERT(rc < 0);
421                         llap[i].llap_cookie = NULL;
422                         RETURN(rc);
423                 }
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;
430
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);
436                 if (rc) {
437                         LASSERT(rc < 0);
438                         RETURN(rc);
439                 }
440
441                 llap[i].llap_queued = 1;
442         }
443
444         RETURN(ret_bytes);
445 }
446
447 static
448 struct llu_io_group * get_io_group(struct inode *inode, int maxpages)
449 {
450         struct llu_io_group *group;
451         int rc;
452
453         OBD_ALLOC(group, LLU_IO_GROUP_SIZE(maxpages));
454         if (!group)
455                 return ERR_PTR(-ENOMEM);
456
457         I_REF(inode);
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);
462
463         rc = oig_init(&group->lig_oig);
464         if (rc) {
465                 OBD_FREE(group, LLU_IO_GROUP_SIZE(maxpages));
466                 return ERR_PTR(rc);
467         }
468
469         return group;
470 }
471
472 static int max_io_pages(ssize_t len, int iovlen)
473 {
474         return (((len + PAGE_SIZE -1) / PAGE_SIZE) + 2 + iovlen - 1);
475 }
476
477 static
478 void put_io_group(struct llu_io_group *group)
479 {
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;
483         int i;
484
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);
489         }
490
491         I_RELE(group->lig_inode);
492
493         oig_release(group->lig_oig);
494         OBD_FREE(group, LLU_IO_GROUP_SIZE(group->lig_maxpages));
495 }
496
497 static
498 ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
499                         _SYSIO_OFF_T pos, ssize_t len,
500                         void *private)
501 {
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;
513         __u64 kms;
514         int err, is_read, lock_mode, iovidx, ret;
515         ENTRY;
516
517         /* in a large iov read/write we'll be repeatedly called.
518          * so give a chance to answer cancel ast here
519          */
520         liblustre_wait_event(0);
521
522         exp = llu_i2dtexp(inode);
523         if (exp == NULL)
524                 RETURN(-EINVAL);
525
526         if (len == 0 || iovlen == 0)
527                 RETURN(0);
528
529         if (pos + len > lli->lli_maxbytes)
530                 RETURN(-ERANGE);
531
532         iogroup = get_io_group(inode, max_io_pages(len, iovlen));
533         if (IS_ERR(iogroup))
534                 RETURN(PTR_ERR(iogroup));
535
536         is_read = session->lis_cmd == OBD_BRW_READ;
537         lock_mode = is_read ? LCK_PR : LCK_PW;
538
539         if (!is_read && (lli->lli_open_flags & O_APPEND)) {
540                 policy.l_extent.start = 0;
541                 policy.l_extent.end = OBD_OBJECT_EOF;
542         } else {
543                 policy.l_extent.start = pos;
544                 policy.l_extent.end = pos + len - 1;
545         }
546
547         err = llu_extent_lock(fd, inode, lsm, lock_mode, &policy,
548                               &lockh, astflag);
549         if (err != ELDLM_OK)
550                 GOTO(err_put, err);
551
552         if (is_read) {
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
557                          * the buffer */
558                         if ((err = llu_glimpse_size(inode))) {
559                                 llu_extent_unlock(fd, inode, lsm,
560                                                   lock_mode, &lockh);
561                                 GOTO(err_put, err);
562                         }
563                 } else {
564                         lli->lli_st_size = kms;
565                 }
566         } else {
567                 if (lli->lli_open_flags & O_APPEND)
568                         pos = lli->lli_st_size;
569         }
570
571         for (iovidx = 0; iovidx < iovlen; iovidx++) {
572                 char *buf = (char *) iovec[iovidx].iov_base;
573                 size_t count = iovec[iovidx].iov_len;
574
575                 if (!count)
576                         continue;
577                 if (len < count)
578                         count = 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);
582                 }
583
584                 if (is_read) {
585                         if (pos >= lli->lli_st_size)
586                                 break;
587                 } else {
588                         if (pos >= lli->lli_maxbytes) {
589                                 llu_extent_unlock(fd, inode, lsm, lock_mode,
590                                                   &lockh);
591                                 GOTO(err_put, err = -EFBIG);
592                         }
593                         if (pos + count >= lli->lli_maxbytes)
594                                 count = lli->lli_maxbytes - pos;
595                 }
596
597                 ret = llu_queue_pio(session->lis_cmd, iogroup, buf, count, pos);
598                 if (ret < 0) {
599                         llu_extent_unlock(fd, inode, lsm, lock_mode, &lockh);
600                         GOTO(err_put, err = ret);
601                 } else {
602                         pos += ret;
603                         if (!is_read) {
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;
609                         }
610                         len -= ret;
611                         if (!len)
612                                 break;
613                 }
614         }
615         LASSERT(len == 0 || is_read); /* libsysio should guarantee this */
616
617         err = llu_extent_unlock(fd, inode, lsm, lock_mode, &lockh);
618         if (err)
619                 CERROR("extent unlock error %d\n", err);
620
621         err = obd_trigger_group_io(exp, lsm, NULL, iogroup->lig_oig);
622         if (err)
623                 GOTO(err_put, err);
624
625         session->lis_groups[session->lis_ngroups++] = iogroup;
626         RETURN(0);
627 err_put:
628         put_io_group(iogroup);
629         RETURN((ssize_t)err);
630 }
631
632 static
633 struct llu_io_session *get_io_session(struct inode *ino, int ngroups, int cmd)
634 {
635         struct llu_io_session *session;
636
637         OBD_ALLOC(session, LLU_IO_SESSION_SIZE(ngroups));
638         if (!session)
639                 return NULL;
640
641         I_REF(ino);
642         session->lis_inode = ino;
643         session->lis_max_groups = ngroups;
644         session->lis_cmd = cmd;
645         return session;
646 }
647
648 static void put_io_session(struct llu_io_session *session)
649 {
650         int i;
651
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;
656                 }
657         }
658
659         I_RELE(session->lis_inode);
660         OBD_FREE(session, LLU_IO_SESSION_SIZE(session->lis_max_groups));
661 }
662
663 static int llu_file_rwx(struct inode *ino,
664                         struct ioctx *ioctx,
665                         int read)
666 {
667         struct llu_io_session *session;
668         ssize_t cc;
669         int cmd = read ? OBD_BRW_READ : OBD_BRW_WRITE;
670         ENTRY;
671
672         LASSERT(ioctx->ioctx_xtvlen >= 0);
673         LASSERT(ioctx->ioctx_iovlen >= 0);
674
675         liblustre_wait_event(0);
676
677         if (!ioctx->ioctx_xtvlen)
678                 RETURN(0);
679
680         /* XXX consider other types later */
681         LASSERT(S_ISREG(llu_i2info(ino)->lli_st_mode));
682
683         session = get_io_session(ino, ioctx->ioctx_xtvlen * 2, cmd);
684         if (!session)
685                 RETURN(-ENOMEM);
686
687         cc = _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
688                                       ioctx->ioctx_iov, ioctx->ioctx_iovlen,
689                                       llu_file_prwv, session);
690
691         if (cc >= 0) {
692                 LASSERT(!ioctx->ioctx_cc);
693                 ioctx->ioctx_private = session;
694                 RETURN(0);
695         } else {
696                 put_io_session(session);
697                 RETURN(cc);
698         }
699 }
700
701 int llu_iop_read(struct inode *ino,
702                  struct ioctx *ioctx)
703 {
704         return llu_file_rwx(ino, ioctx, 1);
705 }
706
707 int llu_iop_write(struct inode *ino,
708                   struct ioctx *ioctx)
709 {
710         struct iattr iattr;
711         int rc;
712
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;
716
717         liblustre_wait_event(0);
718         rc = llu_setattr_raw(ino, &iattr);
719         if (rc) {
720                 CERROR("failed to set mtime/atime during write: %d", rc);
721                 /* XXX should continue or return error? */
722         }
723
724         return llu_file_rwx(ino, ioctx, 0);
725 }
726
727 int llu_iop_iodone(struct ioctx *ioctx)
728 {
729         struct llu_io_session *session;
730         struct llu_io_group *group;
731         int i, err = 0, rc = 0;
732         ENTRY;
733
734         liblustre_wait_event(0);
735
736         session = (struct llu_io_session *) ioctx->ioctx_private;
737         LASSERT(session);
738         LASSERT(!IS_ERR(session));
739
740         for (i = 0; i < session->lis_ngroups; i++) {
741                 group = session->lis_groups[i];
742                 if (group) {
743                         if (!rc) {
744                                 err = oig_wait(group->lig_oig);
745                                 if (err)
746                                         rc = err;
747                         }
748                         if (!rc)
749                                 ioctx->ioctx_cc += group->lig_rwcount;
750                         put_io_group(group);
751                         session->lis_groups[i] = NULL;
752                 }
753         }
754
755         if (rc) {
756                 LASSERT(rc < 0);
757                 ioctx->ioctx_cc = -1;
758                 ioctx->ioctx_errno = -rc;
759         }
760
761         put_io_session(session);
762         ioctx->ioctx_private = NULL;
763
764         RETURN(1);
765 }