Whamcloud - gitweb
LU-2675 lov: remove unused lov obd functions
[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, 2013, 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, 0);
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 int llu_merge_lvb(const struct lu_env *env, struct inode *inode)
153 {
154         struct llu_inode_info *lli = llu_i2info(inode);
155         struct cl_object *obj = lli->lli_clob;
156         struct intnl_stat *st = llu_i2stat(inode);
157         struct cl_attr *attr = ccc_env_thread_attr(env);
158         struct ost_lvb lvb;
159         int rc;
160         ENTRY;
161
162         /* merge timestamps the most recently obtained from mds with
163            timestamps obtained from osts */
164         LTIME_S(inode->i_atime) = lli->lli_lvb.lvb_atime;
165         LTIME_S(inode->i_mtime) = lli->lli_lvb.lvb_mtime;
166         LTIME_S(inode->i_ctime) = lli->lli_lvb.lvb_ctime;
167
168         inode_init_lvb(inode, &lvb);
169
170         cl_object_attr_lock(obj);
171         rc = cl_object_attr_get(env, obj, attr);
172         cl_object_attr_unlock(obj);
173         if (rc == 0) {
174                 if (lvb.lvb_atime < attr->cat_atime)
175                         lvb.lvb_atime = attr->cat_atime;
176                 if (lvb.lvb_ctime < attr->cat_ctime)
177                         lvb.lvb_ctime = attr->cat_ctime;
178                 if (lvb.lvb_mtime < attr->cat_mtime)
179                         lvb.lvb_mtime = attr->cat_mtime;
180
181                 st->st_size = lvb.lvb_size;
182                 st->st_blocks = lvb.lvb_blocks;
183                 st->st_mtime = lvb.lvb_mtime;
184                 st->st_atime = lvb.lvb_atime;
185                 st->st_ctime = lvb.lvb_ctime;
186         }
187
188         RETURN(rc);
189 }
190
191 static
192 ssize_t llu_file_prwv(const struct iovec *iovec, int iovlen,
193                         _SYSIO_OFF_T pos, ssize_t len,
194                         void *private)
195 {
196         struct llu_io_session *session = (struct llu_io_session *) private;
197         struct inode *inode = session->lis_inode;
198         struct llu_inode_info *lli = llu_i2info(inode);
199         int err;
200         struct lu_env *env;
201         struct cl_io  *io;
202         struct slp_io *sio;
203         int refcheck;
204         ENTRY;
205
206         /* in a large iov read/write we'll be repeatedly called.
207          * so give a chance to answer cancel ast here
208          */
209         liblustre_wait_event(0);
210
211         if (len == 0 || iovlen == 0)
212                 RETURN(0);
213
214         if (pos + len > lli->lli_maxbytes)
215                 RETURN(-ERANGE);
216
217         env = cl_env_get(&refcheck);
218         if (IS_ERR(env))
219                 RETURN(PTR_ERR(env));
220
221         /* Do NOT call "ccc_env_thread_io()" again to prevent reinitializing */
222         io = &ccc_env_info(env)->cti_io;
223         if (cl_io_rw_init(env, io, session->lis_cmd == OBD_BRW_WRITE?CIT_WRITE:
224                                                                       CIT_READ,
225                           pos, len) == 0) {
226                 struct ccc_io *cio;
227                 sio = slp_env_io(env);
228                 cio = ccc_env_io(env);
229                 /* XXX this is not right: cio->cui_iov can be modified. */
230                 cio->cui_iov = (struct iovec *)iovec;
231                 cio->cui_nrsegs = iovlen;
232                 cio->cui_tot_nrsegs = iovlen;
233                 sio->sio_session = session;
234                 err = cl_io_loop(env, io);
235         } else {
236                 /* XXX WTF? */
237                 LBUG();
238         }
239         cl_io_fini(env, io);
240         cl_env_put(env, &refcheck);
241
242         if (err < 0)
243                 RETURN(err);
244
245         RETURN(len);
246 }
247
248 static
249 struct llu_io_session *get_io_session(struct inode *ino, int ngroups, int cmd)
250 {
251         struct llu_io_session *session;
252
253         OBD_ALLOC_PTR(session);
254         if (!session)
255                 return NULL;
256
257         I_REF(ino);
258         session->lis_inode = ino;
259         session->lis_max_groups = ngroups;
260         session->lis_cmd = cmd;
261         return session;
262 }
263
264 static void put_io_session(struct llu_io_session *session)
265 {
266         I_RELE(session->lis_inode);
267         OBD_FREE_PTR(session);
268 }
269
270 static int llu_file_rwx(struct inode *ino,
271                         struct ioctx *ioctx,
272                         int read)
273 {
274         struct llu_io_session *session;
275         ssize_t cc;
276         int cmd = read ? OBD_BRW_READ : OBD_BRW_WRITE;
277         ENTRY;
278
279         LASSERT(ioctx->ioctx_xtvlen >= 0);
280         LASSERT(ioctx->ioctx_iovlen >= 0);
281
282         liblustre_wait_event(0);
283
284         if (!ioctx->ioctx_xtvlen)
285                 RETURN(0);
286
287         /* XXX consider other types later */
288         if (S_ISDIR(llu_i2stat(ino)->st_mode))
289                 RETURN(-EISDIR);
290         if (!S_ISREG(llu_i2stat(ino)->st_mode))
291                 RETURN(-EOPNOTSUPP);
292
293         session = get_io_session(ino, ioctx->ioctx_xtvlen * 2, cmd);
294         if (!session)
295                 RETURN(-ENOMEM);
296
297         cc = _sysio_enumerate_extents(ioctx->ioctx_xtv, ioctx->ioctx_xtvlen,
298                                       ioctx->ioctx_iov, ioctx->ioctx_iovlen,
299                                       llu_file_prwv, session);
300
301         if (cc >= 0) {
302                 LASSERT(!ioctx->ioctx_cc);
303                 ioctx->ioctx_private = session;
304                 cc = 0;
305         } else {
306                 put_io_session(session);
307         }
308
309         liblustre_wait_event(0);
310         RETURN(cc);
311 }
312
313 void llu_io_init(struct cl_io *io, struct inode *inode, int write)
314 {
315         struct llu_inode_info *lli = llu_i2info(inode);
316
317         io->u.ci_rw.crw_nonblock = lli->lli_open_flags & O_NONBLOCK;
318         if (write)
319                 io->u.ci_wr.wr_append = lli->lli_open_flags & O_APPEND;
320         io->ci_obj  = llu_i2info(inode)->lli_clob;
321
322         if ((lli->lli_open_flags & O_APPEND) && write)
323                 io->ci_lockreq = CILR_MANDATORY;
324         else
325                 io->ci_lockreq = CILR_NEVER;
326 }
327
328 int llu_iop_read(struct inode *ino,
329                  struct ioctx *ioctx)
330 {
331         struct intnl_stat *st = llu_i2stat(ino);
332         struct lu_env *env;
333         struct cl_io  *io;
334         int refcheck;
335         int ret;
336
337         /* BUG: 5972 */
338         st->st_atime = CFS_CURRENT_TIME;
339
340         env = cl_env_get(&refcheck);
341         if (IS_ERR(env))
342                 RETURN(PTR_ERR(env));
343
344         io = ccc_env_thread_io(env);
345         llu_io_init(io, ino, 0);
346
347         ret = llu_file_rwx(ino, ioctx, 1);
348
349         cl_env_put(env, &refcheck);
350         return ret;
351 }
352
353 int llu_iop_write(struct inode *ino,
354                   struct ioctx *ioctx)
355 {
356         struct intnl_stat *st = llu_i2stat(ino);
357         struct lu_env *env;
358         struct cl_io  *io;
359         int refcheck;
360         int ret;
361
362         st->st_mtime = st->st_ctime = CFS_CURRENT_TIME;
363
364         env = cl_env_get(&refcheck);
365         if (IS_ERR(env))
366                 RETURN(PTR_ERR(env));
367
368         io = ccc_env_thread_io(env);
369         llu_io_init(io, ino, 1);
370
371         ret = llu_file_rwx(ino, ioctx, 0);
372         cl_env_put(env, &refcheck);
373         return ret;
374 }
375
376 int llu_iop_iodone(struct ioctx *ioctx)
377 {
378         struct llu_io_session *session;
379         struct lu_env *env;
380         struct cl_io  *io;
381         int refcheck;
382         ENTRY;
383
384         liblustre_wait_event(0);
385
386         env = cl_env_get(&refcheck);
387         if (IS_ERR(env))
388                 RETURN(PTR_ERR(env));
389
390         io = &ccc_env_info(env)->cti_io;
391         cl_io_fini(env, io);
392         cl_env_put(env, &refcheck);
393         session = (struct llu_io_session *) ioctx->ioctx_private;
394         LASSERT(session);
395         LASSERT(!IS_ERR(session));
396
397         if (session->lis_rc == 0) {
398                 ioctx->ioctx_cc = session->lis_rwcount;
399         } else {
400                 LASSERT(session->lis_rc < 0);
401                 ioctx->ioctx_cc = -1;
402                 ioctx->ioctx_errno = -session->lis_rc;
403         }
404
405         put_io_session(session);
406         ioctx->ioctx_private = NULL;
407         liblustre_wait_event(0);
408
409         RETURN(1);
410 }