1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/liblustre/super.c
38 * Lustre Light Super operations
41 #define DEBUG_SUBSYSTEM S_LLITE
47 #include <sys/types.h>
50 #include <sys/queue.h>
52 # include <sys/statvfs.h>
54 # include <sys/statfs.h>
70 #include "llite_lib.h"
78 #define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
80 static int ll_permission(struct inode *inode, int mask)
82 struct intnl_stat *st = llu_i2stat(inode);
83 mode_t mode = st->st_mode;
85 if (current->fsuid == st->st_uid)
87 else if (in_group_p(st->st_gid))
90 if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
93 if ((mask & (MAY_READ|MAY_WRITE)) ||
94 (st->st_mode & S_IXUGO))
95 if (cfs_capable(CFS_CAP_DAC_OVERRIDE))
98 if (mask == MAY_READ ||
99 (S_ISDIR(st->st_mode) && !(mask & MAY_WRITE))) {
100 if (cfs_capable(CFS_CAP_DAC_READ_SEARCH))
107 static void llu_fsop_gone(struct filesys *fs)
109 struct llu_sb_info *sbi = (struct llu_sb_info *) fs->fs_private;
110 struct obd_device *obd = class_exp2obd(sbi->ll_md_exp);
114 list_del(&sbi->ll_conn_chain);
116 obd_disconnect(sbi->ll_dt_exp);
117 obd_disconnect(sbi->ll_md_exp);
119 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
120 class_manual_cleanup(obd);
122 OBD_FREE(sbi, sizeof(*sbi));
124 liblustre_wait_idle();
128 static struct inode_ops llu_inode_ops;
130 static ldlm_mode_t llu_take_md_lock(struct inode *inode, __u64 bits,
131 struct lustre_handle *lockh)
133 ldlm_policy_data_t policy = { .l_inodebits = {bits}};
139 fid = &llu_i2info(inode)->lli_fid;
140 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
142 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
143 rc = md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
144 LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
148 void llu_update_inode(struct inode *inode, struct lustre_md *md)
150 struct llu_inode_info *lli = llu_i2info(inode);
151 struct mdt_body *body = md->body;
152 struct lov_stripe_md *lsm = md->lsm;
153 struct intnl_stat *st = llu_i2stat(inode);
155 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
157 if (body->valid & OBD_MD_FLMODE)
158 st->st_mode = (st->st_mode & S_IFMT)|(body->mode & ~S_IFMT);
159 if (body->valid & OBD_MD_FLTYPE)
160 st->st_mode = (st->st_mode & ~S_IFMT)|(body->mode & S_IFMT);
163 if (lli->lli_smd == NULL) {
164 cl_inode_init(inode, md);
166 lli->lli_maxbytes = lsm->lsm_maxbytes;
167 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
168 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
170 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
171 CERROR("lsm mismatch for inode %lld\n",
172 (long long)st->st_ino);
178 if (body->valid & OBD_MD_FLMTIME &&
179 body->mtime > LTIME_S(st->st_mtime))
180 LTIME_S(st->st_mtime) = body->mtime;
181 if (body->valid & OBD_MD_FLATIME &&
182 body->atime > LTIME_S(st->st_atime))
183 LTIME_S(st->st_atime) = body->atime;
185 /* mtime is always updated with ctime, but can be set in past.
186 As write and utime(2) may happen within 1 second, and utime's
187 mtime has a priority over write's one, so take mtime from mds
188 for the same ctimes. */
189 if (body->valid & OBD_MD_FLCTIME &&
190 body->ctime >= LTIME_S(st->st_ctime)) {
191 LTIME_S(st->st_ctime) = body->ctime;
192 if (body->valid & OBD_MD_FLMTIME)
193 LTIME_S(st->st_mtime) = body->mtime;
195 if (S_ISREG(st->st_mode))
196 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
198 st->st_blksize = 4096;
199 if (body->valid & OBD_MD_FLUID)
200 st->st_uid = body->uid;
201 if (body->valid & OBD_MD_FLGID)
202 st->st_gid = body->gid;
203 if (body->valid & OBD_MD_FLNLINK)
204 st->st_nlink = body->nlink;
205 if (body->valid & OBD_MD_FLRDEV)
206 st->st_rdev = body->rdev;
207 if (body->valid & OBD_MD_FLFLAGS)
208 lli->lli_st_flags = body->flags;
209 if (body->valid & OBD_MD_FLSIZE) {
210 if ((llu_i2sbi(inode)->ll_lco.lco_flags & OBD_CONNECT_SOM) &&
211 S_ISREG(st->st_mode) && lli->lli_smd) {
212 struct lustre_handle lockh;
215 /* As it is possible a blocking ast has been processed
216 * by this time, we need to check there is an UPDATE
217 * lock on the client and set LLIF_MDS_SIZE_LOCK holding
219 mode = llu_take_md_lock(inode, MDS_INODELOCK_UPDATE,
222 st->st_size = body->size;
223 lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
224 ldlm_lock_decref(&lockh, mode);
227 st->st_size = body->size;
230 if (body->valid & OBD_MD_FLBLOCKS)
231 st->st_blocks = body->blocks;
235 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
237 struct llu_inode_info *lli = llu_i2info(dst);
238 struct intnl_stat *st = llu_i2stat(dst);
240 valid &= src->o_valid;
242 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
243 CDEBUG(D_INODE,"valid "LPX64", cur time "CFS_TIME_T"/"CFS_TIME_T
246 LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
247 (long)src->o_mtime, (long)src->o_ctime);
249 if (valid & OBD_MD_FLATIME)
250 LTIME_S(st->st_atime) = src->o_atime;
251 if (valid & OBD_MD_FLMTIME)
252 LTIME_S(st->st_mtime) = src->o_mtime;
253 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
254 LTIME_S(st->st_ctime) = src->o_ctime;
255 if (valid & OBD_MD_FLSIZE)
256 st->st_size = src->o_size;
257 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
258 st->st_blocks = src->o_blocks;
259 if (valid & OBD_MD_FLBLKSZ)
260 st->st_blksize = src->o_blksize;
261 if (valid & OBD_MD_FLTYPE)
262 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
263 if (valid & OBD_MD_FLMODE)
264 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
265 if (valid & OBD_MD_FLUID)
266 st->st_uid = src->o_uid;
267 if (valid & OBD_MD_FLGID)
268 st->st_gid = src->o_gid;
269 if (valid & OBD_MD_FLFLAGS)
270 lli->lli_st_flags = src->o_flags;
273 #define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
274 #define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
276 void obdo_from_inode(struct obdo *dst, struct inode *src, obd_flag valid)
278 struct llu_inode_info *lli = llu_i2info(src);
279 struct intnl_stat *st = llu_i2stat(src);
280 obd_flag newvalid = 0;
282 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
283 CDEBUG(D_INODE, "valid %x, new time "CFS_TIME_T"/"CFS_TIME_T"\n",
284 valid, LTIME_S(st->st_mtime),
285 LTIME_S(st->st_ctime));
287 if (valid & OBD_MD_FLATIME) {
288 dst->o_atime = LTIME_S(st->st_atime);
289 newvalid |= OBD_MD_FLATIME;
291 if (valid & OBD_MD_FLMTIME) {
292 dst->o_mtime = LTIME_S(st->st_mtime);
293 newvalid |= OBD_MD_FLMTIME;
295 if (valid & OBD_MD_FLCTIME) {
296 dst->o_ctime = LTIME_S(st->st_ctime);
297 newvalid |= OBD_MD_FLCTIME;
299 if (valid & OBD_MD_FLSIZE) {
300 dst->o_size = st->st_size;
301 newvalid |= OBD_MD_FLSIZE;
303 if (valid & OBD_MD_FLBLOCKS) { /* allocation of space (x512 bytes) */
304 dst->o_blocks = st->st_blocks;
305 newvalid |= OBD_MD_FLBLOCKS;
307 if (valid & OBD_MD_FLBLKSZ) { /* optimal block size */
308 dst->o_blksize = st->st_blksize;
309 newvalid |= OBD_MD_FLBLKSZ;
311 if (valid & OBD_MD_FLTYPE) {
312 dst->o_mode = (dst->o_mode & S_IALLUGO)|(st->st_mode & S_IFMT);
313 newvalid |= OBD_MD_FLTYPE;
315 if (valid & OBD_MD_FLMODE) {
316 dst->o_mode = (dst->o_mode & S_IFMT)|(st->st_mode & S_IALLUGO);
317 newvalid |= OBD_MD_FLMODE;
319 if (valid & OBD_MD_FLUID) {
320 dst->o_uid = st->st_uid;
321 newvalid |= OBD_MD_FLUID;
323 if (valid & OBD_MD_FLGID) {
324 dst->o_gid = st->st_gid;
325 newvalid |= OBD_MD_FLGID;
327 if (valid & OBD_MD_FLFLAGS) {
328 dst->o_flags = lli->lli_st_flags;
329 newvalid |= OBD_MD_FLFLAGS;
331 if (valid & OBD_MD_FLGENER) {
332 dst->o_generation = lli->lli_st_generation;
333 newvalid |= OBD_MD_FLGENER;
335 if (valid & OBD_MD_FLFID) {
336 dst->o_fid = st->st_ino;
337 newvalid |= OBD_MD_FLFID;
340 dst->o_valid |= newvalid;
344 * Performs the getattr on the inode and updates its fields.
345 * If @sync != 0, perform the getattr under the server-side lock.
347 int llu_inode_getattr(struct inode *inode, struct obdo *obdo,
348 __u64 ioepoch, int sync)
350 struct llu_inode_info *lli = llu_i2info(inode);
351 struct ptlrpc_request_set *set;
352 struct lov_stripe_md *lsm = lli->lli_smd;
353 struct obd_info oinfo = { { { 0 } } };
361 oinfo.oi_oa->o_id = lsm->lsm_object_id;
362 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
363 oinfo.oi_oa->o_mode = S_IFREG;
364 oinfo.oi_oa->o_ioepoch = ioepoch;
365 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
366 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
367 OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
368 OBD_MD_FLCTIME | OBD_MD_FLGROUP |
369 OBD_MD_FLATIME | OBD_MD_FLEPOCH;
371 oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS;
372 oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK;
375 set = ptlrpc_prep_set();
377 CERROR ("ENOMEM allocing request set\n");
380 rc = obd_getattr_async(llu_i2obdexp(inode), &oinfo, set);
382 rc = ptlrpc_set_wait(set);
383 ptlrpc_set_destroy(set);
388 oinfo.oi_oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
389 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
392 obdo_refresh_inode(inode, oinfo.oi_oa, oinfo.oi_oa->o_valid);
393 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %Lu, "
394 "blksize %Lu\n", lli->lli_smd->lsm_object_id,
395 (long long unsigned)llu_i2stat(inode)->st_size,
396 (long long unsigned)llu_i2stat(inode)->st_blocks,
397 (long long unsigned)llu_i2stat(inode)->st_blksize);
401 static struct inode* llu_new_inode(struct filesys *fs,
405 struct llu_inode_info *lli;
406 struct intnl_stat st = {
409 #ifndef AUTOMOUNT_FILE_NAME
410 .st_mode = fid->f_type & S_IFMT,
412 .st_mode = fid->f_type /* all of the bits! */
415 /* FIXME: fix this later */
422 OBD_ALLOC(lli, sizeof(*lli));
426 /* initialize lli here */
427 lli->lli_sbi = llu_fs2sbi(fs);
429 lli->lli_symlink_name = NULL;
431 lli->lli_maxbytes = (__u64)(~0UL);
432 lli->lli_file_data = NULL;
434 lli->lli_sysio_fid.fid_data = &lli->lli_fid;
435 lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
438 /* file identifier is needed by functions like _sysio_i_find() */
439 inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
440 &st, 0, &llu_inode_ops, lli);
443 OBD_FREE(lli, sizeof(*lli));
448 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
450 struct lustre_handle lockh;
451 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
458 fid = &llu_i2info(inode)->lli_fid;
459 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
461 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
462 if (md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
463 LCK_CR|LCK_CW|LCK_PR|LCK_PW, &lockh)) {
469 static int llu_inode_revalidate(struct inode *inode)
471 struct lov_stripe_md *lsm = NULL;
475 CERROR("REPORT THIS LINE TO PETER\n");
479 if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
481 struct ptlrpc_request *req = NULL;
482 struct llu_sb_info *sbi = llu_i2sbi(inode);
483 unsigned long valid = OBD_MD_FLGETATTR;
486 /* Why don't we update all valid MDS fields here, if we're
487 * doing an RPC anyways? -phil */
488 if (S_ISREG(llu_i2stat(inode)->st_mode)) {
489 ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
490 valid |= OBD_MD_FLEASIZE;
492 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
493 NULL, valid, ealen, &req);
495 CERROR("failure %d inode %llu\n", rc,
496 (long long)llu_i2stat(inode)->st_ino);
499 rc = md_get_lustre_md(sbi->ll_md_exp, req,
500 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
502 /* XXX Too paranoid? */
503 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
504 !((md.body->valid & OBD_MD_FLNLINK) &&
505 (md.body->nlink == 0))) {
506 CERROR("Asked for %s eadata but got %s (%d)\n",
507 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
508 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
509 md.body->eadatasize);
512 ptlrpc_req_finished(req);
517 llu_update_inode(inode, &md);
518 if (md.lsm != NULL && llu_i2info(inode)->lli_smd != md.lsm)
519 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
520 ptlrpc_req_finished(req);
523 lsm = llu_i2info(inode)->lli_smd;
524 if (!lsm) /* object not yet allocated, don't validate size */
527 /* ll_glimpse_size will prefer locally cached writes if they extend
529 RETURN(cl_glimpse_size(inode));
532 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
534 *b = *llu_i2stat(ino);
537 static int llu_iop_getattr(struct pnode *pno,
539 struct intnl_stat *b)
544 liblustre_wait_event(0);
548 LASSERT(pno->p_base->pb_ino);
549 ino = pno->p_base->pb_ino;
551 LASSERT(!pno || pno->p_base->pb_ino == ino);
554 /* libsysio might call us directly without intent lock,
555 * we must re-fetch the attrs here
557 rc = llu_inode_revalidate(ino);
559 copy_stat_buf(ino, b);
560 LASSERT(!llu_i2info(ino)->lli_it);
563 liblustre_wait_event(0);
567 static int null_if_equal(struct ldlm_lock *lock, void *data)
569 if (data == lock->l_ast_data) {
570 lock->l_ast_data = NULL;
572 if (lock->l_req_mode != lock->l_granted_mode)
573 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
576 return LDLM_ITER_CONTINUE;
579 void llu_clear_inode(struct inode *inode)
581 struct llu_inode_info *lli = llu_i2info(inode);
582 struct llu_sb_info *sbi = llu_i2sbi(inode);
585 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
586 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
589 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
590 md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
591 null_if_equal, inode);
594 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
595 null_if_equal, inode);
597 cl_inode_fini(inode);
600 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
604 if (lli->lli_symlink_name) {
605 OBD_FREE(lli->lli_symlink_name,
606 strlen(lli->lli_symlink_name) + 1);
607 lli->lli_symlink_name = NULL;
613 void llu_iop_gone(struct inode *inode)
615 struct llu_inode_info *lli = llu_i2info(inode);
618 liblustre_wait_event(0);
619 llu_clear_inode(inode);
621 OBD_FREE(lli, sizeof(*lli));
625 static int inode_setattr(struct inode * inode, struct iattr * attr)
627 unsigned int ia_valid = attr->ia_valid;
628 struct intnl_stat *st = llu_i2stat(inode);
632 * inode_setattr() is only ever invoked with ATTR_SIZE (by
633 * llu_setattr_raw()) when file has no bodies. Check this.
635 LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
637 if (ia_valid & ATTR_SIZE)
638 st->st_size = attr->ia_size;
639 if (ia_valid & ATTR_UID)
640 st->st_uid = attr->ia_uid;
641 if (ia_valid & ATTR_GID)
642 st->st_gid = attr->ia_gid;
643 if (ia_valid & ATTR_ATIME)
644 st->st_atime = attr->ia_atime;
645 if (ia_valid & ATTR_MTIME)
646 st->st_mtime = attr->ia_mtime;
647 if (ia_valid & ATTR_CTIME)
648 st->st_ctime = attr->ia_ctime;
649 if (ia_valid & ATTR_MODE) {
650 st->st_mode = attr->ia_mode;
651 if (!in_group_p(st->st_gid) && !cfs_capable(CFS_CAP_FSETID))
652 st->st_mode &= ~S_ISGID;
654 /* mark_inode_dirty(inode); */
658 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
659 struct md_open_data **mod)
662 struct llu_sb_info *sbi = llu_i2sbi(inode);
663 struct ptlrpc_request *request = NULL;
667 llu_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0, LUSTRE_OPC_ANY);
668 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL,
672 ptlrpc_req_finished(request);
673 if (rc != -EPERM && rc != -EACCES)
674 CERROR("md_setattr fails: rc = %d\n", rc);
678 rc = md_get_lustre_md(sbi->ll_md_exp, request,
679 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
681 ptlrpc_req_finished(request);
685 /* We call inode_setattr to adjust timestamps.
686 * If there is at least some data in file, we cleared ATTR_SIZE
687 * above to avoid invoking vmtruncate, otherwise it is important
688 * to call vmtruncate in inode_setattr to update inode->i_size
690 inode_setattr(inode, &op_data->op_attr);
691 llu_update_inode(inode, &md);
692 ptlrpc_req_finished(request);
697 /* Close IO epoch and send Size-on-MDS attribute update. */
698 static int llu_setattr_done_writing(struct inode *inode,
699 struct md_op_data *op_data,
700 struct md_open_data *mod)
702 struct llu_inode_info *lli = llu_i2info(inode);
703 struct intnl_stat *st = llu_i2stat(inode);
707 LASSERT(op_data != NULL);
708 if (!S_ISREG(st->st_mode))
711 /* XXX: pass och here for the recovery purpose. */
712 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
713 op_data->op_ioepoch, PFID(&lli->lli_fid));
715 op_data->op_flags = MF_EPOCH_CLOSE;
716 llu_done_writing_attr(inode, op_data);
717 llu_pack_inode2opdata(inode, op_data, NULL);
719 rc = md_done_writing(llu_i2sbi(inode)->ll_md_exp, op_data, mod);
721 /* MDS has instructed us to obtain Size-on-MDS attribute
722 * from OSTs and send setattr to back to MDS. */
723 rc = llu_som_update(inode, op_data);
725 CERROR("inode %llu mdc truncate failed: rc = %d\n",
726 (unsigned long long)st->st_ino, rc);
731 /* If this inode has objects allocated to it (lsm != NULL), then the OST
732 * object(s) determine the file size and mtime. Otherwise, the MDS will
733 * keep these values until such a time that objects are allocated for it.
734 * We do the MDS operations first, as it is checking permissions for us.
735 * We don't to the MDS RPC if there is nothing that we want to store there,
736 * otherwise there is no harm in updating mtime/atime on the MDS if we are
737 * going to do an RPC anyways.
739 * If we are doing a truncate, we will send the mtime and ctime updates
740 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
741 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
744 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
746 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
747 struct intnl_stat *st = llu_i2stat(inode);
748 int ia_valid = attr->ia_valid;
749 struct md_op_data op_data = { { 0 } };
750 struct md_open_data *mod = NULL;
754 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
756 if (ia_valid & ATTR_SIZE) {
757 if (attr->ia_size > ll_file_maxbytes(inode)) {
758 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
759 (long long)attr->ia_size,
760 ll_file_maxbytes(inode));
764 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
767 /* We mark all of the fields "set" so MDS/OST does not re-set them */
768 if (attr->ia_valid & ATTR_CTIME) {
769 attr->ia_ctime = CURRENT_TIME;
770 attr->ia_valid |= ATTR_CTIME_SET;
772 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
773 attr->ia_atime = CURRENT_TIME;
774 attr->ia_valid |= ATTR_ATIME_SET;
776 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
777 attr->ia_mtime = CURRENT_TIME;
778 attr->ia_valid |= ATTR_MTIME_SET;
780 if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
781 /* To avoid stale mtime on mds, obtain it from ost and send
783 rc = cl_glimpse_size(inode);
787 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
788 attr->ia_mtime = inode->i_stbuf.st_mtime;
791 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
792 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T", ctime "CFS_TIME_T
793 ", now = "CFS_TIME_T"\n",
794 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
795 LTIME_S(CURRENT_TIME));
797 /* NB: ATTR_SIZE will only be set after this point if the size
798 * resides on the MDS, ie, this file has no objects. */
800 attr->ia_valid &= ~ATTR_SIZE;
802 /* If only OST attributes being set on objects, don't do MDS RPC.
803 * In that case, we need to check permissions and update the local
804 * inode ourselves so we can call obdo_from_inode() always. */
805 if (ia_valid & (lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
806 memcpy(&op_data.op_attr, attr, sizeof(*attr));
808 /* Open epoch for truncate. */
809 if (exp_connect_som(llu_i2mdexp(inode)) &&
810 (ia_valid & ATTR_SIZE))
811 op_data.op_flags = MF_EPOCH_OPEN;
812 rc = llu_md_setattr(inode, &op_data, &mod);
816 llu_ioepoch_open(llu_i2info(inode), op_data.op_ioepoch);
817 if (!lsm || !S_ISREG(st->st_mode)) {
818 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
822 /* The OST doesn't check permissions, but the alternative is
823 * a gratuitous RPC to the MDS. We already rely on the client
824 * to do read/write/truncate permission checks, so is mtime OK?
826 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
827 /* from sys_utime() */
828 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
829 if (current->fsuid != st->st_uid &&
830 (rc = ll_permission(inode, MAY_WRITE)) != 0)
833 /* from inode_change_ok() */
834 if (current->fsuid != st->st_uid &&
835 !cfs_capable(CFS_CAP_FOWNER))
841 /* Won't invoke llu_vmtruncate(), as we already cleared
843 inode_setattr(inode, attr);
846 if (ia_valid & ATTR_SIZE) {
847 rc = cl_setattr_do_truncate(inode, attr->ia_size, NULL);
848 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
849 CDEBUG(D_INODE, "set mtime on OST inode %llu to %lu\n",
850 (long long unsigned)st->st_ino, LTIME_S(attr->ia_mtime));
851 rc = cl_setattr_ost(inode, NULL);
855 if (op_data.op_ioepoch)
856 rc1 = llu_setattr_done_writing(inode, &op_data, mod);
857 return rc ? rc : rc1;
860 /* here we simply act as a thin layer to glue it with
861 * llu_setattr_raw(), which is copy from kernel
863 static int llu_iop_setattr(struct pnode *pno,
866 struct intnl_stat *stbuf)
872 liblustre_wait_event(0);
874 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
875 SETATTR_UID | SETATTR_GID |
876 SETATTR_LEN | SETATTR_MODE)));
877 memset(&iattr, 0, sizeof(iattr));
879 if (mask & SETATTR_MODE) {
880 iattr.ia_mode = stbuf->st_mode;
881 iattr.ia_valid |= ATTR_MODE;
883 if (mask & SETATTR_MTIME) {
884 iattr.ia_mtime = stbuf->st_mtime;
885 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
887 if (mask & SETATTR_ATIME) {
888 iattr.ia_atime = stbuf->st_atime;
889 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
891 if (mask & SETATTR_UID) {
892 iattr.ia_uid = stbuf->st_uid;
893 iattr.ia_valid |= ATTR_UID;
895 if (mask & SETATTR_GID) {
896 iattr.ia_gid = stbuf->st_gid;
897 iattr.ia_valid |= ATTR_GID;
899 if (mask & SETATTR_LEN) {
900 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
901 iattr.ia_valid |= ATTR_SIZE;
904 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
905 iattr.ia_ctime = CURRENT_TIME;
907 rc = llu_setattr_raw(ino, &iattr);
908 liblustre_wait_idle();
912 #define EXT2_LINK_MAX 32000
914 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
916 struct inode *dir = pno->p_base->pb_parent->pb_ino;
917 struct qstr *qstr = &pno->p_base->pb_name;
918 const char *name = qstr->name;
920 struct ptlrpc_request *request = NULL;
921 struct llu_sb_info *sbi = llu_i2sbi(dir);
922 struct md_op_data op_data = {{ 0 }};
926 liblustre_wait_event(0);
927 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
930 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
933 err = md_create(sbi->ll_md_exp, &op_data, tgt, strlen(tgt) + 1,
934 S_IFLNK | S_IRWXUGO, current->fsuid, current->fsgid,
935 cfs_curproc_cap_pack(), 0, &request);
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;
955 if (lli->lli_symlink_name) {
956 *symname = lli->lli_symlink_name;
957 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
961 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), NULL,
962 OBD_MD_LINKNAME, symlen, request);
964 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
968 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
969 LASSERT(body != NULL);
971 if ((body->valid & OBD_MD_LINKNAME) == 0) {
972 CERROR ("OBD_MD_LINKNAME not set on reply\n");
973 GOTO (failed, rc = -EPROTO);
976 LASSERT(symlen != 0);
977 if (body->eadatasize != symlen) {
978 CERROR("inode %llu: symlink length %d not expected %d\n",
979 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
980 GOTO(failed, rc = -EPROTO);
983 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
984 if (*symname == NULL ||
985 strnlen(*symname, symlen) != symlen - 1) {
986 /* not full/NULL terminated */
987 CERROR("inode %llu: symlink not NULL terminated string"
988 "of length %d\n", (long long)st->st_ino, symlen - 1);
989 GOTO(failed, rc = -EPROTO);
992 OBD_ALLOC(lli->lli_symlink_name, symlen);
993 /* do not return an error if we cannot cache the symlink locally */
994 if (lli->lli_symlink_name)
995 memcpy(lli->lli_symlink_name, *symname, symlen);
1000 ptlrpc_req_finished (*request);
1004 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
1006 struct inode *inode = pno->p_base->pb_ino;
1007 struct ptlrpc_request *request;
1012 liblustre_wait_event(0);
1013 rc = llu_readlink_internal(inode, &request, &symname);
1018 strncpy(data, symname, bufsize);
1019 rc = strlen(symname);
1021 ptlrpc_req_finished(request);
1023 liblustre_wait_event(0);
1027 static int llu_iop_mknod_raw(struct pnode *pno,
1031 struct ptlrpc_request *request = NULL;
1032 struct inode *dir = pno->p_parent->p_base->pb_ino;
1033 struct llu_sb_info *sbi = llu_i2sbi(dir);
1034 struct md_op_data op_data = {{ 0 }};
1038 liblustre_wait_event(0);
1039 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
1040 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
1041 (long long)llu_i2stat(dir)->st_ino);
1043 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
1046 switch (mode & S_IFMT) {
1049 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1054 llu_prep_md_op_data(&op_data, dir, NULL,
1055 pno->p_base->pb_name.name,
1056 pno->p_base->pb_name.len, 0,
1059 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
1060 current->fsuid, current->fsgid,
1061 cfs_curproc_cap_pack(), dev, &request);
1062 ptlrpc_req_finished(request);
1070 liblustre_wait_event(0);
1074 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1076 struct inode *src = old->p_base->pb_ino;
1077 struct inode *dir = new->p_parent->p_base->pb_ino;
1078 const char *name = new->p_base->pb_name.name;
1079 int namelen = new->p_base->pb_name.len;
1080 struct ptlrpc_request *request = NULL;
1081 struct md_op_data op_data = {{ 0 }};
1088 liblustre_wait_event(0);
1089 llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0,
1091 rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1092 ptlrpc_req_finished(request);
1093 liblustre_wait_event(0);
1099 * libsysio will clear the inode immediately after return
1101 static int llu_iop_unlink_raw(struct pnode *pno)
1103 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1104 struct qstr *qstr = &pno->p_base->pb_name;
1105 const char *name = qstr->name;
1106 int len = qstr->len;
1107 struct inode *target = pno->p_base->pb_ino;
1108 struct ptlrpc_request *request = NULL;
1109 struct md_op_data op_data = { { 0 } };
1115 liblustre_wait_event(0);
1116 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1118 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1120 rc = llu_objects_destroy(request, dir);
1121 ptlrpc_req_finished(request);
1122 liblustre_wait_idle();
1127 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1129 struct inode *src = old->p_parent->p_base->pb_ino;
1130 struct inode *tgt = new->p_parent->p_base->pb_ino;
1131 const char *oldname = old->p_base->pb_name.name;
1132 int oldnamelen = old->p_base->pb_name.len;
1133 const char *newname = new->p_base->pb_name.name;
1134 int newnamelen = new->p_base->pb_name.len;
1135 struct ptlrpc_request *request = NULL;
1136 struct md_op_data op_data = { { 0 } };
1143 liblustre_wait_event(0);
1144 llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0,
1146 rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1147 oldname, oldnamelen, newname, newnamelen,
1150 rc = llu_objects_destroy(request, src);
1153 ptlrpc_req_finished(request);
1154 liblustre_wait_idle();
1159 #ifdef _HAVE_STATVFS
1160 static int llu_statfs_internal(struct llu_sb_info *sbi,
1161 struct obd_statfs *osfs, __u64 max_age)
1163 struct obd_statfs obd_osfs;
1167 rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age, 0);
1169 CERROR("md_statfs fails: rc = %d\n", rc);
1173 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1174 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1176 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1177 &obd_statfs, max_age, 0);
1179 CERROR("obd_statfs fails: rc = %d\n", rc);
1183 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1184 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1187 osfs->os_blocks = obd_osfs.os_blocks;
1188 osfs->os_bfree = obd_osfs.os_bfree;
1189 osfs->os_bavail = obd_osfs.os_bavail;
1191 /* If we don't have as many objects free on the OST as inodes
1192 * on the MDS, we reduce the total number of inodes to
1193 * compensate, so that the "inodes in use" number is correct.
1195 if (obd_osfs.os_ffree < osfs->os_ffree) {
1196 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1198 osfs->os_ffree = obd_osfs.os_ffree;
1204 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1206 struct obd_statfs osfs;
1209 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1211 /* For now we will always get up-to-date statfs values, but in the
1212 * future we may allow some amount of caching on the client (e.g.
1213 * from QOS or lprocfs updates). */
1214 rc = llu_statfs_internal(sbi, &osfs, cfs_time_current_64() - HZ);
1218 statfs_unpack(sfs, &osfs);
1220 if (sizeof(sfs->f_blocks) == 4) {
1221 while (osfs.os_blocks > ~0UL) {
1224 osfs.os_blocks >>= 1;
1225 osfs.os_bfree >>= 1;
1226 osfs.os_bavail >>= 1;
1230 sfs->f_blocks = osfs.os_blocks;
1231 sfs->f_bfree = osfs.os_bfree;
1232 sfs->f_bavail = osfs.os_bavail;
1237 static int llu_iop_statvfs(struct pnode *pno,
1239 struct intnl_statvfs *buf)
1245 liblustre_wait_event(0);
1248 LASSERT(pno->p_base->pb_ino);
1249 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1253 /* from native driver */
1254 buf->f_bsize = fs.f_bsize; /* file system block size */
1255 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1256 buf->f_blocks = fs.f_blocks;
1257 buf->f_bfree = fs.f_bfree;
1258 buf->f_bavail = fs.f_bavail;
1259 buf->f_files = fs.f_files; /* Total number serial numbers */
1260 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1261 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1262 buf->f_fsid = fs.f_fsid.__val[1];
1263 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1264 buf->f_namemax = fs.f_namelen;
1267 liblustre_wait_event(0);
1270 #endif /* _HAVE_STATVFS */
1272 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1274 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1275 struct qstr *qstr = &pno->p_base->pb_name;
1276 const char *name = qstr->name;
1277 int len = qstr->len;
1278 struct ptlrpc_request *request = NULL;
1279 struct intnl_stat *st = llu_i2stat(dir);
1280 struct md_op_data op_data = {{ 0 }};
1284 liblustre_wait_event(0);
1285 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1286 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1288 if (st->st_nlink >= EXT2_LINK_MAX)
1291 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1294 err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0,
1295 mode | S_IFDIR, current->fsuid, current->fsgid,
1296 cfs_curproc_cap_pack(), 0, &request);
1297 ptlrpc_req_finished(request);
1298 liblustre_wait_event(0);
1302 static int llu_iop_rmdir_raw(struct pnode *pno)
1304 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1305 struct qstr *qstr = &pno->p_base->pb_name;
1306 const char *name = qstr->name;
1307 int len = qstr->len;
1308 struct ptlrpc_request *request = NULL;
1309 struct md_op_data op_data = {{ 0 }};
1313 liblustre_wait_event(0);
1314 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1315 (long long)llu_i2stat(dir)->st_ino,
1316 llu_i2info(dir)->lli_st_generation, dir);
1318 llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR,
1320 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1321 ptlrpc_req_finished(request);
1323 liblustre_wait_event(0);
1328 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1330 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1332 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1334 /* refer to ll_file_flock() for details */
1335 static int llu_file_flock(struct inode *ino,
1337 struct file_lock *file_lock)
1339 struct llu_inode_info *lli = llu_i2info(ino);
1340 struct intnl_stat *st = llu_i2stat(ino);
1341 struct ldlm_res_id res_id =
1342 { .name = {fid_seq(&lli->lli_fid),
1343 fid_oid(&lli->lli_fid),
1344 fid_ver(&lli->lli_fid),
1346 struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1347 ldlm_flock_completion_ast, NULL, NULL, file_lock };
1349 struct lustre_handle lockh = {0};
1350 ldlm_policy_data_t flock;
1354 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1355 (unsigned long long)st->st_ino, file_lock);
1357 flock.l_flock.pid = file_lock->fl_pid;
1358 flock.l_flock.start = file_lock->fl_start;
1359 flock.l_flock.end = file_lock->fl_end;
1361 switch (file_lock->fl_type) {
1363 einfo.ei_mode = LCK_PR;
1366 einfo.ei_mode = LCK_NL;
1369 einfo.ei_mode = LCK_PW;
1372 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1379 #if F_SETLKW64 != F_SETLKW
1387 #if F_SETLK64 != F_SETLK
1391 flags = LDLM_FL_BLOCK_NOWAIT;
1395 #if F_GETLK64 != F_GETLK
1399 flags = LDLM_FL_TEST_LOCK;
1400 file_lock->fl_type = einfo.ei_mode;
1403 CERROR("unknown fcntl cmd: %d\n", cmd);
1407 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, cmd=%d, flags=%#x, mode=%u, "
1408 "start="LPX64", end="LPX64"\n", (unsigned long long)st->st_ino,
1409 flock.l_flock.pid, cmd, flags, einfo.ei_mode, flock.l_flock.start,
1413 struct lmv_obd *lmv;
1414 struct obd_device *lmv_obd;
1415 lmv_obd = class_exp2obd(llu_i2mdexp(ino));
1416 lmv = &lmv_obd->u.lmv;
1418 if (lmv->desc.ld_tgt_count < 1)
1419 RETURN(rc = -ENODEV);
1421 if (lmv->tgts[0].ltd_exp != NULL)
1422 rc = ldlm_cli_enqueue(lmv->tgts[0].ltd_exp, NULL, &einfo, &res_id,
1423 &flock, &flags, NULL, 0, &lockh, 0);
1430 static int assign_type(struct file_lock *fl, int type)
1443 static int flock_to_posix_lock(struct inode *ino,
1444 struct file_lock *fl,
1447 switch (l->l_whence) {
1448 /* XXX: only SEEK_SET is supported in lustre */
1456 fl->fl_end = l->l_len - 1;
1460 fl->fl_end = OFFSET_MAX;
1462 fl->fl_pid = getpid();
1463 fl->fl_flags = FL_POSIX;
1464 fl->fl_notify = NULL;
1465 fl->fl_insert = NULL;
1466 fl->fl_remove = NULL;
1467 /* XXX: these fields can't be filled with suitable values,
1468 but I think lustre doesn't use them.
1470 fl->fl_owner = NULL;
1473 return assign_type(fl, l->l_type);
1476 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1478 struct file_lock fl;
1482 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1485 error = flock_to_posix_lock(ino, &fl, flock);
1489 error = llu_file_flock(ino, F_GETLK, &fl);
1493 flock->l_type = F_UNLCK;
1494 if (fl.fl_type != F_UNLCK) {
1495 flock->l_pid = fl.fl_pid;
1496 flock->l_start = fl.fl_start;
1497 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1498 fl.fl_end - fl.fl_start + 1;
1499 flock->l_whence = SEEK_SET;
1500 flock->l_type = fl.fl_type;
1507 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1509 struct file_lock fl;
1510 int flags = llu_i2info(ino)->lli_open_flags + 1;
1513 error = flock_to_posix_lock(ino, &fl, flock);
1516 if (cmd == F_SETLKW)
1517 fl.fl_flags |= FL_SLEEP;
1520 switch (flock->l_type) {
1522 if (!(flags & FMODE_READ))
1526 if (!(flags & FMODE_WRITE))
1536 error = llu_file_flock(ino, cmd, &fl);
1544 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1546 struct llu_inode_info *lli = llu_i2info(ino);
1548 struct flock *flock;
1551 liblustre_wait_event(0);
1554 *rtn = lli->lli_open_flags;
1557 flags = va_arg(ap, long);
1558 flags &= FCNTL_FLMASK;
1559 if (flags & FCNTL_FLMASK_INVALID) {
1560 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1561 "the O_NONBLOCK or O_ASYNC flags. "
1562 "Please fix your application.\n");
1567 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1568 (lli->lli_open_flags & ~FCNTL_FLMASK);
1573 #if F_GETLK64 != F_GETLK
1577 flock = va_arg(ap, struct flock *);
1578 err = llu_fcntl_getlk(ino, flock);
1583 #if F_SETLKW64 != F_SETLKW
1589 #if F_SETLK64 != F_SETLK
1593 flock = va_arg(ap, struct flock *);
1594 err = llu_fcntl_setlk(ino, cmd, flock);
1598 CERROR("unsupported fcntl cmd %x\n", cmd);
1604 liblustre_wait_event(0);
1608 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1610 struct llu_inode_info *lli = llu_i2info(inode);
1611 struct ll_file_data *fd = lli->lli_file_data;
1613 struct ccc_grouplock grouplock;
1616 if (fd->fd_flags & LL_FILE_IGNORE_LOCK) {
1619 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1622 LASSERT(fd->fd_grouplock.cg_lock == NULL);
1624 rc = cl_get_grouplock(cl_i2info(inode)->lli_clob,
1625 arg, (lli->lli_open_flags & O_NONBLOCK),
1631 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1632 fd->fd_grouplock = grouplock;
1637 int llu_put_grouplock(struct inode *inode, unsigned long arg)
1639 struct llu_inode_info *lli = llu_i2info(inode);
1640 struct ll_file_data *fd = lli->lli_file_data;
1641 struct ccc_grouplock grouplock;
1644 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1647 LASSERT(fd->fd_grouplock.cg_lock != NULL);
1649 if (fd->fd_grouplock.cg_gid != arg)
1652 grouplock = fd->fd_grouplock;
1653 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1654 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1656 cl_put_grouplock(&grouplock);
1661 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1663 struct llu_sb_info *sbi = llu_i2sbi(ino);
1664 struct ptlrpc_request *request = NULL;
1665 struct md_op_data op_data = {{ 0 }};
1666 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1669 llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0,
1672 LASSERT(sizeof(lum) == sizeof(*lump));
1673 LASSERT(sizeof(lum.lmm_objects[0]) ==
1674 sizeof(lump->lmm_objects[0]));
1675 if (copy_from_user(&lum, lump, sizeof(lum)))
1678 switch (lum.lmm_magic) {
1679 case LOV_USER_MAGIC_V1: {
1680 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
1681 lustre_swab_lov_user_md_v1(&lum);
1684 case LOV_USER_MAGIC_V3: {
1685 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
1686 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)&lum);
1690 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
1691 " %#08x != %#08x nor %#08x\n",
1692 lum.lmm_magic, LOV_USER_MAGIC_V1,
1698 /* swabbing is done in lov_setstripe() on server side */
1699 rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1700 sizeof(lum), NULL, 0, &request, NULL);
1702 ptlrpc_req_finished(request);
1703 if (rc != -EPERM && rc != -EACCES)
1704 CERROR("md_setattr fails: rc = %d\n", rc);
1707 ptlrpc_req_finished(request);
1712 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1713 struct lov_user_md *lum, int lum_size)
1715 struct llu_sb_info *sbi = llu_i2sbi(ino);
1716 struct llu_inode_info *lli = llu_i2info(ino);
1717 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1718 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1719 llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
1720 struct ptlrpc_request *req = NULL;
1721 struct lustre_md md;
1722 struct md_op_data data = {{ 0 }};
1723 struct lustre_handle lockh;
1728 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1729 PFID(&lli->lli_fid));
1733 llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1735 rc = md_enqueue(sbi->ll_md_exp, &einfo, &oit, &data,
1736 &lockh, lum, lum_size, NULL, LDLM_FL_INTENT_ONLY);
1740 req = oit.d.lustre.it_data;
1741 rc = it_open_error(DISP_IT_EXECD, &oit);
1747 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1753 rc = md_get_lustre_md(sbi->ll_md_exp, req,
1754 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1758 llu_update_inode(ino, &md);
1759 llu_local_open(lli, &oit);
1760 /* release intent */
1761 if (lustre_handle_is_used(&lockh))
1762 ldlm_lock_decref(&lockh, LCK_CR);
1763 ptlrpc_req_finished(req);
1765 rc = llu_file_release(ino);
1770 ptlrpc_req_finished(req);
1774 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1776 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1778 int flags = FMODE_WRITE;
1781 LASSERT(sizeof(lum) == sizeof(*lump));
1782 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1783 if (copy_from_user(&lum, lump, sizeof(lum)))
1786 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1790 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1792 struct intnl_stat *st = llu_i2stat(ino);
1793 if (S_ISREG(st->st_mode))
1794 return llu_lov_file_setstripe(ino, arg);
1795 if (S_ISDIR(st->st_mode))
1796 return llu_lov_dir_setstripe(ino, arg);
1801 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1803 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1808 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1812 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1818 liblustre_wait_event(0);
1821 case LL_IOC_GROUP_LOCK:
1822 arg = va_arg(ap, unsigned long);
1823 rc = llu_get_grouplock(ino, arg);
1825 case LL_IOC_GROUP_UNLOCK:
1826 arg = va_arg(ap, unsigned long);
1827 rc = llu_put_grouplock(ino, arg);
1829 case LL_IOC_LOV_SETSTRIPE:
1830 arg = va_arg(ap, unsigned long);
1831 rc = llu_lov_setstripe(ino, arg);
1833 case LL_IOC_LOV_GETSTRIPE:
1834 arg = va_arg(ap, unsigned long);
1835 rc = llu_lov_getstripe(ino, arg);
1838 CERROR("did not support ioctl cmd %lx\n", request);
1843 liblustre_wait_event(0);
1848 * we already do syncronous read/write
1850 static int llu_iop_sync(struct inode *inode)
1852 liblustre_wait_event(0);
1856 static int llu_iop_datasync(struct inode *inode)
1858 liblustre_wait_event(0);
1862 struct filesys_ops llu_filesys_ops =
1864 fsop_gone: llu_fsop_gone,
1867 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1869 struct inode *inode;
1871 struct file_identifier fileid = {&fid, sizeof(fid)};
1873 if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1874 (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1875 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1877 return ERR_PTR(-EPERM);
1880 /* try to find existing inode */
1881 fid = md->body->fid1;
1883 inode = _sysio_i_find(fs, &fileid);
1885 if (inode->i_zombie/* ||
1886 lli->lli_st_generation != md->body->generation*/) {
1890 llu_update_inode(inode, md);
1895 inode = llu_new_inode(fs, &fid);
1897 llu_update_inode(inode, md);
1903 llu_fsswop_mount(const char *source,
1905 const void *data __IS_UNUSED,
1906 struct pnode *tocover,
1907 struct mount **mntp)
1911 struct pnode_base *rootpb;
1912 struct obd_device *obd;
1913 struct llu_sb_info *sbi;
1914 struct obd_statfs osfs;
1915 static struct qstr noname = { NULL, 0, 0 };
1916 struct ptlrpc_request *request = NULL;
1917 struct lustre_md md;
1919 struct config_llog_instance cfg = {0, };
1920 char ll_instance[sizeof(sbi) * 2 + 1];
1921 struct lustre_profile *lprof;
1922 char *zconf_mgsnid, *zconf_profile;
1923 char *osc = NULL, *mdc = NULL;
1924 int async = 1, err = -EINVAL;
1925 struct obd_connect_data ocd = {0,};
1929 if (ll_parse_mount_target(source,
1932 CERROR("mal-formed target %s\n", source);
1935 if (!zconf_mgsnid || !zconf_profile) {
1936 printf("Liblustre: invalid target %s\n", source);
1939 /* allocate & initialize sbi */
1940 OBD_ALLOC(sbi, sizeof(*sbi));
1944 CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
1945 ll_generate_random_uuid(uuid);
1946 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1948 /* generate a string unique to this super, let's try
1949 the address of the super itself.*/
1950 sprintf(ll_instance, "%p", sbi);
1952 /* retrive & parse config log */
1953 cfg.cfg_instance = ll_instance;
1954 cfg.cfg_uuid = sbi->ll_sb_uuid;
1955 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
1957 CERROR("Unable to process log: %s\n", zconf_profile);
1958 GOTO(out_free, err);
1961 lprof = class_get_profile(zconf_profile);
1962 if (lprof == NULL) {
1963 CERROR("No profile found: %s\n", zconf_profile);
1964 GOTO(out_free, err = -EINVAL);
1966 OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
1967 sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
1969 OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
1970 sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
1974 GOTO(out_free, err = -EINVAL);
1978 GOTO(out_free, err = -EINVAL);
1981 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
1987 obd = class_name2obd(mdc);
1989 CERROR("MDC %s: not setup or attached\n", mdc);
1990 GOTO(out_free, err = -EINVAL);
1992 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
1993 sizeof(async), &async, NULL);
1995 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
1996 OBD_CONNECT_FID | OBD_CONNECT_AT |
1997 OBD_CONNECT_VBR | OBD_CONNECT_SOM;
1998 #ifdef LIBLUSTRE_POSIX_ACL
1999 ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
2001 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
2002 ocd.ocd_version = LUSTRE_VERSION_CODE;
2005 err = obd_connect(NULL, &sbi->ll_md_exp, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2007 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
2008 GOTO(out_free, err);
2011 err = obd_statfs(obd, &osfs, 100000000, 0);
2016 * FIXME fill fs stat data into sbi here!!! FIXME
2020 obd = class_name2obd(osc);
2022 CERROR("OSC %s: not setup or attached\n", osc);
2023 GOTO(out_md, err = -EINVAL);
2025 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
2026 sizeof(async), &async, NULL);
2028 obd->obd_upcall.onu_owner = &sbi->ll_lco;
2029 obd->obd_upcall.onu_upcall = cl_ocd_update;
2031 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
2032 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK |
2033 OBD_CONNECT_FID | OBD_CONNECT_AT |
2036 ocd.ocd_version = LUSTRE_VERSION_CODE;
2037 err = obd_connect(NULL, &sbi->ll_dt_exp, obd, &sbi->ll_sb_uuid, &ocd, NULL);
2039 CERROR("cannot connect to %s: rc = %d\n", osc, err);
2042 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
2043 sbi->ll_lco.lco_md_exp = sbi->ll_md_exp;
2044 sbi->ll_lco.lco_dt_exp = sbi->ll_dt_exp;
2046 fid_zero(&sbi->ll_root_fid);
2047 err = md_getstatus(sbi->ll_md_exp, &sbi->ll_root_fid, NULL);
2049 CERROR("cannot mds_connect: rc = %d\n", err);
2050 GOTO(out_lock_cn_cb, err);
2052 if (!fid_is_sane(&sbi->ll_root_fid)) {
2053 CERROR("Invalid root fid during mount\n");
2054 GOTO(out_lock_cn_cb, err = -EINVAL);
2056 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&sbi->ll_root_fid));
2058 /* fetch attr of root inode */
2059 err = md_getattr(sbi->ll_md_exp, &sbi->ll_root_fid, NULL,
2060 OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
2062 CERROR("md_getattr failed for root: rc = %d\n", err);
2063 GOTO(out_lock_cn_cb, err);
2066 err = md_get_lustre_md(sbi->ll_md_exp, request,
2067 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2069 CERROR("failed to understand root inode md: rc = %d\n",err);
2070 GOTO(out_request, err);
2073 LASSERT(fid_is_sane(&sbi->ll_root_fid));
2075 root = llu_iget(fs, &md);
2076 if (!root || IS_ERR(root)) {
2077 CERROR("fail to generate root inode\n");
2078 GOTO(out_request, err = -EBADF);
2082 * Generate base path-node for root.
2084 rootpb = _sysio_pb_new(&noname, NULL, root);
2090 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2092 _sysio_pb_gone(rootpb);
2098 ptlrpc_req_finished(request);
2100 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2101 liblustre_wait_idle();
2106 _sysio_i_gone(root);
2108 ptlrpc_req_finished(request);
2110 obd_disconnect(sbi->ll_dt_exp);
2112 obd_disconnect(sbi->ll_md_exp);
2115 OBD_FREE(osc, strlen(osc) + 1);
2117 OBD_FREE(mdc, strlen(mdc) + 1);
2118 OBD_FREE(sbi, sizeof(*sbi));
2119 liblustre_wait_idle();
2123 struct fssw_ops llu_fssw_ops = {
2127 static struct inode_ops llu_inode_ops = {
2128 inop_lookup: llu_iop_lookup,
2129 inop_getattr: llu_iop_getattr,
2130 inop_setattr: llu_iop_setattr,
2131 inop_filldirentries: llu_iop_filldirentries,
2132 inop_mkdir: llu_iop_mkdir_raw,
2133 inop_rmdir: llu_iop_rmdir_raw,
2134 inop_symlink: llu_iop_symlink_raw,
2135 inop_readlink: llu_iop_readlink,
2136 inop_open: llu_iop_open,
2137 inop_close: llu_iop_close,
2138 inop_link: llu_iop_link_raw,
2139 inop_unlink: llu_iop_unlink_raw,
2140 inop_rename: llu_iop_rename_raw,
2141 inop_pos: llu_iop_pos,
2142 inop_read: llu_iop_read,
2143 inop_write: llu_iop_write,
2144 inop_iodone: llu_iop_iodone,
2145 inop_fcntl: llu_iop_fcntl,
2146 inop_sync: llu_iop_sync,
2147 inop_datasync: llu_iop_datasync,
2148 inop_ioctl: llu_iop_ioctl,
2149 inop_mknod: llu_iop_mknod_raw,
2150 #ifdef _HAVE_STATVFS
2151 inop_statvfs: llu_iop_statvfs,
2153 inop_gone: llu_iop_gone,