Whamcloud - gitweb
b=17167 libcfs: ensure all libcfs exported symbols to have cfs_ prefix
[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 #include "llite_lib.h"
65
66 typedef ssize_t llu_file_piov_t(const struct iovec *iovec, int iovlen,
67                                 _SYSIO_OFF_T pos, ssize_t len,
68                                 void *private);
69
70 size_t llap_cookie_size;
71
72 static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
73 {
74         struct llu_inode_info *lli = llu_i2info(inode);
75         struct lov_stripe_md *lsm = lli->lli_smd;
76         struct obd_export *exp = llu_i2obdexp(inode);
77         struct {
78                 char name[16];
79                 struct ldlm_lock *lock;
80         } key = { .name = KEY_LOCK_TO_STRIPE, .lock = lock };
81         __u32 stripe, vallen = sizeof(stripe);
82         int rc;
83         ENTRY;
84
85         if (lsm->lsm_stripe_count == 1)
86                 RETURN(0);
87
88         /* get our offset in the lov */
89         rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe, lsm);
90         if (rc != 0) {
91                 CERROR("obd_get_info: rc = %d\n", rc);
92                 LBUG();
93         }
94         LASSERT(stripe < lsm->lsm_stripe_count);
95         RETURN(stripe);
96 }
97
98 int llu_extent_lock_cancel_cb(struct ldlm_lock *lock,
99                               struct ldlm_lock_desc *new, void *data,
100                               int flag)
101 {
102         struct lustre_handle lockh = { 0 };
103         int rc;
104         ENTRY;
105
106         if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
107                 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
108                 LBUG();
109         }
110
111         switch (flag) {
112         case LDLM_CB_BLOCKING:
113                 ldlm_lock2handle(lock, &lockh);
114                 rc = ldlm_cli_cancel(&lockh);
115                 if (rc != ELDLM_OK)
116                         CERROR("ldlm_cli_cancel failed: %d\n", rc);
117                 break;
118         case LDLM_CB_CANCELING: {
119                 struct inode *inode;
120                 struct llu_inode_info *lli;
121                 struct lov_stripe_md *lsm;
122                 __u32 stripe;
123                 __u64 kms;
124
125                 /* This lock wasn't granted, don't try to evict pages */
126                 if (lock->l_req_mode != lock->l_granted_mode)
127                         RETURN(0);
128
129                 inode = llu_inode_from_lock(lock);
130                 if (!inode)
131                         RETURN(0);
132                 lli= llu_i2info(inode);
133                 if (!lli)
134                         goto iput;
135                 if (!lli->lli_smd)
136                         goto iput;
137                 lsm = lli->lli_smd;
138
139                 stripe = llu_lock_to_stripe_offset(inode, lock);
140                 lock_res_and_lock(lock);
141                 kms = ldlm_extent_shift_kms(lock,
142                                             lsm->lsm_oinfo[stripe]->loi_kms);
143                 unlock_res_and_lock(lock);
144                 if (lsm->lsm_oinfo[stripe]->loi_kms != kms)
145                         LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
146                                    lsm->lsm_oinfo[stripe]->loi_kms, kms);
147                 loi_kms_set(lsm->lsm_oinfo[stripe], kms);
148 iput:
149                 I_RELE(inode);
150                 break;
151         }
152         default:
153                 LBUG();
154         }
155
156         RETURN(0);
157 }
158
159 static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
160 {
161         struct ptlrpc_request *req = reqp;
162         struct inode *inode = llu_inode_from_lock(lock);
163         struct llu_inode_info *lli;
164         struct ost_lvb *lvb;
165         int rc, stripe = 0;
166         ENTRY;
167
168         if (inode == NULL)
169                 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
170         lli = llu_i2info(inode);
171         if (lli == NULL)
172                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
173         if (lli->lli_smd == NULL)
174                 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
175
176         /* First, find out which stripe index this lock corresponds to. */
177         if (lli->lli_smd->lsm_stripe_count > 1)
178                 stripe = llu_lock_to_stripe_offset(inode, lock);
179
180         req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
181         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
182                              sizeof(*lvb));
183         rc = req_capsule_server_pack(&req->rq_pill);
184         if (rc) {
185                 CERROR("failed pack reply: %d\n", rc);
186                 GOTO(iput, rc);
187         }
188
189         lvb = req_capsule_server_get(&req->rq_pill, &RMF_DLM_LVB);
190         lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe]->loi_kms;
191
192         LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
193                    (__u64)llu_i2stat(inode)->st_size, stripe,lvb->lvb_size);
194  iput:
195         I_RELE(inode);
196  out:
197         /* These errors are normal races, so we don't want to fill the console
198          * with messages by calling ptlrpc_error() */
199         if (rc == -ELDLM_NO_LOCK_DATA)
200                 lustre_pack_reply(req, 1, NULL, NULL);
201
202         req->rq_status = rc;
203         return rc;
204 }
205
206 int llu_merge_lvb(struct inode *inode)
207 {
208         struct llu_inode_info *lli = llu_i2info(inode);
209         struct llu_sb_info *sbi = llu_i2sbi(inode);
210         struct intnl_stat *st = llu_i2stat(inode);
211         struct ost_lvb lvb;
212         int rc;
213         ENTRY;
214
215         inode_init_lvb(inode, &lvb);
216         rc = obd_merge_lvb(sbi->ll_dt_exp, lli->lli_smd, &lvb, 0);
217         st->st_size = lvb.lvb_size;
218         st->st_blocks = lvb.lvb_blocks;
219         /* handle st_blocks overflow gracefully */
220         if (st->st_blocks < lvb.lvb_blocks)
221                 st->st_blocks = ~0UL;
222         st->st_mtime = lvb.lvb_mtime;
223         st->st_atime = lvb.lvb_atime;
224         st->st_ctime = lvb.lvb_ctime;
225
226         RETURN(rc);
227 }
228
229 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
230                     struct lov_stripe_md *lsm, int mode,
231                     ldlm_policy_data_t *policy, struct lustre_handle *lockh,
232                     int ast_flags)
233 {
234         struct llu_sb_info *sbi = llu_i2sbi(inode);
235         struct intnl_stat *st = llu_i2stat(inode);
236         struct ldlm_enqueue_info einfo = { 0 };
237         struct obd_info oinfo = { { { 0 } } };
238         struct ost_lvb lvb;
239         int rc;
240         ENTRY;
241
242         LASSERT(!lustre_handle_is_used(lockh));
243         CLASSERT(ELDLM_OK == 0);
244
245         /* XXX phil: can we do this?  won't it screw the file size up? */
246         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
247             (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
248                 RETURN(0);
249
250         CDEBUG(D_DLMTRACE, "Locking inode %llu, start "LPU64" end "LPU64"\n",
251                (__u64)st->st_ino, policy->l_extent.start,
252                policy->l_extent.end);
253
254         einfo.ei_type = LDLM_EXTENT;
255         einfo.ei_mode = mode;
256         einfo.ei_cb_bl = llu_extent_lock_cancel_cb;
257         einfo.ei_cb_cp = ldlm_completion_ast;
258         einfo.ei_cb_gl = llu_glimpse_callback;
259         einfo.ei_cbdata = inode;
260
261         oinfo.oi_policy = *policy;
262         oinfo.oi_lockh = lockh;
263         oinfo.oi_md = lsm;
264         oinfo.oi_flags = ast_flags;
265
266         rc = obd_enqueue(sbi->ll_dt_exp, &oinfo, &einfo, NULL);
267         *policy = oinfo.oi_policy;
268         if (rc > 0)
269                 rc = -EIO;
270
271         inode_init_lvb(inode, &lvb);
272         obd_merge_lvb(sbi->ll_dt_exp, lsm, &lvb, 1);
273         if (policy->l_extent.start == 0 &&
274             policy->l_extent.end == OBD_OBJECT_EOF)
275                 st->st_size = lvb.lvb_size;
276
277         if (rc == 0) {
278                 st->st_mtime = lvb.lvb_mtime;
279                 st->st_atime = lvb.lvb_atime;
280                 st->st_ctime = lvb.lvb_ctime;
281         }
282
283         RETURN(rc);
284 }
285
286 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
287                 struct lov_stripe_md *lsm, int mode,
288                 struct lustre_handle *lockh)
289 {
290         struct llu_sb_info *sbi = llu_i2sbi(inode);
291         int rc;
292         ENTRY;
293
294         CLASSERT(ELDLM_OK == 0);
295
296         /* XXX phil: can we do this?  won't it screw the file size up? */
297         if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
298             (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
299                 RETURN(0);
300
301         rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
302
303         RETURN(rc);
304 }
305
306 static
307 ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
308                         _SYSIO_OFF_T pos, ssize_t len,
309                         void *private)
310 {
311         struct llu_io_session *session = (struct llu_io_session *) private;
312         struct inode *inode = session->lis_inode;
313         struct llu_inode_info *lli = llu_i2info(inode);
314         int err;
315         struct lu_env *env;
316         struct cl_io  *io;
317         struct slp_io *sio;
318         int refcheck;
319         ENTRY;
320
321         /* in a large iov read/write we'll be repeatedly called.
322          * so give a chance to answer cancel ast here
323          */
324         liblustre_wait_event(0);
325
326         if (len == 0 || iovlen == 0)
327                 RETURN(0);
328
329         if (pos + len > lli->lli_maxbytes)
330                 RETURN(-ERANGE);
331
332         env = cl_env_get(&refcheck);
333         if (IS_ERR(env))
334                 RETURN(PTR_ERR(env));
335
336         io = &ccc_env_info(env)->cti_io;
337
338         if (cl_io_rw_init(env, io, session->lis_cmd == OBD_BRW_WRITE?CIT_WRITE:
339                                                                       CIT_READ,
340                           pos, len) == 0) {
341                 struct ccc_io *cio;
342                 sio = slp_env_io(env);
343                 cio = ccc_env_io(env);
344                 /* XXX this is not right: cio->cui_iov can be modified. */
345                 cio->cui_iov = (struct iovec *)iovec;
346                 cio->cui_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         memset(io, 0, sizeof *io);
432
433         io->u.ci_rw.crw_nonblock = lli->lli_open_flags & O_NONBLOCK;
434         if (write)
435                 io->u.ci_wr.wr_append = lli->lli_open_flags & O_APPEND;
436         io->ci_obj  = llu_i2info(inode)->lli_clob;
437
438         if ((lli->lli_open_flags & O_APPEND) && write)
439                 io->ci_lockreq = CILR_MANDATORY;
440         else
441                 io->ci_lockreq = CILR_NEVER;
442 }
443
444 int llu_iop_read(struct inode *ino,
445                  struct ioctx *ioctx)
446 {
447         struct intnl_stat *st = llu_i2stat(ino);
448         struct lu_env *env;
449         struct cl_io  *io;
450         int refcheck;
451         int ret;
452
453         /* BUG: 5972 */
454         st->st_atime = CFS_CURRENT_TIME;
455
456         env = cl_env_get(&refcheck);
457         if (IS_ERR(env))
458                 RETURN(PTR_ERR(env));
459
460         io = &ccc_env_info(env)->cti_io;
461         llu_io_init(io, ino, 0);
462
463         ret = llu_file_rwx(ino, ioctx, 1);
464
465         cl_env_put(env, &refcheck);
466         return ret;
467 }
468
469 int llu_iop_write(struct inode *ino,
470                   struct ioctx *ioctx)
471 {
472         struct intnl_stat *st = llu_i2stat(ino);
473         struct lu_env *env;
474         struct cl_io  *io;
475         int refcheck;
476         int ret;
477
478         st->st_mtime = st->st_ctime = CFS_CURRENT_TIME;
479
480         env = cl_env_get(&refcheck);
481         if (IS_ERR(env))
482                 RETURN(PTR_ERR(env));
483
484         io = &ccc_env_info(env)->cti_io;
485         llu_io_init(io, ino, 1);
486
487         ret = llu_file_rwx(ino, ioctx, 0);
488         cl_env_put(env, &refcheck);
489         return ret;
490 }
491
492 int llu_iop_iodone(struct ioctx *ioctx)
493 {
494         struct llu_io_session *session;
495         struct lu_env *env;
496         struct cl_io  *io;
497         int refcheck;
498         ENTRY;
499
500         liblustre_wait_event(0);
501
502         env = cl_env_get(&refcheck);
503         if (IS_ERR(env))
504                 RETURN(PTR_ERR(env));
505
506         io = &ccc_env_info(env)->cti_io;
507         cl_io_fini(env, io);
508         cl_env_put(env, &refcheck);
509         session = (struct llu_io_session *) ioctx->ioctx_private;
510         LASSERT(session);
511         LASSERT(!IS_ERR(session));
512
513         if (session->lis_rc == 0) {
514                 ioctx->ioctx_cc = session->lis_rwcount;
515         } else {
516                 LASSERT(session->lis_rc < 0);
517                 ioctx->ioctx_cc = -1;
518                 ioctx->ioctx_errno = -session->lis_rc;
519         }
520
521         put_io_session(session);
522         ioctx->ioctx_private = NULL;
523         liblustre_wait_event(0);
524
525         RETURN(1);
526 }