Whamcloud - gitweb
a75e5cd75cfeaeb0681e6d915b574b3f05180a31
[fs/lustre-release.git] / lustre / ofd / ofd_io.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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
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/ofd/ofd_io.c
37  *
38  * Author: Alex Tomas <bzzz@whamcloud.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_FILTER
42
43 #include "ofd_internal.h"
44
45 static int ofd_preprw_read(const struct lu_env *env, struct ofd_device *ofd,
46                            struct lu_fid *fid, struct lu_attr *la, int niocount,
47                            struct niobuf_remote *rnb, int *nr_local,
48                            struct niobuf_local *lnb)
49 {
50         struct ofd_object       *fo;
51         int                      i, j, rc, tot_bytes = 0;
52
53         ENTRY;
54         LASSERT(env != NULL);
55
56         fo = ofd_object_find(env, ofd, fid);
57         if (IS_ERR(fo))
58                 RETURN(PTR_ERR(fo));
59         LASSERT(fo != NULL);
60
61         ofd_read_lock(env, fo);
62         if (!ofd_object_exists(fo))
63                 GOTO(unlock, rc = -ENOENT);
64
65         /* parse remote buffers to local buffers and prepare the latter */
66         for (i = 0, j = 0; i < niocount; i++) {
67                 rc = dt_bufs_get(env, ofd_object_child(fo), rnb + i,
68                                  lnb + j, 0, ofd_object_capa(env, fo));
69                 LASSERT(rc > 0);
70                 LASSERT(rc <= PTLRPC_MAX_BRW_PAGES);
71                 /* correct index for local buffers to continue with */
72                 j += rc;
73                 LASSERT(j <= PTLRPC_MAX_BRW_PAGES);
74                 tot_bytes += rnb[i].rnb_len;
75         }
76
77         *nr_local = j;
78         LASSERT(*nr_local > 0 && *nr_local <= PTLRPC_MAX_BRW_PAGES);
79         rc = dt_attr_get(env, ofd_object_child(fo), la,
80                          ofd_object_capa(env, fo));
81         if (unlikely(rc))
82                 GOTO(buf_put, rc);
83
84         rc = dt_read_prep(env, ofd_object_child(fo), lnb, *nr_local);
85         if (unlikely(rc))
86                 GOTO(buf_put, rc);
87         lprocfs_counter_add(ofd_obd(ofd)->obd_stats,
88                             LPROC_OFD_READ_BYTES, tot_bytes);
89         RETURN(0);
90
91 buf_put:
92         dt_bufs_put(env, ofd_object_child(fo), lnb, *nr_local);
93 unlock:
94         ofd_read_unlock(env, fo);
95         ofd_object_put(env, fo);
96         return rc;
97 }
98
99 static int ofd_preprw_write(const struct lu_env *env, struct obd_export *exp,
100                             struct ofd_device *ofd, struct lu_fid *fid,
101                             struct lu_attr *la, struct obdo *oa,
102                             int objcount, struct obd_ioobj *obj,
103                             struct niobuf_remote *rnb, int *nr_local,
104                             struct niobuf_local *lnb,
105                             struct obd_trans_info *oti)
106 {
107         struct ofd_object       *fo;
108         int                      i, j, k, rc = 0, tot_bytes = 0;
109
110         ENTRY;
111         LASSERT(env != NULL);
112         LASSERT(objcount == 1);
113
114         if (unlikely(exp->exp_obd->obd_recovering)) {
115                 struct ofd_thread_info *info = ofd_info(env);
116
117                 /* copied from ofd_precreate_object */
118                 /* XXX this should be consolidated to use the same code
119                  *     instead of a copy, due to the ongoing risk of bugs. */
120                 memset(&info->fti_attr, 0, sizeof(info->fti_attr));
121                 info->fti_attr.la_valid = LA_TYPE | LA_MODE;
122                 info->fti_attr.la_mode = S_IFREG | S_ISUID | S_ISGID | 0666;
123                 info->fti_attr.la_valid |= LA_ATIME | LA_MTIME | LA_CTIME;
124                 /* Initialize a/c/m time so any client timestamp will always
125                  * be newer and update the inode. ctime = 0 is also handled
126                  * specially in osd_inode_setattr().  See LU-221, LU-1042 */
127                 info->fti_attr.la_atime = 0;
128                 info->fti_attr.la_mtime = 0;
129                 info->fti_attr.la_ctime = 0;
130
131                 fo = ofd_object_find_or_create(env, ofd, fid, &info->fti_attr);
132         } else {
133                 fo = ofd_object_find(env, ofd, fid);
134         }
135
136         if (IS_ERR(fo))
137                 GOTO(out, rc = PTR_ERR(fo));
138         LASSERT(fo != NULL);
139
140         ofd_read_lock(env, fo);
141         if (!ofd_object_exists(fo)) {
142                 CERROR("%s: BRW to missing obj "LPU64"/"LPU64"\n",
143                        exp->exp_obd->obd_name, obj->ioo_id, obj->ioo_seq);
144                 ofd_read_unlock(env, fo);
145                 ofd_object_put(env, fo);
146                 GOTO(out, rc = -ENOENT);
147         }
148
149         /* Always sync if syncjournal parameter is set */
150         oti->oti_sync_write = ofd->ofd_syncjournal;
151
152         /* Process incoming grant info, set OBD_BRW_GRANTED flag and grant some
153          * space back if possible */
154         ofd_grant_prepare_write(env, exp, oa, rnb, obj->ioo_bufcnt);
155
156         /* parse remote buffers to local buffers and prepare the latter */
157         for (i = 0, j = 0; i < obj->ioo_bufcnt; i++) {
158                 rc = dt_bufs_get(env, ofd_object_child(fo),
159                                  rnb + i, lnb + j, 1,
160                                  ofd_object_capa(env, fo));
161                 LASSERT(rc > 0);
162                 LASSERT(rc <= PTLRPC_MAX_BRW_PAGES);
163                 /* correct index for local buffers to continue with */
164                 for (k = 0; k < rc; k++) {
165                         lnb[j+k].lnb_flags = rnb[i].rnb_flags;
166                         if (!(rnb[i].rnb_flags & OBD_BRW_GRANTED))
167                                 lnb[j+k].lnb_rc = -ENOSPC;
168                         if (!(rnb[i].rnb_flags & OBD_BRW_ASYNC))
169                                 oti->oti_sync_write = 1;
170                 }
171                 j += rc;
172                 LASSERT(j <= PTLRPC_MAX_BRW_PAGES);
173                 tot_bytes += rnb[i].rnb_len;
174         }
175         *nr_local = j;
176         LASSERT(*nr_local > 0 && *nr_local <= PTLRPC_MAX_BRW_PAGES);
177
178         lprocfs_counter_add(ofd_obd(ofd)->obd_stats,
179                             LPROC_OFD_WRITE_BYTES, tot_bytes);
180         rc = dt_write_prep(env, ofd_object_child(fo), lnb, *nr_local);
181         if (unlikely(rc != 0)) {
182                 dt_bufs_put(env, ofd_object_child(fo), lnb, *nr_local);
183                 ofd_read_unlock(env, fo);
184                 /* ofd_grant_prepare_write() was called, so we must commit */
185                 ofd_grant_commit(env, exp, rc);
186         }
187
188         RETURN(rc);
189 out:
190         /* let's still process incoming grant information packed in the oa,
191          * but without enforcing grant since we won't proceed with the write.
192          * Just like a read request actually. */
193         ofd_grant_prepare_read(env, exp, oa);
194         RETURN(rc);
195 }
196
197 int ofd_preprw(const struct lu_env* env, int cmd, struct obd_export *exp,
198                struct obdo *oa, int objcount, struct obd_ioobj *obj,
199                struct niobuf_remote *rnb, int *nr_local,
200                struct niobuf_local *lnb, struct obd_trans_info *oti,
201                struct lustre_capa *capa)
202 {
203         struct ofd_device       *ofd = ofd_exp(exp);
204         struct ofd_thread_info  *info;
205         int                      rc = 0;
206
207         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT) &&
208             ofd->ofd_destroys_in_progress == 0) {
209                 /* don't fail lookups for orphan recovery, it causes
210                  * later LBUGs when objects still exist during precreate */
211                 CDEBUG(D_INFO, "*** obd_fail_loc=%x ***\n",OBD_FAIL_OST_ENOENT);
212                 RETURN(-ENOENT);
213         }
214
215         info = ofd_info_init(env, exp);
216
217         LASSERT(objcount == 1);
218         LASSERT(obj->ioo_bufcnt > 0);
219
220         fid_ostid_unpack(&info->fti_fid, &oa->o_oi, 0);
221         if (cmd == OBD_BRW_WRITE) {
222                 rc = ofd_auth_capa(exp, &info->fti_fid, oa->o_seq,
223                                    capa, CAPA_OPC_OSS_WRITE);
224                 if (rc == 0) {
225                         LASSERT(oa != NULL);
226                         la_from_obdo(&info->fti_attr, oa, OBD_MD_FLGETATTR);
227                         rc = ofd_preprw_write(env, exp, ofd, &info->fti_fid,
228                                               &info->fti_attr, oa, objcount,
229                                               obj, rnb, nr_local, lnb, oti);
230                 }
231         } else if (cmd == OBD_BRW_READ) {
232                 rc = ofd_auth_capa(exp, &info->fti_fid, oa->o_seq,
233                                    capa, CAPA_OPC_OSS_READ);
234                 if (rc == 0) {
235                         ofd_grant_prepare_read(env, exp, oa);
236                         rc = ofd_preprw_read(env, ofd, &info->fti_fid,
237                                              &info->fti_attr, obj->ioo_bufcnt,
238                                              rnb, nr_local, lnb);
239                         obdo_from_la(oa, &info->fti_attr, LA_ATIME);
240                 }
241         } else {
242                 CERROR("%s: wrong cmd %d received!\n",
243                        exp->exp_obd->obd_name, cmd);
244                 rc = -EPROTO;
245         }
246         RETURN(rc);
247 }
248
249 static int
250 ofd_commitrw_read(const struct lu_env *env, struct ofd_device *ofd,
251                   struct lu_fid *fid, int objcount, int niocount,
252                   struct niobuf_local *lnb)
253 {
254         struct ofd_object *fo;
255
256         ENTRY;
257
258         LASSERT(niocount > 0);
259
260         fo = ofd_object_find(env, ofd, fid);
261         if (IS_ERR(fo))
262                 RETURN(PTR_ERR(fo));
263         LASSERT(fo != NULL);
264         LASSERT(ofd_object_exists(fo));
265         dt_bufs_put(env, ofd_object_child(fo), lnb, niocount);
266
267         ofd_read_unlock(env, fo);
268         ofd_object_put(env, fo);
269         /* second put is pair to object_get in ofd_preprw_read */
270         ofd_object_put(env, fo);
271
272         RETURN(0);
273 }
274
275 static int
276 ofd_write_attr_set(const struct lu_env *env, struct ofd_device *ofd,
277                    struct ofd_object *ofd_obj, struct lu_attr *la,
278                    struct filter_fid *ff)
279 {
280         struct ofd_thread_info  *info = ofd_info(env);
281         __u64                    valid = la->la_valid;
282         int                      rc;
283         struct thandle          *th;
284         struct dt_object        *dt_obj;
285         int                      ff_needed = 0;
286
287         ENTRY;
288
289         LASSERT(la);
290
291         dt_obj = ofd_object_child(ofd_obj);
292         LASSERT(dt_obj != NULL);
293
294         la->la_valid &= LA_UID | LA_GID;
295
296         rc = ofd_attr_handle_ugid(env, ofd_obj, la, 0 /* !is_setattr */);
297         if (rc != 0)
298                 GOTO(out, rc);
299
300         if (ff != NULL) {
301                 rc = ofd_object_ff_check(env, ofd_obj);
302                 if (rc == -ENODATA)
303                         ff_needed = 1;
304                 else if (rc < 0)
305                         GOTO(out, rc);
306         }
307
308         if (!la->la_valid && !ff_needed)
309                 /* no attributes to set */
310                 GOTO(out, rc = 0);
311
312         th = ofd_trans_create(env, ofd);
313         if (IS_ERR(th))
314                 GOTO(out, rc = PTR_ERR(th));
315
316         if (la->la_valid) {
317                 rc = dt_declare_attr_set(env, dt_obj, la, th);
318                 if (rc)
319                         GOTO(out_tx, rc);
320         }
321
322         if (ff_needed) {
323                 info->fti_buf.lb_buf = ff;
324                 info->fti_buf.lb_len = sizeof(*ff);
325                 rc = dt_declare_xattr_set(env, dt_obj, &info->fti_buf,
326                                           XATTR_NAME_FID, 0, th);
327                 if (rc)
328                         GOTO(out_tx, rc);
329         }
330
331         /* We don't need a transno for this operation which will be re-executed
332          * anyway when the OST_WRITE (with a transno assigned) is replayed */
333         rc = dt_trans_start_local(env, ofd->ofd_osd , th);
334         if (rc)
335                 GOTO(out_tx, rc);
336
337         /* set uid/gid */
338         if (la->la_valid) {
339                 rc = dt_attr_set(env, dt_obj, la, th,
340                                  ofd_object_capa(env, ofd_obj));
341                 if (rc)
342                         GOTO(out_tx, rc);
343         }
344
345         /* set filter fid EA */
346         if (ff_needed) {
347                 rc = dt_xattr_set(env, dt_obj, &info->fti_buf, XATTR_NAME_FID,
348                                   0, th, BYPASS_CAPA);
349                 if (rc)
350                         GOTO(out_tx, rc);
351         }
352
353         EXIT;
354 out_tx:
355         dt_trans_stop(env, ofd->ofd_osd, th);
356 out:
357         la->la_valid = valid;
358         return rc;
359 }
360
361 static int
362 ofd_commitrw_write(const struct lu_env *env, struct ofd_device *ofd,
363                    struct lu_fid *fid, struct lu_attr *la,
364                    struct filter_fid *ff, int objcount,
365                    int niocount, struct niobuf_local *lnb,
366                    struct obd_trans_info *oti, int old_rc)
367 {
368         struct ofd_thread_info  *info = ofd_info(env);
369         struct ofd_object       *fo;
370         struct dt_object        *o;
371         struct thandle          *th;
372         int                      rc = 0;
373         int                      retries = 0;
374
375         ENTRY;
376
377         LASSERT(objcount == 1);
378
379         fo = ofd_object_find(env, ofd, fid);
380         LASSERT(fo != NULL);
381         LASSERT(ofd_object_exists(fo));
382
383         o = ofd_object_child(fo);
384         LASSERT(o != NULL);
385
386         if (old_rc)
387                 GOTO(out, rc = old_rc);
388
389         /*
390          * The first write to each object must set some attributes.  It is
391          * important to set the uid/gid before calling
392          * dt_declare_write_commit() since quota enforcement is now handled in
393          * declare phases.
394          */
395         rc = ofd_write_attr_set(env, ofd, fo, la, ff);
396         if (rc)
397                 GOTO(out, rc);
398
399         la->la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
400
401 retry:
402         th = ofd_trans_create(env, ofd);
403         if (IS_ERR(th))
404                 GOTO(out, rc = PTR_ERR(th));
405
406         th->th_sync |= oti->oti_sync_write;
407
408         rc = dt_declare_write_commit(env, o, lnb, niocount, th);
409         if (rc)
410                 GOTO(out_stop, rc);
411
412         if (la->la_valid) {
413                 /* update [mac]time if needed */
414                 rc = dt_declare_attr_set(env, o, la, th);
415                 if (rc)
416                         GOTO(out_stop, rc);
417         }
418
419         rc = ofd_trans_start(env, ofd, fo, th);
420         if (rc)
421                 GOTO(out_stop, rc);
422
423         rc = dt_write_commit(env, o, lnb, niocount, th);
424         if (rc)
425                 GOTO(out_stop, rc);
426
427         if (la->la_valid) {
428                 rc = dt_attr_set(env, o, la, th, ofd_object_capa(env, fo));
429                 if (rc)
430                         GOTO(out_stop, rc);
431         }
432
433         /* get attr to return */
434         dt_attr_get(env, o, la, ofd_object_capa(env, fo));
435
436 out_stop:
437         /* Force commit to make the just-deleted blocks
438          * reusable. LU-456 */
439         if (rc == -ENOSPC)
440                 th->th_sync = 1;
441
442         ofd_trans_stop(env, ofd, th, rc);
443         if (rc == -ENOSPC && retries++ < 3) {
444                 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
445                        retries);
446                 goto retry;
447         }
448
449 out:
450         dt_bufs_put(env, o, lnb, niocount);
451         ofd_read_unlock(env, fo);
452         ofd_object_put(env, fo);
453         /* second put is pair to object_get in ofd_preprw_write */
454         ofd_object_put(env, fo);
455         ofd_grant_commit(env, info->fti_exp, old_rc);
456         RETURN(rc);
457 }
458
459 int ofd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
460                  struct obdo *oa, int objcount, struct obd_ioobj *obj,
461                  struct niobuf_remote *rnb, int npages,
462                  struct niobuf_local *lnb, struct obd_trans_info *oti,
463                  int old_rc)
464 {
465         struct ofd_thread_info  *info;
466         struct ofd_mod_data     *fmd;
467         __u64                    valid;
468         struct ofd_device       *ofd = ofd_exp(exp);
469         struct filter_fid       *ff = NULL;
470         int                      rc = 0;
471
472         info = ofd_info(env);
473         ofd_oti2info(info, oti);
474
475         LASSERT(npages > 0);
476
477         fid_ostid_unpack(&info->fti_fid, &oa->o_oi, 0);
478         if (cmd == OBD_BRW_WRITE) {
479                 /* Don't update timestamps if this write is older than a
480                  * setattr which modifies the timestamps. b=10150 */
481
482                 /* XXX when we start having persistent reservations this needs
483                  * to be changed to ofd_fmd_get() to create the fmd if it
484                  * doesn't already exist so we can store the reservation handle
485                  * there. */
486                 valid = OBD_MD_FLUID | OBD_MD_FLGID;
487                 fmd = ofd_fmd_find(exp, &info->fti_fid);
488                 if (!fmd || fmd->fmd_mactime_xid < info->fti_xid)
489                         valid |= OBD_MD_FLATIME | OBD_MD_FLMTIME |
490                                  OBD_MD_FLCTIME;
491                 ofd_fmd_put(exp, fmd);
492                 la_from_obdo(&info->fti_attr, oa, valid);
493
494                 if (oa->o_valid & OBD_MD_FLFID) {
495                         ff = &info->fti_mds_fid;
496                         ofd_prepare_fidea(ff, oa);
497                 }
498
499                 rc = ofd_commitrw_write(env, ofd, &info->fti_fid,
500                                         &info->fti_attr, ff, objcount, npages,
501                                         lnb, oti, old_rc);
502                 if (rc == 0)
503                         obdo_from_la(oa, &info->fti_attr,
504                                      OFD_VALID_FLAGS | LA_GID | LA_UID);
505                 else
506                         obdo_from_la(oa, &info->fti_attr, LA_GID | LA_UID);
507
508                 if (ofd_grant_prohibit(exp, ofd))
509                         /* Trick to prevent clients from waiting for bulk write
510                          * in flight since they won't get any grant in the reply
511                          * anyway so they had better firing the sync write RPC
512                          * straight away */
513                         oa->o_valid |= OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA;
514         } else if (cmd == OBD_BRW_READ) {
515                 struct ldlm_namespace *ns = ofd->ofd_namespace;
516
517                 /* If oa != NULL then ofd_preprw_read updated the inode
518                  * atime and we should update the lvb so that other glimpses
519                  * will also get the updated value. bug 5972 */
520                 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
521                          struct ldlm_resource *rs = NULL;
522
523                         ofd_build_resid(&info->fti_fid, &info->fti_resid);
524                         rs = ldlm_resource_get(ns, NULL, &info->fti_resid,
525                                                LDLM_EXTENT, 0);
526                         if (rs != NULL) {
527                                 ns->ns_lvbo->lvbo_update(rs, NULL, 1);
528                                 ldlm_resource_putref(rs);
529                         }
530                 }
531                 rc = ofd_commitrw_read(env, ofd, &info->fti_fid, objcount,
532                                           npages, lnb);
533                 if (old_rc)
534                         rc = old_rc;
535         } else {
536                 LBUG();
537                 rc = -EPROTO;
538         }
539
540         ofd_info2oti(info, oti);
541         RETURN(rc);
542 }