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