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_disconnect(sbi->ll_dt_exp);
99 obd_disconnect(sbi->ll_md_exp);
101 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
102 class_manual_cleanup(obd);
104 OBD_FREE(sbi, sizeof(*sbi));
106 liblustre_wait_idle();
110 static struct inode_ops llu_inode_ops;
112 static ldlm_mode_t llu_take_md_lock(struct inode *inode, __u64 bits,
113 struct lustre_handle *lockh)
115 ldlm_policy_data_t policy = { .l_inodebits = {bits}};
121 fid = &llu_i2info(inode)->lli_fid;
122 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
124 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
125 rc = md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
126 LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
130 void llu_update_inode(struct inode *inode, struct mdt_body *body,
131 struct lov_stripe_md *lsm)
133 struct llu_inode_info *lli = llu_i2info(inode);
134 struct intnl_stat *st = llu_i2stat(inode);
136 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
138 if (lli->lli_smd == NULL) {
140 lli->lli_maxbytes = lsm->lsm_maxbytes;
141 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
142 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
144 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
145 CERROR("lsm mismatch for inode %lld\n",
146 (long long)st->st_ino);
152 if (body->valid & OBD_MD_FLMTIME &&
153 body->mtime > LTIME_S(st->st_mtime))
154 LTIME_S(st->st_mtime) = body->mtime;
155 if (body->valid & OBD_MD_FLATIME &&
156 body->atime > LTIME_S(st->st_atime))
157 LTIME_S(st->st_atime) = body->atime;
159 /* mtime is always updated with ctime, but can be set in past.
160 As write and utime(2) may happen within 1 second, and utime's
161 mtime has a priority over write's one, so take mtime from mds
162 for the same ctimes. */
163 if (body->valid & OBD_MD_FLCTIME &&
164 body->ctime >= LTIME_S(st->st_ctime)) {
165 LTIME_S(st->st_ctime) = body->ctime;
166 if (body->valid & OBD_MD_FLMTIME)
167 LTIME_S(st->st_mtime) = body->mtime;
169 if (body->valid & OBD_MD_FLMODE)
170 st->st_mode = (st->st_mode & S_IFMT)|(body->mode & ~S_IFMT);
171 if (body->valid & OBD_MD_FLTYPE)
172 st->st_mode = (st->st_mode & ~S_IFMT)|(body->mode & S_IFMT);
173 if (S_ISREG(st->st_mode))
174 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
176 st->st_blksize = 4096;
177 if (body->valid & OBD_MD_FLUID)
178 st->st_uid = body->uid;
179 if (body->valid & OBD_MD_FLGID)
180 st->st_gid = body->gid;
181 if (body->valid & OBD_MD_FLNLINK)
182 st->st_nlink = body->nlink;
183 if (body->valid & OBD_MD_FLRDEV)
184 st->st_rdev = body->rdev;
185 if (body->valid & OBD_MD_FLFLAGS)
186 lli->lli_st_flags = body->flags;
187 if (body->valid & OBD_MD_FLSIZE) {
188 if ((llu_i2sbi(inode)->ll_lco.lco_flags & OBD_CONNECT_SOM) &&
189 S_ISREG(st->st_mode) && lli->lli_smd) {
190 struct lustre_handle lockh;
193 /* As it is possible a blocking ast has been processed
194 * by this time, we need to check there is an UPDATE
195 * lock on the client and set LLIF_MDS_SIZE_LOCK holding
197 mode = llu_take_md_lock(inode, MDS_INODELOCK_UPDATE,
200 st->st_size = body->size;
201 lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
202 ldlm_lock_decref(&lockh, mode);
205 st->st_size = body->size;
208 if (body->valid & OBD_MD_FLBLOCKS)
209 st->st_blocks = body->blocks;
213 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
215 struct llu_inode_info *lli = llu_i2info(dst);
216 struct intnl_stat *st = llu_i2stat(dst);
218 valid &= src->o_valid;
220 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
221 CDEBUG(D_INODE,"valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
223 LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
224 (long)src->o_mtime, (long)src->o_ctime);
226 if (valid & OBD_MD_FLATIME)
227 LTIME_S(st->st_atime) = src->o_atime;
228 if (valid & OBD_MD_FLMTIME)
229 LTIME_S(st->st_mtime) = src->o_mtime;
230 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
231 LTIME_S(st->st_ctime) = src->o_ctime;
232 if (valid & OBD_MD_FLSIZE)
233 st->st_size = src->o_size;
234 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
235 st->st_blocks = src->o_blocks;
236 if (valid & OBD_MD_FLBLKSZ)
237 st->st_blksize = src->o_blksize;
238 if (valid & OBD_MD_FLTYPE)
239 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
240 if (valid & OBD_MD_FLMODE)
241 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
242 if (valid & OBD_MD_FLUID)
243 st->st_uid = src->o_uid;
244 if (valid & OBD_MD_FLGID)
245 st->st_gid = src->o_gid;
246 if (valid & OBD_MD_FLFLAGS)
247 lli->lli_st_flags = src->o_flags;
250 #define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
251 #define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
253 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
255 struct llu_inode_info *lli = llu_i2info(src);
256 struct intnl_stat *st = llu_i2stat(src);
257 obd_flag newvalid = 0;
259 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
260 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
261 valid, LTIME_S(st->st_mtime),
262 LTIME_S(st->st_ctime));
264 if (valid & OBD_MD_FLATIME) {
265 dst->o_atime = LTIME_S(st->st_atime);
266 newvalid |= OBD_MD_FLATIME;
268 if (valid & OBD_MD_FLMTIME) {
269 dst->o_mtime = LTIME_S(st->st_mtime);
270 newvalid |= OBD_MD_FLMTIME;
272 if (valid & OBD_MD_FLCTIME) {
273 dst->o_ctime = LTIME_S(st->st_ctime);
274 newvalid |= OBD_MD_FLCTIME;
276 if (valid & OBD_MD_FLSIZE) {
277 dst->o_size = st->st_size;
278 newvalid |= OBD_MD_FLSIZE;
280 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
281 dst->o_blocks = st->st_blocks;
282 newvalid |= OBD_MD_FLBLOCKS;
284 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
285 dst->o_blksize = st->st_blksize;
286 newvalid |= OBD_MD_FLBLKSZ;
288 if (valid & OBD_MD_FLTYPE) {
289 dst->o_mode = (dst->o_mode & S_IALLUGO)|(st->st_mode & S_IFMT);
290 newvalid |= OBD_MD_FLTYPE;
292 if (valid & OBD_MD_FLMODE) {
293 dst->o_mode = (dst->o_mode & S_IFMT)|(st->st_mode & S_IALLUGO);
294 newvalid |= OBD_MD_FLMODE;
296 if (valid & OBD_MD_FLUID) {
297 dst->o_uid = st->st_uid;
298 newvalid |= OBD_MD_FLUID;
300 if (valid & OBD_MD_FLGID) {
301 dst->o_gid = st->st_gid;
302 newvalid |= OBD_MD_FLGID;
304 if (valid & OBD_MD_FLFLAGS) {
305 dst->o_flags = lli->lli_st_flags;
306 newvalid |= OBD_MD_FLFLAGS;
308 if (valid & OBD_MD_FLGENER) {
309 dst->o_generation = lli->lli_st_generation;
310 newvalid |= OBD_MD_FLGENER;
312 if (valid & OBD_MD_FLFID) {
313 dst->o_fid = st->st_ino;
314 newvalid |= OBD_MD_FLFID;
317 dst->o_valid |= newvalid;
321 * really does the getattr on the inode and updates its fields
323 int llu_inode_getattr(struct inode *inode, struct obdo *obdo)
325 struct llu_inode_info *lli = llu_i2info(inode);
326 struct ptlrpc_request_set *set;
327 struct lov_stripe_md *lsm = lli->lli_smd;
328 struct obd_info oinfo = { { { 0 } } };
336 oinfo.oi_oa->o_id = lsm->lsm_object_id;
337 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
338 oinfo.oi_oa->o_mode = S_IFREG;
339 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
340 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
341 OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
342 OBD_MD_FLCTIME | OBD_MD_FLGROUP;
344 set = ptlrpc_prep_set();
346 CERROR ("ENOMEM allocing request set\n");
349 rc = obd_getattr_async(llu_i2obdexp(inode), &oinfo, set);
351 rc = ptlrpc_set_wait(set);
352 ptlrpc_set_destroy(set);
357 oinfo.oi_oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
358 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
361 obdo_refresh_inode(inode, oinfo.oi_oa, oinfo.oi_oa->o_valid);
362 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %Lu, "
363 "blksize %Lu\n", lli->lli_smd->lsm_object_id,
364 (long long unsigned)llu_i2stat(inode)->st_size,
365 (long long unsigned)llu_i2stat(inode)->st_blocks,
366 (long long unsigned)llu_i2stat(inode)->st_blksize);
370 static struct inode* llu_new_inode(struct filesys *fs,
374 struct llu_inode_info *lli;
375 struct intnl_stat st = {
378 #ifndef AUTOMOUNT_FILE_NAME
379 .st_mode = fid->f_type & S_IFMT,
381 .st_mode = fid->f_type /* all of the bits! */
384 /* FIXME: fix this later */
391 OBD_ALLOC(lli, sizeof(*lli));
395 /* initialize lli here */
396 lli->lli_sbi = llu_fs2sbi(fs);
398 lli->lli_symlink_name = NULL;
400 lli->lli_maxbytes = (__u64)(~0UL);
401 lli->lli_file_data = NULL;
403 lli->lli_sysio_fid.fid_data = &lli->lli_fid;
404 lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
407 /* file identifier is needed by functions like _sysio_i_find() */
408 inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
409 &st, 0, &llu_inode_ops, lli);
412 OBD_FREE(lli, sizeof(*lli));
417 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
419 struct lustre_handle lockh;
420 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
427 fid = &llu_i2info(inode)->lli_fid;
428 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
430 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
431 if (md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
432 LCK_CR|LCK_CW|LCK_PR|LCK_PW, &lockh)) {
438 static int llu_inode_revalidate(struct inode *inode)
440 struct lov_stripe_md *lsm = NULL;
444 CERROR("REPORT THIS LINE TO PETER\n");
448 if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
450 struct ptlrpc_request *req = NULL;
451 struct llu_sb_info *sbi = llu_i2sbi(inode);
452 unsigned long valid = OBD_MD_FLGETATTR;
455 /* Why don't we update all valid MDS fields here, if we're
456 * doing an RPC anyways? -phil */
457 if (S_ISREG(llu_i2stat(inode)->st_mode)) {
458 ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
459 valid |= OBD_MD_FLEASIZE;
461 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
462 NULL, valid, ealen, &req);
464 CERROR("failure %d inode %llu\n", rc,
465 (long long)llu_i2stat(inode)->st_ino);
468 rc = md_get_lustre_md(sbi->ll_md_exp, req,
469 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
471 /* XXX Too paranoid? */
472 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
473 !((md.body->valid & OBD_MD_FLNLINK) &&
474 (md.body->nlink == 0))) {
475 CERROR("Asked for %s eadata but got %s (%d)\n",
476 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
477 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
478 md.body->eadatasize);
481 ptlrpc_req_finished(req);
486 llu_update_inode(inode, md.body, md.lsm);
487 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
488 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
489 ptlrpc_req_finished(req);
492 lsm = llu_i2info(inode)->lli_smd;
493 if (!lsm) /* object not yet allocated, don't validate size */
496 /* ll_glimpse_size will prefer locally cached writes if they extend
498 RETURN(llu_glimpse_size(inode));
501 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
503 *b = *llu_i2stat(ino);
506 static int llu_iop_getattr(struct pnode *pno,
508 struct intnl_stat *b)
513 liblustre_wait_event(0);
517 LASSERT(pno->p_base->pb_ino);
518 ino = pno->p_base->pb_ino;
520 LASSERT(!pno || pno->p_base->pb_ino == ino);
523 /* libsysio might call us directly without intent lock,
524 * we must re-fetch the attrs here
526 rc = llu_inode_revalidate(ino);
528 copy_stat_buf(ino, b);
529 LASSERT(!llu_i2info(ino)->lli_it);
532 liblustre_wait_event(0);
536 static int null_if_equal(struct ldlm_lock *lock, void *data)
538 if (data == lock->l_ast_data) {
539 lock->l_ast_data = NULL;
541 if (lock->l_req_mode != lock->l_granted_mode)
542 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
545 return LDLM_ITER_CONTINUE;
548 void llu_clear_inode(struct inode *inode)
550 struct llu_inode_info *lli = llu_i2info(inode);
551 struct llu_sb_info *sbi = llu_i2sbi(inode);
554 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
555 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
558 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
559 md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
560 null_if_equal, inode);
563 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
564 null_if_equal, inode);
567 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
571 if (lli->lli_symlink_name) {
572 OBD_FREE(lli->lli_symlink_name,
573 strlen(lli->lli_symlink_name) + 1);
574 lli->lli_symlink_name = NULL;
580 void llu_iop_gone(struct inode *inode)
582 struct llu_inode_info *lli = llu_i2info(inode);
585 liblustre_wait_event(0);
586 llu_clear_inode(inode);
588 OBD_FREE(lli, sizeof(*lli));
592 static int inode_setattr(struct inode * inode, struct iattr * attr)
594 unsigned int ia_valid = attr->ia_valid;
595 struct intnl_stat *st = llu_i2stat(inode);
599 * inode_setattr() is only ever invoked with ATTR_SIZE (by
600 * llu_setattr_raw()) when file has no bodies. Check this.
602 LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
604 if (ia_valid & ATTR_SIZE)
605 st->st_size = attr->ia_size;
606 if (ia_valid & ATTR_UID)
607 st->st_uid = attr->ia_uid;
608 if (ia_valid & ATTR_GID)
609 st->st_gid = attr->ia_gid;
610 if (ia_valid & ATTR_ATIME)
611 st->st_atime = attr->ia_atime;
612 if (ia_valid & ATTR_MTIME)
613 st->st_mtime = attr->ia_mtime;
614 if (ia_valid & ATTR_CTIME)
615 st->st_ctime = attr->ia_ctime;
616 if (ia_valid & ATTR_MODE) {
617 st->st_mode = attr->ia_mode;
618 if (!in_group_p(st->st_gid) && !capable(CAP_FSETID))
619 st->st_mode &= ~S_ISGID;
621 /* mark_inode_dirty(inode); */
625 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
626 struct md_open_data **mod)
629 struct llu_sb_info *sbi = llu_i2sbi(inode);
630 struct ptlrpc_request *request = NULL;
634 llu_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0, LUSTRE_OPC_ANY);
635 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL,
639 ptlrpc_req_finished(request);
640 if (rc != -EPERM && rc != -EACCES)
641 CERROR("md_setattr fails: rc = %d\n", rc);
645 rc = md_get_lustre_md(sbi->ll_md_exp, request,
646 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
648 ptlrpc_req_finished(request);
652 /* We call inode_setattr to adjust timestamps.
653 * If there is at least some data in file, we cleared ATTR_SIZE
654 * above to avoid invoking vmtruncate, otherwise it is important
655 * to call vmtruncate in inode_setattr to update inode->i_size
657 inode_setattr(inode, &op_data->op_attr);
658 llu_update_inode(inode, md.body, md.lsm);
659 ptlrpc_req_finished(request);
664 /* Close IO epoch and send Size-on-MDS attribute update. */
665 static int llu_setattr_done_writing(struct inode *inode,
666 struct md_op_data *op_data,
667 struct md_open_data *mod)
669 struct llu_inode_info *lli = llu_i2info(inode);
670 struct intnl_stat *st = llu_i2stat(inode);
674 LASSERT(op_data != NULL);
675 if (!S_ISREG(st->st_mode))
678 /* XXX: pass och here for the recovery purpose. */
679 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
680 op_data->op_ioepoch, PFID(&lli->lli_fid));
682 op_data->op_flags = MF_EPOCH_CLOSE | MF_SOM_CHANGE;
683 rc = md_done_writing(llu_i2sbi(inode)->ll_md_exp, op_data, mod);
685 /* MDS has instructed us to obtain Size-on-MDS attribute
686 * from OSTs and send setattr to back to MDS. */
687 rc = llu_sizeonmds_update(inode, mod, &op_data->op_handle,
688 op_data->op_ioepoch);
690 CERROR("inode %llu mdc truncate failed: rc = %d\n",
696 /* If this inode has objects allocated to it (lsm != NULL), then the OST
697 * object(s) determine the file size and mtime. Otherwise, the MDS will
698 * keep these values until such a time that objects are allocated for it.
699 * We do the MDS operations first, as it is checking permissions for us.
700 * We don't to the MDS RPC if there is nothing that we want to store there,
701 * otherwise there is no harm in updating mtime/atime on the MDS if we are
702 * going to do an RPC anyways.
704 * If we are doing a truncate, we will send the mtime and ctime updates
705 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
706 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
709 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
711 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
712 struct llu_sb_info *sbi = llu_i2sbi(inode);
713 struct intnl_stat *st = llu_i2stat(inode);
714 int ia_valid = attr->ia_valid;
715 struct md_op_data op_data = { { 0 } };
716 struct md_open_data *mod = NULL;
720 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
722 if (ia_valid & ATTR_SIZE) {
723 if (attr->ia_size > ll_file_maxbytes(inode)) {
724 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
725 (long long)attr->ia_size,
726 ll_file_maxbytes(inode));
730 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
733 /* We mark all of the fields "set" so MDS/OST does not re-set them */
734 if (attr->ia_valid & ATTR_CTIME) {
735 attr->ia_ctime = CURRENT_TIME;
736 attr->ia_valid |= ATTR_CTIME_SET;
738 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
739 attr->ia_atime = CURRENT_TIME;
740 attr->ia_valid |= ATTR_ATIME_SET;
742 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
743 attr->ia_mtime = CURRENT_TIME;
744 attr->ia_valid |= ATTR_MTIME_SET;
746 if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
747 /* To avoid stale mtime on mds, obtain it from ost and send
749 rc = llu_glimpse_size(inode);
753 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
754 attr->ia_mtime = inode->i_stbuf.st_mtime;
757 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
758 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
759 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
760 LTIME_S(CURRENT_TIME));
762 /* NB: ATTR_SIZE will only be set after this point if the size
763 * resides on the MDS, ie, this file has no objects. */
765 attr->ia_valid &= ~ATTR_SIZE;
767 /* If only OST attributes being set on objects, don't do MDS RPC.
768 * In that case, we need to check permissions and update the local
769 * inode ourselves so we can call obdo_from_inode() always. */
770 if (ia_valid & (lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
771 memcpy(&op_data.op_attr, attr, sizeof(*attr));
773 /* Open epoch for truncate. */
774 if (ia_valid & ATTR_SIZE)
775 op_data.op_flags = MF_EPOCH_OPEN;
776 rc = llu_md_setattr(inode, &op_data, &mod);
780 if (op_data.op_ioepoch)
781 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID" for "
782 "truncate\n", op_data.op_ioepoch,
783 PFID(&llu_i2info(inode)->lli_fid));
785 if (!lsm || !S_ISREG(st->st_mode)) {
786 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
790 /* The OST doesn't check permissions, but the alternative is
791 * a gratuitous RPC to the MDS. We already rely on the client
792 * to do read/write/truncate permission checks, so is mtime OK?
794 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
795 /* from sys_utime() */
796 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
797 if (current->fsuid != st->st_uid &&
798 (rc = ll_permission(inode, MAY_WRITE)) != 0)
801 /* from inode_change_ok() */
802 if (current->fsuid != st->st_uid &&
803 !capable(CAP_FOWNER))
809 /* Won't invoke llu_vmtruncate(), as we already cleared
811 inode_setattr(inode, attr);
814 if (ia_valid & ATTR_SIZE) {
815 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
817 struct lustre_handle lockh = { 0, };
818 struct lustre_handle match_lockh = { 0, };
821 int flags = LDLM_FL_TEST_LOCK; /* for assertion check below */
825 /* check that there are no matching locks */
826 LASSERT(obd_match(sbi->ll_dt_exp, lsm, LDLM_EXTENT, &policy,
827 LCK_PW, &flags, inode, &match_lockh) <= 0);
829 /* XXX when we fix the AST intents to pass the discard-range
830 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
832 flags = (attr->ia_size == 0) ? LDLM_AST_DISCARD_DATA : 0;
834 if (sbi->ll_lco.lco_flags & OBD_CONNECT_TRUNCLOCK) {
836 obd_flags = OBD_FL_TRUNCLOCK;
837 CDEBUG(D_INODE, "delegating locking to the OST");
843 /* with lock_mode == LK_NL no lock is taken. */
844 rc = llu_extent_lock(NULL, inode, lsm, lock_mode, &policy,
846 if (rc != ELDLM_OK) {
848 GOTO(out, rc = -ENOLCK);
851 rc = llu_vmtruncate(inode, attr->ia_size, obd_flags);
853 /* unlock now as we don't mind others file lockers racing with
854 * the mds updates below? */
855 err = llu_extent_unlock(NULL, inode, lsm, lock_mode, &lockh);
857 CERROR("llu_extent_unlock failed: %d\n", err);
861 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
862 struct obd_info oinfo = { { { 0 } } };
865 CDEBUG(D_INODE, "set mtime on OST inode %llu to %lu\n",
866 (long long)st->st_ino, LTIME_S(attr->ia_mtime));
867 oa.o_id = lsm->lsm_object_id;
868 oa.o_gr = lsm->lsm_object_gr;
869 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
871 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
872 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
877 rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
879 CERROR("obd_setattr_async fails: rc=%d\n", rc);
883 if (op_data.op_ioepoch)
884 rc1 = llu_setattr_done_writing(inode, &op_data, mod);
885 return rc ? rc : rc1;
888 /* here we simply act as a thin layer to glue it with
889 * llu_setattr_raw(), which is copy from kernel
891 static int llu_iop_setattr(struct pnode *pno,
894 struct intnl_stat *stbuf)
900 liblustre_wait_event(0);
902 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
903 SETATTR_UID | SETATTR_GID |
904 SETATTR_LEN | SETATTR_MODE)));
905 memset(&iattr, 0, sizeof(iattr));
907 if (mask & SETATTR_MODE) {
908 iattr.ia_mode = stbuf->st_mode;
909 iattr.ia_valid |= ATTR_MODE;
911 if (mask & SETATTR_MTIME) {
912 iattr.ia_mtime = stbuf->st_mtime;
913 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
915 if (mask & SETATTR_ATIME) {
916 iattr.ia_atime = stbuf->st_atime;
917 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
919 if (mask & SETATTR_UID) {
920 iattr.ia_uid = stbuf->st_uid;
921 iattr.ia_valid |= ATTR_UID;
923 if (mask & SETATTR_GID) {
924 iattr.ia_gid = stbuf->st_gid;
925 iattr.ia_valid |= ATTR_GID;
927 if (mask & SETATTR_LEN) {
928 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
929 iattr.ia_valid |= ATTR_SIZE;
932 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
933 iattr.ia_ctime = CURRENT_TIME;
935 rc = llu_setattr_raw(ino, &iattr);
936 liblustre_wait_event(0);
940 #define EXT2_LINK_MAX 32000
942 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
944 struct inode *dir = pno->p_base->pb_parent->pb_ino;
945 struct qstr *qstr = &pno->p_base->pb_name;
946 const char *name = qstr->name;
948 struct ptlrpc_request *request = NULL;
949 struct llu_sb_info *sbi = llu_i2sbi(dir);
950 struct md_op_data op_data = {{ 0 }};
954 liblustre_wait_event(0);
955 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
958 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
961 err = md_create(sbi->ll_md_exp, &op_data,
962 tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
963 current->fsuid, current->fsgid, current->cap_effective,
965 ptlrpc_req_finished(request);
966 liblustre_wait_event(0);
970 static int llu_readlink_internal(struct inode *inode,
971 struct ptlrpc_request **request,
974 struct llu_inode_info *lli = llu_i2info(inode);
975 struct llu_sb_info *sbi = llu_i2sbi(inode);
976 struct mdt_body *body;
977 struct intnl_stat *st = llu_i2stat(inode);
978 int rc, symlen = st->st_size + 1;
983 if (lli->lli_symlink_name) {
984 *symname = lli->lli_symlink_name;
985 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
989 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), NULL,
990 OBD_MD_LINKNAME, symlen, request);
992 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
996 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
997 LASSERT(body != NULL);
999 if ((body->valid & OBD_MD_LINKNAME) == 0) {
1000 CERROR ("OBD_MD_LINKNAME not set on reply\n");
1001 GOTO (failed, rc = -EPROTO);
1004 LASSERT(symlen != 0);
1005 if (body->eadatasize != symlen) {
1006 CERROR("inode %llu: symlink length %d not expected %d\n",
1007 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
1008 GOTO(failed, rc = -EPROTO);
1011 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
1012 if (*symname == NULL ||
1013 strnlen(*symname, symlen) != symlen - 1) {
1014 /* not full/NULL terminated */
1015 CERROR("inode %llu: symlink not NULL terminated string"
1016 "of length %d\n", (long long)st->st_ino, symlen - 1);
1017 GOTO(failed, rc = -EPROTO);
1020 OBD_ALLOC(lli->lli_symlink_name, symlen);
1021 /* do not return an error if we cannot cache the symlink locally */
1022 if (lli->lli_symlink_name)
1023 memcpy(lli->lli_symlink_name, *symname, symlen);
1028 ptlrpc_req_finished (*request);
1032 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
1034 struct inode *inode = pno->p_base->pb_ino;
1035 struct ptlrpc_request *request;
1040 liblustre_wait_event(0);
1041 rc = llu_readlink_internal(inode, &request, &symname);
1046 strncpy(data, symname, bufsize);
1047 rc = strlen(symname);
1049 ptlrpc_req_finished(request);
1051 liblustre_wait_event(0);
1055 static int llu_iop_mknod_raw(struct pnode *pno,
1059 struct ptlrpc_request *request = NULL;
1060 struct inode *dir = pno->p_parent->p_base->pb_ino;
1061 struct llu_sb_info *sbi = llu_i2sbi(dir);
1062 struct md_op_data op_data = {{ 0 }};
1066 liblustre_wait_event(0);
1067 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
1068 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
1069 (long long)llu_i2stat(dir)->st_ino);
1071 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
1074 switch (mode & S_IFMT) {
1077 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1082 llu_prep_md_op_data(&op_data, dir, NULL,
1083 pno->p_base->pb_name.name,
1084 pno->p_base->pb_name.len, 0,
1087 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
1088 current->fsuid, current->fsgid,
1089 current->cap_effective, dev, &request);
1090 ptlrpc_req_finished(request);
1098 liblustre_wait_event(0);
1102 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1104 struct inode *src = old->p_base->pb_ino;
1105 struct inode *dir = new->p_parent->p_base->pb_ino;
1106 const char *name = new->p_base->pb_name.name;
1107 int namelen = new->p_base->pb_name.len;
1108 struct ptlrpc_request *request = NULL;
1109 struct md_op_data op_data = {{ 0 }};
1116 liblustre_wait_event(0);
1117 llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0,
1119 rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1120 ptlrpc_req_finished(request);
1121 liblustre_wait_event(0);
1127 * libsysio will clear the inode immediately after return
1129 static int llu_iop_unlink_raw(struct pnode *pno)
1131 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1132 struct qstr *qstr = &pno->p_base->pb_name;
1133 const char *name = qstr->name;
1134 int len = qstr->len;
1135 struct inode *target = pno->p_base->pb_ino;
1136 struct ptlrpc_request *request = NULL;
1137 struct md_op_data op_data = { { 0 } };
1143 liblustre_wait_event(0);
1144 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1146 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1148 rc = llu_objects_destroy(request, dir);
1149 ptlrpc_req_finished(request);
1150 liblustre_wait_event(0);
1155 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1157 struct inode *src = old->p_parent->p_base->pb_ino;
1158 struct inode *tgt = new->p_parent->p_base->pb_ino;
1159 const char *oldname = old->p_base->pb_name.name;
1160 int oldnamelen = old->p_base->pb_name.len;
1161 const char *newname = new->p_base->pb_name.name;
1162 int newnamelen = new->p_base->pb_name.len;
1163 struct ptlrpc_request *request = NULL;
1164 struct md_op_data op_data = { { 0 } };
1171 liblustre_wait_event(0);
1172 llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0,
1174 rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1175 oldname, oldnamelen, newname, newnamelen,
1178 rc = llu_objects_destroy(request, src);
1181 ptlrpc_req_finished(request);
1182 liblustre_wait_event(0);
1187 #ifdef _HAVE_STATVFS
1188 static int llu_statfs_internal(struct llu_sb_info *sbi,
1189 struct obd_statfs *osfs, __u64 max_age)
1191 struct obd_statfs obd_osfs;
1195 rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age);
1197 CERROR("md_statfs fails: rc = %d\n", rc);
1201 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1202 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1204 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1205 &obd_statfs, max_age, 0);
1207 CERROR("obd_statfs fails: rc = %d\n", rc);
1211 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1212 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1215 osfs->os_blocks = obd_osfs.os_blocks;
1216 osfs->os_bfree = obd_osfs.os_bfree;
1217 osfs->os_bavail = obd_osfs.os_bavail;
1219 /* If we don't have as many objects free on the OST as inodes
1220 * on the MDS, we reduce the total number of inodes to
1221 * compensate, so that the "inodes in use" number is correct.
1223 if (obd_osfs.os_ffree < osfs->os_ffree) {
1224 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1226 osfs->os_ffree = obd_osfs.os_ffree;
1232 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1234 struct obd_statfs osfs;
1237 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1239 /* For now we will always get up-to-date statfs values, but in the
1240 * future we may allow some amount of caching on the client (e.g.
1241 * from QOS or lprocfs updates). */
1242 rc = llu_statfs_internal(sbi, &osfs, cfs_time_current_64() - HZ);
1246 statfs_unpack(sfs, &osfs);
1248 if (sizeof(sfs->f_blocks) == 4) {
1249 while (osfs.os_blocks > ~0UL) {
1252 osfs.os_blocks >>= 1;
1253 osfs.os_bfree >>= 1;
1254 osfs.os_bavail >>= 1;
1258 sfs->f_blocks = osfs.os_blocks;
1259 sfs->f_bfree = osfs.os_bfree;
1260 sfs->f_bavail = osfs.os_bavail;
1265 static int llu_iop_statvfs(struct pnode *pno,
1267 struct intnl_statvfs *buf)
1273 liblustre_wait_event(0);
1276 LASSERT(pno->p_base->pb_ino);
1277 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1281 /* from native driver */
1282 buf->f_bsize = fs.f_bsize; /* file system block size */
1283 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1284 buf->f_blocks = fs.f_blocks;
1285 buf->f_bfree = fs.f_bfree;
1286 buf->f_bavail = fs.f_bavail;
1287 buf->f_files = fs.f_files; /* Total number serial numbers */
1288 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1289 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1290 buf->f_fsid = fs.f_fsid.__val[1];
1291 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1292 buf->f_namemax = fs.f_namelen;
1295 liblustre_wait_event(0);
1298 #endif /* _HAVE_STATVFS */
1300 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1302 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1303 struct qstr *qstr = &pno->p_base->pb_name;
1304 const char *name = qstr->name;
1305 int len = qstr->len;
1306 struct ptlrpc_request *request = NULL;
1307 struct intnl_stat *st = llu_i2stat(dir);
1308 struct md_op_data op_data = {{ 0 }};
1312 liblustre_wait_event(0);
1313 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1314 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1316 if (st->st_nlink >= EXT2_LINK_MAX)
1319 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1322 err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0,
1323 mode | S_IFDIR, current->fsuid, current->fsgid,
1324 current->cap_effective, 0, &request);
1325 ptlrpc_req_finished(request);
1326 liblustre_wait_event(0);
1330 static int llu_iop_rmdir_raw(struct pnode *pno)
1332 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1333 struct qstr *qstr = &pno->p_base->pb_name;
1334 const char *name = qstr->name;
1335 int len = qstr->len;
1336 struct ptlrpc_request *request = NULL;
1337 struct md_op_data op_data = {{ 0 }};
1341 liblustre_wait_event(0);
1342 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1343 (long long)llu_i2stat(dir)->st_ino,
1344 llu_i2info(dir)->lli_st_generation, dir);
1346 llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR,
1348 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1349 ptlrpc_req_finished(request);
1351 liblustre_wait_event(0);
1356 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1358 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1360 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1362 /* refer to ll_file_flock() for details */
1363 static int llu_file_flock(struct inode *ino,
1365 struct file_lock *file_lock)
1367 struct llu_inode_info *lli = llu_i2info(ino);
1368 struct intnl_stat *st = llu_i2stat(ino);
1369 struct ldlm_res_id res_id =
1370 { .name = {fid_seq(&lli->lli_fid),
1371 fid_oid(&lli->lli_fid),
1372 fid_ver(&lli->lli_fid),
1374 struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1375 ldlm_flock_completion_ast, NULL, file_lock };
1377 struct lustre_handle lockh = {0};
1378 ldlm_policy_data_t flock;
1382 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1383 (unsigned long long)st->st_ino, file_lock);
1385 flock.l_flock.pid = file_lock->fl_pid;
1386 flock.l_flock.start = file_lock->fl_start;
1387 flock.l_flock.end = file_lock->fl_end;
1389 switch (file_lock->fl_type) {
1391 einfo.ei_mode = LCK_PR;
1394 einfo.ei_mode = LCK_NL;
1397 einfo.ei_mode = LCK_PW;
1400 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1407 #if F_SETLKW64 != F_SETLKW
1415 #if F_SETLK64 != F_SETLK
1419 flags = LDLM_FL_BLOCK_NOWAIT;
1423 #if F_GETLK64 != F_GETLK
1427 flags = LDLM_FL_TEST_LOCK;
1428 file_lock->fl_type = einfo.ei_mode;
1431 CERROR("unknown fcntl cmd: %d\n", cmd);
1435 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, cmd=%d, flags=%#x, mode=%u, "
1436 "start="LPX64", end="LPX64"\n", (unsigned long long)st->st_ino,
1437 flock.l_flock.pid, cmd, flags, einfo.ei_mode, flock.l_flock.start,
1441 struct lmv_obd *lmv;
1442 struct obd_device *lmv_obd;
1443 lmv_obd = class_exp2obd(llu_i2mdexp(ino));
1444 lmv = &lmv_obd->u.lmv;
1446 if (lmv->desc.ld_tgt_count < 1)
1447 RETURN(rc = -ENODEV);
1449 if (lmv->tgts[0].ltd_exp != NULL)
1450 rc = ldlm_cli_enqueue(lmv->tgts[0].ltd_exp, NULL, &einfo, &res_id,
1451 &flock, &flags, NULL, 0, NULL, &lockh, 0);
1458 static int assign_type(struct file_lock *fl, int type)
1471 static int flock_to_posix_lock(struct inode *ino,
1472 struct file_lock *fl,
1475 switch (l->l_whence) {
1476 /* XXX: only SEEK_SET is supported in lustre */
1484 fl->fl_end = l->l_len - 1;
1488 fl->fl_end = OFFSET_MAX;
1490 fl->fl_pid = getpid();
1491 fl->fl_flags = FL_POSIX;
1492 fl->fl_notify = NULL;
1493 fl->fl_insert = NULL;
1494 fl->fl_remove = NULL;
1495 /* XXX: these fields can't be filled with suitable values,
1496 but I think lustre doesn't use them.
1498 fl->fl_owner = NULL;
1501 return assign_type(fl, l->l_type);
1504 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1506 struct file_lock fl;
1510 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1513 error = flock_to_posix_lock(ino, &fl, flock);
1517 error = llu_file_flock(ino, F_GETLK, &fl);
1521 flock->l_type = F_UNLCK;
1522 if (fl.fl_type != F_UNLCK) {
1523 flock->l_pid = fl.fl_pid;
1524 flock->l_start = fl.fl_start;
1525 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1526 fl.fl_end - fl.fl_start + 1;
1527 flock->l_whence = SEEK_SET;
1528 flock->l_type = fl.fl_type;
1535 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1537 struct file_lock fl;
1538 int flags = llu_i2info(ino)->lli_open_flags + 1;
1541 error = flock_to_posix_lock(ino, &fl, flock);
1544 if (cmd == F_SETLKW)
1545 fl.fl_flags |= FL_SLEEP;
1548 switch (flock->l_type) {
1550 if (!(flags & FMODE_READ))
1554 if (!(flags & FMODE_WRITE))
1564 error = llu_file_flock(ino, cmd, &fl);
1572 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1574 struct llu_inode_info *lli = llu_i2info(ino);
1576 struct flock *flock;
1579 liblustre_wait_event(0);
1582 *rtn = lli->lli_open_flags;
1585 flags = va_arg(ap, long);
1586 flags &= FCNTL_FLMASK;
1587 if (flags & FCNTL_FLMASK_INVALID) {
1588 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1589 "the O_NONBLOCK or O_ASYNC flags. "
1590 "Please fix your application.\n");
1595 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1596 (lli->lli_open_flags & ~FCNTL_FLMASK);
1601 #if F_GETLK64 != F_GETLK
1605 flock = va_arg(ap, struct flock *);
1606 err = llu_fcntl_getlk(ino, flock);
1611 #if F_SETLKW64 != F_SETLKW
1617 #if F_SETLK64 != F_SETLK
1621 flock = va_arg(ap, struct flock *);
1622 err = llu_fcntl_setlk(ino, cmd, flock);
1626 CERROR("unsupported fcntl cmd %x\n", cmd);
1632 liblustre_wait_event(0);
1636 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1638 struct llu_inode_info *lli = llu_i2info(inode);
1639 struct ll_file_data *fd = lli->lli_file_data;
1640 ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1641 .end = OBD_OBJECT_EOF}};
1642 struct lustre_handle lockh = { 0 };
1643 struct lov_stripe_md *lsm = lli->lli_smd;
1648 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1652 policy.l_extent.gid = arg;
1653 if (lli->lli_open_flags & O_NONBLOCK)
1654 flags = LDLM_FL_BLOCK_NOWAIT;
1656 err = llu_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh,
1661 fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1663 memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1668 static int llu_put_grouplock(struct inode *inode, unsigned long arg)
1670 struct llu_inode_info *lli = llu_i2info(inode);
1671 struct ll_file_data *fd = lli->lli_file_data;
1672 struct lov_stripe_md *lsm = lli->lli_smd;
1676 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1679 if (fd->fd_gid != arg)
1682 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1684 err = llu_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1689 memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1694 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1696 struct llu_sb_info *sbi = llu_i2sbi(ino);
1697 struct ptlrpc_request *request = NULL;
1698 struct md_op_data op_data = {{ 0 }};
1699 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1702 llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0,
1705 LASSERT(sizeof(lum) == sizeof(*lump));
1706 LASSERT(sizeof(lum.lmm_objects[0]) ==
1707 sizeof(lump->lmm_objects[0]));
1708 rc = copy_from_user(&lum, lump, sizeof(lum));
1712 if (lum.lmm_magic != LOV_USER_MAGIC)
1715 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
1716 lustre_swab_lov_user_md(&lum);
1718 /* swabbing is done in lov_setstripe() on server side */
1719 rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1720 sizeof(lum), NULL, 0, &request, NULL);
1722 ptlrpc_req_finished(request);
1723 if (rc != -EPERM && rc != -EACCES)
1724 CERROR("md_setattr fails: rc = %d\n", rc);
1727 ptlrpc_req_finished(request);
1732 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1733 struct lov_user_md *lum, int lum_size)
1735 struct llu_sb_info *sbi = llu_i2sbi(ino);
1736 struct llu_inode_info *lli = llu_i2info(ino);
1737 struct llu_inode_info *lli2 = NULL;
1738 struct lov_stripe_md *lsm;
1739 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1740 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1741 llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL };
1743 struct ptlrpc_request *req = NULL;
1744 struct lustre_md md;
1745 struct md_op_data data = {{ 0 }};
1746 struct lustre_handle lockh;
1752 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1753 PFID(&lli->lli_fid));
1757 OBD_ALLOC(lli2, sizeof(struct llu_inode_info));
1761 memcpy(lli2, lli, sizeof(struct llu_inode_info));
1762 lli2->lli_open_count = 0;
1763 lli2->lli_it = NULL;
1764 lli2->lli_file_data = NULL;
1765 lli2->lli_smd = NULL;
1766 lli2->lli_symlink_name = NULL;
1767 ino->i_private = lli2;
1769 llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1772 rc = md_enqueue(sbi->ll_md_exp, &einfo, &oit, &data,
1773 &lockh, lum, lum_size, LDLM_FL_INTENT_ONLY);
1777 req = oit.d.lustre.it_data;
1778 rc = it_open_error(DISP_IT_EXECD, &oit);
1784 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1790 rc = md_get_lustre_md(sbi->ll_md_exp, req,
1791 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1795 llu_update_inode(ino, md.body, md.lsm);
1796 lli->lli_smd = lli2->lli_smd;
1797 lli2->lli_smd = NULL;
1799 llu_local_open(lli2, &oit);
1801 /* release intent */
1802 if (lustre_handle_is_used(&lockh))
1803 ldlm_lock_decref(&lockh, LCK_CR);
1805 ptlrpc_req_finished(req);
1808 rc = llu_file_release(ino);
1810 ino->i_private = lli;
1812 OBD_FREE(lli2, sizeof(struct llu_inode_info));
1814 ptlrpc_req_finished(req);
1818 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1820 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1822 int flags = FMODE_WRITE;
1825 LASSERT(sizeof(lum) == sizeof(*lump));
1826 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1827 rc = copy_from_user(&lum, lump, sizeof(lum));
1831 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1835 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1837 struct intnl_stat *st = llu_i2stat(ino);
1838 if (S_ISREG(st->st_mode))
1839 return llu_lov_file_setstripe(ino, arg);
1840 if (S_ISDIR(st->st_mode))
1841 return llu_lov_dir_setstripe(ino, arg);
1846 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1848 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1853 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1857 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1863 liblustre_wait_event(0);
1866 case LL_IOC_GROUP_LOCK:
1867 arg = va_arg(ap, unsigned long);
1868 rc = llu_get_grouplock(ino, arg);
1870 case LL_IOC_GROUP_UNLOCK:
1871 arg = va_arg(ap, unsigned long);
1872 rc = llu_put_grouplock(ino, arg);
1874 case LL_IOC_LOV_SETSTRIPE:
1875 arg = va_arg(ap, unsigned long);
1876 rc = llu_lov_setstripe(ino, arg);
1878 case LL_IOC_LOV_GETSTRIPE:
1879 arg = va_arg(ap, unsigned long);
1880 rc = llu_lov_getstripe(ino, arg);
1883 CERROR("did not support ioctl cmd %lx\n", request);
1888 liblustre_wait_event(0);
1893 * we already do syncronous read/write
1895 static int llu_iop_sync(struct inode *inode)
1897 liblustre_wait_event(0);
1901 static int llu_iop_datasync(struct inode *inode)
1903 liblustre_wait_event(0);
1907 struct filesys_ops llu_filesys_ops =
1909 fsop_gone: llu_fsop_gone,
1912 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1914 struct inode *inode;
1916 struct file_identifier fileid = {&fid, sizeof(fid)};
1918 if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1919 (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1920 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1922 return ERR_PTR(-EPERM);
1925 /* try to find existing inode */
1926 fid = md->body->fid1;
1928 inode = _sysio_i_find(fs, &fileid);
1930 if (inode->i_zombie/* ||
1931 lli->lli_st_generation != md->body->generation*/) {
1935 llu_update_inode(inode, md->body, md->lsm);
1940 inode = llu_new_inode(fs, &fid);
1942 llu_update_inode(inode, md->body, md->lsm);
1948 llu_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
1950 struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC };
1951 __u32 valsize = sizeof(struct lov_desc);
1952 int rc, easize, def_easize, cookiesize;
1953 struct lov_desc desc;
1957 rc = obd_get_info(dt_exp, strlen(KEY_LOVDESC) + 1, KEY_LOVDESC,
1962 stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
1963 lsm.lsm_stripe_count = stripes;
1964 easize = obd_size_diskmd(dt_exp, &lsm);
1966 lsm.lsm_stripe_count = desc.ld_default_stripe_count;
1967 def_easize = obd_size_diskmd(dt_exp, &lsm);
1969 cookiesize = stripes * sizeof(struct llog_cookie);
1971 CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
1972 easize, cookiesize);
1974 rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize);
1979 llu_fsswop_mount(const char *source,
1981 const void *data __IS_UNUSED,
1982 struct pnode *tocover,
1983 struct mount **mntp)
1987 struct pnode_base *rootpb;
1988 struct obd_device *obd;
1989 struct lu_fid rootfid;
1990 struct llu_sb_info *sbi;
1991 struct obd_statfs osfs;
1992 static struct qstr noname = { NULL, 0, 0 };
1993 struct ptlrpc_request *request = NULL;
1994 struct lustre_handle md_conn = {0, };
1995 struct lustre_handle dt_conn = {0, };
1996 struct lustre_md md;
1998 struct config_llog_instance cfg = {0, };
1999 char ll_instance[sizeof(sbi) * 2 + 1];
2000 struct lustre_profile *lprof;
2001 char *zconf_mgsnid, *zconf_profile;
2002 char *osc = NULL, *mdc = NULL;
2003 int async = 1, err = -EINVAL;
2004 struct obd_connect_data ocd = {0,};
2008 if (ll_parse_mount_target(source,
2011 CERROR("mal-formed target %s\n", source);
2014 if (!zconf_mgsnid || !zconf_profile) {
2015 printf("Liblustre: invalid target %s\n", source);
2018 /* allocate & initialize sbi */
2019 OBD_ALLOC(sbi, sizeof(*sbi));
2023 INIT_LIST_HEAD(&sbi->ll_conn_chain);
2024 ll_generate_random_uuid(uuid);
2025 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
2027 /* generate a string unique to this super, let's try
2028 the address of the super itself.*/
2029 sprintf(ll_instance, "%p", sbi);
2031 /* retrive & parse config log */
2032 cfg.cfg_instance = ll_instance;
2033 cfg.cfg_uuid = sbi->ll_sb_uuid;
2034 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
2036 CERROR("Unable to process log: %s\n", zconf_profile);
2037 GOTO(out_free, err);
2040 lprof = class_get_profile(zconf_profile);
2041 if (lprof == NULL) {
2042 CERROR("No profile found: %s\n", zconf_profile);
2043 GOTO(out_free, err = -EINVAL);
2045 OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
2046 sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
2048 OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
2049 sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
2053 GOTO(out_free, err = -EINVAL);
2057 GOTO(out_free, err = -EINVAL);
2060 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
2066 obd = class_name2obd(mdc);
2068 CERROR("MDC %s: not setup or attached\n", mdc);
2069 GOTO(out_free, err = -EINVAL);
2071 obd_set_info_async(obd->obd_self_export, strlen("async"), "async",
2072 sizeof(async), &async, NULL);
2074 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
2076 #ifdef LIBLUSTRE_POSIX_ACL
2077 ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
2079 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2080 ocd.ocd_version = LUSTRE_VERSION_CODE;
2083 err = obd_connect(NULL, &md_conn, obd, &sbi->ll_sb_uuid, &ocd);
2085 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2086 GOTO(out_free, err);
2088 sbi->ll_md_exp = class_conn2export(&md_conn);
2090 err = obd_statfs(obd, &osfs, 100000000);
2095 * FIXME fill fs stat data into sbi here!!! FIXME
2099 obd = class_name2obd(osc);
2101 CERROR("OSC %s: not setup or attached\n", osc);
2102 GOTO(out_md, err = -EINVAL);
2104 obd_set_info_async(obd->obd_self_export, strlen("async"), "async",
2105 sizeof(async), &async, NULL);
2107 obd->obd_upcall.onu_owner = &sbi->ll_lco;
2108 obd->obd_upcall.onu_upcall = ll_ocd_update;
2110 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2111 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK |
2113 ocd.ocd_version = LUSTRE_VERSION_CODE;
2114 err = obd_connect(NULL, &dt_conn, obd, &sbi->ll_sb_uuid, &ocd);
2116 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2119 sbi->ll_dt_exp = class_conn2export(&dt_conn);
2120 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2122 llu_init_ea_size(sbi->ll_md_exp, sbi->ll_dt_exp);
2124 err = md_getstatus(sbi->ll_md_exp, &rootfid, NULL);
2126 CERROR("cannot mds_connect: rc = %d\n", err);
2129 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&rootfid));
2130 sbi->ll_root_fid = rootfid;
2132 /* fetch attr of root inode */
2133 err = md_getattr(sbi->ll_md_exp, &rootfid, NULL,
2134 OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
2136 CERROR("md_getattr failed for root: rc = %d\n", err);
2140 err = md_get_lustre_md(sbi->ll_md_exp, request,
2141 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2143 CERROR("failed to understand root inode md: rc = %d\n",err);
2144 GOTO(out_request, err);
2147 LASSERT(fid_is_sane(&sbi->ll_root_fid));
2149 root = llu_iget(fs, &md);
2150 if (!root || IS_ERR(root)) {
2151 CERROR("fail to generate root inode\n");
2152 GOTO(out_request, err = -EBADF);
2156 * Generate base path-node for root.
2158 rootpb = _sysio_pb_new(&noname, NULL, root);
2164 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2166 _sysio_pb_gone(rootpb);
2170 ptlrpc_req_finished(request);
2172 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2173 liblustre_wait_idle();
2178 _sysio_i_gone(root);
2180 ptlrpc_req_finished(request);
2182 obd_disconnect(sbi->ll_dt_exp);
2184 obd_disconnect(sbi->ll_md_exp);
2187 OBD_FREE(osc, strlen(osc) + 1);
2189 OBD_FREE(mdc, strlen(mdc) + 1);
2190 OBD_FREE(sbi, sizeof(*sbi));
2191 liblustre_wait_idle();
2195 struct fssw_ops llu_fssw_ops = {
2199 static struct inode_ops llu_inode_ops = {
2200 inop_lookup: llu_iop_lookup,
2201 inop_getattr: llu_iop_getattr,
2202 inop_setattr: llu_iop_setattr,
2203 inop_filldirentries: llu_iop_filldirentries,
2204 inop_mkdir: llu_iop_mkdir_raw,
2205 inop_rmdir: llu_iop_rmdir_raw,
2206 inop_symlink: llu_iop_symlink_raw,
2207 inop_readlink: llu_iop_readlink,
2208 inop_open: llu_iop_open,
2209 inop_close: llu_iop_close,
2210 inop_link: llu_iop_link_raw,
2211 inop_unlink: llu_iop_unlink_raw,
2212 inop_rename: llu_iop_rename_raw,
2213 inop_pos: llu_iop_pos,
2214 inop_read: llu_iop_read,
2215 inop_write: llu_iop_write,
2216 inop_iodone: llu_iop_iodone,
2217 inop_fcntl: llu_iop_fcntl,
2218 inop_sync: llu_iop_sync,
2219 inop_datasync: llu_iop_datasync,
2220 inop_ioctl: llu_iop_ioctl,
2221 inop_mknod: llu_iop_mknod_raw,
2222 #ifdef _HAVE_STATVFS
2223 inop_statvfs: llu_iop_statvfs,
2225 inop_gone: llu_iop_gone,