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