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,
598 struct md_open_data **mod)
601 struct llu_sb_info *sbi = llu_i2sbi(inode);
602 struct ptlrpc_request *request = NULL;
606 llu_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0, LUSTRE_OPC_ANY);
607 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL,
611 ptlrpc_req_finished(request);
612 if (rc != -EPERM && rc != -EACCES)
613 CERROR("md_setattr fails: rc = %d\n", rc);
617 rc = md_get_lustre_md(sbi->ll_md_exp, request, REPLY_REC_OFF,
618 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
620 ptlrpc_req_finished(request);
624 /* We call inode_setattr to adjust timestamps.
625 * If there is at least some data in file, we cleared ATTR_SIZE
626 * above to avoid invoking vmtruncate, otherwise it is important
627 * to call vmtruncate in inode_setattr to update inode->i_size
629 inode_setattr(inode, &op_data->op_attr);
630 llu_update_inode(inode, md.body, md.lsm);
631 ptlrpc_req_finished(request);
636 /* Close IO epoch and send Size-on-MDS attribute update. */
637 static int llu_setattr_done_writing(struct inode *inode,
638 struct md_op_data *op_data,
639 struct md_open_data *mod)
641 struct llu_inode_info *lli = llu_i2info(inode);
642 struct intnl_stat *st = llu_i2stat(inode);
646 LASSERT(op_data != NULL);
647 if (!S_ISREG(st->st_mode))
650 /* XXX: pass och here for the recovery purpose. */
651 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
652 op_data->op_ioepoch, PFID(&lli->lli_fid));
654 op_data->op_flags = MF_EPOCH_CLOSE | MF_SOM_CHANGE;
655 rc = md_done_writing(llu_i2sbi(inode)->ll_md_exp, op_data, mod);
657 /* MDS has instructed us to obtain Size-on-MDS attribute
658 * from OSTs and send setattr to back to MDS. */
659 rc = llu_sizeonmds_update(inode, mod, &op_data->op_handle,
660 op_data->op_ioepoch);
662 CERROR("inode %llu mdc truncate failed: rc = %d\n",
668 /* If this inode has objects allocated to it (lsm != NULL), then the OST
669 * object(s) determine the file size and mtime. Otherwise, the MDS will
670 * keep these values until such a time that objects are allocated for it.
671 * We do the MDS operations first, as it is checking permissions for us.
672 * We don't to the MDS RPC if there is nothing that we want to store there,
673 * otherwise there is no harm in updating mtime/atime on the MDS if we are
674 * going to do an RPC anyways.
676 * If we are doing a truncate, we will send the mtime and ctime updates
677 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
678 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
681 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
683 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
684 struct llu_sb_info *sbi = llu_i2sbi(inode);
685 struct intnl_stat *st = llu_i2stat(inode);
686 int ia_valid = attr->ia_valid;
687 struct md_op_data op_data = { { 0 } };
688 struct md_open_data *mod = NULL;
692 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
694 if (ia_valid & ATTR_SIZE) {
695 if (attr->ia_size > ll_file_maxbytes(inode)) {
696 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
697 (long long)attr->ia_size,
698 ll_file_maxbytes(inode));
702 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
705 /* We mark all of the fields "set" so MDS/OST does not re-set them */
706 if (attr->ia_valid & ATTR_CTIME) {
707 attr->ia_ctime = CURRENT_TIME;
708 attr->ia_valid |= ATTR_CTIME_SET;
710 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
711 attr->ia_atime = CURRENT_TIME;
712 attr->ia_valid |= ATTR_ATIME_SET;
714 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
715 attr->ia_mtime = CURRENT_TIME;
716 attr->ia_valid |= ATTR_MTIME_SET;
718 if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
719 /* To avoid stale mtime on mds, obtain it from ost and send
721 rc = llu_glimpse_size(inode);
725 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
726 attr->ia_mtime = inode->i_stbuf.st_mtime;
729 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
730 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
731 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
732 LTIME_S(CURRENT_TIME));
734 /* NB: ATTR_SIZE will only be set after this point if the size
735 * resides on the MDS, ie, this file has no objects. */
737 attr->ia_valid &= ~ATTR_SIZE;
739 /* If only OST attributes being set on objects, don't do MDS RPC.
740 * In that case, we need to check permissions and update the local
741 * inode ourselves so we can call obdo_from_inode() always. */
742 if (ia_valid & (lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
743 memcpy(&op_data.op_attr, attr, sizeof(*attr));
745 /* Open epoch for truncate. */
746 if (ia_valid & ATTR_SIZE)
747 op_data.op_flags = MF_EPOCH_OPEN;
748 rc = llu_md_setattr(inode, &op_data, &mod);
752 if (op_data.op_ioepoch)
753 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID" for "
754 "truncate\n", op_data.op_ioepoch,
755 PFID(&llu_i2info(inode)->lli_fid));
757 if (!lsm || !S_ISREG(st->st_mode)) {
758 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
762 /* The OST doesn't check permissions, but the alternative is
763 * a gratuitous RPC to the MDS. We already rely on the client
764 * to do read/write/truncate permission checks, so is mtime OK?
766 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
767 /* from sys_utime() */
768 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
769 if (current->fsuid != st->st_uid &&
770 (rc = ll_permission(inode, MAY_WRITE)) != 0)
773 /* from inode_change_ok() */
774 if (current->fsuid != st->st_uid &&
775 !capable(CAP_FOWNER))
781 /* Won't invoke llu_vmtruncate(), as we already cleared
783 inode_setattr(inode, attr);
786 if (ia_valid & ATTR_SIZE) {
787 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
789 struct lustre_handle lockh = { 0, };
790 struct lustre_handle match_lockh = { 0, };
793 int flags = LDLM_FL_TEST_LOCK; /* for assertion check below */
797 /* check that there are no matching locks */
798 LASSERT(obd_match(sbi->ll_dt_exp, lsm, LDLM_EXTENT, &policy,
799 LCK_PW, &flags, inode, &match_lockh) <= 0);
801 /* XXX when we fix the AST intents to pass the discard-range
802 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
804 flags = (attr->ia_size == 0) ? LDLM_AST_DISCARD_DATA : 0;
806 if (sbi->ll_lco.lco_flags & OBD_CONNECT_TRUNCLOCK) {
808 obd_flags = OBD_FL_TRUNCLOCK;
809 CDEBUG(D_INODE, "delegating locking to the OST");
815 /* with lock_mode == LK_NL no lock is taken. */
816 rc = llu_extent_lock(NULL, inode, lsm, lock_mode, &policy,
818 if (rc != ELDLM_OK) {
820 GOTO(out, rc = -ENOLCK);
823 rc = llu_vmtruncate(inode, attr->ia_size, obd_flags);
825 /* unlock now as we don't mind others file lockers racing with
826 * the mds updates below? */
827 err = llu_extent_unlock(NULL, inode, lsm, lock_mode, &lockh);
829 CERROR("llu_extent_unlock failed: %d\n", err);
833 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
834 struct obd_info oinfo = { { { 0 } } };
837 CDEBUG(D_INODE, "set mtime on OST inode %llu to %lu\n",
838 (long long)st->st_ino, LTIME_S(attr->ia_mtime));
839 oa.o_id = lsm->lsm_object_id;
840 oa.o_valid = OBD_MD_FLID;
842 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
843 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
848 rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
850 CERROR("obd_setattr_async fails: rc=%d\n", rc);
854 if (op_data.op_ioepoch)
855 rc1 = llu_setattr_done_writing(inode, &op_data, mod);
856 return rc ? rc : rc1;
859 /* here we simply act as a thin layer to glue it with
860 * llu_setattr_raw(), which is copy from kernel
862 static int llu_iop_setattr(struct pnode *pno,
865 struct intnl_stat *stbuf)
871 liblustre_wait_event(0);
873 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
874 SETATTR_UID | SETATTR_GID |
875 SETATTR_LEN | SETATTR_MODE)));
876 memset(&iattr, 0, sizeof(iattr));
878 if (mask & SETATTR_MODE) {
879 iattr.ia_mode = stbuf->st_mode;
880 iattr.ia_valid |= ATTR_MODE;
882 if (mask & SETATTR_MTIME) {
883 iattr.ia_mtime = stbuf->st_mtime;
884 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
886 if (mask & SETATTR_ATIME) {
887 iattr.ia_atime = stbuf->st_atime;
888 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
890 if (mask & SETATTR_UID) {
891 iattr.ia_uid = stbuf->st_uid;
892 iattr.ia_valid |= ATTR_UID;
894 if (mask & SETATTR_GID) {
895 iattr.ia_gid = stbuf->st_gid;
896 iattr.ia_valid |= ATTR_GID;
898 if (mask & SETATTR_LEN) {
899 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
900 iattr.ia_valid |= ATTR_SIZE;
903 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
904 iattr.ia_ctime = CURRENT_TIME;
906 rc = llu_setattr_raw(ino, &iattr);
907 liblustre_wait_event(0);
911 #define EXT2_LINK_MAX 32000
913 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
915 struct inode *dir = pno->p_base->pb_parent->pb_ino;
916 struct qstr *qstr = &pno->p_base->pb_name;
917 const char *name = qstr->name;
919 struct ptlrpc_request *request = NULL;
920 struct llu_sb_info *sbi = llu_i2sbi(dir);
921 struct md_op_data op_data;
925 liblustre_wait_event(0);
926 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
929 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
932 err = md_create(sbi->ll_md_exp, &op_data,
933 tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
934 current->fsuid, current->fsgid, current->cap_effective,
936 ptlrpc_req_finished(request);
937 liblustre_wait_event(0);
941 static int llu_readlink_internal(struct inode *inode,
942 struct ptlrpc_request **request,
945 struct llu_inode_info *lli = llu_i2info(inode);
946 struct llu_sb_info *sbi = llu_i2sbi(inode);
947 struct mdt_body *body;
948 struct intnl_stat *st = llu_i2stat(inode);
949 int rc, symlen = st->st_size + 1;
954 if (lli->lli_symlink_name) {
955 *symname = lli->lli_symlink_name;
956 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
960 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), NULL,
961 OBD_MD_LINKNAME, symlen, request);
963 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
967 body = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF,
969 LASSERT(body != NULL);
970 LASSERT_REPSWABBED(*request, REPLY_REC_OFF);
972 if ((body->valid & OBD_MD_LINKNAME) == 0) {
973 CERROR ("OBD_MD_LINKNAME not set on reply\n");
974 GOTO (failed, rc = -EPROTO);
977 LASSERT(symlen != 0);
978 if (body->eadatasize != symlen) {
979 CERROR("inode %llu: symlink length %d not expected %d\n",
980 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
981 GOTO(failed, rc = -EPROTO);
984 *symname = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF + 1,
986 if (*symname == NULL ||
987 strnlen(*symname, symlen) != symlen - 1) {
988 /* not full/NULL terminated */
989 CERROR("inode %llu: symlink not NULL terminated string"
990 "of length %d\n", (long long)st->st_ino, symlen - 1);
991 GOTO(failed, rc = -EPROTO);
994 OBD_ALLOC(lli->lli_symlink_name, symlen);
995 /* do not return an error if we cannot cache the symlink locally */
996 if (lli->lli_symlink_name)
997 memcpy(lli->lli_symlink_name, *symname, symlen);
1002 ptlrpc_req_finished (*request);
1006 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
1008 struct inode *inode = pno->p_base->pb_ino;
1009 struct ptlrpc_request *request;
1014 liblustre_wait_event(0);
1015 rc = llu_readlink_internal(inode, &request, &symname);
1020 strncpy(data, symname, bufsize);
1021 rc = strlen(symname);
1023 ptlrpc_req_finished(request);
1025 liblustre_wait_event(0);
1029 static int llu_iop_mknod_raw(struct pnode *pno,
1033 struct ptlrpc_request *request = NULL;
1034 struct inode *dir = pno->p_parent->p_base->pb_ino;
1035 struct llu_sb_info *sbi = llu_i2sbi(dir);
1036 struct md_op_data op_data;
1040 liblustre_wait_event(0);
1041 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
1042 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
1043 (long long)llu_i2stat(dir)->st_ino);
1045 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
1048 switch (mode & S_IFMT) {
1051 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1056 llu_prep_md_op_data(&op_data, dir, NULL,
1057 pno->p_base->pb_name.name,
1058 pno->p_base->pb_name.len, 0,
1061 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
1062 current->fsuid, current->fsgid,
1063 current->cap_effective, dev, &request);
1064 ptlrpc_req_finished(request);
1072 liblustre_wait_event(0);
1076 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1078 struct inode *src = old->p_base->pb_ino;
1079 struct inode *dir = new->p_parent->p_base->pb_ino;
1080 const char *name = new->p_base->pb_name.name;
1081 int namelen = new->p_base->pb_name.len;
1082 struct ptlrpc_request *request = NULL;
1083 struct md_op_data op_data;
1090 liblustre_wait_event(0);
1091 llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0,
1093 rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1094 ptlrpc_req_finished(request);
1095 liblustre_wait_event(0);
1101 * libsysio will clear the inode immediately after return
1103 static int llu_iop_unlink_raw(struct pnode *pno)
1105 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1106 struct qstr *qstr = &pno->p_base->pb_name;
1107 const char *name = qstr->name;
1108 int len = qstr->len;
1109 struct inode *target = pno->p_base->pb_ino;
1110 struct ptlrpc_request *request = NULL;
1111 struct md_op_data op_data;
1117 liblustre_wait_event(0);
1118 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1120 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1122 rc = llu_objects_destroy(request, dir);
1123 ptlrpc_req_finished(request);
1124 liblustre_wait_event(0);
1129 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1131 struct inode *src = old->p_parent->p_base->pb_ino;
1132 struct inode *tgt = new->p_parent->p_base->pb_ino;
1133 const char *oldname = old->p_base->pb_name.name;
1134 int oldnamelen = old->p_base->pb_name.len;
1135 const char *newname = new->p_base->pb_name.name;
1136 int newnamelen = new->p_base->pb_name.len;
1137 struct ptlrpc_request *request = NULL;
1138 struct md_op_data op_data;
1145 liblustre_wait_event(0);
1146 llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0,
1148 rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1149 oldname, oldnamelen, newname, newnamelen,
1152 rc = llu_objects_destroy(request, src);
1155 ptlrpc_req_finished(request);
1156 liblustre_wait_event(0);
1161 #ifdef _HAVE_STATVFS
1162 static int llu_statfs_internal(struct llu_sb_info *sbi,
1163 struct obd_statfs *osfs, __u64 max_age)
1165 struct obd_statfs obd_osfs;
1169 rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age);
1171 CERROR("md_statfs fails: rc = %d\n", rc);
1175 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1176 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1178 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1179 &obd_statfs, max_age);
1181 CERROR("obd_statfs fails: rc = %d\n", rc);
1185 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1186 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1189 osfs->os_blocks = obd_osfs.os_blocks;
1190 osfs->os_bfree = obd_osfs.os_bfree;
1191 osfs->os_bavail = obd_osfs.os_bavail;
1193 /* If we don't have as many objects free on the OST as inodes
1194 * on the MDS, we reduce the total number of inodes to
1195 * compensate, so that the "inodes in use" number is correct.
1197 if (obd_osfs.os_ffree < osfs->os_ffree) {
1198 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1200 osfs->os_ffree = obd_osfs.os_ffree;
1206 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1208 struct obd_statfs osfs;
1211 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1213 /* For now we will always get up-to-date statfs values, but in the
1214 * future we may allow some amount of caching on the client (e.g.
1215 * from QOS or lprocfs updates). */
1216 rc = llu_statfs_internal(sbi, &osfs, cfs_time_current_64() - HZ);
1220 statfs_unpack(sfs, &osfs);
1222 if (sizeof(sfs->f_blocks) == 4) {
1223 while (osfs.os_blocks > ~0UL) {
1226 osfs.os_blocks >>= 1;
1227 osfs.os_bfree >>= 1;
1228 osfs.os_bavail >>= 1;
1232 sfs->f_blocks = osfs.os_blocks;
1233 sfs->f_bfree = osfs.os_bfree;
1234 sfs->f_bavail = osfs.os_bavail;
1239 static int llu_iop_statvfs(struct pnode *pno,
1241 struct intnl_statvfs *buf)
1247 liblustre_wait_event(0);
1250 LASSERT(pno->p_base->pb_ino);
1251 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1255 /* from native driver */
1256 buf->f_bsize = fs.f_bsize; /* file system block size */
1257 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1258 buf->f_blocks = fs.f_blocks;
1259 buf->f_bfree = fs.f_bfree;
1260 buf->f_bavail = fs.f_bavail;
1261 buf->f_files = fs.f_files; /* Total number serial numbers */
1262 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1263 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1264 buf->f_fsid = fs.f_fsid.__val[1];
1265 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1266 buf->f_namemax = fs.f_namelen;
1269 liblustre_wait_event(0);
1272 #endif /* _HAVE_STATVFS */
1274 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1276 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1277 struct qstr *qstr = &pno->p_base->pb_name;
1278 const char *name = qstr->name;
1279 int len = qstr->len;
1280 struct ptlrpc_request *request = NULL;
1281 struct intnl_stat *st = llu_i2stat(dir);
1282 struct md_op_data op_data;
1286 liblustre_wait_event(0);
1287 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1288 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1290 if (st->st_nlink >= EXT2_LINK_MAX)
1293 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1296 err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0,
1297 mode | S_IFDIR, current->fsuid, current->fsgid,
1298 current->cap_effective, 0, &request);
1299 ptlrpc_req_finished(request);
1300 liblustre_wait_event(0);
1304 static int llu_iop_rmdir_raw(struct pnode *pno)
1306 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1307 struct qstr *qstr = &pno->p_base->pb_name;
1308 const char *name = qstr->name;
1309 int len = qstr->len;
1310 struct ptlrpc_request *request = NULL;
1311 struct md_op_data op_data;
1315 liblustre_wait_event(0);
1316 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1317 (long long)llu_i2stat(dir)->st_ino,
1318 llu_i2info(dir)->lli_st_generation, dir);
1320 llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR,
1322 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1323 ptlrpc_req_finished(request);
1325 liblustre_wait_event(0);
1330 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1332 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1334 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1336 /* refer to ll_file_flock() for details */
1337 static int llu_file_flock(struct inode *ino,
1339 struct file_lock *file_lock)
1341 struct llu_inode_info *lli = llu_i2info(ino);
1342 struct intnl_stat *st = llu_i2stat(ino);
1343 struct ldlm_res_id res_id =
1344 { .name = {fid_seq(&lli->lli_fid),
1345 fid_oid(&lli->lli_fid),
1346 fid_ver(&lli->lli_fid),
1348 struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1349 ldlm_flock_completion_ast, NULL, file_lock };
1351 struct lustre_handle lockh = {0};
1352 ldlm_policy_data_t flock;
1356 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1357 (unsigned long long)st->st_ino, file_lock);
1359 flock.l_flock.pid = file_lock->fl_pid;
1360 flock.l_flock.start = file_lock->fl_start;
1361 flock.l_flock.end = file_lock->fl_end;
1363 switch (file_lock->fl_type) {
1365 einfo.ei_mode = LCK_PR;
1368 einfo.ei_mode = LCK_NL;
1371 einfo.ei_mode = LCK_PW;
1374 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1381 #if F_SETLKW64 != F_SETLKW
1389 #if F_SETLK64 != F_SETLK
1393 flags = LDLM_FL_BLOCK_NOWAIT;
1397 #if F_GETLK64 != F_GETLK
1401 flags = LDLM_FL_TEST_LOCK;
1402 file_lock->fl_type = einfo.ei_mode;
1405 CERROR("unknown fcntl cmd: %d\n", cmd);
1409 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, flags=%#x, mode=%u, "
1410 "start="LPU64", end="LPU64"\n", (unsigned long long)st->st_ino,
1411 flock.l_flock.pid, flags, einfo.ei_mode, flock.l_flock.start,
1414 rc = ldlm_cli_enqueue(llu_i2mdcexp(ino), NULL, &einfo, &res_id,
1415 &flock, &flags, NULL, 0, NULL, &lockh, 0);
1419 static int assign_type(struct file_lock *fl, int type)
1432 static int flock_to_posix_lock(struct inode *ino,
1433 struct file_lock *fl,
1436 switch (l->l_whence) {
1437 /* XXX: only SEEK_SET is supported in lustre */
1445 fl->fl_end = l->l_len - 1;
1449 fl->fl_end = OFFSET_MAX;
1451 fl->fl_pid = getpid();
1452 fl->fl_flags = FL_POSIX;
1453 fl->fl_notify = NULL;
1454 fl->fl_insert = NULL;
1455 fl->fl_remove = NULL;
1456 /* XXX: these fields can't be filled with suitable values,
1457 but I think lustre doesn't use them.
1459 fl->fl_owner = NULL;
1462 return assign_type(fl, l->l_type);
1465 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1467 struct file_lock fl;
1471 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1474 error = flock_to_posix_lock(ino, &fl, flock);
1478 error = llu_file_flock(ino, F_GETLK, &fl);
1482 flock->l_type = F_UNLCK;
1483 if (fl.fl_type != F_UNLCK) {
1484 flock->l_pid = fl.fl_pid;
1485 flock->l_start = fl.fl_start;
1486 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1487 fl.fl_end - fl.fl_start + 1;
1488 flock->l_whence = SEEK_SET;
1489 flock->l_type = fl.fl_type;
1496 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1498 struct file_lock fl;
1499 int flags = llu_i2info(ino)->lli_open_flags + 1;
1502 error = flock_to_posix_lock(ino, &fl, flock);
1505 if (cmd == F_SETLKW)
1506 fl.fl_flags |= FL_SLEEP;
1509 switch (flock->l_type) {
1511 if (!(flags & FMODE_READ))
1515 if (!(flags & FMODE_WRITE))
1525 error = llu_file_flock(ino, cmd, &fl);
1533 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1535 struct llu_inode_info *lli = llu_i2info(ino);
1537 struct flock *flock;
1540 liblustre_wait_event(0);
1543 *rtn = lli->lli_open_flags;
1546 flags = va_arg(ap, long);
1547 flags &= FCNTL_FLMASK;
1548 if (flags & FCNTL_FLMASK_INVALID) {
1549 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1550 "the O_NONBLOCK or O_ASYNC flags. "
1551 "Please fix your application.\n");
1556 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1557 (lli->lli_open_flags & ~FCNTL_FLMASK);
1562 #if F_GETLK64 != F_GETLK
1566 flock = va_arg(ap, struct flock *);
1567 err = llu_fcntl_getlk(ino, flock);
1572 #if F_SETLKW64 != F_SETLKW
1578 #if F_SETLK64 != F_SETLK
1582 flock = va_arg(ap, struct flock *);
1583 err = llu_fcntl_setlk(ino, cmd, flock);
1587 CERROR("unsupported fcntl cmd %x\n", cmd);
1593 liblustre_wait_event(0);
1597 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1599 struct llu_inode_info *lli = llu_i2info(inode);
1600 struct ll_file_data *fd = lli->lli_file_data;
1601 ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1602 .end = OBD_OBJECT_EOF}};
1603 struct lustre_handle lockh = { 0 };
1604 struct lov_stripe_md *lsm = lli->lli_smd;
1609 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1613 policy.l_extent.gid = arg;
1614 if (lli->lli_open_flags & O_NONBLOCK)
1615 flags = LDLM_FL_BLOCK_NOWAIT;
1617 err = llu_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh,
1622 fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1624 memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1629 static int llu_put_grouplock(struct inode *inode, unsigned long arg)
1631 struct llu_inode_info *lli = llu_i2info(inode);
1632 struct ll_file_data *fd = lli->lli_file_data;
1633 struct lov_stripe_md *lsm = lli->lli_smd;
1637 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1640 if (fd->fd_gid != arg)
1643 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1645 err = llu_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1650 memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1655 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1657 struct llu_sb_info *sbi = llu_i2sbi(ino);
1658 struct ptlrpc_request *request = NULL;
1659 struct md_op_data op_data;
1660 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1663 llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0,
1666 LASSERT(sizeof(lum) == sizeof(*lump));
1667 LASSERT(sizeof(lum.lmm_objects[0]) ==
1668 sizeof(lump->lmm_objects[0]));
1669 rc = copy_from_user(&lum, lump, sizeof(lum));
1673 if (lum.lmm_magic != LOV_USER_MAGIC)
1676 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
1677 lustre_swab_lov_user_md(&lum);
1679 /* swabbing is done in lov_setstripe() on server side */
1680 rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1681 sizeof(lum), NULL, 0, &request, NULL);
1683 ptlrpc_req_finished(request);
1684 if (rc != -EPERM && rc != -EACCES)
1685 CERROR("md_setattr fails: rc = %d\n", rc);
1688 ptlrpc_req_finished(request);
1693 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1694 struct lov_user_md *lum, int lum_size)
1696 struct llu_sb_info *sbi = llu_i2sbi(ino);
1697 struct llu_inode_info *lli = llu_i2info(ino);
1698 struct llu_inode_info *lli2 = NULL;
1699 struct lov_stripe_md *lsm;
1700 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1701 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1702 llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL };
1704 struct ptlrpc_request *req = NULL;
1705 struct lustre_md md;
1706 struct md_op_data data;
1707 struct lustre_handle lockh;
1713 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1714 PFID(&lli->lli_fid));
1718 OBD_ALLOC(lli2, sizeof(struct llu_inode_info));
1722 memcpy(lli2, lli, sizeof(struct llu_inode_info));
1723 lli2->lli_open_count = 0;
1724 lli2->lli_it = NULL;
1725 lli2->lli_file_data = NULL;
1726 lli2->lli_smd = NULL;
1727 lli2->lli_symlink_name = NULL;
1728 ino->i_private = lli2;
1730 llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1733 rc = md_enqueue(sbi->ll_md_exp, &einfo, &oit, &data,
1734 &lockh, lum, lum_size, LDLM_FL_INTENT_ONLY);
1738 req = oit.d.lustre.it_data;
1739 rc = it_open_error(DISP_IT_EXECD, &oit);
1745 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1751 rc = md_get_lustre_md(sbi->ll_md_exp, req,
1752 DLM_REPLY_REC_OFF, sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1756 llu_update_inode(ino, md.body, md.lsm);
1757 lli->lli_smd = lli2->lli_smd;
1758 lli2->lli_smd = NULL;
1760 llu_local_open(lli2, &oit);
1762 /* release intent */
1763 if (lustre_handle_is_used(&lockh))
1764 ldlm_lock_decref(&lockh, LCK_CR);
1766 ptlrpc_req_finished(req);
1769 rc = llu_file_release(ino);
1771 ino->i_private = lli;
1773 OBD_FREE(lli2, sizeof(struct llu_inode_info));
1775 ptlrpc_req_finished(req);
1779 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1781 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1783 int flags = FMODE_WRITE;
1786 LASSERT(sizeof(lum) == sizeof(*lump));
1787 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1788 rc = copy_from_user(&lum, lump, sizeof(lum));
1792 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1796 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1798 struct intnl_stat *st = llu_i2stat(ino);
1799 if (S_ISREG(st->st_mode))
1800 return llu_lov_file_setstripe(ino, arg);
1801 if (S_ISDIR(st->st_mode))
1802 return llu_lov_dir_setstripe(ino, arg);
1807 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1809 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1814 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1818 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1824 liblustre_wait_event(0);
1827 case LL_IOC_GROUP_LOCK:
1828 arg = va_arg(ap, unsigned long);
1829 rc = llu_get_grouplock(ino, arg);
1831 case LL_IOC_GROUP_UNLOCK:
1832 arg = va_arg(ap, unsigned long);
1833 rc = llu_put_grouplock(ino, arg);
1835 case LL_IOC_LOV_SETSTRIPE:
1836 arg = va_arg(ap, unsigned long);
1837 rc = llu_lov_setstripe(ino, arg);
1839 case LL_IOC_LOV_GETSTRIPE:
1840 arg = va_arg(ap, unsigned long);
1841 rc = llu_lov_getstripe(ino, arg);
1844 CERROR("did not support ioctl cmd %lx\n", request);
1849 liblustre_wait_event(0);
1854 * we already do syncronous read/write
1856 static int llu_iop_sync(struct inode *inode)
1858 liblustre_wait_event(0);
1862 static int llu_iop_datasync(struct inode *inode)
1864 liblustre_wait_event(0);
1868 struct filesys_ops llu_filesys_ops =
1870 fsop_gone: llu_fsop_gone,
1873 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1875 struct inode *inode;
1877 struct file_identifier fileid = {&fid, sizeof(fid)};
1879 if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1880 (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1881 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1883 return ERR_PTR(-EPERM);
1886 /* try to find existing inode */
1887 fid = md->body->fid1;
1889 inode = _sysio_i_find(fs, &fileid);
1891 if (inode->i_zombie/* ||
1892 lli->lli_st_generation != md->body->generation*/) {
1896 llu_update_inode(inode, md->body, md->lsm);
1901 inode = llu_new_inode(fs, &fid);
1903 llu_update_inode(inode, md->body, md->lsm);
1909 llu_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
1911 struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC };
1912 __u32 valsize = sizeof(struct lov_desc);
1913 int rc, easize, def_easize, cookiesize;
1914 struct lov_desc desc;
1918 rc = obd_get_info(dt_exp, strlen(KEY_LOVDESC) + 1, KEY_LOVDESC,
1923 stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
1924 lsm.lsm_stripe_count = stripes;
1925 easize = obd_size_diskmd(dt_exp, &lsm);
1927 lsm.lsm_stripe_count = desc.ld_default_stripe_count;
1928 def_easize = obd_size_diskmd(dt_exp, &lsm);
1930 cookiesize = stripes * sizeof(struct llog_cookie);
1932 CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
1933 easize, cookiesize);
1935 rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize);
1940 llu_fsswop_mount(const char *source,
1942 const void *data __IS_UNUSED,
1943 struct pnode *tocover,
1944 struct mount **mntp)
1948 struct pnode_base *rootpb;
1949 struct obd_device *obd;
1950 struct lu_fid rootfid;
1951 struct llu_sb_info *sbi;
1952 struct obd_statfs osfs;
1953 static struct qstr noname = { NULL, 0, 0 };
1954 struct ptlrpc_request *request = NULL;
1955 struct lustre_handle md_conn = {0, };
1956 struct lustre_handle dt_conn = {0, };
1957 struct lustre_md md;
1959 struct config_llog_instance cfg = {0, };
1960 char ll_instance[sizeof(sbi) * 2 + 1];
1961 struct lustre_profile *lprof;
1962 char *zconf_mgsnid, *zconf_profile;
1963 char *osc = NULL, *mdc = NULL;
1964 int async = 1, err = -EINVAL;
1965 struct obd_connect_data ocd = {0,};
1969 if (ll_parse_mount_target(source,
1972 CERROR("mal-formed target %s\n", source);
1975 if (!zconf_mgsnid || !zconf_profile) {
1976 printf("Liblustre: invalid target %s\n", source);
1979 /* allocate & initialize sbi */
1980 OBD_ALLOC(sbi, sizeof(*sbi));
1984 INIT_LIST_HEAD(&sbi->ll_conn_chain);
1985 ll_generate_random_uuid(uuid);
1986 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1988 /* generate a string unique to this super, let's try
1989 the address of the super itself.*/
1990 sprintf(ll_instance, "%p", sbi);
1992 /* retrive & parse config log */
1993 cfg.cfg_instance = ll_instance;
1994 cfg.cfg_uuid = sbi->ll_sb_uuid;
1995 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
1997 CERROR("Unable to process log: %s\n", zconf_profile);
1998 GOTO(out_free, err);
2001 lprof = class_get_profile(zconf_profile);
2002 if (lprof == NULL) {
2003 CERROR("No profile found: %s\n", zconf_profile);
2004 GOTO(out_free, err = -EINVAL);
2006 OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
2007 sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
2009 OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
2010 sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
2014 GOTO(out_free, err = -EINVAL);
2018 GOTO(out_free, err = -EINVAL);
2021 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
2027 obd = class_name2obd(mdc);
2029 CERROR("MDC %s: not setup or attached\n", mdc);
2030 GOTO(out_free, err = -EINVAL);
2032 obd_set_info_async(obd->obd_self_export, strlen("async"), "async",
2033 sizeof(async), &async, NULL);
2035 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION;
2036 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2037 ocd.ocd_version = LUSTRE_VERSION_CODE;
2040 err = obd_connect(NULL, &md_conn, obd, &sbi->ll_sb_uuid, &ocd);
2042 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2043 GOTO(out_free, err);
2045 sbi->ll_md_exp = class_conn2export(&md_conn);
2047 err = obd_statfs(obd, &osfs, 100000000);
2052 * FIXME fill fs stat data into sbi here!!! FIXME
2056 obd = class_name2obd(osc);
2058 CERROR("OSC %s: not setup or attached\n", osc);
2059 GOTO(out_md, err = -EINVAL);
2061 obd_set_info_async(obd->obd_self_export, strlen("async"), "async",
2062 sizeof(async), &async, NULL);
2064 obd->obd_upcall.onu_owner = &sbi->ll_lco;
2065 obd->obd_upcall.onu_upcall = ll_ocd_update;
2067 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2068 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK;
2069 ocd.ocd_version = LUSTRE_VERSION_CODE;
2070 err = obd_connect(NULL, &dt_conn, obd, &sbi->ll_sb_uuid, &ocd);
2072 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2075 sbi->ll_dt_exp = class_conn2export(&dt_conn);
2076 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2078 llu_init_ea_size(sbi->ll_md_exp, sbi->ll_dt_exp);
2080 err = md_getstatus(sbi->ll_md_exp, &rootfid, NULL);
2082 CERROR("cannot mds_connect: rc = %d\n", err);
2085 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&rootfid));
2086 sbi->ll_root_fid = rootfid;
2088 /* fetch attr of root inode */
2089 err = md_getattr(sbi->ll_md_exp, &rootfid, NULL,
2090 OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
2092 CERROR("md_getattr failed for root: rc = %d\n", err);
2096 err = md_get_lustre_md(sbi->ll_md_exp, request, REPLY_REC_OFF,
2097 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2099 CERROR("failed to understand root inode md: rc = %d\n",err);
2100 GOTO(out_request, err);
2103 LASSERT(fid_is_sane(&sbi->ll_root_fid));
2105 root = llu_iget(fs, &md);
2106 if (!root || IS_ERR(root)) {
2107 CERROR("fail to generate root inode\n");
2108 GOTO(out_request, err = -EBADF);
2112 * Generate base path-node for root.
2114 rootpb = _sysio_pb_new(&noname, NULL, root);
2120 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2122 _sysio_pb_gone(rootpb);
2126 ptlrpc_req_finished(request);
2128 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2129 liblustre_wait_idle();
2134 _sysio_i_gone(root);
2136 ptlrpc_req_finished(request);
2138 obd_disconnect(sbi->ll_dt_exp);
2140 obd_disconnect(sbi->ll_md_exp);
2143 OBD_FREE(osc, strlen(osc) + 1);
2145 OBD_FREE(mdc, strlen(mdc) + 1);
2146 OBD_FREE(sbi, sizeof(*sbi));
2147 liblustre_wait_idle();
2151 struct fssw_ops llu_fssw_ops = {
2155 static struct inode_ops llu_inode_ops = {
2156 inop_lookup: llu_iop_lookup,
2157 inop_getattr: llu_iop_getattr,
2158 inop_setattr: llu_iop_setattr,
2159 inop_filldirentries: llu_iop_filldirentries,
2160 inop_mkdir: llu_iop_mkdir_raw,
2161 inop_rmdir: llu_iop_rmdir_raw,
2162 inop_symlink: llu_iop_symlink_raw,
2163 inop_readlink: llu_iop_readlink,
2164 inop_open: llu_iop_open,
2165 inop_close: llu_iop_close,
2166 inop_link: llu_iop_link_raw,
2167 inop_unlink: llu_iop_unlink_raw,
2168 inop_rename: llu_iop_rename_raw,
2169 inop_pos: llu_iop_pos,
2170 inop_read: llu_iop_read,
2171 inop_write: llu_iop_write,
2172 inop_iodone: llu_iop_iodone,
2173 inop_fcntl: llu_iop_fcntl,
2174 inop_sync: llu_iop_sync,
2175 inop_datasync: llu_iop_datasync,
2176 inop_ioctl: llu_iop_ioctl,
2177 inop_mknod: llu_iop_mknod_raw,
2178 #ifdef _HAVE_STATVFS
2179 inop_statvfs: llu_iop_statvfs,
2181 inop_gone: llu_iop_gone,