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