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