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