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