Whamcloud - gitweb
LU-467 Not reinitialize "cl_io" in "llu_file_prwv()"
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * Copyright (c) 2011 Whamcloud, Inc.
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * lustre/liblustre/rw.c
40  *
41  * Lustre Light block IO
42  */
43
44 #define DEBUG_SUBSYSTEM S_LLITE
45
46 #include <stdlib.h>
47 #include <string.h>
48 #include <assert.h>
49 #include <time.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <sys/queue.h>
53 #include <fcntl.h>
54 #include <sys/uio.h>
55
56 #include "llite_lib.h"
57
58 typedef ssize_t llu_file_piov_t(const struct iovec *iovec, int iovlen,
59                                 _SYSIO_OFF_T pos, ssize_t len,
60                                 void *private);
61
62 size_t llap_cookie_size;
63
64 static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
65 {
66         struct llu_inode_info *lli = llu_i2info(inode);
67         struct lov_stripe_md *lsm = lli->lli_smd;
68         struct obd_export *exp = llu_i2obdexp(inode);
69         struct {
70                 char name[16];
71                 struct ldlm_lock *lock;
72         } key = { .name = KEY_LOCK_TO_STRIPE, .lock = lock };
73         __u32 stripe, vallen = sizeof(stripe);
74         int rc;
75         ENTRY;
76
77         if (lsm->lsm_stripe_count == 1)
78                 RETURN(0);
79
80         /* get our offset in the lov */
81         rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe, lsm);
82         if (rc != 0) {
83                 CERROR("obd_get_info: rc = %d\n", rc);
84                 LBUG();
85         }
86         LASSERT(stripe < lsm->lsm_stripe_count);
87         RETURN(stripe);
88 }
89
90 int llu_extent_lock_cancel_cb(struct ldlm_lock *lock,
91                               struct ldlm_lock_desc *new, void *data,
92                               int flag)
93 {
94         struct lustre_handle lockh = { 0 };
95         int rc;
96         ENTRY;
97
98         if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
99                 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
100                 LBUG();
101         }
102
103         switch (flag) {
104         case LDLM_CB_BLOCKING:
105                 ldlm_lock2handle(lock, &lockh);
106                 rc = ldlm_cli_cancel(&lockh);
107                 if (rc != ELDLM_OK)
108                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
109                 break;
110         case LDLM_CB_CANCELING: {
111                 struct inode *inode;
112                 struct llu_inode_info *lli;
113                 struct lov_stripe_md *lsm;
114                 __u32 stripe;
115                 __u64 kms;
116
117                 /* This lock wasn't granted, don't try to evict pages */
118                 if (lock->l_req_mode != lock->l_granted_mode)
119                         RETURN(0);
120
121                 inode = llu_inode_from_lock(lock);
122                 if (!inode)
123                         RETURN(0);
124                 lli= llu_i2info(inode);
125                 if (!lli)
126                         goto iput;
127                 if (!lli->lli_smd)
128                         goto iput;
129                 lsm = lli->lli_smd;
130
131                 stripe = llu_lock_to_stripe_offset(inode, lock);
132                 lock_res_and_lock(lock);
133                 kms = ldlm_extent_shift_kms(lock,
134                                             lsm->lsm_oinfo[stripe]->loi_kms);
135                 unlock_res_and_lock(lock);
136                 if (lsm->lsm_oinfo[stripe]->loi_kms != kms)
137                         LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
138                                    lsm->lsm_oinfo[stripe]->loi_kms, kms);
139                 loi_kms_set(lsm->lsm_oinfo[stripe], kms);
140 iput:
141                 I_RELE(inode);
142                 break;
143         }
144         default:
145                 LBUG();
146         }
147
148         RETURN(0);
149 }
150
151 static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
152 {
153         struct ptlrpc_request *req = reqp;
154         struct inode *inode = llu_inode_from_lock(lock);
155         struct llu_inode_info *lli;
156         struct ost_lvb *lvb;
157         int rc, stripe = 0;
158         ENTRY;
159
160         if (inode == NULL)
161                 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
162         lli = llu_i2info(inode);
163         if (lli == NULL)
164                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
165         if (lli->lli_smd == NULL)
166                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
167
168         /* First, find out which stripe index this lock corresponds to. */
169         if (lli->lli_smd->lsm_stripe_count > 1)
170                 stripe = llu_lock_to_stripe_offset(inode, lock);
171
172         req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
173         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
174                              sizeof(*lvb));
175         rc = req_capsule_server_pack(&req->rq_pill);
176         if (rc) {
177                 CERROR("failed pack reply: %d\n", rc);
178                 GOTO(iput, rc);
179         }
180
181         lvb = req_capsule_server_get(&req->rq_pill, &RMF_DLM_LVB);
182         lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe]->loi_kms;
183
184         LDLM_DEBUG(lock, "i_size: "LPU64" -> stripe number %u -> kms "LPU64,
185                    (__u64)llu_i2stat(inode)->st_size, stripe,lvb->lvb_size);
186  iput:
187         I_RELE(inode);
188  out:
189         /* These errors are normal races, so we don't want to fill the console
190          * with messages by calling ptlrpc_error() */
191         if (rc == -ELDLM_NO_LOCK_DATA)
192                 lustre_pack_reply(req, 1, NULL, NULL);
193
194         req->rq_status = rc;
195         return rc;
196 }
197
198 int llu_merge_lvb(struct inode *inode)
199 {
200         struct llu_inode_info *lli = llu_i2info(inode);
201         struct llu_sb_info *sbi = llu_i2sbi(inode);
202         struct intnl_stat *st = llu_i2stat(inode);
203         struct ost_lvb lvb;
204         int rc;
205         ENTRY;
206
207         lov_stripe_lock(lli->lli_smd);
208         inode_init_lvb(inode, &lvb);
209         /* merge timestamps the most resently obtained from mds with
210            timestamps obtained from osts */
211         lvb.lvb_atime = lli->lli_lvb.lvb_atime;
212         lvb.lvb_mtime = lli->lli_lvb.lvb_mtime;
213         lvb.lvb_ctime = lli->lli_lvb.lvb_ctime;
214         rc = obd_merge_lvb(sbi->ll_dt_exp, lli->lli_smd, &lvb, 0);
215         st->st_size = lvb.lvb_size;
216         st->st_blocks = lvb.lvb_blocks;
217         /* handle st_blocks overflow gracefully */
218         if (st->st_blocks < lvb.lvb_blocks)
219                 st->st_blocks = ~0UL;
220         st->st_mtime = lvb.lvb_mtime;
221         st->st_atime = lvb.lvb_atime;
222         st->st_ctime = lvb.lvb_ctime;
223         lov_stripe_unlock(lli->lli_smd);
224
225         RETURN(rc);
226 }
227
228 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
229                     struct lov_stripe_md *lsm, int mode,
230                     ldlm_policy_data_t *policy, struct lustre_handle *lockh,
231                     int ast_flags)
232 {
233         struct llu_sb_info *sbi = llu_i2sbi(inode);
234         struct intnl_stat *st = llu_i2stat(inode);
235         struct ldlm_enqueue_info einfo = { 0 };
236         struct obd_info oinfo = { { { 0 } } };
237         struct ost_lvb lvb;
238         int rc;
239         ENTRY;
240
241         LASSERT(!lustre_handle_is_used(lockh));
242         CLASSERT(ELDLM_OK == 0);
243
244         /* XXX phil: can we do this?  won't it screw the file size up? */
245         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
246             (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
247                 RETURN(0);
248
249         CDEBUG(D_DLMTRACE, "Locking inode "LPU64", start "LPU64" end "LPU64"\n",
250                (__u64)st->st_ino, policy->l_extent.start,
251                policy->l_extent.end);
252
253         einfo.ei_type = LDLM_EXTENT;
254         einfo.ei_mode = mode;
255         einfo.ei_cb_bl = llu_extent_lock_cancel_cb;
256         einfo.ei_cb_cp = ldlm_completion_ast;
257         einfo.ei_cb_gl = llu_glimpse_callback;
258         einfo.ei_cbdata = inode;
259
260         oinfo.oi_policy = *policy;
261         oinfo.oi_lockh = lockh;
262         oinfo.oi_md = lsm;
263         oinfo.oi_flags = ast_flags;
264
265         rc = obd_enqueue(sbi->ll_dt_exp, &oinfo, &einfo, NULL);
266         *policy = oinfo.oi_policy;
267         if (rc > 0)
268                 rc = -EIO;
269
270         inode_init_lvb(inode, &lvb);
271         obd_merge_lvb(sbi->ll_dt_exp, lsm, &lvb, 1);
272         if (policy->l_extent.start == 0 &&
273             policy->l_extent.end == OBD_OBJECT_EOF)
274                 st->st_size = lvb.lvb_size;
275
276         if (rc == 0) {
277                 st->st_mtime = lvb.lvb_mtime;
278                 st->st_atime = lvb.lvb_atime;
279                 st->st_ctime = lvb.lvb_ctime;
280         }
281
282         RETURN(rc);
283 }
284
285 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
286                 struct lov_stripe_md *lsm, int mode,
287                 struct lustre_handle *lockh)
288 {
289         struct llu_sb_info *sbi = llu_i2sbi(inode);
290         int rc;
291         ENTRY;
292
293         CLASSERT(ELDLM_OK == 0);
294
295         /* XXX phil: can we do this?  won't it screw the file size up? */
296         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
297             (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
298                 RETURN(0);
299
300         rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
301
302         RETURN(rc);
303 }
304
305 static
306 ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
307                         _SYSIO_OFF_T pos, ssize_t len,
308                         void *private)
309 {
310         struct llu_io_session *session = (struct llu_io_session *) private;
311         struct inode *inode = session->lis_inode;
312         struct llu_inode_info *lli = llu_i2info(inode);
313         int err;
314         struct lu_env *env;
315         struct cl_io  *io;
316         struct slp_io *sio;
317         int refcheck;
318         ENTRY;
319
320         /* in a large iov read/write we'll be repeatedly called.
321          * so give a chance to answer cancel ast here
322          */
323         liblustre_wait_event(0);
324
325         if (len == 0 || iovlen == 0)
326                 RETURN(0);
327
328         if (pos + len > lli->lli_maxbytes)
329                 RETURN(-ERANGE);
330
331         env = cl_env_get(&refcheck);
332         if (IS_ERR(env))
333                 RETURN(PTR_ERR(env));
334
335         /* Do NOT call "ccc_env_thread_io()" again to prevent reinitializing */
336         io = &ccc_env_info(env)->cti_io;
337         if (cl_io_rw_init(env, io, session->lis_cmd == OBD_BRW_WRITE?CIT_WRITE:
338                                                                       CIT_READ,
339                           pos, len) == 0) {
340                 struct ccc_io *cio;
341                 sio = slp_env_io(env);
342                 cio = ccc_env_io(env);
343                 /* XXX this is not right: cio->cui_iov can be modified. */
344                 cio->cui_iov = (struct iovec *)iovec;
345                 cio->cui_nrsegs = iovlen;
346                 cio->cui_tot_nrsegs = iovlen;
347                 sio->sio_session = session;
348                 err = cl_io_loop(env, io);
349         } else {
350                 /* XXX WTF? */
351                 LBUG();
352         }
353         cl_io_fini(env, io);
354         cl_env_put(env, &refcheck);
355
356         if (err < 0)
357                 RETURN(err);
358
359         RETURN(len);
360 }
361
362 static
363 struct llu_io_session *get_io_session(struct inode *ino, int ngroups, int cmd)
364 {
365         struct llu_io_session *session;
366
367         OBD_ALLOC_PTR(session);
368         if (!session)
369                 return NULL;
370
371         I_REF(ino);
372         session->lis_inode = ino;
373         session->lis_max_groups = ngroups;
374         session->lis_cmd = cmd;
375         return session;
376 }
377
378 static void put_io_session(struct llu_io_session *session)
379 {
380         I_RELE(session->lis_inode);
381         OBD_FREE_PTR(session);
382 }
383
384 static int llu_file_rwx(struct inode *ino,
385                         struct ioctx *ioctx,
386                         int read)
387 {
388         struct llu_io_session *session;
389         ssize_t cc;
390         int cmd = read ? OBD_BRW_READ : OBD_BRW_WRITE;
391         ENTRY;
392
393         LASSERT(ioctx->ioctx_xtvlen >= 0);
394         LASSERT(ioctx->ioctx_iovlen >= 0);
395
396         liblustre_wait_event(0);
397
398         if (!ioctx->ioctx_xtvlen)
399                 RETURN(0);
400
401         /* XXX consider other types later */
402         if (S_ISDIR(llu_i2stat(ino)->st_mode))
403                 RETURN(-EISDIR);
404         if (!S_ISREG(llu_i2stat(ino)->st_mode))
405                 RETURN(-EOPNOTSUPP);
406
407         session = get_io_session(ino, ioctx->ioctx_xtvlen * 2, cmd);
408         if (!session)
409                 RETURN(-ENOMEM);
410
411         cc = _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
412                                       ioctx->ioctx_iov, ioctx->ioctx_iovlen,
413                                       llu_file_prwv, session);
414
415         if (cc >= 0) {
416                 LASSERT(!ioctx->ioctx_cc);
417                 ioctx->ioctx_private = session;
418                 cc = 0;
419         } else {
420                 put_io_session(session);
421         }
422
423         liblustre_wait_event(0);
424         RETURN(cc);
425 }
426
427 void llu_io_init(struct cl_io *io, struct inode *inode, int write)
428 {
429         struct llu_inode_info *lli = llu_i2info(inode);
430
431         io->u.ci_rw.crw_nonblock = lli->lli_open_flags & O_NONBLOCK;
432         if (write)
433                 io->u.ci_wr.wr_append = lli->lli_open_flags & O_APPEND;
434         io->ci_obj  = llu_i2info(inode)->lli_clob;
435
436         if ((lli->lli_open_flags & O_APPEND) && write)
437                 io->ci_lockreq = CILR_MANDATORY;
438         else
439                 io->ci_lockreq = CILR_NEVER;
440 }
441
442 int llu_iop_read(struct inode *ino,
443                  struct ioctx *ioctx)
444 {
445         struct intnl_stat *st = llu_i2stat(ino);
446         struct lu_env *env;
447         struct cl_io  *io;
448         int refcheck;
449         int ret;
450
451         /* BUG: 5972 */
452         st->st_atime = CFS_CURRENT_TIME;
453
454         env = cl_env_get(&refcheck);
455         if (IS_ERR(env))
456                 RETURN(PTR_ERR(env));
457
458         io = ccc_env_thread_io(env);
459         llu_io_init(io, ino, 0);
460
461         ret = llu_file_rwx(ino, ioctx, 1);
462
463         cl_env_put(env, &refcheck);
464         return ret;
465 }
466
467 int llu_iop_write(struct inode *ino,
468                   struct ioctx *ioctx)
469 {
470         struct intnl_stat *st = llu_i2stat(ino);
471         struct lu_env *env;
472         struct cl_io  *io;
473         int refcheck;
474         int ret;
475
476         st->st_mtime = st->st_ctime = CFS_CURRENT_TIME;
477
478         env = cl_env_get(&refcheck);
479         if (IS_ERR(env))
480                 RETURN(PTR_ERR(env));
481
482         io = ccc_env_thread_io(env);
483         llu_io_init(io, ino, 1);
484
485         ret = llu_file_rwx(ino, ioctx, 0);
486         cl_env_put(env, &refcheck);
487         return ret;
488 }
489
490 int llu_iop_iodone(struct ioctx *ioctx)
491 {
492         struct llu_io_session *session;
493         struct lu_env *env;
494         struct cl_io  *io;
495         int refcheck;
496         ENTRY;
497
498         liblustre_wait_event(0);
499
500         env = cl_env_get(&refcheck);
501         if (IS_ERR(env))
502                 RETURN(PTR_ERR(env));
503
504         io = &ccc_env_info(env)->cti_io;
505         cl_io_fini(env, io);
506         cl_env_put(env, &refcheck);
507         session = (struct llu_io_session *) ioctx->ioctx_private;
508         LASSERT(session);
509         LASSERT(!IS_ERR(session));
510
511         if (session->lis_rc == 0) {
512                 ioctx->ioctx_cc = session->lis_rwcount;
513         } else {
514                 LASSERT(session->lis_rc < 0);
515                 ioctx->ioctx_cc = -1;
516                 ioctx->ioctx_errno = -session->lis_rc;
517         }
518
519         put_io_session(session);
520         ioctx->ioctx_private = NULL;
521         liblustre_wait_event(0);
522
523         RETURN(1);
524 }