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