4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Alex Tomas <bzzz@whamcloud.com>
41 #define DEBUG_SUBSYSTEM S_FILTER
43 #include "ofd_internal.h"
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)
52 struct ofd_object *fo;
53 int i, j, rc, tot_bytes = 0;
58 fo = ofd_object_find(env, ofd, fid);
63 ofd_read_lock(env, fo);
64 if (!ofd_object_exists(fo))
65 GOTO(unlock, rc = -ENOENT);
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));
72 LASSERT(rc <= PTLRPC_MAX_BRW_PAGES);
73 /* correct index for local buffers to continue with */
75 LASSERT(j <= PTLRPC_MAX_BRW_PAGES);
76 tot_bytes += rnb[i].rnb_len;
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));
86 rc = dt_read_prep(env, ofd_object_child(fo), lnb, *nr_local);
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);
96 dt_bufs_put(env, ofd_object_child(fo), lnb, *nr_local);
98 ofd_read_unlock(env, fo);
99 ofd_object_put(env, fo);
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)
111 struct ofd_object *fo;
112 int i, j, k, rc = 0, tot_bytes = 0;
115 LASSERT(env != NULL);
116 LASSERT(objcount == 1);
118 if (unlikely(exp->exp_obd->obd_recovering)) {
119 struct ofd_thread_info *info = ofd_info(env);
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;
135 fo = ofd_object_find_or_create(env, ofd, fid, &info->fti_attr);
137 fo = ofd_object_find(env, ofd, fid);
141 GOTO(out, rc = PTR_ERR(fo));
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);
153 /* Always sync if syncjournal parameter is set */
154 oti->oti_sync_write = ofd->ofd_syncjournal;
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);
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),
164 ofd_object_capa(env, fo));
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;
179 LASSERT(j <= PTLRPC_MAX_BRW_PAGES);
180 tot_bytes += rnb[i].rnb_len;
183 LASSERT(*nr_local > 0 && *nr_local <= PTLRPC_MAX_BRW_PAGES);
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);
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);
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);
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)
213 struct ofd_device *ofd = ofd_exp(exp);
214 struct ofd_thread_info *info;
217 rc = lu_env_refill((struct lu_env *)env);
219 info = ofd_info_init(env, exp);
223 if (OBD_FAIL_CHECK(OBD_FAIL_OST_ENOENT)) {
224 struct ofd_seq *oseq;
225 oseq = ofd_seq_load(env, ofd, oa->o_seq);
227 CERROR("%s: Can not find seq for "LPU64":"LPU64
228 ": rc = %ld\n", ofd_name(ofd), oa->o_seq,
229 oa->o_id, PTR_ERR(oseq));
233 if (oseq->os_destroys_in_progress == 0) {
234 /* don't fail lookups for orphan recovery, it causes
235 * later LBUGs when objects still exist during
237 ofd_seq_put(env, oseq);
240 ofd_seq_put(env, oseq);
243 LASSERT(objcount == 1);
244 LASSERT(obj->ioo_bufcnt > 0);
246 fid_ostid_unpack(&info->fti_fid, &oa->o_oi, 0);
247 if (cmd == OBD_BRW_WRITE) {
248 rc = ofd_auth_capa(exp, &info->fti_fid, oa->o_seq,
249 capa, CAPA_OPC_OSS_WRITE);
251 la_from_obdo(&info->fti_attr, oa, OBD_MD_FLGETATTR);
252 rc = ofd_preprw_write(env, exp, ofd, &info->fti_fid,
253 &info->fti_attr, oa, objcount,
254 obj, rnb, nr_local, lnb, oti);
256 } else if (cmd == OBD_BRW_READ) {
257 rc = ofd_auth_capa(exp, &info->fti_fid, oa->o_seq,
258 capa, CAPA_OPC_OSS_READ);
260 ofd_grant_prepare_read(env, exp, oa);
261 rc = ofd_preprw_read(env, exp, ofd, &info->fti_fid,
262 &info->fti_attr, obj->ioo_bufcnt,
263 rnb, nr_local, lnb, oti);
264 obdo_from_la(oa, &info->fti_attr, LA_ATIME);
267 CERROR("%s: wrong cmd %d received!\n",
268 exp->exp_obd->obd_name, cmd);
275 ofd_commitrw_read(const struct lu_env *env, struct ofd_device *ofd,
276 struct lu_fid *fid, int objcount, int niocount,
277 struct niobuf_local *lnb)
279 struct ofd_object *fo;
283 LASSERT(niocount > 0);
285 fo = ofd_object_find(env, ofd, fid);
289 LASSERT(ofd_object_exists(fo));
290 dt_bufs_put(env, ofd_object_child(fo), lnb, niocount);
292 ofd_read_unlock(env, fo);
293 ofd_object_put(env, fo);
294 /* second put is pair to object_get in ofd_preprw_read */
295 ofd_object_put(env, fo);
301 ofd_write_attr_set(const struct lu_env *env, struct ofd_device *ofd,
302 struct ofd_object *ofd_obj, struct lu_attr *la,
303 struct filter_fid *ff)
305 struct ofd_thread_info *info = ofd_info(env);
306 __u64 valid = la->la_valid;
309 struct dt_object *dt_obj;
316 dt_obj = ofd_object_child(ofd_obj);
317 LASSERT(dt_obj != NULL);
319 la->la_valid &= LA_UID | LA_GID;
321 rc = ofd_attr_handle_ugid(env, ofd_obj, la, 0 /* !is_setattr */);
326 rc = ofd_object_ff_check(env, ofd_obj);
333 if (!la->la_valid && !ff_needed)
334 /* no attributes to set */
337 th = ofd_trans_create(env, ofd);
339 GOTO(out, rc = PTR_ERR(th));
342 rc = dt_declare_attr_set(env, dt_obj, la, th);
348 info->fti_buf.lb_buf = ff;
349 info->fti_buf.lb_len = sizeof(*ff);
350 rc = dt_declare_xattr_set(env, dt_obj, &info->fti_buf,
351 XATTR_NAME_FID, 0, th);
356 /* We don't need a transno for this operation which will be re-executed
357 * anyway when the OST_WRITE (with a transno assigned) is replayed */
358 rc = dt_trans_start_local(env, ofd->ofd_osd , th);
364 rc = dt_attr_set(env, dt_obj, la, th,
365 ofd_object_capa(env, ofd_obj));
370 /* set filter fid EA */
372 rc = dt_xattr_set(env, dt_obj, &info->fti_buf, XATTR_NAME_FID,
380 dt_trans_stop(env, ofd->ofd_osd, th);
382 la->la_valid = valid;
387 ofd_commitrw_write(const struct lu_env *env, struct ofd_device *ofd,
388 struct lu_fid *fid, struct lu_attr *la,
389 struct filter_fid *ff, int objcount,
390 int niocount, struct niobuf_local *lnb,
391 struct obd_trans_info *oti, int old_rc)
393 struct ofd_thread_info *info = ofd_info(env);
394 struct ofd_object *fo;
402 LASSERT(objcount == 1);
404 fo = ofd_object_find(env, ofd, fid);
406 LASSERT(ofd_object_exists(fo));
408 o = ofd_object_child(fo);
412 GOTO(out, rc = old_rc);
415 * The first write to each object must set some attributes. It is
416 * important to set the uid/gid before calling
417 * dt_declare_write_commit() since quota enforcement is now handled in
420 rc = ofd_write_attr_set(env, ofd, fo, la, ff);
424 la->la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
427 th = ofd_trans_create(env, ofd);
429 GOTO(out, rc = PTR_ERR(th));
431 th->th_sync |= oti->oti_sync_write;
433 if (OBD_FAIL_CHECK(OBD_FAIL_OST_DQACQ_NET))
434 GOTO(out_stop, rc = -EINPROGRESS);
436 rc = dt_declare_write_commit(env, o, lnb, niocount, th);
441 /* update [mac]time if needed */
442 rc = dt_declare_attr_set(env, o, la, th);
447 rc = ofd_trans_start(env, ofd, fo, th);
451 rc = dt_write_commit(env, o, lnb, niocount, th);
456 rc = dt_attr_set(env, o, la, th, ofd_object_capa(env, fo));
461 /* get attr to return */
462 rc = dt_attr_get(env, o, la, ofd_object_capa(env, fo));
465 /* Force commit to make the just-deleted blocks
466 * reusable. LU-456 */
470 ofd_trans_stop(env, ofd, th, rc);
471 if (rc == -ENOSPC && retries++ < 3) {
472 CDEBUG(D_INODE, "retry after force commit, retries:%d\n",
478 dt_bufs_put(env, o, lnb, niocount);
479 ofd_read_unlock(env, fo);
480 ofd_object_put(env, fo);
481 /* second put is pair to object_get in ofd_preprw_write */
482 ofd_object_put(env, fo);
483 ofd_grant_commit(env, info->fti_exp, old_rc);
487 int ofd_commitrw(const struct lu_env *env, int cmd, struct obd_export *exp,
488 struct obdo *oa, int objcount, struct obd_ioobj *obj,
489 struct niobuf_remote *rnb, int npages,
490 struct niobuf_local *lnb, struct obd_trans_info *oti,
493 struct ofd_thread_info *info;
494 struct ofd_mod_data *fmd;
496 struct ofd_device *ofd = ofd_exp(exp);
497 struct filter_fid *ff = NULL;
500 info = ofd_info(env);
501 ofd_oti2info(info, oti);
505 fid_ostid_unpack(&info->fti_fid, &oa->o_oi, 0);
506 if (cmd == OBD_BRW_WRITE) {
507 /* Don't update timestamps if this write is older than a
508 * setattr which modifies the timestamps. b=10150 */
510 /* XXX when we start having persistent reservations this needs
511 * to be changed to ofd_fmd_get() to create the fmd if it
512 * doesn't already exist so we can store the reservation handle
514 valid = OBD_MD_FLUID | OBD_MD_FLGID;
515 fmd = ofd_fmd_find(exp, &info->fti_fid);
516 if (!fmd || fmd->fmd_mactime_xid < info->fti_xid)
517 valid |= OBD_MD_FLATIME | OBD_MD_FLMTIME |
519 ofd_fmd_put(exp, fmd);
520 la_from_obdo(&info->fti_attr, oa, valid);
522 if (oa->o_valid & OBD_MD_FLFID) {
523 ff = &info->fti_mds_fid;
524 ofd_prepare_fidea(ff, oa);
527 rc = ofd_commitrw_write(env, ofd, &info->fti_fid,
528 &info->fti_attr, ff, objcount, npages,
531 obdo_from_la(oa, &info->fti_attr,
532 OFD_VALID_FLAGS | LA_GID | LA_UID);
534 obdo_from_la(oa, &info->fti_attr, LA_GID | LA_UID);
536 /* don't report overquota flag if we failed before reaching
538 if (old_rc == 0 && (rc == 0 || rc == -EDQUOT)) {
539 /* return the overquota flags to client */
540 if (lnb[0].lnb_flags & OBD_BRW_OVER_USRQUOTA) {
541 if (oa->o_valid & OBD_MD_FLFLAGS)
542 oa->o_flags |= OBD_FL_NO_USRQUOTA;
544 oa->o_flags = OBD_FL_NO_USRQUOTA;
547 if (lnb[0].lnb_flags & OBD_BRW_OVER_GRPQUOTA) {
548 if (oa->o_valid & OBD_MD_FLFLAGS)
549 oa->o_flags |= OBD_FL_NO_GRPQUOTA;
551 oa->o_flags = OBD_FL_NO_GRPQUOTA;
554 oa->o_valid |= OBD_MD_FLFLAGS;
555 oa->o_valid |= OBD_MD_FLUSRQUOTA | OBD_MD_FLGRPQUOTA;
557 } else if (cmd == OBD_BRW_READ) {
558 struct ldlm_namespace *ns = ofd->ofd_namespace;
560 /* If oa != NULL then ofd_preprw_read updated the inode
561 * atime and we should update the lvb so that other glimpses
562 * will also get the updated value. bug 5972 */
563 if (oa && ns && ns->ns_lvbo && ns->ns_lvbo->lvbo_update) {
564 struct ldlm_resource *rs = NULL;
566 ofd_build_resid(&info->fti_fid, &info->fti_resid);
567 rs = ldlm_resource_get(ns, NULL, &info->fti_resid,
570 ns->ns_lvbo->lvbo_update(rs, NULL, 1);
571 ldlm_resource_putref(rs);
574 rc = ofd_commitrw_read(env, ofd, &info->fti_fid, objcount,
583 ofd_info2oti(info, oti);