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 (c) 2003, 2010, Oracle and/or its affiliates. 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>
57 #include "llite_lib.h"
65 #define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
67 static int ll_permission(struct inode *inode, int mask)
69 struct intnl_stat *st = llu_i2stat(inode);
70 mode_t mode = st->st_mode;
72 if (current->fsuid == st->st_uid)
74 else if (cfs_curproc_is_in_groups(st->st_gid))
77 if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
80 if ((mask & (MAY_READ|MAY_WRITE)) ||
81 (st->st_mode & S_IXUGO))
82 if (cfs_capable(CFS_CAP_DAC_OVERRIDE))
85 if (mask == MAY_READ ||
86 (S_ISDIR(st->st_mode) && !(mask & MAY_WRITE))) {
87 if (cfs_capable(CFS_CAP_DAC_READ_SEARCH))
94 static void llu_fsop_gone(struct filesys *fs)
96 struct llu_sb_info *sbi = (struct llu_sb_info *) fs->fs_private;
97 struct obd_device *obd = class_exp2obd(sbi->ll_md_exp);
101 cfs_list_del(&sbi->ll_conn_chain);
103 obd_disconnect(sbi->ll_dt_exp);
104 obd_disconnect(sbi->ll_md_exp);
106 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
107 class_manual_cleanup(obd);
109 OBD_FREE(sbi, sizeof(*sbi));
111 liblustre_wait_idle();
115 static struct inode_ops llu_inode_ops;
117 static ldlm_mode_t llu_take_md_lock(struct inode *inode, __u64 bits,
118 struct lustre_handle *lockh)
120 ldlm_policy_data_t policy = { .l_inodebits = {bits}};
126 fid = &llu_i2info(inode)->lli_fid;
127 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
129 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
130 rc = md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
131 LCK_CR|LCK_CW|LCK_PR|LCK_PW, lockh);
135 void llu_update_inode(struct inode *inode, struct lustre_md *md)
137 struct llu_inode_info *lli = llu_i2info(inode);
138 struct mdt_body *body = md->body;
139 struct lov_stripe_md *lsm = md->lsm;
140 struct intnl_stat *st = llu_i2stat(inode);
142 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
144 if (body->valid & OBD_MD_FLMODE)
145 st->st_mode = (st->st_mode & S_IFMT)|(body->mode & ~S_IFMT);
146 if (body->valid & OBD_MD_FLTYPE)
147 st->st_mode = (st->st_mode & ~S_IFMT)|(body->mode & S_IFMT);
150 if (lli->lli_smd == NULL) {
151 cl_inode_init(inode, md);
153 lli->lli_maxbytes = lsm->lsm_maxbytes;
154 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
155 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
157 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
158 CERROR("lsm mismatch for inode %lld\n",
159 (long long)st->st_ino);
165 if (body->valid & OBD_MD_FLATIME) {
166 if (body->atime > LTIME_S(st->st_atime))
167 LTIME_S(st->st_atime) = body->atime;
168 lli->lli_lvb.lvb_atime = body->atime;
170 if (body->valid & OBD_MD_FLMTIME) {
171 if (body->mtime > LTIME_S(st->st_mtime))
172 LTIME_S(st->st_mtime) = body->mtime;
173 lli->lli_lvb.lvb_mtime = body->mtime;
175 if (body->valid & OBD_MD_FLCTIME) {
176 if (body->ctime > LTIME_S(st->st_ctime))
177 LTIME_S(st->st_ctime) = body->ctime;
178 lli->lli_lvb.lvb_ctime = body->ctime;
180 if (S_ISREG(st->st_mode))
181 st->st_blksize = min(2UL * PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
183 st->st_blksize = 4096;
184 if (body->valid & OBD_MD_FLUID)
185 st->st_uid = body->uid;
186 if (body->valid & OBD_MD_FLGID)
187 st->st_gid = body->gid;
188 if (body->valid & OBD_MD_FLNLINK)
189 st->st_nlink = body->nlink;
190 if (body->valid & OBD_MD_FLRDEV)
191 st->st_rdev = body->rdev;
192 if (body->valid & OBD_MD_FLFLAGS)
193 lli->lli_st_flags = body->flags;
194 if (body->valid & OBD_MD_FLSIZE) {
195 if ((llu_i2sbi(inode)->ll_lco.lco_flags & OBD_CONNECT_SOM) &&
196 S_ISREG(st->st_mode) && lli->lli_smd) {
197 struct lustre_handle lockh;
200 /* As it is possible a blocking ast has been processed
201 * by this time, we need to check there is an UPDATE
202 * lock on the client and set LLIF_MDS_SIZE_LOCK holding
204 mode = llu_take_md_lock(inode, MDS_INODELOCK_UPDATE,
207 st->st_size = body->size;
208 lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
209 ldlm_lock_decref(&lockh, mode);
212 st->st_size = body->size;
215 if (body->valid & OBD_MD_FLBLOCKS)
216 st->st_blocks = body->blocks;
220 void obdo_to_inode(struct inode *dst, struct obdo *src, obd_flag valid)
222 struct llu_inode_info *lli = llu_i2info(dst);
223 struct intnl_stat *st = llu_i2stat(dst);
225 valid &= src->o_valid;
227 LASSERTF(!(valid & (OBD_MD_FLTYPE | OBD_MD_FLGENER | OBD_MD_FLFID |
228 OBD_MD_FLID | OBD_MD_FLGROUP)),
229 "object "LPU64"/"LPU64", valid %x\n",
230 src->o_id, src->o_seq, valid);
232 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
233 CDEBUG(D_INODE,"valid "LPX64", cur time "CFS_TIME_T"/"CFS_TIME_T
236 LTIME_S(st->st_mtime), LTIME_S(st->st_ctime),
237 (long)src->o_mtime, (long)src->o_ctime);
239 if (valid & OBD_MD_FLATIME)
240 LTIME_S(st->st_atime) = src->o_atime;
241 if (valid & OBD_MD_FLMTIME)
242 LTIME_S(st->st_mtime) = src->o_mtime;
243 if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
244 LTIME_S(st->st_ctime) = src->o_ctime;
245 if (valid & OBD_MD_FLSIZE)
246 st->st_size = src->o_size;
247 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
248 st->st_blocks = src->o_blocks;
249 if (valid & OBD_MD_FLBLKSZ)
250 st->st_blksize = src->o_blksize;
251 if (valid & OBD_MD_FLTYPE)
252 st->st_mode = (st->st_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
253 if (valid & OBD_MD_FLMODE)
254 st->st_mode = (st->st_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
255 if (valid & OBD_MD_FLUID)
256 st->st_uid = src->o_uid;
257 if (valid & OBD_MD_FLGID)
258 st->st_gid = src->o_gid;
259 if (valid & OBD_MD_FLFLAGS)
260 lli->lli_st_flags = src->o_flags;
264 * Performs the getattr on the inode and updates its fields.
265 * If @sync != 0, perform the getattr under the server-side lock.
267 int llu_inode_getattr(struct inode *inode, struct obdo *obdo,
268 __u64 ioepoch, int sync)
270 struct llu_inode_info *lli = llu_i2info(inode);
271 struct ptlrpc_request_set *set;
272 struct lov_stripe_md *lsm = lli->lli_smd;
273 struct obd_info oinfo = { { { 0 } } };
281 oinfo.oi_oa->o_id = lsm->lsm_object_id;
282 oinfo.oi_oa->o_seq = lsm->lsm_object_seq;
283 oinfo.oi_oa->o_mode = S_IFREG;
284 oinfo.oi_oa->o_ioepoch = ioepoch;
285 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
286 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
287 OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
288 OBD_MD_FLCTIME | OBD_MD_FLGROUP |
289 OBD_MD_FLATIME | OBD_MD_FLEPOCH;
290 obdo_from_inode(oinfo.oi_oa, NULL, &llu_i2info(inode)->lli_fid, 0);
292 oinfo.oi_oa->o_valid |= OBD_MD_FLFLAGS;
293 oinfo.oi_oa->o_flags |= OBD_FL_SRVLOCK;
296 set = ptlrpc_prep_set();
298 CERROR ("ENOMEM allocing request set\n");
301 rc = obd_getattr_async(llu_i2obdexp(inode), &oinfo, set);
303 rc = ptlrpc_set_wait(set);
304 ptlrpc_set_destroy(set);
309 oinfo.oi_oa->o_valid = OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ |
310 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
313 obdo_refresh_inode(inode, oinfo.oi_oa, oinfo.oi_oa->o_valid);
314 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %Lu, "
315 "blksize %Lu\n", lli->lli_smd->lsm_object_id,
316 (long long unsigned)llu_i2stat(inode)->st_size,
317 (long long unsigned)llu_i2stat(inode)->st_blocks,
318 (long long unsigned)llu_i2stat(inode)->st_blksize);
322 static struct inode* llu_new_inode(struct filesys *fs,
326 struct llu_inode_info *lli;
327 struct intnl_stat st = {
330 #ifndef AUTOMOUNT_FILE_NAME
331 .st_mode = fid->f_type & S_IFMT,
333 .st_mode = fid->f_type /* all of the bits! */
336 /* FIXME: fix this later */
343 OBD_ALLOC(lli, sizeof(*lli));
347 /* initialize lli here */
348 lli->lli_sbi = llu_fs2sbi(fs);
350 lli->lli_symlink_name = NULL;
352 lli->lli_maxbytes = (__u64)(~0UL);
353 lli->lli_file_data = NULL;
355 lli->lli_sysio_fid.fid_data = &lli->lli_fid;
356 lli->lli_sysio_fid.fid_len = sizeof(lli->lli_fid);
359 /* file identifier is needed by functions like _sysio_i_find() */
360 inode = _sysio_i_new(fs, &lli->lli_sysio_fid,
361 &st, 0, &llu_inode_ops, lli);
364 OBD_FREE(lli, sizeof(*lli));
369 static int llu_have_md_lock(struct inode *inode, __u64 lockpart)
371 struct lustre_handle lockh;
372 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
379 fid = &llu_i2info(inode)->lli_fid;
380 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
382 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
383 if (md_lock_match(llu_i2mdexp(inode), flags, fid, LDLM_IBITS, &policy,
384 LCK_CR|LCK_CW|LCK_PR|LCK_PW, &lockh)) {
390 static int llu_inode_revalidate(struct inode *inode)
392 struct llu_inode_info *lli = llu_i2info(inode);
393 struct intnl_stat *st = llu_i2stat(inode);
397 CERROR("REPORT THIS LINE TO PETER\n");
401 if (!llu_have_md_lock(inode, MDS_INODELOCK_UPDATE)) {
403 struct ptlrpc_request *req = NULL;
404 struct llu_sb_info *sbi = llu_i2sbi(inode);
405 struct md_op_data op_data = { { 0 } };
406 unsigned long valid = OBD_MD_FLGETATTR;
409 /* Why don't we update all valid MDS fields here, if we're
410 * doing an RPC anyways? -phil */
411 if (S_ISREG(st->st_mode)) {
412 ealen = obd_size_diskmd(sbi->ll_dt_exp, NULL);
413 valid |= OBD_MD_FLEASIZE;
416 llu_prep_md_op_data(&op_data, inode, NULL, NULL, 0, ealen,
418 op_data.op_valid = valid;
420 rc = md_getattr(sbi->ll_md_exp, &op_data, &req);
422 CERROR("failure %d inode %llu\n", rc,
423 (long long)st->st_ino);
426 rc = md_get_lustre_md(sbi->ll_md_exp, req,
427 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
429 /* XXX Too paranoid? */
430 if (((md.body->valid ^ valid) & OBD_MD_FLEASIZE) &&
431 !((md.body->valid & OBD_MD_FLNLINK) &&
432 (md.body->nlink == 0))) {
433 CERROR("Asked for %s eadata but got %s (%d)\n",
434 (valid & OBD_MD_FLEASIZE) ? "some" : "no",
435 (md.body->valid & OBD_MD_FLEASIZE) ? "some":"none",
436 md.body->eadatasize);
439 ptlrpc_req_finished(req);
444 llu_update_inode(inode, &md);
445 if (md.lsm != NULL && lli->lli_smd != md.lsm)
446 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
447 ptlrpc_req_finished(req);
451 /* object not yet allocated, don't validate size */
452 st->st_atime = lli->lli_lvb.lvb_atime;
453 st->st_mtime = lli->lli_lvb.lvb_mtime;
454 st->st_ctime = lli->lli_lvb.lvb_ctime;
458 /* ll_glimpse_size will prefer locally cached writes if they extend
460 RETURN(cl_glimpse_size(inode));
463 static void copy_stat_buf(struct inode *ino, struct intnl_stat *b)
465 *b = *llu_i2stat(ino);
468 static int llu_iop_getattr(struct pnode *pno,
470 struct intnl_stat *b)
475 liblustre_wait_event(0);
479 LASSERT(pno->p_base->pb_ino);
480 ino = pno->p_base->pb_ino;
482 LASSERT(!pno || pno->p_base->pb_ino == ino);
485 /* libsysio might call us directly without intent lock,
486 * we must re-fetch the attrs here
488 rc = llu_inode_revalidate(ino);
490 copy_stat_buf(ino, b);
491 LASSERT(!llu_i2info(ino)->lli_it);
494 liblustre_wait_event(0);
498 static int null_if_equal(struct ldlm_lock *lock, void *data)
500 if (data == lock->l_ast_data) {
501 lock->l_ast_data = NULL;
503 if (lock->l_req_mode != lock->l_granted_mode)
504 LDLM_ERROR(lock,"clearing inode with ungranted lock\n");
507 return LDLM_ITER_CONTINUE;
510 void llu_clear_inode(struct inode *inode)
512 struct llu_inode_info *lli = llu_i2info(inode);
513 struct llu_sb_info *sbi = llu_i2sbi(inode);
516 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu(%p)\n",
517 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation,
520 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
521 md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
522 null_if_equal, inode);
525 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
526 null_if_equal, inode);
528 cl_inode_fini(inode);
531 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
535 if (lli->lli_symlink_name) {
536 OBD_FREE(lli->lli_symlink_name,
537 strlen(lli->lli_symlink_name) + 1);
538 lli->lli_symlink_name = NULL;
544 void llu_iop_gone(struct inode *inode)
546 struct llu_inode_info *lli = llu_i2info(inode);
549 liblustre_wait_event(0);
550 llu_clear_inode(inode);
552 OBD_FREE(lli, sizeof(*lli));
556 static int inode_setattr(struct inode * inode, struct iattr * attr)
558 unsigned int ia_valid = attr->ia_valid;
559 struct intnl_stat *st = llu_i2stat(inode);
563 * inode_setattr() is only ever invoked with ATTR_SIZE (by
564 * llu_setattr_raw()) when file has no bodies. Check this.
566 LASSERT(ergo(ia_valid & ATTR_SIZE, llu_i2info(inode)->lli_smd == NULL));
568 if (ia_valid & ATTR_SIZE)
569 st->st_size = attr->ia_size;
570 if (ia_valid & ATTR_UID)
571 st->st_uid = attr->ia_uid;
572 if (ia_valid & ATTR_GID)
573 st->st_gid = attr->ia_gid;
574 if (ia_valid & ATTR_ATIME)
575 st->st_atime = attr->ia_atime;
576 if (ia_valid & ATTR_MTIME)
577 st->st_mtime = attr->ia_mtime;
578 if (ia_valid & ATTR_CTIME)
579 st->st_ctime = attr->ia_ctime;
580 if (ia_valid & ATTR_MODE) {
581 st->st_mode = attr->ia_mode;
582 if (!cfs_curproc_is_in_groups(st->st_gid) &&
583 !cfs_capable(CFS_CAP_FSETID))
584 st->st_mode &= ~S_ISGID;
586 /* mark_inode_dirty(inode); */
590 int llu_md_setattr(struct inode *inode, struct md_op_data *op_data,
591 struct md_open_data **mod)
594 struct llu_sb_info *sbi = llu_i2sbi(inode);
595 struct ptlrpc_request *request = NULL;
599 llu_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0, LUSTRE_OPC_ANY);
600 rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL,
604 ptlrpc_req_finished(request);
605 if (rc != -EPERM && rc != -EACCES)
606 CERROR("md_setattr fails: rc = %d\n", rc);
610 rc = md_get_lustre_md(sbi->ll_md_exp, request,
611 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
613 ptlrpc_req_finished(request);
617 /* We call inode_setattr to adjust timestamps.
618 * If there is at least some data in file, we cleared ATTR_SIZE
619 * above to avoid invoking vmtruncate, otherwise it is important
620 * to call vmtruncate in inode_setattr to update inode->i_size
622 inode_setattr(inode, &op_data->op_attr);
623 llu_update_inode(inode, &md);
624 ptlrpc_req_finished(request);
629 /* Close IO epoch and send Size-on-MDS attribute update. */
630 static int llu_setattr_done_writing(struct inode *inode,
631 struct md_op_data *op_data,
632 struct md_open_data *mod)
634 struct llu_inode_info *lli = llu_i2info(inode);
635 struct intnl_stat *st = llu_i2stat(inode);
639 LASSERT(op_data != NULL);
640 if (!S_ISREG(st->st_mode))
643 /* XXX: pass och here for the recovery purpose. */
644 CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
645 op_data->op_ioepoch, PFID(&lli->lli_fid));
647 op_data->op_flags = MF_EPOCH_CLOSE;
648 llu_done_writing_attr(inode, op_data);
649 llu_pack_inode2opdata(inode, op_data, NULL);
651 rc = md_done_writing(llu_i2sbi(inode)->ll_md_exp, op_data, mod);
653 /* MDS has instructed us to obtain Size-on-MDS attribute
654 * from OSTs and send setattr to back to MDS. */
655 rc = llu_som_update(inode, op_data);
657 CERROR("inode %llu mdc truncate failed: rc = %d\n",
658 (unsigned long long)st->st_ino, rc);
663 /* If this inode has objects allocated to it (lsm != NULL), then the OST
664 * object(s) determine the file size and mtime. Otherwise, the MDS will
665 * keep these values until such a time that objects are allocated for it.
666 * We do the MDS operations first, as it is checking permissions for us.
667 * We don't to the MDS RPC if there is nothing that we want to store there,
668 * otherwise there is no harm in updating mtime/atime on the MDS if we are
669 * going to do an RPC anyways.
671 * If we are doing a truncate, we will send the mtime and ctime updates
672 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
673 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
676 int llu_setattr_raw(struct inode *inode, struct iattr *attr)
678 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
679 struct intnl_stat *st = llu_i2stat(inode);
680 int ia_valid = attr->ia_valid;
681 struct md_op_data op_data = { { 0 } };
682 struct md_open_data *mod = NULL;
686 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
688 if (ia_valid & ATTR_SIZE) {
689 if (attr->ia_size > ll_file_maxbytes(inode)) {
690 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
691 (long long)attr->ia_size,
692 ll_file_maxbytes(inode));
696 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
699 /* We mark all of the fields "set" so MDS/OST does not re-set them */
700 if (attr->ia_valid & ATTR_CTIME) {
701 attr->ia_ctime = CFS_CURRENT_TIME;
702 attr->ia_valid |= ATTR_CTIME_SET;
704 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
705 attr->ia_atime = CFS_CURRENT_TIME;
706 attr->ia_valid |= ATTR_ATIME_SET;
708 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
709 attr->ia_mtime = CFS_CURRENT_TIME;
710 attr->ia_valid |= ATTR_MTIME_SET;
713 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
714 CDEBUG(D_INODE, "setting mtime "CFS_TIME_T", ctime "CFS_TIME_T
715 ", now = "CFS_TIME_T"\n",
716 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
717 LTIME_S(CFS_CURRENT_TIME));
719 /* NB: ATTR_SIZE will only be set after this point if the size
720 * resides on the MDS, ie, this file has no objects. */
722 attr->ia_valid &= ~ATTR_SIZE;
724 /* If only OST attributes being set on objects, don't do MDS RPC.
725 * In that case, we need to check permissions and update the local
726 * inode ourselves so we can call obdo_from_inode() always. */
727 if (ia_valid & (lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
728 memcpy(&op_data.op_attr, attr, sizeof(*attr));
730 /* Open epoch for truncate. */
731 if (exp_connect_som(llu_i2mdexp(inode)) &&
732 (ia_valid & ATTR_SIZE))
733 op_data.op_flags = MF_EPOCH_OPEN;
734 rc = llu_md_setattr(inode, &op_data, &mod);
738 llu_ioepoch_open(llu_i2info(inode), op_data.op_ioepoch);
739 if (!lsm || !S_ISREG(st->st_mode)) {
740 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
744 /* The OST doesn't check permissions, but the alternative is
745 * a gratuitous RPC to the MDS. We already rely on the client
746 * to do read/write/truncate permission checks, so is mtime OK?
748 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
749 /* from sys_utime() */
750 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
751 if (current->fsuid != st->st_uid &&
752 (rc = ll_permission(inode, MAY_WRITE)) != 0)
755 /* from inode_change_ok() */
756 if (current->fsuid != st->st_uid &&
757 !cfs_capable(CFS_CAP_FOWNER))
763 /* Won't invoke llu_vmtruncate(), as we already cleared
765 inode_setattr(inode, attr);
768 if (ia_valid & ATTR_SIZE)
769 attr->ia_valid |= ATTR_SIZE;
770 if (ia_valid & (ATTR_SIZE |
771 ATTR_ATIME | ATTR_ATIME_SET |
772 ATTR_MTIME | ATTR_MTIME_SET))
773 /* on truncate and utimes send attributes to osts, setting
774 * mtime/atime to past will be performed under PW 0:EOF extent
775 * lock (new_size:EOF for truncate)
776 * it may seem excessive to send mtime/atime updates to osts
777 * when not setting times to past, but it is necessary due to
778 * possible time de-synchronization */
779 rc = cl_setattr_ost(inode, attr, NULL);
782 if (op_data.op_ioepoch)
783 rc1 = llu_setattr_done_writing(inode, &op_data, mod);
784 return rc ? rc : rc1;
787 /* here we simply act as a thin layer to glue it with
788 * llu_setattr_raw(), which is copy from kernel
790 static int llu_iop_setattr(struct pnode *pno,
793 struct intnl_stat *stbuf)
799 liblustre_wait_event(0);
801 LASSERT(!(mask & ~(SETATTR_MTIME | SETATTR_ATIME |
802 SETATTR_UID | SETATTR_GID |
803 SETATTR_LEN | SETATTR_MODE)));
804 memset(&iattr, 0, sizeof(iattr));
806 if (mask & SETATTR_MODE) {
807 iattr.ia_mode = stbuf->st_mode;
808 iattr.ia_valid |= ATTR_MODE;
810 if (mask & SETATTR_MTIME) {
811 iattr.ia_mtime = stbuf->st_mtime;
812 iattr.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
814 if (mask & SETATTR_ATIME) {
815 iattr.ia_atime = stbuf->st_atime;
816 iattr.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
818 if (mask & SETATTR_UID) {
819 iattr.ia_uid = stbuf->st_uid;
820 iattr.ia_valid |= ATTR_UID;
822 if (mask & SETATTR_GID) {
823 iattr.ia_gid = stbuf->st_gid;
824 iattr.ia_valid |= ATTR_GID;
826 if (mask & SETATTR_LEN) {
827 iattr.ia_size = stbuf->st_size; /* XXX signed expansion problem */
828 iattr.ia_valid |= ATTR_SIZE;
831 iattr.ia_valid |= ATTR_RAW | ATTR_CTIME;
832 iattr.ia_ctime = CFS_CURRENT_TIME;
834 rc = llu_setattr_raw(ino, &iattr);
835 liblustre_wait_idle();
839 #define EXT2_LINK_MAX 32000
841 static int llu_iop_symlink_raw(struct pnode *pno, const char *tgt)
843 struct inode *dir = pno->p_base->pb_parent->pb_ino;
844 struct qstr *qstr = &pno->p_base->pb_name;
845 const char *name = qstr->name;
847 struct ptlrpc_request *request = NULL;
848 struct llu_sb_info *sbi = llu_i2sbi(dir);
849 struct md_op_data op_data = {{ 0 }};
853 liblustre_wait_event(0);
854 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
857 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
860 err = md_create(sbi->ll_md_exp, &op_data, tgt, strlen(tgt) + 1,
861 S_IFLNK | S_IRWXUGO, current->fsuid, current->fsgid,
862 cfs_curproc_cap_pack(), 0, &request);
863 ptlrpc_req_finished(request);
864 liblustre_wait_event(0);
868 static int llu_readlink_internal(struct inode *inode,
869 struct ptlrpc_request **request,
872 struct llu_inode_info *lli = llu_i2info(inode);
873 struct llu_sb_info *sbi = llu_i2sbi(inode);
874 struct mdt_body *body;
875 struct intnl_stat *st = llu_i2stat(inode);
876 struct md_op_data op_data = {{ 0 }};
877 int rc, symlen = st->st_size + 1;
883 if (lli->lli_symlink_name) {
884 *symname = lli->lli_symlink_name;
885 CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
889 llu_prep_md_op_data(&op_data, inode, NULL, NULL, 0, symlen,
891 op_data.op_valid = OBD_MD_LINKNAME;
893 rc = md_getattr(sbi->ll_md_exp, &op_data, request);
895 CERROR("inode %llu: rc = %d\n", (long long)st->st_ino, rc);
899 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
900 LASSERT(body != NULL);
902 if ((body->valid & OBD_MD_LINKNAME) == 0) {
903 CERROR ("OBD_MD_LINKNAME not set on reply\n");
904 GOTO (failed, rc = -EPROTO);
907 LASSERT(symlen != 0);
908 if (body->eadatasize != symlen) {
909 CERROR("inode %llu: symlink length %d not expected %d\n",
910 (long long)st->st_ino, body->eadatasize - 1, symlen - 1);
911 GOTO(failed, rc = -EPROTO);
914 *symname = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_MD);
915 if (*symname == NULL ||
916 strnlen(*symname, symlen) != symlen - 1) {
917 /* not full/NULL terminated */
918 CERROR("inode %llu: symlink not NULL terminated string"
919 "of length %d\n", (long long)st->st_ino, symlen - 1);
920 GOTO(failed, rc = -EPROTO);
923 OBD_ALLOC(lli->lli_symlink_name, symlen);
924 /* do not return an error if we cannot cache the symlink locally */
925 if (lli->lli_symlink_name)
926 memcpy(lli->lli_symlink_name, *symname, symlen);
931 ptlrpc_req_finished (*request);
935 static int llu_iop_readlink(struct pnode *pno, char *data, size_t bufsize)
937 struct inode *inode = pno->p_base->pb_ino;
938 struct ptlrpc_request *request;
943 liblustre_wait_event(0);
944 rc = llu_readlink_internal(inode, &request, &symname);
949 strncpy(data, symname, bufsize);
950 rc = strlen(symname);
952 ptlrpc_req_finished(request);
954 liblustre_wait_event(0);
958 static int llu_iop_mknod_raw(struct pnode *pno,
962 struct ptlrpc_request *request = NULL;
963 struct inode *dir = pno->p_parent->p_base->pb_ino;
964 struct llu_sb_info *sbi = llu_i2sbi(dir);
965 struct md_op_data op_data = {{ 0 }};
969 liblustre_wait_event(0);
970 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu\n",
971 (int)pno->p_base->pb_name.len, pno->p_base->pb_name.name,
972 (long long)llu_i2stat(dir)->st_ino);
974 if (llu_i2stat(dir)->st_nlink >= EXT2_LINK_MAX)
977 switch (mode & S_IFMT) {
980 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
985 llu_prep_md_op_data(&op_data, dir, NULL,
986 pno->p_base->pb_name.name,
987 pno->p_base->pb_name.len, 0,
990 err = md_create(sbi->ll_md_exp, &op_data, NULL, 0, mode,
991 current->fsuid, current->fsgid,
992 cfs_curproc_cap_pack(), dev, &request);
993 ptlrpc_req_finished(request);
1001 liblustre_wait_event(0);
1005 static int llu_iop_link_raw(struct pnode *old, struct pnode *new)
1007 struct inode *src = old->p_base->pb_ino;
1008 struct inode *dir = new->p_parent->p_base->pb_ino;
1009 const char *name = new->p_base->pb_name.name;
1010 int namelen = new->p_base->pb_name.len;
1011 struct ptlrpc_request *request = NULL;
1012 struct md_op_data op_data = {{ 0 }};
1019 liblustre_wait_event(0);
1020 llu_prep_md_op_data(&op_data, src, dir, name, namelen, 0,
1022 rc = md_link(llu_i2sbi(src)->ll_md_exp, &op_data, &request);
1023 ptlrpc_req_finished(request);
1024 liblustre_wait_event(0);
1030 * libsysio will clear the inode immediately after return
1032 static int llu_iop_unlink_raw(struct pnode *pno)
1034 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1035 struct qstr *qstr = &pno->p_base->pb_name;
1036 const char *name = qstr->name;
1037 int len = qstr->len;
1038 struct inode *target = pno->p_base->pb_ino;
1039 struct ptlrpc_request *request = NULL;
1040 struct md_op_data op_data = { { 0 } };
1046 liblustre_wait_event(0);
1047 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1049 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1051 rc = llu_objects_destroy(request, dir);
1052 ptlrpc_req_finished(request);
1053 liblustre_wait_idle();
1058 static int llu_iop_rename_raw(struct pnode *old, struct pnode *new)
1060 struct inode *src = old->p_parent->p_base->pb_ino;
1061 struct inode *tgt = new->p_parent->p_base->pb_ino;
1062 const char *oldname = old->p_base->pb_name.name;
1063 int oldnamelen = old->p_base->pb_name.len;
1064 const char *newname = new->p_base->pb_name.name;
1065 int newnamelen = new->p_base->pb_name.len;
1066 struct ptlrpc_request *request = NULL;
1067 struct md_op_data op_data = { { 0 } };
1074 liblustre_wait_event(0);
1075 llu_prep_md_op_data(&op_data, src, tgt, NULL, 0, 0,
1077 rc = md_rename(llu_i2sbi(src)->ll_md_exp, &op_data,
1078 oldname, oldnamelen, newname, newnamelen,
1081 rc = llu_objects_destroy(request, src);
1084 ptlrpc_req_finished(request);
1085 liblustre_wait_idle();
1090 #ifdef _HAVE_STATVFS
1091 static int llu_statfs_internal(struct llu_sb_info *sbi,
1092 struct obd_statfs *osfs, __u64 max_age)
1094 struct obd_statfs obd_osfs;
1098 rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age, 0);
1100 CERROR("md_statfs fails: rc = %d\n", rc);
1104 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1105 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1107 rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1108 &obd_statfs, max_age, 0);
1110 CERROR("obd_statfs fails: rc = %d\n", rc);
1114 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1115 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1118 osfs->os_blocks = obd_osfs.os_blocks;
1119 osfs->os_bfree = obd_osfs.os_bfree;
1120 osfs->os_bavail = obd_osfs.os_bavail;
1122 /* If we don't have as many objects free on the OST as inodes
1123 * on the MDS, we reduce the total number of inodes to
1124 * compensate, so that the "inodes in use" number is correct.
1126 if (obd_osfs.os_ffree < osfs->os_ffree) {
1127 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1129 osfs->os_ffree = obd_osfs.os_ffree;
1135 static int llu_statfs(struct llu_sb_info *sbi, struct statfs *sfs)
1137 struct obd_statfs osfs;
1140 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1142 /* For now we will always get up-to-date statfs values, but in the
1143 * future we may allow some amount of caching on the client (e.g.
1144 * from QOS or lprocfs updates). */
1145 rc = llu_statfs_internal(sbi, &osfs,
1146 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS));
1150 statfs_unpack(sfs, &osfs);
1152 if (sizeof(sfs->f_blocks) == 4) {
1153 while (osfs.os_blocks > ~0UL) {
1156 osfs.os_blocks >>= 1;
1157 osfs.os_bfree >>= 1;
1158 osfs.os_bavail >>= 1;
1162 sfs->f_blocks = osfs.os_blocks;
1163 sfs->f_bfree = osfs.os_bfree;
1164 sfs->f_bavail = osfs.os_bavail;
1169 static int llu_iop_statvfs(struct pnode *pno,
1171 struct intnl_statvfs *buf)
1177 liblustre_wait_event(0);
1180 LASSERT(pno->p_base->pb_ino);
1181 rc = llu_statfs(llu_i2sbi(pno->p_base->pb_ino), &fs);
1185 /* from native driver */
1186 buf->f_bsize = fs.f_bsize; /* file system block size */
1187 buf->f_frsize = fs.f_bsize; /* file system fundamental block size */
1188 buf->f_blocks = fs.f_blocks;
1189 buf->f_bfree = fs.f_bfree;
1190 buf->f_bavail = fs.f_bavail;
1191 buf->f_files = fs.f_files; /* Total number serial numbers */
1192 buf->f_ffree = fs.f_ffree; /* Number free serial numbers */
1193 buf->f_favail = fs.f_ffree; /* Number free ser num for non-privileged*/
1194 buf->f_fsid = fs.f_fsid.__val[1];
1195 buf->f_flag = 0; /* No equiv in statfs; maybe use type? */
1196 buf->f_namemax = fs.f_namelen;
1199 liblustre_wait_event(0);
1202 #endif /* _HAVE_STATVFS */
1204 static int llu_iop_mkdir_raw(struct pnode *pno, mode_t mode)
1206 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1207 struct qstr *qstr = &pno->p_base->pb_name;
1208 const char *name = qstr->name;
1209 int len = qstr->len;
1210 struct ptlrpc_request *request = NULL;
1211 struct intnl_stat *st = llu_i2stat(dir);
1212 struct md_op_data op_data = {{ 0 }};
1216 liblustre_wait_event(0);
1217 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1218 (long long)st->st_ino, llu_i2info(dir)->lli_st_generation, dir);
1220 if (st->st_nlink >= EXT2_LINK_MAX)
1223 llu_prep_md_op_data(&op_data, dir, NULL, name, len, 0,
1226 err = md_create(llu_i2sbi(dir)->ll_md_exp, &op_data, NULL, 0,
1227 mode | S_IFDIR, current->fsuid, current->fsgid,
1228 cfs_curproc_cap_pack(), 0, &request);
1229 ptlrpc_req_finished(request);
1230 liblustre_wait_event(0);
1234 static int llu_iop_rmdir_raw(struct pnode *pno)
1236 struct inode *dir = pno->p_base->pb_parent->pb_ino;
1237 struct qstr *qstr = &pno->p_base->pb_name;
1238 const char *name = qstr->name;
1239 int len = qstr->len;
1240 struct ptlrpc_request *request = NULL;
1241 struct md_op_data op_data = {{ 0 }};
1245 liblustre_wait_event(0);
1246 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%llu/%lu(%p)\n", len, name,
1247 (long long)llu_i2stat(dir)->st_ino,
1248 llu_i2info(dir)->lli_st_generation, dir);
1250 llu_prep_md_op_data(&op_data, dir, NULL, name, len, S_IFDIR,
1252 rc = md_unlink(llu_i2sbi(dir)->ll_md_exp, &op_data, &request);
1253 ptlrpc_req_finished(request);
1255 liblustre_wait_event(0);
1260 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC|O_DIRECT)
1262 #define FCNTL_FLMASK (O_APPEND|O_NONBLOCK|O_ASYNC)
1264 #define FCNTL_FLMASK_INVALID (O_NONBLOCK|O_ASYNC)
1266 /* refer to ll_file_flock() for details */
1267 static int llu_file_flock(struct inode *ino,
1269 struct file_lock *file_lock)
1271 struct llu_inode_info *lli = llu_i2info(ino);
1272 struct intnl_stat *st = llu_i2stat(ino);
1273 struct ldlm_res_id res_id =
1274 { .name = {fid_seq(&lli->lli_fid),
1275 fid_oid(&lli->lli_fid),
1276 fid_ver(&lli->lli_fid),
1278 struct ldlm_enqueue_info einfo = { LDLM_FLOCK, 0, NULL,
1279 ldlm_flock_completion_ast, NULL, NULL, file_lock };
1281 struct lustre_handle lockh = {0};
1282 ldlm_policy_data_t flock;
1286 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu file_lock=%p\n",
1287 (unsigned long long)st->st_ino, file_lock);
1289 flock.l_flock.pid = file_lock->fl_pid;
1290 flock.l_flock.start = file_lock->fl_start;
1291 flock.l_flock.end = file_lock->fl_end;
1293 switch (file_lock->fl_type) {
1295 einfo.ei_mode = LCK_PR;
1298 einfo.ei_mode = LCK_NL;
1301 einfo.ei_mode = LCK_PW;
1304 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1311 #if F_SETLKW64 != F_SETLKW
1319 #if F_SETLK64 != F_SETLK
1323 flags = LDLM_FL_BLOCK_NOWAIT;
1327 #if F_GETLK64 != F_GETLK
1331 flags = LDLM_FL_TEST_LOCK;
1332 file_lock->fl_type = einfo.ei_mode;
1335 CERROR("unknown fcntl cmd: %d\n", cmd);
1339 CDEBUG(D_DLMTRACE, "inode=%llu, pid=%u, cmd=%d, flags=%#x, mode=%u, "
1340 "start="LPX64", end="LPX64"\n", (unsigned long long)st->st_ino,
1341 flock.l_flock.pid, cmd, flags, einfo.ei_mode, flock.l_flock.start,
1345 struct lmv_obd *lmv;
1346 struct obd_device *lmv_obd;
1347 lmv_obd = class_exp2obd(llu_i2mdexp(ino));
1348 lmv = &lmv_obd->u.lmv;
1350 if (lmv->desc.ld_tgt_count < 1)
1351 RETURN(rc = -ENODEV);
1353 if (lmv->tgts[0].ltd_exp != NULL)
1354 rc = ldlm_cli_enqueue(lmv->tgts[0].ltd_exp, NULL, &einfo, &res_id,
1355 &flock, &flags, NULL, 0, &lockh, 0);
1362 static int assign_type(struct file_lock *fl, int type)
1375 static int flock_to_posix_lock(struct inode *ino,
1376 struct file_lock *fl,
1379 switch (l->l_whence) {
1380 /* XXX: only SEEK_SET is supported in lustre */
1388 fl->fl_end = l->l_len - 1;
1392 fl->fl_end = OFFSET_MAX;
1394 fl->fl_pid = getpid();
1395 fl->fl_flags = FL_POSIX;
1396 fl->fl_notify = NULL;
1397 fl->fl_insert = NULL;
1398 fl->fl_remove = NULL;
1399 /* XXX: these fields can't be filled with suitable values,
1400 but I think lustre doesn't use them.
1402 fl->fl_owner = NULL;
1405 return assign_type(fl, l->l_type);
1408 static int llu_fcntl_getlk(struct inode *ino, struct flock *flock)
1410 struct file_lock fl;
1414 if ((flock->l_type != F_RDLCK) && (flock->l_type != F_WRLCK))
1417 error = flock_to_posix_lock(ino, &fl, flock);
1421 error = llu_file_flock(ino, F_GETLK, &fl);
1425 flock->l_type = F_UNLCK;
1426 if (fl.fl_type != F_UNLCK) {
1427 flock->l_pid = fl.fl_pid;
1428 flock->l_start = fl.fl_start;
1429 flock->l_len = fl.fl_end == OFFSET_MAX ? 0:
1430 fl.fl_end - fl.fl_start + 1;
1431 flock->l_whence = SEEK_SET;
1432 flock->l_type = fl.fl_type;
1439 static int llu_fcntl_setlk(struct inode *ino, int cmd, struct flock *flock)
1441 struct file_lock fl;
1442 int flags = llu_i2info(ino)->lli_open_flags + 1;
1445 error = flock_to_posix_lock(ino, &fl, flock);
1448 if (cmd == F_SETLKW)
1449 fl.fl_flags |= FL_SLEEP;
1452 switch (flock->l_type) {
1454 if (!(flags & FMODE_READ))
1458 if (!(flags & FMODE_WRITE))
1468 error = llu_file_flock(ino, cmd, &fl);
1476 static int llu_iop_fcntl(struct inode *ino, int cmd, va_list ap, int *rtn)
1478 struct llu_inode_info *lli = llu_i2info(ino);
1480 struct flock *flock;
1483 liblustre_wait_event(0);
1486 *rtn = lli->lli_open_flags;
1489 flags = va_arg(ap, long);
1490 flags &= FCNTL_FLMASK;
1491 if (flags & FCNTL_FLMASK_INVALID) {
1492 LCONSOLE_ERROR_MSG(0x010, "liblustre does not support "
1493 "the O_NONBLOCK or O_ASYNC flags. "
1494 "Please fix your application.\n");
1499 lli->lli_open_flags = (int)(flags & FCNTL_FLMASK) |
1500 (lli->lli_open_flags & ~FCNTL_FLMASK);
1505 #if F_GETLK64 != F_GETLK
1509 flock = va_arg(ap, struct flock *);
1510 err = llu_fcntl_getlk(ino, flock);
1515 #if F_SETLKW64 != F_SETLKW
1521 #if F_SETLK64 != F_SETLK
1525 flock = va_arg(ap, struct flock *);
1526 err = llu_fcntl_setlk(ino, cmd, flock);
1530 CERROR("unsupported fcntl cmd %x\n", cmd);
1536 liblustre_wait_event(0);
1540 static int llu_get_grouplock(struct inode *inode, unsigned long arg)
1542 struct llu_inode_info *lli = llu_i2info(inode);
1543 struct ll_file_data *fd = lli->lli_file_data;
1545 struct ccc_grouplock grouplock;
1548 if (fd->fd_flags & LL_FILE_IGNORE_LOCK) {
1551 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1554 LASSERT(fd->fd_grouplock.cg_lock == NULL);
1556 rc = cl_get_grouplock(cl_i2info(inode)->lli_clob,
1557 arg, (lli->lli_open_flags & O_NONBLOCK),
1563 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
1564 fd->fd_grouplock = grouplock;
1569 int llu_put_grouplock(struct inode *inode, unsigned long arg)
1571 struct llu_inode_info *lli = llu_i2info(inode);
1572 struct ll_file_data *fd = lli->lli_file_data;
1573 struct ccc_grouplock grouplock;
1576 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED))
1579 LASSERT(fd->fd_grouplock.cg_lock != NULL);
1581 if (fd->fd_grouplock.cg_gid != arg)
1584 grouplock = fd->fd_grouplock;
1585 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
1586 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
1588 cl_put_grouplock(&grouplock);
1593 static int llu_lov_dir_setstripe(struct inode *ino, unsigned long arg)
1595 struct llu_sb_info *sbi = llu_i2sbi(ino);
1596 struct ptlrpc_request *request = NULL;
1597 struct md_op_data op_data = {{ 0 }};
1598 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1601 llu_prep_md_op_data(&op_data, ino, NULL, NULL, 0, 0,
1604 LASSERT(sizeof(lum) == sizeof(*lump));
1605 LASSERT(sizeof(lum.lmm_objects[0]) ==
1606 sizeof(lump->lmm_objects[0]));
1607 if (cfs_copy_from_user(&lum, lump, sizeof(lum)))
1610 switch (lum.lmm_magic) {
1611 case LOV_USER_MAGIC_V1: {
1612 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
1613 lustre_swab_lov_user_md_v1(&lum);
1616 case LOV_USER_MAGIC_V3: {
1617 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
1618 lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)&lum);
1622 CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
1623 " %#08x != %#08x nor %#08x\n",
1624 lum.lmm_magic, LOV_USER_MAGIC_V1,
1630 /* swabbing is done in lov_setstripe() on server side */
1631 rc = md_setattr(sbi->ll_md_exp, &op_data, &lum,
1632 sizeof(lum), NULL, 0, &request, NULL);
1634 ptlrpc_req_finished(request);
1635 if (rc != -EPERM && rc != -EACCES)
1636 CERROR("md_setattr fails: rc = %d\n", rc);
1639 ptlrpc_req_finished(request);
1644 static int llu_lov_setstripe_ea_info(struct inode *ino, int flags,
1645 struct lov_user_md *lum, int lum_size)
1647 struct llu_sb_info *sbi = llu_i2sbi(ino);
1648 struct llu_inode_info *lli = llu_i2info(ino);
1649 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1650 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
1651 llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
1652 struct ptlrpc_request *req = NULL;
1653 struct lustre_md md;
1654 struct md_op_data data = {{ 0 }};
1655 struct lustre_handle lockh;
1660 CDEBUG(D_IOCTL, "stripe already exists for ino "DFID"\n",
1661 PFID(&lli->lli_fid));
1665 llu_prep_md_op_data(&data, NULL, ino, NULL, 0, O_RDWR,
1667 rc = md_enqueue(sbi->ll_md_exp, &einfo, &oit, &data,
1668 &lockh, lum, lum_size, NULL, LDLM_FL_INTENT_ONLY);
1672 req = oit.d.lustre.it_data;
1673 rc = it_open_error(DISP_IT_EXECD, &oit);
1679 rc = it_open_error(DISP_OPEN_OPEN, &oit);
1685 rc = md_get_lustre_md(sbi->ll_md_exp, req,
1686 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1690 llu_update_inode(ino, &md);
1691 llu_local_open(lli, &oit);
1692 /* release intent */
1693 if (lustre_handle_is_used(&lockh))
1694 ldlm_lock_decref(&lockh, LCK_CR);
1695 ptlrpc_req_finished(req);
1697 rc = llu_file_release(ino);
1702 ptlrpc_req_finished(req);
1706 static int llu_lov_file_setstripe(struct inode *ino, unsigned long arg)
1708 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1710 int flags = FMODE_WRITE;
1713 LASSERT(sizeof(lum) == sizeof(*lump));
1714 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1715 if (cfs_copy_from_user(&lum, lump, sizeof(lum)))
1718 rc = llu_lov_setstripe_ea_info(ino, flags, &lum, sizeof(lum));
1722 static int llu_lov_setstripe(struct inode *ino, unsigned long arg)
1724 struct intnl_stat *st = llu_i2stat(ino);
1725 if (S_ISREG(st->st_mode))
1726 return llu_lov_file_setstripe(ino, arg);
1727 if (S_ISDIR(st->st_mode))
1728 return llu_lov_dir_setstripe(ino, arg);
1733 static int llu_lov_getstripe(struct inode *ino, unsigned long arg)
1735 struct lov_stripe_md *lsm = llu_i2info(ino)->lli_smd;
1740 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, llu_i2obdexp(ino), 0, lsm,
1744 static int llu_iop_ioctl(struct inode *ino, unsigned long int request,
1750 liblustre_wait_event(0);
1753 case LL_IOC_GROUP_LOCK:
1754 arg = va_arg(ap, unsigned long);
1755 rc = llu_get_grouplock(ino, arg);
1757 case LL_IOC_GROUP_UNLOCK:
1758 arg = va_arg(ap, unsigned long);
1759 rc = llu_put_grouplock(ino, arg);
1761 case LL_IOC_LOV_SETSTRIPE:
1762 arg = va_arg(ap, unsigned long);
1763 rc = llu_lov_setstripe(ino, arg);
1765 case LL_IOC_LOV_GETSTRIPE:
1766 arg = va_arg(ap, unsigned long);
1767 rc = llu_lov_getstripe(ino, arg);
1770 CERROR("did not support ioctl cmd %lx\n", request);
1775 liblustre_wait_event(0);
1780 * we already do syncronous read/write
1782 static int llu_iop_sync(struct inode *inode)
1784 liblustre_wait_event(0);
1788 static int llu_iop_datasync(struct inode *inode)
1790 liblustre_wait_event(0);
1794 struct filesys_ops llu_filesys_ops =
1796 fsop_gone: llu_fsop_gone,
1799 struct inode *llu_iget(struct filesys *fs, struct lustre_md *md)
1801 struct inode *inode;
1803 struct file_identifier fileid = {&fid, sizeof(fid)};
1805 if ((md->body->valid & (OBD_MD_FLID | OBD_MD_FLTYPE)) !=
1806 (OBD_MD_FLID | OBD_MD_FLTYPE)) {
1807 CERROR("bad md body valid mask "LPX64"\n", md->body->valid);
1809 return ERR_PTR(-EPERM);
1812 /* try to find existing inode */
1813 fid = md->body->fid1;
1815 inode = _sysio_i_find(fs, &fileid);
1817 if (inode->i_zombie/* ||
1818 lli->lli_st_generation != md->body->generation*/) {
1822 llu_update_inode(inode, md);
1827 inode = llu_new_inode(fs, &fid);
1829 llu_update_inode(inode, md);
1835 llu_fsswop_mount(const char *source,
1837 const void *data __IS_UNUSED,
1838 struct pnode *tocover,
1839 struct mount **mntp)
1843 struct pnode_base *rootpb;
1844 struct obd_device *obd;
1845 struct llu_sb_info *sbi;
1846 struct obd_statfs osfs;
1847 static struct qstr noname = { NULL, 0, 0 };
1848 struct ptlrpc_request *request = NULL;
1849 struct lustre_md md;
1851 struct config_llog_instance cfg = {0, };
1852 char ll_instance[sizeof(sbi) * 2 + 3];
1853 struct lustre_profile *lprof;
1854 char *zconf_mgsnid, *zconf_profile;
1855 char *osc = NULL, *mdc = NULL;
1856 int async = 1, err = -EINVAL;
1857 struct obd_connect_data ocd = {0,};
1858 struct md_op_data op_data = {{0}};
1862 if (ll_parse_mount_target(source,
1865 CERROR("mal-formed target %s\n", source);
1868 if (!zconf_mgsnid || !zconf_profile) {
1869 printf("Liblustre: invalid target %s\n", source);
1872 /* allocate & initialize sbi */
1873 OBD_ALLOC(sbi, sizeof(*sbi));
1877 CFS_INIT_LIST_HEAD(&sbi->ll_conn_chain);
1878 ll_generate_random_uuid(uuid);
1879 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
1881 /* generate a string unique to this super, let's try
1882 the address of the super itself.*/
1883 snprintf(ll_instance, sizeof(ll_instance), "%p", sbi);
1885 /* retrive & parse config log */
1886 cfg.cfg_instance = ll_instance;
1887 cfg.cfg_uuid = sbi->ll_sb_uuid;
1888 err = liblustre_process_log(&cfg, zconf_mgsnid, zconf_profile, 1);
1890 CERROR("Unable to process log: %s\n", zconf_profile);
1891 GOTO(out_free, err);
1894 lprof = class_get_profile(zconf_profile);
1895 if (lprof == NULL) {
1896 CERROR("No profile found: %s\n", zconf_profile);
1897 GOTO(out_free, err = -EINVAL);
1899 OBD_ALLOC(osc, strlen(lprof->lp_dt) + strlen(ll_instance) + 2);
1900 sprintf(osc, "%s-%s", lprof->lp_dt, ll_instance);
1902 OBD_ALLOC(mdc, strlen(lprof->lp_md) + strlen(ll_instance) + 2);
1903 sprintf(mdc, "%s-%s", lprof->lp_md, ll_instance);
1907 GOTO(out_free, err = -EINVAL);
1911 GOTO(out_free, err = -EINVAL);
1914 fs = _sysio_fs_new(&llu_filesys_ops, flags, sbi);
1920 obd = class_name2obd(mdc);
1922 CERROR("MDC %s: not setup or attached\n", mdc);
1923 GOTO(out_free, err = -EINVAL);
1925 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
1926 sizeof(async), &async, NULL);
1928 ocd.ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_VERSION |
1929 OBD_CONNECT_FID | OBD_CONNECT_AT |
1930 OBD_CONNECT_VBR | OBD_CONNECT_FULL20;
1932 #ifdef LIBLUSTRE_POSIX_ACL
1933 ocd.ocd_connect_flags |= OBD_CONNECT_ACL;
1935 ocd.ocd_ibits_known = MDS_INODELOCK_FULL;
1936 ocd.ocd_version = LUSTRE_VERSION_CODE;
1939 err = obd_connect(NULL, &sbi->ll_md_exp, obd, &sbi->ll_sb_uuid, &ocd, NULL);
1941 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
1942 GOTO(out_free, err);
1945 err = obd_statfs(obd, &osfs, 100000000, 0);
1950 * FIXME fill fs stat data into sbi here!!! FIXME
1954 obd = class_name2obd(osc);
1956 CERROR("OSC %s: not setup or attached\n", osc);
1957 GOTO(out_md, err = -EINVAL);
1959 obd_set_info_async(obd->obd_self_export, sizeof(KEY_ASYNC), KEY_ASYNC,
1960 sizeof(async), &async, NULL);
1962 obd->obd_upcall.onu_owner = &sbi->ll_lco;
1963 obd->obd_upcall.onu_upcall = cl_ocd_update;
1965 ocd.ocd_connect_flags = OBD_CONNECT_SRVLOCK | OBD_CONNECT_REQPORTAL |
1966 OBD_CONNECT_VERSION | OBD_CONNECT_TRUNCLOCK |
1967 OBD_CONNECT_FID | OBD_CONNECT_AT |
1970 ocd.ocd_version = LUSTRE_VERSION_CODE;
1971 err = obd_connect(NULL, &sbi->ll_dt_exp, obd, &sbi->ll_sb_uuid, &ocd, NULL);
1973 CERROR("cannot connect to %s: rc = %d\n", osc, err);
1976 sbi->ll_lco.lco_flags = ocd.ocd_connect_flags;
1977 sbi->ll_lco.lco_md_exp = sbi->ll_md_exp;
1978 sbi->ll_lco.lco_dt_exp = sbi->ll_dt_exp;
1980 fid_zero(&sbi->ll_root_fid);
1981 err = md_getstatus(sbi->ll_md_exp, &sbi->ll_root_fid, NULL);
1983 CERROR("cannot mds_connect: rc = %d\n", err);
1984 GOTO(out_lock_cn_cb, err);
1986 if (!fid_is_sane(&sbi->ll_root_fid)) {
1987 CERROR("Invalid root fid during mount\n");
1988 GOTO(out_lock_cn_cb, err = -EINVAL);
1990 CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&sbi->ll_root_fid));
1992 op_data.op_fid1 = sbi->ll_root_fid;
1993 op_data.op_valid = OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS;
1994 /* fetch attr of root inode */
1995 err = md_getattr(sbi->ll_md_exp, &op_data, &request);
1997 CERROR("md_getattr failed for root: rc = %d\n", err);
1998 GOTO(out_lock_cn_cb, err);
2001 err = md_get_lustre_md(sbi->ll_md_exp, request,
2002 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2004 CERROR("failed to understand root inode md: rc = %d\n",err);
2005 GOTO(out_request, err);
2008 LASSERT(fid_is_sane(&sbi->ll_root_fid));
2010 root = llu_iget(fs, &md);
2011 if (!root || IS_ERR(root)) {
2012 CERROR("fail to generate root inode\n");
2013 GOTO(out_request, err = -EBADF);
2017 * Generate base path-node for root.
2019 rootpb = _sysio_pb_new(&noname, NULL, root);
2025 err = _sysio_do_mount(fs, rootpb, flags, tocover, mntp);
2027 _sysio_pb_gone(rootpb);
2033 ptlrpc_req_finished(request);
2035 CDEBUG(D_SUPER, "LibLustre: %s mounted successfully!\n", source);
2036 liblustre_wait_idle();
2041 _sysio_i_gone(root);
2043 ptlrpc_req_finished(request);
2045 obd_disconnect(sbi->ll_dt_exp);
2047 obd_disconnect(sbi->ll_md_exp);
2050 OBD_FREE(osc, strlen(osc) + 1);
2052 OBD_FREE(mdc, strlen(mdc) + 1);
2053 OBD_FREE(sbi, sizeof(*sbi));
2054 liblustre_wait_idle();
2058 struct fssw_ops llu_fssw_ops = {
2062 static struct inode_ops llu_inode_ops = {
2063 inop_lookup: llu_iop_lookup,
2064 inop_getattr: llu_iop_getattr,
2065 inop_setattr: llu_iop_setattr,
2066 inop_filldirentries: llu_iop_filldirentries,
2067 inop_mkdir: llu_iop_mkdir_raw,
2068 inop_rmdir: llu_iop_rmdir_raw,
2069 inop_symlink: llu_iop_symlink_raw,
2070 inop_readlink: llu_iop_readlink,
2071 inop_open: llu_iop_open,
2072 inop_close: llu_iop_close,
2073 inop_link: llu_iop_link_raw,
2074 inop_unlink: llu_iop_unlink_raw,
2075 inop_rename: llu_iop_rename_raw,
2076 inop_pos: llu_iop_pos,
2077 inop_read: llu_iop_read,
2078 inop_write: llu_iop_write,
2079 inop_iodone: llu_iop_iodone,
2080 inop_fcntl: llu_iop_fcntl,
2081 inop_sync: llu_iop_sync,
2082 inop_datasync: llu_iop_datasync,
2083 inop_ioctl: llu_iop_ioctl,
2084 inop_mknod: llu_iop_mknod_raw,
2085 #ifdef _HAVE_STATVFS
2086 inop_statvfs: llu_iop_statvfs,
2088 inop_gone: llu_iop_gone,