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 void llu_update_inode(struct inode *inode, struct mdt_body *body,
113 struct lov_stripe_md *lsm)
115 struct llu_inode_info *lli = llu_i2info(inode);
116 struct intnl_stat *st = llu_i2stat(inode);
118 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
120 if (lli->lli_smd == NULL) {
122 lli->lli_maxbytes = lsm->lsm_maxbytes;
123 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
124 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
126 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
127 CERROR("lsm mismatch for inode %lld\n",
128 (long long)st->st_ino);
134 if (body->valid & OBD_MD_FLMTIME &&
135 body->mtime > LTIME_S(st->st_mtime))
136 LTIME_S(st->st_mtime) = body->mtime;
137 if (body->valid & OBD_MD_FLATIME &&
138 body->atime > LTIME_S(st->st_atime))
139 LTIME_S(st->st_atime) = body->atime;
141 /* mtime is always updated with ctime, but can be set in past.
142 As write and utime(2) may happen within 1 second, and utime's
143 mtime has a priority over write's one, so take mtime from mds
144 for the same ctimes. */
145 if (body->valid & OBD_MD_FLCTIME &&
146 body->ctime >= LTIME_S(st->st_ctime)) {
147 LTIME_S(st->st_ctime) = body->ctime;
148 if (body->valid & OBD_MD_FLMTIME)
149 LTIME_S(st->st_mtime) = body->mtime;
151 if (body->valid & OBD_MD_FLMODE)
152 st->st_mode = (st->st_mode & S_IFMT)|(body->mode & ~S_IFMT);
153 if (body->valid & OBD_MD_FLTYPE)
154 st->st_mode = (st->st_mode & ~S_IFMT)|(body->mode & S_IFMT);
155 if (S_ISREG(st->st_mode))
156 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
158 st->st_blksize = 4096;
159 if (body->valid & OBD_MD_FLUID)
160 st->st_uid = body->uid;
161 if (body->valid & OBD_MD_FLGID)
162 st->st_gid = body->gid;
163 if (body->valid & OBD_MD_FLNLINK)
164 st->st_nlink = body->nlink;
165 if (body->valid & OBD_MD_FLRDEV)
166 st->st_rdev = body->rdev;
167 if (body->valid & OBD_MD_FLSIZE)
168 st->st_size = body->size;
169 if (body->valid & OBD_MD_FLBLOCKS)
170 st->st_blocks = body->blocks;
171 if (body->valid & OBD_MD_FLFLAGS)
172 lli->lli_st_flags = body->flags;
175 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
177 struct llu_inode_info *lli = llu_i2info(dst);
178 struct intnl_stat *st = llu_i2stat(dst);
180 valid &= src->o_valid;
182 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
183 CDEBUG(D_INODE,"valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
185 LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
186 (long)src->o_mtime, (long)src->o_ctime);
188 if (valid & OBD_MD_FLATIME)
189 LTIME_S(st->st_atime) = src->o_atime;
190 if (valid & OBD_MD_FLMTIME)
191 LTIME_S(st->st_mtime) = src->o_mtime;
192 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
193 LTIME_S(st->st_ctime) = src->o_ctime;
194 if (valid & OBD_MD_FLSIZE)
195 st->st_size = src->o_size;
196 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
197 st->st_blocks = src->o_blocks;
198 if (valid & OBD_MD_FLBLKSZ)
199 st->st_blksize = src->o_blksize;
200 if (valid & OBD_MD_FLTYPE)
201 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
202 if (valid & OBD_MD_FLMODE)
203 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
204 if (valid & OBD_MD_FLUID)
205 st->st_uid = src->o_uid;
206 if (valid & OBD_MD_FLGID)
207 st->st_gid = src->o_gid;
208 if (valid & OBD_MD_FLFLAGS)
209 lli->lli_st_flags = src->o_flags;
212 #define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
213 #define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
215 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
217 struct llu_inode_info *lli = llu_i2info(src);
218 struct intnl_stat *st = llu_i2stat(src);
219 obd_flag newvalid = 0;
221 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
222 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
223 valid, LTIME_S(st->st_mtime),
224 LTIME_S(st->st_ctime));
226 if (valid & OBD_MD_FLATIME) {
227 dst->o_atime = LTIME_S(st->st_atime);
228 newvalid |= OBD_MD_FLATIME;
230 if (valid & OBD_MD_FLMTIME) {
231 dst->o_mtime = LTIME_S(st->st_mtime);
232 newvalid |= OBD_MD_FLMTIME;
234 if (valid & OBD_MD_FLCTIME) {
235 dst->o_ctime = LTIME_S(st->st_ctime);
236 newvalid |= OBD_MD_FLCTIME;
238 if (valid & OBD_MD_FLSIZE) {
239 dst->o_size = st->st_size;
240 newvalid |= OBD_MD_FLSIZE;
242 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
243 dst->o_blocks = st->st_blocks;
244 newvalid |= OBD_MD_FLBLOCKS;
246 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
247 dst->o_blksize = st->st_blksize;
248 newvalid |= OBD_MD_FLBLKSZ;
250 if (valid & OBD_MD_FLTYPE) {
251 dst->o_mode = (dst->o_mode & S_IALLUGO)|(st->st_mode & S_IFMT);
252 newvalid |= OBD_MD_FLTYPE;
254 if (valid & OBD_MD_FLMODE) {
255 dst->o_mode = (dst->o_mode & S_IFMT)|(st->st_mode & S_IALLUGO);
256 newvalid |= OBD_MD_FLMODE;
258 if (valid & OBD_MD_FLUID) {
259 dst->o_uid = st->st_uid;
260 newvalid |= OBD_MD_FLUID;
262 if (valid & OBD_MD_FLGID) {
263 dst->o_gid = st->st_gid;
264 newvalid |= OBD_MD_FLGID;
266 if (valid & OBD_MD_FLFLAGS) {
267 dst->o_flags = lli->lli_st_flags;
268 newvalid |= OBD_MD_FLFLAGS;
270 if (valid & OBD_MD_FLGENER) {
271 dst->o_generation = lli->lli_st_generation;
272 newvalid |= OBD_MD_FLGENER;
274 if (valid & OBD_MD_FLFID) {
275 dst->o_fid = st->st_ino;
276 newvalid |= OBD_MD_FLFID;
279 dst->o_valid |= newvalid;
283 * really does the getattr on the inode and updates its fields
285 int llu_inode_getattr(struct inode *inode, struct obdo *obdo)
287 struct llu_inode_info *lli = llu_i2info(inode);
288 struct ptlrpc_request_set *set;
289 struct lov_stripe_md *lsm = lli->lli_smd;
290 struct obd_info oinfo = { { { 0 } } };
298 oinfo.oi_oa->o_id = lsm->lsm_object_id;
299 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
300 oinfo.oi_oa->o_mode = S_IFREG;
301 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
302 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
303 OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
306 set = ptlrpc_prep_set();
308 CERROR ("ENOMEM allocing request set\n");
311 rc = obd_getattr_async(llu_i2obdexp(inode), &oinfo, set);
313 rc = ptlrpc_set_wait(set);
314 ptlrpc_set_destroy(set);
319 oinfo.oi_oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
320 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
323 obdo_refresh_inode(inode, oinfo.oi_oa, oinfo.oi_oa->o_valid);
324 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %Lu, "
325 "blksize %Lu\n", lli->lli_smd->lsm_object_id,
326 (long long unsigned)llu_i2stat(inode)->st_size,
327 (long long unsigned)llu_i2stat(inode)->st_blocks,
328 (long long unsigned)llu_i2stat(inode)->st_blksize);
332 static struct inode* llu_new_inode(struct filesys *fs,
336 struct llu_inode_info *lli;
337 struct intnl_stat st = {
340 #ifndef AUTOMOUNT_FILE_NAME
341 .st_mode = fid->f_type & S_IFMT,
343 .st_mode = fid->f_type /* all of the bits! */
346 /* FIXME: fix this later */
353 OBD_ALLOC(lli, sizeof(*lli));
357 /* initialize lli here */
358 lli->lli_sbi = llu_fs2sbi(fs);
360 lli->lli_symlink_name = NULL;
362 lli->lli_maxbytes = (__u64)(~0UL);
363 lli->lli_file_data = NULL;
365 lli->lli_sysio_fid.fid_data = &lli->lli_fid;
366 lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
369 /* file identifier is needed by functions like _sysio_i_find() */
370 inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
371 &st, 0, &llu_inode_ops, lli);
374 OBD_FREE(lli, sizeof(*lli));
379 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
381 struct llu_sb_info *sbi = llu_i2sbi(inode);
382 struct llu_inode_info *lli = llu_i2info(inode);
383 struct lustre_handle lockh;
384 struct ldlm_res_id res_id = { .name = {0} };
385 struct obd_device *obddev;
386 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
392 obddev = sbi->ll_md_exp->exp_obd;
393 res_id.name[0] = fid_seq(&lli->lli_fid);
394 res_id.name[1] = fid_oid(&lli->lli_fid);
395 res_id.name[2] = fid_ver(&lli->lli_fid);
397 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
399 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
400 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
401 &policy, LCK_PW | LCK_PR, &lockh)) {
407 static int llu_inode_revalidate(struct inode *inode)
409 struct lov_stripe_md *lsm = NULL;
413 CERROR("REPORT THIS LINE TO PETER\n");
417 if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
419 struct ptlrpc_request *req = NULL;
420 struct llu_sb_info *sbi = llu_i2sbi(inode);
421 unsigned long valid = OBD_MD_FLGETATTR;
424 /* Why don't we update all valid MDS fields here, if we're
425 * doing an RPC anyways? -phil */
426 if (S_ISREG(llu_i2stat(inode)->st_mode)) {
427 ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
428 valid |= OBD_MD_FLEASIZE;
430 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
431 NULL, valid, ealen, &req);
433 CERROR("failure %d inode %llu\n", rc,
434 (long long)llu_i2stat(inode)->st_ino);
437 rc = md_get_lustre_md(sbi->ll_md_exp, req, REPLY_REC_OFF,
438 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
440 /* XXX Too paranoid? */
441 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
442 !((md.body->valid & OBD_MD_FLNLINK) &&
443 (md.body->nlink == 0))) {
444 CERROR("Asked for %s eadata but got %s (%d)\n",
445 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
446 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
447 md.body->eadatasize);
450 ptlrpc_req_finished(req);
455 llu_update_inode(inode, md.body, md.lsm);
456 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
457 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
458 if (md.body->valid & OBD_MD_FLSIZE &&
459 sbi->ll_lco.lco_flags & OBD_CONNECT_SOM)
460 llu_i2info(inode)->lli_flags |= LLIF_MDS_SIZE_LOCK;
461 ptlrpc_req_finished(req);
464 lsm = llu_i2info(inode)->lli_smd;
465 if (!lsm) /* object not yet allocated, don't validate size */
468 /* ll_glimpse_size will prefer locally cached writes if they extend
470 RETURN(llu_glimpse_size(inode));
473 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
475 *b = *llu_i2stat(ino);
478 static int llu_iop_getattr(struct pnode *pno,
480 struct intnl_stat *b)
485 liblustre_wait_event(0);
489 LASSERT(pno->p_base->pb_ino);
490 ino = pno->p_base->pb_ino;
492 LASSERT(!pno || pno->p_base->pb_ino == ino);
495 /* libsysio might call us directly without intent lock,
496 * we must re-fetch the attrs here
498 rc = llu_inode_revalidate(ino);
500 copy_stat_buf(ino, b);
501 LASSERT(!llu_i2info(ino)->lli_it);
504 liblustre_wait_event(0);
508 static int null_if_equal(struct ldlm_lock *lock, void *data)
510 if (data == lock->l_ast_data) {
511 lock->l_ast_data = NULL;
513 if (lock->l_req_mode != lock->l_granted_mode)
514 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
517 return LDLM_ITER_CONTINUE;
520 void llu_clear_inode(struct inode *inode)
522 struct llu_inode_info *lli = llu_i2info(inode);
523 struct llu_sb_info *sbi = llu_i2sbi(inode);
526 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
527 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
530 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
531 md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
532 null_if_equal, inode);
535 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
536 null_if_equal, inode);
539 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
543 if (lli->lli_symlink_name) {
544 OBD_FREE(lli->lli_symlink_name,
545 strlen(lli->lli_symlink_name) + 1);
546 lli->lli_symlink_name = NULL;
552 void llu_iop_gone(struct inode *inode)
554 struct llu_inode_info *lli = llu_i2info(inode);
557 liblustre_wait_event(0);
558 llu_clear_inode(inode);
560 OBD_FREE(lli, sizeof(*lli));
564 static int inode_setattr(struct inode * inode, struct iattr * attr)
566 unsigned int ia_valid = attr->ia_valid;
567 struct intnl_stat *st = llu_i2stat(inode);
571 * inode_setattr() is only ever invoked with ATTR_SIZE (by
572 * llu_setattr_raw()) when file has no bodies. Check this.
574 LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
576 if (ia_valid & ATTR_SIZE)
577 st->st_size = attr->ia_size;
578 if (ia_valid & ATTR_UID)
579 st->st_uid = attr->ia_uid;
580 if (ia_valid & ATTR_GID)
581 st->st_gid = attr->ia_gid;
582 if (ia_valid & ATTR_ATIME)
583 st->st_atime = attr->ia_atime;
584 if (ia_valid & ATTR_MTIME)
585 st->st_mtime = attr->ia_mtime;
586 if (ia_valid & ATTR_CTIME)
587 st->st_ctime = attr->ia_ctime;
588 if (ia_valid & ATTR_MODE) {
589 st->st_mode = attr->ia_mode;
590 if (!in_group_p(st->st_gid) && !capable(CAP_FSETID))
591 st->st_mode &= ~S_ISGID;
593 /* mark_inode_dirty(inode); */
597 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data)
600 struct llu_sb_info *sbi = llu_i2sbi(inode);
601 struct ptlrpc_request *request = NULL;
605 llu_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0, LUSTRE_OPC_ANY);
606 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL, 0, &request);
609 ptlrpc_req_finished(request);
610 if (rc != -EPERM && rc != -EACCES)
611 CERROR("md_setattr fails: rc = %d\n", rc);
615 rc = md_get_lustre_md(sbi->ll_md_exp, request, REPLY_REC_OFF,
616 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
618 ptlrpc_req_finished(request);
622 /* We call inode_setattr to adjust timestamps.
623 * If there is at least some data in file, we cleared ATTR_SIZE
624 * above to avoid invoking vmtruncate, otherwise it is important
625 * to call vmtruncate in inode_setattr to update inode->i_size
627 inode_setattr(inode, &op_data->op_attr);
628 llu_update_inode(inode, md.body, md.lsm);
629 ptlrpc_req_finished(request);
634 /* Close IO epoch and send Size-on-MDS attribute update. */
635 static int llu_setattr_done_writing(struct inode *inode,
636 struct md_op_data *op_data)
638 struct llu_inode_info *lli = llu_i2info(inode);
639 struct intnl_stat *st = llu_i2stat(inode);
643 LASSERT(op_data != NULL);
644 if (!S_ISREG(st->st_mode))
647 /* XXX: pass och here for the recovery purpose. */
648 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
649 op_data->op_ioepoch, PFID(&lli->lli_fid));
651 op_data->op_flags = MF_EPOCH_CLOSE | MF_SOM_CHANGE;
652 rc = md_done_writing(llu_i2sbi(inode)->ll_md_exp, op_data, NULL);
654 /* MDS has instructed us to obtain Size-on-MDS attribute
655 * from OSTs and send setattr to back to MDS. */
656 rc = llu_sizeonmds_update(inode, &op_data->op_handle,
657 op_data->op_ioepoch);
659 CERROR("inode %llu mdc truncate failed: rc = %d\n",
665 /* If this inode has objects allocated to it (lsm != NULL), then the OST
666 * object(s) determine the file size and mtime. Otherwise, the MDS will
667 * keep these values until such a time that objects are allocated for it.
668 * We do the MDS operations first, as it is checking permissions for us.
669 * We don't to the MDS RPC if there is nothing that we want to store there,
670 * otherwise there is no harm in updating mtime/atime on the MDS if we are
671 * going to do an RPC anyways.
673 * If we are doing a truncate, we will send the mtime and ctime updates
674 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
675 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
678 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
680 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
681 struct llu_sb_info *sbi = llu_i2sbi(inode);
682 struct intnl_stat *st = llu_i2stat(inode);
683 int ia_valid = attr->ia_valid;
684 struct md_op_data op_data = { { 0 } };
688 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
690 if (ia_valid & ATTR_SIZE) {
691 if (attr->ia_size > ll_file_maxbytes(inode)) {
692 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
693 (long long)attr->ia_size,
694 ll_file_maxbytes(inode));
698 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
701 /* We mark all of the fields "set" so MDS/OST does not re-set them */
702 if (attr->ia_valid & ATTR_CTIME) {
703 attr->ia_ctime = CURRENT_TIME;
704 attr->ia_valid |= ATTR_CTIME_SET;
706 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
707 attr->ia_atime = CURRENT_TIME;
708 attr->ia_valid |= ATTR_ATIME_SET;
710 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
711 attr->ia_mtime = CURRENT_TIME;
712 attr->ia_valid |= ATTR_MTIME_SET;
714 if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
715 /* To avoid stale mtime on mds, obtain it from ost and send
717 rc = llu_glimpse_size(inode);
721 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
722 attr->ia_mtime = inode->i_stbuf.st_mtime;
725 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
726 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
727 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
728 LTIME_S(CURRENT_TIME));
730 /* NB: ATTR_SIZE will only be set after this point if the size
731 * resides on the MDS, ie, this file has no objects. */
733 attr->ia_valid &= ~ATTR_SIZE;
735 /* If only OST attributes being set on objects, don't do MDS RPC.
736 * In that case, we need to check permissions and update the local
737 * inode ourselves so we can call obdo_from_inode() always. */
738 if (ia_valid & (lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
739 memcpy(&op_data.op_attr, attr, sizeof(*attr));
741 /* Open epoch for truncate. */
742 if (ia_valid & ATTR_SIZE)
743 op_data.op_flags = MF_EPOCH_OPEN;
744 rc = llu_md_setattr(inode, &op_data);
748 if (!lsm || !S_ISREG(st->st_mode)) {
749 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
750 if (op_data.op_ioepoch)
751 rc = llu_setattr_done_writing(inode, &op_data);
755 /* The OST doesn't check permissions, but the alternative is
756 * a gratuitous RPC to the MDS. We already rely on the client
757 * to do read/write/truncate permission checks, so is mtime OK?
759 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
760 /* from sys_utime() */
761 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
762 if (current->fsuid != st->st_uid &&
763 (rc = ll_permission(inode, MAY_WRITE)) != 0)
766 /* from inode_change_ok() */
767 if (current->fsuid != st->st_uid &&
768 !capable(CAP_FOWNER))
774 /* Won't invoke llu_vmtruncate(), as we already cleared
776 inode_setattr(inode, attr);
779 if (ia_valid & ATTR_SIZE) {
780 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
782 struct lustre_handle lockh = { 0, };
783 struct lustre_handle match_lockh = { 0, };
786 int flags = LDLM_FL_TEST_LOCK; /* for assertion check below */
790 /* check that there are no matching locks */
791 LASSERT(obd_match(sbi->ll_dt_exp, lsm, LDLM_EXTENT, &policy,
792 LCK_PW, &flags, inode, &match_lockh) <= 0);
794 /* XXX when we fix the AST intents to pass the discard-range
795 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
797 flags = (attr->ia_size == 0) ? LDLM_AST_DISCARD_DATA : 0;
799 if (sbi->ll_lco.lco_flags & OBD_CONNECT_TRUNCLOCK) {
801 obd_flags = OBD_FL_TRUNCLOCK;
802 CDEBUG(D_INODE, "delegating locking to the OST");
808 /* with lock_mode == LK_NL no lock is taken. */
809 rc = llu_extent_lock(NULL, inode, lsm, lock_mode, &policy,
811 if (rc != ELDLM_OK) {
817 rc = llu_vmtruncate(inode, attr->ia_size, obd_flags);
819 /* unlock now as we don't mind others file lockers racing with
820 * the mds updates below? */
821 err = llu_extent_unlock(NULL, inode, lsm, lock_mode, &lockh);
823 CERROR("llu_extent_unlock failed: %d\n", err);
828 if (op_data.op_ioepoch)
829 rc = llu_setattr_done_writing(inode, &op_data);
830 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
831 struct obd_info oinfo = { { { 0 } } };
834 CDEBUG(D_INODE, "set mtime on OST inode %llu to %lu\n",
835 (long long)st->st_ino, LTIME_S(attr->ia_mtime));
836 oa.o_id = lsm->lsm_object_id;
837 oa.o_valid = OBD_MD_FLID;
839 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
840 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
845 rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
847 CERROR("obd_setattr_async fails: rc=%d\n", rc);
852 /* here we simply act as a thin layer to glue it with
853 * llu_setattr_raw(), which is copy from kernel
855 static int llu_iop_setattr(struct pnode *pno,
858 struct intnl_stat *stbuf)
864 liblustre_wait_event(0);
866 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
867 SETATTR_UID | SETATTR_GID |
868 SETATTR_LEN | SETATTR_MODE)));
869 memset(&iattr, 0, sizeof(iattr));
871 if (mask & SETATTR_MODE) {
872 iattr.ia_mode = stbuf->st_mode;
873 iattr.ia_valid |= ATTR_MODE;
875 if (mask & SETATTR_MTIME) {
876 iattr.ia_mtime = stbuf->st_mtime;
877 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
879 if (mask & SETATTR_ATIME) {
880 iattr.ia_atime = stbuf->st_atime;
881 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
883 if (mask & SETATTR_UID) {
884 iattr.ia_uid = stbuf->st_uid;
885 iattr.ia_valid |= ATTR_UID;
887 if (mask & SETATTR_GID) {
888 iattr.ia_gid = stbuf->st_gid;
889 iattr.ia_valid |= ATTR_GID;
891 if (mask & SETATTR_LEN) {
892 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
893 iattr.ia_valid |= ATTR_SIZE;
896 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
897 iattr.ia_ctime = CURRENT_TIME;
899 rc = llu_setattr_raw(ino, &iattr);
900 liblustre_wait_event(0);
904 #define EXT2_LINK_MAX 32000
906 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
908 struct inode *dir = pno->p_base->pb_parent->pb_ino;
909 struct qstr *qstr = &pno->p_base->pb_name;
910 const char *name = qstr->name;
912 struct ptlrpc_request *request = NULL;
913 struct llu_sb_info *sbi = llu_i2sbi(dir);
914 struct md_op_data op_data;
918 liblustre_wait_event(0);
919 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
922 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
925 err = md_create(sbi->ll_md_exp, &op_data,
926 tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
927 current->fsuid, current->fsgid, current->cap_effective,
929 ptlrpc_req_finished(request);
930 liblustre_wait_event(0);
934 static int llu_readlink_internal(struct inode *inode,
935 struct ptlrpc_request **request,
938 struct llu_inode_info *lli = llu_i2info(inode);
939 struct llu_sb_info *sbi = llu_i2sbi(inode);
940 struct mdt_body *body;
941 struct intnl_stat *st = llu_i2stat(inode);
942 int rc, symlen = st->st_size + 1;
947 if (lli->lli_symlink_name) {
948 *symname = lli->lli_symlink_name;
949 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
953 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), NULL,
954 OBD_MD_LINKNAME, symlen, request);
956 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
960 body = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF,
962 LASSERT(body != NULL);
963 LASSERT_REPSWABBED(*request, REPLY_REC_OFF);
965 if ((body->valid & OBD_MD_LINKNAME) == 0) {
966 CERROR ("OBD_MD_LINKNAME not set on reply\n");
967 GOTO (failed, rc = -EPROTO);
970 LASSERT(symlen != 0);
971 if (body->eadatasize != symlen) {
972 CERROR("inode %llu: symlink length %d not expected %d\n",
973 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
974 GOTO(failed, rc = -EPROTO);
977 *symname = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF + 1,
979 if (*symname == NULL ||
980 strnlen(*symname, symlen) != symlen - 1) {
981 /* not full/NULL terminated */
982 CERROR("inode %llu: symlink not NULL terminated string"
983 "of length %d\n", (long long)st->st_ino, symlen - 1);
984 GOTO(failed, rc = -EPROTO);
987 OBD_ALLOC(lli->lli_symlink_name, symlen);
988 /* do not return an error if we cannot cache the symlink locally */
989 if (lli->lli_symlink_name)
990 memcpy(lli->lli_symlink_name, *symname, symlen);
995 ptlrpc_req_finished (*request);
999 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
1001 struct inode *inode = pno->p_base->pb_ino;
1002 struct ptlrpc_request *request;
1007 liblustre_wait_event(0);
1008 rc = llu_readlink_internal(inode, &request, &symname);
1013 strncpy(data, symname, bufsize);
1014 rc = strlen(symname);
1016 ptlrpc_req_finished(request);
1018 liblustre_wait_event(0);
1022 static int llu_iop_mknod_raw(struct pnode *pno,
1026 struct ptlrpc_request *request = NULL;
1027 struct inode *dir = pno->p_parent->p_base->pb_ino;
1028 struct llu_sb_info *sbi = llu_i2sbi(dir);
1029 struct md_op_data op_data;
1033 liblustre_wait_event(0);
1034 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
1035 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
1036 (long long)llu_i2stat(dir)->st_ino);
1038 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
1041 switch (mode & S_IFMT) {
1044 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1049 llu_prep_md_op_data(&op_data, dir, NULL,
1050 pno->p_base->pb_name.name,
1051 pno->p_base->pb_name.len, 0,
1054 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
1055 current->fsuid, current->fsgid,
1056 current->cap_effective, dev, &request);
1057 ptlrpc_req_finished(request);
1065 liblustre_wait_event(0);
1069 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1071 struct inode *src = old->p_base->pb_ino;
1072 struct inode *dir = new->p_parent->p_base->pb_ino;
1073 const char *name = new->p_base->pb_name.name;
1074 int namelen = new->p_base->pb_name.len;
1075 struct ptlrpc_request *request = NULL;
1076 struct md_op_data op_data;
1083 liblustre_wait_event(0);
1084 llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0,
1086 rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1087 ptlrpc_req_finished(request);
1088 liblustre_wait_event(0);
1094 * libsysio will clear the inode immediately after return
1096 static int llu_iop_unlink_raw(struct pnode *pno)
1098 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1099 struct qstr *qstr = &pno->p_base->pb_name;
1100 const char *name = qstr->name;
1101 int len = qstr->len;
1102 struct inode *target = pno->p_base->pb_ino;
1103 struct ptlrpc_request *request = NULL;
1104 struct md_op_data op_data;
1110 liblustre_wait_event(0);
1111 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1113 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1115 rc = llu_objects_destroy(request, dir);
1116 ptlrpc_req_finished(request);
1117 liblustre_wait_event(0);
1122 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1124 struct inode *src = old->p_parent->p_base->pb_ino;
1125 struct inode *tgt = new->p_parent->p_base->pb_ino;
1126 const char *oldname = old->p_base->pb_name.name;
1127 int oldnamelen = old->p_base->pb_name.len;
1128 const char *newname = new->p_base->pb_name.name;
1129 int newnamelen = new->p_base->pb_name.len;
1130 struct ptlrpc_request *request = NULL;
1131 struct md_op_data op_data;
1138 liblustre_wait_event(0);
1139 llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0,
1141 rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1142 oldname, oldnamelen, newname, newnamelen,
1145 rc = llu_objects_destroy(request, src);
1148 ptlrpc_req_finished(request);
1149 liblustre_wait_event(0);
1154 #ifdef _HAVE_STATVFS
1155 static int llu_statfs_internal(struct llu_sb_info *sbi,
1156 struct obd_statfs *osfs, __u64 max_age)
1158 struct obd_statfs obd_osfs;
1162 rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age);
1164 CERROR("md_statfs fails: rc = %d\n", rc);
1168 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1169 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1171 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1172 &obd_statfs, max_age);
1174 CERROR("obd_statfs fails: rc = %d\n", rc);
1178 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1179 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1182 osfs->os_blocks = obd_osfs.os_blocks;
1183 osfs->os_bfree = obd_osfs.os_bfree;
1184 osfs->os_bavail = obd_osfs.os_bavail;
1186 /* If we don't have as many objects free on the OST as inodes
1187 * on the MDS, we reduce the total number of inodes to
1188 * compensate, so that the "inodes in use" number is correct.
1190 if (obd_osfs.os_ffree < osfs->os_ffree) {
1191 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1193 osfs->os_ffree = obd_osfs.os_ffree;
1199 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1201 struct obd_statfs osfs;
1204 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1206 /* For now we will always get up-to-date statfs values, but in the
1207 * future we may allow some amount of caching on the client (e.g.
1208 * from QOS or lprocfs updates). */
1209 rc = llu_statfs_internal(sbi, &osfs, cfs_time_current_64() - HZ);
1213 statfs_unpack(sfs, &osfs);
1215 if (sizeof(sfs->f_blocks) == 4) {
1216 while (osfs.os_blocks > ~0UL) {
1219 osfs.os_blocks >>= 1;
1220 osfs.os_bfree >>= 1;
1221 osfs.os_bavail >>= 1;
1225 sfs->f_blocks = osfs.os_blocks;
1226 sfs->f_bfree = osfs.os_bfree;
1227 sfs->f_bavail = osfs.os_bavail;
1232 static int llu_iop_statvfs(struct pnode *pno,
1234 struct intnl_statvfs *buf)
1240 liblustre_wait_event(0);
1243 LASSERT(pno->p_base->pb_ino);
1244 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1248 /* from native driver */
1249 buf->f_bsize = fs.f_bsize; /* file system block size */
1250 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1251 buf->f_blocks = fs.f_blocks;
1252 buf->f_bfree = fs.f_bfree;
1253 buf->f_bavail = fs.f_bavail;
1254 buf->f_files = fs.f_files; /* Total number serial numbers */
1255 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1256 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1257 buf->f_fsid = fs.f_fsid.__val[1];
1258 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1259 buf->f_namemax = fs.f_namelen;
1262 liblustre_wait_event(0);
1265 #endif /* _HAVE_STATVFS */
1267 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1269 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1270 struct qstr *qstr = &pno->p_base->pb_name;
1271 const char *name = qstr->name;
1272 int len = qstr->len;
1273 struct ptlrpc_request *request = NULL;
1274 struct intnl_stat *st = llu_i2stat(dir);
1275 struct md_op_data op_data;
1279 liblustre_wait_event(0);
1280 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1281 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1283 if (st->st_nlink >= EXT2_LINK_MAX)
1286 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1289 err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0,
1290 mode | S_IFDIR, current->fsuid, current->fsgid,
1291 current->cap_effective, 0, &request);
1292 ptlrpc_req_finished(request);
1293 liblustre_wait_event(0);
1297 static int llu_iop_rmdir_raw(struct pnode *pno)
1299 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1300 struct qstr *qstr = &pno->p_base->pb_name;
1301 const char *name = qstr->name;
1302 int len = qstr->len;
1303 struct ptlrpc_request *request = NULL;
1304 struct md_op_data op_data;
1308 liblustre_wait_event(0);
1309 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1310 (long long)llu_i2stat(dir)->st_ino,
1311 llu_i2info(dir)->lli_st_generation, dir);
1313 llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR,
1315 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1316 ptlrpc_req_finished(request);
1318 liblustre_wait_event(0);
1323 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1325 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1327 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1329 /* refer to ll_file_flock() for details */
1330 static int llu_file_flock(struct inode *ino,
1332 struct file_lock *file_lock)
1334 struct llu_inode_info *lli = llu_i2info(ino);
1335 struct intnl_stat *st = llu_i2stat(ino);
1336 struct ldlm_res_id res_id =
1337 { .name = {fid_seq(&lli->lli_fid),
1338 fid_oid(&lli->lli_fid),
1339 fid_ver(&lli->lli_fid),
1341 struct lustre_handle lockh = {0};
1342 ldlm_policy_data_t flock;
1343 ldlm_mode_t mode = 0;
1347 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1348 (unsigned long long)st->st_ino, file_lock);
1350 flock.l_flock.pid = file_lock->fl_pid;
1351 flock.l_flock.start = file_lock->fl_start;
1352 flock.l_flock.end = file_lock->fl_end;
1354 switch (file_lock->fl_type) {
1365 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1372 #if F_SETLKW64 != F_SETLKW
1380 #if F_SETLK64 != F_SETLK
1384 flags = LDLM_FL_BLOCK_NOWAIT;
1388 #if F_GETLK64 != F_GETLK
1392 flags = LDLM_FL_TEST_LOCK;
1393 file_lock->fl_type = mode;
1396 CERROR("unknown fcntl cmd: %d\n", cmd);
1400 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, flags=%#x, mode=%u, "
1401 "start="LPU64", end="LPU64"\n", (unsigned long long)st->st_ino,
1402 flock.l_flock.pid, flags, mode, flock.l_flock.start,
1405 rc = ldlm_cli_enqueue(llu_i2mdcexp(ino), NULL, &res_id,
1406 LDLM_FLOCK, &flock, mode, &flags, NULL,
1407 ldlm_flock_completion_ast, NULL,
1408 file_lock, NULL, 0, NULL, &lockh, 0);
1412 static int assign_type(struct file_lock *fl, int type)
1425 static int flock_to_posix_lock(struct inode *ino,
1426 struct file_lock *fl,
1429 switch (l->l_whence) {
1430 /* XXX: only SEEK_SET is supported in lustre */
1438 fl->fl_end = l->l_len - 1;
1442 fl->fl_end = OFFSET_MAX;
1444 fl->fl_pid = getpid();
1445 fl->fl_flags = FL_POSIX;
1446 fl->fl_notify = NULL;
1447 fl->fl_insert = NULL;
1448 fl->fl_remove = NULL;
1449 /* XXX: these fields can't be filled with suitable values,
1450 but I think lustre doesn't use them.
1452 fl->fl_owner = NULL;
1455 return assign_type(fl, l->l_type);
1458 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1460 struct file_lock fl;
1464 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1467 error = flock_to_posix_lock(ino, &fl, flock);
1471 error = llu_file_flock(ino, F_GETLK, &fl);
1475 flock->l_type = F_UNLCK;
1476 if (fl.fl_type != F_UNLCK) {
1477 flock->l_pid = fl.fl_pid;
1478 flock->l_start = fl.fl_start;
1479 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1480 fl.fl_end - fl.fl_start + 1;
1481 flock->l_whence = SEEK_SET;
1482 flock->l_type = fl.fl_type;
1489 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1491 struct file_lock fl;
1492 int flags = llu_i2info(ino)->lli_open_flags + 1;
1495 error = flock_to_posix_lock(ino, &fl, flock);
1498 if (cmd == F_SETLKW)
1499 fl.fl_flags |= FL_SLEEP;
1502 switch (flock->l_type) {
1504 if (!(flags & FMODE_READ))
1508 if (!(flags & FMODE_WRITE))
1518 error = llu_file_flock(ino, cmd, &fl);
1526 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1528 struct llu_inode_info *lli = llu_i2info(ino);
1530 struct flock *flock;
1533 liblustre_wait_event(0);
1536 *rtn = lli->lli_open_flags;
1539 flags = va_arg(ap, long);
1540 flags &= FCNTL_FLMASK;
1541 if (flags & FCNTL_FLMASK_INVALID) {
1542 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1543 "the O_NONBLOCK or O_ASYNC flags. "
1544 "Please fix your application.\n");
1549 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1550 (lli->lli_open_flags & ~FCNTL_FLMASK);
1555 #if F_GETLK64 != F_GETLK
1559 flock = va_arg(ap, struct flock *);
1560 err = llu_fcntl_getlk(ino, flock);
1565 #if F_SETLKW64 != F_SETLKW
1571 #if F_SETLK64 != F_SETLK
1575 flock = va_arg(ap, struct flock *);
1576 err = llu_fcntl_setlk(ino, cmd, flock);
1580 CERROR("unsupported fcntl cmd %x\n", cmd);
1586 liblustre_wait_event(0);
1590 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1592 struct llu_inode_info *lli = llu_i2info(inode);
1593 struct ll_file_data *fd = lli->lli_file_data;
1594 ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1595 .end = OBD_OBJECT_EOF}};
1596 struct lustre_handle lockh = { 0 };
1597 struct lov_stripe_md *lsm = lli->lli_smd;
1602 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1606 policy.l_extent.gid = arg;
1607 if (lli->lli_open_flags & O_NONBLOCK)
1608 flags = LDLM_FL_BLOCK_NOWAIT;
1610 err = llu_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh,
1615 fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1617 memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1622 static int llu_put_grouplock(struct inode *inode, unsigned long arg)
1624 struct llu_inode_info *lli = llu_i2info(inode);
1625 struct ll_file_data *fd = lli->lli_file_data;
1626 struct lov_stripe_md *lsm = lli->lli_smd;
1630 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1633 if (fd->fd_gid != arg)
1636 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1638 err = llu_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1643 memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1648 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1650 struct llu_sb_info *sbi = llu_i2sbi(ino);
1651 struct ptlrpc_request *request = NULL;
1652 struct md_op_data op_data;
1653 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1656 llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0,
1659 LASSERT(sizeof(lum) == sizeof(*lump));
1660 LASSERT(sizeof(lum.lmm_objects[0]) ==
1661 sizeof(lump->lmm_objects[0]));
1662 rc = copy_from_user(&lum, lump, sizeof(lum));
1666 if (lum.lmm_magic != LOV_USER_MAGIC)
1669 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
1670 lustre_swab_lov_user_md(&lum);
1672 /* swabbing is done in lov_setstripe() on server side */
1673 rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1674 sizeof(lum), NULL, 0, &request);
1676 ptlrpc_req_finished(request);
1677 if (rc != -EPERM && rc != -EACCES)
1678 CERROR("md_setattr fails: rc = %d\n", rc);
1681 ptlrpc_req_finished(request);
1686 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1687 struct lov_user_md *lum, int lum_size)
1689 struct llu_sb_info *sbi = llu_i2sbi(ino);
1690 struct llu_inode_info *lli = llu_i2info(ino);
1691 struct llu_inode_info *lli2 = NULL;
1692 struct lov_stripe_md *lsm;
1693 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1694 struct ptlrpc_request *req = NULL;
1695 struct lustre_md md;
1696 struct md_op_data data;
1697 struct lustre_handle lockh;
1703 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1704 PFID(&lli->lli_fid));
1708 OBD_ALLOC(lli2, sizeof(struct llu_inode_info));
1712 memcpy(lli2, lli, sizeof(struct llu_inode_info));
1713 lli2->lli_open_count = 0;
1714 lli2->lli_it = NULL;
1715 lli2->lli_file_data = NULL;
1716 lli2->lli_smd = NULL;
1717 lli2->lli_symlink_name = NULL;
1718 ino->i_private = lli2;
1720 llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1723 rc = md_enqueue(sbi->ll_md_exp, LDLM_IBITS, &oit, LCK_CR, &data,
1724 &lockh, lum, lum_size, ldlm_completion_ast,
1725 llu_md_blocking_ast, NULL, LDLM_FL_INTENT_ONLY);
1729 req = oit.d.lustre.it_data;
1730 rc = it_open_error(DISP_IT_EXECD, &oit);
1736 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1742 rc = md_get_lustre_md(sbi->ll_md_exp, req,
1743 DLM_REPLY_REC_OFF, sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1747 llu_update_inode(ino, md.body, md.lsm);
1748 lli->lli_smd = lli2->lli_smd;
1749 lli2->lli_smd = NULL;
1751 llu_local_open(lli2, &oit);
1753 /* release intent */
1754 if (lustre_handle_is_used(&lockh))
1755 ldlm_lock_decref(&lockh, LCK_CR);
1757 ptlrpc_req_finished(req);
1760 rc = llu_file_release(ino);
1762 ino->i_private = lli;
1764 OBD_FREE(lli2, sizeof(struct llu_inode_info));
1766 ptlrpc_req_finished(req);
1770 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1772 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1774 int flags = FMODE_WRITE;
1777 LASSERT(sizeof(lum) == sizeof(*lump));
1778 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1779 rc = copy_from_user(&lum, lump, sizeof(lum));
1783 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1787 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1789 struct intnl_stat *st = llu_i2stat(ino);
1790 if (S_ISREG(st->st_mode))
1791 return llu_lov_file_setstripe(ino, arg);
1792 if (S_ISDIR(st->st_mode))
1793 return llu_lov_dir_setstripe(ino, arg);
1798 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1800 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1805 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1809 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1815 liblustre_wait_event(0);
1818 case LL_IOC_GROUP_LOCK:
1819 arg = va_arg(ap, unsigned long);
1820 rc = llu_get_grouplock(ino, arg);
1822 case LL_IOC_GROUP_UNLOCK:
1823 arg = va_arg(ap, unsigned long);
1824 rc = llu_put_grouplock(ino, arg);
1826 case LL_IOC_LOV_SETSTRIPE:
1827 arg = va_arg(ap, unsigned long);
1828 rc = llu_lov_setstripe(ino, arg);
1830 case LL_IOC_LOV_GETSTRIPE:
1831 arg = va_arg(ap, unsigned long);
1832 rc = llu_lov_getstripe(ino, arg);
1835 CERROR("did not support ioctl cmd %lx\n", request);
1840 liblustre_wait_event(0);
1845 * we already do syncronous read/write
1847 static int llu_iop_sync(struct inode *inode)
1849 liblustre_wait_event(0);
1853 static int llu_iop_datasync(struct inode *inode)
1855 liblustre_wait_event(0);
1859 struct filesys_ops llu_filesys_ops =
1861 fsop_gone: llu_fsop_gone,
1864 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1866 struct inode *inode;
1868 struct file_identifier fileid = {&fid, sizeof(fid)};
1870 if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1871 (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1872 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1874 return ERR_PTR(-EPERM);
1877 /* try to find existing inode */
1878 fid = md->body->fid1;
1880 inode = _sysio_i_find(fs, &fileid);
1882 if (inode->i_zombie/* ||
1883 lli->lli_st_generation != md->body->generation*/) {
1887 llu_update_inode(inode, md->body, md->lsm);
1892 inode = llu_new_inode(fs, &fid);
1894 llu_update_inode(inode, md->body, md->lsm);
1900 llu_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
1902 struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC };
1903 __u32 valsize = sizeof(struct lov_desc);
1904 int rc, easize, def_easize, cookiesize;
1905 struct lov_desc desc;
1909 rc = obd_get_info(dt_exp, strlen(KEY_LOVDESC) + 1, KEY_LOVDESC,
1914 stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
1915 lsm.lsm_stripe_count = stripes;
1916 easize = obd_size_diskmd(dt_exp, &lsm);
1918 lsm.lsm_stripe_count = desc.ld_default_stripe_count;
1919 def_easize = obd_size_diskmd(dt_exp, &lsm);
1921 cookiesize = stripes * sizeof(struct llog_cookie);
1923 CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
1924 easize, cookiesize);
1926 rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize);
1931 llu_fsswop_mount(const char *source,
1933 const void *data __IS_UNUSED,
1934 struct pnode *tocover,
1935 struct mount **mntp)
1939 struct pnode_base *rootpb;
1940 struct obd_device *obd;
1941 struct lu_fid rootfid;
1942 struct llu_sb_info *sbi;
1943 struct obd_statfs osfs;
1944 static struct qstr noname = { NULL, 0, 0 };
1945 struct ptlrpc_request *request = NULL;
1946 struct lustre_handle md_conn = {0, };
1947 struct lustre_handle dt_conn = {0, };
1948 struct lustre_md md;
1950 struct config_llog_instance cfg = {0, };
1951 char ll_instance[sizeof(sbi) * 2 + 1];
1952 struct lustre_profile *lprof;
1953 char *zconf_mgsnid, *zconf_profile;
1954 char *osc = NULL, *mdc = NULL;
1955 int async = 1, err = -EINVAL;
1956 struct obd_connect_data ocd = {0,};
1960 if (ll_parse_mount_target(source,
1963 CERROR("mal-formed target %s\n", source);
1966 if (!zconf_mgsnid || !zconf_profile) {
1967 printf("Liblustre: invalid target %s\n", source);
1970 /* allocate & initialize sbi */
1971 OBD_ALLOC(sbi, sizeof(*sbi));
1975 INIT_LIST_HEAD(&sbi->ll_conn_chain);
1976 ll_generate_random_uuid(uuid);
1977 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1979 /* generate a string unique to this super, let's try
1980 the address of the super itself.*/
1981 sprintf(ll_instance, "%p", sbi);
1983 /* retrive & parse config log */
1984 cfg.cfg_instance = ll_instance;
1985 cfg.cfg_uuid = sbi->ll_sb_uuid;
1986 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
1988 CERROR("Unable to process log: %s\n", zconf_profile);
1989 GOTO(out_free, err);
1992 lprof = class_get_profile(zconf_profile);
1993 if (lprof == NULL) {
1994 CERROR("No profile found: %s\n", zconf_profile);
1995 GOTO(out_free, err = -EINVAL);
1997 OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
1998 sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
2000 OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
2001 sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
2005 GOTO(out_free, err = -EINVAL);
2009 GOTO(out_free, err = -EINVAL);
2012 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
2018 obd = class_name2obd(mdc);
2020 CERROR("MDC %s: not setup or attached\n", mdc);
2021 GOTO(out_free, err = -EINVAL);
2023 obd_set_info_async(obd->obd_self_export, strlen("async"), "async",
2024 sizeof(async), &async, NULL);
2026 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION;
2027 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2028 ocd.ocd_version = LUSTRE_VERSION_CODE;
2031 err = obd_connect(NULL, &md_conn, obd, &sbi->ll_sb_uuid, &ocd);
2033 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2034 GOTO(out_free, err);
2036 sbi->ll_md_exp = class_conn2export(&md_conn);
2038 err = obd_statfs(obd, &osfs, 100000000);
2043 * FIXME fill fs stat data into sbi here!!! FIXME
2047 obd = class_name2obd(osc);
2049 CERROR("OSC %s: not setup or attached\n", osc);
2050 GOTO(out_md, err = -EINVAL);
2052 obd_set_info_async(obd->obd_self_export, strlen("async"), "async",
2053 sizeof(async), &async, NULL);
2055 obd->obd_upcall.onu_owner = &sbi->ll_lco;
2056 obd->obd_upcall.onu_upcall = ll_ocd_update;
2058 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2059 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK;
2060 ocd.ocd_version = LUSTRE_VERSION_CODE;
2061 err = obd_connect(NULL, &dt_conn, obd, &sbi->ll_sb_uuid, &ocd);
2063 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2066 sbi->ll_dt_exp = class_conn2export(&dt_conn);
2067 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2069 llu_init_ea_size(sbi->ll_md_exp, sbi->ll_dt_exp);
2071 err = md_getstatus(sbi->ll_md_exp, &rootfid, NULL);
2073 CERROR("cannot mds_connect: rc = %d\n", err);
2076 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&rootfid));
2077 sbi->ll_root_fid = rootfid;
2079 /* fetch attr of root inode */
2080 err = md_getattr(sbi->ll_md_exp, &rootfid, NULL,
2081 OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
2083 CERROR("md_getattr failed for root: rc = %d\n", err);
2087 err = md_get_lustre_md(sbi->ll_md_exp, request, REPLY_REC_OFF,
2088 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2090 CERROR("failed to understand root inode md: rc = %d\n",err);
2091 GOTO(out_request, err);
2094 LASSERT(fid_is_sane(&sbi->ll_root_fid));
2096 root = llu_iget(fs, &md);
2097 if (!root || IS_ERR(root)) {
2098 CERROR("fail to generate root inode\n");
2099 GOTO(out_request, err = -EBADF);
2103 * Generate base path-node for root.
2105 rootpb = _sysio_pb_new(&noname, NULL, root);
2111 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2113 _sysio_pb_gone(rootpb);
2117 ptlrpc_req_finished(request);
2119 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2120 liblustre_wait_idle();
2125 _sysio_i_gone(root);
2127 ptlrpc_req_finished(request);
2129 obd_disconnect(sbi->ll_dt_exp);
2131 obd_disconnect(sbi->ll_md_exp);
2134 OBD_FREE(osc, strlen(osc) + 1);
2136 OBD_FREE(mdc, strlen(mdc) + 1);
2137 OBD_FREE(sbi, sizeof(*sbi));
2138 liblustre_wait_idle();
2142 struct fssw_ops llu_fssw_ops = {
2146 static struct inode_ops llu_inode_ops = {
2147 inop_lookup: llu_iop_lookup,
2148 inop_getattr: llu_iop_getattr,
2149 inop_setattr: llu_iop_setattr,
2150 inop_filldirentries: llu_iop_filldirentries,
2151 inop_mkdir: llu_iop_mkdir_raw,
2152 inop_rmdir: llu_iop_rmdir_raw,
2153 inop_symlink: llu_iop_symlink_raw,
2154 inop_readlink: llu_iop_readlink,
2155 inop_open: llu_iop_open,
2156 inop_close: llu_iop_close,
2157 inop_link: llu_iop_link_raw,
2158 inop_unlink: llu_iop_unlink_raw,
2159 inop_rename: llu_iop_rename_raw,
2160 inop_pos: llu_iop_pos,
2161 inop_read: llu_iop_read,
2162 inop_write: llu_iop_write,
2163 inop_iodone: llu_iop_iodone,
2164 inop_fcntl: llu_iop_fcntl,
2165 inop_sync: llu_iop_sync,
2166 inop_datasync: llu_iop_datasync,
2167 inop_ioctl: llu_iop_ioctl,
2168 inop_mknod: llu_iop_mknod_raw,
2169 #ifdef _HAVE_STATVFS
2170 inop_statvfs: llu_iop_statvfs,
2172 inop_gone: llu_iop_gone,