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