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