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) 2011, 2012, Whamcloud, Inc.
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 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)
50 struct ofd_object *fo;
51 int i, j, rc, tot_bytes = 0;
56 fo = ofd_object_find(env, ofd, fid);
61 ofd_read_lock(env, fo);
62 if (!ofd_object_exists(fo))
63 GOTO(unlock, rc = -ENOENT);
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));
70 LASSERT(rc <= PTLRPC_MAX_BRW_PAGES);
71 /* correct index for local buffers to continue with */
73 LASSERT(j <= PTLRPC_MAX_BRW_PAGES);
74 tot_bytes += rnb[i].rnb_len;
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));
84 rc = dt_read_prep(env, ofd_object_child(fo), lnb, *nr_local);
87 lprocfs_counter_add(ofd_obd(ofd)->obd_stats,
88 LPROC_OFD_READ_BYTES, tot_bytes);
92 dt_bufs_put(env, ofd_object_child(fo), lnb, *nr_local);
94 ofd_read_unlock(env, fo);
95 ofd_object_put(env, fo);
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)
107 struct ofd_object *fo;
108 int i, j, k, rc = 0, tot_bytes = 0;
111 LASSERT(env != NULL);
112 LASSERT(objcount == 1);
114 if (unlikely(exp->exp_obd->obd_recovering)) {
115 struct ofd_thread_info *info = ofd_info(env);
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;
131 fo = ofd_object_find_or_create(env, ofd, fid, &info->fti_attr);
133 fo = ofd_object_find(env, ofd, fid);
137 GOTO(out, rc = PTR_ERR(fo));
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);
149 /* Always sync if syncjournal parameter is set */
150 oti->oti_sync_write = ofd->ofd_syncjournal;
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);
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),
160 ofd_object_capa(env, fo));
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;
172 LASSERT(j <= PTLRPC_MAX_BRW_PAGES);
173 tot_bytes += rnb[i].rnb_len;
176 LASSERT(*nr_local > 0 && *nr_local <= PTLRPC_MAX_BRW_PAGES);
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);
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);
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)
203 struct ofd_device *ofd = ofd_exp(exp);
204 struct ofd_thread_info *info;
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);
215 info = ofd_info_init(env, exp);
217 LASSERT(objcount == 1);
218 LASSERT(obj->ioo_bufcnt > 0);
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);
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);
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);
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,
239 obdo_from_la(oa, &info->fti_attr, LA_ATIME);
242 CERROR("%s: wrong cmd %d received!\n",
243 exp->exp_obd->obd_name, cmd);
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)
254 struct ofd_object *fo;
258 LASSERT(niocount > 0);
260 fo = ofd_object_find(env, ofd, fid);
264 LASSERT(ofd_object_exists(fo));
265 dt_bufs_put(env, ofd_object_child(fo), lnb, niocount);
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);
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)
280 struct ofd_thread_info *info = ofd_info(env);
281 __u64 valid = la->la_valid;
284 struct dt_object *dt_obj;
291 dt_obj = ofd_object_child(ofd_obj);
292 LASSERT(dt_obj != NULL);
294 la->la_valid &= LA_UID | LA_GID;
296 rc = ofd_attr_handle_ugid(env, ofd_obj, la, 0 /* !is_setattr */);
301 rc = ofd_object_ff_check(env, ofd_obj);
308 if (!la->la_valid && !ff_needed)
309 /* no attributes to set */
312 th = ofd_trans_create(env, ofd);
314 GOTO(out, rc = PTR_ERR(th));
317 rc = dt_declare_attr_set(env, dt_obj, la, th);
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);
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);
339 rc = dt_attr_set(env, dt_obj, la, th,
340 ofd_object_capa(env, ofd_obj));
345 /* set filter fid EA */
347 rc = dt_xattr_set(env, dt_obj, &info->fti_buf, XATTR_NAME_FID,
355 dt_trans_stop(env, ofd->ofd_osd, th);
357 la->la_valid = valid;
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)
368 struct ofd_thread_info *info = ofd_info(env);
369 struct ofd_object *fo;
377 LASSERT(objcount == 1);
379 fo = ofd_object_find(env, ofd, fid);
381 LASSERT(ofd_object_exists(fo));
383 o = ofd_object_child(fo);
387 GOTO(out, rc = old_rc);
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
395 rc = ofd_write_attr_set(env, ofd, fo, la, ff);
399 la->la_valid &= LA_ATIME | LA_MTIME | LA_CTIME;
402 th = ofd_trans_create(env, ofd);
404 GOTO(out, rc = PTR_ERR(th));
406 th->th_sync |= oti->oti_sync_write;
408 rc = dt_declare_write_commit(env, o, lnb, niocount, th);
413 /* update [mac]time if needed */
414 rc = dt_declare_attr_set(env, o, la, th);
419 rc = ofd_trans_start(env, ofd, fo, th);
423 rc = dt_write_commit(env, o, lnb, niocount, th);
428 rc = dt_attr_set(env, o, la, th, ofd_object_capa(env, fo));
433 /* get attr to return */
434 dt_attr_get(env, o, la, ofd_object_capa(env, fo));
437 /* Force commit to make the just-deleted blocks
438 * reusable. LU-456 */
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",
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);
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,
465 struct ofd_thread_info *info;
466 struct ofd_mod_data *fmd;
468 struct ofd_device *ofd = ofd_exp(exp);
469 struct filter_fid *ff = NULL;
472 info = ofd_info(env);
473 ofd_oti2info(info, oti);
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 */
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
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 |
491 ofd_fmd_put(exp, fmd);
492 la_from_obdo(&info->fti_attr, oa, valid);
494 if (oa->o_valid & OBD_MD_FLFID) {
495 ff = &info->fti_mds_fid;
496 ofd_prepare_fidea(ff, oa);
499 rc = ofd_commitrw_write(env, ofd, &info->fti_fid,
500 &info->fti_attr, ff, objcount, npages,
503 obdo_from_la(oa, &info->fti_attr,
504 OFD_VALID_FLAGS | LA_GID | LA_UID);
506 obdo_from_la(oa, &info->fti_attr, LA_GID | LA_UID);
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
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;
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;
523 ofd_build_resid(&info->fti_fid, &info->fti_resid);
524 rs = ldlm_resource_get(ns, NULL, &info->fti_resid,
527 ns->ns_lvbo->lvbo_update(rs, NULL, 1);
528 ldlm_resource_putref(rs);
531 rc = ofd_commitrw_read(env, ofd, &info->fti_fid, objcount,
540 ofd_info2oti(info, oti);