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