Whamcloud - gitweb
file lc_modprobe.sh.in was added on branch b_new_cmd on 2006-08-19 03:08:14 +0000
[fs/lustre-release.git] / lustre / liblustre / rw.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light block IO
5  *
6  *  Copyright (c) 2002-2004 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/queue.h>
33 #include <fcntl.h>
34 #include <sys/uio.h>
35
36 #ifdef HAVE_XTIO_H
37 #include <xtio.h>
38 #endif
39 #include <sysio.h>
40 #include <fs.h>
41 #include <mount.h>
42 #include <inode.h>
43 #ifdef HAVE_FILE_H
44 #include <file.h>
45 #endif
46
47 #undef LIST_HEAD
48
49 #include "llite_lib.h"
50
51 struct llu_io_group
52 {
53         struct obd_io_group    *lig_oig;
54         struct inode           *lig_inode;
55         struct lustre_rw_params *lig_params;
56         int                     lig_maxpages;
57         int                     lig_npages;
58         __u64                   lig_rwcount;
59         struct ll_async_page   *lig_llaps;
60         struct page            *lig_pages;
61         void                   *lig_llap_cookies;
62 };
63
64 #define LLU_IO_GROUP_SIZE(x) \
65         (sizeof(struct llu_io_group) + \
66          (sizeof(struct ll_async_page) + \
67           sizeof(struct page) + \
68           llap_cookie_size) * (x))
69
70 struct llu_io_session
71 {
72         struct inode           *lis_inode;
73         int                     lis_cmd;
74         int                     lis_max_groups;
75         int                     lis_ngroups;
76         struct llu_io_group    *lis_groups[0];
77 };
78 #define LLU_IO_SESSION_SIZE(x)  \
79         (sizeof(struct llu_io_session) + (x) * 2 * sizeof(void *))
80
81
82 typedef ssize_t llu_file_piov_t(const struct iovec *iovec, int iovlen,
83                                 _SYSIO_OFF_T pos, ssize_t len,
84                                 void *private);
85
86 size_t llap_cookie_size;
87
88 static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
89 {
90         struct llu_inode_info *lli = llu_i2info(inode);
91         struct lov_stripe_md *lsm = lli->lli_smd;
92         struct obd_export *exp = llu_i2obdexp(inode);
93         struct {
94                 char name[16];
95                 struct ldlm_lock *lock;
96                 struct lov_stripe_md *lsm;
97         } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
98         __u32 stripe, vallen = sizeof(stripe);
99         int rc;
100         ENTRY;
101
102         if (lsm->lsm_stripe_count == 1)
103                 RETURN(0);
104
105         /* get our offset in the lov */
106         rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
107         if (rc != 0) {
108                 CERROR("obd_get_info: rc = %d\n", rc);
109                 LBUG();
110         }
111         LASSERT(stripe < lsm->lsm_stripe_count);
112         RETURN(stripe);
113 }
114
115 static int llu_extent_lock_callback(struct ldlm_lock *lock,
116                                     struct ldlm_lock_desc *new, void *data,
117                                     int flag)
118 {
119         struct lustre_handle lockh = { 0 };
120         int rc;
121         ENTRY;
122
123         if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
124                 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
125                 LBUG();
126         }
127
128         switch (flag) {
129         case LDLM_CB_BLOCKING:
130                 ldlm_lock2handle(lock, &lockh);
131                 rc = ldlm_cli_cancel(&lockh);
132                 if (rc != ELDLM_OK)
133                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
134                 break;
135         case LDLM_CB_CANCELING: {
136                 struct inode *inode;
137                 struct llu_inode_info *lli;
138                 struct lov_stripe_md *lsm;
139                 __u32 stripe;
140                 __u64 kms;
141
142                 /* This lock wasn't granted, don't try to evict pages */
143                 if (lock->l_req_mode != lock->l_granted_mode)
144                         RETURN(0);
145
146                 inode = llu_inode_from_lock(lock);
147                 if (!inode)
148                         RETURN(0);
149                 lli= llu_i2info(inode);
150                 if (!lli)
151                         goto iput;
152                 if (!lli->lli_smd)
153                         goto iput;
154                 lsm = lli->lli_smd;
155
156                 stripe = llu_lock_to_stripe_offset(inode, lock);
157                 l_lock(&lock->l_resource->lr_namespace->ns_lock);
158                 kms = ldlm_extent_shift_kms(lock,
159                                             lsm->lsm_oinfo[stripe].loi_kms);
160                 l_unlock(&lock->l_resource->lr_namespace->ns_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, size = sizeof(*lvb), 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         rc = lustre_pack_reply(req, 1, &size, NULL);
198         if (rc) {
199                 CERROR("lustre_pack_reply: %d\n", rc);
200                 GOTO(iput, rc);
201         }
202
203         lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
204         lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
205
206         LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
207                    (long long)llu_i2stat(inode)->st_size, stripe,lvb->lvb_size);
208  iput:
209         I_RELE(inode);
210  out:
211         /* These errors are normal races, so we don't want to fill the console
212          * with messages by calling ptlrpc_error() */
213         if (rc == -ELDLM_NO_LOCK_DATA)
214                 lustre_pack_reply(req, 0, NULL, NULL);
215
216         req->rq_status = rc;
217         return rc;
218 }
219
220 /* NB: lov_merge_size will prefer locally cached writes if they extend the
221  * file (because it prefers KMS over RSS when larger) */
222 int llu_glimpse_size(struct inode *inode)
223 {
224         struct llu_inode_info *lli = llu_i2info(inode);
225         struct intnl_stat *st = llu_i2stat(inode);
226         struct llu_sb_info *sbi = llu_i2sbi(inode);
227         ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
228         struct lustre_handle lockh = { 0 };
229         struct ost_lvb lvb;
230         int rc, flags = LDLM_FL_HAS_INTENT;
231         ENTRY;
232
233         CDEBUG(D_DLMTRACE, "Glimpsing inode %llu\n", (long long)st->st_ino);
234
235         rc = obd_enqueue(sbi->ll_dt_exp, lli->lli_smd, LDLM_EXTENT, &policy,
236                          LCK_PR, &flags, llu_extent_lock_callback,
237                          ldlm_completion_ast, llu_glimpse_callback, inode,
238                          sizeof(struct ost_lvb), lustre_swab_ost_lvb, &lockh);
239         if (rc) {
240                 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
241                 RETURN(rc > 0 ? -EIO : rc);
242         }
243
244         inode_init_lvb(inode, &lvb);
245         obd_merge_lvb(sbi->ll_dt_exp, lli->lli_smd, &lvb, 0);
246         st->st_size = lvb.lvb_size;
247         st->st_blocks = lvb.lvb_blocks;
248         st->st_mtime = lvb.lvb_mtime;
249         st->st_atime = lvb.lvb_atime;
250         st->st_ctime = lvb.lvb_ctime;
251
252         CDEBUG(D_DLMTRACE, "glimpse: size: %llu, blocks: %llu\n",
253                (long long)st->st_size, (long long)st->st_blocks);
254
255         obd_cancel(sbi->ll_dt_exp, lli->lli_smd, LCK_PR, &lockh);
256
257         RETURN(rc);
258 }
259
260 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
261                     struct lov_stripe_md *lsm, int mode,
262                     ldlm_policy_data_t *policy, struct lustre_handle *lockh,
263                     int ast_flags)
264 {
265         struct llu_sb_info *sbi = llu_i2sbi(inode);
266         struct intnl_stat *st = llu_i2stat(inode);
267         struct ost_lvb lvb;
268         int rc;
269         ENTRY;
270
271         LASSERT(!lustre_handle_is_used(lockh));
272         CLASSERT(ELDLM_OK == 0);
273
274         /* XXX phil: can we do this?  won't it screw the file size up? */
275         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
276             (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
277                 RETURN(0);
278
279         CDEBUG(D_DLMTRACE, "Locking inode %llu, start "LPU64" end "LPU64"\n",
280                (long long)st->st_ino, policy->l_extent.start,
281                policy->l_extent.end);
282
283         rc = obd_enqueue(sbi->ll_dt_exp, lsm, LDLM_EXTENT, policy, mode,
284                          &ast_flags, llu_extent_lock_callback,
285                          ldlm_completion_ast, llu_glimpse_callback, inode,
286                          sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
287         if (rc > 0)
288                 rc = -EIO;
289
290         inode_init_lvb(inode, &lvb);
291         obd_merge_lvb(sbi->ll_dt_exp, lsm, &lvb, 1);
292         if (policy->l_extent.start == 0 &&
293             policy->l_extent.end == OBD_OBJECT_EOF)
294                 st->st_size = lvb.lvb_size;
295
296         if (rc == 0) {
297                 st->st_mtime = lvb.lvb_mtime;
298                 st->st_atime = lvb.lvb_atime;
299                 st->st_ctime = lvb.lvb_ctime;
300         }
301
302         RETURN(rc);
303 }
304
305 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
306                 struct lov_stripe_md *lsm, int mode,
307                 struct lustre_handle *lockh)
308 {
309         struct llu_sb_info *sbi = llu_i2sbi(inode);
310         int rc;
311         ENTRY;
312
313         CLASSERT(ELDLM_OK == 0);
314
315         /* XXX phil: can we do this?  won't it screw the file size up? */
316         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
317             (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
318                 RETURN(0);
319
320         rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
321
322         RETURN(rc);
323 }
324
325 #define LLAP_MAGIC 12346789
326
327 struct ll_async_page {
328         int             llap_magic;
329         void           *llap_cookie;
330         int             llap_queued;
331         struct page    *llap_page;
332         struct inode   *llap_inode;
333 };
334
335 static void llu_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
336 {
337         struct ll_async_page *llap;
338         struct inode *inode;
339         struct lov_stripe_md *lsm;
340         obd_flag valid_flags;
341         ENTRY;
342
343         llap = LLAP_FROM_COOKIE(data);
344         inode = llap->llap_inode;
345         lsm = llu_i2info(inode)->lli_smd;
346
347         oa->o_id = lsm->lsm_object_id;
348         oa->o_valid = OBD_MD_FLID;
349         valid_flags = OBD_MD_FLTYPE | OBD_MD_FLATIME;
350         if (cmd & OBD_BRW_WRITE)
351                 valid_flags |= OBD_MD_FLMTIME | OBD_MD_FLCTIME;
352
353         obdo_from_inode(oa, inode, valid_flags);
354         EXIT;
355 }
356
357 /* called for each page in a completed rpc.*/
358 static void llu_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
359 {
360         struct ll_async_page *llap;
361         struct page *page;
362         ENTRY;
363
364         llap = LLAP_FROM_COOKIE(data);
365         llap->llap_queued = 0;
366         page = llap->llap_page;
367
368         if (rc != 0) {
369                 if (cmd & OBD_BRW_WRITE)
370                         CERROR("writeback error on page %p index %ld: %d\n",
371                                page, page->index, rc);
372         }
373         EXIT;
374 }
375
376 static struct obd_async_page_ops llu_async_page_ops = {
377         .ap_make_ready =        NULL,
378         .ap_refresh_count =     NULL,
379         .ap_fill_obdo =         llu_ap_fill_obdo,
380         .ap_completion =        llu_ap_completion,
381 };
382
383 static int llu_queue_pio(int cmd, struct llu_io_group *group,
384                          char *buf, size_t count, loff_t pos)
385 {
386         struct llu_inode_info *lli = llu_i2info(group->lig_inode);
387         struct intnl_stat *st = llu_i2stat(group->lig_inode);
388         struct lov_stripe_md *lsm = lli->lli_smd;
389         struct obd_export *exp = llu_i2obdexp(group->lig_inode);
390         struct page *pages = &group->lig_pages[group->lig_npages],*page = pages;
391         struct ll_async_page *llap = &group->lig_llaps[group->lig_npages];
392         void *llap_cookie = group->lig_llap_cookies +
393                 llap_cookie_size * group->lig_npages;
394         int i, rc, npages = 0, ret_bytes = 0;
395         int local_lock;
396         ENTRY;
397
398         if (!exp)
399                 RETURN(-EINVAL);
400
401         local_lock = group->lig_params->lrp_lock_mode != LCK_NL;
402         /* prepare the pages array */
403         do {
404                 unsigned long index, offset, bytes;
405
406                 offset = (pos & ~PAGE_CACHE_MASK);
407                 index = pos >> PAGE_CACHE_SHIFT;
408                 bytes = PAGE_CACHE_SIZE - offset;
409                 if (bytes > count)
410                         bytes = count;
411
412                 /* prevent read beyond file range */
413                 if (/* local_lock && */
414                     cmd == OBD_BRW_READ && pos + bytes >= st->st_size) {
415                         if (pos >= st->st_size)
416                                 break;
417                         bytes = st->st_size - pos;
418                 }
419
420                 /* prepare page for this index */
421                 page->index = index;
422                 page->addr = buf - offset;
423
424                 page->_offset = offset;
425                 page->_count = bytes;
426
427                 page++;
428                 npages++;
429                 count -= bytes;
430                 pos += bytes;
431                 buf += bytes;
432
433                 group->lig_rwcount += bytes;
434                 ret_bytes += bytes;
435         } while (count);
436
437         group->lig_npages += npages;
438
439         for (i = 0, page = pages; i < npages;
440              i++, page++, llap++, llap_cookie += llap_cookie_size){
441                 llap->llap_magic = LLAP_MAGIC;
442                 llap->llap_cookie = llap_cookie;
443                 rc = obd_prep_async_page(exp, lsm, NULL, page,
444                                          (obd_off)page->index << PAGE_SHIFT,
445                                          &llu_async_page_ops,
446                                          llap, &llap->llap_cookie);
447                 if (rc) {
448                         LASSERT(rc < 0);
449                         llap->llap_cookie = NULL;
450                         RETURN(rc);
451                 }
452                 CDEBUG(D_CACHE, "llap %p page %p group %p obj off "LPU64"\n",
453                        llap, page, llap->llap_cookie,
454                        (obd_off)pages->index << PAGE_SHIFT);
455                 page->private = (unsigned long)llap;
456                 llap->llap_page = page;
457                 llap->llap_inode = group->lig_inode;
458
459                 rc = obd_queue_group_io(exp, lsm, NULL, group->lig_oig,
460                                         llap->llap_cookie, cmd,
461                                         page->_offset, page->_count,
462                                         group->lig_params->lrp_brw_flags,
463                                         ASYNC_READY | ASYNC_URGENT |
464                                         ASYNC_COUNT_STABLE | ASYNC_GROUP_SYNC);
465                 if (!local_lock && cmd == OBD_BRW_READ) {
466                         /*
467                          * In OST-side locking case short reads cannot be
468                          * detected properly.
469                          *
470                          * The root of the problem is that
471                          *
472                          * kms = lov_merge_size(lsm, 1);
473                          * if (end >= kms)
474                          *         glimpse_size(inode);
475                          * else
476                          *         st->st_size = kms;
477                          *
478                          * logic in the read code (both llite and liblustre)
479                          * only works correctly when client holds DLM lock on
480                          * [start, end]. Without DLM lock KMS can be
481                          * completely out of date, and client can either make
482                          * spurious short-read (missing concurrent write), or
483                          * return stale data (missing concurrent
484                          * truncate). For llite client this is fatal, because
485                          * incorrect data are cached and can be later sent
486                          * back to the server (vide bug 5047). This is hard to
487                          * fix by handling short-reads on the server, as there
488                          * is no easy way to communicate file size (or amount
489                          * of bytes read/written) back to the client,
490                          * _especially_ because OSC pages can be sliced and
491                          * dices into multiple RPCs arbitrary. Fortunately,
492                          * liblustre doesn't cache data and the worst case is
493                          * that we get race with concurrent write or truncate.
494                          */
495                 }
496                 if (rc) {
497                         LASSERT(rc < 0);
498                         RETURN(rc);
499                 }
500
501                 llap->llap_queued = 1;
502         }
503
504         RETURN(ret_bytes);
505 }
506
507 static
508 struct llu_io_group * get_io_group(struct inode *inode, int maxpages,
509                                    struct lustre_rw_params *params)
510 {
511         struct llu_io_group *group;
512         int rc;
513
514         if (!llap_cookie_size)
515                 llap_cookie_size = obd_prep_async_page(llu_i2obdexp(inode),
516                                                        NULL, NULL, NULL, 0,
517                                                        NULL, NULL, NULL);
518
519         OBD_ALLOC(group, LLU_IO_GROUP_SIZE(maxpages));
520         if (!group)
521                 return ERR_PTR(-ENOMEM);
522
523         I_REF(inode);
524         group->lig_inode = inode;
525         group->lig_maxpages = maxpages;
526         group->lig_params = params;
527         group->lig_llaps = (struct ll_async_page *)(group + 1);
528         group->lig_pages = (struct page *)(&group->lig_llaps[maxpages]);
529         group->lig_llap_cookies = (void *)(&group->lig_pages[maxpages]);
530
531         rc = oig_init(&group->lig_oig);
532         if (rc) {
533                 OBD_FREE(group, LLU_IO_GROUP_SIZE(maxpages));
534                 return ERR_PTR(rc);
535         }
536
537         return group;
538 }
539
540 static int max_io_pages(ssize_t len, int iovlen)
541 {
542         return (((len + PAGE_SIZE -1) / PAGE_SIZE) + 2 + iovlen - 1);
543 }
544
545 static
546 void put_io_group(struct llu_io_group *group)
547 {
548         struct lov_stripe_md *lsm = llu_i2info(group->lig_inode)->lli_smd;
549         struct obd_export *exp = llu_i2obdexp(group->lig_inode);
550         struct ll_async_page *llap = group->lig_llaps;
551         int i;
552
553         for (i = 0; i < group->lig_npages; i++, llap++) {
554                 if (llap->llap_cookie)
555                         obd_teardown_async_page(exp, lsm, NULL,
556                                                 llap->llap_cookie);
557         }
558
559         I_RELE(group->lig_inode);
560
561         oig_release(group->lig_oig);
562         OBD_FREE(group, LLU_IO_GROUP_SIZE(group->lig_maxpages));
563 }
564
565 static
566 ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
567                         _SYSIO_OFF_T pos, ssize_t len,
568                         void *private)
569 {
570         struct llu_io_session *session = (struct llu_io_session *) private;
571         struct inode *inode = session->lis_inode;
572         struct llu_inode_info *lli = llu_i2info(inode);
573         struct intnl_stat *st = llu_i2stat(inode);
574         struct ll_file_data *fd = lli->lli_file_data;
575         struct lustre_handle lockh = {0};
576         struct lov_stripe_md *lsm = lli->lli_smd;
577         struct obd_export *exp = NULL;
578         struct llu_io_group *iogroup;
579         struct lustre_rw_params p;
580         struct ost_lvb lvb;
581         __u64 kms;
582         int err, is_read, iovidx, ret;
583         int local_lock;
584         ENTRY;
585
586         /* in a large iov read/write we'll be repeatedly called.
587          * so give a chance to answer cancel ast here
588          */
589         liblustre_wait_event(0);
590
591         exp = llu_i2obdexp(inode);
592         if (exp == NULL)
593                 RETURN(-EINVAL);
594
595         if (len == 0 || iovlen == 0)
596                 RETURN(0);
597
598         if (pos + len > lli->lli_maxbytes)
599                 RETURN(-ERANGE);
600
601         lustre_build_lock_params(session->lis_cmd, lli->lli_open_flags,
602                                  lli->lli_sbi->ll_lco.lco_flags,
603                                  pos, len, &p);
604
605         iogroup = get_io_group(inode, max_io_pages(len, iovlen), &p);
606         if (IS_ERR(iogroup))
607                 RETURN(PTR_ERR(iogroup));
608
609         local_lock = p.lrp_lock_mode != LCK_NL;
610
611         err = llu_extent_lock(fd, inode, lsm, p.lrp_lock_mode, &p.lrp_policy,
612                               &lockh, p.lrp_ast_flags);
613         if (err != ELDLM_OK)
614                 GOTO(err_put, err);
615
616         is_read = (session->lis_cmd == OBD_BRW_READ);
617         if (is_read) {
618                 /*
619                  * If OST-side locking is used, KMS can be completely out of
620                  * date, and, hence, cannot be used for short-read
621                  * detection. Rely in OST to handle short reads in that case.
622                  */
623                 inode_init_lvb(inode, &lvb);
624                 obd_merge_lvb(exp, lsm, &lvb, 1);
625                 kms = lvb.lvb_size;
626                 /* extent.end is last byte of the range */
627                 if (p.lrp_policy.l_extent.end >= kms) {
628                         /* A glimpse is necessary to determine whether
629                          * we return a short read or some zeroes at
630                          * the end of the buffer
631                          *
632                          * In the case of OST-side locking KMS can be
633                          * completely out of date and short-reads maybe
634                          * mishandled. See llu_queue_pio() for more detailed
635                          * comment.
636                          */
637                         if ((err = llu_glimpse_size(inode))) {
638                                 GOTO(err_unlock, err);
639                         }
640                 } else {
641                         st->st_size = kms;
642                 }
643         } else if (lli->lli_open_flags & O_APPEND) {
644                 pos = st->st_size;
645         }
646
647         for (iovidx = 0; iovidx < iovlen; iovidx++) {
648                 char *buf = (char *) iovec[iovidx].iov_base;
649                 size_t count = iovec[iovidx].iov_len;
650
651                 if (!count)
652                         continue;
653                 if (len < count)
654                         count = len;
655                 if (IS_BAD_PTR(buf) || IS_BAD_PTR(buf + count)) {
656                         GOTO(err_unlock, err = -EFAULT);
657                 }
658
659                 if (is_read) {
660                         if (/* local_lock && */ pos >= st->st_size)
661                                 break;
662                 } else {
663                         if (pos >= lli->lli_maxbytes) {
664                                 GOTO(err_unlock, err = -EFBIG);
665                         }
666                         if (pos + count >= lli->lli_maxbytes)
667                                 count = lli->lli_maxbytes - pos;
668                 }
669
670                 ret = llu_queue_pio(session->lis_cmd, iogroup, buf, count, pos);
671                 if (ret < 0) {
672                         GOTO(err_unlock, err = ret);
673                 } else {
674                         pos += ret;
675                         if (!is_read) {
676                                 LASSERT(ret == count);
677                                 obd_adjust_kms(exp, lsm, pos, 0);
678                                 /* file size grow immediately */
679                                 if (pos > st->st_size)
680                                         st->st_size = pos;
681                         }
682                         len -= ret;
683                         if (!len)
684                                 break;
685                 }
686         }
687         LASSERT(len == 0 || is_read); /* libsysio should guarantee this */
688
689         err = obd_trigger_group_io(exp, lsm, NULL, iogroup->lig_oig);
690         if (err)
691                 GOTO(err_unlock, err);
692
693         err = oig_wait(iogroup->lig_oig);
694         if (err) {
695                 CERROR("sync error %d, data corruption possible\n", err);
696                 GOTO(err_unlock, err);
697         }
698
699         ret = llu_extent_unlock(fd, inode, lsm, p.lrp_lock_mode, &lockh);
700         if (ret)
701                 CERROR("extent unlock error %d\n", ret);
702
703         session->lis_groups[session->lis_ngroups++] = iogroup;
704         RETURN(0);
705
706 err_unlock:
707         llu_extent_unlock(fd, inode, lsm, p.lrp_lock_mode, &lockh);
708 err_put:
709         put_io_group(iogroup);
710         RETURN((ssize_t)err);
711 }
712
713 static
714 struct llu_io_session *get_io_session(struct inode *ino, int ngroups, int cmd)
715 {
716         struct llu_io_session *session;
717
718         OBD_ALLOC(session, LLU_IO_SESSION_SIZE(ngroups));
719         if (!session)
720                 return NULL;
721
722         I_REF(ino);
723         session->lis_inode = ino;
724         session->lis_max_groups = ngroups;
725         session->lis_cmd = cmd;
726         return session;
727 }
728
729 static void put_io_session(struct llu_io_session *session)
730 {
731         int i;
732
733         for (i = 0; i < session->lis_ngroups; i++) {
734                 if (session->lis_groups[i]) {
735                         put_io_group(session->lis_groups[i]);
736                         session->lis_groups[i] = NULL;
737                 }
738         }
739
740         I_RELE(session->lis_inode);
741         OBD_FREE(session, LLU_IO_SESSION_SIZE(session->lis_max_groups));
742 }
743
744 static int llu_file_rwx(struct inode *ino,
745                         struct ioctx *ioctx,
746                         int read)
747 {
748         struct llu_io_session *session;
749         ssize_t cc;
750         int cmd = read ? OBD_BRW_READ : OBD_BRW_WRITE;
751         ENTRY;
752
753         LASSERT(ioctx->ioctx_xtvlen >= 0);
754         LASSERT(ioctx->ioctx_iovlen >= 0);
755
756         liblustre_wait_event(0);
757
758         if (!ioctx->ioctx_xtvlen)
759                 RETURN(0);
760
761         /* XXX consider other types later */
762         if (S_ISDIR(llu_i2stat(ino)->st_mode))
763                 RETURN(-EISDIR);
764         if (!S_ISREG(llu_i2stat(ino)->st_mode))
765                 RETURN(-EOPNOTSUPP);
766
767         session = get_io_session(ino, ioctx->ioctx_xtvlen * 2, cmd);
768         if (!session)
769                 RETURN(-ENOMEM);
770
771         cc = _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
772                                       ioctx->ioctx_iov, ioctx->ioctx_iovlen,
773                                       llu_file_prwv, session);
774
775         if (cc >= 0) {
776                 LASSERT(!ioctx->ioctx_cc);
777                 ioctx->ioctx_private = session;
778                 cc = 0;
779         } else {
780                 put_io_session(session);
781         }
782
783         liblustre_wait_event(0);
784         RETURN(cc);
785 }
786
787 int llu_iop_read(struct inode *ino,
788                  struct ioctx *ioctx)
789 {
790         /* BUG: 5972 */
791         struct intnl_stat *st = llu_i2stat(ino);
792         st->st_atime = CURRENT_TIME;
793
794         return llu_file_rwx(ino, ioctx, 1);
795 }
796
797 int llu_iop_write(struct inode *ino,
798                   struct ioctx *ioctx)
799 {
800         struct intnl_stat *st = llu_i2stat(ino);
801         st->st_mtime = st->st_ctime = CURRENT_TIME;
802
803         return llu_file_rwx(ino, ioctx, 0);
804 }
805
806 int llu_iop_iodone(struct ioctx *ioctx)
807 {
808         struct llu_io_session *session;
809         struct llu_io_group *group;
810         int i, err = 0, rc = 0;
811         ENTRY;
812
813         liblustre_wait_event(0);
814
815         session = (struct llu_io_session *) ioctx->ioctx_private;
816         LASSERT(session);
817         LASSERT(!IS_ERR(session));
818
819         for (i = 0; i < session->lis_ngroups; i++) {
820                 group = session->lis_groups[i];
821                 if (group) {
822                         if (!rc) {
823                                 err = oig_wait(group->lig_oig);
824                                 if (err)
825                                         rc = err;
826                         }
827                         if (!rc)
828                                 ioctx->ioctx_cc += group->lig_rwcount;
829                         put_io_group(group);
830                         session->lis_groups[i] = NULL;
831                 }
832         }
833
834         if (rc) {
835                 LASSERT(rc < 0);
836                 ioctx->ioctx_cc = -1;
837                 ioctx->ioctx_errno = -rc;
838         }
839
840         put_io_session(session);
841         ioctx->ioctx_private = NULL;
842         liblustre_wait_event(0);
843
844         RETURN(1);
845 }