Whamcloud - gitweb
port the patch(9414) on HEAD(b1_5)
[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 #ifdef HAVE_XTIO_H
37 #include <xtio.h>
38 #endif
39 #include <sysio.h>
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                 CERROR("lustre_pack_reply: %d\n", rc);
201                 GOTO(iput, rc);
202         }
203
204         lvb = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*lvb));
205         lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe]->loi_kms;
206
207         LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
208                    (long long)llu_i2stat(inode)->st_size, stripe,lvb->lvb_size);
209  iput:
210         I_RELE(inode);
211  out:
212         /* These errors are normal races, so we don't want to fill the console
213          * with messages by calling ptlrpc_error() */
214         if (rc == -ELDLM_NO_LOCK_DATA)
215                 lustre_pack_reply(req, 1, NULL, NULL);
216
217         req->rq_status = rc;
218         return rc;
219 }
220
221 /* NB: lov_merge_size will prefer locally cached writes if they extend the
222  * file (because it prefers KMS over RSS when larger) */
223 int llu_glimpse_size(struct inode *inode)
224 {
225         struct llu_inode_info *lli = llu_i2info(inode);
226         struct intnl_stat *st = llu_i2stat(inode);
227         struct llu_sb_info *sbi = llu_i2sbi(inode);
228         struct lustre_handle lockh = { 0 };
229         struct obd_enqueue_info einfo = { 0 };
230         struct obd_info oinfo = { { { 0 } } };
231         struct ost_lvb lvb;
232         int rc;
233         ENTRY;
234
235         CDEBUG(D_DLMTRACE, "Glimpsing inode %llu\n", (long long)st->st_ino);
236
237         if (!lli->lli_smd) {
238                 CDEBUG(D_DLMTRACE, "No objects for inode %llu\n", 
239                        (long long)st->st_ino);
240                 RETURN(0);
241         }
242
243         einfo.ei_type = LDLM_EXTENT;
244         einfo.ei_mode = LCK_PR;
245         einfo.ei_flags = LDLM_FL_HAS_INTENT;
246         einfo.ei_cb_bl = llu_extent_lock_callback;
247         einfo.ei_cb_cp = ldlm_completion_ast;
248         einfo.ei_cb_gl = llu_glimpse_callback;
249         einfo.ei_cbdata = inode;
250
251         oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
252         oinfo.oi_lockh = &lockh;
253         oinfo.oi_md = lli->lli_smd;
254
255         rc = obd_enqueue_rqset(sbi->ll_osc_exp, &oinfo, &einfo);
256         if (rc) {
257                 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
258                 RETURN(rc > 0 ? -EIO : rc);
259         }
260
261         inode_init_lvb(inode, &lvb);
262         obd_merge_lvb(sbi->ll_osc_exp, lli->lli_smd, &lvb, 0);
263         st->st_size = lvb.lvb_size;
264         st->st_blocks = lvb.lvb_blocks;
265         st->st_mtime = lvb.lvb_mtime;
266         st->st_atime = lvb.lvb_atime;
267         st->st_ctime = lvb.lvb_ctime;
268
269         CDEBUG(D_DLMTRACE, "glimpse: size: %llu, blocks: %llu\n",
270                (long long)st->st_size, (long long)st->st_blocks);
271
272         RETURN(rc);
273 }
274
275 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
276                     struct lov_stripe_md *lsm, int mode,
277                     ldlm_policy_data_t *policy, struct lustre_handle *lockh,
278                     int ast_flags)
279 {
280         struct llu_sb_info *sbi = llu_i2sbi(inode);
281         struct intnl_stat *st = llu_i2stat(inode);
282         struct obd_enqueue_info einfo = { 0 };
283         struct obd_info oinfo = { { { 0 } } };
284         struct ost_lvb lvb;
285         int rc;
286         ENTRY;
287
288         LASSERT(!lustre_handle_is_used(lockh));
289         CLASSERT(ELDLM_OK == 0);
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) || mode == LCK_NL)
294                 RETURN(0);
295
296         CDEBUG(D_DLMTRACE, "Locking inode %llu, start "LPU64" end "LPU64"\n",
297                (long long)st->st_ino, policy->l_extent.start,
298                policy->l_extent.end);
299
300         einfo.ei_type = LDLM_EXTENT;
301         einfo.ei_mode = mode;
302         einfo.ei_flags = ast_flags;
303         einfo.ei_cb_bl = llu_extent_lock_callback;
304         einfo.ei_cb_cp = ldlm_completion_ast;
305         einfo.ei_cb_gl = llu_glimpse_callback;
306         einfo.ei_cbdata = inode;
307
308         oinfo.oi_policy = *policy;
309         oinfo.oi_lockh = lockh;
310         oinfo.oi_md = lsm;
311
312         rc = obd_enqueue(sbi->ll_osc_exp, &oinfo, &einfo);
313         *policy = oinfo.oi_policy;
314         if (rc > 0)
315                 rc = -EIO;
316
317         inode_init_lvb(inode, &lvb);
318         obd_merge_lvb(sbi->ll_osc_exp, lsm, &lvb, 1);
319         if (policy->l_extent.start == 0 &&
320             policy->l_extent.end == OBD_OBJECT_EOF)
321                 st->st_size = lvb.lvb_size;
322
323         if (rc == 0) {
324                 st->st_mtime = lvb.lvb_mtime;
325                 st->st_atime = lvb.lvb_atime;
326                 st->st_ctime = lvb.lvb_ctime;
327         }
328
329         RETURN(rc);
330 }
331
332 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
333                 struct lov_stripe_md *lsm, int mode,
334                 struct lustre_handle *lockh)
335 {
336         struct llu_sb_info *sbi = llu_i2sbi(inode);
337         int rc;
338         ENTRY;
339
340         CLASSERT(ELDLM_OK == 0);
341
342         /* XXX phil: can we do this?  won't it screw the file size up? */
343         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
344             (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
345                 RETURN(0);
346
347         rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
348
349         RETURN(rc);
350 }
351
352 #define LLAP_MAGIC 12346789
353
354 struct ll_async_page {
355         int             llap_magic;
356         void           *llap_cookie;
357         int             llap_queued;
358         struct page    *llap_page;
359         struct inode   *llap_inode;
360 };
361
362 static void llu_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
363 {
364         struct ll_async_page *llap;
365         struct inode *inode;
366         struct lov_stripe_md *lsm;
367         obd_flag valid_flags;
368         ENTRY;
369
370         llap = LLAP_FROM_COOKIE(data);
371         inode = llap->llap_inode;
372         lsm = llu_i2info(inode)->lli_smd;
373
374         oa->o_id = lsm->lsm_object_id;
375         oa->o_valid = OBD_MD_FLID;
376         valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
377         if (cmd & OBD_BRW_WRITE)
378                 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME |
379                         OBD_MD_FLUID | OBD_MD_FLGID |
380                         OBD_MD_FLFID | OBD_MD_FLGENER;
381
382         obdo_from_inode(oa, inode, valid_flags);
383         EXIT;
384 }
385
386 static void llu_ap_update_obdo(void *data, int cmd, struct obdo *oa,
387                                obd_valid valid)
388 {
389         struct ll_async_page *llap;
390         ENTRY;
391
392         llap = LLAP_FROM_COOKIE(data);
393         obdo_from_inode(oa, llap->llap_inode, valid);
394
395         EXIT;
396 }
397
398 /* called for each page in a completed rpc.*/
399 static int llu_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
400 {
401         struct ll_async_page *llap;
402         struct page *page;
403         ENTRY;
404
405         llap = LLAP_FROM_COOKIE(data);
406         llap->llap_queued = 0;
407         page = llap->llap_page;
408
409         if (rc != 0) {
410                 if (cmd & OBD_BRW_WRITE)
411                         CERROR("writeback error on page %p index %ld: %d\n",
412                                page, page->index, rc);
413         }
414         RETURN(0);
415 }
416
417 static struct obd_async_page_ops llu_async_page_ops = {
418         .ap_make_ready =        NULL,
419         .ap_refresh_count =     NULL,
420         .ap_fill_obdo =         llu_ap_fill_obdo,
421         .ap_update_obdo =       llu_ap_update_obdo,
422         .ap_completion =        llu_ap_completion,
423 };
424
425 static int llu_queue_pio(int cmd, struct llu_io_group *group,
426                          char *buf, size_t count, loff_t pos)
427 {
428         struct llu_inode_info *lli = llu_i2info(group->lig_inode);
429         struct intnl_stat *st = llu_i2stat(group->lig_inode);
430         struct lov_stripe_md *lsm = lli->lli_smd;
431         struct obd_export *exp = llu_i2obdexp(group->lig_inode);
432         struct page *pages = &group->lig_pages[group->lig_npages],*page = pages;
433         struct ll_async_page *llap = &group->lig_llaps[group->lig_npages];
434         void *llap_cookie = group->lig_llap_cookies +
435                 llap_cookie_size * group->lig_npages;
436         int i, rc, npages = 0, ret_bytes = 0;
437         int local_lock;
438         ENTRY;
439
440         if (!exp)
441                 RETURN(-EINVAL);
442
443         local_lock = group->lig_params->lrp_lock_mode != LCK_NL;
444         /* prepare the pages array */
445         do {
446                 unsigned long index, offset, bytes;
447
448                 offset = (pos & ~CFS_PAGE_MASK);
449                 index = pos >> CFS_PAGE_SHIFT;
450                 bytes = CFS_PAGE_SIZE - offset;
451                 if (bytes > count)
452                         bytes = count;
453
454                 /* prevent read beyond file range */
455                 if (/* local_lock && */
456                     cmd == OBD_BRW_READ && pos + bytes >= st->st_size) {
457                         if (pos >= st->st_size)
458                                 break;
459                         bytes = st->st_size - pos;
460                 }
461
462                 /* prepare page for this index */
463                 page->index = index;
464                 page->addr = buf - offset;
465
466                 page->_offset = offset;
467                 page->_count = bytes;
468
469                 page++;
470                 npages++;
471                 count -= bytes;
472                 pos += bytes;
473                 buf += bytes;
474
475                 group->lig_rwcount += bytes;
476                 ret_bytes += bytes;
477         } while (count);
478
479         group->lig_npages += npages;
480
481         for (i = 0, page = pages; i < npages;
482              i++, page++, llap++, llap_cookie += llap_cookie_size){
483                 llap->llap_magic = LLAP_MAGIC;
484                 llap->llap_cookie = llap_cookie;
485                 rc = obd_prep_async_page(exp, lsm, NULL, page,
486                                          (obd_off)page->index << CFS_PAGE_SHIFT,
487                                          &llu_async_page_ops,
488                                          llap, &llap->llap_cookie);
489                 if (rc) {
490                         LASSERT(rc < 0);
491                         llap->llap_cookie = NULL;
492                         RETURN(rc);
493                 }
494                 CDEBUG(D_CACHE, "llap %p page %p group %p obj off "LPU64"\n",
495                        llap, page, llap->llap_cookie,
496                        (obd_off)pages->index << CFS_PAGE_SHIFT);
497                 page->private = (unsigned long)llap;
498                 llap->llap_page = page;
499                 llap->llap_inode = group->lig_inode;
500
501                 rc = obd_queue_group_io(exp, lsm, NULL, group->lig_oig,
502                                         llap->llap_cookie, cmd,
503                                         page->_offset, page->_count,
504                                         group->lig_params->lrp_brw_flags,
505                                         ASYNC_READY | ASYNC_URGENT |
506                                         ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
507                 if (!local_lock && cmd == OBD_BRW_READ) {
508                         /*
509                          * In OST-side locking case short reads cannot be
510                          * detected properly.
511                          *
512                          * The root of the problem is that
513                          *
514                          * kms = lov_merge_size(lsm, 1);
515                          * if (end >= kms)
516                          *         glimpse_size(inode);
517                          * else
518                          *         st->st_size = kms;
519                          *
520                          * logic in the read code (both llite and liblustre)
521                          * only works correctly when client holds DLM lock on
522                          * [start, end]. Without DLM lock KMS can be
523                          * completely out of date, and client can either make
524                          * spurious short-read (missing concurrent write), or
525                          * return stale data (missing concurrent
526                          * truncate). For llite client this is fatal, because
527                          * incorrect data are cached and can be later sent
528                          * back to the server (vide bug 5047). This is hard to
529                          * fix by handling short-reads on the server, as there
530                          * is no easy way to communicate file size (or amount
531                          * of bytes read/written) back to the client,
532                          * _especially_ because OSC pages can be sliced and
533                          * dices into multiple RPCs arbitrary. Fortunately,
534                          * liblustre doesn't cache data and the worst case is
535                          * that we get race with concurrent write or truncate.
536                          */
537                 }
538                 if (rc) {
539                         LASSERT(rc < 0);
540                         RETURN(rc);
541                 }
542
543                 llap->llap_queued = 1;
544         }
545
546         RETURN(ret_bytes);
547 }
548
549 static
550 struct llu_io_group * get_io_group(struct inode *inode, int maxpages,
551                                    struct lustre_rw_params *params)
552 {
553         struct llu_io_group *group;
554         int rc;
555
556         if (!llap_cookie_size)
557                 llap_cookie_size = obd_prep_async_page(llu_i2obdexp(inode),
558                                                        NULL, NULL, NULL, 0,
559                                                        NULL, NULL, NULL);
560
561         OBD_ALLOC(group, LLU_IO_GROUP_SIZE(maxpages));
562         if (!group)
563                 return ERR_PTR(-ENOMEM);
564
565         I_REF(inode);
566         group->lig_inode = inode;
567         group->lig_maxpages = maxpages;
568         group->lig_params = params;
569         group->lig_llaps = (struct ll_async_page *)(group + 1);
570         group->lig_pages = (struct page *)(&group->lig_llaps[maxpages]);
571         group->lig_llap_cookies = (void *)(&group->lig_pages[maxpages]);
572
573         rc = oig_init(&group->lig_oig);
574         if (rc) {
575                 OBD_FREE(group, LLU_IO_GROUP_SIZE(maxpages));
576                 return ERR_PTR(rc);
577         }
578
579         return group;
580 }
581
582 static int max_io_pages(ssize_t len, int iovlen)
583 {
584         return (((len + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT) + 2 + iovlen - 1);
585 }
586
587 static
588 void put_io_group(struct llu_io_group *group)
589 {
590         struct lov_stripe_md *lsm = llu_i2info(group->lig_inode)->lli_smd;
591         struct obd_export *exp = llu_i2obdexp(group->lig_inode);
592         struct ll_async_page *llap = group->lig_llaps;
593         int i;
594
595         for (i = 0; i < group->lig_npages; i++, llap++) {
596                 if (llap->llap_cookie)
597                         obd_teardown_async_page(exp, lsm, NULL,
598                                                 llap->llap_cookie);
599         }
600
601         I_RELE(group->lig_inode);
602
603         oig_release(group->lig_oig);
604         OBD_FREE(group, LLU_IO_GROUP_SIZE(group->lig_maxpages));
605 }
606
607 static
608 ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
609                         _SYSIO_OFF_T pos, ssize_t len,
610                         void *private)
611 {
612         struct llu_io_session *session = (struct llu_io_session *) private;
613         struct inode *inode = session->lis_inode;
614         struct llu_inode_info *lli = llu_i2info(inode);
615         struct intnl_stat *st = llu_i2stat(inode);
616         struct ll_file_data *fd = lli->lli_file_data;
617         struct lustre_handle lockh = {0};
618         struct lov_stripe_md *lsm = lli->lli_smd;
619         struct obd_export *exp = NULL;
620         struct llu_io_group *iogroup;
621         struct lustre_rw_params p;
622         struct ost_lvb lvb;
623         __u64 kms;
624         int err, is_read, iovidx, ret;
625         int local_lock;
626         ssize_t ret_len = len;
627         ENTRY;
628
629         /* in a large iov read/write we'll be repeatedly called.
630          * so give a chance to answer cancel ast here
631          */
632         liblustre_wait_event(0);
633
634         exp = llu_i2obdexp(inode);
635         if (exp == NULL)
636                 RETURN(-EINVAL);
637
638         if (len == 0 || iovlen == 0)
639                 RETURN(0);
640
641         if (pos + len > lli->lli_maxbytes)
642                 RETURN(-ERANGE);
643
644         lustre_build_lock_params(session->lis_cmd, lli->lli_open_flags,
645                                  lli->lli_sbi->ll_lco.lco_flags,
646                                  pos, len, &p);
647
648         iogroup = get_io_group(inode, max_io_pages(len, iovlen), &p);
649         if (IS_ERR(iogroup))
650                 RETURN(PTR_ERR(iogroup));
651
652         local_lock = p.lrp_lock_mode != LCK_NL;
653
654         err = llu_extent_lock(fd, inode, lsm, p.lrp_lock_mode, &p.lrp_policy,
655                               &lockh, p.lrp_ast_flags);
656         if (err != ELDLM_OK)
657                 GOTO(err_put, err);
658
659         is_read = (session->lis_cmd == OBD_BRW_READ);
660         if (is_read) {
661                 /*
662                  * If OST-side locking is used, KMS can be completely out of
663                  * date, and, hence, cannot be used for short-read
664                  * detection. Rely in OST to handle short reads in that case.
665                  */
666                 inode_init_lvb(inode, &lvb);
667                 obd_merge_lvb(exp, lsm, &lvb, 1);
668                 kms = lvb.lvb_size;
669                 /* extent.end is last byte of the range */
670                 if (p.lrp_policy.l_extent.end >= kms) {
671                         /* A glimpse is necessary to determine whether
672                          * we return a short read or some zeroes at
673                          * the end of the buffer
674                          *
675                          * In the case of OST-side locking KMS can be
676                          * completely out of date and short-reads maybe
677                          * mishandled. See llu_queue_pio() for more detailed
678                          * comment.
679                          */
680                         if ((err = llu_glimpse_size(inode))) {
681                                 GOTO(err_unlock, err);
682                         }
683                 } else {
684                         st->st_size = kms;
685                 }
686         } else if (lli->lli_open_flags & O_APPEND) {
687                 pos = st->st_size;
688         }
689
690         for (iovidx = 0; iovidx < iovlen; iovidx++) {
691                 char *buf = (char *) iovec[iovidx].iov_base;
692                 size_t count = iovec[iovidx].iov_len;
693
694                 if (!count)
695                         continue;
696                 if (len < count)
697                         count = len;
698                 if (IS_BAD_PTR(buf) || IS_BAD_PTR(buf + count)) {
699                         GOTO(err_unlock, err = -EFAULT);
700                 }
701
702                 if (is_read) {
703                         if (/* local_lock && */ pos >= st->st_size)
704                                 break;
705                 } else {
706                         if (pos >= lli->lli_maxbytes) {
707                                 GOTO(err_unlock, err = -EFBIG);
708                         }
709                         if (pos + count >= lli->lli_maxbytes)
710                                 count = lli->lli_maxbytes - pos;
711                 }
712
713                 ret = llu_queue_pio(session->lis_cmd, iogroup, buf, count, pos);
714                 if (ret < 0) {
715                         GOTO(err_unlock, err = ret);
716                 } else {
717                         pos += ret;
718                         if (!is_read) {
719                                 LASSERT(ret == count);
720                                 obd_adjust_kms(exp, lsm, pos, 0);
721                                 /* file size grow immediately */
722                                 if (pos > st->st_size)
723                                         st->st_size = pos;
724                         }
725                         len -= ret;
726                         if (!len)
727                                 break;
728                 }
729         }
730         LASSERT(len == 0 || is_read); /* libsysio should guarantee this */
731
732         err = obd_trigger_group_io(exp, lsm, NULL, iogroup->lig_oig);
733         if (err)
734                 GOTO(err_unlock, err);
735
736         err = oig_wait(iogroup->lig_oig);
737         if (err) {
738                 CERROR("sync error %d, data corruption possible\n", err);
739                 GOTO(err_unlock, err);
740         }
741
742         ret = llu_extent_unlock(fd, inode, lsm, p.lrp_lock_mode, &lockh);
743         if (ret)
744                 CERROR("extent unlock error %d\n", ret);
745
746         session->lis_groups[session->lis_ngroups++] = iogroup;
747         RETURN(ret_len);
748
749 err_unlock:
750         llu_extent_unlock(fd, inode, lsm, p.lrp_lock_mode, &lockh);
751 err_put:
752         put_io_group(iogroup);
753         RETURN((ssize_t)err);
754 }
755
756 static
757 struct llu_io_session *get_io_session(struct inode *ino, int ngroups, int cmd)
758 {
759         struct llu_io_session *session;
760
761         OBD_ALLOC(session, LLU_IO_SESSION_SIZE(ngroups));
762         if (!session)
763                 return NULL;
764
765         I_REF(ino);
766         session->lis_inode = ino;
767         session->lis_max_groups = ngroups;
768         session->lis_cmd = cmd;
769         return session;
770 }
771
772 static void put_io_session(struct llu_io_session *session)
773 {
774         int i;
775
776         for (i = 0; i < session->lis_ngroups; i++) {
777                 if (session->lis_groups[i]) {
778                         put_io_group(session->lis_groups[i]);
779                         session->lis_groups[i] = NULL;
780                 }
781         }
782
783         I_RELE(session->lis_inode);
784         OBD_FREE(session, LLU_IO_SESSION_SIZE(session->lis_max_groups));
785 }
786
787 static int llu_file_rwx(struct inode *ino,
788                         struct ioctx *ioctx,
789                         int read)
790 {
791         struct llu_io_session *session;
792         ssize_t cc;
793         int cmd = read ? OBD_BRW_READ : OBD_BRW_WRITE;
794         ENTRY;
795
796         LASSERT(ioctx->ioctx_xtvlen >= 0);
797         LASSERT(ioctx->ioctx_iovlen >= 0);
798
799         liblustre_wait_event(0);
800
801         if (!ioctx->ioctx_xtvlen)
802                 RETURN(0);
803
804         /* XXX consider other types later */
805         if (S_ISDIR(llu_i2stat(ino)->st_mode))
806                 RETURN(-EISDIR);
807         if (!S_ISREG(llu_i2stat(ino)->st_mode))
808                 RETURN(-EOPNOTSUPP);
809
810         session = get_io_session(ino, ioctx->ioctx_xtvlen * 2, cmd);
811         if (!session)
812                 RETURN(-ENOMEM);
813
814         cc = _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
815                                       ioctx->ioctx_iov, ioctx->ioctx_iovlen,
816                                       llu_file_prwv, session);
817
818         if (cc >= 0) {
819                 LASSERT(!ioctx->ioctx_cc);
820                 ioctx->ioctx_private = session;
821                 cc = 0;
822         } else {
823                 put_io_session(session);
824         }
825
826         liblustre_wait_event(0);
827         RETURN(cc);
828 }
829
830 int llu_iop_read(struct inode *ino,
831                  struct ioctx *ioctx)
832 {
833         /* BUG: 5972 */
834         struct intnl_stat *st = llu_i2stat(ino);
835         st->st_atime = CURRENT_TIME;
836
837         return llu_file_rwx(ino, ioctx, 1);
838 }
839
840 int llu_iop_write(struct inode *ino,
841                   struct ioctx *ioctx)
842 {
843         struct intnl_stat *st = llu_i2stat(ino);
844         st->st_mtime = st->st_ctime = CURRENT_TIME;
845
846         return llu_file_rwx(ino, ioctx, 0);
847 }
848
849 int llu_iop_iodone(struct ioctx *ioctx)
850 {
851         struct llu_io_session *session;
852         struct llu_io_group *group;
853         int i, err = 0, rc = 0;
854         ENTRY;
855
856         liblustre_wait_event(0);
857
858         session = (struct llu_io_session *) ioctx->ioctx_private;
859         LASSERT(session);
860         LASSERT(!IS_ERR(session));
861
862         for (i = 0; i < session->lis_ngroups; i++) {
863                 group = session->lis_groups[i];
864                 if (group) {
865                         if (!rc) {
866                                 err = oig_wait(group->lig_oig);
867                                 if (err)
868                                         rc = err;
869                         }
870                         if (!rc)
871                                 ioctx->ioctx_cc += group->lig_rwcount;
872                         put_io_group(group);
873                         session->lis_groups[i] = NULL;
874                 }
875         }
876
877         if (rc) {
878                 LASSERT(rc < 0);
879                 ioctx->ioctx_cc = -1;
880                 ioctx->ioctx_errno = -rc;
881         }
882
883         put_io_session(session);
884         ioctx->ioctx_private = NULL;
885         liblustre_wait_event(0);
886
887         RETURN(1);
888 }