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_md_exp);
97 list_del(&sbi->ll_conn_chain);
98 obd_unregister_lock_cancel_cb(sbi->ll_dt_exp,
99 llu_extent_lock_cancel_cb);
100 obd_disconnect(sbi->ll_dt_exp);
101 obd_disconnect(sbi->ll_md_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 static ldlm_mode_t llu_take_md_lock(struct inode *inode, __u64 bits,
115 struct lustre_handle *lockh)
117 ldlm_policy_data_t policy = { .l_inodebits = {bits}};
123 fid = &llu_i2info(inode)->lli_fid;
124 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
126 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
127 rc = md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
128 LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
132 void llu_update_inode(struct inode *inode, struct mdt_body *body,
133 struct lov_stripe_md *lsm)
135 struct llu_inode_info *lli = llu_i2info(inode);
136 struct intnl_stat *st = llu_i2stat(inode);
138 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
140 if (lli->lli_smd == NULL) {
142 lli->lli_maxbytes = lsm->lsm_maxbytes;
143 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
144 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
146 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
147 CERROR("lsm mismatch for inode %lld\n",
148 (long long)st->st_ino);
154 if (body->valid & OBD_MD_FLMTIME &&
155 body->mtime > LTIME_S(st->st_mtime))
156 LTIME_S(st->st_mtime) = body->mtime;
157 if (body->valid & OBD_MD_FLATIME &&
158 body->atime > LTIME_S(st->st_atime))
159 LTIME_S(st->st_atime) = body->atime;
161 /* mtime is always updated with ctime, but can be set in past.
162 As write and utime(2) may happen within 1 second, and utime's
163 mtime has a priority over write's one, so take mtime from mds
164 for the same ctimes. */
165 if (body->valid & OBD_MD_FLCTIME &&
166 body->ctime >= LTIME_S(st->st_ctime)) {
167 LTIME_S(st->st_ctime) = body->ctime;
168 if (body->valid & OBD_MD_FLMTIME)
169 LTIME_S(st->st_mtime) = body->mtime;
171 if (body->valid & OBD_MD_FLMODE)
172 st->st_mode = (st->st_mode & S_IFMT)|(body->mode & ~S_IFMT);
173 if (body->valid & OBD_MD_FLTYPE)
174 st->st_mode = (st->st_mode & ~S_IFMT)|(body->mode & S_IFMT);
175 if (S_ISREG(st->st_mode))
176 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
178 st->st_blksize = 4096;
179 if (body->valid & OBD_MD_FLUID)
180 st->st_uid = body->uid;
181 if (body->valid & OBD_MD_FLGID)
182 st->st_gid = body->gid;
183 if (body->valid & OBD_MD_FLNLINK)
184 st->st_nlink = body->nlink;
185 if (body->valid & OBD_MD_FLRDEV)
186 st->st_rdev = body->rdev;
187 if (body->valid & OBD_MD_FLFLAGS)
188 lli->lli_st_flags = body->flags;
189 if (body->valid & OBD_MD_FLSIZE) {
190 if ((llu_i2sbi(inode)->ll_lco.lco_flags & OBD_CONNECT_SOM) &&
191 S_ISREG(st->st_mode) && lli->lli_smd) {
192 struct lustre_handle lockh;
195 /* As it is possible a blocking ast has been processed
196 * by this time, we need to check there is an UPDATE
197 * lock on the client and set LLIF_MDS_SIZE_LOCK holding
199 mode = llu_take_md_lock(inode, MDS_INODELOCK_UPDATE,
202 st->st_size = body->size;
203 lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
204 ldlm_lock_decref(&lockh, mode);
207 st->st_size = body->size;
210 if (body->valid & OBD_MD_FLBLOCKS)
211 st->st_blocks = body->blocks;
215 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
217 struct llu_inode_info *lli = llu_i2info(dst);
218 struct intnl_stat *st = llu_i2stat(dst);
220 valid &= src->o_valid;
222 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
223 CDEBUG(D_INODE,"valid "LPX64", cur time "CFS_TIME_T"/"CFS_TIME_T
226 LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
227 (long)src->o_mtime, (long)src->o_ctime);
229 if (valid & OBD_MD_FLATIME)
230 LTIME_S(st->st_atime) = src->o_atime;
231 if (valid & OBD_MD_FLMTIME)
232 LTIME_S(st->st_mtime) = src->o_mtime;
233 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
234 LTIME_S(st->st_ctime) = src->o_ctime;
235 if (valid & OBD_MD_FLSIZE)
236 st->st_size = src->o_size;
237 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
238 st->st_blocks = src->o_blocks;
239 if (valid & OBD_MD_FLBLKSZ)
240 st->st_blksize = src->o_blksize;
241 if (valid & OBD_MD_FLTYPE)
242 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
243 if (valid & OBD_MD_FLMODE)
244 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
245 if (valid & OBD_MD_FLUID)
246 st->st_uid = src->o_uid;
247 if (valid & OBD_MD_FLGID)
248 st->st_gid = src->o_gid;
249 if (valid & OBD_MD_FLFLAGS)
250 lli->lli_st_flags = src->o_flags;
253 #define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
254 #define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
256 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
258 struct llu_inode_info *lli = llu_i2info(src);
259 struct intnl_stat *st = llu_i2stat(src);
260 obd_flag newvalid = 0;
262 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
263 CDEBUG(D_INODE, "valid %x, new time "CFS_TIME_T"/"CFS_TIME_T"\n",
264 valid, LTIME_S(st->st_mtime),
265 LTIME_S(st->st_ctime));
267 if (valid & OBD_MD_FLATIME) {
268 dst->o_atime = LTIME_S(st->st_atime);
269 newvalid |= OBD_MD_FLATIME;
271 if (valid & OBD_MD_FLMTIME) {
272 dst->o_mtime = LTIME_S(st->st_mtime);
273 newvalid |= OBD_MD_FLMTIME;
275 if (valid & OBD_MD_FLCTIME) {
276 dst->o_ctime = LTIME_S(st->st_ctime);
277 newvalid |= OBD_MD_FLCTIME;
279 if (valid & OBD_MD_FLSIZE) {
280 dst->o_size = st->st_size;
281 newvalid |= OBD_MD_FLSIZE;
283 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
284 dst->o_blocks = st->st_blocks;
285 newvalid |= OBD_MD_FLBLOCKS;
287 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
288 dst->o_blksize = st->st_blksize;
289 newvalid |= OBD_MD_FLBLKSZ;
291 if (valid & OBD_MD_FLTYPE) {
292 dst->o_mode = (dst->o_mode & S_IALLUGO)|(st->st_mode & S_IFMT);
293 newvalid |= OBD_MD_FLTYPE;
295 if (valid & OBD_MD_FLMODE) {
296 dst->o_mode = (dst->o_mode & S_IFMT)|(st->st_mode & S_IALLUGO);
297 newvalid |= OBD_MD_FLMODE;
299 if (valid & OBD_MD_FLUID) {
300 dst->o_uid = st->st_uid;
301 newvalid |= OBD_MD_FLUID;
303 if (valid & OBD_MD_FLGID) {
304 dst->o_gid = st->st_gid;
305 newvalid |= OBD_MD_FLGID;
307 if (valid & OBD_MD_FLFLAGS) {
308 dst->o_flags = lli->lli_st_flags;
309 newvalid |= OBD_MD_FLFLAGS;
311 if (valid & OBD_MD_FLGENER) {
312 dst->o_generation = lli->lli_st_generation;
313 newvalid |= OBD_MD_FLGENER;
315 if (valid & OBD_MD_FLFID) {
316 dst->o_fid = st->st_ino;
317 newvalid |= OBD_MD_FLFID;
320 dst->o_valid |= newvalid;
324 * really does the getattr on the inode and updates its fields
326 int llu_inode_getattr(struct inode *inode, struct obdo *obdo)
328 struct llu_inode_info *lli = llu_i2info(inode);
329 struct ptlrpc_request_set *set;
330 struct lov_stripe_md *lsm = lli->lli_smd;
331 struct obd_info oinfo = { { { 0 } } };
339 oinfo.oi_oa->o_id = lsm->lsm_object_id;
340 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
341 oinfo.oi_oa->o_mode = S_IFREG;
342 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
343 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
344 OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
345 OBD_MD_FLCTIME | OBD_MD_FLGROUP;
347 set = ptlrpc_prep_set();
349 CERROR ("ENOMEM allocing request set\n");
352 rc = obd_getattr_async(llu_i2obdexp(inode), &oinfo, set);
354 rc = ptlrpc_set_wait(set);
355 ptlrpc_set_destroy(set);
360 oinfo.oi_oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
361 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
364 obdo_refresh_inode(inode, oinfo.oi_oa, oinfo.oi_oa->o_valid);
365 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %Lu, "
366 "blksize %Lu\n", lli->lli_smd->lsm_object_id,
367 (long long unsigned)llu_i2stat(inode)->st_size,
368 (long long unsigned)llu_i2stat(inode)->st_blocks,
369 (long long unsigned)llu_i2stat(inode)->st_blksize);
373 static struct inode* llu_new_inode(struct filesys *fs,
377 struct llu_inode_info *lli;
378 struct intnl_stat st = {
381 #ifndef AUTOMOUNT_FILE_NAME
382 .st_mode = fid->f_type & S_IFMT,
384 .st_mode = fid->f_type /* all of the bits! */
387 /* FIXME: fix this later */
394 OBD_ALLOC(lli, sizeof(*lli));
398 /* initialize lli here */
399 lli->lli_sbi = llu_fs2sbi(fs);
401 lli->lli_symlink_name = NULL;
403 lli->lli_maxbytes = (__u64)(~0UL);
404 lli->lli_file_data = NULL;
406 lli->lli_sysio_fid.fid_data = &lli->lli_fid;
407 lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
410 /* file identifier is needed by functions like _sysio_i_find() */
411 inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
412 &st, 0, &llu_inode_ops, lli);
415 OBD_FREE(lli, sizeof(*lli));
420 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
422 struct lustre_handle lockh;
423 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
430 fid = &llu_i2info(inode)->lli_fid;
431 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
433 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
434 if (md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
435 LCK_CR|LCK_CW|LCK_PR|LCK_PW, &lockh)) {
441 static int llu_inode_revalidate(struct inode *inode)
443 struct lov_stripe_md *lsm = NULL;
447 CERROR("REPORT THIS LINE TO PETER\n");
451 if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
453 struct ptlrpc_request *req = NULL;
454 struct llu_sb_info *sbi = llu_i2sbi(inode);
455 unsigned long valid = OBD_MD_FLGETATTR;
458 /* Why don't we update all valid MDS fields here, if we're
459 * doing an RPC anyways? -phil */
460 if (S_ISREG(llu_i2stat(inode)->st_mode)) {
461 ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
462 valid |= OBD_MD_FLEASIZE;
464 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
465 NULL, valid, ealen, &req);
467 CERROR("failure %d inode %llu\n", rc,
468 (long long)llu_i2stat(inode)->st_ino);
471 rc = md_get_lustre_md(sbi->ll_md_exp, req,
472 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
474 /* XXX Too paranoid? */
475 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
476 !((md.body->valid & OBD_MD_FLNLINK) &&
477 (md.body->nlink == 0))) {
478 CERROR("Asked for %s eadata but got %s (%d)\n",
479 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
480 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
481 md.body->eadatasize);
484 ptlrpc_req_finished(req);
489 llu_update_inode(inode, md.body, md.lsm);
490 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
491 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
492 ptlrpc_req_finished(req);
495 lsm = llu_i2info(inode)->lli_smd;
496 if (!lsm) /* object not yet allocated, don't validate size */
499 /* ll_glimpse_size will prefer locally cached writes if they extend
501 RETURN(llu_glimpse_size(inode));
504 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
506 *b = *llu_i2stat(ino);
509 static int llu_iop_getattr(struct pnode *pno,
511 struct intnl_stat *b)
516 liblustre_wait_event(0);
520 LASSERT(pno->p_base->pb_ino);
521 ino = pno->p_base->pb_ino;
523 LASSERT(!pno || pno->p_base->pb_ino == ino);
526 /* libsysio might call us directly without intent lock,
527 * we must re-fetch the attrs here
529 rc = llu_inode_revalidate(ino);
531 copy_stat_buf(ino, b);
532 LASSERT(!llu_i2info(ino)->lli_it);
535 liblustre_wait_event(0);
539 static int null_if_equal(struct ldlm_lock *lock, void *data)
541 if (data == lock->l_ast_data) {
542 lock->l_ast_data = NULL;
544 if (lock->l_req_mode != lock->l_granted_mode)
545 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
548 return LDLM_ITER_CONTINUE;
551 void llu_clear_inode(struct inode *inode)
553 struct llu_inode_info *lli = llu_i2info(inode);
554 struct llu_sb_info *sbi = llu_i2sbi(inode);
557 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
558 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
561 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
562 md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
563 null_if_equal, inode);
566 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
567 null_if_equal, inode);
570 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
574 if (lli->lli_symlink_name) {
575 OBD_FREE(lli->lli_symlink_name,
576 strlen(lli->lli_symlink_name) + 1);
577 lli->lli_symlink_name = NULL;
583 void llu_iop_gone(struct inode *inode)
585 struct llu_inode_info *lli = llu_i2info(inode);
588 liblustre_wait_event(0);
589 llu_clear_inode(inode);
591 OBD_FREE(lli, sizeof(*lli));
595 static int inode_setattr(struct inode * inode, struct iattr * attr)
597 unsigned int ia_valid = attr->ia_valid;
598 struct intnl_stat *st = llu_i2stat(inode);
602 * inode_setattr() is only ever invoked with ATTR_SIZE (by
603 * llu_setattr_raw()) when file has no bodies. Check this.
605 LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
607 if (ia_valid & ATTR_SIZE)
608 st->st_size = attr->ia_size;
609 if (ia_valid & ATTR_UID)
610 st->st_uid = attr->ia_uid;
611 if (ia_valid & ATTR_GID)
612 st->st_gid = attr->ia_gid;
613 if (ia_valid & ATTR_ATIME)
614 st->st_atime = attr->ia_atime;
615 if (ia_valid & ATTR_MTIME)
616 st->st_mtime = attr->ia_mtime;
617 if (ia_valid & ATTR_CTIME)
618 st->st_ctime = attr->ia_ctime;
619 if (ia_valid & ATTR_MODE) {
620 st->st_mode = attr->ia_mode;
621 if (!in_group_p(st->st_gid) && !capable(CAP_FSETID))
622 st->st_mode &= ~S_ISGID;
624 /* mark_inode_dirty(inode); */
628 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
629 struct md_open_data **mod)
632 struct llu_sb_info *sbi = llu_i2sbi(inode);
633 struct ptlrpc_request *request = NULL;
637 llu_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0, LUSTRE_OPC_ANY);
638 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL,
642 ptlrpc_req_finished(request);
643 if (rc != -EPERM && rc != -EACCES)
644 CERROR("md_setattr fails: rc = %d\n", rc);
648 rc = md_get_lustre_md(sbi->ll_md_exp, request,
649 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
651 ptlrpc_req_finished(request);
655 /* We call inode_setattr to adjust timestamps.
656 * If there is at least some data in file, we cleared ATTR_SIZE
657 * above to avoid invoking vmtruncate, otherwise it is important
658 * to call vmtruncate in inode_setattr to update inode->i_size
660 inode_setattr(inode, &op_data->op_attr);
661 llu_update_inode(inode, md.body, md.lsm);
662 ptlrpc_req_finished(request);
667 /* Close IO epoch and send Size-on-MDS attribute update. */
668 static int llu_setattr_done_writing(struct inode *inode,
669 struct md_op_data *op_data,
670 struct md_open_data *mod)
672 struct llu_inode_info *lli = llu_i2info(inode);
673 struct intnl_stat *st = llu_i2stat(inode);
677 LASSERT(op_data != NULL);
678 if (!S_ISREG(st->st_mode))
681 /* XXX: pass och here for the recovery purpose. */
682 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
683 op_data->op_ioepoch, PFID(&lli->lli_fid));
685 op_data->op_flags = MF_EPOCH_CLOSE | MF_SOM_CHANGE;
686 rc = md_done_writing(llu_i2sbi(inode)->ll_md_exp, op_data, mod);
688 /* MDS has instructed us to obtain Size-on-MDS attribute
689 * from OSTs and send setattr to back to MDS. */
690 rc = llu_sizeonmds_update(inode, mod, &op_data->op_handle,
691 op_data->op_ioepoch);
693 CERROR("inode %llu mdc truncate failed: rc = %d\n",
699 /* If this inode has objects allocated to it (lsm != NULL), then the OST
700 * object(s) determine the file size and mtime. Otherwise, the MDS will
701 * keep these values until such a time that objects are allocated for it.
702 * We do the MDS operations first, as it is checking permissions for us.
703 * We don't to the MDS RPC if there is nothing that we want to store there,
704 * otherwise there is no harm in updating mtime/atime on the MDS if we are
705 * going to do an RPC anyways.
707 * If we are doing a truncate, we will send the mtime and ctime updates
708 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
709 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
712 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
714 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
715 struct llu_sb_info *sbi = llu_i2sbi(inode);
716 struct intnl_stat *st = llu_i2stat(inode);
717 int ia_valid = attr->ia_valid;
718 struct md_op_data op_data = { { 0 } };
719 struct md_open_data *mod = NULL;
723 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
725 if (ia_valid & ATTR_SIZE) {
726 if (attr->ia_size > ll_file_maxbytes(inode)) {
727 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
728 (long long)attr->ia_size,
729 ll_file_maxbytes(inode));
733 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME | ATTR_TRUNC;
736 /* We mark all of the fields "set" so MDS/OST does not re-set them */
737 if (attr->ia_valid & ATTR_CTIME) {
738 attr->ia_ctime = CURRENT_TIME;
739 attr->ia_valid |= ATTR_CTIME_SET;
741 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
742 attr->ia_atime = CURRENT_TIME;
743 attr->ia_valid |= ATTR_ATIME_SET;
745 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
746 attr->ia_mtime = CURRENT_TIME;
747 attr->ia_valid |= ATTR_MTIME_SET;
749 if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
750 /* To avoid stale mtime on mds, obtain it from ost and send
752 rc = llu_glimpse_size(inode);
756 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
757 attr->ia_mtime = inode->i_stbuf.st_mtime;
760 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
761 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T", ctime "CFS_TIME_T
762 ", now = "CFS_TIME_T"\n",
763 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
764 LTIME_S(CURRENT_TIME));
766 /* NB: ATTR_SIZE will only be set after this point if the size
767 * resides on the MDS, ie, this file has no objects. */
769 attr->ia_valid &= ~ATTR_SIZE;
771 /* If only OST attributes being set on objects, don't do MDS RPC.
772 * In that case, we need to check permissions and update the local
773 * inode ourselves so we can call obdo_from_inode() always. */
774 if (ia_valid & (lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
775 memcpy(&op_data.op_attr, attr, sizeof(*attr));
777 /* Open epoch for truncate. */
778 if (ia_valid & ATTR_SIZE)
779 op_data.op_flags = MF_EPOCH_OPEN;
780 rc = llu_md_setattr(inode, &op_data, &mod);
784 if (op_data.op_ioepoch)
785 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID" for "
786 "truncate\n", op_data.op_ioepoch,
787 PFID(&llu_i2info(inode)->lli_fid));
789 if (!lsm || !S_ISREG(st->st_mode)) {
790 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
794 /* The OST doesn't check permissions, but the alternative is
795 * a gratuitous RPC to the MDS. We already rely on the client
796 * to do read/write/truncate permission checks, so is mtime OK?
798 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
799 /* from sys_utime() */
800 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
801 if (current->fsuid != st->st_uid &&
802 (rc = ll_permission(inode, MAY_WRITE)) != 0)
805 /* from inode_change_ok() */
806 if (current->fsuid != st->st_uid &&
807 !capable(CAP_FOWNER))
813 /* Won't invoke llu_vmtruncate(), as we already cleared
815 inode_setattr(inode, attr);
818 if (ia_valid & ATTR_SIZE) {
819 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
821 struct lustre_handle lockh = { 0, };
822 struct lustre_handle match_lockh = { 0, };
825 int flags = LDLM_FL_TEST_LOCK; /* for assertion check below */
829 /* check that there are no matching locks */
830 LASSERT(obd_match(sbi->ll_dt_exp, lsm, LDLM_EXTENT, &policy,
831 LCK_PW, &flags, inode, &match_lockh) <= 0);
833 /* XXX when we fix the AST intents to pass the discard-range
834 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
836 flags = (attr->ia_size == 0) ? LDLM_AST_DISCARD_DATA : 0;
838 if (sbi->ll_lco.lco_flags & OBD_CONNECT_TRUNCLOCK) {
840 obd_flags = OBD_FL_TRUNCLOCK;
841 CDEBUG(D_INODE, "delegating locking to the OST");
847 /* with lock_mode == LK_NL no lock is taken. */
848 rc = llu_extent_lock(NULL, inode, lsm, lock_mode, &policy,
850 if (rc != ELDLM_OK) {
852 GOTO(out, rc = -ENOLCK);
855 rc = llu_vmtruncate(inode, attr->ia_size, obd_flags);
857 /* unlock now as we don't mind others file lockers racing with
858 * the mds updates below? */
859 err = llu_extent_unlock(NULL, inode, lsm, lock_mode, &lockh);
861 CERROR("llu_extent_unlock failed: %d\n", err);
865 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
866 struct obd_info oinfo = { { { 0 } } };
869 CDEBUG(D_INODE, "set mtime on OST inode %llu to "CFS_TIME_T"\n",
870 (long long)st->st_ino, LTIME_S(attr->ia_mtime));
871 oa.o_id = lsm->lsm_object_id;
872 oa.o_gr = lsm->lsm_object_gr;
873 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
875 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
876 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
881 rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
883 CERROR("obd_setattr_async fails: rc=%d\n", rc);
887 if (op_data.op_ioepoch)
888 rc1 = llu_setattr_done_writing(inode, &op_data, mod);
889 return rc ? rc : rc1;
892 /* here we simply act as a thin layer to glue it with
893 * llu_setattr_raw(), which is copy from kernel
895 static int llu_iop_setattr(struct pnode *pno,
898 struct intnl_stat *stbuf)
904 liblustre_wait_event(0);
906 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
907 SETATTR_UID | SETATTR_GID |
908 SETATTR_LEN | SETATTR_MODE)));
909 memset(&iattr, 0, sizeof(iattr));
911 if (mask & SETATTR_MODE) {
912 iattr.ia_mode = stbuf->st_mode;
913 iattr.ia_valid |= ATTR_MODE;
915 if (mask & SETATTR_MTIME) {
916 iattr.ia_mtime = stbuf->st_mtime;
917 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
919 if (mask & SETATTR_ATIME) {
920 iattr.ia_atime = stbuf->st_atime;
921 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
923 if (mask & SETATTR_UID) {
924 iattr.ia_uid = stbuf->st_uid;
925 iattr.ia_valid |= ATTR_UID;
927 if (mask & SETATTR_GID) {
928 iattr.ia_gid = stbuf->st_gid;
929 iattr.ia_valid |= ATTR_GID;
931 if (mask & SETATTR_LEN) {
932 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
933 iattr.ia_valid |= ATTR_SIZE;
936 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
937 iattr.ia_ctime = CURRENT_TIME;
939 rc = llu_setattr_raw(ino, &iattr);
940 liblustre_wait_idle();
944 #define EXT2_LINK_MAX 32000
946 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
948 struct inode *dir = pno->p_base->pb_parent->pb_ino;
949 struct qstr *qstr = &pno->p_base->pb_name;
950 const char *name = qstr->name;
952 struct ptlrpc_request *request = NULL;
953 struct llu_sb_info *sbi = llu_i2sbi(dir);
954 struct md_op_data op_data = {{ 0 }};
958 liblustre_wait_event(0);
959 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
962 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
965 err = md_create(sbi->ll_md_exp, &op_data,
966 tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
967 current->fsuid, current->fsgid, current->cap_effective,
969 ptlrpc_req_finished(request);
970 liblustre_wait_event(0);
974 static int llu_readlink_internal(struct inode *inode,
975 struct ptlrpc_request **request,
978 struct llu_inode_info *lli = llu_i2info(inode);
979 struct llu_sb_info *sbi = llu_i2sbi(inode);
980 struct mdt_body *body;
981 struct intnl_stat *st = llu_i2stat(inode);
982 int rc, symlen = st->st_size + 1;
987 if (lli->lli_symlink_name) {
988 *symname = lli->lli_symlink_name;
989 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
993 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), NULL,
994 OBD_MD_LINKNAME, symlen, request);
996 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
1000 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
1001 LASSERT(body != NULL);
1003 if ((body->valid & OBD_MD_LINKNAME) == 0) {
1004 CERROR ("OBD_MD_LINKNAME not set on reply\n");
1005 GOTO (failed, rc = -EPROTO);
1008 LASSERT(symlen != 0);
1009 if (body->eadatasize != symlen) {
1010 CERROR("inode %llu: symlink length %d not expected %d\n",
1011 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
1012 GOTO(failed, rc = -EPROTO);
1015 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
1016 if (*symname == NULL ||
1017 strnlen(*symname, symlen) != symlen - 1) {
1018 /* not full/NULL terminated */
1019 CERROR("inode %llu: symlink not NULL terminated string"
1020 "of length %d\n", (long long)st->st_ino, symlen - 1);
1021 GOTO(failed, rc = -EPROTO);
1024 OBD_ALLOC(lli->lli_symlink_name, symlen);
1025 /* do not return an error if we cannot cache the symlink locally */
1026 if (lli->lli_symlink_name)
1027 memcpy(lli->lli_symlink_name, *symname, symlen);
1032 ptlrpc_req_finished (*request);
1036 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
1038 struct inode *inode = pno->p_base->pb_ino;
1039 struct ptlrpc_request *request;
1044 liblustre_wait_event(0);
1045 rc = llu_readlink_internal(inode, &request, &symname);
1050 strncpy(data, symname, bufsize);
1051 rc = strlen(symname);
1053 ptlrpc_req_finished(request);
1055 liblustre_wait_event(0);
1059 static int llu_iop_mknod_raw(struct pnode *pno,
1063 struct ptlrpc_request *request = NULL;
1064 struct inode *dir = pno->p_parent->p_base->pb_ino;
1065 struct llu_sb_info *sbi = llu_i2sbi(dir);
1066 struct md_op_data op_data = {{ 0 }};
1070 liblustre_wait_event(0);
1071 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
1072 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
1073 (long long)llu_i2stat(dir)->st_ino);
1075 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
1078 switch (mode & S_IFMT) {
1081 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1086 llu_prep_md_op_data(&op_data, dir, NULL,
1087 pno->p_base->pb_name.name,
1088 pno->p_base->pb_name.len, 0,
1091 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
1092 current->fsuid, current->fsgid,
1093 current->cap_effective, dev, &request);
1094 ptlrpc_req_finished(request);
1102 liblustre_wait_event(0);
1106 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1108 struct inode *src = old->p_base->pb_ino;
1109 struct inode *dir = new->p_parent->p_base->pb_ino;
1110 const char *name = new->p_base->pb_name.name;
1111 int namelen = new->p_base->pb_name.len;
1112 struct ptlrpc_request *request = NULL;
1113 struct md_op_data op_data = {{ 0 }};
1120 liblustre_wait_event(0);
1121 llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0,
1123 rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1124 ptlrpc_req_finished(request);
1125 liblustre_wait_event(0);
1131 * libsysio will clear the inode immediately after return
1133 static int llu_iop_unlink_raw(struct pnode *pno)
1135 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1136 struct qstr *qstr = &pno->p_base->pb_name;
1137 const char *name = qstr->name;
1138 int len = qstr->len;
1139 struct inode *target = pno->p_base->pb_ino;
1140 struct ptlrpc_request *request = NULL;
1141 struct md_op_data op_data = { { 0 } };
1147 liblustre_wait_event(0);
1148 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1150 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1152 rc = llu_objects_destroy(request, dir);
1153 ptlrpc_req_finished(request);
1154 liblustre_wait_idle();
1159 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1161 struct inode *src = old->p_parent->p_base->pb_ino;
1162 struct inode *tgt = new->p_parent->p_base->pb_ino;
1163 const char *oldname = old->p_base->pb_name.name;
1164 int oldnamelen = old->p_base->pb_name.len;
1165 const char *newname = new->p_base->pb_name.name;
1166 int newnamelen = new->p_base->pb_name.len;
1167 struct ptlrpc_request *request = NULL;
1168 struct md_op_data op_data = { { 0 } };
1175 liblustre_wait_event(0);
1176 llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0,
1178 rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1179 oldname, oldnamelen, newname, newnamelen,
1182 rc = llu_objects_destroy(request, src);
1185 ptlrpc_req_finished(request);
1186 liblustre_wait_idle();
1191 #ifdef _HAVE_STATVFS
1192 static int llu_statfs_internal(struct llu_sb_info *sbi,
1193 struct obd_statfs *osfs, __u64 max_age)
1195 struct obd_statfs obd_osfs;
1199 rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age, 0);
1201 CERROR("md_statfs fails: rc = %d\n", rc);
1205 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1206 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1208 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1209 &obd_statfs, max_age, 0);
1211 CERROR("obd_statfs fails: rc = %d\n", rc);
1215 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1216 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1219 osfs->os_blocks = obd_osfs.os_blocks;
1220 osfs->os_bfree = obd_osfs.os_bfree;
1221 osfs->os_bavail = obd_osfs.os_bavail;
1223 /* If we don't have as many objects free on the OST as inodes
1224 * on the MDS, we reduce the total number of inodes to
1225 * compensate, so that the "inodes in use" number is correct.
1227 if (obd_osfs.os_ffree < osfs->os_ffree) {
1228 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1230 osfs->os_ffree = obd_osfs.os_ffree;
1236 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1238 struct obd_statfs osfs;
1241 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1243 /* For now we will always get up-to-date statfs values, but in the
1244 * future we may allow some amount of caching on the client (e.g.
1245 * from QOS or lprocfs updates). */
1246 rc = llu_statfs_internal(sbi, &osfs, cfs_time_current_64() - HZ);
1250 statfs_unpack(sfs, &osfs);
1252 if (sizeof(sfs->f_blocks) == 4) {
1253 while (osfs.os_blocks > ~0UL) {
1256 osfs.os_blocks >>= 1;
1257 osfs.os_bfree >>= 1;
1258 osfs.os_bavail >>= 1;
1262 sfs->f_blocks = osfs.os_blocks;
1263 sfs->f_bfree = osfs.os_bfree;
1264 sfs->f_bavail = osfs.os_bavail;
1269 static int llu_iop_statvfs(struct pnode *pno,
1271 struct intnl_statvfs *buf)
1277 liblustre_wait_event(0);
1280 LASSERT(pno->p_base->pb_ino);
1281 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1285 /* from native driver */
1286 buf->f_bsize = fs.f_bsize; /* file system block size */
1287 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1288 buf->f_blocks = fs.f_blocks;
1289 buf->f_bfree = fs.f_bfree;
1290 buf->f_bavail = fs.f_bavail;
1291 buf->f_files = fs.f_files; /* Total number serial numbers */
1292 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1293 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1294 buf->f_fsid = fs.f_fsid.__val[1];
1295 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1296 buf->f_namemax = fs.f_namelen;
1299 liblustre_wait_event(0);
1302 #endif /* _HAVE_STATVFS */
1304 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1306 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1307 struct qstr *qstr = &pno->p_base->pb_name;
1308 const char *name = qstr->name;
1309 int len = qstr->len;
1310 struct ptlrpc_request *request = NULL;
1311 struct intnl_stat *st = llu_i2stat(dir);
1312 struct md_op_data op_data = {{ 0 }};
1316 liblustre_wait_event(0);
1317 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1318 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1320 if (st->st_nlink >= EXT2_LINK_MAX)
1323 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1326 err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0,
1327 mode | S_IFDIR, current->fsuid, current->fsgid,
1328 current->cap_effective, 0, &request);
1329 ptlrpc_req_finished(request);
1330 liblustre_wait_event(0);
1334 static int llu_iop_rmdir_raw(struct pnode *pno)
1336 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1337 struct qstr *qstr = &pno->p_base->pb_name;
1338 const char *name = qstr->name;
1339 int len = qstr->len;
1340 struct ptlrpc_request *request = NULL;
1341 struct md_op_data op_data = {{ 0 }};
1345 liblustre_wait_event(0);
1346 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1347 (long long)llu_i2stat(dir)->st_ino,
1348 llu_i2info(dir)->lli_st_generation, dir);
1350 llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR,
1352 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1353 ptlrpc_req_finished(request);
1355 liblustre_wait_event(0);
1360 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1362 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1364 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1366 /* refer to ll_file_flock() for details */
1367 static int llu_file_flock(struct inode *ino,
1369 struct file_lock *file_lock)
1371 struct llu_inode_info *lli = llu_i2info(ino);
1372 struct intnl_stat *st = llu_i2stat(ino);
1373 struct ldlm_res_id res_id =
1374 { .name = {fid_seq(&lli->lli_fid),
1375 fid_oid(&lli->lli_fid),
1376 fid_ver(&lli->lli_fid),
1378 struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1379 ldlm_flock_completion_ast, NULL, file_lock };
1381 struct lustre_handle lockh = {0};
1382 ldlm_policy_data_t flock;
1386 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1387 (unsigned long long)st->st_ino, file_lock);
1389 flock.l_flock.pid = file_lock->fl_pid;
1390 flock.l_flock.start = file_lock->fl_start;
1391 flock.l_flock.end = file_lock->fl_end;
1393 switch (file_lock->fl_type) {
1395 einfo.ei_mode = LCK_PR;
1398 einfo.ei_mode = LCK_NL;
1401 einfo.ei_mode = LCK_PW;
1404 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1411 #if F_SETLKW64 != F_SETLKW
1419 #if F_SETLK64 != F_SETLK
1423 flags = LDLM_FL_BLOCK_NOWAIT;
1427 #if F_GETLK64 != F_GETLK
1431 flags = LDLM_FL_TEST_LOCK;
1432 file_lock->fl_type = einfo.ei_mode;
1435 CERROR("unknown fcntl cmd: %d\n", cmd);
1439 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, cmd=%d, flags=%#x, mode=%u, "
1440 "start="LPX64", end="LPX64"\n", (unsigned long long)st->st_ino,
1441 flock.l_flock.pid, cmd, flags, einfo.ei_mode, flock.l_flock.start,
1445 struct lmv_obd *lmv;
1446 struct obd_device *lmv_obd;
1447 lmv_obd = class_exp2obd(llu_i2mdexp(ino));
1448 lmv = &lmv_obd->u.lmv;
1450 if (lmv->desc.ld_tgt_count < 1)
1451 RETURN(rc = -ENODEV);
1453 if (lmv->tgts[0].ltd_exp != NULL)
1454 rc = ldlm_cli_enqueue(lmv->tgts[0].ltd_exp, NULL, &einfo, &res_id,
1455 &flock, &flags, NULL, 0, NULL, &lockh, 0);
1462 static int assign_type(struct file_lock *fl, int type)
1475 static int flock_to_posix_lock(struct inode *ino,
1476 struct file_lock *fl,
1479 switch (l->l_whence) {
1480 /* XXX: only SEEK_SET is supported in lustre */
1488 fl->fl_end = l->l_len - 1;
1492 fl->fl_end = OFFSET_MAX;
1494 fl->fl_pid = getpid();
1495 fl->fl_flags = FL_POSIX;
1496 fl->fl_notify = NULL;
1497 fl->fl_insert = NULL;
1498 fl->fl_remove = NULL;
1499 /* XXX: these fields can't be filled with suitable values,
1500 but I think lustre doesn't use them.
1502 fl->fl_owner = NULL;
1505 return assign_type(fl, l->l_type);
1508 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1510 struct file_lock fl;
1514 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1517 error = flock_to_posix_lock(ino, &fl, flock);
1521 error = llu_file_flock(ino, F_GETLK, &fl);
1525 flock->l_type = F_UNLCK;
1526 if (fl.fl_type != F_UNLCK) {
1527 flock->l_pid = fl.fl_pid;
1528 flock->l_start = fl.fl_start;
1529 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1530 fl.fl_end - fl.fl_start + 1;
1531 flock->l_whence = SEEK_SET;
1532 flock->l_type = fl.fl_type;
1539 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1541 struct file_lock fl;
1542 int flags = llu_i2info(ino)->lli_open_flags + 1;
1545 error = flock_to_posix_lock(ino, &fl, flock);
1548 if (cmd == F_SETLKW)
1549 fl.fl_flags |= FL_SLEEP;
1552 switch (flock->l_type) {
1554 if (!(flags & FMODE_READ))
1558 if (!(flags & FMODE_WRITE))
1568 error = llu_file_flock(ino, cmd, &fl);
1576 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1578 struct llu_inode_info *lli = llu_i2info(ino);
1580 struct flock *flock;
1583 liblustre_wait_event(0);
1586 *rtn = lli->lli_open_flags;
1589 flags = va_arg(ap, long);
1590 flags &= FCNTL_FLMASK;
1591 if (flags & FCNTL_FLMASK_INVALID) {
1592 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1593 "the O_NONBLOCK or O_ASYNC flags. "
1594 "Please fix your application.\n");
1599 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1600 (lli->lli_open_flags & ~FCNTL_FLMASK);
1605 #if F_GETLK64 != F_GETLK
1609 flock = va_arg(ap, struct flock *);
1610 err = llu_fcntl_getlk(ino, flock);
1615 #if F_SETLKW64 != F_SETLKW
1621 #if F_SETLK64 != F_SETLK
1625 flock = va_arg(ap, struct flock *);
1626 err = llu_fcntl_setlk(ino, cmd, flock);
1630 CERROR("unsupported fcntl cmd %x\n", cmd);
1636 liblustre_wait_event(0);
1640 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1642 struct llu_inode_info *lli = llu_i2info(inode);
1643 struct ll_file_data *fd = lli->lli_file_data;
1644 ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1645 .end = OBD_OBJECT_EOF}};
1646 struct lustre_handle lockh = { 0 };
1647 struct lov_stripe_md *lsm = lli->lli_smd;
1652 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1656 policy.l_extent.gid = arg;
1657 if (lli->lli_open_flags & O_NONBLOCK)
1658 flags = LDLM_FL_BLOCK_NOWAIT;
1660 err = llu_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh,
1665 fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1667 memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1672 static int llu_put_grouplock(struct inode *inode, unsigned long arg)
1674 struct llu_inode_info *lli = llu_i2info(inode);
1675 struct ll_file_data *fd = lli->lli_file_data;
1676 struct lov_stripe_md *lsm = lli->lli_smd;
1680 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1683 if (fd->fd_gid != arg)
1686 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1688 err = llu_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1693 memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1698 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1700 struct llu_sb_info *sbi = llu_i2sbi(ino);
1701 struct ptlrpc_request *request = NULL;
1702 struct md_op_data op_data = {{ 0 }};
1703 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1706 llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0,
1709 LASSERT(sizeof(lum) == sizeof(*lump));
1710 LASSERT(sizeof(lum.lmm_objects[0]) ==
1711 sizeof(lump->lmm_objects[0]));
1712 rc = copy_from_user(&lum, lump, sizeof(lum));
1716 if (lum.lmm_magic != LOV_USER_MAGIC)
1719 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
1720 lustre_swab_lov_user_md(&lum);
1722 /* swabbing is done in lov_setstripe() on server side */
1723 rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1724 sizeof(lum), NULL, 0, &request, NULL);
1726 ptlrpc_req_finished(request);
1727 if (rc != -EPERM && rc != -EACCES)
1728 CERROR("md_setattr fails: rc = %d\n", rc);
1731 ptlrpc_req_finished(request);
1736 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1737 struct lov_user_md *lum, int lum_size)
1739 struct llu_sb_info *sbi = llu_i2sbi(ino);
1740 struct llu_inode_info *lli = llu_i2info(ino);
1741 struct llu_inode_info *lli2 = NULL;
1742 struct lov_stripe_md *lsm;
1743 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1744 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1745 llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL };
1747 struct ptlrpc_request *req = NULL;
1748 struct lustre_md md;
1749 struct md_op_data data = {{ 0 }};
1750 struct lustre_handle lockh;
1756 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1757 PFID(&lli->lli_fid));
1761 OBD_ALLOC(lli2, sizeof(struct llu_inode_info));
1765 memcpy(lli2, lli, sizeof(struct llu_inode_info));
1766 lli2->lli_open_count = 0;
1767 lli2->lli_it = NULL;
1768 lli2->lli_file_data = NULL;
1769 lli2->lli_smd = NULL;
1770 lli2->lli_symlink_name = NULL;
1771 ino->i_private = lli2;
1773 llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1776 rc = md_enqueue(sbi->ll_md_exp, &einfo, &oit, &data,
1777 &lockh, lum, lum_size, LDLM_FL_INTENT_ONLY);
1781 req = oit.d.lustre.it_data;
1782 rc = it_open_error(DISP_IT_EXECD, &oit);
1788 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1794 rc = md_get_lustre_md(sbi->ll_md_exp, req,
1795 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1799 llu_update_inode(ino, md.body, md.lsm);
1800 lli->lli_smd = lli2->lli_smd;
1801 lli2->lli_smd = NULL;
1803 llu_local_open(lli2, &oit);
1805 /* release intent */
1806 if (lustre_handle_is_used(&lockh))
1807 ldlm_lock_decref(&lockh, LCK_CR);
1809 ptlrpc_req_finished(req);
1812 rc = llu_file_release(ino);
1814 ino->i_private = lli;
1816 OBD_FREE(lli2, sizeof(struct llu_inode_info));
1818 ptlrpc_req_finished(req);
1822 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1824 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1826 int flags = FMODE_WRITE;
1829 LASSERT(sizeof(lum) == sizeof(*lump));
1830 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1831 rc = copy_from_user(&lum, lump, sizeof(lum));
1835 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1839 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1841 struct intnl_stat *st = llu_i2stat(ino);
1842 if (S_ISREG(st->st_mode))
1843 return llu_lov_file_setstripe(ino, arg);
1844 if (S_ISDIR(st->st_mode))
1845 return llu_lov_dir_setstripe(ino, arg);
1850 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1852 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1857 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1861 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1867 liblustre_wait_event(0);
1870 case LL_IOC_GROUP_LOCK:
1871 arg = va_arg(ap, unsigned long);
1872 rc = llu_get_grouplock(ino, arg);
1874 case LL_IOC_GROUP_UNLOCK:
1875 arg = va_arg(ap, unsigned long);
1876 rc = llu_put_grouplock(ino, arg);
1878 case LL_IOC_LOV_SETSTRIPE:
1879 arg = va_arg(ap, unsigned long);
1880 rc = llu_lov_setstripe(ino, arg);
1882 case LL_IOC_LOV_GETSTRIPE:
1883 arg = va_arg(ap, unsigned long);
1884 rc = llu_lov_getstripe(ino, arg);
1887 CERROR("did not support ioctl cmd %lx\n", request);
1892 liblustre_wait_event(0);
1897 * we already do syncronous read/write
1899 static int llu_iop_sync(struct inode *inode)
1901 liblustre_wait_event(0);
1905 static int llu_iop_datasync(struct inode *inode)
1907 liblustre_wait_event(0);
1911 struct filesys_ops llu_filesys_ops =
1913 fsop_gone: llu_fsop_gone,
1916 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1918 struct inode *inode;
1920 struct file_identifier fileid = {&fid, sizeof(fid)};
1922 if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1923 (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1924 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1926 return ERR_PTR(-EPERM);
1929 /* try to find existing inode */
1930 fid = md->body->fid1;
1932 inode = _sysio_i_find(fs, &fileid);
1934 if (inode->i_zombie/* ||
1935 lli->lli_st_generation != md->body->generation*/) {
1939 llu_update_inode(inode, md->body, md->lsm);
1944 inode = llu_new_inode(fs, &fid);
1946 llu_update_inode(inode, md->body, md->lsm);
1952 llu_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
1954 struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC };
1955 __u32 valsize = sizeof(struct lov_desc);
1956 int rc, easize, def_easize, cookiesize;
1957 struct lov_desc desc;
1961 rc = obd_get_info(dt_exp, sizeof(KEY_LOVDESC), KEY_LOVDESC,
1966 stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
1967 lsm.lsm_stripe_count = stripes;
1968 easize = obd_size_diskmd(dt_exp, &lsm);
1970 lsm.lsm_stripe_count = desc.ld_default_stripe_count;
1971 def_easize = obd_size_diskmd(dt_exp, &lsm);
1973 cookiesize = stripes * sizeof(struct llog_cookie);
1975 CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
1976 easize, cookiesize);
1978 rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize);
1983 llu_fsswop_mount(const char *source,
1985 const void *data __IS_UNUSED,
1986 struct pnode *tocover,
1987 struct mount **mntp)
1991 struct pnode_base *rootpb;
1992 struct obd_device *obd;
1993 struct lu_fid rootfid;
1994 struct llu_sb_info *sbi;
1995 struct obd_statfs osfs;
1996 static struct qstr noname = { NULL, 0, 0 };
1997 struct ptlrpc_request *request = NULL;
1998 struct lustre_handle md_conn = {0, };
1999 struct lustre_handle dt_conn = {0, };
2000 struct lustre_md md;
2002 struct config_llog_instance cfg = {0, };
2003 char ll_instance[sizeof(sbi) * 2 + 1];
2004 struct lustre_profile *lprof;
2005 char *zconf_mgsnid, *zconf_profile;
2006 char *osc = NULL, *mdc = NULL;
2007 int async = 1, err = -EINVAL;
2008 struct obd_connect_data ocd = {0,};
2012 if (ll_parse_mount_target(source,
2015 CERROR("mal-formed target %s\n", source);
2018 if (!zconf_mgsnid || !zconf_profile) {
2019 printf("Liblustre: invalid target %s\n", source);
2022 /* allocate & initialize sbi */
2023 OBD_ALLOC(sbi, sizeof(*sbi));
2027 CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
2028 ll_generate_random_uuid(uuid);
2029 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
2031 /* generate a string unique to this super, let's try
2032 the address of the super itself.*/
2033 sprintf(ll_instance, "%p", sbi);
2035 /* retrive & parse config log */
2036 cfg.cfg_instance = ll_instance;
2037 cfg.cfg_uuid = sbi->ll_sb_uuid;
2038 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
2040 CERROR("Unable to process log: %s\n", zconf_profile);
2041 GOTO(out_free, err);
2044 lprof = class_get_profile(zconf_profile);
2045 if (lprof == NULL) {
2046 CERROR("No profile found: %s\n", zconf_profile);
2047 GOTO(out_free, err = -EINVAL);
2049 OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
2050 sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
2052 OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
2053 sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
2057 GOTO(out_free, err = -EINVAL);
2061 GOTO(out_free, err = -EINVAL);
2064 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
2070 obd = class_name2obd(mdc);
2072 CERROR("MDC %s: not setup or attached\n", mdc);
2073 GOTO(out_free, err = -EINVAL);
2075 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2076 sizeof(async), &async, NULL);
2078 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
2080 #ifdef LIBLUSTRE_POSIX_ACL
2081 ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
2083 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2084 ocd.ocd_version = LUSTRE_VERSION_CODE;
2087 err = obd_connect(NULL, &md_conn, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2089 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2090 GOTO(out_free, err);
2092 sbi->ll_md_exp = class_conn2export(&md_conn);
2094 err = obd_statfs(obd, &osfs, 100000000, 0);
2099 * FIXME fill fs stat data into sbi here!!! FIXME
2103 obd = class_name2obd(osc);
2105 CERROR("OSC %s: not setup or attached\n", osc);
2106 GOTO(out_md, err = -EINVAL);
2108 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2109 sizeof(async), &async, NULL);
2111 obd->obd_upcall.onu_owner = &sbi->ll_lco;
2112 obd->obd_upcall.onu_upcall = ll_ocd_update;
2114 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2115 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK |
2117 ocd.ocd_version = LUSTRE_VERSION_CODE;
2118 err = obd_connect(NULL, &dt_conn, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2120 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2123 sbi->ll_dt_exp = class_conn2export(&dt_conn);
2124 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2126 err = obd_register_lock_cancel_cb(sbi->ll_dt_exp,
2127 llu_extent_lock_cancel_cb);
2129 CERROR("cannot register lock cancel callback: rc = %d\n", err);
2133 llu_init_ea_size(sbi->ll_md_exp, sbi->ll_dt_exp);
2135 err = md_getstatus(sbi->ll_md_exp, &rootfid, NULL);
2137 CERROR("cannot mds_connect: rc = %d\n", err);
2138 GOTO(out_lock_cn_cb, err);
2140 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&rootfid));
2141 sbi->ll_root_fid = rootfid;
2143 /* fetch attr of root inode */
2144 err = md_getattr(sbi->ll_md_exp, &rootfid, NULL,
2145 OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
2147 CERROR("md_getattr failed for root: rc = %d\n", err);
2148 GOTO(out_lock_cn_cb, err);
2151 err = md_get_lustre_md(sbi->ll_md_exp, request,
2152 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2154 CERROR("failed to understand root inode md: rc = %d\n",err);
2155 GOTO(out_request, err);
2158 LASSERT(fid_is_sane(&sbi->ll_root_fid));
2160 root = llu_iget(fs, &md);
2161 if (!root || IS_ERR(root)) {
2162 CERROR("fail to generate root inode\n");
2163 GOTO(out_request, err = -EBADF);
2167 * Generate base path-node for root.
2169 rootpb = _sysio_pb_new(&noname, NULL, root);
2175 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2177 _sysio_pb_gone(rootpb);
2181 ptlrpc_req_finished(request);
2183 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2184 liblustre_wait_idle();
2189 _sysio_i_gone(root);
2191 ptlrpc_req_finished(request);
2193 obd_unregister_lock_cancel_cb(sbi->ll_dt_exp,
2194 llu_extent_lock_cancel_cb);
2196 obd_disconnect(sbi->ll_dt_exp);
2198 obd_disconnect(sbi->ll_md_exp);
2201 OBD_FREE(osc, strlen(osc) + 1);
2203 OBD_FREE(mdc, strlen(mdc) + 1);
2204 OBD_FREE(sbi, sizeof(*sbi));
2205 liblustre_wait_idle();
2209 struct fssw_ops llu_fssw_ops = {
2213 static struct inode_ops llu_inode_ops = {
2214 inop_lookup: llu_iop_lookup,
2215 inop_getattr: llu_iop_getattr,
2216 inop_setattr: llu_iop_setattr,
2217 inop_filldirentries: llu_iop_filldirentries,
2218 inop_mkdir: llu_iop_mkdir_raw,
2219 inop_rmdir: llu_iop_rmdir_raw,
2220 inop_symlink: llu_iop_symlink_raw,
2221 inop_readlink: llu_iop_readlink,
2222 inop_open: llu_iop_open,
2223 inop_close: llu_iop_close,
2224 inop_link: llu_iop_link_raw,
2225 inop_unlink: llu_iop_unlink_raw,
2226 inop_rename: llu_iop_rename_raw,
2227 inop_pos: llu_iop_pos,
2228 inop_read: llu_iop_read,
2229 inop_write: llu_iop_write,
2230 inop_iodone: llu_iop_iodone,
2231 inop_fcntl: llu_iop_fcntl,
2232 inop_sync: llu_iop_sync,
2233 inop_datasync: llu_iop_datasync,
2234 inop_ioctl: llu_iop_ioctl,
2235 inop_mknod: llu_iop_mknod_raw,
2236 #ifdef _HAVE_STATVFS
2237 inop_statvfs: llu_iop_statvfs,
2239 inop_gone: llu_iop_gone,