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