1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/cmm/mdc_object.c
38 * Lustre Cluster Metadata Manager (cmm)
40 * Author: Mike Pershin <tappro@clusterfs.com>
44 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
48 #include <obd_support.h>
49 #include <lustre_lib.h>
50 #include <obd_class.h>
51 #include <lustre_mdc.h>
52 #include "cmm_internal.h"
53 #include "mdc_internal.h"
55 static struct md_object_operations mdc_mo_ops;
56 static struct md_dir_operations mdc_dir_ops;
57 static struct lu_object_operations mdc_obj_ops;
59 extern struct lu_context_key mdc_thread_key;
61 struct lu_object *mdc_object_alloc(const struct lu_env *env,
62 const struct lu_object_header *hdr,
65 struct mdc_object *mco;
72 lo = &mco->mco_obj.mo_lu;
73 lu_object_init(lo, NULL, ld);
74 mco->mco_obj.mo_ops = &mdc_mo_ops;
75 mco->mco_obj.mo_dir_ops = &mdc_dir_ops;
76 lo->lo_ops = &mdc_obj_ops;
82 static void mdc_object_free(const struct lu_env *env, struct lu_object *lo)
84 struct mdc_object *mco = lu2mdc_obj(lo);
89 static int mdc_object_init(const struct lu_env *env, struct lu_object *lo)
92 lo->lo_header->loh_attr |= LOHA_REMOTE;
96 static int mdc_object_print(const struct lu_env *env, void *cookie,
97 lu_printer_t p, const struct lu_object *lo)
99 return (*p)(env, cookie, LUSTRE_CMM_MDC_NAME"-object@%p", lo);
102 static struct lu_object_operations mdc_obj_ops = {
103 .loo_object_init = mdc_object_init,
104 .loo_object_free = mdc_object_free,
105 .loo_object_print = mdc_object_print,
108 /* md_object_operations */
110 struct mdc_thread_info *mdc_info_get(const struct lu_env *env)
112 struct mdc_thread_info *mci;
114 mci = lu_context_key_get(&env->le_ctx, &mdc_thread_key);
120 struct mdc_thread_info *mdc_info_init(const struct lu_env *env)
122 struct mdc_thread_info *mci = mdc_info_get(env);
123 memset(mci, 0, sizeof(*mci));
127 static void mdc_body2attr(struct mdt_body *body, struct md_attr *ma)
129 struct lu_attr *la = &ma->ma_attr;
131 if (body->valid & OBD_MD_FLCTIME && body->ctime >= la->la_ctime) {
132 la->la_ctime = body->ctime;
133 if (body->valid & OBD_MD_FLMTIME)
134 la->la_mtime = body->mtime;
137 if (body->valid & OBD_MD_FLMODE)
138 la->la_mode = body->mode;
139 if (body->valid & OBD_MD_FLSIZE)
140 la->la_size = body->size;
141 if (body->valid & OBD_MD_FLBLOCKS)
142 la->la_blocks = body->blocks;
143 if (body->valid & OBD_MD_FLUID)
144 la->la_uid = body->uid;
145 if (body->valid & OBD_MD_FLGID)
146 la->la_gid = body->gid;
147 if (body->valid & OBD_MD_FLFLAGS)
148 la->la_flags = body->flags;
149 if (body->valid & OBD_MD_FLNLINK)
150 la->la_nlink = body->nlink;
151 if (body->valid & OBD_MD_FLRDEV)
152 la->la_rdev = body->rdev;
154 la->la_valid = body->valid;
155 ma->ma_valid = MA_INODE;
158 static int mdc_req2attr_update(const struct lu_env *env,
161 struct mdc_thread_info *mci;
162 struct ptlrpc_request *req;
163 struct mdt_body *body;
164 struct lov_mds_md *lov;
165 struct llog_cookie *cookie;
168 mci = mdc_info_get(env);
171 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
173 mdc_body2attr(body, ma);
175 if (body->valid & OBD_MD_FLMDSCAPA) {
176 struct lustre_capa *capa;
178 /* create for cross-ref will fetch mds capa from remote obj */
179 capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA1);
180 LASSERT(capa != NULL);
181 LASSERT(ma->ma_capa != NULL);
182 *ma->ma_capa = *capa;
185 if (!(body->valid & OBD_MD_FLEASIZE))
188 if (body->eadatasize == 0) {
189 CERROR("OBD_MD_FLEASIZE is set but eadatasize is zero\n");
193 lov = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD,
198 LASSERT(ma->ma_lmm != NULL);
199 LASSERT(ma->ma_lmm_size >= body->eadatasize);
200 ma->ma_lmm_size = body->eadatasize;
201 memcpy(ma->ma_lmm, lov, ma->ma_lmm_size);
202 ma->ma_valid |= MA_LOV;
204 if (!(body->valid & OBD_MD_FLCOOKIE))
207 if (body->aclsize == 0) {
208 CERROR("OBD_MD_FLCOOKIE is set but cookie size is zero\n");
212 cookie = req_capsule_server_sized_get(&req->rq_pill, &RMF_ACL,
217 LASSERT(ma->ma_cookie != NULL);
218 LASSERT(ma->ma_cookie_size == body->aclsize);
219 memcpy(ma->ma_cookie, cookie, ma->ma_cookie_size);
220 ma->ma_valid |= MA_COOKIE;
224 static int mdc_attr_get(const struct lu_env *env, struct md_object *mo,
227 struct mdc_device *mc = md2mdc_dev(md_obj2dev(mo));
228 struct mdc_thread_info *mci;
232 mci = lu_context_key_get(&env->le_ctx, &mdc_thread_key);
235 memset(&mci->mci_opdata, 0, sizeof(mci->mci_opdata));
237 rc = md_getattr(mc->mc_desc.cl_exp, lu_object_fid(&mo->mo_lu),
238 NULL, OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID |
239 OBD_MD_FLFLAGS | OBD_MD_FLCROSSREF, 0, &mci->mci_req);
241 /* get attr from request */
242 rc = mdc_req2attr_update(env, ma);
245 ptlrpc_req_finished(mci->mci_req);
250 static inline struct timespec *mdc_attr_time(struct timespec *t, __u64 seconds)
258 * XXX: It is only used for set ctime when rename's source on remote MDS.
260 static int mdc_attr_set(const struct lu_env *env, struct md_object *mo,
261 const struct md_attr *ma)
263 struct mdc_device *mc = md2mdc_dev(md_obj2dev(mo));
264 const struct lu_attr *la = &ma->ma_attr;
265 struct mdc_thread_info *mci;
266 struct md_ucred *uc = md_ucred(env);
270 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
272 mci = lu_context_key_get(&env->le_ctx, &mdc_thread_key);
275 memset(&mci->mci_opdata, 0, sizeof(mci->mci_opdata));
277 mci->mci_opdata.op_fid1 = *lu_object_fid(&mo->mo_lu);
278 mdc_attr_time(&mci->mci_opdata.op_attr.ia_ctime, la->la_ctime);
279 mci->mci_opdata.op_attr.ia_mode = la->la_mode;
280 mci->mci_opdata.op_attr.ia_valid = ATTR_CTIME_SET;
282 ((uc->mu_valid == UCRED_OLD) || (uc->mu_valid == UCRED_NEW))) {
283 mci->mci_opdata.op_fsuid = uc->mu_fsuid;
284 mci->mci_opdata.op_fsgid = uc->mu_fsgid;
285 mci->mci_opdata.op_cap = uc->mu_cap;
286 if (uc->mu_ginfo || (uc->mu_valid == UCRED_OLD)) {
287 mci->mci_opdata.op_suppgids[0] = uc->mu_suppgids[0];
288 mci->mci_opdata.op_suppgids[1] = uc->mu_suppgids[1];
290 mci->mci_opdata.op_suppgids[0] =
291 mci->mci_opdata.op_suppgids[1] = -1;
294 mci->mci_opdata.op_fsuid = la->la_uid;
295 mci->mci_opdata.op_fsgid = la->la_gid;
296 mci->mci_opdata.op_cap = cfs_curproc_cap_pack();
297 mci->mci_opdata.op_suppgids[0] =
298 mci->mci_opdata.op_suppgids[1] = -1;
301 rc = md_setattr(mc->mc_desc.cl_exp, &mci->mci_opdata,
302 NULL, 0, NULL, 0, &mci->mci_req, NULL);
304 ptlrpc_req_finished(mci->mci_req);
309 static int mdc_object_create(const struct lu_env *env,
310 struct md_object *mo,
311 const struct md_op_spec *spec,
314 struct mdc_device *mc = md2mdc_dev(md_obj2dev(mo));
315 struct lu_attr *la = &ma->ma_attr;
316 struct mdc_thread_info *mci;
318 struct md_ucred *uc = md_ucred(env);
325 LASSERT(S_ISDIR(la->la_mode));
326 LASSERT(spec->u.sp_pfid != NULL);
328 mci = mdc_info_init(env);
329 mci->mci_opdata.op_bias = MDS_CROSS_REF;
330 mci->mci_opdata.op_fid2 = *lu_object_fid(&mo->mo_lu);
332 /* Parent fid is needed to create dotdot on the remote node. */
333 mci->mci_opdata.op_fid1 = *(spec->u.sp_pfid);
334 mci->mci_opdata.op_mod_time = la->la_ctime;
336 ((uc->mu_valid == UCRED_OLD) || (uc->mu_valid == UCRED_NEW))) {
338 if (la->la_mode & S_ISGID)
343 if (uc->mu_ginfo || (uc->mu_valid == UCRED_OLD))
344 mci->mci_opdata.op_suppgids[0] = uc->mu_suppgids[0];
346 mci->mci_opdata.op_suppgids[0] = -1;
351 mci->mci_opdata.op_suppgids[0] = -1;
354 /* get data from spec */
355 if (spec->sp_cr_flags & MDS_CREATE_SLAVE_OBJ) {
356 symname = spec->u.sp_ea.eadata;
357 symlen = spec->u.sp_ea.eadatalen;
358 mci->mci_opdata.op_fid1 = *(spec->u.sp_ea.fid);
359 mci->mci_opdata.op_flags |= MDS_CREATE_SLAVE_OBJ;
360 #ifdef CONFIG_FS_POSIX_ACL
361 } else if (spec->sp_cr_flags & MDS_CREATE_RMT_ACL) {
362 symname = spec->u.sp_ea.eadata;
363 symlen = spec->u.sp_ea.eadatalen;
364 mci->mci_opdata.op_fid1 = *(spec->u.sp_ea.fid);
365 mci->mci_opdata.op_flags |= MDS_CREATE_RMT_ACL;
368 symname = spec->u.sp_symname;
369 symlen = symname ? strlen(symname) + 1 : 0;
372 rc = md_create(mc->mc_desc.cl_exp, &mci->mci_opdata,
373 symname, symlen, la->la_mode, uid, gid,
374 cap, la->la_rdev, &mci->mci_req);
377 /* get attr from request */
378 rc = mdc_req2attr_update(env, ma);
381 ptlrpc_req_finished(mci->mci_req);
386 static int mdc_ref_add(const struct lu_env *env, struct md_object *mo,
387 const struct md_attr *ma)
389 struct mdc_device *mc = md2mdc_dev(md_obj2dev(mo));
390 const struct lu_attr *la = &ma->ma_attr;
391 struct mdc_thread_info *mci;
392 struct md_ucred *uc = md_ucred(env);
396 mci = lu_context_key_get(&env->le_ctx, &mdc_thread_key);
399 memset(&mci->mci_opdata, 0, sizeof(mci->mci_opdata));
400 mci->mci_opdata.op_bias = MDS_CROSS_REF;
401 mci->mci_opdata.op_fid1 = *lu_object_fid(&mo->mo_lu);
402 mci->mci_opdata.op_mod_time = la->la_ctime;
404 ((uc->mu_valid == UCRED_OLD) || (uc->mu_valid == UCRED_NEW))) {
405 mci->mci_opdata.op_fsuid = uc->mu_fsuid;
406 mci->mci_opdata.op_fsgid = uc->mu_fsgid;
407 mci->mci_opdata.op_cap = uc->mu_cap;
408 if (uc->mu_ginfo || (uc->mu_valid == UCRED_OLD)) {
409 mci->mci_opdata.op_suppgids[0] = uc->mu_suppgids[0];
410 mci->mci_opdata.op_suppgids[1] = uc->mu_suppgids[1];
412 mci->mci_opdata.op_suppgids[0] =
413 mci->mci_opdata.op_suppgids[1] = -1;
416 mci->mci_opdata.op_fsuid = la->la_uid;
417 mci->mci_opdata.op_fsgid = la->la_gid;
418 mci->mci_opdata.op_cap = cfs_curproc_cap_pack();
419 mci->mci_opdata.op_suppgids[0] =
420 mci->mci_opdata.op_suppgids[1] = -1;
424 rc = md_link(mc->mc_desc.cl_exp, &mci->mci_opdata, &mci->mci_req);
426 ptlrpc_req_finished(mci->mci_req);
431 static int mdc_ref_del(const struct lu_env *env, struct md_object *mo,
434 struct mdc_device *mc = md2mdc_dev(md_obj2dev(mo));
435 struct lu_attr *la = &ma->ma_attr;
436 struct mdc_thread_info *mci;
437 struct md_ucred *uc = md_ucred(env);
441 mci = mdc_info_init(env);
442 mci->mci_opdata.op_bias = MDS_CROSS_REF;
443 if (ma->ma_attr_flags & MDS_VTX_BYPASS)
444 mci->mci_opdata.op_bias |= MDS_VTX_BYPASS;
446 mci->mci_opdata.op_bias &= ~MDS_VTX_BYPASS;
447 mci->mci_opdata.op_fid1 = *lu_object_fid(&mo->mo_lu);
448 mci->mci_opdata.op_mode = la->la_mode;
449 mci->mci_opdata.op_mod_time = la->la_ctime;
451 ((uc->mu_valid == UCRED_OLD) || (uc->mu_valid == UCRED_NEW))) {
452 mci->mci_opdata.op_fsuid = uc->mu_fsuid;
453 mci->mci_opdata.op_fsgid = uc->mu_fsgid;
454 mci->mci_opdata.op_cap = uc->mu_cap;
455 if (uc->mu_ginfo || (uc->mu_valid == UCRED_OLD))
456 mci->mci_opdata.op_suppgids[0] = uc->mu_suppgids[0];
458 mci->mci_opdata.op_suppgids[0] = -1;
460 mci->mci_opdata.op_fsuid = la->la_uid;
461 mci->mci_opdata.op_fsgid = la->la_gid;
462 mci->mci_opdata.op_cap = cfs_curproc_cap_pack();
463 mci->mci_opdata.op_suppgids[0] = -1;
466 rc = md_unlink(mc->mc_desc.cl_exp, &mci->mci_opdata, &mci->mci_req);
468 /* get attr from request */
469 rc = mdc_req2attr_update(env, ma);
472 ptlrpc_req_finished(mci->mci_req);
477 #ifdef HAVE_SPLIT_SUPPORT
478 int mdc_send_page(struct cmm_device *cm, const struct lu_env *env,
479 struct md_object *mo, struct page *page, __u32 offset)
481 struct mdc_device *mc = md2mdc_dev(md_obj2dev(mo));
485 rc = mdc_sendpage(mc->mc_desc.cl_exp, lu_object_fid(&mo->mo_lu),
487 CDEBUG(rc ? D_ERROR : D_INFO, "send page %p offset %d fid "DFID
488 " rc %d \n", page, offset, PFID(lu_object_fid(&mo->mo_lu)), rc);
493 static struct md_object_operations mdc_mo_ops = {
494 .moo_attr_get = mdc_attr_get,
495 .moo_attr_set = mdc_attr_set,
496 .moo_object_create = mdc_object_create,
497 .moo_ref_add = mdc_ref_add,
498 .moo_ref_del = mdc_ref_del,
501 /* md_dir_operations */
502 static int mdc_rename_tgt(const struct lu_env *env, struct md_object *mo_p,
503 struct md_object *mo_t, const struct lu_fid *lf,
504 const struct lu_name *lname, struct md_attr *ma)
506 struct mdc_device *mc = md2mdc_dev(md_obj2dev(mo_p));
507 struct lu_attr *la = &ma->ma_attr;
508 struct mdc_thread_info *mci;
509 struct md_ucred *uc = md_ucred(env);
513 mci = mdc_info_init(env);
514 mci->mci_opdata.op_bias = MDS_CROSS_REF;
515 if (ma->ma_attr_flags & MDS_VTX_BYPASS)
516 mci->mci_opdata.op_bias |= MDS_VTX_BYPASS;
518 mci->mci_opdata.op_bias &= ~MDS_VTX_BYPASS;
519 mci->mci_opdata.op_fid1 = *lu_object_fid(&mo_p->mo_lu);
520 mci->mci_opdata.op_fid2 = *lf;
521 mci->mci_opdata.op_mode = la->la_mode;
522 mci->mci_opdata.op_mod_time = la->la_ctime;
524 ((uc->mu_valid == UCRED_OLD) || (uc->mu_valid == UCRED_NEW))) {
525 mci->mci_opdata.op_fsuid = uc->mu_fsuid;
526 mci->mci_opdata.op_fsgid = uc->mu_fsgid;
527 mci->mci_opdata.op_cap = uc->mu_cap;
528 if (uc->mu_ginfo || (uc->mu_valid == UCRED_OLD)) {
529 mci->mci_opdata.op_suppgids[0] = uc->mu_suppgids[0];
530 mci->mci_opdata.op_suppgids[1] = uc->mu_suppgids[1];
532 mci->mci_opdata.op_suppgids[0] =
533 mci->mci_opdata.op_suppgids[1] = -1;
536 mci->mci_opdata.op_fsuid = la->la_uid;
537 mci->mci_opdata.op_fsgid = la->la_gid;
538 mci->mci_opdata.op_cap = cfs_curproc_cap_pack();
539 mci->mci_opdata.op_suppgids[0] =
540 mci->mci_opdata.op_suppgids[1] = -1;
543 rc = md_rename(mc->mc_desc.cl_exp, &mci->mci_opdata, NULL, 0,
544 lname->ln_name, lname->ln_namelen, &mci->mci_req);
546 /* get attr from request */
547 mdc_req2attr_update(env, ma);
550 ptlrpc_req_finished(mci->mci_req);
555 * Return resulting fid in sfid
556 * 0: fids are not relatives
557 * fid: fid at which search stopped
559 static int mdc_is_subdir(const struct lu_env *env, struct md_object *mo,
560 const struct lu_fid *fid, struct lu_fid *sfid)
562 struct mdc_device *mc = md2mdc_dev(md_obj2dev(mo));
563 struct mdc_thread_info *mci;
564 struct mdt_body *body;
568 mci = mdc_info_init(env);
570 rc = md_is_subdir(mc->mc_desc.cl_exp, lu_object_fid(&mo->mo_lu),
572 if (rc == 0 || rc == -EREMOTE) {
573 body = req_capsule_server_get(&mci->mci_req->rq_pill,
575 LASSERT(body->valid & OBD_MD_FLID);
577 CDEBUG(D_INFO, "Remote mdo_is_subdir(), new src "DFID"\n",
581 ptlrpc_req_finished(mci->mci_req);
585 static struct md_dir_operations mdc_dir_ops = {
586 .mdo_is_subdir = mdc_is_subdir,
587 .mdo_rename_tgt = mdc_rename_tgt