Whamcloud - gitweb
Branch: 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 #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                 RETURN(rc);
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                 if (stripe < 0) {
191                         CWARN("lock on inode without such object\n");
192                         GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
193                 }
194         }
195
196         rc = lustre_pack_reply(req, 1, &size, NULL);
197         if (rc) {
198                 CERROR("lustre_pack_reply: %d\n", rc);
199                 GOTO(iput, rc);
200         }
201
202         lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
203         lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
204
205         LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
206                    lli->lli_st_size, stripe, lvb->lvb_size);
207  iput:
208         I_RELE(inode);
209  out:
210         /* These errors are normal races, so we don't want to fill the console
211          * with messages by calling ptlrpc_error() */
212         if (rc == -ELDLM_NO_LOCK_DATA)
213                 lustre_pack_reply(req, 0, NULL, NULL);
214
215         req->rq_status = rc;
216         return rc;
217 }
218
219 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms_only);
220 __u64 lov_merge_blocks(struct lov_stripe_md *lsm);
221 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
222
223 /* NB: lov_merge_size will prefer locally cached writes if they extend the
224  * file (because it prefers KMS over RSS when larger) */
225 int llu_glimpse_size(struct inode *inode)
226 {
227         struct llu_inode_info *lli = llu_i2info(inode);
228         struct llu_sb_info *sbi = llu_i2sbi(inode);
229         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
230         struct lustre_handle lockh = { 0 };
231         int rc, flags = LDLM_FL_HAS_INTENT;
232         ENTRY;
233
234         CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", lli->lli_st_ino);
235
236         rc = obd_enqueue(sbi->ll_dt_exp, lli->lli_smd, LDLM_EXTENT, &policy,
237                          LCK_PR, &flags, llu_extent_lock_callback,
238                          ldlm_completion_ast, llu_glimpse_callback, inode,
239                          sizeof(struct ost_lvb), lustre_swab_ost_lvb, &lockh);
240         if (rc) {
241                 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
242                 RETURN(rc > 0 ? -EIO : rc);
243         }
244
245         lli->lli_st_size = lov_merge_size(lli->lli_smd, 0);
246         lli->lli_st_blocks = lov_merge_blocks(lli->lli_smd);
247         //lli->lli_st_mtime = lov_merge_mtime(lli->lli_smd, inode->i_mtime);
248
249         CDEBUG(D_DLMTRACE, "glimpse: size: %llu, blocks: %lu\n",
250                lli->lli_st_size, lli->lli_st_blocks);
251
252         obd_cancel(sbi->ll_dt_exp, lli->lli_smd, LCK_PR, &lockh);
253
254         RETURN(rc);
255 }
256
257 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
258                     struct lov_stripe_md *lsm, int mode,
259                     ldlm_policy_data_t *policy, struct lustre_handle *lockh,
260                     int ast_flags)
261 {
262         struct llu_sb_info *sbi = llu_i2sbi(inode);
263         struct llu_inode_info *lli = llu_i2info(inode);
264         int rc;
265         ENTRY;
266
267         LASSERT(lockh->cookie == 0);
268
269         /* XXX phil: can we do this?  won't it screw the file size up? */
270         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
271             (sbi->ll_flags & LL_SBI_NOLCK))
272                 RETURN(0);
273
274         CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
275                lli->lli_st_ino, policy->l_extent.start, policy->l_extent.end);
276
277         rc = obd_enqueue(sbi->ll_dt_exp, lsm, LDLM_EXTENT, policy, mode,
278                          &ast_flags, llu_extent_lock_callback,
279                          ldlm_completion_ast, llu_glimpse_callback, inode,
280                          sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
281         if (rc > 0)
282                 rc = -EIO;
283
284         if (policy->l_extent.start == 0 &&
285             policy->l_extent.end == OBD_OBJECT_EOF)
286                 lli->lli_st_size = lov_merge_size(lsm, 1);
287
288         //inode->i_mtime = lov_merge_mtime(lsm, inode->i_mtime);
289
290         RETURN(rc);
291 }
292
293 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
294                 struct lov_stripe_md *lsm, int mode,
295                 struct lustre_handle *lockh)
296 {
297         struct llu_sb_info *sbi = llu_i2sbi(inode);
298         int rc;
299         ENTRY;
300
301         /* XXX phil: can we do this?  won't it screw the file size up? */
302         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
303             (sbi->ll_flags & LL_SBI_NOLCK))
304                 RETURN(0);
305
306         rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
307
308         RETURN(rc);
309 }
310
311 #define LLAP_MAGIC 12346789
312
313 struct ll_async_page {
314         int             llap_magic;
315         void           *llap_cookie;
316         int             llap_queued;
317         struct page    *llap_page;
318         struct inode   *llap_inode;
319 };
320
321 static void llu_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
322 {
323         struct ll_async_page *llap;
324         struct inode *inode;
325         struct lov_stripe_md *lsm;
326         obd_valid valid_flags;
327         ENTRY;
328
329         llap = LLAP_FROM_COOKIE(data);
330         inode = llap->llap_inode;
331         lsm = llu_i2info(inode)->lli_smd;
332
333         oa->o_id = lsm->lsm_object_id;
334         oa->o_valid = OBD_MD_FLID;
335         valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
336         if (cmd == OBD_BRW_WRITE)
337                 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
338
339         obdo_from_inode(oa, inode, valid_flags);
340         EXIT;
341 }
342
343 /* called for each page in a completed rpc.*/
344 static void llu_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
345 {
346         struct ll_async_page *llap;
347         struct page *page;
348
349         llap = LLAP_FROM_COOKIE(data);
350         llap->llap_queued = 0;
351         page = llap->llap_page;
352
353         if (rc != 0) {
354                 if (cmd == OBD_BRW_WRITE)
355                         CERROR("writeback error on page %p index %ld: %d\n", 
356                                page, page->index, rc);
357         }
358         EXIT;
359 }
360
361 static struct obd_async_page_ops llu_async_page_ops = {
362         .ap_make_ready =        NULL,
363         .ap_refresh_count =     NULL,
364         .ap_fill_obdo =         llu_ap_fill_obdo,
365         .ap_completion =        llu_ap_completion,
366 };
367
368 static int llu_queue_pio(int cmd, struct llu_io_group *group,
369                          char *buf, size_t count, loff_t pos)
370 {
371         struct llu_inode_info *lli = llu_i2info(group->lig_inode);
372         struct lov_stripe_md *lsm = lli->lli_smd;
373         struct obd_export *exp = llu_i2dtexp(group->lig_inode);
374         struct page *pages = &group->lig_pages[group->lig_npages];
375         struct ll_async_page *llap = &group->lig_llap[group->lig_npages];
376         int i, rc, npages = 0, ret_bytes = 0;
377         ENTRY;
378
379         if (!exp)
380                 RETURN(-EINVAL);
381
382         /* prepare the pages array */
383         do {
384                 unsigned long index, offset, bytes;
385
386                 offset = (pos & ~PAGE_CACHE_MASK);
387                 index = pos >> PAGE_CACHE_SHIFT;
388                 bytes = PAGE_CACHE_SIZE - offset;
389                 if (bytes > count)
390                         bytes = count;
391
392                 /* prevent read beyond file range */
393                 if ((cmd == OBD_BRW_READ) &&
394                     (pos + bytes) >= lli->lli_st_size) {
395                         if (pos >= lli->lli_st_size)
396                                 break;
397                         bytes = lli->lli_st_size - pos;
398                 }
399
400                 /* prepare page for this index */
401                 pages[npages].index = index;
402                 pages[npages].addr = buf - offset;
403
404                 pages[npages]._offset = offset;
405                 pages[npages]._count = bytes;
406
407                 npages++;
408                 count -= bytes;
409                 pos += bytes;
410                 buf += bytes;
411
412                 group->lig_rwcount += bytes;
413                 ret_bytes += bytes;
414         } while (count);
415
416         group->lig_npages += npages;
417
418         for (i = 0; i < npages; i++) {
419                 llap[i].llap_magic = LLAP_MAGIC;
420                 rc = obd_prep_async_page(exp, lsm, NULL, &pages[i],
421                                          (obd_off)pages[i].index << PAGE_SHIFT,
422                                          &llu_async_page_ops,
423                                          &llap[i], &llap[i].llap_cookie);
424                 if (rc) {
425                         LASSERT(rc < 0);
426                         llap[i].llap_cookie = NULL;
427                         RETURN(rc);
428                 }
429                 CDEBUG(D_CACHE, "llap %p page %p group %p obj off "LPU64"\n",
430                        &llap[i], &pages[i], llap[i].llap_cookie,
431                        (obd_off)pages[i].index << PAGE_SHIFT);
432                 pages[i].private = (unsigned long)&llap[i];
433                 llap[i].llap_page = &pages[i];
434                 llap[i].llap_inode = group->lig_inode;
435
436                 rc = obd_queue_group_io(exp, lsm, NULL, group->lig_oig,
437                                         llap[i].llap_cookie, cmd,
438                                         pages[i]._offset, pages[i]._count, 0,
439                                         ASYNC_READY | ASYNC_URGENT |
440                                         ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
441                 if (rc) {
442                         LASSERT(rc < 0);
443                         RETURN(rc);
444                 }
445
446                 llap[i].llap_queued = 1;
447         }
448
449         RETURN(ret_bytes);
450 }
451
452 static
453 struct llu_io_group * get_io_group(struct inode *inode, int maxpages)
454 {
455         struct llu_io_group *group;
456         int rc;
457
458         OBD_ALLOC(group, LLU_IO_GROUP_SIZE(maxpages));
459         if (!group)
460                 return ERR_PTR(-ENOMEM);
461
462         I_REF(inode);
463         group->lig_inode = inode;
464         group->lig_maxpages = maxpages;
465         group->lig_llap = (struct ll_async_page *)(group + 1);
466         group->lig_pages = (struct page *) (group->lig_llap + maxpages);
467
468         rc = oig_init(&group->lig_oig);
469         if (rc) {
470                 OBD_FREE(group, LLU_IO_GROUP_SIZE(maxpages));
471                 return ERR_PTR(rc);
472         }
473
474         return group;
475 }
476
477 static int max_io_pages(ssize_t len, int iovlen)
478 {
479         return (((len + PAGE_SIZE -1) / PAGE_SIZE) + 2 + iovlen - 1);
480 }
481
482 static
483 void put_io_group(struct llu_io_group *group)
484 {
485         struct lov_stripe_md *lsm = llu_i2info(group->lig_inode)->lli_smd;
486         struct obd_export *exp = llu_i2dtexp(group->lig_inode);
487         struct ll_async_page *llap = group->lig_llap;
488         int i;
489
490         for (i = 0; i< group->lig_npages; i++) {
491                 if (llap[i].llap_cookie)
492                         obd_teardown_async_page(exp, lsm, NULL,
493                                                 llap[i].llap_cookie);
494         }
495
496         I_RELE(group->lig_inode);
497
498         oig_release(group->lig_oig);
499         OBD_FREE(group, LLU_IO_GROUP_SIZE(group->lig_maxpages));
500 }
501
502 static
503 ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
504                         _SYSIO_OFF_T pos, ssize_t len,
505                         void *private)
506 {
507         struct llu_io_session *session = (struct llu_io_session *) private;
508         struct inode *inode = session->lis_inode;
509         struct llu_inode_info *lli = llu_i2info(inode);
510         struct ll_file_data *fd = lli->lli_file_data;
511         struct lustre_handle lockh = {0};
512         struct lov_stripe_md *lsm = lli->lli_smd;
513         struct obd_export *exp = NULL;
514         ldlm_policy_data_t policy;
515         struct llu_io_group *iogroup;
516         int astflag = (lli->lli_open_flags & O_NONBLOCK) ?
517                        LDLM_FL_BLOCK_NOWAIT : 0;
518         __u64 kms;
519         int err, is_read, lock_mode, iovidx, ret;
520         ENTRY;
521
522         /* in a large iov read/write we'll be repeatedly called.
523          * so give a chance to answer cancel ast here
524          */
525         liblustre_wait_event(0);
526
527         exp = llu_i2dtexp(inode);
528         if (exp == NULL)
529                 RETURN(-EINVAL);
530
531         if (len == 0 || iovlen == 0)
532                 RETURN(0);
533
534         if (pos + len > lli->lli_maxbytes)
535                 RETURN(-ERANGE);
536
537         iogroup = get_io_group(inode, max_io_pages(len, iovlen));
538         if (IS_ERR(iogroup))
539                 RETURN(PTR_ERR(iogroup));
540
541         is_read = session->lis_cmd == OBD_BRW_READ;
542         lock_mode = is_read ? LCK_PR : LCK_PW;
543
544         if (!is_read && (lli->lli_open_flags & O_APPEND)) {
545                 policy.l_extent.start = 0;
546                 policy.l_extent.end = OBD_OBJECT_EOF;
547         } else {
548                 policy.l_extent.start = pos;
549                 policy.l_extent.end = pos + len - 1;
550         }
551
552         err = llu_extent_lock(fd, inode, lsm, lock_mode, &policy,
553                               &lockh, astflag);
554         if (err != ELDLM_OK)
555                 GOTO(err_put, err);
556
557         if (is_read) {
558                 kms = lov_merge_size(lsm, 1);
559                 if (policy.l_extent.end > kms) {
560                         /* A glimpse is necessary to determine whether we
561                          * return a short read or some zeroes at the end of
562                          * the buffer */
563                         if ((err = llu_glimpse_size(inode))) {
564                                 llu_extent_unlock(fd, inode, lsm,
565                                                   lock_mode, &lockh);
566                                 GOTO(err_put, err);
567                         }
568                 } else {
569                         lli->lli_st_size = kms;
570                 }
571         } else {
572                 if (lli->lli_open_flags & O_APPEND)
573                         pos = lli->lli_st_size;
574         }
575
576         for (iovidx = 0; iovidx < iovlen; iovidx++) {
577                 char *buf = (char *) iovec[iovidx].iov_base;
578                 size_t count = iovec[iovidx].iov_len;
579
580                 if (!count)
581                         continue;
582                 if (len < count)
583                         count = len;
584                 if (IS_BAD_PTR(buf) || IS_BAD_PTR(buf + count)) {
585                         llu_extent_unlock(fd, inode, lsm, lock_mode, &lockh);
586                         GOTO(err_put, err = -EFAULT);
587                 }
588
589                 if (is_read) {
590                         if (pos >= lli->lli_st_size)
591                                 break;
592                 } else {
593                         if (pos >= lli->lli_maxbytes) {
594                                 llu_extent_unlock(fd, inode, lsm, lock_mode,
595                                                   &lockh);
596                                 GOTO(err_put, err = -EFBIG);
597                         }
598                         if (pos + count >= lli->lli_maxbytes)
599                                 count = lli->lli_maxbytes - pos;
600                 }
601
602                 ret = llu_queue_pio(session->lis_cmd, iogroup, buf, count, pos);
603                 if (ret < 0) {
604                         llu_extent_unlock(fd, inode, lsm, lock_mode, &lockh);
605                         GOTO(err_put, err = ret);
606                 } else {
607                         pos += ret;
608                         if (!is_read) {
609                                 LASSERT(ret == count);
610                                 obd_adjust_kms(exp, lsm, pos, 0);
611                                 /* file size grow immediately */
612                                 if (pos > lli->lli_st_size)
613                                         lli->lli_st_size = pos;
614                         }
615                         len -= ret;
616                         if (!len)
617                                 break;
618                 }
619         }
620         LASSERT(len == 0 || is_read); /* libsysio should guarantee this */
621
622         err = llu_extent_unlock(fd, inode, lsm, lock_mode, &lockh);
623         if (err)
624                 CERROR("extent unlock error %d\n", err);
625
626         err = obd_trigger_group_io(exp, lsm, NULL, iogroup->lig_oig);
627         if (err)
628                 GOTO(err_put, err);
629
630         session->lis_groups[session->lis_ngroups++] = iogroup;
631         RETURN(0);
632 err_put:
633         put_io_group(iogroup);
634         RETURN((ssize_t)err);
635 }
636
637 static
638 struct llu_io_session *get_io_session(struct inode *ino, int ngroups, int cmd)
639 {
640         struct llu_io_session *session;
641
642         OBD_ALLOC(session, LLU_IO_SESSION_SIZE(ngroups));
643         if (!session)
644                 return NULL;
645
646         I_REF(ino);
647         session->lis_inode = ino;
648         session->lis_max_groups = ngroups;
649         session->lis_cmd = cmd;
650         return session;
651 }
652
653 static void put_io_session(struct llu_io_session *session)
654 {
655         int i;
656
657         for (i = 0; i < session->lis_ngroups; i++) {
658                 if (session->lis_groups[i]) {
659                         put_io_group(session->lis_groups[i]);
660                         session->lis_groups[i] = NULL;
661                 }
662         }
663
664         I_RELE(session->lis_inode);
665         OBD_FREE(session, LLU_IO_SESSION_SIZE(session->lis_max_groups));
666 }
667
668 static int llu_file_rwx(struct inode *ino,
669                         struct ioctx *ioctx,
670                         int read)
671 {
672         struct llu_io_session *session;
673         ssize_t cc;
674         int cmd = read ? OBD_BRW_READ : OBD_BRW_WRITE;
675         ENTRY;
676
677         LASSERT(ioctx->ioctx_xtvlen >= 0);
678         LASSERT(ioctx->ioctx_iovlen >= 0);
679
680         liblustre_wait_event(0);
681
682         if (!ioctx->ioctx_xtvlen)
683                 RETURN(0);
684
685         /* XXX consider other types later */
686         LASSERT(S_ISREG(llu_i2info(ino)->lli_st_mode));
687
688         session = get_io_session(ino, ioctx->ioctx_xtvlen * 2, cmd);
689         if (!session)
690                 RETURN(-ENOMEM);
691
692         cc = _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
693                                       ioctx->ioctx_iov, ioctx->ioctx_iovlen,
694                                       llu_file_prwv, session);
695
696         if (cc >= 0) {
697                 LASSERT(!ioctx->ioctx_cc);
698                 ioctx->ioctx_private = session;
699                 RETURN(0);
700         } else {
701                 put_io_session(session);
702                 RETURN(cc);
703         }
704 }
705
706 int llu_iop_read(struct inode *ino,
707                  struct ioctx *ioctx)
708 {
709         return llu_file_rwx(ino, ioctx, 1);
710 }
711
712 int llu_iop_write(struct inode *ino,
713                   struct ioctx *ioctx)
714 {
715         struct iattr iattr;
716         int rc;
717
718         memset(&iattr, 0, sizeof(iattr));
719         iattr.ia_mtime = iattr.ia_atime = CURRENT_TIME;
720         iattr.ia_valid = ATTR_MTIME | ATTR_ATIME | ATTR_RAW;
721
722         liblustre_wait_event(0);
723         rc = llu_setattr_raw(ino, &iattr);
724         if (rc) {
725                 CERROR("failed to set mtime/atime during write: %d", rc);
726                 /* XXX should continue or return error? */
727         }
728
729         return llu_file_rwx(ino, ioctx, 0);
730 }
731
732 int llu_iop_iodone(struct ioctx *ioctx)
733 {
734         struct llu_io_session *session;
735         struct llu_io_group *group;
736         int i, err = 0, rc = 0;
737         ENTRY;
738
739         liblustre_wait_event(0);
740
741         session = (struct llu_io_session *) ioctx->ioctx_private;
742         LASSERT(session);
743         LASSERT(!IS_ERR(session));
744
745         for (i = 0; i < session->lis_ngroups; i++) {
746                 group = session->lis_groups[i];
747                 if (group) {
748                         if (!rc) {
749                                 err = oig_wait(group->lig_oig);
750                                 if (err)
751                                         rc = err;
752                         }
753                         if (!rc)
754                                 ioctx->ioctx_cc += group->lig_rwcount;
755                         put_io_group(group);
756                         session->lis_groups[i] = NULL;
757                 }
758         }
759
760         if (rc) {
761                 LASSERT(rc < 0);
762                 ioctx->ioctx_cc = -1;
763                 ioctx->ioctx_errno = -rc;
764         }
765
766         put_io_session(session);
767         ioctx->ioctx_private = NULL;
768
769         RETURN(1);
770 }