1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Light Super operations
6 * Copyright (c) 2002-2004 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_LLITE
30 #include <sys/types.h>
33 #include <sys/queue.h>
35 # include <sys/statvfs.h>
37 # include <sys/statfs.h>
53 #include "llite_lib.h"
61 #define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
63 static int ll_permission(struct inode *inode, int mask)
65 struct intnl_stat *st = llu_i2stat(inode);
66 mode_t mode = st->st_mode;
68 if (current->fsuid == st->st_uid)
70 else if (in_group_p(st->st_gid))
73 if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
76 if ((mask & (MAY_READ|MAY_WRITE)) ||
77 (st->st_mode & S_IXUGO))
78 if (capable(CAP_DAC_OVERRIDE))
81 if (mask == MAY_READ ||
82 (S_ISDIR(st->st_mode) && !(mask & MAY_WRITE))) {
83 if (capable(CAP_DAC_READ_SEARCH))
90 static void llu_fsop_gone(struct filesys *fs)
92 struct llu_sb_info *sbi = (struct llu_sb_info *) fs->fs_private;
93 struct obd_device *obd = class_exp2obd(sbi->ll_mdc_exp);
97 list_del(&sbi->ll_conn_chain);
98 obd_unregister_lock_cancel_cb(sbi->ll_osc_exp,
99 llu_extent_lock_cancel_cb);
100 obd_disconnect(sbi->ll_osc_exp);
101 obd_disconnect(sbi->ll_mdc_exp);
103 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
104 class_manual_cleanup(obd);
106 OBD_FREE(sbi, sizeof(*sbi));
108 liblustre_wait_idle();
112 static struct inode_ops llu_inode_ops;
114 void llu_update_inode(struct inode *inode, struct mds_body *body,
115 struct lov_stripe_md *lsm)
117 struct llu_inode_info *lli = llu_i2info(inode);
118 struct intnl_stat *st = llu_i2stat(inode);
120 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
122 if (lli->lli_smd == NULL) {
124 lli->lli_maxbytes = lsm->lsm_maxbytes;
125 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
126 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
128 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
129 CERROR("lsm mismatch for inode %lld\n",
130 (long long)st->st_ino);
136 if (body->valid & OBD_MD_FLID)
137 st->st_ino = body->ino;
138 if (body->valid & OBD_MD_FLATIME &&
139 body->atime > LTIME_S(st->st_atime))
140 LTIME_S(st->st_atime) = body->atime;
142 /* mtime is always updated with ctime, but can be set in past.
143 As write and utime(2) may happen within 1 second, and utime's
144 mtime has a priority over write's one, so take mtime from mds
145 for the same ctimes. */
146 if (body->valid & OBD_MD_FLCTIME &&
147 body->ctime >= LTIME_S(st->st_ctime)) {
148 LTIME_S(st->st_ctime) = body->ctime;
149 if (body->valid & OBD_MD_FLMTIME)
150 LTIME_S(st->st_mtime) = body->mtime;
152 if (body->valid & OBD_MD_FLMODE)
153 st->st_mode = (st->st_mode & S_IFMT)|(body->mode & ~S_IFMT);
154 if (body->valid & OBD_MD_FLTYPE)
155 st->st_mode = (st->st_mode & ~S_IFMT)|(body->mode & S_IFMT);
156 if (S_ISREG(st->st_mode))
157 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
159 st->st_blksize = 4096;
160 if (body->valid & OBD_MD_FLUID)
161 st->st_uid = body->uid;
162 if (body->valid & OBD_MD_FLGID)
163 st->st_gid = body->gid;
164 if (body->valid & OBD_MD_FLNLINK)
165 st->st_nlink = body->nlink;
166 if (body->valid & OBD_MD_FLRDEV)
167 st->st_rdev = body->rdev;
168 if (body->valid & OBD_MD_FLSIZE)
169 st->st_size = body->size;
170 if (body->valid & OBD_MD_FLBLOCKS)
171 st->st_blocks = body->blocks;
172 if (body->valid & OBD_MD_FLFLAGS)
173 lli->lli_st_flags = body->flags;
174 if (body->valid & OBD_MD_FLGENER)
175 lli->lli_st_generation = body->generation;
178 if (body->valid & OBD_MD_FLID)
179 lli->lli_fid.id = body->ino;
180 if (body->valid & OBD_MD_FLGENER)
181 lli->lli_fid.generation = body->generation;
182 if (body->valid & OBD_MD_FLTYPE)
183 lli->lli_fid.f_type = body->mode & S_IFMT;
186 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
188 struct llu_inode_info *lli = llu_i2info(dst);
189 struct intnl_stat *st = llu_i2stat(dst);
191 valid &= src->o_valid;
193 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
194 CDEBUG(D_INODE,"valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
196 LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
197 (long)src->o_mtime, (long)src->o_ctime);
199 if (valid & OBD_MD_FLATIME)
200 LTIME_S(st->st_atime) = src->o_atime;
201 if (valid & OBD_MD_FLMTIME)
202 LTIME_S(st->st_mtime) = src->o_mtime;
203 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
204 LTIME_S(st->st_ctime) = src->o_ctime;
205 if (valid & OBD_MD_FLSIZE)
206 st->st_size = src->o_size;
207 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
208 st->st_blocks = src->o_blocks;
209 if (valid & OBD_MD_FLBLKSZ)
210 st->st_blksize = src->o_blksize;
211 if (valid & OBD_MD_FLTYPE)
212 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
213 if (valid & OBD_MD_FLMODE)
214 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
215 if (valid & OBD_MD_FLUID)
216 st->st_uid = src->o_uid;
217 if (valid & OBD_MD_FLGID)
218 st->st_gid = src->o_gid;
219 if (valid & OBD_MD_FLFLAGS)
220 lli->lli_st_flags = src->o_flags;
221 if (valid & OBD_MD_FLGENER)
222 lli->lli_st_generation = src->o_generation;
225 #define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
226 #define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
228 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
230 struct llu_inode_info *lli = llu_i2info(src);
231 struct intnl_stat *st = llu_i2stat(src);
232 obd_flag newvalid = 0;
234 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
235 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
236 valid, LTIME_S(st->st_mtime),
237 LTIME_S(st->st_ctime));
239 if (valid & OBD_MD_FLATIME) {
240 dst->o_atime = LTIME_S(st->st_atime);
241 newvalid |= OBD_MD_FLATIME;
243 if (valid & OBD_MD_FLMTIME) {
244 dst->o_mtime = LTIME_S(st->st_mtime);
245 newvalid |= OBD_MD_FLMTIME;
247 if (valid & OBD_MD_FLCTIME) {
248 dst->o_ctime = LTIME_S(st->st_ctime);
249 newvalid |= OBD_MD_FLCTIME;
251 if (valid & OBD_MD_FLSIZE) {
252 dst->o_size = st->st_size;
253 newvalid |= OBD_MD_FLSIZE;
255 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
256 dst->o_blocks = st->st_blocks;
257 newvalid |= OBD_MD_FLBLOCKS;
259 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
260 dst->o_blksize = st->st_blksize;
261 newvalid |= OBD_MD_FLBLKSZ;
263 if (valid & OBD_MD_FLTYPE) {
264 dst->o_mode = (dst->o_mode & S_IALLUGO)|(st->st_mode & S_IFMT);
265 newvalid |= OBD_MD_FLTYPE;
267 if (valid & OBD_MD_FLMODE) {
268 dst->o_mode = (dst->o_mode & S_IFMT)|(st->st_mode & S_IALLUGO);
269 newvalid |= OBD_MD_FLMODE;
271 if (valid & OBD_MD_FLUID) {
272 dst->o_uid = st->st_uid;
273 newvalid |= OBD_MD_FLUID;
275 if (valid & OBD_MD_FLGID) {
276 dst->o_gid = st->st_gid;
277 newvalid |= OBD_MD_FLGID;
279 if (valid & OBD_MD_FLFLAGS) {
280 dst->o_flags = lli->lli_st_flags;
281 newvalid |= OBD_MD_FLFLAGS;
283 if (valid & OBD_MD_FLGENER) {
284 dst->o_generation = lli->lli_st_generation;
285 newvalid |= OBD_MD_FLGENER;
287 if (valid & OBD_MD_FLFID) {
288 dst->o_fid = st->st_ino;
289 newvalid |= OBD_MD_FLFID;
292 dst->o_valid |= newvalid;
296 * really does the getattr on the inode and updates its fields
298 int llu_inode_getattr(struct inode *inode, struct lov_stripe_md *lsm)
300 struct llu_inode_info *lli = llu_i2info(inode);
301 struct obd_export *exp = llu_i2obdexp(inode);
302 struct ptlrpc_request_set *set;
303 struct obd_info oinfo = { { { 0 } } };
304 struct obdo oa = { 0 };
305 obd_flag refresh_valid;
314 oa.o_id = lsm->lsm_object_id;
316 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
317 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
320 set = ptlrpc_prep_set();
322 CERROR ("ENOMEM allocing request set\n");
325 rc = obd_getattr_async(exp, &oinfo, set);
327 rc = ptlrpc_set_wait(set);
328 ptlrpc_set_destroy(set);
333 refresh_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
334 OBD_MD_FLCTIME | OBD_MD_FLSIZE;
336 obdo_refresh_inode(inode, &oa, refresh_valid);
341 static struct inode* llu_new_inode(struct filesys *fs,
345 struct llu_inode_info *lli;
346 struct intnl_stat st = {
348 #ifndef AUTOMOUNT_FILE_NAME
349 .st_mode = fid->f_type & S_IFMT,
351 .st_mode = fid->f_type /* all of the bits! */
357 OBD_ALLOC(lli, sizeof(*lli));
361 /* initialize lli here */
362 lli->lli_sbi = llu_fs2sbi(fs);
364 lli->lli_symlink_name = NULL;
366 lli->lli_maxbytes = (__u64)(~0UL);
367 lli->lli_file_data = NULL;
369 lli->lli_sysio_fid.fid_data = &lli->lli_fid;
370 lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
373 /* file identifier is needed by functions like _sysio_i_find() */
374 inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
375 &st, 0, &llu_inode_ops, lli);
378 OBD_FREE(lli, sizeof(*lli));
383 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
385 struct llu_sb_info *sbi = llu_i2sbi(inode);
386 struct llu_inode_info *lli = llu_i2info(inode);
387 struct lustre_handle lockh;
388 struct ldlm_res_id res_id = { .name = {0} };
389 struct obd_device *obddev;
390 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
396 obddev = sbi->ll_mdc_exp->exp_obd;
397 res_id.name[0] = llu_i2stat(inode)->st_ino;
398 res_id.name[1] = lli->lli_st_generation;
400 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
402 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
403 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
404 &policy, LCK_CR|LCK_CW|LCK_PR|LCK_PW, &lockh)) {
410 static int llu_inode_revalidate(struct inode *inode)
412 struct lov_stripe_md *lsm = NULL;
416 CERROR("REPORT THIS LINE TO PETER\n");
420 if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
422 struct ptlrpc_request *req = NULL;
423 struct llu_sb_info *sbi = llu_i2sbi(inode);
425 unsigned long valid = OBD_MD_FLGETATTR;
428 /* Why don't we update all valid MDS fields here, if we're
429 * doing an RPC anyways? -phil */
430 if (S_ISREG(llu_i2stat(inode)->st_mode)) {
431 ealen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
432 valid |= OBD_MD_FLEASIZE;
434 ll_inode2fid(&fid, inode);
435 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, ealen, &req);
437 CERROR("failure %d inode %llu\n", rc,
438 (long long)llu_i2stat(inode)->st_ino);
441 rc = mdc_req2lustre_md(req, REPLY_REC_OFF, sbi->ll_osc_exp,&md);
443 /* XXX Too paranoid? */
444 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
445 !((md.body->valid & OBD_MD_FLNLINK) &&
446 (md.body->nlink == 0))) {
447 CERROR("Asked for %s eadata but got %s (%d)\n",
448 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
449 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
450 md.body->eadatasize);
453 ptlrpc_req_finished(req);
458 llu_update_inode(inode, md.body, md.lsm);
459 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
460 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
462 if (md.body->valid & OBD_MD_FLSIZE)
463 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
464 &llu_i2info(inode)->lli_flags);
465 ptlrpc_req_finished(req);
468 lsm = llu_i2info(inode)->lli_smd;
469 if (!lsm) /* object not yet allocated, don't validate size */
472 /* ll_glimpse_size will prefer locally cached writes if they extend
474 RETURN(llu_glimpse_size(inode));
477 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
479 *b = *llu_i2stat(ino);
482 static int llu_iop_getattr(struct pnode *pno,
484 struct intnl_stat *b)
489 liblustre_wait_event(0);
493 LASSERT(pno->p_base->pb_ino);
494 ino = pno->p_base->pb_ino;
496 LASSERT(!pno || pno->p_base->pb_ino == ino);
499 /* libsysio might call us directly without intent lock,
500 * we must re-fetch the attrs here
502 rc = llu_inode_revalidate(ino);
504 copy_stat_buf(ino, b);
505 LASSERT(!llu_i2info(ino)->lli_it);
508 liblustre_wait_event(0);
512 static int null_if_equal(struct ldlm_lock *lock, void *data)
514 if (data == lock->l_ast_data) {
515 lock->l_ast_data = NULL;
517 if (lock->l_req_mode != lock->l_granted_mode)
518 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
521 return LDLM_ITER_CONTINUE;
524 void llu_clear_inode(struct inode *inode)
527 struct llu_inode_info *lli = llu_i2info(inode);
528 struct llu_sb_info *sbi = llu_i2sbi(inode);
531 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
532 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
535 ll_inode2fid(&fid, inode);
536 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(lli->lli_flags));
537 mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
540 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
541 null_if_equal, inode);
544 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
548 if (lli->lli_symlink_name) {
549 OBD_FREE(lli->lli_symlink_name,
550 strlen(lli->lli_symlink_name) + 1);
551 lli->lli_symlink_name = NULL;
557 void llu_iop_gone(struct inode *inode)
559 struct llu_inode_info *lli = llu_i2info(inode);
562 liblustre_wait_event(0);
563 llu_clear_inode(inode);
565 OBD_FREE(lli, sizeof(*lli));
569 static int inode_setattr(struct inode * inode, struct iattr * attr)
571 unsigned int ia_valid = attr->ia_valid;
572 struct intnl_stat *st = llu_i2stat(inode);
576 * inode_setattr() is only ever invoked with ATTR_SIZE (by
577 * llu_setattr_raw()) when file has no bodies. Check this.
579 LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
581 if (ia_valid & ATTR_SIZE)
582 st->st_size = attr->ia_size;
583 if (ia_valid & ATTR_UID)
584 st->st_uid = attr->ia_uid;
585 if (ia_valid & ATTR_GID)
586 st->st_gid = attr->ia_gid;
587 if (ia_valid & ATTR_ATIME)
588 st->st_atime = attr->ia_atime;
589 if (ia_valid & ATTR_MTIME)
590 st->st_mtime = attr->ia_mtime;
591 if (ia_valid & ATTR_CTIME)
592 st->st_ctime = attr->ia_ctime;
593 if (ia_valid & ATTR_MODE) {
594 st->st_mode = attr->ia_mode;
595 if (!in_group_p(st->st_gid) && !capable(CAP_FSETID))
596 st->st_mode &= ~S_ISGID;
598 /* mark_inode_dirty(inode); */
602 /* If this inode has objects allocated to it (lsm != NULL), then the OST
603 * object(s) determine the file size and mtime. Otherwise, the MDS will
604 * keep these values until such a time that objects are allocated for it.
605 * We do the MDS operations first, as it is checking permissions for us.
606 * We don't to the MDS RPC if there is nothing that we want to store there,
607 * otherwise there is no harm in updating mtime/atime on the MDS if we are
608 * going to do an RPC anyways.
610 * If we are doing a truncate, we will send the mtime and ctime updates
611 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
612 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
615 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
617 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
618 struct llu_sb_info *sbi = llu_i2sbi(inode);
619 struct intnl_stat *st = llu_i2stat(inode);
620 struct ptlrpc_request *request = NULL;
621 struct mdc_op_data op_data;
622 int ia_valid = attr->ia_valid;
626 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
628 if (ia_valid & ATTR_SIZE) {
629 if (attr->ia_size > ll_file_maxbytes(inode)) {
630 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
631 (long long)attr->ia_size,
632 ll_file_maxbytes(inode));
636 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
639 /* We mark all of the fields "set" so MDS/OST does not re-set them */
640 if (attr->ia_valid & ATTR_CTIME) {
641 attr->ia_ctime = CURRENT_TIME;
642 attr->ia_valid |= ATTR_CTIME_SET;
644 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
645 attr->ia_atime = CURRENT_TIME;
646 attr->ia_valid |= ATTR_ATIME_SET;
648 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
649 attr->ia_mtime = CURRENT_TIME;
650 attr->ia_valid |= ATTR_MTIME_SET;
652 if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
653 /* To avoid stale mtime on mds, obtain it from ost and send
655 rc = llu_glimpse_size(inode);
659 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
660 attr->ia_mtime = inode->i_stbuf.st_mtime;
663 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
664 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
665 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
666 LTIME_S(CURRENT_TIME));
668 attr->ia_valid &= ~ATTR_SIZE;
670 /* If only OST attributes being set on objects, don't do MDS RPC.
671 * In that case, we need to check permissions and update the local
672 * inode ourselves so we can call obdo_from_inode() always. */
673 if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
675 llu_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
677 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
678 attr, NULL, 0, NULL, 0, &request);
681 ptlrpc_req_finished(request);
682 if (rc != -EPERM && rc != -EACCES)
683 CERROR("mdc_setattr fails: rc = %d\n", rc);
687 rc = mdc_req2lustre_md(request, REPLY_REC_OFF, sbi->ll_osc_exp,
690 ptlrpc_req_finished(request);
694 /* We call inode_setattr to adjust timestamps.
695 * If there is at least some data in file, we cleared ATTR_SIZE
696 * above to avoid invoking vmtruncate, otherwise it is important
697 * to call vmtruncate in inode_setattr to update inode->i_size
699 inode_setattr(inode, attr);
700 llu_update_inode(inode, md.body, md.lsm);
701 ptlrpc_req_finished(request);
703 if (!lsm || !S_ISREG(st->st_mode)) {
704 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
708 /* The OST doesn't check permissions, but the alternative is
709 * a gratuitous RPC to the MDS. We already rely on the client
710 * to do read/write/truncate permission checks, so is mtime OK?
712 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
713 /* from sys_utime() */
714 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
715 if (current->fsuid != st->st_uid &&
716 (rc = ll_permission(inode, MAY_WRITE)) != 0)
719 /* from inode_change_ok() */
720 if (current->fsuid != st->st_uid &&
721 !capable(CAP_FOWNER))
726 /* Won't invoke llu_vmtruncate(), as we already cleared
728 inode_setattr(inode, attr);
731 if (ia_valid & ATTR_SIZE) {
732 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
734 struct lustre_handle lockh = { 0, };
735 struct lustre_handle match_lockh = { 0, };
738 int flags = LDLM_FL_TEST_LOCK; /* for assertion check below */
742 /* check that there are no matching locks */
743 LASSERT(obd_match(sbi->ll_osc_exp, lsm, LDLM_EXTENT, &policy,
744 LCK_PW, &flags, inode, &match_lockh) <= 0);
746 /* XXX when we fix the AST intents to pass the discard-range
747 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
749 flags = (attr->ia_size == 0) ? LDLM_AST_DISCARD_DATA : 0;
751 if (sbi->ll_lco.lco_flags & OBD_CONNECT_TRUNCLOCK) {
753 obd_flags = OBD_FL_TRUNCLOCK;
754 CDEBUG(D_INODE, "delegating locking to the OST");
760 /* with lock_mode == LK_NL no lock is taken. */
761 rc = llu_extent_lock(NULL, inode, lsm, lock_mode, &policy,
763 if (rc != ELDLM_OK) {
769 rc = llu_vmtruncate(inode, attr->ia_size, obd_flags);
771 /* unlock now as we don't mind others file lockers racing with
772 * the mds updates below? */
773 err = llu_extent_unlock(NULL, inode, lsm, lock_mode, &lockh);
775 CERROR("llu_extent_unlock failed: %d\n", err);
779 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
780 struct obd_info oinfo = { { { 0 } } };
783 CDEBUG(D_INODE, "set mtime on OST inode %llu to %lu\n",
784 (long long)st->st_ino, LTIME_S(attr->ia_mtime));
785 oa.o_id = lsm->lsm_object_id;
786 oa.o_valid = OBD_MD_FLID;
788 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
789 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
794 rc = obd_setattr_rqset(sbi->ll_osc_exp, &oinfo, NULL);
796 CERROR("obd_setattr_async fails: rc=%d\n", rc);
801 /* here we simply act as a thin layer to glue it with
802 * llu_setattr_raw(), which is copy from kernel
804 static int llu_iop_setattr(struct pnode *pno,
807 struct intnl_stat *stbuf)
813 liblustre_wait_event(0);
815 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
816 SETATTR_UID | SETATTR_GID |
817 SETATTR_LEN | SETATTR_MODE)));
818 memset(&iattr, 0, sizeof(iattr));
820 if (mask & SETATTR_MODE) {
821 iattr.ia_mode = stbuf->st_mode;
822 iattr.ia_valid |= ATTR_MODE;
824 if (mask & SETATTR_MTIME) {
825 iattr.ia_mtime = stbuf->st_mtime;
826 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
828 if (mask & SETATTR_ATIME) {
829 iattr.ia_atime = stbuf->st_atime;
830 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
832 if (mask & SETATTR_UID) {
833 iattr.ia_uid = stbuf->st_uid;
834 iattr.ia_valid |= ATTR_UID;
836 if (mask & SETATTR_GID) {
837 iattr.ia_gid = stbuf->st_gid;
838 iattr.ia_valid |= ATTR_GID;
840 if (mask & SETATTR_LEN) {
841 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
842 iattr.ia_valid |= ATTR_SIZE;
845 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
846 iattr.ia_ctime = CURRENT_TIME;
848 rc = llu_setattr_raw(ino, &iattr);
849 liblustre_wait_idle();
853 #define EXT2_LINK_MAX 32000
855 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
857 struct inode *dir = pno->p_base->pb_parent->pb_ino;
858 struct qstr *qstr = &pno->p_base->pb_name;
859 const char *name = qstr->name;
861 struct ptlrpc_request *request = NULL;
862 struct llu_sb_info *sbi = llu_i2sbi(dir);
863 struct mdc_op_data op_data;
867 liblustre_wait_event(0);
868 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
871 llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
872 err = mdc_create(sbi->ll_mdc_exp, &op_data,
873 tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
874 current->fsuid, current->fsgid, current->cap_effective,
876 ptlrpc_req_finished(request);
877 liblustre_wait_event(0);
881 static int llu_readlink_internal(struct inode *inode,
882 struct ptlrpc_request **request,
885 struct llu_inode_info *lli = llu_i2info(inode);
886 struct llu_sb_info *sbi = llu_i2sbi(inode);
888 struct mds_body *body;
889 struct intnl_stat *st = llu_i2stat(inode);
890 int rc, symlen = st->st_size + 1;
895 if (lli->lli_symlink_name) {
896 *symname = lli->lli_symlink_name;
897 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
901 ll_inode2fid(&fid, inode);
902 rc = mdc_getattr(sbi->ll_mdc_exp, &fid,
903 OBD_MD_LINKNAME, symlen, request);
905 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
909 body = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF,
911 LASSERT(body != NULL);
912 LASSERT(lustre_rep_swabbed(*request, REPLY_REC_OFF));
914 if ((body->valid & OBD_MD_LINKNAME) == 0) {
915 CERROR ("OBD_MD_LINKNAME not set on reply\n");
916 GOTO (failed, rc = -EPROTO);
919 LASSERT(symlen != 0);
920 if (body->eadatasize != symlen) {
921 CERROR("inode %llu: symlink length %d not expected %d\n",
922 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
923 GOTO(failed, rc = -EPROTO);
926 *symname = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF + 1,
928 if (*symname == NULL ||
929 strnlen(*symname, symlen) != symlen - 1) {
930 /* not full/NULL terminated */
931 CERROR("inode %llu: symlink not NULL terminated string"
932 "of length %d\n", (long long)st->st_ino, symlen - 1);
933 GOTO(failed, rc = -EPROTO);
936 OBD_ALLOC(lli->lli_symlink_name, symlen);
937 /* do not return an error if we cannot cache the symlink locally */
938 if (lli->lli_symlink_name)
939 memcpy(lli->lli_symlink_name, *symname, symlen);
944 ptlrpc_req_finished (*request);
948 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
950 struct inode *inode = pno->p_base->pb_ino;
951 struct ptlrpc_request *request;
956 liblustre_wait_event(0);
957 rc = llu_readlink_internal(inode, &request, &symname);
962 strncpy(data, symname, bufsize);
963 rc = strlen(symname);
965 ptlrpc_req_finished(request);
967 liblustre_wait_event(0);
971 static int llu_iop_mknod_raw(struct pnode *pno,
975 struct ptlrpc_request *request = NULL;
976 struct inode *dir = pno->p_parent->p_base->pb_ino;
977 struct llu_sb_info *sbi = llu_i2sbi(dir);
978 struct mdc_op_data op_data;
982 liblustre_wait_event(0);
983 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
984 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
985 (long long)llu_i2stat(dir)->st_ino);
987 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
990 switch (mode & S_IFMT) {
993 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
998 llu_prepare_mdc_op_data(&op_data, dir, NULL,
999 pno->p_base->pb_name.name,
1000 pno->p_base->pb_name.len,
1002 err = mdc_create(sbi->ll_mdc_exp, &op_data, NULL, 0, mode,
1003 current->fsuid, current->fsgid,
1004 current->cap_effective, dev, &request);
1005 ptlrpc_req_finished(request);
1013 liblustre_wait_event(0);
1017 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1019 struct inode *src = old->p_base->pb_ino;
1020 struct inode *dir = new->p_parent->p_base->pb_ino;
1021 const char *name = new->p_base->pb_name.name;
1022 int namelen = new->p_base->pb_name.len;
1023 struct ptlrpc_request *request = NULL;
1024 struct mdc_op_data op_data;
1031 liblustre_wait_event(0);
1032 llu_prepare_mdc_op_data(&op_data, src, dir, name, namelen, 0);
1033 rc = mdc_link(llu_i2sbi(src)->ll_mdc_exp, &op_data, &request);
1034 ptlrpc_req_finished(request);
1035 liblustre_wait_event(0);
1041 * libsysio will clear the inode immediately after return
1043 static int llu_iop_unlink_raw(struct pnode *pno)
1045 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1046 struct qstr *qstr = &pno->p_base->pb_name;
1047 const char *name = qstr->name;
1048 int len = qstr->len;
1049 struct inode *target = pno->p_base->pb_ino;
1050 struct ptlrpc_request *request = NULL;
1051 struct mdc_op_data op_data;
1057 liblustre_wait_event(0);
1058 llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
1059 rc = mdc_unlink(llu_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1061 rc = llu_objects_destroy(request, dir);
1062 ptlrpc_req_finished(request);
1063 liblustre_wait_idle();
1068 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1070 struct inode *src = old->p_parent->p_base->pb_ino;
1071 struct inode *tgt = new->p_parent->p_base->pb_ino;
1072 const char *oldname = old->p_base->pb_name.name;
1073 int oldnamelen = old->p_base->pb_name.len;
1074 const char *newname = new->p_base->pb_name.name;
1075 int newnamelen = new->p_base->pb_name.len;
1076 struct ptlrpc_request *request = NULL;
1077 struct mdc_op_data op_data;
1084 liblustre_wait_event(0);
1085 llu_prepare_mdc_op_data(&op_data, src, tgt, NULL, 0, 0);
1086 rc = mdc_rename(llu_i2sbi(src)->ll_mdc_exp, &op_data,
1087 oldname, oldnamelen, newname, newnamelen,
1090 rc = llu_objects_destroy(request, src);
1093 ptlrpc_req_finished(request);
1094 liblustre_wait_idle();
1099 #ifdef _HAVE_STATVFS
1100 static int llu_statfs_internal(struct llu_sb_info *sbi,
1101 struct obd_statfs *osfs, __u64 max_age)
1103 struct obd_statfs obd_osfs;
1107 rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age, 0);
1109 CERROR("mdc_statfs fails: rc = %d\n", rc);
1113 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1114 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1116 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_osc_exp),
1117 &obd_osfs, max_age, 0);
1119 CERROR("obd_statfs fails: rc = %d\n", rc);
1123 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1124 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1127 osfs->os_blocks = obd_osfs.os_blocks;
1128 osfs->os_bfree = obd_osfs.os_bfree;
1129 osfs->os_bavail = obd_osfs.os_bavail;
1131 /* If we don't have as many objects free on the OST as inodes
1132 * on the MDS, we reduce the total number of inodes to
1133 * compensate, so that the "inodes in use" number is correct.
1135 if (obd_osfs.os_ffree < osfs->os_ffree) {
1136 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1138 osfs->os_ffree = obd_osfs.os_ffree;
1144 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1146 struct obd_statfs osfs;
1149 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1151 /* For now we will always get up-to-date statfs values, but in the
1152 * future we may allow some amount of caching on the client (e.g.
1153 * from QOS or lprocfs updates). */
1154 rc = llu_statfs_internal(sbi, &osfs, cfs_time_current_64() - HZ);
1158 statfs_unpack(sfs, &osfs);
1160 if (sizeof(sfs->f_blocks) == 4) {
1161 while (osfs.os_blocks > ~0UL) {
1164 osfs.os_blocks >>= 1;
1165 osfs.os_bfree >>= 1;
1166 osfs.os_bavail >>= 1;
1170 sfs->f_blocks = osfs.os_blocks;
1171 sfs->f_bfree = osfs.os_bfree;
1172 sfs->f_bavail = osfs.os_bavail;
1177 static int llu_iop_statvfs(struct pnode *pno,
1179 struct intnl_statvfs *buf)
1185 liblustre_wait_event(0);
1188 LASSERT(pno->p_base->pb_ino);
1189 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1193 /* from native driver */
1194 buf->f_bsize = fs.f_bsize; /* file system block size */
1195 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1196 buf->f_blocks = fs.f_blocks;
1197 buf->f_bfree = fs.f_bfree;
1198 buf->f_bavail = fs.f_bavail;
1199 buf->f_files = fs.f_files; /* Total number serial numbers */
1200 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1201 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1202 buf->f_fsid = fs.f_fsid.__val[1];
1203 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1204 buf->f_namemax = fs.f_namelen;
1207 liblustre_wait_event(0);
1210 #endif /* _HAVE_STATVFS */
1212 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1214 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1215 struct qstr *qstr = &pno->p_base->pb_name;
1216 const char *name = qstr->name;
1217 int len = qstr->len;
1218 struct ptlrpc_request *request = NULL;
1219 struct intnl_stat *st = llu_i2stat(dir);
1220 struct mdc_op_data op_data;
1224 liblustre_wait_event(0);
1225 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1226 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1228 if (st->st_nlink >= EXT2_LINK_MAX)
1231 llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
1232 err = mdc_create(llu_i2sbi(dir)->ll_mdc_exp, &op_data, NULL, 0, mode | S_IFDIR,
1233 current->fsuid, current->fsgid, current->cap_effective,
1235 ptlrpc_req_finished(request);
1236 liblustre_wait_event(0);
1240 static int llu_iop_rmdir_raw(struct pnode *pno)
1242 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1243 struct qstr *qstr = &pno->p_base->pb_name;
1244 const char *name = qstr->name;
1245 int len = qstr->len;
1246 struct ptlrpc_request *request = NULL;
1247 struct mdc_op_data op_data;
1251 liblustre_wait_event(0);
1252 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1253 (long long)llu_i2stat(dir)->st_ino,
1254 llu_i2info(dir)->lli_st_generation, dir);
1256 llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, S_IFDIR);
1257 rc = mdc_unlink(llu_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1258 ptlrpc_req_finished(request);
1260 liblustre_wait_event(0);
1265 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1267 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1269 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1271 /* refer to ll_file_flock() for details */
1272 int llu_file_flock(struct inode *ino, int cmd, struct file_lock *file_lock)
1274 struct llu_inode_info *lli = llu_i2info(ino);
1275 struct intnl_stat *st = llu_i2stat(ino);
1276 struct ldlm_res_id res_id =
1277 { .name = {st->st_ino, lli->lli_st_generation, LDLM_FLOCK} };
1278 struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1279 ldlm_flock_completion_ast, NULL, file_lock };
1280 struct lustre_handle lockh = {0};
1281 ldlm_policy_data_t flock;
1285 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1286 (unsigned long long) st->st_ino, file_lock);
1288 flock.l_flock.pid = file_lock->fl_pid;
1289 flock.l_flock.start = file_lock->fl_start;
1290 flock.l_flock.end = file_lock->fl_end;
1292 switch (file_lock->fl_type) {
1294 einfo.ei_mode = LCK_PR;
1297 einfo.ei_mode = LCK_NL;
1300 einfo.ei_mode = LCK_PW;
1303 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1310 #if F_SETLKW64 != F_SETLKW
1318 #if F_SETLK64 != F_SETLK
1322 flags = LDLM_FL_BLOCK_NOWAIT;
1326 #if F_GETLK64 != F_GETLK
1330 flags = LDLM_FL_TEST_LOCK;
1331 file_lock->fl_type = einfo.ei_mode;
1334 CERROR("unknown fcntl cmd: %d\n", cmd);
1338 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, flags=%#x, mode=%u, "
1339 "start="LPU64", end="LPU64"\n",
1340 (unsigned long long) st->st_ino, flock.l_flock.pid,
1341 flags, einfo.ei_mode, flock.l_flock.start, flock.l_flock.end);
1343 rc = ldlm_cli_enqueue(llu_i2mdcexp(ino), NULL, &einfo, res_id,
1344 &flock, &flags, NULL, 0, NULL, &lockh, 0);
1349 static int assign_type(struct file_lock *fl, int type)
1362 static int flock_to_posix_lock(struct inode *ino,
1363 struct file_lock *fl,
1366 switch (l->l_whence) {
1367 /* XXX: only SEEK_SET is supported in lustre */
1375 fl->fl_end = l->l_len - 1;
1379 fl->fl_end = OFFSET_MAX;
1381 fl->fl_pid = getpid();
1382 fl->fl_flags = FL_POSIX;
1383 fl->fl_notify = NULL;
1384 fl->fl_insert = NULL;
1385 fl->fl_remove = NULL;
1386 /* XXX: these fields can't be filled with suitable values,
1387 but I think lustre doesn't use them.
1389 fl->fl_owner = NULL;
1392 return assign_type(fl, l->l_type);
1395 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1397 struct file_lock fl;
1401 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1404 error = flock_to_posix_lock(ino, &fl, flock);
1408 error = llu_file_flock(ino, F_GETLK, &fl);
1412 flock->l_type = F_UNLCK;
1413 if (fl.fl_type != F_UNLCK) {
1414 flock->l_pid = fl.fl_pid;
1415 flock->l_start = fl.fl_start;
1416 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1417 fl.fl_end - fl.fl_start + 1;
1418 flock->l_whence = SEEK_SET;
1419 flock->l_type = fl.fl_type;
1426 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1428 struct file_lock fl;
1429 int flags = llu_i2info(ino)->lli_open_flags + 1;
1432 error = flock_to_posix_lock(ino, &fl, flock);
1435 if (cmd == F_SETLKW)
1436 fl.fl_flags |= FL_SLEEP;
1439 switch (flock->l_type) {
1441 if (!(flags & FMODE_READ))
1445 if (!(flags & FMODE_WRITE))
1455 error = llu_file_flock(ino, cmd, &fl);
1463 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1465 struct llu_inode_info *lli = llu_i2info(ino);
1467 struct flock *flock;
1470 liblustre_wait_event(0);
1473 *rtn = lli->lli_open_flags;
1476 flags = va_arg(ap, long);
1477 flags &= FCNTL_FLMASK;
1478 if (flags & FCNTL_FLMASK_INVALID) {
1479 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1480 "the O_NONBLOCK or O_ASYNC flags. "
1481 "Please fix your application.\n");
1486 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1487 (lli->lli_open_flags & ~FCNTL_FLMASK);
1492 #if F_GETLK64 != F_GETLK
1496 flock = va_arg(ap, struct flock *);
1497 err = llu_fcntl_getlk(ino, flock);
1502 #if F_SETLKW64 != F_SETLKW
1508 #if F_SETLK64 != F_SETLK
1512 flock = va_arg(ap, struct flock *);
1513 err = llu_fcntl_setlk(ino, cmd, flock);
1517 CERROR("unsupported fcntl cmd %x\n", cmd);
1523 liblustre_wait_event(0);
1527 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1529 struct llu_inode_info *lli = llu_i2info(inode);
1530 struct ll_file_data *fd = lli->lli_file_data;
1531 ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1532 .end = OBD_OBJECT_EOF}};
1533 struct lustre_handle lockh = { 0 };
1534 struct lov_stripe_md *lsm = lli->lli_smd;
1539 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1543 policy.l_extent.gid = arg;
1544 if (lli->lli_open_flags & O_NONBLOCK)
1545 flags = LDLM_FL_BLOCK_NOWAIT;
1547 err = llu_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh,
1552 fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1554 memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1559 static int llu_put_grouplock(struct inode *inode, unsigned long arg)
1561 struct llu_inode_info *lli = llu_i2info(inode);
1562 struct ll_file_data *fd = lli->lli_file_data;
1563 struct lov_stripe_md *lsm = lli->lli_smd;
1567 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1570 if (fd->fd_gid != arg)
1573 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1575 err = llu_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1580 memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1585 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1587 struct llu_sb_info *sbi = llu_i2sbi(ino);
1588 struct ptlrpc_request *request = NULL;
1589 struct mdc_op_data op_data;
1590 struct iattr attr = { 0 };
1591 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1594 llu_prepare_mdc_op_data(&op_data, ino, NULL, NULL, 0, 0);
1596 LASSERT(sizeof(lum) == sizeof(*lump));
1597 LASSERT(sizeof(lum.lmm_objects[0]) ==
1598 sizeof(lump->lmm_objects[0]));
1599 rc = copy_from_user(&lum, lump, sizeof(lum));
1603 if (lum.lmm_magic != LOV_USER_MAGIC)
1606 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
1607 lustre_swab_lov_user_md(&lum);
1609 /* swabbing is done in lov_setstripe() on server side */
1610 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
1611 &attr, &lum, sizeof(lum), NULL, 0, &request);
1613 ptlrpc_req_finished(request);
1614 if (rc != -EPERM && rc != -EACCES)
1615 CERROR("mdc_setattr fails: rc = %d\n", rc);
1618 ptlrpc_req_finished(request);
1623 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1624 struct lov_user_md *lum, int lum_size)
1626 struct llu_sb_info *sbi = llu_i2sbi(ino);
1627 struct obd_export *exp = llu_i2obdexp(ino);
1628 struct llu_inode_info *lli = llu_i2info(ino);
1629 struct llu_inode_info *lli2 = NULL;
1630 struct lov_stripe_md *lsm;
1631 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1632 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1633 llu_mdc_blocking_ast, ldlm_completion_ast, NULL, NULL };
1635 struct ptlrpc_request *req = NULL;
1636 struct lustre_md md;
1637 struct mdc_op_data data;
1638 struct lustre_handle lockh;
1644 CDEBUG(D_IOCTL, "stripe already exists for ino "LPU64"\n",
1649 OBD_ALLOC(lli2, sizeof(struct llu_inode_info));
1653 memcpy(lli2, lli, sizeof(struct llu_inode_info));
1654 lli2->lli_open_count = 0;
1655 lli2->lli_it = NULL;
1656 lli2->lli_file_data = NULL;
1657 lli2->lli_smd = NULL;
1658 lli2->lli_symlink_name = NULL;
1659 ino->i_private = lli2;
1661 llu_prepare_mdc_op_data(&data, NULL, ino, NULL, 0, O_RDWR);
1663 rc = mdc_enqueue(sbi->ll_mdc_exp, &einfo, &oit, &data,
1664 &lockh, lum, lum_size, LDLM_FL_INTENT_ONLY);
1668 req = oit.d.lustre.it_data;
1669 rc = it_open_error(DISP_IT_EXECD, &oit);
1675 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1681 rc = mdc_req2lustre_md(req, DLM_REPLY_REC_OFF, exp, &md);
1685 llu_update_inode(ino, md.body, md.lsm);
1686 lli->lli_smd = lli2->lli_smd;
1687 lli2->lli_smd = NULL;
1689 llu_local_open(lli2, &oit);
1691 /* release intent */
1692 if (lustre_handle_is_used(&lockh))
1693 ldlm_lock_decref(&lockh, LCK_CR);
1695 ptlrpc_req_finished(req);
1698 rc = llu_file_release(ino);
1700 ino->i_private = lli;
1702 OBD_FREE(lli2, sizeof(struct llu_inode_info));
1704 ptlrpc_req_finished(req);
1708 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1710 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1712 int flags = FMODE_WRITE;
1715 LASSERT(sizeof(lum) == sizeof(*lump));
1716 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1717 rc = copy_from_user(&lum, lump, sizeof(lum));
1721 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1725 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1727 struct intnl_stat *st = llu_i2stat(ino);
1728 if (S_ISREG(st->st_mode))
1729 return llu_lov_file_setstripe(ino, arg);
1730 if (S_ISDIR(st->st_mode))
1731 return llu_lov_dir_setstripe(ino, arg);
1736 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1738 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1743 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1747 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1753 liblustre_wait_event(0);
1756 case LL_IOC_GROUP_LOCK:
1757 arg = va_arg(ap, unsigned long);
1758 rc = llu_get_grouplock(ino, arg);
1760 case LL_IOC_GROUP_UNLOCK:
1761 arg = va_arg(ap, unsigned long);
1762 rc = llu_put_grouplock(ino, arg);
1764 case LL_IOC_LOV_SETSTRIPE:
1765 arg = va_arg(ap, unsigned long);
1766 rc = llu_lov_setstripe(ino, arg);
1768 case LL_IOC_LOV_GETSTRIPE:
1769 arg = va_arg(ap, unsigned long);
1770 rc = llu_lov_getstripe(ino, arg);
1773 CERROR("did not support ioctl cmd %lx\n", request);
1778 liblustre_wait_event(0);
1783 * we already do syncronous read/write
1785 static int llu_iop_sync(struct inode *inode)
1787 liblustre_wait_event(0);
1791 static int llu_iop_datasync(struct inode *inode)
1793 liblustre_wait_event(0);
1797 struct filesys_ops llu_filesys_ops =
1799 fsop_gone: llu_fsop_gone,
1802 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1804 struct inode *inode;
1806 struct file_identifier fileid = {&fid, sizeof(fid)};
1808 if ((md->body->valid &
1809 (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1810 (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) {
1811 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1813 return ERR_PTR(-EPERM);
1816 /* try to find existing inode */
1817 fid.id = md->body->ino;
1818 fid.generation = md->body->generation;
1819 fid.f_type = md->body->mode & S_IFMT;
1821 inode = _sysio_i_find(fs, &fileid);
1823 struct llu_inode_info *lli = llu_i2info(inode);
1825 if (inode->i_zombie ||
1826 lli->lli_st_generation != md->body->generation) {
1830 llu_update_inode(inode, md->body, md->lsm);
1835 inode = llu_new_inode(fs, &fid);
1837 llu_update_inode(inode, md->body, md->lsm);
1842 extern struct list_head lustre_profile_list;
1845 llu_fsswop_mount(const char *source,
1847 const void *data __IS_UNUSED,
1848 struct pnode *tocover,
1849 struct mount **mntp)
1853 struct pnode_base *rootpb;
1854 struct obd_device *obd;
1855 struct ll_fid rootfid;
1856 struct llu_sb_info *sbi;
1857 struct obd_statfs osfs;
1858 static struct qstr noname = { NULL, 0, 0 };
1859 struct ptlrpc_request *request = NULL;
1860 struct lustre_handle mdc_conn = {0, };
1861 struct lustre_handle osc_conn = {0, };
1862 struct lustre_md md;
1864 struct config_llog_instance cfg = {0, };
1865 char ll_instance[sizeof(sbi) * 2 + 1];
1866 struct lustre_profile *lprof;
1867 char *zconf_mgsnid, *zconf_profile;
1868 char *osc = NULL, *mdc = NULL;
1869 int async = 1, err = -EINVAL;
1870 struct obd_connect_data ocd = {0,};
1874 if (ll_parse_mount_target(source,
1877 CERROR("mal-formed target %s\n", source);
1880 if (!zconf_mgsnid || !zconf_profile) {
1881 printf("Liblustre: invalid target %s\n", source);
1884 /* allocate & initialize sbi */
1885 OBD_ALLOC(sbi, sizeof(*sbi));
1889 CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
1890 ll_generate_random_uuid(uuid);
1891 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1893 /* generate a string unique to this super, let's try
1894 the address of the super itself.*/
1895 sprintf(ll_instance, "%p", sbi);
1897 /* retrive & parse config log */
1898 cfg.cfg_instance = ll_instance;
1899 cfg.cfg_uuid = sbi->ll_sb_uuid;
1900 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
1902 CERROR("Unable to process log: %s\n", zconf_profile);
1903 GOTO(out_free, err);
1906 lprof = class_get_profile(zconf_profile);
1907 if (lprof == NULL) {
1908 CERROR("No profile found: %s\n", zconf_profile);
1909 GOTO(out_free, err = -EINVAL);
1911 OBD_ALLOC(osc, strlen(lprof->lp_osc) + strlen(ll_instance) + 2);
1912 sprintf(osc, "%s-%s", lprof->lp_osc, ll_instance);
1914 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) + strlen(ll_instance) + 2);
1915 sprintf(mdc, "%s-%s", lprof->lp_mdc, ll_instance);
1919 GOTO(out_free, err = -EINVAL);
1923 GOTO(out_free, err = -EINVAL);
1926 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
1932 obd = class_name2obd(mdc);
1934 CERROR("MDC %s: not setup or attached\n", mdc);
1935 GOTO(out_free, err = -EINVAL);
1937 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
1938 sizeof(async), &async, NULL);
1940 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
1942 #ifdef LIBLUSTRE_POSIX_ACL
1943 ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
1945 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
1946 ocd.ocd_version = LUSTRE_VERSION_CODE;
1949 err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid, &ocd, NULL);
1951 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
1952 GOTO(out_free, err);
1954 sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
1956 err = obd_statfs(obd, &osfs, 100000000, 0);
1961 * FIXME fill fs stat data into sbi here!!! FIXME
1965 obd = class_name2obd(osc);
1967 CERROR("OSC %s: not setup or attached\n", osc);
1968 GOTO(out_mdc, err = -EINVAL);
1970 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
1971 sizeof(async), &async, NULL);
1973 obd->obd_upcall.onu_owner = &sbi->ll_lco;
1974 obd->obd_upcall.onu_upcall = ll_ocd_update;
1976 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
1977 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK | OBD_CONNECT_AT;
1978 ocd.ocd_version = LUSTRE_VERSION_CODE;
1979 err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid, &ocd, NULL);
1981 CERROR("cannot connect to %s: rc = %d\n", osc, err);
1984 sbi->ll_osc_exp = class_conn2export(&osc_conn);
1985 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
1987 err = obd_register_lock_cancel_cb(sbi->ll_osc_exp,
1988 llu_extent_lock_cancel_cb);
1990 CERROR("cannot register lock cancel callback: rc = %d\n", err);
1994 mdc_init_ea_size(sbi->ll_mdc_exp, sbi->ll_osc_exp);
1996 err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
1998 CERROR("cannot mds_connect: rc = %d\n", err);
1999 GOTO(out_lock_cn_cb, err);
2001 CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
2002 sbi->ll_rootino = rootfid.id;
2004 /* fetch attr of root inode */
2005 err = mdc_getattr(sbi->ll_mdc_exp, &rootfid,
2006 OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0,
2009 CERROR("mdc_getattr failed for root: rc = %d\n", err);
2010 GOTO(out_lock_cn_cb, err);
2013 err = mdc_req2lustre_md(request, REPLY_REC_OFF, sbi->ll_osc_exp, &md);
2015 CERROR("failed to understand root inode md: rc = %d\n",err);
2016 GOTO(out_request, err);
2019 LASSERT(sbi->ll_rootino != 0);
2021 root = llu_iget(fs, &md);
2022 if (!root || IS_ERR(root)) {
2023 CERROR("fail to generate root inode\n");
2024 GOTO(out_request, err = -EBADF);
2028 * Generate base path-node for root.
2030 rootpb = _sysio_pb_new(&noname, NULL, root);
2036 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2038 _sysio_pb_gone(rootpb);
2042 ptlrpc_req_finished(request);
2044 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2045 liblustre_wait_idle();
2050 _sysio_i_gone(root);
2052 ptlrpc_req_finished(request);
2054 obd_unregister_lock_cancel_cb(sbi->ll_osc_exp,
2055 llu_extent_lock_cancel_cb);
2057 obd_disconnect(sbi->ll_osc_exp);
2059 obd_disconnect(sbi->ll_mdc_exp);
2062 OBD_FREE(osc, strlen(osc) + 1);
2064 OBD_FREE(mdc, strlen(mdc) + 1);
2065 OBD_FREE(sbi, sizeof(*sbi));
2067 liblustre_wait_idle();
2071 struct fssw_ops llu_fssw_ops = {
2075 static struct inode_ops llu_inode_ops = {
2076 inop_lookup: llu_iop_lookup,
2077 inop_getattr: llu_iop_getattr,
2078 inop_setattr: llu_iop_setattr,
2079 inop_filldirentries: llu_iop_filldirentries,
2080 inop_mkdir: llu_iop_mkdir_raw,
2081 inop_rmdir: llu_iop_rmdir_raw,
2082 inop_symlink: llu_iop_symlink_raw,
2083 inop_readlink: llu_iop_readlink,
2084 inop_open: llu_iop_open,
2085 inop_close: llu_iop_close,
2086 inop_link: llu_iop_link_raw,
2087 inop_unlink: llu_iop_unlink_raw,
2088 inop_rename: llu_iop_rename_raw,
2089 inop_pos: llu_iop_pos,
2090 inop_read: llu_iop_read,
2091 inop_write: llu_iop_write,
2092 inop_iodone: llu_iop_iodone,
2093 inop_fcntl: llu_iop_fcntl,
2094 inop_sync: llu_iop_sync,
2095 inop_datasync: llu_iop_datasync,
2096 inop_ioctl: llu_iop_ioctl,
2097 inop_mknod: llu_iop_mknod_raw,
2098 #ifdef _HAVE_STATVFS
2099 inop_statvfs: llu_iop_statvfs,
2101 inop_gone: llu_iop_gone,