Whamcloud - gitweb
allow userland application to be know about about lost one of stripes.
[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/stat.h>
32 #include <sys/queue.h>
33 #include <fcntl.h>
34 #include <sys/uio.h>
35
36 #include <sysio.h>
37 #ifdef HAVE_XTIO_H
38 #include <xtio.h>
39 #endif
40 #include <fs.h>
41 #include <mount.h>
42 #include <inode.h>
43 #ifdef HAVE_FILE_H
44 #include <file.h>
45 #endif
46
47 #undef LIST_HEAD
48
49 #include "llite_lib.h"
50
51 struct llu_io_group
52 {
53         struct obd_io_group    *lig_oig;
54         struct inode           *lig_inode;
55         struct lustre_rw_params *lig_params;
56         int                     lig_maxpages;
57         int                     lig_npages;
58         __u64                   lig_rwcount;
59         struct ll_async_page   *lig_llaps;
60         struct page            *lig_pages;
61         void                   *lig_llap_cookies;
62 };
63
64 #define LLU_IO_GROUP_SIZE(x) \
65         (sizeof(struct llu_io_group) + \
66          (sizeof(struct ll_async_page) + \
67           sizeof(struct page) + \
68           llap_cookie_size) * (x))
69
70 struct llu_io_session
71 {
72         struct inode           *lis_inode;
73         int                     lis_cmd;
74         int                     lis_max_groups;
75         int                     lis_ngroups;
76         struct llu_io_group    *lis_groups[0];
77 };
78 #define LLU_IO_SESSION_SIZE(x)  \
79         (sizeof(struct llu_io_session) + (x) * 2 * sizeof(void *))
80
81
82 typedef ssize_t llu_file_piov_t(const struct iovec *iovec, int iovlen,
83                                 _SYSIO_OFF_T pos, ssize_t len,
84                                 void *private);
85
86 size_t llap_cookie_size;
87
88 static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
89 {
90         struct llu_inode_info *lli = llu_i2info(inode);
91         struct lov_stripe_md *lsm = lli->lli_smd;
92         struct obd_export *exp = llu_i2obdexp(inode);
93         struct {
94                 char name[16];
95                 struct ldlm_lock *lock;
96                 struct lov_stripe_md *lsm;
97         } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
98         __u32 stripe, vallen = sizeof(stripe);
99         int rc;
100         ENTRY;
101
102         if (lsm->lsm_stripe_count == 1)
103                 RETURN(0);
104
105         /* get our offset in the lov */
106         rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
107         if (rc != 0) {
108                 CERROR("obd_get_info: rc = %d\n", rc);
109                 LBUG();
110         }
111         LASSERT(stripe < lsm->lsm_stripe_count);
112         RETURN(stripe);
113 }
114
115 static int llu_extent_lock_callback(struct ldlm_lock *lock,
116                                     struct ldlm_lock_desc *new, void *data,
117                                     int flag)
118 {
119         struct lustre_handle lockh = { 0 };
120         int rc;
121         ENTRY;
122
123         if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
124                 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
125                 LBUG();
126         }
127
128         switch (flag) {
129         case LDLM_CB_BLOCKING:
130                 ldlm_lock2handle(lock, &lockh);
131                 rc = ldlm_cli_cancel(&lockh);
132                 if (rc != ELDLM_OK)
133                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
134                 break;
135         case LDLM_CB_CANCELING: {
136                 struct inode *inode;
137                 struct llu_inode_info *lli;
138                 struct lov_stripe_md *lsm;
139                 __u32 stripe;
140                 __u64 kms;
141
142                 /* This lock wasn't granted, don't try to evict pages */
143                 if (lock->l_req_mode != lock->l_granted_mode)
144                         RETURN(0);
145
146                 inode = llu_inode_from_lock(lock);
147                 if (!inode)
148                         RETURN(0);
149                 lli= llu_i2info(inode);
150                 if (!lli)
151                         goto iput;
152                 if (!lli->lli_smd)
153                         goto iput;
154                 lsm = lli->lli_smd;
155
156                 stripe = llu_lock_to_stripe_offset(inode, lock);
157                 lock_res_and_lock(lock);
158                 kms = ldlm_extent_shift_kms(lock,
159                                             lsm->lsm_oinfo[stripe]->loi_kms);
160                 unlock_res_and_lock(lock);
161                 if (lsm->lsm_oinfo[stripe]->loi_kms != kms)
162                         LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
163                                    lsm->lsm_oinfo[stripe]->loi_kms, kms);
164                 lsm->lsm_oinfo[stripe]->loi_kms = kms;
165 iput:
166                 I_RELE(inode);
167                 break;
168         }
169         default:
170                 LBUG();
171         }
172
173         RETURN(0);
174 }
175
176 static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
177 {
178         struct ptlrpc_request *req = reqp;
179         struct inode *inode = llu_inode_from_lock(lock);
180         struct llu_inode_info *lli;
181         struct ost_lvb *lvb;
182         int size[2] = { sizeof(struct ptlrpc_body), sizeof(*lvb) };
183         int rc, stripe = 0;
184         ENTRY;
185
186         if (inode == NULL)
187                 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
188         lli = llu_i2info(inode);
189         if (lli == NULL)
190                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
191         if (lli->lli_smd == NULL)
192                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
193
194         /* First, find out which stripe index this lock corresponds to. */
195         if (lli->lli_smd->lsm_stripe_count > 1)
196                 stripe = llu_lock_to_stripe_offset(inode, lock);
197
198         rc = lustre_pack_reply(req, 2, size, NULL);
199         if (rc)
200                 GOTO(iput, rc);
201
202         lvb = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, 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                    (__u64)llu_i2stat(inode)->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, 1, NULL, NULL);
214
215         req->rq_status = rc;
216         return rc;
217 }
218
219 static int llu_merge_lvb(struct inode *inode)
220 {
221         struct llu_inode_info *lli = llu_i2info(inode);
222         struct llu_sb_info *sbi = llu_i2sbi(inode);
223         struct intnl_stat *st = llu_i2stat(inode);
224         struct ost_lvb lvb;
225         int rc;
226         ENTRY;
227
228         inode_init_lvb(inode, &lvb);
229         rc = obd_merge_lvb(sbi->ll_dt_exp, lli->lli_smd, &lvb, 0);
230         st->st_size = lvb.lvb_size;
231         st->st_blocks = lvb.lvb_blocks;
232         /* handle st_blocks overflow gracefully */
233         if (st->st_blocks < lvb.lvb_blocks)
234                 st->st_blocks = ~0UL;
235         st->st_mtime = lvb.lvb_mtime;
236         st->st_atime = lvb.lvb_atime;
237         st->st_ctime = lvb.lvb_ctime;
238
239         RETURN(rc);
240 }
241
242 int llu_local_size(struct inode *inode)
243 {
244         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
245         struct llu_inode_info *lli = llu_i2info(inode);
246         struct llu_sb_info *sbi = llu_i2sbi(inode);
247         struct lustre_handle lockh = { 0 };
248         int flags = 0;
249         int rc;
250         ENTRY;
251
252         if (lli->lli_smd->lsm_stripe_count == 0)
253                 RETURN(0);
254         
255         rc = obd_match(sbi->ll_dt_exp, lli->lli_smd, LDLM_EXTENT,
256                        &policy, LCK_PR, &flags, inode, &lockh);
257         if (rc < 0)
258                 RETURN(rc);
259         else if (rc == 0)
260                 RETURN(-ENODATA);
261         
262         rc = llu_merge_lvb(inode);
263         obd_cancel(sbi->ll_dt_exp, lli->lli_smd, LCK_PR, &lockh);
264         RETURN(rc);
265 }
266
267 /* NB: lov_merge_size will prefer locally cached writes if they extend the
268  * file (because it prefers KMS over RSS when larger) */
269 int llu_glimpse_size(struct inode *inode)
270 {
271         struct llu_inode_info *lli = llu_i2info(inode);
272         struct intnl_stat *st = llu_i2stat(inode);
273         struct llu_sb_info *sbi = llu_i2sbi(inode);
274         struct lustre_handle lockh = { 0 };
275         struct ldlm_enqueue_info einfo = { 0 };
276         struct obd_info oinfo = { { { 0 } } };
277         int rc;
278         ENTRY;
279
280         /* If size is cached on the mds, skip glimpse. */
281         if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
282                 RETURN(0);
283
284         CDEBUG(D_DLMTRACE, "Glimpsing inode "LPU64"\n", (__u64)st->st_ino);
285
286         if (!lli->lli_smd) {
287                 CDEBUG(D_DLMTRACE, "No objects for inode "LPU64"\n", 
288                        (__u64)st->st_ino);
289                 RETURN(0);
290         }
291
292         einfo.ei_type = LDLM_EXTENT;
293         einfo.ei_mode = LCK_PR;
294         einfo.ei_cb_bl = llu_extent_lock_callback;
295         einfo.ei_cb_cp = ldlm_completion_ast;
296         einfo.ei_cb_gl = llu_glimpse_callback;
297         einfo.ei_cbdata = inode;
298
299         oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
300         oinfo.oi_lockh = &lockh;
301         oinfo.oi_md = lli->lli_smd;
302         oinfo.oi_flags = LDLM_FL_HAS_INTENT;
303
304         rc = obd_enqueue_rqset(sbi->ll_dt_exp, &oinfo, &einfo);
305         if (rc) {
306                 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
307                 RETURN(rc > 0 ? -EIO : rc);
308         }
309
310         rc = llu_merge_lvb(inode);
311         CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64", blocks: "LPU64"\n",
312                (__u64)st->st_size, (__u64)st->st_blocks);
313
314         RETURN(rc);
315 }
316
317 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
318                     struct lov_stripe_md *lsm, int mode,
319                     ldlm_policy_data_t *policy, struct lustre_handle *lockh,
320                     int ast_flags)
321 {
322         struct llu_sb_info *sbi = llu_i2sbi(inode);
323         struct intnl_stat *st = llu_i2stat(inode);
324         struct ldlm_enqueue_info einfo = { 0 };
325         struct obd_info oinfo = { { { 0 } } };
326         struct ost_lvb lvb;
327         int rc;
328         ENTRY;
329
330         LASSERT(!lustre_handle_is_used(lockh));
331         CLASSERT(ELDLM_OK == 0);
332
333         /* XXX phil: can we do this?  won't it screw the file size up? */
334         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
335             (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
336                 RETURN(0);
337
338         CDEBUG(D_DLMTRACE, "Locking inode %llu, start "LPU64" end "LPU64"\n",
339                (__u64)st->st_ino, policy->l_extent.start,
340                policy->l_extent.end);
341
342         einfo.ei_type = LDLM_EXTENT;
343         einfo.ei_mode = mode;
344         einfo.ei_cb_bl = llu_extent_lock_callback;
345         einfo.ei_cb_cp = ldlm_completion_ast;
346         einfo.ei_cb_gl = llu_glimpse_callback;
347         einfo.ei_cbdata = inode;
348
349         oinfo.oi_policy = *policy;
350         oinfo.oi_lockh = lockh;
351         oinfo.oi_md = lsm;
352         oinfo.oi_flags = ast_flags;
353
354         rc = obd_enqueue(sbi->ll_dt_exp, &oinfo, &einfo, NULL);
355         *policy = oinfo.oi_policy;
356         if (rc > 0)
357                 rc = -EIO;
358
359         inode_init_lvb(inode, &lvb);
360         obd_merge_lvb(sbi->ll_dt_exp, lsm, &lvb, 1);
361         if (policy->l_extent.start == 0 &&
362             policy->l_extent.end == OBD_OBJECT_EOF)
363                 st->st_size = lvb.lvb_size;
364
365         if (rc == 0) {
366                 st->st_mtime = lvb.lvb_mtime;
367                 st->st_atime = lvb.lvb_atime;
368                 st->st_ctime = lvb.lvb_ctime;
369         }
370
371         RETURN(rc);
372 }
373
374 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
375                 struct lov_stripe_md *lsm, int mode,
376                 struct lustre_handle *lockh)
377 {
378         struct llu_sb_info *sbi = llu_i2sbi(inode);
379         int rc;
380         ENTRY;
381
382         CLASSERT(ELDLM_OK == 0);
383
384         /* XXX phil: can we do this?  won't it screw the file size up? */
385         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
386             (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
387                 RETURN(0);
388
389         rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
390
391         RETURN(rc);
392 }
393
394 #define LLAP_MAGIC 12346789
395
396 struct ll_async_page {
397         int             llap_magic;
398         void           *llap_cookie;
399         int             llap_queued;
400         struct page    *llap_page;
401         struct inode   *llap_inode;
402 };
403
404 static void llu_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
405 {
406         struct ll_async_page *llap;
407         struct inode *inode;
408         struct lov_stripe_md *lsm;
409         obd_flag valid_flags;
410         ENTRY;
411
412         llap = LLAP_FROM_COOKIE(data);
413         inode = llap->llap_inode;
414         lsm = llu_i2info(inode)->lli_smd;
415
416         oa->o_id = lsm->lsm_object_id;
417         oa->o_valid = OBD_MD_FLID;
418         valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
419         if (cmd & OBD_BRW_WRITE)
420                 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME |
421                         OBD_MD_FLUID | OBD_MD_FLGID |
422                         OBD_MD_FLFID | OBD_MD_FLGENER;
423
424         obdo_from_inode(oa, inode, valid_flags);
425         EXIT;
426 }
427
428 static void llu_ap_update_obdo(void *data, int cmd, struct obdo *oa,
429                                obd_valid valid)
430 {
431         struct ll_async_page *llap;
432         ENTRY;
433
434         llap = LLAP_FROM_COOKIE(data);
435         obdo_from_inode(oa, llap->llap_inode, valid);
436
437         EXIT;
438 }
439
440 /* called for each page in a completed rpc.*/
441 static int llu_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
442 {
443         struct ll_async_page *llap;
444         struct page *page;
445         ENTRY;
446
447         llap = LLAP_FROM_COOKIE(data);
448         llap->llap_queued = 0;
449         page = llap->llap_page;
450
451         if (rc != 0) {
452                 if (cmd & OBD_BRW_WRITE)
453                         CERROR("writeback error on page %p index %ld: %d\n",
454                                page, page->index, rc);
455         }
456         RETURN(0);
457 }
458
459 static struct obd_capa * llu_ap_lookup_capa(void *data, int cmd)
460 {
461         return NULL;
462 }
463
464 static struct obd_async_page_ops llu_async_page_ops = {
465         .ap_make_ready =        NULL,
466         .ap_refresh_count =     NULL,
467         .ap_fill_obdo =         llu_ap_fill_obdo,
468         .ap_update_obdo =       llu_ap_update_obdo,
469         .ap_completion =        llu_ap_completion,
470         .ap_lookup_capa =       llu_ap_lookup_capa,
471 };
472
473 static int llu_queue_pio(int cmd, struct llu_io_group *group,
474                          char *buf, size_t count, loff_t pos)
475 {
476         struct llu_inode_info *lli = llu_i2info(group->lig_inode);
477         struct intnl_stat *st = llu_i2stat(group->lig_inode);
478         struct lov_stripe_md *lsm = lli->lli_smd;
479         struct obd_export *exp = llu_i2obdexp(group->lig_inode);
480         struct page *pages = &group->lig_pages[group->lig_npages],*page = pages;
481         struct ll_async_page *llap = &group->lig_llaps[group->lig_npages];
482         void *llap_cookie = group->lig_llap_cookies +
483                 llap_cookie_size * group->lig_npages;
484         int i, rc, npages = 0, ret_bytes = 0;
485         int local_lock;
486         ENTRY;
487
488         if (!exp)
489                 RETURN(-EINVAL);
490
491         local_lock = group->lig_params->lrp_lock_mode != LCK_NL;
492         /* prepare the pages array */
493         do {
494                 unsigned long index, offset, bytes;
495
496                 offset = (pos & ~CFS_PAGE_MASK);
497                 index = pos >> CFS_PAGE_SHIFT;
498                 bytes = CFS_PAGE_SIZE - offset;
499                 if (bytes > count)
500                         bytes = count;
501
502                 /* prevent read beyond file range */
503                 if (/* local_lock && */
504                     cmd == OBD_BRW_READ && pos + bytes >= st->st_size) {
505                         if (pos >= st->st_size)
506                                 break;
507                         bytes = st->st_size - pos;
508                 }
509
510                 /* prepare page for this index */
511                 page->index = index;
512                 page->addr = buf - offset;
513
514                 page->_offset = offset;
515                 page->_count = bytes;
516
517                 page++;
518                 npages++;
519                 count -= bytes;
520                 pos += bytes;
521                 buf += bytes;
522
523                 group->lig_rwcount += bytes;
524                 ret_bytes += bytes;
525         } while (count);
526
527         group->lig_npages += npages;
528
529         for (i = 0, page = pages; i < npages;
530              i++, page++, llap++, llap_cookie += llap_cookie_size){
531                 llap->llap_magic = LLAP_MAGIC;
532                 llap->llap_cookie = llap_cookie;
533                 rc = obd_prep_async_page(exp, lsm, NULL, page,
534                                          (obd_off)page->index << CFS_PAGE_SHIFT,
535                                          &llu_async_page_ops,
536                                          llap, &llap->llap_cookie);
537                 if (rc) {
538                         LASSERT(rc < 0);
539                         llap->llap_cookie = NULL;
540                         RETURN(rc);
541                 }
542
543                 CDEBUG(D_CACHE, "llap %p page %p group %p obj off "LPU64"\n",
544                        llap, page, llap->llap_cookie,
545                        (obd_off)pages->index << CFS_PAGE_SHIFT);
546                 page->private = (unsigned long)llap;
547                 llap->llap_page = page;
548                 llap->llap_inode = group->lig_inode;
549
550                 rc = obd_queue_group_io(exp, lsm, NULL, group->lig_oig,
551                                         llap->llap_cookie, cmd,
552                                         page->_offset, page->_count,
553                                         group->lig_params->lrp_brw_flags,
554                                         ASYNC_READY | ASYNC_URGENT |
555                                         ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
556                 if (!local_lock && cmd == OBD_BRW_READ) {
557                         /*
558                          * In OST-side locking case short reads cannot be
559                          * detected properly.
560                          *
561                          * The root of the problem is that
562                          *
563                          * kms = lov_merge_size(lsm, 1);
564                          * if (end >= kms)
565                          *         glimpse_size(inode);
566                          * else
567                          *         st->st_size = kms;
568                          *
569                          * logic in the read code (both llite and liblustre)
570                          * only works correctly when client holds DLM lock on
571                          * [start, end]. Without DLM lock KMS can be
572                          * completely out of date, and client can either make
573                          * spurious short-read (missing concurrent write), or
574                          * return stale data (missing concurrent
575                          * truncate). For llite client this is fatal, because
576                          * incorrect data are cached and can be later sent
577                          * back to the server (vide bug 5047). This is hard to
578                          * fix by handling short-reads on the server, as there
579                          * is no easy way to communicate file size (or amount
580                          * of bytes read/written) back to the client,
581                          * _especially_ because OSC pages can be sliced and
582                          * dices into multiple RPCs arbitrary. Fortunately,
583                          * liblustre doesn't cache data and the worst case is
584                          * that we get race with concurrent write or truncate.
585                          */
586                 }
587                 if (rc) {
588                         LASSERT(rc < 0);
589                         RETURN(rc);
590                 }
591
592                 llap->llap_queued = 1;
593         }
594
595         RETURN(ret_bytes);
596 }
597
598 static
599 struct llu_io_group * get_io_group(struct inode *inode, int maxpages,
600                                    struct lustre_rw_params *params)
601 {
602         struct llu_io_group *group;
603         int rc;
604
605         if (!llap_cookie_size)
606                 llap_cookie_size = obd_prep_async_page(llu_i2obdexp(inode),
607                                                        NULL, NULL, NULL, 0,
608                                                        NULL, NULL, NULL);
609
610         OBD_ALLOC(group, LLU_IO_GROUP_SIZE(maxpages));
611         if (!group)
612                 return ERR_PTR(-ENOMEM);
613
614         I_REF(inode);
615         group->lig_inode = inode;
616         group->lig_maxpages = maxpages;
617         group->lig_params = params;
618         group->lig_llaps = (struct ll_async_page *)(group + 1);
619         group->lig_pages = (struct page *)(&group->lig_llaps[maxpages]);
620         group->lig_llap_cookies = (void *)(&group->lig_pages[maxpages]);
621
622         rc = oig_init(&group->lig_oig);
623         if (rc) {
624                 OBD_FREE(group, LLU_IO_GROUP_SIZE(maxpages));
625                 return ERR_PTR(rc);
626         }
627
628         return group;
629 }
630
631 static int max_io_pages(ssize_t len, int iovlen)
632 {
633         return (((len + CFS_PAGE_SIZE -1) / CFS_PAGE_SIZE) + 2 + iovlen - 1);
634 }
635
636 static
637 void put_io_group(struct llu_io_group *group)
638 {
639         struct lov_stripe_md *lsm = llu_i2info(group->lig_inode)->lli_smd;
640         struct obd_export *exp = llu_i2obdexp(group->lig_inode);
641         struct ll_async_page *llap = group->lig_llaps;
642         int i;
643
644         for (i = 0; i < group->lig_npages; i++, llap++) {
645                 if (llap->llap_cookie)
646                         obd_teardown_async_page(exp, lsm, NULL,
647                                                 llap->llap_cookie);
648         }
649
650         I_RELE(group->lig_inode);
651
652         oig_release(group->lig_oig);
653         OBD_FREE(group, LLU_IO_GROUP_SIZE(group->lig_maxpages));
654 }
655
656 static
657 ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
658                         _SYSIO_OFF_T pos, ssize_t len,
659                         void *private)
660 {
661         struct llu_io_session *session = (struct llu_io_session *) private;
662         struct inode *inode = session->lis_inode;
663         struct llu_inode_info *lli = llu_i2info(inode);
664         struct intnl_stat *st = llu_i2stat(inode);
665         struct ll_file_data *fd = lli->lli_file_data;
666         struct lustre_handle lockh = {0};
667         struct lov_stripe_md *lsm = lli->lli_smd;
668         struct obd_export *exp = NULL;
669         struct llu_io_group *iogroup;
670         struct lustre_rw_params p;
671         struct ost_lvb lvb;
672         __u64 kms;
673         int err, is_read, iovidx, ret;
674         int local_lock;
675         ssize_t ret_len = len;
676         ENTRY;
677
678         /* in a large iov read/write we'll be repeatedly called.
679          * so give a chance to answer cancel ast here
680          */
681         liblustre_wait_event(0);
682
683         exp = llu_i2obdexp(inode);
684         if (exp == NULL)
685                 RETURN(-EINVAL);
686
687         if (len == 0 || iovlen == 0)
688                 RETURN(0);
689
690         if (pos + len > lli->lli_maxbytes)
691                 RETURN(-ERANGE);
692
693         lustre_build_lock_params(session->lis_cmd, lli->lli_open_flags,
694                                  lli->lli_sbi->ll_lco.lco_flags,
695                                  pos, len, &p);
696
697         iogroup = get_io_group(inode, max_io_pages(len, iovlen), &p);
698         if (IS_ERR(iogroup))
699                 RETURN(PTR_ERR(iogroup));
700
701         local_lock = p.lrp_lock_mode != LCK_NL;
702
703         err = llu_extent_lock(fd, inode, lsm, p.lrp_lock_mode, &p.lrp_policy,
704                               &lockh, p.lrp_ast_flags);
705         if (err != ELDLM_OK)
706                 GOTO(err_put, err);
707
708         is_read = (session->lis_cmd == OBD_BRW_READ);
709         if (is_read) {
710                 /*
711                  * If OST-side locking is used, KMS can be completely out of
712                  * date, and, hence, cannot be used for short-read
713                  * detection. Rely in OST to handle short reads in that case.
714                  */
715                 inode_init_lvb(inode, &lvb);
716                 obd_merge_lvb(exp, lsm, &lvb, 1);
717                 kms = lvb.lvb_size;
718                 /* extent.end is last byte of the range */
719                 if (p.lrp_policy.l_extent.end >= kms) {
720                         /* A glimpse is necessary to determine whether
721                          * we return a short read or some zeroes at
722                          * the end of the buffer
723                          *
724                          * In the case of OST-side locking KMS can be
725                          * completely out of date and short-reads maybe
726                          * mishandled. See llu_queue_pio() for more detailed
727                          * comment.
728                          */
729                         if ((err = llu_glimpse_size(inode))) {
730                                 GOTO(err_unlock, err);
731                         }
732                 } else {
733                         st->st_size = kms;
734                 }
735         } else if (lli->lli_open_flags & O_APPEND) {
736                 pos = st->st_size;
737         }
738
739         for (iovidx = 0; iovidx < iovlen; iovidx++) {
740                 char *buf = (char *) iovec[iovidx].iov_base;
741                 size_t count = iovec[iovidx].iov_len;
742
743                 if (!count)
744                         continue;
745                 if (len < count)
746                         count = len;
747                 if (IS_BAD_PTR(buf) || IS_BAD_PTR(buf + count)) {
748                         GOTO(err_unlock, err = -EFAULT);
749                 }
750
751                 if (is_read) {
752                         if (/* local_lock && */ pos >= st->st_size)
753                                 break;
754                 } else {
755                         if (pos >= lli->lli_maxbytes) {
756                                 GOTO(err_unlock, err = -EFBIG);
757                         }
758                         if (pos + count >= lli->lli_maxbytes)
759                                 count = lli->lli_maxbytes - pos;
760                 }
761
762                 ret = llu_queue_pio(session->lis_cmd, iogroup, buf, count, pos);
763                 if (ret < 0) {
764                         GOTO(err_unlock, err = ret);
765                 } else {
766                         pos += ret;
767                         if (!is_read) {
768                                 LASSERT(ret == count);
769                                 obd_adjust_kms(exp, lsm, pos, 0);
770                                 /* file size grow immediately */
771                                 if (pos > st->st_size)
772                                         st->st_size = pos;
773                         }
774                         len -= ret;
775                         if (!len)
776                                 break;
777                 }
778         }
779         LASSERT(len == 0 || is_read); /* libsysio should guarantee this */
780
781         err = obd_trigger_group_io(exp, lsm, NULL, iogroup->lig_oig);
782         if (err)
783                 GOTO(err_unlock, err);
784
785         err = oig_wait(iogroup->lig_oig);
786         if (err) {
787                 CERROR("%s error: %s\n", is_read ? "read" : "write", strerror(-err));
788                 GOTO(err_unlock, err);
789         }
790
791         ret = llu_extent_unlock(fd, inode, lsm, p.lrp_lock_mode, &lockh);
792         if (ret)
793                 CERROR("extent unlock error %d\n", ret);
794
795         session->lis_groups[session->lis_ngroups++] = iogroup;
796         RETURN(ret_len);
797
798 err_unlock:
799         llu_extent_unlock(fd, inode, lsm, p.lrp_lock_mode, &lockh);
800 err_put:
801         put_io_group(iogroup);
802         RETURN((ssize_t)err);
803 }
804
805 static
806 struct llu_io_session *get_io_session(struct inode *ino, int ngroups, int cmd)
807 {
808         struct llu_io_session *session;
809
810         OBD_ALLOC(session, LLU_IO_SESSION_SIZE(ngroups));
811         if (!session)
812                 return NULL;
813
814         I_REF(ino);
815         session->lis_inode = ino;
816         session->lis_max_groups = ngroups;
817         session->lis_cmd = cmd;
818         return session;
819 }
820
821 static void put_io_session(struct llu_io_session *session)
822 {
823         int i;
824
825         for (i = 0; i < session->lis_ngroups; i++) {
826                 if (session->lis_groups[i]) {
827                         put_io_group(session->lis_groups[i]);
828                         session->lis_groups[i] = NULL;
829                 }
830         }
831
832         I_RELE(session->lis_inode);
833         OBD_FREE(session, LLU_IO_SESSION_SIZE(session->lis_max_groups));
834 }
835
836 static int llu_file_rwx(struct inode *ino,
837                         struct ioctx *ioctx,
838                         int read)
839 {
840         struct llu_io_session *session;
841         ssize_t cc;
842         int cmd = read ? OBD_BRW_READ : OBD_BRW_WRITE;
843         ENTRY;
844
845         LASSERT(ioctx->ioctx_xtvlen >= 0);
846         LASSERT(ioctx->ioctx_iovlen >= 0);
847
848         liblustre_wait_event(0);
849
850         if (!ioctx->ioctx_xtvlen)
851                 RETURN(0);
852
853         /* XXX consider other types later */
854         if (S_ISDIR(llu_i2stat(ino)->st_mode))
855                 RETURN(-EISDIR);
856         if (!S_ISREG(llu_i2stat(ino)->st_mode))
857                 RETURN(-EOPNOTSUPP);
858
859         session = get_io_session(ino, ioctx->ioctx_xtvlen * 2, cmd);
860         if (!session)
861                 RETURN(-ENOMEM);
862
863         cc = _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
864                                       ioctx->ioctx_iov, ioctx->ioctx_iovlen,
865                                       llu_file_prwv, session);
866
867         if (cc >= 0) {
868                 LASSERT(!ioctx->ioctx_cc);
869                 ioctx->ioctx_private = session;
870                 cc = 0;
871         } else {
872                 put_io_session(session);
873         }
874
875         liblustre_wait_event(0);
876         RETURN(cc);
877 }
878
879 int llu_iop_read(struct inode *ino,
880                  struct ioctx *ioctx)
881 {
882         /* BUG: 5972 */
883         struct intnl_stat *st = llu_i2stat(ino);
884         st->st_atime = CURRENT_TIME;
885
886         return llu_file_rwx(ino, ioctx, 1);
887 }
888
889 int llu_iop_write(struct inode *ino,
890                   struct ioctx *ioctx)
891 {
892         struct intnl_stat *st = llu_i2stat(ino);
893         st->st_mtime = st->st_ctime = CURRENT_TIME;
894
895         return llu_file_rwx(ino, ioctx, 0);
896 }
897
898 int llu_iop_iodone(struct ioctx *ioctx)
899 {
900         struct llu_io_session *session;
901         struct llu_io_group *group;
902         int i, err = 0, rc = 0;
903         ENTRY;
904
905         liblustre_wait_event(0);
906
907         session = (struct llu_io_session *) ioctx->ioctx_private;
908         LASSERT(session);
909         LASSERT(!IS_ERR(session));
910
911         for (i = 0; i < session->lis_ngroups; i++) {
912                 group = session->lis_groups[i];
913                 if (group) {
914                         if (!rc) {
915                                 err = oig_wait(group->lig_oig);
916                                 if (err)
917                                         rc = err;
918                         }
919                         if (!rc)
920                                 ioctx->ioctx_cc += group->lig_rwcount;
921                         put_io_group(group);
922                         session->lis_groups[i] = NULL;
923                 }
924         }
925
926         if (rc) {
927                 LASSERT(rc < 0);
928                 ioctx->ioctx_cc = -1;
929                 ioctx->ioctx_errno = -rc;
930         }
931
932         put_io_session(session);
933         ioctx->ioctx_private = NULL;
934         liblustre_wait_event(0);
935
936         RETURN(1);
937 }