4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/liblustre/rw.c
38 * Lustre Light block IO
41 #define DEBUG_SUBSYSTEM S_LLITE
47 #include <sys/types.h>
49 #include <sys/queue.h>
53 #include "llite_lib.h"
55 typedef ssize_t llu_file_piov_t(const struct iovec *iovec, int iovlen,
56 _SYSIO_OFF_T pos, ssize_t len,
59 size_t llap_cookie_size;
61 static int llu_lock_to_stripe_offset(struct obd_export *exp,
62 struct lov_stripe_md *lsm,
63 struct ldlm_lock *lock)
67 struct ldlm_lock *lock;
68 } key = { .name = KEY_LOCK_TO_STRIPE, .lock = lock };
69 __u32 stripe, vallen = sizeof(stripe);
73 if (lsm == NULL || lsm->lsm_stripe_count == 1)
76 /* get our offset in the lov */
77 rc = obd_get_info(NULL, exp, sizeof(key), &key, &vallen, &stripe, lsm);
79 CERROR("obd_get_info: rc = %d\n", rc);
82 LASSERT(stripe < lsm->lsm_stripe_count);
86 int llu_extent_lock_cancel_cb(struct ldlm_lock *lock,
87 struct ldlm_lock_desc *new, void *data,
90 struct lustre_handle lockh = { 0 };
94 if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
95 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
100 case LDLM_CB_BLOCKING:
101 ldlm_lock2handle(lock, &lockh);
102 rc = ldlm_cli_cancel(&lockh);
104 CERROR("ldlm_cli_cancel failed: %d\n", rc);
106 case LDLM_CB_CANCELING: {
108 struct llu_inode_info *lli;
109 struct lov_stripe_md *lsm;
113 /* This lock wasn't granted, don't try to evict pages */
114 if (lock->l_req_mode != lock->l_granted_mode)
117 inode = llu_inode_from_lock(lock);
120 lli= llu_i2info(inode);
123 if (!lli->lli_has_smd)
126 lsm = ccc_inode_lsm_get(inode);
130 stripe = llu_lock_to_stripe_offset(llu_i2obdexp(inode),
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);
152 static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
154 struct ptlrpc_request *req = reqp;
155 struct inode *inode = llu_inode_from_lock(lock);
156 struct llu_inode_info *lli;
158 struct lov_stripe_md *lsm;
163 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
164 lli = llu_i2info(inode);
166 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
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,
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);
177 CERROR("failed pack reply: %d\n", rc);
181 lsm = ccc_inode_lsm_get(inode);
183 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
185 /* First, find out which stripe index this lock corresponds to. */
186 stripe = llu_lock_to_stripe_offset(llu_i2obdexp(inode), lsm, lock);
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);
192 LDLM_DEBUG(lock, "i_size: "LPU64" -> stripe number %u -> kms "LPU64,
193 (__u64)llu_i2stat(inode)->st_size, stripe,lvb->lvb_size);
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);
206 int llu_merge_lvb(const struct lu_env *env, struct inode *inode)
208 struct llu_inode_info *lli = llu_i2info(inode);
209 struct cl_object *obj = lli->lli_clob;
210 struct intnl_stat *st = llu_i2stat(inode);
211 struct cl_attr *attr = ccc_env_thread_attr(env);
216 /* merge timestamps the most recently obtained from mds with
217 timestamps obtained from osts */
218 LTIME_S(inode->i_atime) = lli->lli_lvb.lvb_atime;
219 LTIME_S(inode->i_mtime) = lli->lli_lvb.lvb_mtime;
220 LTIME_S(inode->i_ctime) = lli->lli_lvb.lvb_ctime;
222 inode_init_lvb(inode, &lvb);
224 cl_object_attr_lock(obj);
225 rc = cl_object_attr_get(env, obj, attr);
226 cl_object_attr_unlock(obj);
228 if (lvb.lvb_atime < attr->cat_atime)
229 lvb.lvb_atime = attr->cat_atime;
230 if (lvb.lvb_ctime < attr->cat_ctime)
231 lvb.lvb_ctime = attr->cat_ctime;
232 if (lvb.lvb_mtime < attr->cat_mtime)
233 lvb.lvb_mtime = attr->cat_mtime;
235 st->st_size = lvb.lvb_size;
236 st->st_blocks = lvb.lvb_blocks;
237 st->st_mtime = lvb.lvb_mtime;
238 st->st_atime = lvb.lvb_atime;
239 st->st_ctime = lvb.lvb_ctime;
245 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
246 struct lov_stripe_md *lsm, int mode,
247 ldlm_policy_data_t *policy, struct lustre_handle *lockh,
250 struct llu_sb_info *sbi = llu_i2sbi(inode);
251 struct intnl_stat *st = llu_i2stat(inode);
252 struct ldlm_enqueue_info einfo = { 0 };
253 struct obd_info oinfo = { { { 0 } } };
258 LASSERT(!lustre_handle_is_used(lockh));
259 CLASSERT(ELDLM_OK == 0);
261 /* XXX phil: can we do this? won't it screw the file size up? */
262 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
263 (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
266 CDEBUG(D_DLMTRACE, "Locking inode "LPU64", start "LPU64" end "LPU64"\n",
267 (__u64)st->st_ino, policy->l_extent.start,
268 policy->l_extent.end);
270 einfo.ei_type = LDLM_EXTENT;
271 einfo.ei_mode = mode;
272 einfo.ei_cb_bl = llu_extent_lock_cancel_cb;
273 einfo.ei_cb_cp = ldlm_completion_ast;
274 einfo.ei_cb_gl = llu_glimpse_callback;
275 einfo.ei_cbdata = inode;
277 oinfo.oi_policy = *policy;
278 oinfo.oi_lockh = lockh;
280 oinfo.oi_flags = ast_flags;
282 rc = obd_enqueue(sbi->ll_dt_exp, &oinfo, &einfo, NULL);
283 *policy = oinfo.oi_policy;
287 inode_init_lvb(inode, &lvb);
288 obd_merge_lvb(sbi->ll_dt_exp, lsm, &lvb, 1);
289 if (policy->l_extent.start == 0 &&
290 policy->l_extent.end == OBD_OBJECT_EOF)
291 st->st_size = lvb.lvb_size;
294 st->st_mtime = lvb.lvb_mtime;
295 st->st_atime = lvb.lvb_atime;
296 st->st_ctime = lvb.lvb_ctime;
302 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
303 struct lov_stripe_md *lsm, int mode,
304 struct lustre_handle *lockh)
306 struct llu_sb_info *sbi = llu_i2sbi(inode);
310 CLASSERT(ELDLM_OK == 0);
312 /* XXX phil: can we do this? won't it screw the file size up? */
313 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
314 (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
317 rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
323 ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
324 _SYSIO_OFF_T pos, ssize_t len,
327 struct llu_io_session *session = (struct llu_io_session *) private;
328 struct inode *inode = session->lis_inode;
329 struct llu_inode_info *lli = llu_i2info(inode);
337 /* in a large iov read/write we'll be repeatedly called.
338 * so give a chance to answer cancel ast here
340 liblustre_wait_event(0);
342 if (len == 0 || iovlen == 0)
345 if (pos + len > lli->lli_maxbytes)
348 env = cl_env_get(&refcheck);
350 RETURN(PTR_ERR(env));
352 /* Do NOT call "ccc_env_thread_io()" again to prevent reinitializing */
353 io = &ccc_env_info(env)->cti_io;
354 if (cl_io_rw_init(env, io, session->lis_cmd == OBD_BRW_WRITE?CIT_WRITE:
358 sio = slp_env_io(env);
359 cio = ccc_env_io(env);
360 /* XXX this is not right: cio->cui_iov can be modified. */
361 cio->cui_iov = (struct iovec *)iovec;
362 cio->cui_nrsegs = iovlen;
363 cio->cui_tot_nrsegs = iovlen;
364 sio->sio_session = session;
365 err = cl_io_loop(env, io);
371 cl_env_put(env, &refcheck);
380 struct llu_io_session *get_io_session(struct inode *ino, int ngroups, int cmd)
382 struct llu_io_session *session;
384 OBD_ALLOC_PTR(session);
389 session->lis_inode = ino;
390 session->lis_max_groups = ngroups;
391 session->lis_cmd = cmd;
395 static void put_io_session(struct llu_io_session *session)
397 I_RELE(session->lis_inode);
398 OBD_FREE_PTR(session);
401 static int llu_file_rwx(struct inode *ino,
405 struct llu_io_session *session;
407 int cmd = read ? OBD_BRW_READ : OBD_BRW_WRITE;
410 LASSERT(ioctx->ioctx_xtvlen >= 0);
411 LASSERT(ioctx->ioctx_iovlen >= 0);
413 liblustre_wait_event(0);
415 if (!ioctx->ioctx_xtvlen)
418 /* XXX consider other types later */
419 if (S_ISDIR(llu_i2stat(ino)->st_mode))
421 if (!S_ISREG(llu_i2stat(ino)->st_mode))
424 session = get_io_session(ino, ioctx->ioctx_xtvlen * 2, cmd);
428 cc = _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
429 ioctx->ioctx_iov, ioctx->ioctx_iovlen,
430 llu_file_prwv, session);
433 LASSERT(!ioctx->ioctx_cc);
434 ioctx->ioctx_private = session;
437 put_io_session(session);
440 liblustre_wait_event(0);
444 void llu_io_init(struct cl_io *io, struct inode *inode, int write)
446 struct llu_inode_info *lli = llu_i2info(inode);
448 io->u.ci_rw.crw_nonblock = lli->lli_open_flags & O_NONBLOCK;
450 io->u.ci_wr.wr_append = lli->lli_open_flags & O_APPEND;
451 io->ci_obj = llu_i2info(inode)->lli_clob;
453 if ((lli->lli_open_flags & O_APPEND) && write)
454 io->ci_lockreq = CILR_MANDATORY;
456 io->ci_lockreq = CILR_NEVER;
459 int llu_iop_read(struct inode *ino,
462 struct intnl_stat *st = llu_i2stat(ino);
469 st->st_atime = CFS_CURRENT_TIME;
471 env = cl_env_get(&refcheck);
473 RETURN(PTR_ERR(env));
475 io = ccc_env_thread_io(env);
476 llu_io_init(io, ino, 0);
478 ret = llu_file_rwx(ino, ioctx, 1);
480 cl_env_put(env, &refcheck);
484 int llu_iop_write(struct inode *ino,
487 struct intnl_stat *st = llu_i2stat(ino);
493 st->st_mtime = st->st_ctime = CFS_CURRENT_TIME;
495 env = cl_env_get(&refcheck);
497 RETURN(PTR_ERR(env));
499 io = ccc_env_thread_io(env);
500 llu_io_init(io, ino, 1);
502 ret = llu_file_rwx(ino, ioctx, 0);
503 cl_env_put(env, &refcheck);
507 int llu_iop_iodone(struct ioctx *ioctx)
509 struct llu_io_session *session;
515 liblustre_wait_event(0);
517 env = cl_env_get(&refcheck);
519 RETURN(PTR_ERR(env));
521 io = &ccc_env_info(env)->cti_io;
523 cl_env_put(env, &refcheck);
524 session = (struct llu_io_session *) ioctx->ioctx_private;
526 LASSERT(!IS_ERR(session));
528 if (session->lis_rc == 0) {
529 ioctx->ioctx_cc = session->lis_rwcount;
531 LASSERT(session->lis_rc < 0);
532 ioctx->ioctx_cc = -1;
533 ioctx->ioctx_errno = -session->lis_rc;
536 put_io_session(session);
537 ioctx->ioctx_private = NULL;
538 liblustre_wait_event(0);