Whamcloud - gitweb
LU-1842 quota: ldiskfs local enforcement
[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                         /* remote client can't break through quota */
171                         if (exp_connect_rmtclient(exp))
172                                 lnb[j+k].lnb_flags &= ~OBD_BRW_NOQUOTA;
173                 }
174                 j += rc;
175                 LASSERT(j <= PTLRPC_MAX_BRW_PAGES);
176                 tot_bytes += rnb[i].rnb_len;
177         }
178         *nr_local = j;
179         LASSERT(*nr_local > 0 && *nr_local <= PTLRPC_MAX_BRW_PAGES);
180
181         lprocfs_counter_add(ofd_obd(ofd)->obd_stats,
182                             LPROC_OFD_WRITE_BYTES, tot_bytes);
183         rc = dt_write_prep(env, ofd_object_child(fo), lnb, *nr_local);
184         if (unlikely(rc != 0)) {
185                 dt_bufs_put(env, ofd_object_child(fo), lnb, *nr_local);
186                 ofd_read_unlock(env, fo);
187                 /* ofd_grant_prepare_write() was called, so we must commit */
188                 ofd_grant_commit(env, exp, rc);
189         }
190
191         RETURN(rc);
192 out:
193         /* let's still process incoming grant information packed in the oa,
194          * but without enforcing grant since we won't proceed with the write.
195          * Just like a read request actually. */
196         ofd_grant_prepare_read(env, exp, oa);
197         RETURN(rc);
198 }
199
200 int ofd_preprw(const struct lu_env* env, int cmd, struct obd_export *exp,
201                struct obdo *oa, int objcount, struct obd_ioobj *obj,
202                struct niobuf_remote *rnb, int *nr_local,
203                struct niobuf_local *lnb, struct obd_trans_info *oti,
204                struct lustre_capa *capa)
205 {
206         struct ofd_device       *ofd = ofd_exp(exp);
207         struct ofd_thread_info  *info;
208         int                      rc = 0;
209
210         if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT) &&
211             ofd->ofd_destroys_in_progress == 0) {
212                 /* don't fail lookups for orphan recovery, it causes
213                  * later LBUGs when objects still exist during precreate */
214                 CDEBUG(D_INFO, "*** obd_fail_loc=%x ***\n",OBD_FAIL_OST_ENOENT);
215                 RETURN(-ENOENT);
216         }
217
218         info = ofd_info_init(env, exp);
219
220         LASSERT(objcount == 1);
221         LASSERT(obj->ioo_bufcnt > 0);
222
223         fid_ostid_unpack(&info->fti_fid, &oa->o_oi, 0);
224         if (cmd == OBD_BRW_WRITE) {
225                 rc = ofd_auth_capa(exp, &info->fti_fid, oa->o_seq,
226                                    capa, CAPA_OPC_OSS_WRITE);
227                 if (rc == 0) {
228                         LASSERT(oa != NULL);
229                         la_from_obdo(&info->fti_attr, oa, OBD_MD_FLGETATTR);
230                         rc = ofd_preprw_write(env, exp, ofd, &info->fti_fid,
231                                               &info->fti_attr, oa, objcount,
232                                               obj, rnb, nr_local, lnb, oti);
233                 }
234         } else if (cmd == OBD_BRW_READ) {
235                 rc = ofd_auth_capa(exp, &info->fti_fid, oa->o_seq,
236                                    capa, CAPA_OPC_OSS_READ);
237                 if (rc == 0) {
238                         ofd_grant_prepare_read(env, exp, oa);
239                         rc = ofd_preprw_read(env, ofd, &info->fti_fid,
240                                              &info->fti_attr, obj->ioo_bufcnt,
241                                              rnb, nr_local, lnb);
242                         obdo_from_la(oa, &info->fti_attr, LA_ATIME);
243                 }
244         } else {
245                 CERROR("%s: wrong cmd %d received!\n",
246                        exp->exp_obd->obd_name, cmd);
247                 rc = -EPROTO;
248         }
249         RETURN(rc);
250 }
251
252 static int
253 ofd_commitrw_read(const struct lu_env *env, struct ofd_device *ofd,
254                   struct lu_fid *fid, int objcount, int niocount,
255                   struct niobuf_local *lnb)
256 {
257         struct ofd_object *fo;
258
259         ENTRY;
260
261         LASSERT(niocount > 0);
262
263         fo = ofd_object_find(env, ofd, fid);
264         if (IS_ERR(fo))
265                 RETURN(PTR_ERR(fo));
266         LASSERT(fo != NULL);
267         LASSERT(ofd_object_exists(fo));
268         dt_bufs_put(env, ofd_object_child(fo), lnb, niocount);
269
270         ofd_read_unlock(env, fo);
271         ofd_object_put(env, fo);
272         /* second put is pair to object_get in ofd_preprw_read */
273         ofd_object_put(env, fo);
274
275         RETURN(0);
276 }
277
278 static int
279 ofd_write_attr_set(const struct lu_env *env, struct ofd_device *ofd,
280                    struct ofd_object *ofd_obj, struct lu_attr *la,
281                    struct filter_fid *ff)
282 {
283         struct ofd_thread_info  *info = ofd_info(env);
284         __u64                    valid = la->la_valid;
285         int                      rc;
286         struct thandle          *th;
287         struct dt_object        *dt_obj;
288         int                      ff_needed = 0;
289
290         ENTRY;
291
292         LASSERT(la);
293
294         dt_obj = ofd_object_child(ofd_obj);
295         LASSERT(dt_obj != NULL);
296
297         la->la_valid &= LA_UID | LA_GID;
298
299         rc = ofd_attr_handle_ugid(env, ofd_obj, la, 0 /* !is_setattr */);
300         if (rc != 0)
301                 GOTO(out, rc);
302
303         if (ff != NULL) {
304                 rc = ofd_object_ff_check(env, ofd_obj);
305                 if (rc == -ENODATA)
306                         ff_needed = 1;
307                 else if (rc < 0)
308                         GOTO(out, rc);
309         }
310
311         if (!la->la_valid && !ff_needed)
312                 /* no attributes to set */
313                 GOTO(out, rc = 0);
314
315         th = ofd_trans_create(env, ofd);
316         if (IS_ERR(th))
317                 GOTO(out, rc = PTR_ERR(th));
318
319         if (la->la_valid) {
320                 rc = dt_declare_attr_set(env, dt_obj, la, th);
321                 if (rc)
322                         GOTO(out_tx, rc);
323         }
324
325         if (ff_needed) {
326                 info->fti_buf.lb_buf = ff;
327                 info->fti_buf.lb_len = sizeof(*ff);
328                 rc = dt_declare_xattr_set(env, dt_obj, &info->fti_buf,
329                                           XATTR_NAME_FID, 0, th);
330                 if (rc)
331                         GOTO(out_tx, rc);
332         }
333
334         /* We don't need a transno for this operation which will be re-executed
335          * anyway when the OST_WRITE (with a transno assigned) is replayed */
336         rc = dt_trans_start_local(env, ofd->ofd_osd , th);
337         if (rc)
338                 GOTO(out_tx, rc);
339
340         /* set uid/gid */
341         if (la->la_valid) {
342                 rc = dt_attr_set(env, dt_obj, la, th,
343                                  ofd_object_capa(env, ofd_obj));
344                 if (rc)
345                         GOTO(out_tx, rc);
346         }
347
348         /* set filter fid EA */
349         if (ff_needed) {
350                 rc = dt_xattr_set(env, dt_obj, &info->fti_buf, XATTR_NAME_FID,
351                                   0, th, BYPASS_CAPA);
352                 if (rc)
353                         GOTO(out_tx, rc);
354         }
355
356         EXIT;
357 out_tx:
358         dt_trans_stop(env, ofd->ofd_osd, th);
359 out:
360         la->la_valid = valid;
361         return rc;
362 }
363
364 static int
365 ofd_commitrw_write(const struct lu_env *env, struct ofd_device *ofd,
366                    struct lu_fid *fid, struct lu_attr *la,
367                    struct filter_fid *ff, int objcount,
368                    int niocount, struct niobuf_local *lnb,
369                    struct obd_trans_info *oti, int old_rc)
370 {
371         struct ofd_thread_info  *info = ofd_info(env);
372         struct ofd_object       *fo;
373         struct dt_object        *o;
374         struct thandle          *th;
375         int                      rc = 0;
376         int                      retries = 0;
377
378         ENTRY;
379
380         LASSERT(objcount == 1);
381
382         fo = ofd_object_find(env, ofd, fid);
383         LASSERT(fo != NULL);
384         LASSERT(ofd_object_exists(fo));
385
386         o = ofd_object_child(fo);
387         LASSERT(o != NULL);
388
389         if (old_rc)
390                 GOTO(out, rc = old_rc);
391
392         /*
393          * The first write to each object must set some attributes.  It is
394          * important to set the uid/gid before calling
395          * dt_declare_write_commit() since quota enforcement is now handled in
396          * declare phases.
397          */
398         rc = ofd_write_attr_set(env, ofd, fo, la, ff);
399         if (rc)
400                 GOTO(out, rc);
401
402         la->la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
403
404 retry:
405         th = ofd_trans_create(env, ofd);
406         if (IS_ERR(th))
407                 GOTO(out, rc = PTR_ERR(th));
408
409         th->th_sync |= oti->oti_sync_write;
410
411         if (OBD_FAIL_CHECK(OBD_FAIL_OST_DQACQ_NET))
412                 GOTO(out_stop, rc = -EINPROGRESS);
413
414         rc = dt_declare_write_commit(env, o, lnb, niocount, th);
415         if (rc)
416                 GOTO(out_stop, rc);
417
418         if (la->la_valid) {
419                 /* update [mac]time if needed */
420                 rc = dt_declare_attr_set(env, o, la, th);
421                 if (rc)
422                         GOTO(out_stop, rc);
423         }
424
425         rc = ofd_trans_start(env, ofd, fo, th);
426         if (rc)
427                 GOTO(out_stop, rc);
428
429         rc = dt_write_commit(env, o, lnb, niocount, th);
430         if (rc)
431                 GOTO(out_stop, rc);
432
433         if (la->la_valid) {
434                 rc = dt_attr_set(env, o, la, th, ofd_object_capa(env, fo));
435                 if (rc)
436                         GOTO(out_stop, rc);
437         }
438
439         /* get attr to return */
440         dt_attr_get(env, o, la, ofd_object_capa(env, fo));
441
442 out_stop:
443         /* Force commit to make the just-deleted blocks
444          * reusable. LU-456 */
445         if (rc == -ENOSPC)
446                 th->th_sync = 1;
447
448         ofd_trans_stop(env, ofd, th, rc);
449         if (rc == -ENOSPC && retries++ < 3) {
450                 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
451                        retries);
452                 goto retry;
453         }
454
455 out:
456         dt_bufs_put(env, o, lnb, niocount);
457         ofd_read_unlock(env, fo);
458         ofd_object_put(env, fo);
459         /* second put is pair to object_get in ofd_preprw_write */
460         ofd_object_put(env, fo);
461         ofd_grant_commit(env, info->fti_exp, old_rc);
462         RETURN(rc);
463 }
464
465 int ofd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
466                  struct obdo *oa, int objcount, struct obd_ioobj *obj,
467                  struct niobuf_remote *rnb, int npages,
468                  struct niobuf_local *lnb, struct obd_trans_info *oti,
469                  int old_rc)
470 {
471         struct ofd_thread_info  *info;
472         struct ofd_mod_data     *fmd;
473         __u64                    valid;
474         struct ofd_device       *ofd = ofd_exp(exp);
475         struct filter_fid       *ff = NULL;
476         int                      rc = 0;
477
478         info = ofd_info(env);
479         ofd_oti2info(info, oti);
480
481         LASSERT(npages > 0);
482
483         fid_ostid_unpack(&info->fti_fid, &oa->o_oi, 0);
484         if (cmd == OBD_BRW_WRITE) {
485                 /* Don't update timestamps if this write is older than a
486                  * setattr which modifies the timestamps. b=10150 */
487
488                 /* XXX when we start having persistent reservations this needs
489                  * to be changed to ofd_fmd_get() to create the fmd if it
490                  * doesn't already exist so we can store the reservation handle
491                  * there. */
492                 valid = OBD_MD_FLUID | OBD_MD_FLGID;
493                 fmd = ofd_fmd_find(exp, &info->fti_fid);
494                 if (!fmd || fmd->fmd_mactime_xid < info->fti_xid)
495                         valid |= OBD_MD_FLATIME | OBD_MD_FLMTIME |
496                                  OBD_MD_FLCTIME;
497                 ofd_fmd_put(exp, fmd);
498                 la_from_obdo(&info->fti_attr, oa, valid);
499
500                 if (oa->o_valid & OBD_MD_FLFID) {
501                         ff = &info->fti_mds_fid;
502                         ofd_prepare_fidea(ff, oa);
503                 }
504
505                 rc = ofd_commitrw_write(env, ofd, &info->fti_fid,
506                                         &info->fti_attr, ff, objcount, npages,
507                                         lnb, oti, old_rc);
508                 if (rc == 0)
509                         obdo_from_la(oa, &info->fti_attr,
510                                      OFD_VALID_FLAGS | LA_GID | LA_UID);
511                 else
512                         obdo_from_la(oa, &info->fti_attr, LA_GID | LA_UID);
513
514                 /* don't report overquota flag if we failed before reaching
515                  * commit */
516                 if (old_rc == 0 && (rc == 0 || rc == -EDQUOT)) {
517                         /* return the overquota flags to client */
518                         if (lnb[0].lnb_flags & OBD_BRW_OVER_USRQUOTA) {
519                                 if (oa->o_valid & OBD_MD_FLFLAGS)
520                                         oa->o_flags |= OBD_FL_NO_USRQUOTA;
521                                 else
522                                         oa->o_flags = OBD_FL_NO_USRQUOTA;
523                         }
524
525                         if (lnb[0].lnb_flags & OBD_BRW_OVER_GRPQUOTA) {
526                                 if (oa->o_valid & OBD_MD_FLFLAGS)
527                                         oa->o_flags |= OBD_FL_NO_GRPQUOTA;
528                                 else
529                                         oa->o_flags = OBD_FL_NO_GRPQUOTA;
530                         }
531
532                         oa->o_valid |= OBD_MD_FLFLAGS;
533                         oa->o_valid |= OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA;
534                 }
535         } else if (cmd == OBD_BRW_READ) {
536                 struct ldlm_namespace *ns = ofd->ofd_namespace;
537
538                 /* If oa != NULL then ofd_preprw_read updated the inode
539                  * atime and we should update the lvb so that other glimpses
540                  * will also get the updated value. bug 5972 */
541                 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
542                          struct ldlm_resource *rs = NULL;
543
544                         ofd_build_resid(&info->fti_fid, &info->fti_resid);
545                         rs = ldlm_resource_get(ns, NULL, &info->fti_resid,
546                                                LDLM_EXTENT, 0);
547                         if (rs != NULL) {
548                                 ns->ns_lvbo->lvbo_update(rs, NULL, 1);
549                                 ldlm_resource_putref(rs);
550                         }
551                 }
552                 rc = ofd_commitrw_read(env, ofd, &info->fti_fid, objcount,
553                                           npages, lnb);
554                 if (old_rc)
555                         rc = old_rc;
556         } else {
557                 LBUG();
558                 rc = -EPROTO;
559         }
560
561         ofd_info2oti(info, oti);
562         RETURN(rc);
563 }