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/liblustre/super.c
38 * Lustre Light Super operations
41 #define DEBUG_SUBSYSTEM S_LLITE
47 #include <sys/types.h>
50 #include <sys/queue.h>
52 # include <sys/statvfs.h>
54 # include <sys/statfs.h>
70 #include "llite_lib.h"
78 #define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
80 static int ll_permission(struct inode *inode, int mask)
82 struct intnl_stat *st = llu_i2stat(inode);
83 mode_t mode = st->st_mode;
85 if (current->fsuid == st->st_uid)
87 else if (in_group_p(st->st_gid))
90 if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
93 if ((mask & (MAY_READ|MAY_WRITE)) ||
94 (st->st_mode & S_IXUGO))
95 if (cfs_capable(CFS_CAP_DAC_OVERRIDE))
98 if (mask == MAY_READ ||
99 (S_ISDIR(st->st_mode) && !(mask & MAY_WRITE))) {
100 if (cfs_capable(CFS_CAP_DAC_READ_SEARCH))
107 static void llu_fsop_gone(struct filesys *fs)
109 struct llu_sb_info *sbi = (struct llu_sb_info *) fs->fs_private;
110 struct obd_device *obd = class_exp2obd(sbi->ll_mdc_exp);
111 struct obd_device *lov_obd = class_exp2obd(sbi->ll_osc_exp);
115 list_del(&sbi->ll_conn_chain);
117 obd_disconnect(sbi->ll_osc_exp);
118 obd_unregister_lock_cancel_cb(lov_obd, llu_extent_lock_cancel_cb);
120 obd_disconnect(sbi->ll_mdc_exp);
122 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
123 class_manual_cleanup(obd);
125 OBD_FREE(sbi, sizeof(*sbi));
127 liblustre_wait_idle();
131 static struct inode_ops llu_inode_ops;
133 void llu_update_inode(struct inode *inode, struct mds_body *body,
134 struct lov_stripe_md *lsm)
136 struct llu_inode_info *lli = llu_i2info(inode);
137 struct intnl_stat *st = llu_i2stat(inode);
139 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
141 if (lli->lli_smd == NULL) {
143 lli->lli_maxbytes = lsm->lsm_maxbytes;
144 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
145 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
147 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
148 CERROR("lsm mismatch for inode %lld\n",
149 (long long)st->st_ino);
155 if (body->valid & OBD_MD_FLID)
156 st->st_ino = body->ino;
157 if (body->valid & OBD_MD_FLGENER)
158 lli->lli_st_generation = body->generation;
159 if (body->valid & OBD_MD_FLMTIME) {
160 if (body->mtime > LTIME_S(st->st_mtime))
161 LTIME_S(st->st_mtime) = body->mtime;
162 lli->lli_lvb.lvb_mtime = body->mtime;
164 if (body->valid & OBD_MD_FLATIME) {
165 if (body->atime > LTIME_S(st->st_atime))
166 LTIME_S(st->st_atime) = body->atime;
167 lli->lli_lvb.lvb_atime = body->atime;
169 if (body->valid & OBD_MD_FLCTIME) {
170 if (body->ctime > LTIME_S(st->st_ctime))
171 LTIME_S(st->st_ctime) = body->ctime;
172 lli->lli_lvb.lvb_ctime = body->ctime;
174 if (body->valid & OBD_MD_FLMODE)
175 st->st_mode = (st->st_mode & S_IFMT)|(body->mode & ~S_IFMT);
176 if (body->valid & OBD_MD_FLTYPE)
177 st->st_mode = (st->st_mode & ~S_IFMT)|(body->mode & S_IFMT);
178 if (S_ISREG(st->st_mode))
179 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
181 st->st_blksize = 4096;
182 if (body->valid & OBD_MD_FLUID)
183 st->st_uid = body->uid;
184 if (body->valid & OBD_MD_FLGID)
185 st->st_gid = body->gid;
186 if (body->valid & OBD_MD_FLNLINK)
187 st->st_nlink = body->nlink;
188 if (body->valid & OBD_MD_FLRDEV)
189 st->st_rdev = body->rdev;
190 if (body->valid & OBD_MD_FLSIZE)
191 st->st_size = body->size;
192 if (body->valid & OBD_MD_FLBLOCKS)
193 st->st_blocks = body->blocks;
194 if (body->valid & OBD_MD_FLFLAGS)
195 lli->lli_st_flags = body->flags;
197 lli->lli_fid = body->fid1;
200 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
202 struct llu_inode_info *lli = llu_i2info(dst);
203 struct intnl_stat *st = llu_i2stat(dst);
205 valid &= src->o_valid;
207 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
208 CDEBUG(D_INODE,"valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
210 LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
211 (long)src->o_mtime, (long)src->o_ctime);
213 if (valid & OBD_MD_FLATIME)
214 LTIME_S(st->st_atime) = src->o_atime;
215 if (valid & OBD_MD_FLMTIME)
216 LTIME_S(st->st_mtime) = src->o_mtime;
217 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
218 LTIME_S(st->st_ctime) = src->o_ctime;
219 if (valid & OBD_MD_FLSIZE)
220 st->st_size = src->o_size;
221 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
222 st->st_blocks = src->o_blocks;
223 if (valid & OBD_MD_FLBLKSZ)
224 st->st_blksize = src->o_blksize;
225 if (valid & OBD_MD_FLTYPE)
226 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
227 if (valid & OBD_MD_FLMODE)
228 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
229 if (valid & OBD_MD_FLUID)
230 st->st_uid = src->o_uid;
231 if (valid & OBD_MD_FLGID)
232 st->st_gid = src->o_gid;
233 if (valid & OBD_MD_FLFLAGS)
234 lli->lli_st_flags = src->o_flags;
235 if (valid & OBD_MD_FLGENER)
236 lli->lli_st_generation = src->o_generation;
239 #define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
240 #define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
242 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
244 struct llu_inode_info *lli = llu_i2info(src);
245 struct intnl_stat *st = llu_i2stat(src);
246 obd_flag newvalid = 0;
248 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
249 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
250 valid, LTIME_S(st->st_mtime),
251 LTIME_S(st->st_ctime));
253 if (valid & OBD_MD_FLATIME) {
254 dst->o_atime = LTIME_S(st->st_atime);
255 newvalid |= OBD_MD_FLATIME;
257 if (valid & OBD_MD_FLMTIME) {
258 dst->o_mtime = LTIME_S(st->st_mtime);
259 newvalid |= OBD_MD_FLMTIME;
261 if (valid & OBD_MD_FLCTIME) {
262 dst->o_ctime = LTIME_S(st->st_ctime);
263 newvalid |= OBD_MD_FLCTIME;
265 if (valid & OBD_MD_FLSIZE) {
266 dst->o_size = st->st_size;
267 newvalid |= OBD_MD_FLSIZE;
269 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
270 dst->o_blocks = st->st_blocks;
271 newvalid |= OBD_MD_FLBLOCKS;
273 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
274 dst->o_blksize = st->st_blksize;
275 newvalid |= OBD_MD_FLBLKSZ;
277 if (valid & OBD_MD_FLTYPE) {
278 dst->o_mode = (dst->o_mode & S_IALLUGO)|(st->st_mode & S_IFMT);
279 newvalid |= OBD_MD_FLTYPE;
281 if (valid & OBD_MD_FLMODE) {
282 dst->o_mode = (dst->o_mode & S_IFMT)|(st->st_mode & S_IALLUGO);
283 newvalid |= OBD_MD_FLMODE;
285 if (valid & OBD_MD_FLUID) {
286 dst->o_uid = st->st_uid;
287 newvalid |= OBD_MD_FLUID;
289 if (valid & OBD_MD_FLGID) {
290 dst->o_gid = st->st_gid;
291 newvalid |= OBD_MD_FLGID;
293 if (valid & OBD_MD_FLFLAGS) {
294 dst->o_flags = lli->lli_st_flags;
295 newvalid |= OBD_MD_FLFLAGS;
297 if (valid & OBD_MD_FLGENER) {
298 dst->o_generation = lli->lli_st_generation;
299 newvalid |= OBD_MD_FLGENER;
301 if (valid & OBD_MD_FLFID) {
302 dst->o_fid = st->st_ino;
303 newvalid |= OBD_MD_FLFID;
306 dst->o_valid |= newvalid;
310 * really does the getattr on the inode and updates its fields
312 int llu_inode_getattr(struct inode *inode, struct lov_stripe_md *lsm)
314 struct llu_inode_info *lli = llu_i2info(inode);
315 struct obd_export *exp = llu_i2obdexp(inode);
316 struct ptlrpc_request_set *set;
317 struct obd_info oinfo = { { { 0 } } };
318 struct obdo oa = { 0 };
319 obd_flag refresh_valid;
328 oa.o_id = lsm->lsm_object_id;
330 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
331 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
334 set = ptlrpc_prep_set();
336 CERROR ("ENOMEM allocing request set\n");
339 rc = obd_getattr_async(exp, &oinfo, set);
341 rc = ptlrpc_set_wait(set);
342 ptlrpc_set_destroy(set);
347 refresh_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
348 OBD_MD_FLCTIME | OBD_MD_FLSIZE;
350 obdo_refresh_inode(inode, &oa, refresh_valid);
355 static struct inode* llu_new_inode(struct filesys *fs,
359 struct llu_inode_info *lli;
360 struct intnl_stat st = {
362 #ifndef AUTOMOUNT_FILE_NAME
363 .st_mode = fid->f_type & S_IFMT,
365 .st_mode = fid->f_type /* all of the bits! */
371 OBD_ALLOC(lli, sizeof(*lli));
375 /* initialize lli here */
376 lli->lli_sbi = llu_fs2sbi(fs);
378 lli->lli_symlink_name = NULL;
380 lli->lli_maxbytes = (__u64)(~0UL);
381 lli->lli_file_data = NULL;
383 lli->lli_sysio_fid.fid_data = &lli->lli_fid;
384 lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
387 /* file identifier is needed by functions like _sysio_i_find() */
388 inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
389 &st, 0, &llu_inode_ops, lli);
392 OBD_FREE(lli, sizeof(*lli));
397 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
399 struct llu_sb_info *sbi = llu_i2sbi(inode);
400 struct llu_inode_info *lli = llu_i2info(inode);
401 struct lustre_handle lockh;
402 struct ldlm_res_id res_id = { .name = {0} };
403 struct obd_device *obddev;
404 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
410 obddev = sbi->ll_mdc_exp->exp_obd;
411 res_id.name[0] = llu_i2stat(inode)->st_ino;
412 res_id.name[1] = lli->lli_st_generation;
414 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
416 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
417 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
418 &policy, LCK_CR|LCK_CW|LCK_PR|LCK_PW, &lockh)) {
424 static int llu_inode_revalidate(struct inode *inode)
426 struct lov_stripe_md *lsm = NULL;
427 struct llu_inode_info *lli = llu_i2info(inode);
428 struct intnl_stat *st = llu_i2stat(inode);
432 CERROR("REPORT THIS LINE TO PETER\n");
436 if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
438 struct ptlrpc_request *req = NULL;
439 struct llu_sb_info *sbi = llu_i2sbi(inode);
441 unsigned long valid = OBD_MD_FLGETATTR;
444 /* Why don't we update all valid MDS fields here, if we're
445 * doing an RPC anyways? -phil */
446 if (S_ISREG(st->st_mode)) {
447 ealen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
448 valid |= OBD_MD_FLEASIZE;
450 llu_inode2fid(&fid, inode);
451 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, ealen, &req);
453 CERROR("failure %d inode %llu\n", rc,
454 (long long)st->st_ino);
457 rc = mdc_req2lustre_md(req, REPLY_REC_OFF, sbi->ll_osc_exp,&md);
459 /* XXX Too paranoid? */
460 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
461 !((md.body->valid & OBD_MD_FLNLINK) &&
462 (md.body->nlink == 0))) {
463 CERROR("Asked for %s eadata but got %s (%d)\n",
464 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
465 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
466 md.body->eadatasize);
469 ptlrpc_req_finished(req);
474 llu_update_inode(inode, md.body, md.lsm);
475 if (md.lsm != NULL && lli->lli_smd != md.lsm)
476 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
478 if (md.body->valid & OBD_MD_FLSIZE)
479 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
481 ptlrpc_req_finished(req);
486 /* object not yet allocated, don't validate size */
487 st->st_atime = lli->lli_lvb.lvb_atime;
488 st->st_mtime = lli->lli_lvb.lvb_mtime;
489 st->st_ctime = lli->lli_lvb.lvb_ctime;
493 /* ll_glimpse_size will prefer locally cached writes if they extend
495 RETURN(llu_glimpse_size(inode));
498 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
500 *b = *llu_i2stat(ino);
503 static int llu_iop_getattr(struct pnode *pno,
505 struct intnl_stat *b)
510 liblustre_wait_event(0);
514 LASSERT(pno->p_base->pb_ino);
515 ino = pno->p_base->pb_ino;
517 LASSERT(!pno || pno->p_base->pb_ino == ino);
520 /* libsysio might call us directly without intent lock,
521 * we must re-fetch the attrs here
523 rc = llu_inode_revalidate(ino);
525 copy_stat_buf(ino, b);
526 LASSERT(!llu_i2info(ino)->lli_it);
529 liblustre_wait_event(0);
533 static int null_if_equal(struct ldlm_lock *lock, void *data)
535 if (data == lock->l_ast_data) {
536 lock->l_ast_data = NULL;
538 if (lock->l_req_mode != lock->l_granted_mode)
539 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
542 return LDLM_ITER_CONTINUE;
545 void llu_clear_inode(struct inode *inode)
548 struct llu_inode_info *lli = llu_i2info(inode);
549 struct llu_sb_info *sbi = llu_i2sbi(inode);
552 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
553 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
556 llu_inode2fid(&fid, inode);
557 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(lli->lli_flags));
558 mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
561 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
562 null_if_equal, inode);
565 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
569 if (lli->lli_symlink_name) {
570 OBD_FREE(lli->lli_symlink_name,
571 strlen(lli->lli_symlink_name) + 1);
572 lli->lli_symlink_name = NULL;
578 void llu_iop_gone(struct inode *inode)
580 struct llu_inode_info *lli = llu_i2info(inode);
583 liblustre_wait_event(0);
584 llu_clear_inode(inode);
586 OBD_FREE(lli, sizeof(*lli));
590 static int inode_setattr(struct inode * inode, struct iattr * attr)
592 unsigned int ia_valid = attr->ia_valid;
593 struct intnl_stat *st = llu_i2stat(inode);
597 * inode_setattr() is only ever invoked with ATTR_SIZE (by
598 * llu_setattr_raw()) when file has no bodies. Check this.
600 LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
602 if (ia_valid & ATTR_SIZE)
603 st->st_size = attr->ia_size;
604 if (ia_valid & ATTR_UID)
605 st->st_uid = attr->ia_uid;
606 if (ia_valid & ATTR_GID)
607 st->st_gid = attr->ia_gid;
608 if (ia_valid & ATTR_ATIME)
609 st->st_atime = attr->ia_atime;
610 if (ia_valid & ATTR_MTIME)
611 st->st_mtime = attr->ia_mtime;
612 if (ia_valid & ATTR_CTIME)
613 st->st_ctime = attr->ia_ctime;
614 if (ia_valid & ATTR_MODE) {
615 st->st_mode = attr->ia_mode;
616 if (!in_group_p(st->st_gid) && !cfs_capable(CFS_CAP_FSETID))
617 st->st_mode &= ~S_ISGID;
619 /* mark_inode_dirty(inode); */
623 /* If this inode has objects allocated to it (lsm != NULL), then the OST
624 * object(s) determine the file size and mtime. Otherwise, the MDS will
625 * keep these values until such a time that objects are allocated for it.
626 * We do the MDS operations first, as it is checking permissions for us.
627 * We don't to the MDS RPC if there is nothing that we want to store there,
628 * otherwise there is no harm in updating mtime/atime on the MDS if we are
629 * going to do an RPC anyways.
631 * If we are doing a truncate, we will send the mtime and ctime updates
632 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
633 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
636 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
638 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
639 struct llu_sb_info *sbi = llu_i2sbi(inode);
640 struct intnl_stat *st = llu_i2stat(inode);
641 struct ptlrpc_request *request = NULL;
642 struct mdc_op_data op_data;
643 int ia_valid = attr->ia_valid;
647 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
649 if (ia_valid & ATTR_SIZE) {
650 if (attr->ia_size > ll_file_maxbytes(inode)) {
651 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
652 (long long)attr->ia_size,
653 ll_file_maxbytes(inode));
657 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
660 /* We mark all of the fields "set" so MDS/OST does not re-set them */
661 if (attr->ia_valid & ATTR_CTIME) {
662 attr->ia_ctime = CURRENT_TIME;
663 attr->ia_valid |= ATTR_CTIME_SET;
665 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
666 attr->ia_atime = CURRENT_TIME;
667 attr->ia_valid |= ATTR_ATIME_SET;
669 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
670 attr->ia_mtime = CURRENT_TIME;
671 attr->ia_valid |= ATTR_MTIME_SET;
674 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
675 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
676 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
677 LTIME_S(CURRENT_TIME));
679 attr->ia_valid &= ~ATTR_SIZE;
681 /* If only OST attributes being set on objects, don't do MDS RPC.
682 * In that case, we need to check permissions and update the local
683 * inode ourselves so we can call obdo_from_inode() always. */
684 if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
686 llu_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
688 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
689 attr, NULL, 0, NULL, 0, &request);
692 ptlrpc_req_finished(request);
693 if (rc != -EPERM && rc != -EACCES)
694 CERROR("mdc_setattr fails: rc = %d\n", rc);
698 rc = mdc_req2lustre_md(request, REPLY_REC_OFF, sbi->ll_osc_exp,
701 ptlrpc_req_finished(request);
705 /* We call inode_setattr to adjust timestamps.
706 * If there is at least some data in file, we cleared ATTR_SIZE
707 * above to avoid invoking vmtruncate, otherwise it is important
708 * to call vmtruncate in inode_setattr to update inode->i_size
710 inode_setattr(inode, attr);
711 llu_update_inode(inode, md.body, md.lsm);
712 ptlrpc_req_finished(request);
714 if (!lsm || !S_ISREG(st->st_mode)) {
715 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
719 /* The OST doesn't check permissions, but the alternative is
720 * a gratuitous RPC to the MDS. We already rely on the client
721 * to do read/write/truncate permission checks, so is mtime OK?
723 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
724 /* from sys_utime() */
725 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
726 if (current->fsuid != st->st_uid &&
727 (rc = ll_permission(inode, MAY_WRITE)) != 0)
730 /* from inode_change_ok() */
731 if (current->fsuid != st->st_uid &&
732 !cfs_capable(CFS_CAP_FOWNER))
737 /* Won't invoke llu_vmtruncate(), as we already cleared
739 inode_setattr(inode, attr);
742 if (ia_valid & ATTR_SIZE) {
743 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
745 struct lustre_handle lockh = { 0, };
746 struct lustre_handle match_lockh = { 0, };
749 int flags = LDLM_FL_TEST_LOCK; /* for assertion check below */
753 /* check that there are no matching locks */
754 LASSERT(obd_match(sbi->ll_osc_exp, lsm, LDLM_EXTENT, &policy,
755 LCK_PW, &flags, inode, &match_lockh, NULL)
758 /* XXX when we fix the AST intents to pass the discard-range
759 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
761 flags = (attr->ia_size == 0) ? LDLM_AST_DISCARD_DATA : 0;
763 if (sbi->ll_lco.lco_flags & OBD_CONNECT_TRUNCLOCK) {
765 obd_flags = OBD_FL_TRUNCLOCK;
766 CDEBUG(D_INODE, "delegating locking to the OST");
772 /* with lock_mode == LK_NL no lock is taken. */
773 rc = llu_extent_lock(NULL, inode, lsm, lock_mode, &policy,
775 if (rc != ELDLM_OK) {
781 rc = llu_vmtruncate(inode, attr->ia_size, obd_flags);
783 /* unlock now as we don't mind others file lockers racing with
784 * the mds updates below? */
785 err = llu_extent_unlock(NULL, inode, lsm, lock_mode, &lockh);
787 CERROR("llu_extent_unlock failed: %d\n", err);
791 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
792 struct obd_info oinfo = { { { 0 } } };
793 struct obdo oa = { 0 };
794 struct lustre_handle lockh = { 0 };
797 CDEBUG(D_INODE, "set mtime on OST inode %llu to %lu\n",
798 (long long)st->st_ino, LTIME_S(attr->ia_mtime));
800 oa.o_id = lsm->lsm_object_id;
801 oa.o_valid = OBD_MD_FLID;
803 valid = OBD_MD_FLTYPE;
805 if (LTIME_S(attr->ia_mtime) < LTIME_S(attr->ia_ctime)){
806 struct ost_lvb xtimes;
808 /* setting mtime to past is performed under PW
810 oinfo.oi_policy.l_extent.start = 0;
811 oinfo.oi_policy.l_extent.end = OBD_OBJECT_EOF;
812 rc = llu_extent_lock(NULL, inode, lsm, LCK_PW,
818 /* setattr under locks
820 * 1. restore inode's timestamps which are
821 * about to be set as long as concurrent stat
822 * (via llu_glimpse_size) might bring
825 * 2. update lsm so that next stat (via
826 * llu_glimpse_size) could get correct values
828 lov_stripe_lock(lsm);
829 if (ia_valid & ATTR_ATIME) {
830 st->st_atime = xtimes.lvb_atime =
832 valid |= OBD_MD_FLATIME;
834 if (ia_valid & ATTR_MTIME) {
835 st->st_mtime = xtimes.lvb_mtime =
837 valid |= OBD_MD_FLMTIME;
839 if (ia_valid & ATTR_CTIME) {
840 st->st_ctime = xtimes.lvb_ctime =
842 valid |= OBD_MD_FLCTIME;
845 obd_update_lvb(sbi->ll_osc_exp, lsm,
847 lov_stripe_unlock(lsm);
851 * 1. do not use inode's timestamps because
852 * concurrent stat might fill the inode with
853 * out-of-date times, send values from attr
856 * 2.do no update lsm, as long as stat (via
857 * ll_glimpse_size) will bring attributes from
859 if (ia_valid & ATTR_ATIME) {
860 oa.o_atime = attr->ia_atime;
861 oa.o_valid |= OBD_MD_FLATIME;
863 if (ia_valid & ATTR_MTIME) {
864 oa.o_mtime = attr->ia_mtime;
865 oa.o_valid |= OBD_MD_FLMTIME;
867 if (ia_valid & ATTR_CTIME) {
868 oa.o_ctime = attr->ia_ctime;
869 oa.o_valid |= OBD_MD_FLCTIME;
873 obdo_from_inode(&oa, inode, valid);
878 rc = obd_setattr_rqset(sbi->ll_osc_exp, &oinfo, NULL);
880 CERROR("obd_setattr_async fails: rc=%d\n", rc);
882 if (LTIME_S(attr->ia_mtime) < LTIME_S(attr->ia_ctime)){
885 err = llu_extent_unlock(NULL, inode, lsm,
887 if (unlikely(err != 0)) {
888 CERROR("extent unlock failed: "
898 /* here we simply act as a thin layer to glue it with
899 * llu_setattr_raw(), which is copy from kernel
901 static int llu_iop_setattr(struct pnode *pno,
904 struct intnl_stat *stbuf)
910 liblustre_wait_event(0);
912 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
913 SETATTR_UID | SETATTR_GID |
914 SETATTR_LEN | SETATTR_MODE)));
915 memset(&iattr, 0, sizeof(iattr));
917 if (mask & SETATTR_MODE) {
918 iattr.ia_mode = stbuf->st_mode;
919 iattr.ia_valid |= ATTR_MODE;
921 if (mask & SETATTR_MTIME) {
922 iattr.ia_mtime = stbuf->st_mtime;
923 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
925 if (mask & SETATTR_ATIME) {
926 iattr.ia_atime = stbuf->st_atime;
927 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
929 if (mask & SETATTR_UID) {
930 iattr.ia_uid = stbuf->st_uid;
931 iattr.ia_valid |= ATTR_UID;
933 if (mask & SETATTR_GID) {
934 iattr.ia_gid = stbuf->st_gid;
935 iattr.ia_valid |= ATTR_GID;
937 if (mask & SETATTR_LEN) {
938 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
939 iattr.ia_valid |= ATTR_SIZE;
942 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
943 iattr.ia_ctime = CURRENT_TIME;
945 rc = llu_setattr_raw(ino, &iattr);
946 liblustre_wait_idle();
950 #define EXT2_LINK_MAX 32000
952 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
954 struct inode *dir = pno->p_base->pb_parent->pb_ino;
955 struct qstr *qstr = &pno->p_base->pb_name;
956 const char *name = qstr->name;
958 struct ptlrpc_request *request = NULL;
959 struct llu_sb_info *sbi = llu_i2sbi(dir);
960 struct mdc_op_data op_data;
964 liblustre_wait_event(0);
965 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
968 llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
969 err = mdc_create(sbi->ll_mdc_exp, &op_data, tgt, strlen(tgt) + 1,
970 S_IFLNK | S_IRWXUGO, current->fsuid, current->fsgid,
971 cfs_curproc_cap_pack(), 0, &request);
972 ptlrpc_req_finished(request);
973 liblustre_wait_event(0);
977 static int llu_readlink_internal(struct inode *inode,
978 struct ptlrpc_request **request,
981 struct llu_inode_info *lli = llu_i2info(inode);
982 struct llu_sb_info *sbi = llu_i2sbi(inode);
984 struct mds_body *body;
985 struct intnl_stat *st = llu_i2stat(inode);
986 int rc, symlen = st->st_size + 1;
992 if (lli->lli_symlink_name) {
993 *symname = lli->lli_symlink_name;
994 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
998 llu_inode2fid(&fid, inode);
999 rc = mdc_getattr(sbi->ll_mdc_exp, &fid,
1000 OBD_MD_LINKNAME, symlen, request);
1002 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
1006 body = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF,
1008 LASSERT(body != NULL);
1009 LASSERT(lustre_rep_swabbed(*request, REPLY_REC_OFF));
1011 if ((body->valid & OBD_MD_LINKNAME) == 0) {
1012 CERROR ("OBD_MD_LINKNAME not set on reply\n");
1013 GOTO (failed, rc = -EPROTO);
1016 LASSERT(symlen != 0);
1017 if (body->eadatasize != symlen) {
1018 CERROR("inode %llu: symlink length %d not expected %d\n",
1019 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
1020 GOTO(failed, rc = -EPROTO);
1023 *symname = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF + 1,
1025 if (*symname == NULL ||
1026 strnlen(*symname, symlen) != symlen - 1) {
1027 /* not full/NULL terminated */
1028 CERROR("inode %llu: symlink not NULL terminated string"
1029 "of length %d\n", (long long)st->st_ino, symlen - 1);
1030 GOTO(failed, rc = -EPROTO);
1033 OBD_ALLOC(lli->lli_symlink_name, symlen);
1034 /* do not return an error if we cannot cache the symlink locally */
1035 if (lli->lli_symlink_name)
1036 memcpy(lli->lli_symlink_name, *symname, symlen);
1041 ptlrpc_req_finished (*request);
1045 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
1047 struct inode *inode = pno->p_base->pb_ino;
1048 struct ptlrpc_request *request;
1053 liblustre_wait_event(0);
1054 rc = llu_readlink_internal(inode, &request, &symname);
1059 strncpy(data, symname, bufsize);
1060 rc = strlen(symname);
1062 ptlrpc_req_finished(request);
1064 liblustre_wait_event(0);
1068 static int llu_iop_mknod_raw(struct pnode *pno,
1072 struct ptlrpc_request *request = NULL;
1073 struct inode *dir = pno->p_parent->p_base->pb_ino;
1074 struct llu_sb_info *sbi = llu_i2sbi(dir);
1075 struct mdc_op_data op_data;
1079 liblustre_wait_event(0);
1080 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
1081 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
1082 (long long)llu_i2stat(dir)->st_ino);
1084 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
1087 switch (mode & S_IFMT) {
1090 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1095 llu_prepare_mdc_op_data(&op_data, dir, NULL,
1096 pno->p_base->pb_name.name,
1097 pno->p_base->pb_name.len,
1099 err = mdc_create(sbi->ll_mdc_exp, &op_data, NULL, 0, mode,
1100 current->fsuid, current->fsgid,
1101 cfs_curproc_cap_pack(), dev, &request);
1102 ptlrpc_req_finished(request);
1110 liblustre_wait_event(0);
1114 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1116 struct inode *src = old->p_base->pb_ino;
1117 struct inode *dir = new->p_parent->p_base->pb_ino;
1118 const char *name = new->p_base->pb_name.name;
1119 int namelen = new->p_base->pb_name.len;
1120 struct ptlrpc_request *request = NULL;
1121 struct mdc_op_data op_data;
1128 liblustre_wait_event(0);
1129 llu_prepare_mdc_op_data(&op_data, src, dir, name, namelen, 0);
1130 rc = mdc_link(llu_i2sbi(src)->ll_mdc_exp, &op_data, &request);
1131 ptlrpc_req_finished(request);
1132 liblustre_wait_event(0);
1138 * libsysio will clear the inode immediately after return
1140 static int llu_iop_unlink_raw(struct pnode *pno)
1142 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1143 struct qstr *qstr = &pno->p_base->pb_name;
1144 const char *name = qstr->name;
1145 int len = qstr->len;
1146 struct inode *target = pno->p_base->pb_ino;
1147 struct ptlrpc_request *request = NULL;
1148 struct mdc_op_data op_data;
1154 liblustre_wait_event(0);
1155 llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
1156 rc = mdc_unlink(llu_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1158 rc = llu_objects_destroy(request, dir);
1159 ptlrpc_req_finished(request);
1160 liblustre_wait_idle();
1165 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1167 struct inode *src = old->p_parent->p_base->pb_ino;
1168 struct inode *tgt = new->p_parent->p_base->pb_ino;
1169 const char *oldname = old->p_base->pb_name.name;
1170 int oldnamelen = old->p_base->pb_name.len;
1171 const char *newname = new->p_base->pb_name.name;
1172 int newnamelen = new->p_base->pb_name.len;
1173 struct ptlrpc_request *request = NULL;
1174 struct mdc_op_data op_data;
1181 liblustre_wait_event(0);
1182 llu_prepare_mdc_op_data(&op_data, src, tgt, NULL, 0, 0);
1183 rc = mdc_rename(llu_i2sbi(src)->ll_mdc_exp, &op_data,
1184 oldname, oldnamelen, newname, newnamelen,
1187 rc = llu_objects_destroy(request, src);
1190 ptlrpc_req_finished(request);
1191 liblustre_wait_idle();
1196 #ifdef _HAVE_STATVFS
1197 static int llu_statfs_internal(struct llu_sb_info *sbi,
1198 struct obd_statfs *osfs, __u64 max_age)
1200 struct obd_statfs obd_osfs;
1204 rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age, 0);
1206 CERROR("mdc_statfs fails: rc = %d\n", rc);
1210 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1211 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1213 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_osc_exp),
1214 &obd_osfs, max_age, 0);
1216 CERROR("obd_statfs fails: rc = %d\n", rc);
1220 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1221 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1224 osfs->os_blocks = obd_osfs.os_blocks;
1225 osfs->os_bfree = obd_osfs.os_bfree;
1226 osfs->os_bavail = obd_osfs.os_bavail;
1228 /* If we don't have as many objects free on the OST as inodes
1229 * on the MDS, we reduce the total number of inodes to
1230 * compensate, so that the "inodes in use" number is correct.
1232 if (obd_osfs.os_ffree < osfs->os_ffree) {
1233 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1235 osfs->os_ffree = obd_osfs.os_ffree;
1241 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1243 struct obd_statfs osfs;
1246 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1248 /* For now we will always get up-to-date statfs values, but in the
1249 * future we may allow some amount of caching on the client (e.g.
1250 * from QOS or lprocfs updates). */
1251 rc = llu_statfs_internal(sbi, &osfs,
1252 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS));
1256 statfs_unpack(sfs, &osfs);
1258 if (sizeof(sfs->f_blocks) == 4) {
1259 while (osfs.os_blocks > ~0UL) {
1262 osfs.os_blocks >>= 1;
1263 osfs.os_bfree >>= 1;
1264 osfs.os_bavail >>= 1;
1268 sfs->f_blocks = osfs.os_blocks;
1269 sfs->f_bfree = osfs.os_bfree;
1270 sfs->f_bavail = osfs.os_bavail;
1275 static int llu_iop_statvfs(struct pnode *pno,
1277 struct intnl_statvfs *buf)
1283 liblustre_wait_event(0);
1286 LASSERT(pno->p_base->pb_ino);
1287 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1291 /* from native driver */
1292 buf->f_bsize = fs.f_bsize; /* file system block size */
1293 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1294 buf->f_blocks = fs.f_blocks;
1295 buf->f_bfree = fs.f_bfree;
1296 buf->f_bavail = fs.f_bavail;
1297 buf->f_files = fs.f_files; /* Total number serial numbers */
1298 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1299 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1300 buf->f_fsid = fs.f_fsid.__val[1];
1301 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1302 buf->f_namemax = fs.f_namelen;
1305 liblustre_wait_event(0);
1308 #endif /* _HAVE_STATVFS */
1310 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1312 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1313 struct qstr *qstr = &pno->p_base->pb_name;
1314 const char *name = qstr->name;
1315 int len = qstr->len;
1316 struct ptlrpc_request *request = NULL;
1317 struct intnl_stat *st = llu_i2stat(dir);
1318 struct mdc_op_data op_data;
1322 liblustre_wait_event(0);
1323 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1324 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1326 if (st->st_nlink >= EXT2_LINK_MAX)
1329 llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
1330 err = mdc_create(llu_i2sbi(dir)->ll_mdc_exp, &op_data, NULL, 0,
1331 mode | S_IFDIR, current->fsuid, current->fsgid,
1332 cfs_curproc_cap_pack(), 0, &request);
1333 ptlrpc_req_finished(request);
1334 liblustre_wait_event(0);
1338 static int llu_iop_rmdir_raw(struct pnode *pno)
1340 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1341 struct qstr *qstr = &pno->p_base->pb_name;
1342 const char *name = qstr->name;
1343 int len = qstr->len;
1344 struct ptlrpc_request *request = NULL;
1345 struct mdc_op_data op_data;
1349 liblustre_wait_event(0);
1350 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1351 (long long)llu_i2stat(dir)->st_ino,
1352 llu_i2info(dir)->lli_st_generation, dir);
1354 llu_prepare_mdc_op_data(&op_data, dir, NULL, name, len, S_IFDIR);
1355 rc = mdc_unlink(llu_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
1356 ptlrpc_req_finished(request);
1358 liblustre_wait_event(0);
1363 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1365 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1367 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1369 /* refer to ll_file_flock() for details */
1370 int llu_file_flock(struct inode *ino, int cmd, struct file_lock *file_lock)
1372 struct llu_inode_info *lli = llu_i2info(ino);
1373 struct intnl_stat *st = llu_i2stat(ino);
1374 struct ldlm_res_id res_id =
1375 { .name = {st->st_ino, lli->lli_st_generation, LDLM_FLOCK} };
1376 struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1377 ldlm_flock_completion_ast, NULL, file_lock };
1378 struct lustre_handle lockh = {0};
1379 ldlm_policy_data_t flock;
1383 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1384 (unsigned long long) st->st_ino, file_lock);
1386 flock.l_flock.pid = file_lock->fl_pid;
1387 flock.l_flock.start = file_lock->fl_start;
1388 flock.l_flock.end = file_lock->fl_end;
1390 switch (file_lock->fl_type) {
1392 einfo.ei_mode = LCK_PR;
1395 einfo.ei_mode = LCK_NL;
1398 einfo.ei_mode = LCK_PW;
1401 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1408 #if F_SETLKW64 != F_SETLKW
1416 #if F_SETLK64 != F_SETLK
1420 flags = LDLM_FL_BLOCK_NOWAIT;
1424 #if F_GETLK64 != F_GETLK
1428 flags = LDLM_FL_TEST_LOCK;
1429 file_lock->fl_type = einfo.ei_mode;
1432 CERROR("unknown fcntl cmd: %d\n", cmd);
1436 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, flags=%#x, mode=%u, "
1437 "start="LPU64", end="LPU64"\n",
1438 (unsigned long long) st->st_ino, flock.l_flock.pid,
1439 flags, einfo.ei_mode, flock.l_flock.start, flock.l_flock.end);
1441 rc = ldlm_cli_enqueue(llu_i2mdcexp(ino), NULL, &einfo, res_id,
1442 &flock, &flags, NULL, 0, NULL, &lockh, 0);
1447 static int assign_type(struct file_lock *fl, int type)
1460 static int flock_to_posix_lock(struct inode *ino,
1461 struct file_lock *fl,
1464 switch (l->l_whence) {
1465 /* XXX: only SEEK_SET is supported in lustre */
1473 fl->fl_end = l->l_len - 1;
1477 fl->fl_end = OFFSET_MAX;
1479 fl->fl_pid = getpid();
1480 fl->fl_flags = FL_POSIX;
1481 fl->fl_notify = NULL;
1482 fl->fl_insert = NULL;
1483 fl->fl_remove = NULL;
1484 /* XXX: these fields can't be filled with suitable values,
1485 but I think lustre doesn't use them.
1487 fl->fl_owner = NULL;
1490 return assign_type(fl, l->l_type);
1493 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1495 struct file_lock fl;
1499 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1502 error = flock_to_posix_lock(ino, &fl, flock);
1506 error = llu_file_flock(ino, F_GETLK, &fl);
1510 flock->l_type = F_UNLCK;
1511 if (fl.fl_type != F_UNLCK) {
1512 flock->l_pid = fl.fl_pid;
1513 flock->l_start = fl.fl_start;
1514 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1515 fl.fl_end - fl.fl_start + 1;
1516 flock->l_whence = SEEK_SET;
1517 flock->l_type = fl.fl_type;
1524 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1526 struct file_lock fl;
1527 int flags = llu_i2info(ino)->lli_open_flags + 1;
1530 error = flock_to_posix_lock(ino, &fl, flock);
1533 if (cmd == F_SETLKW)
1534 fl.fl_flags |= FL_SLEEP;
1537 switch (flock->l_type) {
1539 if (!(flags & FMODE_READ))
1543 if (!(flags & FMODE_WRITE))
1553 error = llu_file_flock(ino, cmd, &fl);
1561 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1563 struct llu_inode_info *lli = llu_i2info(ino);
1565 struct flock *flock;
1568 liblustre_wait_event(0);
1571 *rtn = lli->lli_open_flags;
1574 flags = va_arg(ap, long);
1575 flags &= FCNTL_FLMASK;
1576 if (flags & FCNTL_FLMASK_INVALID) {
1577 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1578 "the O_NONBLOCK or O_ASYNC flags. "
1579 "Please fix your application.\n");
1584 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1585 (lli->lli_open_flags & ~FCNTL_FLMASK);
1590 #if F_GETLK64 != F_GETLK
1594 flock = va_arg(ap, struct flock *);
1595 err = llu_fcntl_getlk(ino, flock);
1600 #if F_SETLKW64 != F_SETLKW
1606 #if F_SETLK64 != F_SETLK
1610 flock = va_arg(ap, struct flock *);
1611 err = llu_fcntl_setlk(ino, cmd, flock);
1615 CERROR("unsupported fcntl cmd %x\n", cmd);
1621 liblustre_wait_event(0);
1625 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1627 struct llu_inode_info *lli = llu_i2info(inode);
1628 struct ll_file_data *fd = lli->lli_file_data;
1629 ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1630 .end = OBD_OBJECT_EOF}};
1631 struct lustre_handle lockh = { 0 };
1632 struct lov_stripe_md *lsm = lli->lli_smd;
1637 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1641 policy.l_extent.gid = arg;
1642 if (lli->lli_open_flags & O_NONBLOCK)
1643 flags = LDLM_FL_BLOCK_NOWAIT;
1645 err = llu_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh,
1650 fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1652 memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1657 static int llu_put_grouplock(struct inode *inode, unsigned long arg)
1659 struct llu_inode_info *lli = llu_i2info(inode);
1660 struct ll_file_data *fd = lli->lli_file_data;
1661 struct lov_stripe_md *lsm = lli->lli_smd;
1665 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1668 if (fd->fd_gid != arg)
1671 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1673 err = llu_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1678 memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1683 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1685 struct llu_sb_info *sbi = llu_i2sbi(ino);
1686 struct ptlrpc_request *request = NULL;
1687 struct mdc_op_data op_data;
1688 struct iattr attr = { 0 };
1689 struct lov_user_md_v3 lum;
1690 struct lov_user_md *lump = (struct lov_user_md *)arg;
1691 int rc = 0, lum_size = 0;
1693 llu_prepare_mdc_op_data(&op_data, ino, NULL, NULL, 0, 0);
1695 LASSERT(sizeof(lum.lmm_objects[0]) ==
1696 sizeof(lump->lmm_objects[0]));
1697 rc = copy_from_user(&lum, lump, sizeof(*lump));
1700 lum_size = sizeof(struct lov_user_md_v1);
1701 if (lum.lmm_magic == LOV_USER_MAGIC_V3) {
1702 rc = copy_from_user(&lum, lump, sizeof(lum));
1705 lum_size = sizeof(struct lov_user_md_v3);
1708 if ((lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1)) &&
1709 (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))) {
1710 rc = lustre_swab_lov_user_md((struct lov_user_md_v1 *)&lum);
1715 /* swabbing is done in lov_setstripe() on server side */
1716 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
1717 &attr, &lum, lum_size, NULL, 0, &request);
1719 ptlrpc_req_finished(request);
1720 if (rc != -EPERM && rc != -EACCES)
1721 CERROR("mdc_setattr fails: rc = %d\n", rc);
1724 ptlrpc_req_finished(request);
1729 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1730 struct lov_user_md *lum, int lum_size)
1732 struct llu_sb_info *sbi = llu_i2sbi(ino);
1733 struct obd_export *exp = llu_i2obdexp(ino);
1734 struct llu_inode_info *lli = llu_i2info(ino);
1735 struct llu_inode_info *lli2 = NULL;
1736 struct lov_stripe_md *lsm;
1737 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1738 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1739 llu_mdc_blocking_ast, ldlm_completion_ast, NULL, NULL };
1741 struct ptlrpc_request *req = NULL;
1742 struct lustre_md md;
1743 struct mdc_op_data data;
1744 struct lustre_handle lockh;
1750 CDEBUG(D_IOCTL, "stripe already exists for ino "LPU64"\n",
1755 OBD_ALLOC(lli2, sizeof(struct llu_inode_info));
1759 memcpy(lli2, lli, sizeof(struct llu_inode_info));
1760 lli2->lli_open_count = 0;
1761 lli2->lli_it = NULL;
1762 lli2->lli_file_data = NULL;
1763 lli2->lli_smd = NULL;
1764 lli2->lli_symlink_name = NULL;
1765 ino->i_private = lli2;
1767 llu_prepare_mdc_op_data(&data, NULL, ino, NULL, 0, O_RDWR);
1769 rc = mdc_enqueue(sbi->ll_mdc_exp, &einfo, &oit, &data,
1770 &lockh, lum, lum_size, LDLM_FL_INTENT_ONLY);
1774 req = oit.d.lustre.it_data;
1775 rc = it_open_error(DISP_IT_EXECD, &oit);
1781 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1787 rc = mdc_req2lustre_md(req, DLM_REPLY_REC_OFF, exp, &md);
1791 llu_update_inode(ino, md.body, md.lsm);
1792 lli->lli_smd = lli2->lli_smd;
1793 lli2->lli_smd = NULL;
1795 llu_local_open(lli2, &oit);
1797 /* release intent */
1798 if (lustre_handle_is_used(&lockh))
1799 ldlm_lock_decref(&lockh, LCK_CR);
1801 ptlrpc_req_finished(req);
1804 rc = llu_file_release(ino);
1806 ino->i_private = lli;
1808 OBD_FREE(lli2, sizeof(struct llu_inode_info));
1810 ptlrpc_req_finished(req);
1814 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1816 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1818 int flags = FMODE_WRITE;
1821 LASSERT(sizeof(lum) == sizeof(*lump));
1822 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1823 rc = copy_from_user(&lum, lump, sizeof(lum));
1827 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1831 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1833 struct intnl_stat *st = llu_i2stat(ino);
1834 if (S_ISREG(st->st_mode))
1835 return llu_lov_file_setstripe(ino, arg);
1836 if (S_ISDIR(st->st_mode))
1837 return llu_lov_dir_setstripe(ino, arg);
1842 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1844 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1849 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1853 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1859 liblustre_wait_event(0);
1862 case LL_IOC_GROUP_LOCK:
1863 arg = va_arg(ap, unsigned long);
1864 rc = llu_get_grouplock(ino, arg);
1866 case LL_IOC_GROUP_UNLOCK:
1867 arg = va_arg(ap, unsigned long);
1868 rc = llu_put_grouplock(ino, arg);
1870 case LL_IOC_LOV_SETSTRIPE:
1871 arg = va_arg(ap, unsigned long);
1872 rc = llu_lov_setstripe(ino, arg);
1874 case LL_IOC_LOV_GETSTRIPE:
1875 arg = va_arg(ap, unsigned long);
1876 rc = llu_lov_getstripe(ino, arg);
1879 CERROR("did not support ioctl cmd %lx\n", request);
1884 liblustre_wait_event(0);
1889 * we already do syncronous read/write
1891 static int llu_iop_sync(struct inode *inode)
1893 liblustre_wait_event(0);
1897 static int llu_iop_datasync(struct inode *inode)
1899 liblustre_wait_event(0);
1903 struct filesys_ops llu_filesys_ops =
1905 fsop_gone: llu_fsop_gone,
1908 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1910 struct inode *inode;
1912 struct file_identifier fileid = {&fid, sizeof(fid)};
1914 if ((md->body->valid &
1915 (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1916 (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) {
1917 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1919 return ERR_PTR(-EPERM);
1922 /* try to find existing inode */
1923 fid = md->body->fid1;
1925 inode = _sysio_i_find(fs, &fileid);
1927 struct llu_inode_info *lli = llu_i2info(inode);
1929 if (inode->i_zombie ||
1930 lli->lli_st_generation != md->body->generation) {
1934 llu_update_inode(inode, md->body, md->lsm);
1939 inode = llu_new_inode(fs, &fid);
1941 llu_update_inode(inode, md->body, md->lsm);
1946 extern struct list_head lustre_profile_list;
1949 llu_fsswop_mount(const char *source,
1951 const void *data __IS_UNUSED,
1952 struct pnode *tocover,
1953 struct mount **mntp)
1957 struct pnode_base *rootpb;
1958 struct obd_device *obd;
1959 struct ll_fid rootfid;
1960 struct llu_sb_info *sbi;
1961 struct obd_statfs osfs;
1962 static struct qstr noname = { NULL, 0, 0 };
1963 struct ptlrpc_request *request = NULL;
1964 struct lustre_handle mdc_conn = {0, };
1965 struct lustre_handle osc_conn = {0, };
1966 struct lustre_md md;
1968 struct config_llog_instance cfg = {0, };
1969 char ll_instance[sizeof(sbi) * 2 + 1];
1970 struct lustre_profile *lprof;
1971 char *zconf_mgsnid, *zconf_profile;
1972 char *osc = NULL, *mdc = NULL;
1973 int async = 1, err = -EINVAL;
1974 struct obd_connect_data ocd = {0,};
1978 if (ll_parse_mount_target(source,
1981 CERROR("mal-formed target %s\n", source);
1984 if (!zconf_mgsnid || !zconf_profile) {
1985 printf("Liblustre: invalid target %s\n", source);
1988 /* allocate & initialize sbi */
1989 OBD_ALLOC(sbi, sizeof(*sbi));
1993 CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
1994 ll_generate_random_uuid(uuid);
1995 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1997 /* generate a string unique to this super, let's try
1998 the address of the super itself.*/
1999 sprintf(ll_instance, "%p", sbi);
2001 /* retrive & parse config log */
2002 cfg.cfg_instance = ll_instance;
2003 cfg.cfg_uuid = sbi->ll_sb_uuid;
2004 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
2006 CERROR("Unable to process log: %s\n", zconf_profile);
2007 GOTO(out_free, err);
2010 lprof = class_get_profile(zconf_profile);
2011 if (lprof == NULL) {
2012 CERROR("No profile found: %s\n", zconf_profile);
2013 GOTO(out_free, err = -EINVAL);
2015 OBD_ALLOC(osc, strlen(lprof->lp_osc) + strlen(ll_instance) + 2);
2016 sprintf(osc, "%s-%s", lprof->lp_osc, ll_instance);
2018 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) + strlen(ll_instance) + 2);
2019 sprintf(mdc, "%s-%s", lprof->lp_mdc, ll_instance);
2023 GOTO(out_free, err = -EINVAL);
2027 GOTO(out_free, err = -EINVAL);
2030 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
2036 obd = class_name2obd(mdc);
2038 CERROR("MDC %s: not setup or attached\n", mdc);
2039 GOTO(out_free, err = -EINVAL);
2041 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2042 sizeof(async), &async, NULL);
2044 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
2045 OBD_CONNECT_AT | OBD_CONNECT_VBR;
2046 #ifdef LIBLUSTRE_POSIX_ACL
2047 ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
2049 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2050 ocd.ocd_version = LUSTRE_VERSION_CODE;
2053 err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid, &ocd, &sbi->ll_mdc_exp);
2055 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2056 GOTO(out_free, err);
2059 err = obd_statfs(obd, &osfs, 100000000, 0);
2064 * FIXME fill fs stat data into sbi here!!! FIXME
2068 obd = class_name2obd(osc);
2070 CERROR("OSC %s: not setup or attached\n", osc);
2071 GOTO(out_mdc, err = -EINVAL);
2073 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2074 sizeof(async), &async, NULL);
2076 obd->obd_upcall.onu_owner = &sbi->ll_lco;
2077 obd->obd_upcall.onu_upcall = ll_ocd_update;
2079 /* ask lov to generate OBD_NOTIFY_CREATE events for already registered
2081 obd_notify(obd, NULL, OBD_NOTIFY_CREATE, NULL);
2083 obd_register_lock_cancel_cb(obd, llu_extent_lock_cancel_cb);
2085 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2086 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK | OBD_CONNECT_AT;
2087 ocd.ocd_version = LUSTRE_VERSION_CODE;
2088 err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid, &ocd, &sbi->ll_osc_exp);
2090 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2091 GOTO(out_lock_cb, err);
2093 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2094 sbi->ll_lco.lco_mdc_exp = sbi->ll_mdc_exp;
2095 sbi->ll_lco.lco_osc_exp = sbi->ll_osc_exp;
2097 mdc_init_ea_size(sbi->ll_mdc_exp, sbi->ll_osc_exp);
2099 err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
2101 CERROR("cannot mds_connect: rc = %d\n", err);
2102 GOTO(out_lock_cb, err);
2104 CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
2105 sbi->ll_rootino = rootfid.id;
2107 /* fetch attr of root inode */
2108 err = mdc_getattr(sbi->ll_mdc_exp, &rootfid,
2109 OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0,
2112 CERROR("mdc_getattr failed for root: rc = %d\n", err);
2116 err = mdc_req2lustre_md(request, REPLY_REC_OFF, sbi->ll_osc_exp, &md);
2118 CERROR("failed to understand root inode md: rc = %d\n",err);
2119 GOTO(out_request, err);
2122 LASSERT(sbi->ll_rootino != 0);
2124 root = llu_iget(fs, &md);
2125 if (!root || IS_ERR(root)) {
2126 CERROR("fail to generate root inode\n");
2127 GOTO(out_request, err = -EBADF);
2131 * Generate base path-node for root.
2133 rootpb = _sysio_pb_new(&noname, NULL, root);
2139 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2141 _sysio_pb_gone(rootpb);
2145 ptlrpc_req_finished(request);
2147 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2148 liblustre_wait_idle();
2153 _sysio_i_gone(root);
2155 ptlrpc_req_finished(request);
2157 obd_disconnect(sbi->ll_osc_exp);
2159 obd = class_name2obd(osc);
2160 obd_unregister_lock_cancel_cb(obd, llu_extent_lock_cancel_cb);
2162 obd_disconnect(sbi->ll_mdc_exp);
2165 OBD_FREE(osc, strlen(osc) + 1);
2167 OBD_FREE(mdc, strlen(mdc) + 1);
2168 OBD_FREE(sbi, sizeof(*sbi));
2170 liblustre_wait_idle();
2174 struct fssw_ops llu_fssw_ops = {
2178 static struct inode_ops llu_inode_ops = {
2179 inop_lookup: llu_iop_lookup,
2180 inop_getattr: llu_iop_getattr,
2181 inop_setattr: llu_iop_setattr,
2182 inop_filldirentries: llu_iop_filldirentries,
2183 inop_mkdir: llu_iop_mkdir_raw,
2184 inop_rmdir: llu_iop_rmdir_raw,
2185 inop_symlink: llu_iop_symlink_raw,
2186 inop_readlink: llu_iop_readlink,
2187 inop_open: llu_iop_open,
2188 inop_close: llu_iop_close,
2189 inop_link: llu_iop_link_raw,
2190 inop_unlink: llu_iop_unlink_raw,
2191 inop_rename: llu_iop_rename_raw,
2192 inop_pos: llu_iop_pos,
2193 inop_read: llu_iop_read,
2194 inop_write: llu_iop_write,
2195 inop_iodone: llu_iop_iodone,
2196 inop_fcntl: llu_iop_fcntl,
2197 inop_sync: llu_iop_sync,
2198 inop_datasync: llu_iop_datasync,
2199 inop_ioctl: llu_iop_ioctl,
2200 inop_mknod: llu_iop_mknod_raw,
2201 #ifdef _HAVE_STATVFS
2202 inop_statvfs: llu_iop_statvfs,
2204 inop_gone: llu_iop_gone,