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 struct lustre_cfg_bufs bufs;
103 struct lustre_cfg *lcfg;
106 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
107 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
108 err = class_process_config(lcfg);
109 lustre_cfg_free(lcfg);
111 CERROR("cleanup failed: %s\n", obd->obd_name);
113 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
114 lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
115 err = class_process_config(lcfg);
117 CERROR("detach failed: %s\n", obd->obd_name);
120 OBD_FREE(sbi, sizeof(*sbi));
125 static struct inode_ops llu_inode_ops;
127 void llu_update_inode(struct inode *inode, struct mdt_body *body,
128 struct lov_stripe_md *lsm)
130 struct llu_inode_info *lli = llu_i2info(inode);
131 struct intnl_stat *st = llu_i2stat(inode);
133 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
135 if (lli->lli_smd == NULL) {
137 lli->lli_maxbytes = lsm->lsm_maxbytes;
138 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
139 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
141 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
142 CERROR("lsm mismatch for inode %lld\n",
143 (long long)st->st_ino);
149 if (body->valid & OBD_MD_FLMTIME &&
150 body->mtime > LTIME_S(st->st_mtime))
151 LTIME_S(st->st_mtime) = body->mtime;
152 if (body->valid & OBD_MD_FLATIME &&
153 body->atime > LTIME_S(st->st_atime))
154 LTIME_S(st->st_atime) = body->atime;
156 /* mtime is always updated with ctime, but can be set in past.
157 As write and utime(2) may happen within 1 second, and utime's
158 mtime has a priority over write's one, so take mtime from mds
159 for the same ctimes. */
160 if (body->valid & OBD_MD_FLCTIME &&
161 body->ctime >= LTIME_S(st->st_ctime)) {
162 LTIME_S(st->st_ctime) = body->ctime;
163 if (body->valid & OBD_MD_FLMTIME)
164 LTIME_S(st->st_mtime) = body->mtime;
166 if (body->valid & OBD_MD_FLMODE)
167 st->st_mode = (st->st_mode & S_IFMT)|(body->mode & ~S_IFMT);
168 if (body->valid & OBD_MD_FLTYPE)
169 st->st_mode = (st->st_mode & ~S_IFMT)|(body->mode & S_IFMT);
170 if (S_ISREG(st->st_mode))
171 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
173 st->st_blksize = 4096;
174 if (body->valid & OBD_MD_FLUID)
175 st->st_uid = body->uid;
176 if (body->valid & OBD_MD_FLGID)
177 st->st_gid = body->gid;
178 if (body->valid & OBD_MD_FLNLINK)
179 st->st_nlink = body->nlink;
180 if (body->valid & OBD_MD_FLRDEV)
181 st->st_rdev = body->rdev;
182 if (body->valid & OBD_MD_FLSIZE)
183 st->st_size = body->size;
184 if (body->valid & OBD_MD_FLBLOCKS)
185 st->st_blocks = body->blocks;
186 if (body->valid & OBD_MD_FLFLAGS)
187 lli->lli_st_flags = body->flags;
190 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
192 struct llu_inode_info *lli = llu_i2info(dst);
193 struct intnl_stat *st = llu_i2stat(dst);
195 valid &= src->o_valid;
197 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
198 CDEBUG(D_INODE,"valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
200 LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
201 (long)src->o_mtime, (long)src->o_ctime);
203 if (valid & OBD_MD_FLATIME)
204 LTIME_S(st->st_atime) = src->o_atime;
205 if (valid & OBD_MD_FLMTIME)
206 LTIME_S(st->st_mtime) = src->o_mtime;
207 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
208 LTIME_S(st->st_ctime) = src->o_ctime;
209 if (valid & OBD_MD_FLSIZE)
210 st->st_size = src->o_size;
211 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
212 st->st_blocks = src->o_blocks;
213 if (valid & OBD_MD_FLBLKSZ)
214 st->st_blksize = src->o_blksize;
215 if (valid & OBD_MD_FLTYPE)
216 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
217 if (valid & OBD_MD_FLMODE)
218 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
219 if (valid & OBD_MD_FLUID)
220 st->st_uid = src->o_uid;
221 if (valid & OBD_MD_FLGID)
222 st->st_gid = src->o_gid;
223 if (valid & OBD_MD_FLFLAGS)
224 lli->lli_st_flags = src->o_flags;
227 #define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
228 #define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
230 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
232 struct llu_inode_info *lli = llu_i2info(src);
233 struct intnl_stat *st = llu_i2stat(src);
234 obd_flag newvalid = 0;
236 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
237 CDEBUG(D_INODE, "valid %x, new time %lu/%lu\n",
238 valid, LTIME_S(st->st_mtime),
239 LTIME_S(st->st_ctime));
241 if (valid & OBD_MD_FLATIME) {
242 dst->o_atime = LTIME_S(st->st_atime);
243 newvalid |= OBD_MD_FLATIME;
245 if (valid & OBD_MD_FLMTIME) {
246 dst->o_mtime = LTIME_S(st->st_mtime);
247 newvalid |= OBD_MD_FLMTIME;
249 if (valid & OBD_MD_FLCTIME) {
250 dst->o_ctime = LTIME_S(st->st_ctime);
251 newvalid |= OBD_MD_FLCTIME;
253 if (valid & OBD_MD_FLSIZE) {
254 dst->o_size = st->st_size;
255 newvalid |= OBD_MD_FLSIZE;
257 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
258 dst->o_blocks = st->st_blocks;
259 newvalid |= OBD_MD_FLBLOCKS;
261 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
262 dst->o_blksize = st->st_blksize;
263 newvalid |= OBD_MD_FLBLKSZ;
265 if (valid & OBD_MD_FLTYPE) {
266 dst->o_mode = (dst->o_mode & S_IALLUGO)|(st->st_mode & S_IFMT);
267 newvalid |= OBD_MD_FLTYPE;
269 if (valid & OBD_MD_FLMODE) {
270 dst->o_mode = (dst->o_mode & S_IFMT)|(st->st_mode & S_IALLUGO);
271 newvalid |= OBD_MD_FLMODE;
273 if (valid & OBD_MD_FLUID) {
274 dst->o_uid = st->st_uid;
275 newvalid |= OBD_MD_FLUID;
277 if (valid & OBD_MD_FLGID) {
278 dst->o_gid = st->st_gid;
279 newvalid |= OBD_MD_FLGID;
281 if (valid & OBD_MD_FLFLAGS) {
282 dst->o_flags = lli->lli_st_flags;
283 newvalid |= OBD_MD_FLFLAGS;
285 if (valid & OBD_MD_FLGENER) {
286 dst->o_generation = lli->lli_st_generation;
287 newvalid |= OBD_MD_FLGENER;
289 if (valid & OBD_MD_FLFID) {
290 dst->o_fid = st->st_ino;
291 newvalid |= OBD_MD_FLFID;
294 dst->o_valid |= newvalid;
298 * really does the getattr on the inode and updates its fields
300 int llu_inode_getattr(struct inode *inode, struct obdo *obdo)
302 struct llu_inode_info *lli = llu_i2info(inode);
303 struct ptlrpc_request_set *set;
304 struct lov_stripe_md *lsm = lli->lli_smd;
305 struct obd_info oinfo = { { { 0 } } };
313 oinfo.oi_oa->o_id = lsm->lsm_object_id;
314 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
315 oinfo.oi_oa->o_mode = S_IFREG;
316 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
317 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
318 OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
321 set = ptlrpc_prep_set();
323 CERROR ("ENOMEM allocing request set\n");
326 rc = obd_getattr_async(llu_i2obdexp(inode), &oinfo, set);
328 rc = ptlrpc_set_wait(set);
329 ptlrpc_set_destroy(set);
334 oinfo.oi_oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
335 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
338 obdo_refresh_inode(inode, oinfo.oi_oa, oinfo.oi_oa->o_valid);
339 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %Lu, "
340 "blksize %Lu\n", lli->lli_smd->lsm_object_id,
341 (long long unsigned)llu_i2stat(inode)->st_size,
342 (long long unsigned)llu_i2stat(inode)->st_blocks,
343 (long long unsigned)llu_i2stat(inode)->st_blksize);
347 static struct inode* llu_new_inode(struct filesys *fs,
351 struct llu_inode_info *lli;
352 struct intnl_stat st = {
355 #ifndef AUTOMOUNT_FILE_NAME
356 .st_mode = fid->f_type & S_IFMT,
358 .st_mode = fid->f_type /* all of the bits! */
361 /* FIXME: fix this later */
368 OBD_ALLOC(lli, sizeof(*lli));
372 /* initialize lli here */
373 lli->lli_sbi = llu_fs2sbi(fs);
375 lli->lli_symlink_name = NULL;
377 lli->lli_maxbytes = (__u64)(~0UL);
378 lli->lli_file_data = NULL;
380 lli->lli_sysio_fid.fid_data = &lli->lli_fid;
381 lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
384 /* file identifier is needed by functions like _sysio_i_find() */
385 inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
386 &st, 0, &llu_inode_ops, lli);
389 OBD_FREE(lli, sizeof(*lli));
394 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
396 struct llu_sb_info *sbi = llu_i2sbi(inode);
397 struct llu_inode_info *lli = llu_i2info(inode);
398 struct lustre_handle lockh;
399 struct ldlm_res_id res_id = { .name = {0} };
400 struct obd_device *obddev;
401 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
407 obddev = sbi->ll_md_exp->exp_obd;
408 res_id.name[0] = fid_seq(&lli->lli_fid);
409 res_id.name[1] = fid_oid(&lli->lli_fid);
410 res_id.name[2] = fid_ver(&lli->lli_fid);
412 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
414 /* FIXME use LDLM_FL_TEST_LOCK instead */
415 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
416 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
417 &policy, LCK_PR, &lockh)) {
418 ldlm_lock_decref(&lockh, LCK_PR);
422 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
423 &policy, LCK_PW, &lockh)) {
424 ldlm_lock_decref(&lockh, LCK_PW);
430 static int llu_inode_revalidate(struct inode *inode)
432 struct lov_stripe_md *lsm = NULL;
436 CERROR("REPORT THIS LINE TO PETER\n");
440 if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
442 struct ptlrpc_request *req = NULL;
443 struct llu_sb_info *sbi = llu_i2sbi(inode);
444 unsigned long valid = OBD_MD_FLGETATTR;
447 /* Why don't we update all valid MDS fields here, if we're
448 * doing an RPC anyways? -phil */
449 if (S_ISREG(llu_i2stat(inode)->st_mode)) {
450 ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
451 valid |= OBD_MD_FLEASIZE;
453 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
454 NULL, valid, ealen, &req);
456 CERROR("failure %d inode %llu\n", rc,
457 (long long)llu_i2stat(inode)->st_ino);
460 rc = md_get_lustre_md(sbi->ll_md_exp, req, REPLY_REC_OFF,
461 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
463 /* XXX Too paranoid? */
464 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
465 !((md.body->valid & OBD_MD_FLNLINK) &&
466 (md.body->nlink == 0))) {
467 CERROR("Asked for %s eadata but got %s (%d)\n",
468 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
469 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
470 md.body->eadatasize);
473 ptlrpc_req_finished(req);
478 llu_update_inode(inode, md.body, md.lsm);
479 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
480 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
481 if (md.body->valid & OBD_MD_FLSIZE)
482 llu_i2info(inode)->lli_flags |= LLIF_MDS_SIZE_LOCK;
483 ptlrpc_req_finished(req);
486 lsm = llu_i2info(inode)->lli_smd;
487 if (!lsm) /* object not yet allocated, don't validate size */
490 /* ll_glimpse_size will prefer locally cached writes if they extend
492 RETURN(llu_glimpse_size(inode));
495 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
497 *b = *llu_i2stat(ino);
500 static int llu_iop_getattr(struct pnode *pno,
502 struct intnl_stat *b)
507 liblustre_wait_event(0);
511 LASSERT(pno->p_base->pb_ino);
512 ino = pno->p_base->pb_ino;
514 LASSERT(!pno || pno->p_base->pb_ino == ino);
517 /* libsysio might call us directly without intent lock,
518 * we must re-fetch the attrs here
520 rc = llu_inode_revalidate(ino);
522 copy_stat_buf(ino, b);
523 LASSERT(!llu_i2info(ino)->lli_it);
526 liblustre_wait_event(0);
530 static int null_if_equal(struct ldlm_lock *lock, void *data)
532 if (data == lock->l_ast_data) {
533 lock->l_ast_data = NULL;
535 if (lock->l_req_mode != lock->l_granted_mode)
536 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
539 return LDLM_ITER_CONTINUE;
542 void llu_clear_inode(struct inode *inode)
544 struct llu_inode_info *lli = llu_i2info(inode);
545 struct llu_sb_info *sbi = llu_i2sbi(inode);
548 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
549 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
552 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
553 md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
554 null_if_equal, inode);
557 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
558 null_if_equal, inode);
561 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
565 if (lli->lli_symlink_name) {
566 OBD_FREE(lli->lli_symlink_name,
567 strlen(lli->lli_symlink_name) + 1);
568 lli->lli_symlink_name = NULL;
574 void llu_iop_gone(struct inode *inode)
576 struct llu_inode_info *lli = llu_i2info(inode);
579 liblustre_wait_event(0);
580 llu_clear_inode(inode);
582 OBD_FREE(lli, sizeof(*lli));
586 static int inode_setattr(struct inode * inode, struct iattr * attr)
588 unsigned int ia_valid = attr->ia_valid;
589 struct intnl_stat *st = llu_i2stat(inode);
593 * inode_setattr() is only ever invoked with ATTR_SIZE (by
594 * llu_setattr_raw()) when file has no bodies. Check this.
596 LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
598 if (ia_valid & ATTR_SIZE)
599 st->st_size = attr->ia_size;
600 if (ia_valid & ATTR_UID)
601 st->st_uid = attr->ia_uid;
602 if (ia_valid & ATTR_GID)
603 st->st_gid = attr->ia_gid;
604 if (ia_valid & ATTR_ATIME)
605 st->st_atime = attr->ia_atime;
606 if (ia_valid & ATTR_MTIME)
607 st->st_mtime = attr->ia_mtime;
608 if (ia_valid & ATTR_CTIME)
609 st->st_ctime = attr->ia_ctime;
610 if (ia_valid & ATTR_MODE) {
611 st->st_mode = attr->ia_mode;
612 if (!in_group_p(st->st_gid) && !capable(CAP_FSETID))
613 st->st_mode &= ~S_ISGID;
615 /* mark_inode_dirty(inode); */
619 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data)
622 struct llu_sb_info *sbi = llu_i2sbi(inode);
623 struct ptlrpc_request *request = NULL;
627 llu_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0);
628 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL, 0, &request);
631 ptlrpc_req_finished(request);
632 if (rc != -EPERM && rc != -EACCES)
633 CERROR("md_setattr fails: rc = %d\n", rc);
637 rc = md_get_lustre_md(sbi->ll_md_exp, request, REPLY_REC_OFF,
638 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
640 ptlrpc_req_finished(request);
644 /* We call inode_setattr to adjust timestamps.
645 * If there is at least some data in file, we cleared ATTR_SIZE
646 * above to avoid invoking vmtruncate, otherwise it is important
647 * to call vmtruncate in inode_setattr to update inode->i_size
649 inode_setattr(inode, &op_data->attr);
650 llu_update_inode(inode, md.body, md.lsm);
651 ptlrpc_req_finished(request);
656 /* Close IO epoch and send Size-on-MDS attribute update. */
657 static int llu_setattr_done_writing(struct inode *inode,
658 struct md_op_data *op_data)
660 struct llu_inode_info *lli = llu_i2info(inode);
661 struct intnl_stat *st = llu_i2stat(inode);
665 LASSERT(op_data != NULL);
666 if (!S_ISREG(st->st_mode))
669 /* XXX: pass och here for the recovery purpose. */
670 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
671 op_data->ioepoch, PFID(&lli->lli_fid));
673 op_data->flags = MF_EPOCH_CLOSE | MF_SOM_CHANGE;
674 rc = md_done_writing(llu_i2sbi(inode)->ll_md_exp, op_data, NULL);
676 /* MDS has instructed us to obtain Size-on-MDS attribute
677 * from OSTs and send setattr to back to MDS. */
678 rc = llu_sizeonmds_update(inode, &op_data->handle);
680 CERROR("inode %llu mdc truncate failed: rc = %d\n",
686 /* If this inode has objects allocated to it (lsm != NULL), then the OST
687 * object(s) determine the file size and mtime. Otherwise, the MDS will
688 * keep these values until such a time that objects are allocated for it.
689 * We do the MDS operations first, as it is checking permissions for us.
690 * We don't to the MDS RPC if there is nothing that we want to store there,
691 * otherwise there is no harm in updating mtime/atime on the MDS if we are
692 * going to do an RPC anyways.
694 * If we are doing a truncate, we will send the mtime and ctime updates
695 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
696 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
699 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
701 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
702 struct llu_sb_info *sbi = llu_i2sbi(inode);
703 struct intnl_stat *st = llu_i2stat(inode);
704 int ia_valid = attr->ia_valid;
705 struct md_op_data op_data = { { 0 } };
709 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
711 if (ia_valid & ATTR_SIZE) {
712 if (attr->ia_size > ll_file_maxbytes(inode)) {
713 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
714 (long long)attr->ia_size,
715 ll_file_maxbytes(inode));
719 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
722 /* We mark all of the fields "set" so MDS/OST does not re-set them */
723 if (attr->ia_valid & ATTR_CTIME) {
724 attr->ia_ctime = CURRENT_TIME;
725 attr->ia_valid |= ATTR_CTIME_SET;
727 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
728 attr->ia_atime = CURRENT_TIME;
729 attr->ia_valid |= ATTR_ATIME_SET;
731 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
732 attr->ia_mtime = CURRENT_TIME;
733 attr->ia_valid |= ATTR_MTIME_SET;
735 if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
736 /* To avoid stale mtime on mds, obtain it from ost and send
738 rc = llu_glimpse_size(inode);
742 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
743 attr->ia_mtime = inode->i_stbuf.st_mtime;
746 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
747 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
748 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
749 LTIME_S(CURRENT_TIME));
751 /* NB: ATTR_SIZE will only be set after this point if the size
752 * resides on the MDS, ie, this file has no objects. */
754 attr->ia_valid &= ~ATTR_SIZE;
756 /* If only OST attributes being set on objects, don't do MDS RPC.
757 * In that case, we need to check permissions and update the local
758 * inode ourselves so we can call obdo_from_inode() always. */
759 if (ia_valid & (lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
760 memcpy(&op_data.attr, attr, sizeof(*attr));
762 /* Open epoch for truncate. */
763 if (ia_valid & ATTR_SIZE)
764 op_data.flags = MF_EPOCH_OPEN;
765 rc = llu_md_setattr(inode, &op_data);
769 if (!lsm || !S_ISREG(st->st_mode)) {
770 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
772 rc = llu_setattr_done_writing(inode, &op_data);
776 /* The OST doesn't check permissions, but the alternative is
777 * a gratuitous RPC to the MDS. We already rely on the client
778 * to do read/write/truncate permission checks, so is mtime OK?
780 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
781 /* from sys_utime() */
782 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
783 if (current->fsuid != st->st_uid &&
784 (rc = ll_permission(inode, MAY_WRITE)) != 0)
787 /* from inode_change_ok() */
788 if (current->fsuid != st->st_uid &&
789 !capable(CAP_FOWNER))
795 /* Won't invoke llu_vmtruncate(), as we already cleared
797 inode_setattr(inode, attr);
800 if (ia_valid & ATTR_SIZE) {
801 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
803 struct lustre_handle lockh = { 0, };
804 struct lustre_handle match_lockh = { 0, };
807 int flags = LDLM_FL_TEST_LOCK; /* for assertion check below */
811 /* check that there are no matching locks */
812 LASSERT(obd_match(sbi->ll_dt_exp, lsm, LDLM_EXTENT, &policy,
813 LCK_PW, &flags, inode, &match_lockh) <= 0);
815 /* XXX when we fix the AST intents to pass the discard-range
816 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
818 flags = (attr->ia_size == 0) ? LDLM_AST_DISCARD_DATA : 0;
820 if (sbi->ll_lco.lco_flags & OBD_CONNECT_TRUNCLOCK) {
822 obd_flags = OBD_FL_TRUNCLOCK;
823 CDEBUG(D_INODE, "delegating locking to the OST");
829 /* with lock_mode == LK_NL no lock is taken. */
830 rc = llu_extent_lock(NULL, inode, lsm, lock_mode, &policy,
832 if (rc != ELDLM_OK) {
838 rc = llu_vmtruncate(inode, attr->ia_size, obd_flags);
840 /* unlock now as we don't mind others file lockers racing with
841 * the mds updates below? */
842 err = llu_extent_unlock(NULL, inode, lsm, lock_mode, &lockh);
844 CERROR("llu_extent_unlock failed: %d\n", err);
850 rc = llu_setattr_done_writing(inode, &op_data);
851 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
852 struct obd_info oinfo = { { { 0 } } };
855 CDEBUG(D_INODE, "set mtime on OST inode %llu to %lu\n",
856 (long long)st->st_ino, LTIME_S(attr->ia_mtime));
857 oa.o_id = lsm->lsm_object_id;
858 oa.o_valid = OBD_MD_FLID;
860 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
861 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
866 rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
868 CERROR("obd_setattr_async fails: rc=%d\n", rc);
873 /* here we simply act as a thin layer to glue it with
874 * llu_setattr_raw(), which is copy from kernel
876 static int llu_iop_setattr(struct pnode *pno,
879 struct intnl_stat *stbuf)
885 liblustre_wait_event(0);
887 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
888 SETATTR_UID | SETATTR_GID |
889 SETATTR_LEN | SETATTR_MODE)));
890 memset(&iattr, 0, sizeof(iattr));
892 if (mask & SETATTR_MODE) {
893 iattr.ia_mode = stbuf->st_mode;
894 iattr.ia_valid |= ATTR_MODE;
896 if (mask & SETATTR_MTIME) {
897 iattr.ia_mtime = stbuf->st_mtime;
898 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
900 if (mask & SETATTR_ATIME) {
901 iattr.ia_atime = stbuf->st_atime;
902 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
904 if (mask & SETATTR_UID) {
905 iattr.ia_uid = stbuf->st_uid;
906 iattr.ia_valid |= ATTR_UID;
908 if (mask & SETATTR_GID) {
909 iattr.ia_gid = stbuf->st_gid;
910 iattr.ia_valid |= ATTR_GID;
912 if (mask & SETATTR_LEN) {
913 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
914 iattr.ia_valid |= ATTR_SIZE;
917 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
918 iattr.ia_ctime = CURRENT_TIME;
920 rc = llu_setattr_raw(ino, &iattr);
921 liblustre_wait_event(0);
925 #define EXT2_LINK_MAX 32000
927 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
929 struct inode *dir = pno->p_base->pb_parent->pb_ino;
930 struct qstr *qstr = &pno->p_base->pb_name;
931 const char *name = qstr->name;
933 struct ptlrpc_request *request = NULL;
934 struct llu_sb_info *sbi = llu_i2sbi(dir);
935 struct md_op_data op_data;
936 struct lu_placement_hint hint = {
939 .ph_opc = LUSTRE_OPC_SYMLINK
944 liblustre_wait_event(0);
945 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
948 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0);
950 /* allocate new fid */
951 err = llu_fid_md_alloc(sbi, &op_data.fid2, &hint);
953 CERROR("can't allocate new fid, rc %d\n", err);
956 err = md_create(sbi->ll_md_exp, &op_data,
957 tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
958 current->fsuid, current->fsgid, current->cap_effective,
960 ptlrpc_req_finished(request);
961 liblustre_wait_event(0);
965 static int llu_readlink_internal(struct inode *inode,
966 struct ptlrpc_request **request,
969 struct llu_inode_info *lli = llu_i2info(inode);
970 struct llu_sb_info *sbi = llu_i2sbi(inode);
971 struct mdt_body *body;
972 struct intnl_stat *st = llu_i2stat(inode);
973 int rc, symlen = st->st_size + 1;
978 if (lli->lli_symlink_name) {
979 *symname = lli->lli_symlink_name;
980 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
984 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), NULL,
985 OBD_MD_LINKNAME, symlen, request);
987 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
991 body = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF,
993 LASSERT(body != NULL);
994 LASSERT_REPSWABBED(*request, REPLY_REC_OFF);
996 if ((body->valid & OBD_MD_LINKNAME) == 0) {
997 CERROR ("OBD_MD_LINKNAME not set on reply\n");
998 GOTO (failed, rc = -EPROTO);
1001 LASSERT(symlen != 0);
1002 if (body->eadatasize != symlen) {
1003 CERROR("inode %llu: symlink length %d not expected %d\n",
1004 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
1005 GOTO(failed, rc = -EPROTO);
1008 *symname = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF + 1,
1010 if (*symname == NULL ||
1011 strnlen(*symname, symlen) != symlen - 1) {
1012 /* not full/NULL terminated */
1013 CERROR("inode %llu: symlink not NULL terminated string"
1014 "of length %d\n", (long long)st->st_ino, symlen - 1);
1015 GOTO(failed, rc = -EPROTO);
1018 OBD_ALLOC(lli->lli_symlink_name, symlen);
1019 /* do not return an error if we cannot cache the symlink locally */
1020 if (lli->lli_symlink_name)
1021 memcpy(lli->lli_symlink_name, *symname, symlen);
1026 ptlrpc_req_finished (*request);
1030 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
1032 struct inode *inode = pno->p_base->pb_ino;
1033 struct ptlrpc_request *request;
1038 liblustre_wait_event(0);
1039 rc = llu_readlink_internal(inode, &request, &symname);
1044 strncpy(data, symname, bufsize);
1045 rc = strlen(symname);
1047 ptlrpc_req_finished(request);
1049 liblustre_wait_event(0);
1053 static int llu_iop_mknod_raw(struct pnode *pno,
1057 struct ptlrpc_request *request = NULL;
1058 struct inode *dir = pno->p_parent->p_base->pb_ino;
1059 struct llu_sb_info *sbi = llu_i2sbi(dir);
1060 struct md_op_data op_data;
1062 struct lu_placement_hint hint = {
1064 .ph_cname = &pno->p_base->pb_name,
1065 .ph_opc = LUSTRE_OPC_MKNOD
1069 liblustre_wait_event(0);
1070 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
1071 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
1072 (long long)llu_i2stat(dir)->st_ino);
1074 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
1077 switch (mode & S_IFMT) {
1080 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1085 llu_prep_md_op_data(&op_data, dir, NULL,
1086 pno->p_base->pb_name.name,
1087 pno->p_base->pb_name.len, 0);
1088 /* allocate new fid */
1089 err = llu_fid_md_alloc(sbi, &op_data.fid2, &hint);
1091 CERROR("can't allocate new fid, rc %d\n", err);
1095 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
1096 current->fsuid, current->fsgid,
1097 current->cap_effective, dev, &request);
1098 ptlrpc_req_finished(request);
1106 liblustre_wait_event(0);
1110 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1112 struct inode *src = old->p_base->pb_ino;
1113 struct inode *dir = new->p_parent->p_base->pb_ino;
1114 const char *name = new->p_base->pb_name.name;
1115 int namelen = new->p_base->pb_name.len;
1116 struct ptlrpc_request *request = NULL;
1117 struct md_op_data op_data;
1124 liblustre_wait_event(0);
1125 llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0);
1126 rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1127 ptlrpc_req_finished(request);
1128 liblustre_wait_event(0);
1134 * libsysio will clear the inode immediately after return
1136 static int llu_iop_unlink_raw(struct pnode *pno)
1138 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1139 struct qstr *qstr = &pno->p_base->pb_name;
1140 const char *name = qstr->name;
1141 int len = qstr->len;
1142 struct inode *target = pno->p_base->pb_ino;
1143 struct ptlrpc_request *request = NULL;
1144 struct md_op_data op_data;
1150 liblustre_wait_event(0);
1151 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0);
1152 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1154 rc = llu_objects_destroy(request, dir);
1155 ptlrpc_req_finished(request);
1156 liblustre_wait_event(0);
1161 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1163 struct inode *src = old->p_parent->p_base->pb_ino;
1164 struct inode *tgt = new->p_parent->p_base->pb_ino;
1165 const char *oldname = old->p_base->pb_name.name;
1166 int oldnamelen = old->p_base->pb_name.len;
1167 const char *newname = new->p_base->pb_name.name;
1168 int newnamelen = new->p_base->pb_name.len;
1169 struct ptlrpc_request *request = NULL;
1170 struct md_op_data op_data;
1177 liblustre_wait_event(0);
1178 llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0);
1179 rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1180 oldname, oldnamelen, newname, newnamelen,
1183 rc = llu_objects_destroy(request, src);
1186 ptlrpc_req_finished(request);
1187 liblustre_wait_event(0);
1192 #ifdef _HAVE_STATVFS
1193 static int llu_statfs_internal(struct llu_sb_info *sbi,
1194 struct obd_statfs *osfs, __u64 max_age)
1196 struct obd_statfs obd_osfs;
1200 rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age);
1202 CERROR("md_statfs fails: rc = %d\n", rc);
1206 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1207 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1209 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1210 &obd_statfs, max_age);
1212 CERROR("obd_statfs fails: rc = %d\n", rc);
1216 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1217 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1220 osfs->os_blocks = obd_osfs.os_blocks;
1221 osfs->os_bfree = obd_osfs.os_bfree;
1222 osfs->os_bavail = obd_osfs.os_bavail;
1224 /* If we don't have as many objects free on the OST as inodes
1225 * on the MDS, we reduce the total number of inodes to
1226 * compensate, so that the "inodes in use" number is correct.
1228 if (obd_osfs.os_ffree < osfs->os_ffree) {
1229 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1231 osfs->os_ffree = obd_osfs.os_ffree;
1237 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1239 struct obd_statfs osfs;
1242 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1244 /* For now we will always get up-to-date statfs values, but in the
1245 * future we may allow some amount of caching on the client (e.g.
1246 * from QOS or lprocfs updates). */
1247 rc = llu_statfs_internal(sbi, &osfs, cfs_time_current_64() - HZ);
1251 statfs_unpack(sfs, &osfs);
1253 if (sizeof(sfs->f_blocks) == 4) {
1254 while (osfs.os_blocks > ~0UL) {
1257 osfs.os_blocks >>= 1;
1258 osfs.os_bfree >>= 1;
1259 osfs.os_bavail >>= 1;
1263 sfs->f_blocks = osfs.os_blocks;
1264 sfs->f_bfree = osfs.os_bfree;
1265 sfs->f_bavail = osfs.os_bavail;
1270 static int llu_iop_statvfs(struct pnode *pno,
1272 struct intnl_statvfs *buf)
1278 liblustre_wait_event(0);
1281 LASSERT(pno->p_base->pb_ino);
1282 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1286 /* from native driver */
1287 buf->f_bsize = fs.f_bsize; /* file system block size */
1288 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1289 buf->f_blocks = fs.f_blocks;
1290 buf->f_bfree = fs.f_bfree;
1291 buf->f_bavail = fs.f_bavail;
1292 buf->f_files = fs.f_files; /* Total number serial numbers */
1293 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1294 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1295 buf->f_fsid = fs.f_fsid.__val[1];
1296 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1297 buf->f_namemax = fs.f_namelen;
1300 liblustre_wait_event(0);
1303 #endif /* _HAVE_STATVFS */
1305 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1307 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1308 struct qstr *qstr = &pno->p_base->pb_name;
1309 const char *name = qstr->name;
1310 int len = qstr->len;
1311 struct ptlrpc_request *request = NULL;
1312 struct intnl_stat *st = llu_i2stat(dir);
1313 struct md_op_data op_data;
1314 struct lu_placement_hint hint = {
1317 .ph_opc = LUSTRE_OPC_MKDIR
1323 liblustre_wait_event(0);
1324 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1325 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1327 if (st->st_nlink >= EXT2_LINK_MAX)
1330 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0);
1332 /* allocate new fid */
1333 err = llu_fid_md_alloc(llu_i2sbi(dir), &op_data.fid2, &hint);
1335 CERROR("can't allocate new fid, rc %d\n", err);
1338 err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0, mode,
1339 current->fsuid, current->fsgid, current->cap_effective,
1341 ptlrpc_req_finished(request);
1342 liblustre_wait_event(0);
1346 static int llu_iop_rmdir_raw(struct pnode *pno)
1348 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1349 struct qstr *qstr = &pno->p_base->pb_name;
1350 const char *name = qstr->name;
1351 int len = qstr->len;
1352 struct ptlrpc_request *request = NULL;
1353 struct md_op_data op_data;
1357 liblustre_wait_event(0);
1358 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1359 (long long)llu_i2stat(dir)->st_ino,
1360 llu_i2info(dir)->lli_st_generation, dir);
1362 llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR);
1363 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1364 ptlrpc_req_finished(request);
1366 liblustre_wait_event(0);
1371 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1373 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1375 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1377 /* refer to ll_file_flock() for details */
1378 static int llu_file_flock(struct inode *ino,
1380 struct file_lock *file_lock)
1382 struct llu_inode_info *lli = llu_i2info(ino);
1383 struct intnl_stat *st = llu_i2stat(ino);
1384 struct ldlm_res_id res_id =
1385 { .name = {fid_seq(&lli->lli_fid),
1386 fid_oid(&lli->lli_fid),
1387 fid_ver(&lli->lli_fid),
1389 struct lustre_handle lockh = {0};
1390 ldlm_policy_data_t flock;
1391 ldlm_mode_t mode = 0;
1395 CDEBUG(D_VFSTRACE, "VFS Op:inode="LPU64" file_lock=%p\n",
1396 st->st_ino, file_lock);
1398 flock.l_flock.pid = file_lock->fl_pid;
1399 flock.l_flock.start = file_lock->fl_start;
1400 flock.l_flock.end = file_lock->fl_end;
1402 switch (file_lock->fl_type) {
1413 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1420 #if F_SETLKW64 != F_SETLKW
1428 #if F_SETLK64 != F_SETLK
1432 flags = LDLM_FL_BLOCK_NOWAIT;
1436 #if F_GETLK64 != F_GETLK
1440 flags = LDLM_FL_TEST_LOCK;
1441 file_lock->fl_type = mode;
1444 CERROR("unknown fcntl cmd: %d\n", cmd);
1448 CDEBUG(D_DLMTRACE, "inode="LPU64", pid=%u, flags=%#x, mode=%u, "
1449 "start="LPU64", end="LPU64"\n", st->st_ino, flock.l_flock.pid,
1450 flags, mode, flock.l_flock.start, flock.l_flock.end);
1452 rc = ldlm_cli_enqueue(llu_i2mdcexp(ino), NULL, &res_id,
1453 LDLM_FLOCK, &flock, mode, &flags, NULL,
1454 ldlm_flock_completion_ast, NULL,
1455 file_lock, NULL, 0, NULL, &lockh, 0);
1459 static int assign_type(struct file_lock *fl, int type)
1472 static int flock_to_posix_lock(struct inode *ino,
1473 struct file_lock *fl,
1476 switch (l->l_whence) {
1477 /* XXX: only SEEK_SET is supported in lustre */
1485 fl->fl_end = l->l_len - 1;
1489 fl->fl_end = OFFSET_MAX;
1491 fl->fl_pid = getpid();
1492 fl->fl_flags = FL_POSIX;
1493 fl->fl_notify = NULL;
1494 fl->fl_insert = NULL;
1495 fl->fl_remove = NULL;
1496 /* XXX: these fields can't be filled with suitable values,
1497 but I think lustre doesn't use them.
1499 fl->fl_owner = NULL;
1502 return assign_type(fl, l->l_type);
1505 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1507 struct file_lock fl;
1511 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1514 error = flock_to_posix_lock(ino, &fl, flock);
1518 error = llu_file_flock(ino, F_GETLK, &fl);
1522 flock->l_type = F_UNLCK;
1523 if (fl.fl_type != F_UNLCK) {
1524 flock->l_pid = fl.fl_pid;
1525 flock->l_start = fl.fl_start;
1526 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1527 fl.fl_end - fl.fl_start + 1;
1528 flock->l_whence = SEEK_SET;
1529 flock->l_type = fl.fl_type;
1536 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1538 struct file_lock fl;
1539 int flags = llu_i2info(ino)->lli_open_flags + 1;
1542 error = flock_to_posix_lock(ino, &fl, flock);
1545 if (cmd == F_SETLKW)
1546 fl.fl_flags |= FL_SLEEP;
1549 switch (flock->l_type) {
1551 if (!(flags & FMODE_READ))
1555 if (!(flags & FMODE_WRITE))
1565 error = llu_file_flock(ino, cmd, &fl);
1573 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1575 struct llu_inode_info *lli = llu_i2info(ino);
1577 struct flock *flock;
1580 liblustre_wait_event(0);
1583 *rtn = lli->lli_open_flags;
1586 flags = va_arg(ap, long);
1587 flags &= FCNTL_FLMASK;
1588 if (flags & FCNTL_FLMASK_INVALID) {
1589 CERROR("liblustre don't support O_NONBLOCK, O_ASYNC, "
1590 "and O_DIRECT on file descriptor\n");
1595 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1596 (lli->lli_open_flags & ~FCNTL_FLMASK);
1601 #if F_GETLK64 != F_GETLK
1605 flock = va_arg(ap, struct flock *);
1606 err = llu_fcntl_getlk(ino, flock);
1611 #if F_SETLKW64 != F_SETLKW
1617 #if F_SETLK64 != F_SETLK
1621 flock = va_arg(ap, struct flock *);
1622 err = llu_fcntl_setlk(ino, cmd, flock);
1626 CERROR("unsupported fcntl cmd %x\n", cmd);
1632 liblustre_wait_event(0);
1636 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1638 struct llu_inode_info *lli = llu_i2info(inode);
1639 struct ll_file_data *fd = lli->lli_file_data;
1640 ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1641 .end = OBD_OBJECT_EOF}};
1642 struct lustre_handle lockh = { 0 };
1643 struct lov_stripe_md *lsm = lli->lli_smd;
1648 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1652 policy.l_extent.gid = arg;
1653 if (lli->lli_open_flags & O_NONBLOCK)
1654 flags = LDLM_FL_BLOCK_NOWAIT;
1656 err = llu_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh,
1661 fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1663 memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1668 static int llu_put_grouplock(struct inode *inode, unsigned long arg)
1670 struct llu_inode_info *lli = llu_i2info(inode);
1671 struct ll_file_data *fd = lli->lli_file_data;
1672 struct lov_stripe_md *lsm = lli->lli_smd;
1676 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1679 if (fd->fd_gid != arg)
1682 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1684 err = llu_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1689 memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1694 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1696 struct llu_sb_info *sbi = llu_i2sbi(ino);
1697 struct ptlrpc_request *request = NULL;
1698 struct md_op_data op_data;
1699 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1702 llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0);
1704 LASSERT(sizeof(lum) == sizeof(*lump));
1705 LASSERT(sizeof(lum.lmm_objects[0]) ==
1706 sizeof(lump->lmm_objects[0]));
1707 rc = copy_from_user(&lum, lump, sizeof(lum));
1711 if (lum.lmm_magic != LOV_USER_MAGIC)
1714 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
1715 lustre_swab_lov_user_md(&lum);
1717 /* swabbing is done in lov_setstripe() on server side */
1718 rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1719 sizeof(lum), NULL, 0, &request);
1721 ptlrpc_req_finished(request);
1722 if (rc != -EPERM && rc != -EACCES)
1723 CERROR("md_setattr fails: rc = %d\n", rc);
1726 ptlrpc_req_finished(request);
1731 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1732 struct lov_user_md *lum, int lum_size)
1734 struct llu_sb_info *sbi = llu_i2sbi(ino);
1735 struct llu_inode_info *lli = llu_i2info(ino);
1736 struct llu_inode_info *lli2 = NULL;
1737 struct lov_stripe_md *lsm;
1738 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1739 struct ptlrpc_request *req = NULL;
1740 struct lustre_md md;
1741 struct md_op_data data;
1742 struct lustre_handle lockh;
1748 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1749 PFID(&lli->lli_fid));
1753 OBD_ALLOC(lli2, sizeof(struct llu_inode_info));
1757 memcpy(lli2, lli, sizeof(struct llu_inode_info));
1758 lli2->lli_open_count = 0;
1759 lli2->lli_it = NULL;
1760 lli2->lli_file_data = NULL;
1761 lli2->lli_smd = NULL;
1762 lli2->lli_symlink_name = NULL;
1763 ino->i_private = lli2;
1765 llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR);
1767 rc = md_enqueue(sbi->ll_md_exp, LDLM_IBITS, &oit, LCK_CR, &data,
1768 &lockh, lum, lum_size, ldlm_completion_ast,
1769 llu_md_blocking_ast, NULL, LDLM_FL_INTENT_ONLY);
1773 req = oit.d.lustre.it_data;
1774 rc = it_open_error(DISP_IT_EXECD, &oit);
1780 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1786 rc = md_get_lustre_md(sbi->ll_md_exp, req,
1787 1, sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1791 llu_update_inode(ino, md.body, md.lsm);
1792 lli->lli_smd = lli2->lli_smd;
1793 lli2->lli_smd = NULL;
1795 llu_local_open(lli2, &oit);
1797 /* release intent */
1798 if (lustre_handle_is_used(&lockh))
1799 ldlm_lock_decref(&lockh, LCK_CR);
1801 ptlrpc_req_finished(req);
1804 rc = llu_file_release(ino);
1806 ino->i_private = lli;
1808 OBD_FREE(lli2, sizeof(struct llu_inode_info));
1810 ptlrpc_req_finished(req);
1814 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1816 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1818 int flags = FMODE_WRITE;
1821 LASSERT(sizeof(lum) == sizeof(*lump));
1822 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1823 rc = copy_from_user(&lum, lump, sizeof(lum));
1827 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1831 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1833 struct intnl_stat *st = llu_i2stat(ino);
1834 if (S_ISREG(st->st_mode))
1835 return llu_lov_file_setstripe(ino, arg);
1836 if (S_ISDIR(st->st_mode))
1837 return llu_lov_dir_setstripe(ino, arg);
1842 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1844 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1849 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1853 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1859 liblustre_wait_event(0);
1862 case LL_IOC_GROUP_LOCK:
1863 arg = va_arg(ap, unsigned long);
1864 rc = llu_get_grouplock(ino, arg);
1866 case LL_IOC_GROUP_UNLOCK:
1867 arg = va_arg(ap, unsigned long);
1868 rc = llu_put_grouplock(ino, arg);
1870 case LL_IOC_LOV_SETSTRIPE:
1871 arg = va_arg(ap, unsigned long);
1872 rc = llu_lov_setstripe(ino, arg);
1874 case LL_IOC_LOV_GETSTRIPE:
1875 arg = va_arg(ap, unsigned long);
1876 rc = llu_lov_getstripe(ino, arg);
1879 CERROR("did not support ioctl cmd %lx\n", request);
1884 liblustre_wait_event(0);
1889 * we already do syncronous read/write
1891 static int llu_iop_sync(struct inode *inode)
1893 liblustre_wait_event(0);
1897 static int llu_iop_datasync(struct inode *inode)
1899 liblustre_wait_event(0);
1903 struct filesys_ops llu_filesys_ops =
1905 fsop_gone: llu_fsop_gone,
1908 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1910 struct inode *inode;
1912 struct file_identifier fileid = {&fid, sizeof(fid)};
1914 if ((md->body->valid &
1915 (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1916 (OBD_MD_FLGENER | OBD_MD_FLID | OBD_MD_FLTYPE)) {
1917 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1919 return ERR_PTR(-EPERM);
1922 /* try to find existing inode */
1923 fid = md->body->fid1;
1925 inode = _sysio_i_find(fs, &fileid);
1927 if (inode->i_zombie/* ||
1928 lli->lli_st_generation != md->body->generation*/) {
1932 llu_update_inode(inode, md->body, md->lsm);
1937 inode = llu_new_inode(fs, &fid);
1939 llu_update_inode(inode, md->body, md->lsm);
1945 llu_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
1947 struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC };
1948 __u32 valsize = sizeof(struct lov_desc);
1949 int rc, easize, def_easize, cookiesize;
1950 struct lov_desc desc;
1954 rc = obd_get_info(dt_exp, strlen(KEY_LOVDESC) + 1, KEY_LOVDESC,
1959 stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
1960 lsm.lsm_stripe_count = stripes;
1961 easize = obd_size_diskmd(dt_exp, &lsm);
1963 lsm.lsm_stripe_count = desc.ld_default_stripe_count;
1964 def_easize = obd_size_diskmd(dt_exp, &lsm);
1966 cookiesize = stripes * sizeof(struct llog_cookie);
1968 CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
1969 easize, cookiesize);
1971 rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize);
1976 llu_fsswop_mount(const char *source,
1978 const void *data __IS_UNUSED,
1979 struct pnode *tocover,
1980 struct mount **mntp)
1984 struct pnode_base *rootpb;
1985 struct obd_device *obd;
1986 struct lu_fid rootfid;
1987 struct llu_sb_info *sbi;
1988 struct obd_statfs osfs;
1989 static struct qstr noname = { NULL, 0, 0 };
1990 struct ptlrpc_request *request = NULL;
1991 struct lustre_handle md_conn = {0, };
1992 struct lustre_handle dt_conn = {0, };
1993 struct lustre_md md;
1995 struct config_llog_instance cfg;
1996 char ll_instance[sizeof(sbi) * 2 + 1];
1997 struct lustre_profile *lprof;
1998 char *zconf_mgsnid, *zconf_profile;
1999 char *osc = NULL, *mdc = NULL;
2000 int async = 1, err = -EINVAL;
2001 struct obd_connect_data ocd = {0,};
2005 if (ll_parse_mount_target(source,
2008 CERROR("mal-formed target %s\n", source);
2011 if (!zconf_mgsnid || !zconf_profile) {
2012 printf("Liblustre: invalid target %s\n", source);
2015 /* allocate & initialize sbi */
2016 OBD_ALLOC(sbi, sizeof(*sbi));
2020 INIT_LIST_HEAD(&sbi->ll_conn_chain);
2021 generate_random_uuid(uuid);
2022 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
2024 /* generate a string unique to this super, let's try
2025 the address of the super itself.*/
2026 sprintf(ll_instance, "%p", sbi);
2028 /* retrive & parse config log */
2029 cfg.cfg_instance = ll_instance;
2030 cfg.cfg_uuid = sbi->ll_sb_uuid;
2031 cfg.cfg_last_idx = 0;
2032 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
2034 CERROR("Unable to process log: %s\n", zconf_profile);
2035 GOTO(out_free, err);
2038 lprof = class_get_profile(zconf_profile);
2039 if (lprof == NULL) {
2040 CERROR("No profile found: %s\n", zconf_profile);
2041 GOTO(out_free, err = -EINVAL);
2043 OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
2044 sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
2046 OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
2047 sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
2051 GOTO(out_free, err = -EINVAL);
2055 GOTO(out_free, err = -EINVAL);
2058 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
2064 obd = class_name2obd(mdc);
2066 CERROR("MDC %s: not setup or attached\n", mdc);
2067 GOTO(out_free, err = -EINVAL);
2069 obd_set_info_async(obd->obd_self_export, strlen("async"), "async",
2070 sizeof(async), &async, NULL);
2072 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION;
2073 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2074 ocd.ocd_version = LUSTRE_VERSION_CODE;
2077 err = obd_connect(NULL, &md_conn, obd, &sbi->ll_sb_uuid, &ocd);
2079 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2080 GOTO(out_free, err);
2082 sbi->ll_md_exp = class_conn2export(&md_conn);
2084 err = obd_statfs(obd, &osfs, 100000000);
2089 * FIXME fill fs stat data into sbi here!!! FIXME
2093 obd = class_name2obd(osc);
2095 CERROR("OSC %s: not setup or attached\n", osc);
2096 GOTO(out_md, err = -EINVAL);
2098 obd_set_info_async(obd->obd_self_export, strlen("async"), "async",
2099 sizeof(async), &async, NULL);
2101 obd->obd_upcall.onu_owner = &sbi->ll_lco;
2102 obd->obd_upcall.onu_upcall = ll_ocd_update;
2104 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2105 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK;
2106 ocd.ocd_version = LUSTRE_VERSION_CODE;
2107 err = obd_connect(NULL, &dt_conn, obd, &sbi->ll_sb_uuid, &ocd);
2109 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2112 sbi->ll_dt_exp = class_conn2export(&dt_conn);
2113 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2115 llu_init_ea_size(sbi->ll_md_exp, sbi->ll_dt_exp);
2117 err = md_getstatus(sbi->ll_md_exp, &rootfid, NULL);
2119 CERROR("cannot mds_connect: rc = %d\n", err);
2122 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&rootfid));
2123 sbi->ll_root_fid = rootfid;
2125 /* fetch attr of root inode */
2126 err = md_getattr(sbi->ll_md_exp, &rootfid, NULL,
2127 OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
2129 CERROR("md_getattr failed for root: rc = %d\n", err);
2133 err = md_get_lustre_md(sbi->ll_md_exp, request, REPLY_REC_OFF,
2134 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2136 CERROR("failed to understand root inode md: rc = %d\n",err);
2137 GOTO(out_request, err);
2140 LASSERT(fid_is_sane(&sbi->ll_root_fid));
2142 root = llu_iget(fs, &md);
2143 if (!root || IS_ERR(root)) {
2144 CERROR("fail to generate root inode\n");
2145 GOTO(out_request, err = -EBADF);
2149 * Generate base path-node for root.
2151 rootpb = _sysio_pb_new(&noname, NULL, root);
2157 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2159 _sysio_pb_gone(rootpb);
2163 ptlrpc_req_finished(request);
2165 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2166 liblustre_wait_event(0);
2171 _sysio_i_gone(root);
2173 ptlrpc_req_finished(request);
2175 obd_disconnect(sbi->ll_dt_exp);
2177 obd_disconnect(sbi->ll_md_exp);
2180 OBD_FREE(osc, strlen(osc) + 1);
2182 OBD_FREE(mdc, strlen(mdc) + 1);
2183 OBD_FREE(sbi, sizeof(*sbi));
2187 struct fssw_ops llu_fssw_ops = {
2191 static struct inode_ops llu_inode_ops = {
2192 inop_lookup: llu_iop_lookup,
2193 inop_getattr: llu_iop_getattr,
2194 inop_setattr: llu_iop_setattr,
2195 inop_filldirentries: llu_iop_filldirentries,
2196 inop_mkdir: llu_iop_mkdir_raw,
2197 inop_rmdir: llu_iop_rmdir_raw,
2198 inop_symlink: llu_iop_symlink_raw,
2199 inop_readlink: llu_iop_readlink,
2200 inop_open: llu_iop_open,
2201 inop_close: llu_iop_close,
2202 inop_link: llu_iop_link_raw,
2203 inop_unlink: llu_iop_unlink_raw,
2204 inop_rename: llu_iop_rename_raw,
2205 inop_pos: llu_iop_pos,
2206 inop_read: llu_iop_read,
2207 inop_write: llu_iop_write,
2208 inop_iodone: llu_iop_iodone,
2209 inop_fcntl: llu_iop_fcntl,
2210 inop_sync: llu_iop_sync,
2211 inop_datasync: llu_iop_datasync,
2212 inop_ioctl: llu_iop_ioctl,
2213 inop_mknod: llu_iop_mknod_raw,
2214 #ifdef _HAVE_STATVFS
2215 inop_statvfs: llu_iop_statvfs,
2217 inop_gone: llu_iop_gone,