1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
64 #include "llite_lib.h"
66 typedef ssize_t llu_file_piov_t(const struct iovec *iovec, int iovlen,
67 _SYSIO_OFF_T pos, ssize_t len,
70 size_t llap_cookie_size;
72 static int llu_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
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);
79 struct ldlm_lock *lock;
80 } key = { .name = KEY_LOCK_TO_STRIPE, .lock = lock };
81 __u32 stripe, vallen = sizeof(stripe);
85 if (lsm->lsm_stripe_count == 1)
88 /* get our offset in the lov */
89 rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe, lsm);
91 CERROR("obd_get_info: rc = %d\n", rc);
94 LASSERT(stripe < lsm->lsm_stripe_count);
98 int llu_extent_lock_cancel_cb(struct ldlm_lock *lock,
99 struct ldlm_lock_desc *new, void *data,
102 struct lustre_handle lockh = { 0 };
106 if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
107 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
112 case LDLM_CB_BLOCKING:
113 ldlm_lock2handle(lock, &lockh);
114 rc = ldlm_cli_cancel(&lockh);
116 CERROR("ldlm_cli_cancel failed: %d\n", rc);
118 case LDLM_CB_CANCELING: {
120 struct llu_inode_info *lli;
121 struct lov_stripe_md *lsm;
125 /* This lock wasn't granted, don't try to evict pages */
126 if (lock->l_req_mode != lock->l_granted_mode)
129 inode = llu_inode_from_lock(lock);
132 lli= llu_i2info(inode);
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);
159 static int llu_glimpse_callback(struct ldlm_lock *lock, void *reqp)
161 struct ptlrpc_request *req = reqp;
162 struct inode *inode = llu_inode_from_lock(lock);
163 struct llu_inode_info *lli;
169 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
170 lli = llu_i2info(inode);
172 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
173 if (lli->lli_smd == NULL)
174 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
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);
180 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
181 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
183 rc = req_capsule_server_pack(&req->rq_pill);
185 CERROR("failed pack reply: %d\n", rc);
189 lvb = req_capsule_server_get(&req->rq_pill, &RMF_DLM_LVB);
190 lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe]->loi_kms;
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(struct inode *inode)
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);
215 lov_stripe_lock(lli->lli_smd);
216 inode_init_lvb(inode, &lvb);
217 /* merge timestamps the most resently obtained from mds with
218 timestamps obtained from osts */
219 lvb.lvb_atime = lli->lli_lvb.lvb_atime;
220 lvb.lvb_mtime = lli->lli_lvb.lvb_mtime;
221 lvb.lvb_ctime = lli->lli_lvb.lvb_ctime;
222 rc = obd_merge_lvb(sbi->ll_dt_exp, lli->lli_smd, &lvb, 0);
223 st->st_size = lvb.lvb_size;
224 st->st_blocks = lvb.lvb_blocks;
225 /* handle st_blocks overflow gracefully */
226 if (st->st_blocks < lvb.lvb_blocks)
227 st->st_blocks = ~0UL;
228 st->st_mtime = lvb.lvb_mtime;
229 st->st_atime = lvb.lvb_atime;
230 st->st_ctime = lvb.lvb_ctime;
231 lov_stripe_unlock(lli->lli_smd);
236 int llu_extent_lock(struct ll_file_data *fd, struct inode *inode,
237 struct lov_stripe_md *lsm, int mode,
238 ldlm_policy_data_t *policy, struct lustre_handle *lockh,
241 struct llu_sb_info *sbi = llu_i2sbi(inode);
242 struct intnl_stat *st = llu_i2stat(inode);
243 struct ldlm_enqueue_info einfo = { 0 };
244 struct obd_info oinfo = { { { 0 } } };
249 LASSERT(!lustre_handle_is_used(lockh));
250 CLASSERT(ELDLM_OK == 0);
252 /* XXX phil: can we do this? won't it screw the file size up? */
253 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
254 (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
257 CDEBUG(D_DLMTRACE, "Locking inode "LPU64", start "LPU64" end "LPU64"\n",
258 (__u64)st->st_ino, policy->l_extent.start,
259 policy->l_extent.end);
261 einfo.ei_type = LDLM_EXTENT;
262 einfo.ei_mode = mode;
263 einfo.ei_cb_bl = llu_extent_lock_cancel_cb;
264 einfo.ei_cb_cp = ldlm_completion_ast;
265 einfo.ei_cb_gl = llu_glimpse_callback;
266 einfo.ei_cbdata = inode;
268 oinfo.oi_policy = *policy;
269 oinfo.oi_lockh = lockh;
271 oinfo.oi_flags = ast_flags;
273 rc = obd_enqueue(sbi->ll_dt_exp, &oinfo, &einfo, NULL);
274 *policy = oinfo.oi_policy;
278 inode_init_lvb(inode, &lvb);
279 obd_merge_lvb(sbi->ll_dt_exp, lsm, &lvb, 1);
280 if (policy->l_extent.start == 0 &&
281 policy->l_extent.end == OBD_OBJECT_EOF)
282 st->st_size = lvb.lvb_size;
285 st->st_mtime = lvb.lvb_mtime;
286 st->st_atime = lvb.lvb_atime;
287 st->st_ctime = lvb.lvb_ctime;
293 int llu_extent_unlock(struct ll_file_data *fd, struct inode *inode,
294 struct lov_stripe_md *lsm, int mode,
295 struct lustre_handle *lockh)
297 struct llu_sb_info *sbi = llu_i2sbi(inode);
301 CLASSERT(ELDLM_OK == 0);
303 /* XXX phil: can we do this? won't it screw the file size up? */
304 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
305 (sbi->ll_flags & LL_SBI_NOLCK) || mode == LCK_NL)
308 rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
314 ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
315 _SYSIO_OFF_T pos, ssize_t len,
318 struct llu_io_session *session = (struct llu_io_session *) private;
319 struct inode *inode = session->lis_inode;
320 struct llu_inode_info *lli = llu_i2info(inode);
328 /* in a large iov read/write we'll be repeatedly called.
329 * so give a chance to answer cancel ast here
331 liblustre_wait_event(0);
333 if (len == 0 || iovlen == 0)
336 if (pos + len > lli->lli_maxbytes)
339 env = cl_env_get(&refcheck);
341 RETURN(PTR_ERR(env));
343 io = &ccc_env_info(env)->cti_io;
345 if (cl_io_rw_init(env, io, session->lis_cmd == OBD_BRW_WRITE?CIT_WRITE:
349 sio = slp_env_io(env);
350 cio = ccc_env_io(env);
351 /* XXX this is not right: cio->cui_iov can be modified. */
352 cio->cui_iov = (struct iovec *)iovec;
353 cio->cui_nrsegs = iovlen;
354 cio->cui_tot_nrsegs = iovlen;
355 sio->sio_session = session;
356 err = cl_io_loop(env, io);
362 cl_env_put(env, &refcheck);
371 struct llu_io_session *get_io_session(struct inode *ino, int ngroups, int cmd)
373 struct llu_io_session *session;
375 OBD_ALLOC_PTR(session);
380 session->lis_inode = ino;
381 session->lis_max_groups = ngroups;
382 session->lis_cmd = cmd;
386 static void put_io_session(struct llu_io_session *session)
388 I_RELE(session->lis_inode);
389 OBD_FREE_PTR(session);
392 static int llu_file_rwx(struct inode *ino,
396 struct llu_io_session *session;
398 int cmd = read ? OBD_BRW_READ : OBD_BRW_WRITE;
401 LASSERT(ioctx->ioctx_xtvlen >= 0);
402 LASSERT(ioctx->ioctx_iovlen >= 0);
404 liblustre_wait_event(0);
406 if (!ioctx->ioctx_xtvlen)
409 /* XXX consider other types later */
410 if (S_ISDIR(llu_i2stat(ino)->st_mode))
412 if (!S_ISREG(llu_i2stat(ino)->st_mode))
415 session = get_io_session(ino, ioctx->ioctx_xtvlen * 2, cmd);
419 cc = _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
420 ioctx->ioctx_iov, ioctx->ioctx_iovlen,
421 llu_file_prwv, session);
424 LASSERT(!ioctx->ioctx_cc);
425 ioctx->ioctx_private = session;
428 put_io_session(session);
431 liblustre_wait_event(0);
435 void llu_io_init(struct cl_io *io, struct inode *inode, int write)
437 struct llu_inode_info *lli = llu_i2info(inode);
439 memset(io, 0, sizeof *io);
441 io->u.ci_rw.crw_nonblock = lli->lli_open_flags & O_NONBLOCK;
443 io->u.ci_wr.wr_append = lli->lli_open_flags & O_APPEND;
444 io->ci_obj = llu_i2info(inode)->lli_clob;
446 if ((lli->lli_open_flags & O_APPEND) && write)
447 io->ci_lockreq = CILR_MANDATORY;
449 io->ci_lockreq = CILR_NEVER;
452 int llu_iop_read(struct inode *ino,
455 struct intnl_stat *st = llu_i2stat(ino);
462 st->st_atime = CFS_CURRENT_TIME;
464 env = cl_env_get(&refcheck);
466 RETURN(PTR_ERR(env));
468 io = &ccc_env_info(env)->cti_io;
469 llu_io_init(io, ino, 0);
471 ret = llu_file_rwx(ino, ioctx, 1);
473 cl_env_put(env, &refcheck);
477 int llu_iop_write(struct inode *ino,
480 struct intnl_stat *st = llu_i2stat(ino);
486 st->st_mtime = st->st_ctime = CFS_CURRENT_TIME;
488 env = cl_env_get(&refcheck);
490 RETURN(PTR_ERR(env));
492 io = &ccc_env_info(env)->cti_io;
493 llu_io_init(io, ino, 1);
495 ret = llu_file_rwx(ino, ioctx, 0);
496 cl_env_put(env, &refcheck);
500 int llu_iop_iodone(struct ioctx *ioctx)
502 struct llu_io_session *session;
508 liblustre_wait_event(0);
510 env = cl_env_get(&refcheck);
512 RETURN(PTR_ERR(env));
514 io = &ccc_env_info(env)->cti_io;
516 cl_env_put(env, &refcheck);
517 session = (struct llu_io_session *) ioctx->ioctx_private;
519 LASSERT(!IS_ERR(session));
521 if (session->lis_rc == 0) {
522 ioctx->ioctx_cc = session->lis_rwcount;
524 LASSERT(session->lis_rc < 0);
525 ioctx->ioctx_cc = -1;
526 ioctx->ioctx_errno = -session->lis_rc;
529 put_io_session(session);
530 ioctx->ioctx_private = NULL;
531 liblustre_wait_event(0);