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