4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_LLITE
40 #include <lustre_dlm.h>
41 #include <linux/pagemap.h>
42 #include <linux/file.h>
43 #include <linux/sched.h>
44 #include <linux/user_namespace.h>
45 #include <linux/uidgid.h>
46 #include <linux/falloc.h>
48 #include <uapi/linux/lustre/lustre_ioctl.h>
49 #include <lustre_swab.h>
51 #include "cl_object.h"
52 #include "llite_internal.h"
53 #include "vvp_internal.h"
56 struct inode *sp_inode;
61 __u64 pa_data_version;
67 ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
69 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
72 static struct ll_file_data *ll_file_data_get(void)
74 struct ll_file_data *fd;
76 OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, GFP_NOFS);
80 fd->fd_write_failed = false;
81 pcc_file_init(&fd->fd_pcc_file);
86 static void ll_file_data_put(struct ll_file_data *fd)
89 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
93 * Packs all the attributes into @op_data for the CLOSE rpc.
95 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
96 struct obd_client_handle *och)
100 ll_prep_md_op_data(op_data, inode, NULL, NULL,
101 0, 0, LUSTRE_OPC_ANY, NULL);
103 op_data->op_attr.ia_mode = inode->i_mode;
104 op_data->op_attr.ia_atime = inode->i_atime;
105 op_data->op_attr.ia_mtime = inode->i_mtime;
106 op_data->op_attr.ia_ctime = inode->i_ctime;
107 op_data->op_attr.ia_size = i_size_read(inode);
108 op_data->op_attr.ia_valid |= (ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
109 ATTR_MTIME | ATTR_MTIME_SET |
111 op_data->op_xvalid |= OP_XVALID_CTIME_SET;
112 op_data->op_attr_blocks = inode->i_blocks;
113 op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
114 if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT))
115 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
116 op_data->op_open_handle = och->och_open_handle;
118 if (och->och_flags & FMODE_WRITE &&
119 ll_file_test_and_clear_flag(ll_i2info(inode), LLIF_DATA_MODIFIED))
120 /* For HSM: if inode data has been modified, pack it so that
121 * MDT can set data dirty flag in the archive. */
122 op_data->op_bias |= MDS_DATA_MODIFIED;
128 * Perform a close, possibly with a bias.
129 * The meaning of "data" depends on the value of "bias".
131 * If \a bias is MDS_HSM_RELEASE then \a data is a pointer to the data version.
132 * If \a bias is MDS_CLOSE_LAYOUT_SWAP then \a data is a pointer to the inode to
135 static int ll_close_inode_openhandle(struct inode *inode,
136 struct obd_client_handle *och,
137 enum mds_op_bias bias, void *data)
139 struct obd_export *md_exp = ll_i2mdexp(inode);
140 const struct ll_inode_info *lli = ll_i2info(inode);
141 struct md_op_data *op_data;
142 struct ptlrpc_request *req = NULL;
146 if (class_exp2obd(md_exp) == NULL) {
147 CERROR("%s: invalid MDC connection handle closing "DFID"\n",
148 ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
152 OBD_ALLOC_PTR(op_data);
153 /* We leak openhandle and request here on error, but not much to be
154 * done in OOM case since app won't retry close on error either. */
156 GOTO(out, rc = -ENOMEM);
158 ll_prepare_close(inode, op_data, och);
160 case MDS_CLOSE_LAYOUT_MERGE:
161 /* merge blocks from the victim inode */
162 op_data->op_attr_blocks += ((struct inode *)data)->i_blocks;
163 op_data->op_attr.ia_valid |= ATTR_SIZE;
164 op_data->op_xvalid |= OP_XVALID_BLOCKS;
166 case MDS_CLOSE_LAYOUT_SPLIT:
167 case MDS_CLOSE_LAYOUT_SWAP: {
168 struct split_param *sp = data;
170 LASSERT(data != NULL);
171 op_data->op_bias |= bias;
172 op_data->op_data_version = 0;
173 op_data->op_lease_handle = och->och_lease_handle;
174 if (bias == MDS_CLOSE_LAYOUT_SPLIT) {
175 op_data->op_fid2 = *ll_inode2fid(sp->sp_inode);
176 op_data->op_mirror_id = sp->sp_mirror_id;
178 op_data->op_fid2 = *ll_inode2fid(data);
183 case MDS_CLOSE_RESYNC_DONE: {
184 struct ll_ioc_lease *ioc = data;
186 LASSERT(data != NULL);
187 op_data->op_attr_blocks +=
188 ioc->lil_count * op_data->op_attr_blocks;
189 op_data->op_attr.ia_valid |= ATTR_SIZE;
190 op_data->op_xvalid |= OP_XVALID_BLOCKS;
191 op_data->op_bias |= MDS_CLOSE_RESYNC_DONE;
193 op_data->op_lease_handle = och->och_lease_handle;
194 op_data->op_data = &ioc->lil_ids[0];
195 op_data->op_data_size =
196 ioc->lil_count * sizeof(ioc->lil_ids[0]);
200 case MDS_PCC_ATTACH: {
201 struct pcc_param *param = data;
203 LASSERT(data != NULL);
204 op_data->op_bias |= MDS_HSM_RELEASE | MDS_PCC_ATTACH;
205 op_data->op_archive_id = param->pa_archive_id;
206 op_data->op_data_version = param->pa_data_version;
207 op_data->op_lease_handle = och->och_lease_handle;
211 case MDS_HSM_RELEASE:
212 LASSERT(data != NULL);
213 op_data->op_bias |= MDS_HSM_RELEASE;
214 op_data->op_data_version = *(__u64 *)data;
215 op_data->op_lease_handle = och->och_lease_handle;
216 op_data->op_attr.ia_valid |= ATTR_SIZE;
217 op_data->op_xvalid |= OP_XVALID_BLOCKS;
221 LASSERT(data == NULL);
225 if (!(op_data->op_attr.ia_valid & ATTR_SIZE))
226 op_data->op_xvalid |= OP_XVALID_LAZYSIZE;
227 if (!(op_data->op_xvalid & OP_XVALID_BLOCKS))
228 op_data->op_xvalid |= OP_XVALID_LAZYBLOCKS;
230 rc = md_close(md_exp, op_data, och->och_mod, &req);
231 if (rc != 0 && rc != -EINTR)
232 CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
233 md_exp->exp_obd->obd_name, PFID(&lli->lli_fid), rc);
235 if (rc == 0 && op_data->op_bias & bias) {
236 struct mdt_body *body;
238 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
239 if (!(body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED))
242 if (bias & MDS_PCC_ATTACH) {
243 struct pcc_param *param = data;
245 param->pa_layout_gen = body->mbo_layout_gen;
249 ll_finish_md_op_data(op_data);
253 md_clear_open_replay_data(md_exp, och);
254 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
257 ptlrpc_req_finished(req); /* This is close request */
261 int ll_md_real_close(struct inode *inode, fmode_t fmode)
263 struct ll_inode_info *lli = ll_i2info(inode);
264 struct obd_client_handle **och_p;
265 struct obd_client_handle *och;
270 if (fmode & FMODE_WRITE) {
271 och_p = &lli->lli_mds_write_och;
272 och_usecount = &lli->lli_open_fd_write_count;
273 } else if (fmode & FMODE_EXEC) {
274 och_p = &lli->lli_mds_exec_och;
275 och_usecount = &lli->lli_open_fd_exec_count;
277 LASSERT(fmode & FMODE_READ);
278 och_p = &lli->lli_mds_read_och;
279 och_usecount = &lli->lli_open_fd_read_count;
282 mutex_lock(&lli->lli_och_mutex);
283 if (*och_usecount > 0) {
284 /* There are still users of this handle, so skip
286 mutex_unlock(&lli->lli_och_mutex);
292 mutex_unlock(&lli->lli_och_mutex);
295 /* There might be a race and this handle may already
297 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
303 static int ll_md_close(struct inode *inode, struct file *file)
305 union ldlm_policy_data policy = {
306 .l_inodebits = { MDS_INODELOCK_OPEN },
308 __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
309 struct ll_file_data *fd = file->private_data;
310 struct ll_inode_info *lli = ll_i2info(inode);
311 struct lustre_handle lockh;
312 enum ldlm_mode lockmode;
316 /* clear group lock, if present */
317 if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
318 ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid);
320 if (fd->fd_lease_och != NULL) {
323 /* Usually the lease is not released when the
324 * application crashed, we need to release here. */
325 rc = ll_lease_close(fd->fd_lease_och, inode, &lease_broken);
326 CDEBUG(rc ? D_ERROR : D_INODE, "Clean up lease "DFID" %d/%d\n",
327 PFID(&lli->lli_fid), rc, lease_broken);
329 fd->fd_lease_och = NULL;
332 if (fd->fd_och != NULL) {
333 rc = ll_close_inode_openhandle(inode, fd->fd_och, 0, NULL);
338 /* Let's see if we have good enough OPEN lock on the file and if
339 we can skip talking to MDS */
340 mutex_lock(&lli->lli_och_mutex);
341 if (fd->fd_omode & FMODE_WRITE) {
343 LASSERT(lli->lli_open_fd_write_count);
344 lli->lli_open_fd_write_count--;
345 } else if (fd->fd_omode & FMODE_EXEC) {
347 LASSERT(lli->lli_open_fd_exec_count);
348 lli->lli_open_fd_exec_count--;
351 LASSERT(lli->lli_open_fd_read_count);
352 lli->lli_open_fd_read_count--;
354 mutex_unlock(&lli->lli_och_mutex);
356 /* LU-4398: do not cache write open lock if the file has exec bit */
357 if ((lockmode == LCK_CW && inode->i_mode & S_IXUGO) ||
358 !md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
359 LDLM_IBITS, &policy, lockmode, &lockh))
360 rc = ll_md_real_close(inode, fd->fd_omode);
363 file->private_data = NULL;
364 ll_file_data_put(fd);
369 /* While this returns an error code, fput() the caller does not, so we need
370 * to make every effort to clean up all of our state here. Also, applications
371 * rarely check close errors and even if an error is returned they will not
372 * re-try the close call.
374 int ll_file_release(struct inode *inode, struct file *file)
376 struct ll_file_data *fd;
377 struct ll_sb_info *sbi = ll_i2sbi(inode);
378 struct ll_inode_info *lli = ll_i2info(inode);
379 ktime_t kstart = ktime_get();
384 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
385 PFID(ll_inode2fid(inode)), inode);
387 fd = file->private_data;
390 /* The last ref on @file, maybe not the the owner pid of statahead,
391 * because parent and child process can share the same file handle. */
392 if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd)
393 ll_deauthorize_statahead(inode, fd);
395 if (inode->i_sb->s_root == file_dentry(file)) {
396 file->private_data = NULL;
397 ll_file_data_put(fd);
401 pcc_file_release(inode, file);
403 if (!S_ISDIR(inode->i_mode)) {
404 if (lli->lli_clob != NULL)
405 lov_read_and_clear_async_rc(lli->lli_clob);
406 lli->lli_async_rc = 0;
409 rc = ll_md_close(inode, file);
411 if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
412 libcfs_debug_dumplog();
415 if (!rc && inode->i_sb->s_root != file_dentry(file))
416 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE,
417 ktime_us_delta(ktime_get(), kstart));
421 static inline int ll_dom_readpage(void *data, struct page *page)
423 struct niobuf_local *lnb = data;
426 kaddr = kmap_atomic(page);
427 memcpy(kaddr, lnb->lnb_data, lnb->lnb_len);
428 if (lnb->lnb_len < PAGE_SIZE)
429 memset(kaddr + lnb->lnb_len, 0,
430 PAGE_SIZE - lnb->lnb_len);
431 flush_dcache_page(page);
432 SetPageUptodate(page);
433 kunmap_atomic(kaddr);
439 void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req,
440 struct lookup_intent *it)
442 struct ll_inode_info *lli = ll_i2info(inode);
443 struct cl_object *obj = lli->lli_clob;
444 struct address_space *mapping = inode->i_mapping;
446 struct niobuf_remote *rnb;
447 struct mdt_body *body;
449 unsigned long index, start;
450 struct niobuf_local lnb;
457 if (!req_capsule_field_present(&req->rq_pill, &RMF_NIOBUF_INLINE,
461 rnb = req_capsule_server_get(&req->rq_pill, &RMF_NIOBUF_INLINE);
462 if (rnb == NULL || rnb->rnb_len == 0)
465 /* LU-11595: Server may return whole file and that is OK always or
466 * it may return just file tail and its offset must be aligned with
467 * client PAGE_SIZE to be used on that client, if server's PAGE_SIZE is
468 * smaller then offset may be not aligned and that data is just ignored.
470 if (rnb->rnb_offset & ~PAGE_MASK)
473 /* Server returns whole file or just file tail if it fills in reply
474 * buffer, in both cases total size should be equal to the file size.
476 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
477 if (rnb->rnb_offset + rnb->rnb_len != body->mbo_dom_size) {
478 CERROR("%s: server returns off/len %llu/%u but size %llu\n",
479 ll_i2sbi(inode)->ll_fsname, rnb->rnb_offset,
480 rnb->rnb_len, body->mbo_dom_size);
484 CDEBUG(D_INFO, "Get data along with open at %llu len %i, size %llu\n",
485 rnb->rnb_offset, rnb->rnb_len, body->mbo_dom_size);
487 data = (char *)rnb + sizeof(*rnb);
489 lnb.lnb_file_offset = rnb->rnb_offset;
490 start = lnb.lnb_file_offset >> PAGE_SHIFT;
492 LASSERT((lnb.lnb_file_offset & ~PAGE_MASK) == 0);
493 lnb.lnb_page_offset = 0;
495 lnb.lnb_data = data + (index << PAGE_SHIFT);
496 lnb.lnb_len = rnb->rnb_len - (index << PAGE_SHIFT);
497 if (lnb.lnb_len > PAGE_SIZE)
498 lnb.lnb_len = PAGE_SIZE;
500 vmpage = read_cache_page(mapping, index + start,
501 ll_dom_readpage, &lnb);
502 if (IS_ERR(vmpage)) {
503 CWARN("%s: cannot fill page %lu for "DFID
504 " with data: rc = %li\n",
505 ll_i2sbi(inode)->ll_fsname, index + start,
506 PFID(lu_object_fid(&obj->co_lu)),
512 } while (rnb->rnb_len > (index << PAGE_SHIFT));
516 static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
517 struct lookup_intent *itp)
519 struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
520 struct dentry *parent = de->d_parent;
523 struct md_op_data *op_data;
524 struct ptlrpc_request *req = NULL;
528 LASSERT(parent != NULL);
529 LASSERT(itp->it_flags & MDS_OPEN_BY_FID);
531 /* if server supports open-by-fid, or file name is invalid, don't pack
532 * name in open request */
533 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_OPEN_BY_NAME) ||
534 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID)) {
536 len = de->d_name.len;
537 name = kmalloc(len + 1, GFP_NOFS);
542 spin_lock(&de->d_lock);
543 if (len != de->d_name.len) {
544 spin_unlock(&de->d_lock);
548 memcpy(name, de->d_name.name, len);
550 spin_unlock(&de->d_lock);
552 if (!lu_name_is_valid_2(name, len)) {
558 op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
559 name, len, 0, LUSTRE_OPC_ANY, NULL);
560 if (IS_ERR(op_data)) {
562 RETURN(PTR_ERR(op_data));
564 op_data->op_data = lmm;
565 op_data->op_data_size = lmmsize;
567 rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
568 &ll_md_blocking_ast, 0);
570 ll_finish_md_op_data(op_data);
572 /* reason for keep own exit path - don`t flood log
573 * with messages with -ESTALE errors.
575 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
576 it_open_error(DISP_OPEN_OPEN, itp))
578 ll_release_openhandle(de, itp);
582 if (it_disposition(itp, DISP_LOOKUP_NEG))
583 GOTO(out, rc = -ENOENT);
585 if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
586 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
587 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
591 rc = ll_prep_inode(&de->d_inode, req, NULL, itp);
593 if (!rc && itp->it_lock_mode) {
594 struct lustre_handle handle = {.cookie = itp->it_lock_handle};
595 struct ldlm_lock *lock;
596 bool has_dom_bit = false;
598 /* If we got a lock back and it has a LOOKUP bit set,
599 * make sure the dentry is marked as valid so we can find it.
600 * We don't need to care about actual hashing since other bits
601 * of kernel will deal with that later.
603 lock = ldlm_handle2lock(&handle);
605 has_dom_bit = ldlm_has_dom(lock);
606 if (lock->l_policy_data.l_inodebits.bits &
607 MDS_INODELOCK_LOOKUP)
608 d_lustre_revalidate(de);
612 ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, NULL);
614 ll_dom_finish_open(de->d_inode, req, itp);
618 ptlrpc_req_finished(req);
619 ll_intent_drop_lock(itp);
621 /* We did open by fid, but by the time we got to the server,
622 * the object disappeared. If this is a create, we cannot really
623 * tell the userspace that the file it was trying to create
624 * does not exist. Instead let's return -ESTALE, and the VFS will
625 * retry the create with LOOKUP_REVAL that we are going to catch
626 * in ll_revalidate_dentry() and use lookup then.
628 if (rc == -ENOENT && itp->it_op & IT_CREAT)
634 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
635 struct obd_client_handle *och)
637 struct mdt_body *body;
639 body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
640 och->och_open_handle = body->mbo_open_handle;
641 och->och_fid = body->mbo_fid1;
642 och->och_lease_handle.cookie = it->it_lock_handle;
643 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
644 och->och_flags = it->it_flags;
646 return md_set_open_replay_data(md_exp, och, it);
649 static int ll_local_open(struct file *file, struct lookup_intent *it,
650 struct ll_file_data *fd, struct obd_client_handle *och)
652 struct inode *inode = file_inode(file);
655 LASSERT(!file->private_data);
662 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
667 file->private_data = fd;
668 ll_readahead_init(inode, &fd->fd_ras);
669 fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
671 /* ll_cl_context initialize */
672 rwlock_init(&fd->fd_lock);
673 INIT_LIST_HEAD(&fd->fd_lccs);
678 /* Open a file, and (for the very first open) create objects on the OSTs at
679 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
680 * creation or open until ll_lov_setstripe() ioctl is called.
682 * If we already have the stripe MD locally then we don't request it in
683 * md_open(), by passing a lmm_size = 0.
685 * It is up to the application to ensure no other processes open this file
686 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
687 * used. We might be able to avoid races of that sort by getting lli_open_sem
688 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
689 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
691 int ll_file_open(struct inode *inode, struct file *file)
693 struct ll_inode_info *lli = ll_i2info(inode);
694 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
695 .it_flags = file->f_flags };
696 struct obd_client_handle **och_p = NULL;
697 __u64 *och_usecount = NULL;
698 struct ll_file_data *fd;
699 ktime_t kstart = ktime_get();
703 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
704 PFID(ll_inode2fid(inode)), inode, file->f_flags);
706 it = file->private_data; /* XXX: compat macro */
707 file->private_data = NULL; /* prevent ll_local_open assertion */
709 if (S_ISREG(inode->i_mode)) {
710 rc = llcrypt_file_open(inode, file);
712 GOTO(out_nofiledata, rc);
715 fd = ll_file_data_get();
717 GOTO(out_nofiledata, rc = -ENOMEM);
720 if (S_ISDIR(inode->i_mode))
721 ll_authorize_statahead(inode, fd);
723 if (inode->i_sb->s_root == file_dentry(file)) {
724 file->private_data = fd;
728 if (!it || !it->it_disposition) {
729 /* Convert f_flags into access mode. We cannot use file->f_mode,
730 * because everything but O_ACCMODE mask was stripped from
732 if ((oit.it_flags + 1) & O_ACCMODE)
734 if (file->f_flags & O_TRUNC)
735 oit.it_flags |= FMODE_WRITE;
737 /* kernel only call f_op->open in dentry_open. filp_open calls
738 * dentry_open after call to open_namei that checks permissions.
739 * Only nfsd_open call dentry_open directly without checking
740 * permissions and because of that this code below is safe.
742 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
743 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
745 /* We do not want O_EXCL here, presumably we opened the file
746 * already? XXX - NFS implications? */
747 oit.it_flags &= ~O_EXCL;
749 /* bug20584, if "it_flags" contains O_CREAT, the file will be
750 * created if necessary, then "IT_CREAT" should be set to keep
751 * consistent with it */
752 if (oit.it_flags & O_CREAT)
753 oit.it_op |= IT_CREAT;
759 /* Let's see if we have file open on MDS already. */
760 if (it->it_flags & FMODE_WRITE) {
761 och_p = &lli->lli_mds_write_och;
762 och_usecount = &lli->lli_open_fd_write_count;
763 } else if (it->it_flags & FMODE_EXEC) {
764 och_p = &lli->lli_mds_exec_och;
765 och_usecount = &lli->lli_open_fd_exec_count;
767 och_p = &lli->lli_mds_read_och;
768 och_usecount = &lli->lli_open_fd_read_count;
771 mutex_lock(&lli->lli_och_mutex);
772 if (*och_p) { /* Open handle is present */
773 if (it_disposition(it, DISP_OPEN_OPEN)) {
774 /* Well, there's extra open request that we do not need,
775 * let's close it somehow. This will decref request. */
776 rc = it_open_error(DISP_OPEN_OPEN, it);
778 mutex_unlock(&lli->lli_och_mutex);
779 GOTO(out_openerr, rc);
782 ll_release_openhandle(file_dentry(file), it);
786 rc = ll_local_open(file, it, fd, NULL);
789 mutex_unlock(&lli->lli_och_mutex);
790 GOTO(out_openerr, rc);
793 LASSERT(*och_usecount == 0);
794 if (!it->it_disposition) {
795 struct dentry *dentry = file_dentry(file);
796 struct ll_dentry_data *ldd;
798 /* We cannot just request lock handle now, new ELC code
799 * means that one of other OPEN locks for this file
800 * could be cancelled, and since blocking ast handler
801 * would attempt to grab och_mutex as well, that would
802 * result in a deadlock
804 mutex_unlock(&lli->lli_och_mutex);
806 * Normally called under two situations:
808 * 2. A race/condition on MDS resulting in no open
809 * handle to be returned from LOOKUP|OPEN request,
810 * for example if the target entry was a symlink.
812 * Only fetch MDS_OPEN_LOCK if this is in NFS path,
813 * marked by a bit set in ll_iget_for_nfs. Clear the
814 * bit so that it's not confusing later callers.
816 * NB; when ldd is NULL, it must have come via normal
817 * lookup path only, since ll_iget_for_nfs always calls
820 ldd = ll_d2d(dentry);
821 if (ldd && ldd->lld_nfs_dentry) {
822 ldd->lld_nfs_dentry = 0;
823 if (!filename_is_volatile(dentry->d_name.name,
826 it->it_flags |= MDS_OPEN_LOCK;
830 * Always specify MDS_OPEN_BY_FID because we don't want
831 * to get file with different fid.
833 it->it_flags |= MDS_OPEN_BY_FID;
834 rc = ll_intent_file_open(dentry, NULL, 0, it);
836 GOTO(out_openerr, rc);
840 OBD_ALLOC(*och_p, sizeof(struct obd_client_handle));
842 GOTO(out_och_free, rc = -ENOMEM);
846 /* md_intent_lock() didn't get a request ref if there was an
847 * open error, so don't do cleanup on the request here
849 /* XXX (green): Should not we bail out on any error here, not
850 * just open error? */
851 rc = it_open_error(DISP_OPEN_OPEN, it);
853 GOTO(out_och_free, rc);
855 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
856 "inode %p: disposition %x, status %d\n", inode,
857 it_disposition(it, ~0), it->it_status);
859 rc = ll_local_open(file, it, fd, *och_p);
861 GOTO(out_och_free, rc);
864 rc = pcc_file_open(inode, file);
866 GOTO(out_och_free, rc);
868 mutex_unlock(&lli->lli_och_mutex);
870 /* lockless for direct IO so that it can do IO in parallel */
871 if (file->f_flags & O_DIRECT)
872 fd->fd_flags |= LL_FILE_LOCKLESS_IO;
875 /* Must do this outside lli_och_mutex lock to prevent deadlock where
876 different kind of OPEN lock for this same inode gets cancelled
877 by ldlm_cancel_lru */
878 if (!S_ISREG(inode->i_mode))
879 GOTO(out_och_free, rc);
880 cl_lov_delay_create_clear(&file->f_flags);
881 GOTO(out_och_free, rc);
885 if (och_p && *och_p) {
886 OBD_FREE(*och_p, sizeof(struct obd_client_handle));
887 *och_p = NULL; /* OBD_FREE writes some magic there */
890 mutex_unlock(&lli->lli_och_mutex);
893 if (lli->lli_opendir_key == fd)
894 ll_deauthorize_statahead(inode, fd);
897 ll_file_data_put(fd);
899 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN,
900 ktime_us_delta(ktime_get(), kstart));
904 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
905 ptlrpc_req_finished(it->it_request);
906 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
912 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
913 struct ldlm_lock_desc *desc, void *data, int flag)
916 struct lustre_handle lockh;
920 case LDLM_CB_BLOCKING:
921 ldlm_lock2handle(lock, &lockh);
922 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
924 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
928 case LDLM_CB_CANCELING:
936 * When setting a lease on a file, we take ownership of the lli_mds_*_och
937 * and save it as fd->fd_och so as to force client to reopen the file even
938 * if it has an open lock in cache already.
940 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
941 struct lustre_handle *old_open_handle)
943 struct ll_inode_info *lli = ll_i2info(inode);
944 struct ll_file_data *fd = file->private_data;
945 struct obd_client_handle **och_p;
950 /* Get the openhandle of the file */
951 mutex_lock(&lli->lli_och_mutex);
952 if (fd->fd_lease_och != NULL)
953 GOTO(out_unlock, rc = -EBUSY);
955 if (fd->fd_och == NULL) {
956 if (file->f_mode & FMODE_WRITE) {
957 LASSERT(lli->lli_mds_write_och != NULL);
958 och_p = &lli->lli_mds_write_och;
959 och_usecount = &lli->lli_open_fd_write_count;
961 LASSERT(lli->lli_mds_read_och != NULL);
962 och_p = &lli->lli_mds_read_och;
963 och_usecount = &lli->lli_open_fd_read_count;
966 if (*och_usecount > 1)
967 GOTO(out_unlock, rc = -EBUSY);
974 *old_open_handle = fd->fd_och->och_open_handle;
978 mutex_unlock(&lli->lli_och_mutex);
983 * Release ownership on lli_mds_*_och when putting back a file lease.
985 static int ll_lease_och_release(struct inode *inode, struct file *file)
987 struct ll_inode_info *lli = ll_i2info(inode);
988 struct ll_file_data *fd = file->private_data;
989 struct obd_client_handle **och_p;
990 struct obd_client_handle *old_och = NULL;
995 mutex_lock(&lli->lli_och_mutex);
996 if (file->f_mode & FMODE_WRITE) {
997 och_p = &lli->lli_mds_write_och;
998 och_usecount = &lli->lli_open_fd_write_count;
1000 och_p = &lli->lli_mds_read_och;
1001 och_usecount = &lli->lli_open_fd_read_count;
1004 /* The file may have been open by another process (broken lease) so
1005 * *och_p is not NULL. In this case we should simply increase usecount
1008 if (*och_p != NULL) {
1009 old_och = fd->fd_och;
1012 *och_p = fd->fd_och;
1016 mutex_unlock(&lli->lli_och_mutex);
1018 if (old_och != NULL)
1019 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
1025 * Acquire a lease and open the file.
1027 static struct obd_client_handle *
1028 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
1031 struct lookup_intent it = { .it_op = IT_OPEN };
1032 struct ll_sb_info *sbi = ll_i2sbi(inode);
1033 struct md_op_data *op_data;
1034 struct ptlrpc_request *req = NULL;
1035 struct lustre_handle old_open_handle = { 0 };
1036 struct obd_client_handle *och = NULL;
1041 if (fmode != FMODE_WRITE && fmode != FMODE_READ)
1042 RETURN(ERR_PTR(-EINVAL));
1045 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
1046 RETURN(ERR_PTR(-EPERM));
1048 rc = ll_lease_och_acquire(inode, file, &old_open_handle);
1050 RETURN(ERR_PTR(rc));
1055 RETURN(ERR_PTR(-ENOMEM));
1057 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
1058 LUSTRE_OPC_ANY, NULL);
1059 if (IS_ERR(op_data))
1060 GOTO(out, rc = PTR_ERR(op_data));
1062 /* To tell the MDT this openhandle is from the same owner */
1063 op_data->op_open_handle = old_open_handle;
1065 it.it_flags = fmode | open_flags;
1066 it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
1067 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
1068 &ll_md_blocking_lease_ast,
1069 /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
1070 * it can be cancelled which may mislead applications that the lease is
1072 * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
1073 * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
1074 * doesn't deal with openhandle, so normal openhandle will be leaked. */
1075 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
1076 ll_finish_md_op_data(op_data);
1077 ptlrpc_req_finished(req);
1079 GOTO(out_release_it, rc);
1081 if (it_disposition(&it, DISP_LOOKUP_NEG))
1082 GOTO(out_release_it, rc = -ENOENT);
1084 rc = it_open_error(DISP_OPEN_OPEN, &it);
1086 GOTO(out_release_it, rc);
1088 LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
1089 rc = ll_och_fill(sbi->ll_md_exp, &it, och);
1091 GOTO(out_release_it, rc);
1093 if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
1094 GOTO(out_close, rc = -EOPNOTSUPP);
1096 /* already get lease, handle lease lock */
1097 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
1098 if (it.it_lock_mode == 0 ||
1099 it.it_lock_bits != MDS_INODELOCK_OPEN) {
1100 /* open lock must return for lease */
1101 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
1102 PFID(ll_inode2fid(inode)), it.it_lock_mode,
1104 GOTO(out_close, rc = -EPROTO);
1107 ll_intent_release(&it);
1111 /* Cancel open lock */
1112 if (it.it_lock_mode != 0) {
1113 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
1115 it.it_lock_mode = 0;
1116 och->och_lease_handle.cookie = 0ULL;
1118 rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
1120 CERROR("%s: error closing file "DFID": %d\n",
1121 sbi->ll_fsname, PFID(&ll_i2info(inode)->lli_fid), rc2);
1122 och = NULL; /* och has been freed in ll_close_inode_openhandle() */
1124 ll_intent_release(&it);
1128 RETURN(ERR_PTR(rc));
1132 * Check whether a layout swap can be done between two inodes.
1134 * \param[in] inode1 First inode to check
1135 * \param[in] inode2 Second inode to check
1137 * \retval 0 on success, layout swap can be performed between both inodes
1138 * \retval negative error code if requirements are not met
1140 static int ll_check_swap_layouts_validity(struct inode *inode1,
1141 struct inode *inode2)
1143 if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
1146 if (inode_permission(inode1, MAY_WRITE) ||
1147 inode_permission(inode2, MAY_WRITE))
1150 if (inode1->i_sb != inode2->i_sb)
1156 static int ll_swap_layouts_close(struct obd_client_handle *och,
1157 struct inode *inode, struct inode *inode2)
1159 const struct lu_fid *fid1 = ll_inode2fid(inode);
1160 const struct lu_fid *fid2;
1164 CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
1165 ll_i2sbi(inode)->ll_fsname, PFID(fid1));
1167 rc = ll_check_swap_layouts_validity(inode, inode2);
1169 GOTO(out_free_och, rc);
1171 /* We now know that inode2 is a lustre inode */
1172 fid2 = ll_inode2fid(inode2);
1174 rc = lu_fid_cmp(fid1, fid2);
1176 GOTO(out_free_och, rc = -EINVAL);
1178 /* Close the file and {swap,merge} layouts between inode & inode2.
1179 * NB: lease lock handle is released in mdc_close_layout_swap_pack()
1180 * because we still need it to pack l_remote_handle to MDT. */
1181 rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
1184 och = NULL; /* freed in ll_close_inode_openhandle() */
1194 * Release lease and close the file.
1195 * It will check if the lease has ever broken.
1197 static int ll_lease_close_intent(struct obd_client_handle *och,
1198 struct inode *inode,
1199 bool *lease_broken, enum mds_op_bias bias,
1202 struct ldlm_lock *lock;
1203 bool cancelled = true;
1207 lock = ldlm_handle2lock(&och->och_lease_handle);
1209 lock_res_and_lock(lock);
1210 cancelled = ldlm_is_cancel(lock);
1211 unlock_res_and_lock(lock);
1212 LDLM_LOCK_PUT(lock);
1215 CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
1216 PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
1218 if (lease_broken != NULL)
1219 *lease_broken = cancelled;
1221 if (!cancelled && !bias)
1222 ldlm_cli_cancel(&och->och_lease_handle, 0);
1224 if (cancelled) { /* no need to excute intent */
1229 rc = ll_close_inode_openhandle(inode, och, bias, data);
1233 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
1236 return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
1240 * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
1242 static int ll_lease_file_resync(struct obd_client_handle *och,
1243 struct inode *inode, unsigned long arg)
1245 struct ll_sb_info *sbi = ll_i2sbi(inode);
1246 struct md_op_data *op_data;
1247 struct ll_ioc_lease_id ioc;
1248 __u64 data_version_unused;
1252 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1253 LUSTRE_OPC_ANY, NULL);
1254 if (IS_ERR(op_data))
1255 RETURN(PTR_ERR(op_data));
1257 if (copy_from_user(&ioc, (struct ll_ioc_lease_id __user *)arg,
1261 /* before starting file resync, it's necessary to clean up page cache
1262 * in client memory, otherwise once the layout version is increased,
1263 * writing back cached data will be denied the OSTs. */
1264 rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
1268 op_data->op_lease_handle = och->och_lease_handle;
1269 op_data->op_mirror_id = ioc.lil_mirror_id;
1270 rc = md_file_resync(sbi->ll_md_exp, op_data);
1276 ll_finish_md_op_data(op_data);
1280 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1282 struct ll_inode_info *lli = ll_i2info(inode);
1283 struct cl_object *obj = lli->lli_clob;
1284 struct cl_attr *attr = vvp_env_thread_attr(env);
1292 ll_inode_size_lock(inode);
1294 /* Merge timestamps the most recently obtained from MDS with
1295 * timestamps obtained from OSTs.
1297 * Do not overwrite atime of inode because it may be refreshed
1298 * by file_accessed() function. If the read was served by cache
1299 * data, there is no RPC to be sent so that atime may not be
1300 * transferred to OSTs at all. MDT only updates atime at close time
1301 * if it's at least 'mdd.*.atime_diff' older.
1302 * All in all, the atime in Lustre does not strictly comply with
1303 * POSIX. Solving this problem needs to send an RPC to MDT for each
1304 * read, this will hurt performance.
1306 if (ll_file_test_and_clear_flag(lli, LLIF_UPDATE_ATIME) ||
1307 inode->i_atime.tv_sec < lli->lli_atime)
1308 inode->i_atime.tv_sec = lli->lli_atime;
1310 inode->i_mtime.tv_sec = lli->lli_mtime;
1311 inode->i_ctime.tv_sec = lli->lli_ctime;
1313 mtime = inode->i_mtime.tv_sec;
1314 atime = inode->i_atime.tv_sec;
1315 ctime = inode->i_ctime.tv_sec;
1317 cl_object_attr_lock(obj);
1318 if (OBD_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
1321 rc = cl_object_attr_get(env, obj, attr);
1322 cl_object_attr_unlock(obj);
1325 GOTO(out_size_unlock, rc = (rc == -ENODATA ? 0 : rc));
1327 if (atime < attr->cat_atime)
1328 atime = attr->cat_atime;
1330 if (ctime < attr->cat_ctime)
1331 ctime = attr->cat_ctime;
1333 if (mtime < attr->cat_mtime)
1334 mtime = attr->cat_mtime;
1336 CDEBUG(D_VFSTRACE, DFID" updating i_size %llu\n",
1337 PFID(&lli->lli_fid), attr->cat_size);
1339 i_size_write(inode, attr->cat_size);
1340 inode->i_blocks = attr->cat_blocks;
1342 inode->i_mtime.tv_sec = mtime;
1343 inode->i_atime.tv_sec = atime;
1344 inode->i_ctime.tv_sec = ctime;
1347 ll_inode_size_unlock(inode);
1353 * Set designated mirror for I/O.
1355 * So far only read, write, and truncated can support to issue I/O to
1356 * designated mirror.
1358 void ll_io_set_mirror(struct cl_io *io, const struct file *file)
1360 struct ll_file_data *fd = file->private_data;
1362 /* clear layout version for generic(non-resync) I/O in case it carries
1363 * stale layout version due to I/O restart */
1364 io->ci_layout_version = 0;
1366 /* FLR: disable non-delay for designated mirror I/O because obviously
1367 * only one mirror is available */
1368 if (fd->fd_designated_mirror > 0) {
1370 io->ci_designated_mirror = fd->fd_designated_mirror;
1371 io->ci_layout_version = fd->fd_layout_version;
1374 CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
1375 file->f_path.dentry->d_name.name, io->ci_designated_mirror);
1378 static bool file_is_noatime(const struct file *file)
1380 const struct vfsmount *mnt = file->f_path.mnt;
1381 const struct inode *inode = file_inode((struct file *)file);
1383 /* Adapted from file_accessed() and touch_atime().*/
1384 if (file->f_flags & O_NOATIME)
1387 if (inode->i_flags & S_NOATIME)
1390 if (IS_NOATIME(inode))
1393 if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1396 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1399 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
1405 void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot,
1406 struct vvp_io_args *args)
1408 struct inode *inode = file_inode(file);
1409 struct ll_file_data *fd = file->private_data;
1411 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1412 io->ci_lock_no_expand = fd->ll_lock_no_expand;
1414 if (iot == CIT_WRITE) {
1415 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1416 io->u.ci_wr.wr_sync = !!(file->f_flags & O_SYNC ||
1417 file->f_flags & O_DIRECT ||
1419 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1420 io->u.ci_wr.wr_sync |= !!(args &&
1421 args->via_io_subtype == IO_NORMAL &&
1422 args->u.normal.via_iocb->ki_flags & IOCB_DSYNC);
1426 io->ci_obj = ll_i2info(inode)->lli_clob;
1427 io->ci_lockreq = CILR_MAYBE;
1428 if (ll_file_nolock(file)) {
1429 io->ci_lockreq = CILR_NEVER;
1430 io->ci_no_srvlock = 1;
1431 } else if (file->f_flags & O_APPEND) {
1432 io->ci_lockreq = CILR_MANDATORY;
1434 io->ci_noatime = file_is_noatime(file);
1435 io->ci_async_readahead = false;
1437 /* FLR: only use non-delay I/O for read as there is only one
1438 * avaliable mirror for write. */
1439 io->ci_ndelay = !(iot == CIT_WRITE);
1441 ll_io_set_mirror(io, file);
1444 static void ll_heat_add(struct inode *inode, enum cl_io_type iot,
1447 struct ll_inode_info *lli = ll_i2info(inode);
1448 struct ll_sb_info *sbi = ll_i2sbi(inode);
1449 enum obd_heat_type sample_type;
1450 enum obd_heat_type iobyte_type;
1451 __u64 now = ktime_get_real_seconds();
1453 if (!ll_sbi_has_file_heat(sbi) ||
1454 lli->lli_heat_flags & LU_HEAT_FLAG_OFF)
1457 if (iot == CIT_READ) {
1458 sample_type = OBD_HEAT_READSAMPLE;
1459 iobyte_type = OBD_HEAT_READBYTE;
1460 } else if (iot == CIT_WRITE) {
1461 sample_type = OBD_HEAT_WRITESAMPLE;
1462 iobyte_type = OBD_HEAT_WRITEBYTE;
1467 spin_lock(&lli->lli_heat_lock);
1468 obd_heat_add(&lli->lli_heat_instances[sample_type], now, 1,
1469 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1470 obd_heat_add(&lli->lli_heat_instances[iobyte_type], now, count,
1471 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1472 spin_unlock(&lli->lli_heat_lock);
1476 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1477 struct file *file, enum cl_io_type iot,
1478 loff_t *ppos, size_t count)
1480 struct vvp_io *vio = vvp_env_io(env);
1481 struct inode *inode = file_inode(file);
1482 struct ll_inode_info *lli = ll_i2info(inode);
1483 struct ll_file_data *fd = file->private_data;
1484 struct range_lock range;
1488 unsigned retried = 0;
1489 unsigned ignore_lockless = 0;
1493 CDEBUG(D_VFSTRACE, "%s: %s ppos: %llu, count: %zu\n",
1494 file_dentry(file)->d_name.name,
1495 iot == CIT_READ ? "read" : "write", *ppos, count);
1498 io = vvp_env_thread_io(env);
1499 ll_io_init(io, file, iot, args);
1500 io->ci_ignore_lockless = ignore_lockless;
1501 io->ci_ndelay_tried = retried;
1503 if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
1504 bool range_locked = false;
1506 if (file->f_flags & O_APPEND)
1507 range_lock_init(&range, 0, LUSTRE_EOF);
1509 range_lock_init(&range, *ppos, *ppos + count - 1);
1511 vio->vui_fd = file->private_data;
1512 vio->vui_io_subtype = args->via_io_subtype;
1514 switch (vio->vui_io_subtype) {
1516 vio->vui_iter = args->u.normal.via_iter;
1517 vio->vui_iocb = args->u.normal.via_iocb;
1518 /* Direct IO reads must also take range lock,
1519 * or multiple reads will try to work on the same pages
1520 * See LU-6227 for details. */
1521 if (((iot == CIT_WRITE) ||
1522 (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1523 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1524 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1526 rc = range_lock(&lli->lli_write_tree, &range);
1530 range_locked = true;
1534 vio->u.splice.vui_pipe = args->u.splice.via_pipe;
1535 vio->u.splice.vui_flags = args->u.splice.via_flags;
1538 CERROR("unknown IO subtype %u\n", vio->vui_io_subtype);
1542 ll_cl_add(file, env, io, LCC_RW);
1543 rc = cl_io_loop(env, io);
1544 ll_cl_remove(file, env);
1547 CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1549 range_unlock(&lli->lli_write_tree, &range);
1552 /* cl_io_rw_init() handled IO */
1556 if (io->ci_nob > 0) {
1557 result += io->ci_nob;
1558 count -= io->ci_nob;
1559 *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
1561 /* prepare IO restart */
1562 if (count > 0 && args->via_io_subtype == IO_NORMAL)
1563 args->u.normal.via_iter = vio->vui_iter;
1566 cl_io_fini(env, io);
1569 "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1570 file->f_path.dentry->d_name.name,
1571 iot, rc, result, io->ci_need_restart);
1573 if ((rc == 0 || rc == -ENODATA || rc == -ENOLCK) &&
1574 count > 0 && io->ci_need_restart) {
1576 "%s: restart %s from %lld, count: %zu, ret: %zd, rc: %d\n",
1577 file_dentry(file)->d_name.name,
1578 iot == CIT_READ ? "read" : "write",
1579 *ppos, count, result, rc);
1580 /* preserve the tried count for FLR */
1581 retried = io->ci_ndelay_tried;
1582 ignore_lockless = io->ci_ignore_lockless;
1586 if (iot == CIT_READ) {
1588 ll_stats_ops_tally(ll_i2sbi(inode),
1589 LPROC_LL_READ_BYTES, result);
1590 } else if (iot == CIT_WRITE) {
1592 ll_stats_ops_tally(ll_i2sbi(inode),
1593 LPROC_LL_WRITE_BYTES, result);
1594 fd->fd_write_failed = false;
1595 } else if (result == 0 && rc == 0) {
1598 fd->fd_write_failed = true;
1600 fd->fd_write_failed = false;
1601 } else if (rc != -ERESTARTSYS) {
1602 fd->fd_write_failed = true;
1606 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1608 ll_heat_add(inode, iot, result);
1610 RETURN(result > 0 ? result : rc);
1614 * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1615 * especially for small I/O.
1617 * To serve a read request, CLIO has to create and initialize a cl_io and
1618 * then request DLM lock. This has turned out to have siginificant overhead
1619 * and affects the performance of small I/O dramatically.
1621 * It's not necessary to create a cl_io for each I/O. Under the help of read
1622 * ahead, most of the pages being read are already in memory cache and we can
1623 * read those pages directly because if the pages exist, the corresponding DLM
1624 * lock must exist so that page content must be valid.
1626 * In fast read implementation, the llite speculatively finds and reads pages
1627 * in memory cache. There are three scenarios for fast read:
1628 * - If the page exists and is uptodate, kernel VM will provide the data and
1629 * CLIO won't be intervened;
1630 * - If the page was brought into memory by read ahead, it will be exported
1631 * and read ahead parameters will be updated;
1632 * - Otherwise the page is not in memory, we can't do fast read. Therefore,
1633 * it will go back and invoke normal read, i.e., a cl_io will be created
1634 * and DLM lock will be requested.
1636 * POSIX compliance: posix standard states that read is intended to be atomic.
1637 * Lustre read implementation is in line with Linux kernel read implementation
1638 * and neither of them complies with POSIX standard in this matter. Fast read
1639 * doesn't make the situation worse on single node but it may interleave write
1640 * results from multiple nodes due to short read handling in ll_file_aio_read().
1642 * \param env - lu_env
1643 * \param iocb - kiocb from kernel
1644 * \param iter - user space buffers where the data will be copied
1646 * \retval - number of bytes have been read, or error code if error occurred.
1649 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1653 if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1656 /* NB: we can't do direct IO for fast read because it will need a lock
1657 * to make IO engine happy. */
1658 if (iocb->ki_filp->f_flags & O_DIRECT)
1661 result = generic_file_read_iter(iocb, iter);
1663 /* If the first page is not in cache, generic_file_aio_read() will be
1664 * returned with -ENODATA.
1665 * See corresponding code in ll_readpage(). */
1666 if (result == -ENODATA)
1670 ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
1671 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1672 LPROC_LL_READ_BYTES, result);
1679 * Read from a file (through the page cache).
1681 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1684 struct vvp_io_args *args;
1685 struct file *file = iocb->ki_filp;
1689 ktime_t kstart = ktime_get();
1692 if (!iov_iter_count(to))
1696 * Currently when PCC read failed, we do not fall back to the
1697 * normal read path, just return the error.
1698 * The resaon is that: for RW-PCC, the file data may be modified
1699 * in the PCC and inconsistent with the data on OSTs (or file
1700 * data has been removed from the Lustre file system), at this
1701 * time, fallback to the normal read path may read the wrong
1703 * TODO: for RO-PCC (readonly PCC), fall back to normal read
1704 * path: read data from data copy on OSTs.
1706 result = pcc_file_read_iter(iocb, to, &cached);
1710 ll_ras_enter(file, iocb->ki_pos, iov_iter_count(to));
1712 result = ll_do_fast_read(iocb, to);
1713 if (result < 0 || iov_iter_count(to) == 0)
1716 env = cl_env_get(&refcheck);
1718 return PTR_ERR(env);
1720 args = ll_env_args(env, IO_NORMAL);
1721 args->u.normal.via_iter = to;
1722 args->u.normal.via_iocb = iocb;
1724 rc2 = ll_file_io_generic(env, args, file, CIT_READ,
1725 &iocb->ki_pos, iov_iter_count(to));
1728 else if (result == 0)
1731 cl_env_put(env, &refcheck);
1734 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
1735 file->private_data, iocb->ki_pos, result,
1737 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_READ,
1738 ktime_us_delta(ktime_get(), kstart));
1745 * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
1746 * If a page is already in the page cache and dirty (and some other things -
1747 * See ll_tiny_write_begin for the instantiation of these rules), then we can
1748 * write to it without doing a full I/O, because Lustre already knows about it
1749 * and will write it out. This saves a lot of processing time.
1751 * All writes here are within one page, so exclusion is handled by the page
1752 * lock on the vm page. We do not do tiny writes for writes which touch
1753 * multiple pages because it's very unlikely multiple sequential pages are
1754 * are already dirty.
1756 * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
1757 * and are unlikely to be to already dirty pages.
1759 * Attribute updates are important here, we do them in ll_tiny_write_end.
1761 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
1763 ssize_t count = iov_iter_count(iter);
1764 struct file *file = iocb->ki_filp;
1765 struct inode *inode = file_inode(file);
1766 bool lock_inode = !IS_NOSEC(inode);
1771 /* Restrict writes to single page and < PAGE_SIZE. See comment at top
1772 * of function for why.
1774 if (count >= PAGE_SIZE ||
1775 (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
1778 if (unlikely(lock_inode))
1780 result = __generic_file_write_iter(iocb, iter);
1782 if (unlikely(lock_inode))
1783 inode_unlock(inode);
1785 /* If the page is not already dirty, ll_tiny_write_begin returns
1786 * -ENODATA. We continue on to normal write.
1788 if (result == -ENODATA)
1792 ll_heat_add(inode, CIT_WRITE, result);
1793 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_WRITE_BYTES,
1795 ll_file_set_flag(ll_i2info(inode), LLIF_DATA_MODIFIED);
1798 CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
1804 * Write to a file (through the page cache).
1806 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1808 struct vvp_io_args *args;
1810 ssize_t rc_tiny = 0, rc_normal;
1811 struct file *file = iocb->ki_filp;
1814 ktime_t kstart = ktime_get();
1819 if (!iov_iter_count(from))
1820 GOTO(out, rc_normal = 0);
1823 * When PCC write failed, we usually do not fall back to the normal
1824 * write path, just return the error. But there is a special case when
1825 * returned error code is -ENOSPC due to running out of space on PCC HSM
1826 * bakcend. At this time, it will fall back to normal I/O path and
1827 * retry the I/O. As the file is in HSM released state, it will restore
1828 * the file data to OSTs first and redo the write again. And the
1829 * restore process will revoke the layout lock and detach the file
1830 * from PCC cache automatically.
1832 result = pcc_file_write_iter(iocb, from, &cached);
1833 if (cached && result != -ENOSPC && result != -EDQUOT)
1834 GOTO(out, rc_normal = result);
1836 /* NB: we can't do direct IO for tiny writes because they use the page
1837 * cache, we can't do sync writes because tiny writes can't flush
1838 * pages, and we can't do append writes because we can't guarantee the
1839 * required DLM locks are held to protect file size.
1841 if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(file))) &&
1842 !(file->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
1843 rc_tiny = ll_do_tiny_write(iocb, from);
1845 /* In case of error, go on and try normal write - Only stop if tiny
1846 * write completed I/O.
1848 if (iov_iter_count(from) == 0)
1849 GOTO(out, rc_normal = rc_tiny);
1851 env = cl_env_get(&refcheck);
1853 return PTR_ERR(env);
1855 args = ll_env_args(env, IO_NORMAL);
1856 args->u.normal.via_iter = from;
1857 args->u.normal.via_iocb = iocb;
1859 rc_normal = ll_file_io_generic(env, args, file, CIT_WRITE,
1860 &iocb->ki_pos, iov_iter_count(from));
1862 /* On success, combine bytes written. */
1863 if (rc_tiny >= 0 && rc_normal > 0)
1864 rc_normal += rc_tiny;
1865 /* On error, only return error from normal write if tiny write did not
1866 * write any bytes. Otherwise return bytes written by tiny write.
1868 else if (rc_tiny > 0)
1869 rc_normal = rc_tiny;
1871 cl_env_put(env, &refcheck);
1873 if (rc_normal > 0) {
1874 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
1875 file->private_data, iocb->ki_pos,
1877 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_WRITE,
1878 ktime_us_delta(ktime_get(), kstart));
1884 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1886 * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
1888 static int ll_file_get_iov_count(const struct iovec *iov,
1889 unsigned long *nr_segs, size_t *count,
1895 for (seg = 0; seg < *nr_segs; seg++) {
1896 const struct iovec *iv = &iov[seg];
1899 * If any segment has a negative length, or the cumulative
1900 * length ever wraps negative then return -EINVAL.
1903 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1905 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
1910 cnt -= iv->iov_len; /* This segment is no good */
1917 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1918 unsigned long nr_segs, loff_t pos)
1925 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_READ);
1932 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
1933 iov_iter_init(&to, READ, iov, nr_segs, iov_count);
1934 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
1935 iov_iter_init(&to, iov, nr_segs, iov_count, 0);
1936 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
1938 result = ll_file_read_iter(iocb, &to);
1943 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
1946 struct iovec iov = { .iov_base = buf, .iov_len = count };
1955 init_sync_kiocb(&kiocb, file);
1956 kiocb.ki_pos = *ppos;
1957 #ifdef HAVE_KIOCB_KI_LEFT
1958 kiocb.ki_left = count;
1959 #elif defined(HAVE_KI_NBYTES)
1960 kiocb.i_nbytes = count;
1963 result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
1964 *ppos = kiocb.ki_pos;
1970 * Write to a file (through the page cache).
1973 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1974 unsigned long nr_segs, loff_t pos)
1976 struct iov_iter from;
1981 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_WRITE);
1988 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
1989 iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
1990 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
1991 iov_iter_init(&from, iov, nr_segs, iov_count, 0);
1992 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
1994 result = ll_file_write_iter(iocb, &from);
1999 static ssize_t ll_file_write(struct file *file, const char __user *buf,
2000 size_t count, loff_t *ppos)
2002 struct iovec iov = { .iov_base = (void __user *)buf,
2012 init_sync_kiocb(&kiocb, file);
2013 kiocb.ki_pos = *ppos;
2014 #ifdef HAVE_KIOCB_KI_LEFT
2015 kiocb.ki_left = count;
2016 #elif defined(HAVE_KI_NBYTES)
2017 kiocb.ki_nbytes = count;
2020 result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
2021 *ppos = kiocb.ki_pos;
2025 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
2028 * Send file content (through pagecache) somewhere with helper
2030 static ssize_t ll_file_splice_read(struct file *in_file, loff_t *ppos,
2031 struct pipe_inode_info *pipe, size_t count,
2035 struct vvp_io_args *args;
2042 result = pcc_file_splice_read(in_file, ppos, pipe,
2043 count, flags, &cached);
2047 ll_ras_enter(in_file, *ppos, count);
2049 env = cl_env_get(&refcheck);
2051 RETURN(PTR_ERR(env));
2053 args = ll_env_args(env, IO_SPLICE);
2054 args->u.splice.via_pipe = pipe;
2055 args->u.splice.via_flags = flags;
2057 result = ll_file_io_generic(env, args, in_file, CIT_READ, ppos, count);
2058 cl_env_put(env, &refcheck);
2061 ll_rw_stats_tally(ll_i2sbi(file_inode(in_file)), current->pid,
2062 in_file->private_data, *ppos, result,
2067 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
2068 __u64 flags, struct lov_user_md *lum, int lum_size)
2070 struct lookup_intent oit = {
2072 .it_flags = flags | MDS_OPEN_BY_FID,
2077 if ((__swab32(lum->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
2078 le32_to_cpu(LOV_MAGIC_MAGIC)) {
2079 /* this code will only exist for big-endian systems */
2080 lustre_swab_lov_user_md(lum, 0);
2083 ll_inode_size_lock(inode);
2084 rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
2086 GOTO(out_unlock, rc);
2088 ll_release_openhandle(dentry, &oit);
2091 ll_inode_size_unlock(inode);
2092 ll_intent_release(&oit);
2097 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
2098 struct lov_mds_md **lmmp, int *lmm_size,
2099 struct ptlrpc_request **request)
2101 struct ll_sb_info *sbi = ll_i2sbi(inode);
2102 struct mdt_body *body;
2103 struct lov_mds_md *lmm = NULL;
2104 struct ptlrpc_request *req = NULL;
2105 struct md_op_data *op_data;
2108 rc = ll_get_default_mdsize(sbi, &lmmsize);
2112 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
2113 strlen(filename), lmmsize,
2114 LUSTRE_OPC_ANY, NULL);
2115 if (IS_ERR(op_data))
2116 RETURN(PTR_ERR(op_data));
2118 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
2119 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
2120 ll_finish_md_op_data(op_data);
2122 CDEBUG(D_INFO, "md_getattr_name failed "
2123 "on %s: rc %d\n", filename, rc);
2127 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2128 LASSERT(body != NULL); /* checked by mdc_getattr_name */
2130 lmmsize = body->mbo_eadatasize;
2132 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
2134 GOTO(out, rc = -ENODATA);
2137 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
2138 LASSERT(lmm != NULL);
2140 if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
2141 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
2142 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1) &&
2143 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_FOREIGN))
2144 GOTO(out, rc = -EPROTO);
2147 * This is coming from the MDS, so is probably in
2148 * little endian. We convert it to host endian before
2149 * passing it to userspace.
2151 if ((lmm->lmm_magic & __swab32(LOV_MAGIC_MAGIC)) ==
2152 __swab32(LOV_MAGIC_MAGIC)) {
2153 int stripe_count = 0;
2155 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
2156 lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2157 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
2158 if (le32_to_cpu(lmm->lmm_pattern) &
2159 LOV_PATTERN_F_RELEASED)
2163 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
2165 /* if function called for directory - we should
2166 * avoid swab not existent lsm objects */
2167 if (lmm->lmm_magic == LOV_MAGIC_V1 && S_ISREG(body->mbo_mode))
2168 lustre_swab_lov_user_md_objects(
2169 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
2171 else if (lmm->lmm_magic == LOV_MAGIC_V3 &&
2172 S_ISREG(body->mbo_mode))
2173 lustre_swab_lov_user_md_objects(
2174 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
2180 *lmm_size = lmmsize;
2185 static int ll_lov_setea(struct inode *inode, struct file *file,
2188 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
2189 struct lov_user_md *lump;
2190 int lum_size = sizeof(struct lov_user_md) +
2191 sizeof(struct lov_user_ost_data);
2195 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2198 OBD_ALLOC_LARGE(lump, lum_size);
2202 if (copy_from_user(lump, arg, lum_size))
2203 GOTO(out_lump, rc = -EFAULT);
2205 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
2207 cl_lov_delay_create_clear(&file->f_flags);
2210 OBD_FREE_LARGE(lump, lum_size);
2214 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
2221 env = cl_env_get(&refcheck);
2223 RETURN(PTR_ERR(env));
2225 rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
2226 cl_env_put(env, &refcheck);
2230 static int ll_lov_setstripe(struct inode *inode, struct file *file,
2233 struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
2234 struct lov_user_md *klum;
2236 __u64 flags = FMODE_WRITE;
2239 rc = ll_copy_user_md(lum, &klum);
2244 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2249 rc = put_user(0, &lum->lmm_stripe_count);
2253 rc = ll_layout_refresh(inode, &gen);
2257 rc = ll_file_getstripe(inode, arg, lum_size);
2259 cl_lov_delay_create_clear(&file->f_flags);
2262 OBD_FREE_LARGE(klum, lum_size);
2268 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2270 struct ll_inode_info *lli = ll_i2info(inode);
2271 struct cl_object *obj = lli->lli_clob;
2272 struct ll_file_data *fd = file->private_data;
2273 struct ll_grouplock grouplock;
2278 CWARN("group id for group lock must not be 0\n");
2282 if (ll_file_nolock(file))
2283 RETURN(-EOPNOTSUPP);
2285 if (file->f_flags & O_NONBLOCK) {
2286 if (!mutex_trylock(&lli->lli_group_mutex))
2289 mutex_lock(&lli->lli_group_mutex);
2291 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2292 CWARN("group lock already existed with gid %lu\n",
2293 fd->fd_grouplock.lg_gid);
2294 GOTO(out, rc = -EINVAL);
2296 if (arg != lli->lli_group_gid && lli->lli_group_users != 0) {
2297 if (file->f_flags & O_NONBLOCK)
2298 GOTO(out, rc = -EAGAIN);
2299 mutex_unlock(&lli->lli_group_mutex);
2300 wait_var_event(&lli->lli_group_users, !lli->lli_group_users);
2301 GOTO(retry, rc = 0);
2303 LASSERT(fd->fd_grouplock.lg_lock == NULL);
2306 * XXX: group lock needs to protect all OST objects while PFL
2307 * can add new OST objects during the IO, so we'd instantiate
2308 * all OST objects before getting its group lock.
2313 struct cl_layout cl = {
2314 .cl_is_composite = false,
2316 struct lu_extent ext = {
2318 .e_end = OBD_OBJECT_EOF,
2321 env = cl_env_get(&refcheck);
2323 GOTO(out, rc = PTR_ERR(env));
2325 rc = cl_object_layout_get(env, obj, &cl);
2326 if (!rc && cl.cl_is_composite)
2327 rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2330 cl_env_put(env, &refcheck);
2335 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2336 arg, (file->f_flags & O_NONBLOCK), &grouplock);
2341 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2342 fd->fd_grouplock = grouplock;
2343 if (lli->lli_group_users == 0)
2344 lli->lli_group_gid = grouplock.lg_gid;
2345 lli->lli_group_users++;
2347 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2349 mutex_unlock(&lli->lli_group_mutex);
2354 static int ll_put_grouplock(struct inode *inode, struct file *file,
2357 struct ll_inode_info *lli = ll_i2info(inode);
2358 struct ll_file_data *fd = file->private_data;
2359 struct ll_grouplock grouplock;
2363 mutex_lock(&lli->lli_group_mutex);
2364 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2365 CWARN("no group lock held\n");
2366 GOTO(out, rc = -EINVAL);
2369 LASSERT(fd->fd_grouplock.lg_lock != NULL);
2371 if (fd->fd_grouplock.lg_gid != arg) {
2372 CWARN("group lock %lu doesn't match current id %lu\n",
2373 arg, fd->fd_grouplock.lg_gid);
2374 GOTO(out, rc = -EINVAL);
2377 grouplock = fd->fd_grouplock;
2378 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2379 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2381 cl_put_grouplock(&grouplock);
2383 lli->lli_group_users--;
2384 if (lli->lli_group_users == 0) {
2385 lli->lli_group_gid = 0;
2386 wake_up_var(&lli->lli_group_users);
2388 CDEBUG(D_INFO, "group lock %lu released\n", arg);
2391 mutex_unlock(&lli->lli_group_mutex);
2397 * Close inode open handle
2399 * \param dentry [in] dentry which contains the inode
2400 * \param it [in,out] intent which contains open info and result
2403 * \retval <0 failure
2405 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2407 struct inode *inode = dentry->d_inode;
2408 struct obd_client_handle *och;
2414 /* Root ? Do nothing. */
2415 if (dentry->d_inode->i_sb->s_root == dentry)
2418 /* No open handle to close? Move away */
2419 if (!it_disposition(it, DISP_OPEN_OPEN))
2422 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2424 OBD_ALLOC(och, sizeof(*och));
2426 GOTO(out, rc = -ENOMEM);
2428 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2432 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2434 /* this one is in place of ll_file_open */
2435 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2436 ptlrpc_req_finished(it->it_request);
2437 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2443 * Get size for inode for which FIEMAP mapping is requested.
2444 * Make the FIEMAP get_info call and returns the result.
2445 * \param fiemap kernel buffer to hold extens
2446 * \param num_bytes kernel buffer size
2448 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2454 struct ll_fiemap_info_key fmkey = { .lfik_name = KEY_FIEMAP, };
2457 /* Checks for fiemap flags */
2458 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2459 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2463 /* Check for FIEMAP_FLAG_SYNC */
2464 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2465 rc = filemap_fdatawrite(inode->i_mapping);
2470 env = cl_env_get(&refcheck);
2472 RETURN(PTR_ERR(env));
2474 if (i_size_read(inode) == 0) {
2475 rc = ll_glimpse_size(inode);
2480 fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2481 obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2482 obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2484 /* If filesize is 0, then there would be no objects for mapping */
2485 if (fmkey.lfik_oa.o_size == 0) {
2486 fiemap->fm_mapped_extents = 0;
2490 fmkey.lfik_fiemap = *fiemap;
2492 rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2493 &fmkey, fiemap, &num_bytes);
2495 cl_env_put(env, &refcheck);
2499 int ll_fid2path(struct inode *inode, void __user *arg)
2501 struct obd_export *exp = ll_i2mdexp(inode);
2502 const struct getinfo_fid2path __user *gfin = arg;
2504 struct getinfo_fid2path *gfout;
2510 if (!cfs_capable(CFS_CAP_DAC_READ_SEARCH) &&
2511 !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2514 /* Only need to get the buflen */
2515 if (get_user(pathlen, &gfin->gf_pathlen))
2518 if (pathlen > PATH_MAX)
2521 outsize = sizeof(*gfout) + pathlen;
2522 OBD_ALLOC(gfout, outsize);
2526 if (copy_from_user(gfout, arg, sizeof(*gfout)))
2527 GOTO(gf_free, rc = -EFAULT);
2528 /* append root FID after gfout to let MDT know the root FID so that it
2529 * can lookup the correct path, this is mainly for fileset.
2530 * old server without fileset mount support will ignore this. */
2531 *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
2533 /* Call mdc_iocontrol */
2534 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
2538 if (copy_to_user(arg, gfout, outsize))
2542 OBD_FREE(gfout, outsize);
2547 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
2549 struct cl_object *obj = ll_i2info(inode)->lli_clob;
2557 ioc->idv_version = 0;
2558 ioc->idv_layout_version = UINT_MAX;
2560 /* If no file object initialized, we consider its version is 0. */
2564 env = cl_env_get(&refcheck);
2566 RETURN(PTR_ERR(env));
2568 io = vvp_env_thread_io(env);
2570 io->u.ci_data_version.dv_data_version = 0;
2571 io->u.ci_data_version.dv_layout_version = UINT_MAX;
2572 io->u.ci_data_version.dv_flags = ioc->idv_flags;
2575 if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
2576 result = cl_io_loop(env, io);
2578 result = io->ci_result;
2580 ioc->idv_version = io->u.ci_data_version.dv_data_version;
2581 ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
2583 cl_io_fini(env, io);
2585 if (unlikely(io->ci_need_restart))
2588 cl_env_put(env, &refcheck);
2594 * Read the data_version for inode.
2596 * This value is computed using stripe object version on OST.
2597 * Version is computed using server side locking.
2599 * @param flags if do sync on the OST side;
2601 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
2602 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
2604 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
2606 struct ioc_data_version ioc = { .idv_flags = flags };
2609 rc = ll_ioc_data_version(inode, &ioc);
2611 *data_version = ioc.idv_version;
2617 * Trigger a HSM release request for the provided inode.
2619 int ll_hsm_release(struct inode *inode)
2622 struct obd_client_handle *och = NULL;
2623 __u64 data_version = 0;
2628 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2629 ll_i2sbi(inode)->ll_fsname,
2630 PFID(&ll_i2info(inode)->lli_fid));
2632 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2634 GOTO(out, rc = PTR_ERR(och));
2636 /* Grab latest data_version and [am]time values */
2637 rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
2641 env = cl_env_get(&refcheck);
2643 GOTO(out, rc = PTR_ERR(env));
2645 rc = ll_merge_attr(env, inode);
2646 cl_env_put(env, &refcheck);
2648 /* If error happen, we have the wrong size for a file.
2654 /* Release the file.
2655 * NB: lease lock handle is released in mdc_hsm_release_pack() because
2656 * we still need it to pack l_remote_handle to MDT. */
2657 rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
2663 if (och != NULL && !IS_ERR(och)) /* close the file */
2664 ll_lease_close(och, inode, NULL);
2669 struct ll_swap_stack {
2672 struct inode *inode1;
2673 struct inode *inode2;
2678 static int ll_swap_layouts(struct file *file1, struct file *file2,
2679 struct lustre_swap_layouts *lsl)
2681 struct mdc_swap_layouts msl;
2682 struct md_op_data *op_data;
2685 struct ll_swap_stack *llss = NULL;
2688 OBD_ALLOC_PTR(llss);
2692 llss->inode1 = file_inode(file1);
2693 llss->inode2 = file_inode(file2);
2695 rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
2699 /* we use 2 bool because it is easier to swap than 2 bits */
2700 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2701 llss->check_dv1 = true;
2703 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2704 llss->check_dv2 = true;
2706 /* we cannot use lsl->sl_dvX directly because we may swap them */
2707 llss->dv1 = lsl->sl_dv1;
2708 llss->dv2 = lsl->sl_dv2;
2710 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2711 if (rc == 0) /* same file, done! */
2714 if (rc < 0) { /* sequentialize it */
2715 swap(llss->inode1, llss->inode2);
2717 swap(llss->dv1, llss->dv2);
2718 swap(llss->check_dv1, llss->check_dv2);
2722 if (gid != 0) { /* application asks to flush dirty cache */
2723 rc = ll_get_grouplock(llss->inode1, file1, gid);
2727 rc = ll_get_grouplock(llss->inode2, file2, gid);
2729 ll_put_grouplock(llss->inode1, file1, gid);
2734 /* ultimate check, before swaping the layouts we check if
2735 * dataversion has changed (if requested) */
2736 if (llss->check_dv1) {
2737 rc = ll_data_version(llss->inode1, &dv, 0);
2740 if (dv != llss->dv1)
2741 GOTO(putgl, rc = -EAGAIN);
2744 if (llss->check_dv2) {
2745 rc = ll_data_version(llss->inode2, &dv, 0);
2748 if (dv != llss->dv2)
2749 GOTO(putgl, rc = -EAGAIN);
2752 /* struct md_op_data is used to send the swap args to the mdt
2753 * only flags is missing, so we use struct mdc_swap_layouts
2754 * through the md_op_data->op_data */
2755 /* flags from user space have to be converted before they are send to
2756 * server, no flag is sent today, they are only used on the client */
2759 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
2760 0, LUSTRE_OPC_ANY, &msl);
2761 if (IS_ERR(op_data))
2762 GOTO(free, rc = PTR_ERR(op_data));
2764 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
2765 sizeof(*op_data), op_data, NULL);
2766 ll_finish_md_op_data(op_data);
2773 ll_put_grouplock(llss->inode2, file2, gid);
2774 ll_put_grouplock(llss->inode1, file1, gid);
2784 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
2786 struct obd_export *exp = ll_i2mdexp(inode);
2787 struct md_op_data *op_data;
2791 /* Detect out-of range masks */
2792 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
2795 /* Non-root users are forbidden to set or clear flags which are
2796 * NOT defined in HSM_USER_MASK. */
2797 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
2798 !cfs_capable(CFS_CAP_SYS_ADMIN))
2801 if (!exp_connect_archive_id_array(exp)) {
2802 /* Detect out-of range archive id */
2803 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
2804 (hss->hss_archive_id > LL_HSM_ORIGIN_MAX_ARCHIVE))
2808 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2809 LUSTRE_OPC_ANY, hss);
2810 if (IS_ERR(op_data))
2811 RETURN(PTR_ERR(op_data));
2813 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, exp, sizeof(*op_data),
2816 ll_finish_md_op_data(op_data);
2821 static int ll_hsm_import(struct inode *inode, struct file *file,
2822 struct hsm_user_import *hui)
2824 struct hsm_state_set *hss = NULL;
2825 struct iattr *attr = NULL;
2829 if (!S_ISREG(inode->i_mode))
2835 GOTO(out, rc = -ENOMEM);
2837 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
2838 hss->hss_archive_id = hui->hui_archive_id;
2839 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
2840 rc = ll_hsm_state_set(inode, hss);
2844 OBD_ALLOC_PTR(attr);
2846 GOTO(out, rc = -ENOMEM);
2848 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
2849 attr->ia_mode |= S_IFREG;
2850 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
2851 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
2852 attr->ia_size = hui->hui_size;
2853 attr->ia_mtime.tv_sec = hui->hui_mtime;
2854 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
2855 attr->ia_atime.tv_sec = hui->hui_atime;
2856 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
2858 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
2859 ATTR_UID | ATTR_GID |
2860 ATTR_MTIME | ATTR_MTIME_SET |
2861 ATTR_ATIME | ATTR_ATIME_SET;
2865 rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
2869 inode_unlock(inode);
2881 static inline long ll_lease_type_from_fmode(fmode_t fmode)
2883 return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
2884 ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
2887 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
2889 struct inode *inode = file_inode(file);
2891 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
2892 ATTR_MTIME | ATTR_MTIME_SET |
2895 .tv_sec = lfu->lfu_atime_sec,
2896 .tv_nsec = lfu->lfu_atime_nsec,
2899 .tv_sec = lfu->lfu_mtime_sec,
2900 .tv_nsec = lfu->lfu_mtime_nsec,
2903 .tv_sec = lfu->lfu_ctime_sec,
2904 .tv_nsec = lfu->lfu_ctime_nsec,
2910 if (!capable(CAP_SYS_ADMIN))
2913 if (!S_ISREG(inode->i_mode))
2917 rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
2919 inode_unlock(inode);
2924 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
2927 case MODE_READ_USER:
2929 case MODE_WRITE_USER:
2936 static const char *const user_lockname[] = LOCK_MODE_NAMES;
2938 /* Used to allow the upper layers of the client to request an LDLM lock
2939 * without doing an actual read or write.
2941 * Used for ladvise lockahead to manually request specific locks.
2943 * \param[in] file file this ladvise lock request is on
2944 * \param[in] ladvise ladvise struct describing this lock request
2946 * \retval 0 success, no detailed result available (sync requests
2947 * and requests sent to the server [not handled locally]
2948 * cannot return detailed results)
2949 * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
2950 * see definitions for details.
2951 * \retval negative negative errno on error
2953 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
2955 struct lu_env *env = NULL;
2956 struct cl_io *io = NULL;
2957 struct cl_lock *lock = NULL;
2958 struct cl_lock_descr *descr = NULL;
2959 struct dentry *dentry = file->f_path.dentry;
2960 struct inode *inode = dentry->d_inode;
2961 enum cl_lock_mode cl_mode;
2962 off_t start = ladvise->lla_start;
2963 off_t end = ladvise->lla_end;
2970 "Lock request: file=%pd, inode=%p, mode=%s start=%llu, end=%llu\n",
2971 dentry, dentry->d_inode,
2972 user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
2975 cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
2977 GOTO(out, result = cl_mode);
2979 /* Get IO environment */
2980 result = cl_io_get(inode, &env, &io, &refcheck);
2984 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
2987 * nothing to do for this io. This currently happens when
2988 * stripe sub-object's are not yet created.
2990 result = io->ci_result;
2991 } else if (result == 0) {
2992 lock = vvp_env_lock(env);
2993 descr = &lock->cll_descr;
2995 descr->cld_obj = io->ci_obj;
2996 /* Convert byte offsets to pages */
2997 descr->cld_start = cl_index(io->ci_obj, start);
2998 descr->cld_end = cl_index(io->ci_obj, end);
2999 descr->cld_mode = cl_mode;
3000 /* CEF_MUST is used because we do not want to convert a
3001 * lockahead request to a lockless lock */
3002 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND |
3005 if (ladvise->lla_peradvice_flags & LF_ASYNC)
3006 descr->cld_enq_flags |= CEF_SPECULATIVE;
3008 result = cl_lock_request(env, io, lock);
3010 /* On success, we need to release the lock */
3012 cl_lock_release(env, lock);
3014 cl_io_fini(env, io);
3015 cl_env_put(env, &refcheck);
3017 /* -ECANCELED indicates a matching lock with a different extent
3018 * was already present, and -EEXIST indicates a matching lock
3019 * on exactly the same extent was already present.
3020 * We convert them to positive values for userspace to make
3021 * recognizing true errors easier.
3022 * Note we can only return these detailed results on async requests,
3023 * as sync requests look the same as i/o requests for locking. */
3024 if (result == -ECANCELED)
3025 result = LLA_RESULT_DIFFERENT;
3026 else if (result == -EEXIST)
3027 result = LLA_RESULT_SAME;
3032 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
3034 static int ll_ladvise_sanity(struct inode *inode,
3035 struct llapi_lu_ladvise *ladvise)
3037 struct ll_sb_info *sbi = ll_i2sbi(inode);
3038 enum lu_ladvise_type advice = ladvise->lla_advice;
3039 /* Note the peradvice flags is a 32 bit field, so per advice flags must
3040 * be in the first 32 bits of enum ladvise_flags */
3041 __u32 flags = ladvise->lla_peradvice_flags;
3042 /* 3 lines at 80 characters per line, should be plenty */
3045 if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
3048 "%s: advice with value '%d' not recognized, last supported advice is %s (value '%d'): rc = %d\n",
3049 sbi->ll_fsname, advice,
3050 ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
3054 /* Per-advice checks */
3056 case LU_LADVISE_LOCKNOEXPAND:
3057 if (flags & ~LF_LOCKNOEXPAND_MASK) {
3059 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3060 "rc = %d\n", sbi->ll_fsname, flags,
3061 ladvise_names[advice], rc);
3065 case LU_LADVISE_LOCKAHEAD:
3066 /* Currently only READ and WRITE modes can be requested */
3067 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
3068 ladvise->lla_lockahead_mode == 0) {
3070 CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
3071 "rc = %d\n", sbi->ll_fsname,
3072 ladvise->lla_lockahead_mode,
3073 ladvise_names[advice], rc);
3077 case LU_LADVISE_WILLREAD:
3078 case LU_LADVISE_DONTNEED:
3080 /* Note fall through above - These checks apply to all advices
3081 * except LOCKNOEXPAND */
3082 if (flags & ~LF_DEFAULT_MASK) {
3084 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3085 "rc = %d\n", sbi->ll_fsname, flags,
3086 ladvise_names[advice], rc);
3089 if (ladvise->lla_start >= ladvise->lla_end) {
3091 CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
3092 "for %s: rc = %d\n", sbi->ll_fsname,
3093 ladvise->lla_start, ladvise->lla_end,
3094 ladvise_names[advice], rc);
3106 * Give file access advices
3108 * The ladvise interface is similar to Linux fadvise() system call, except it
3109 * forwards the advices directly from Lustre client to server. The server side
3110 * codes will apply appropriate read-ahead and caching techniques for the
3111 * corresponding files.
3113 * A typical workload for ladvise is e.g. a bunch of different clients are
3114 * doing small random reads of a file, so prefetching pages into OSS cache
3115 * with big linear reads before the random IO is a net benefit. Fetching
3116 * all that data into each client cache with fadvise() may not be, due to
3117 * much more data being sent to the client.
3119 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
3120 struct llapi_lu_ladvise *ladvise)
3124 struct cl_ladvise_io *lio;
3129 env = cl_env_get(&refcheck);
3131 RETURN(PTR_ERR(env));
3133 io = vvp_env_thread_io(env);
3134 io->ci_obj = ll_i2info(inode)->lli_clob;
3136 /* initialize parameters for ladvise */
3137 lio = &io->u.ci_ladvise;
3138 lio->li_start = ladvise->lla_start;
3139 lio->li_end = ladvise->lla_end;
3140 lio->li_fid = ll_inode2fid(inode);
3141 lio->li_advice = ladvise->lla_advice;
3142 lio->li_flags = flags;
3144 if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
3145 rc = cl_io_loop(env, io);
3149 cl_io_fini(env, io);
3150 cl_env_put(env, &refcheck);
3154 static int ll_lock_noexpand(struct file *file, int flags)
3156 struct ll_file_data *fd = file->private_data;
3158 fd->ll_lock_no_expand = !(flags & LF_UNSET);
3163 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
3166 struct fsxattr fsxattr;
3168 if (copy_from_user(&fsxattr,
3169 (const struct fsxattr __user *)arg,
3173 fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
3174 if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT))
3175 fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
3176 fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
3177 if (copy_to_user((struct fsxattr __user *)arg,
3178 &fsxattr, sizeof(fsxattr)))
3184 int ll_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
3187 * Project Quota ID state is only allowed to change from within the init
3188 * namespace. Enforce that restriction only if we are trying to change
3189 * the quota ID state. Everything else is allowed in user namespaces.
3191 if (current_user_ns() == &init_user_ns)
3194 if (ll_i2info(inode)->lli_projid != fa->fsx_projid)
3197 if (ll_file_test_flag(ll_i2info(inode), LLIF_PROJECT_INHERIT)) {
3198 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
3201 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
3208 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
3212 struct md_op_data *op_data;
3213 struct ptlrpc_request *req = NULL;
3215 struct fsxattr fsxattr;
3216 struct cl_object *obj;
3220 if (copy_from_user(&fsxattr,
3221 (const struct fsxattr __user *)arg,
3225 rc = ll_ioctl_check_project(inode, &fsxattr);
3229 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3230 LUSTRE_OPC_ANY, NULL);
3231 if (IS_ERR(op_data))
3232 RETURN(PTR_ERR(op_data));
3234 flags = ll_xflags_to_inode_flags(fsxattr.fsx_xflags);
3235 op_data->op_attr_flags = ll_inode_to_ext_flags(flags);
3236 if (fsxattr.fsx_xflags & FS_XFLAG_PROJINHERIT)
3237 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
3238 op_data->op_projid = fsxattr.fsx_projid;
3239 op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
3240 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL,
3242 ptlrpc_req_finished(req);
3244 GOTO(out_fsxattr, rc);
3245 ll_update_inode_flags(inode, op_data->op_attr_flags);
3246 obj = ll_i2info(inode)->lli_clob;
3248 GOTO(out_fsxattr, rc);
3250 /* Avoiding OST RPC if this is only project ioctl */
3251 if (fsxattr.fsx_xflags == 0 ||
3252 fsxattr.fsx_xflags == FS_XFLAG_PROJINHERIT)
3253 GOTO(out_fsxattr, rc);
3255 OBD_ALLOC_PTR(attr);
3257 GOTO(out_fsxattr, rc = -ENOMEM);
3259 rc = cl_setattr_ost(obj, attr, OP_XVALID_FLAGS,
3260 fsxattr.fsx_xflags);
3263 ll_finish_md_op_data(op_data);
3267 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
3270 struct inode *inode = file_inode(file);
3271 struct ll_file_data *fd = file->private_data;
3272 struct ll_inode_info *lli = ll_i2info(inode);
3273 struct obd_client_handle *och = NULL;
3274 struct split_param sp;
3275 struct pcc_param param;
3276 bool lease_broken = false;
3278 enum mds_op_bias bias = 0;
3279 struct file *layout_file = NULL;
3281 size_t data_size = 0;
3282 bool attached = false;
3287 mutex_lock(&lli->lli_och_mutex);
3288 if (fd->fd_lease_och != NULL) {
3289 och = fd->fd_lease_och;
3290 fd->fd_lease_och = NULL;
3292 mutex_unlock(&lli->lli_och_mutex);
3297 fmode = och->och_flags;
3299 switch (ioc->lil_flags) {
3300 case LL_LEASE_RESYNC_DONE:
3301 if (ioc->lil_count > IOC_IDS_MAX)
3302 GOTO(out_lease_close, rc = -EINVAL);
3304 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
3305 OBD_ALLOC(data, data_size);
3307 GOTO(out_lease_close, rc = -ENOMEM);
3309 if (copy_from_user(data, (void __user *)arg, data_size))
3310 GOTO(out_lease_close, rc = -EFAULT);
3312 bias = MDS_CLOSE_RESYNC_DONE;
3314 case LL_LEASE_LAYOUT_MERGE: {
3317 if (ioc->lil_count != 1)
3318 GOTO(out_lease_close, rc = -EINVAL);
3320 arg += sizeof(*ioc);
3321 if (copy_from_user(&fd, (void __user *)arg, sizeof(__u32)))
3322 GOTO(out_lease_close, rc = -EFAULT);
3324 layout_file = fget(fd);
3326 GOTO(out_lease_close, rc = -EBADF);
3328 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
3329 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
3330 GOTO(out_lease_close, rc = -EPERM);
3332 data = file_inode(layout_file);
3333 bias = MDS_CLOSE_LAYOUT_MERGE;
3336 case LL_LEASE_LAYOUT_SPLIT: {
3340 if (ioc->lil_count != 2)
3341 GOTO(out_lease_close, rc = -EINVAL);
3343 arg += sizeof(*ioc);
3344 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3345 GOTO(out_lease_close, rc = -EFAULT);
3347 arg += sizeof(__u32);
3348 if (copy_from_user(&mirror_id, (void __user *)arg,
3350 GOTO(out_lease_close, rc = -EFAULT);
3352 layout_file = fget(fdv);
3354 GOTO(out_lease_close, rc = -EBADF);
3356 sp.sp_inode = file_inode(layout_file);
3357 sp.sp_mirror_id = (__u16)mirror_id;
3359 bias = MDS_CLOSE_LAYOUT_SPLIT;
3362 case LL_LEASE_PCC_ATTACH:
3363 if (ioc->lil_count != 1)
3366 arg += sizeof(*ioc);
3367 if (copy_from_user(¶m.pa_archive_id, (void __user *)arg,
3369 GOTO(out_lease_close, rc2 = -EFAULT);
3371 rc2 = pcc_readwrite_attach(file, inode, param.pa_archive_id);
3373 GOTO(out_lease_close, rc2);
3376 /* Grab latest data version */
3377 rc2 = ll_data_version(inode, ¶m.pa_data_version,
3380 GOTO(out_lease_close, rc2);
3383 bias = MDS_PCC_ATTACH;
3386 /* without close intent */
3391 rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
3395 rc = ll_lease_och_release(inode, file);
3404 switch (ioc->lil_flags) {
3405 case LL_LEASE_RESYNC_DONE:
3407 OBD_FREE(data, data_size);
3409 case LL_LEASE_LAYOUT_MERGE:
3410 case LL_LEASE_LAYOUT_SPLIT:
3414 case LL_LEASE_PCC_ATTACH:
3417 rc = pcc_readwrite_attach_fini(file, inode,
3418 param.pa_layout_gen,
3425 rc = ll_lease_type_from_fmode(fmode);
3429 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
3432 struct inode *inode = file_inode(file);
3433 struct ll_inode_info *lli = ll_i2info(inode);
3434 struct ll_file_data *fd = file->private_data;
3435 struct obd_client_handle *och = NULL;
3436 __u64 open_flags = 0;
3442 switch (ioc->lil_mode) {
3443 case LL_LEASE_WRLCK:
3444 if (!(file->f_mode & FMODE_WRITE))
3446 fmode = FMODE_WRITE;
3448 case LL_LEASE_RDLCK:
3449 if (!(file->f_mode & FMODE_READ))
3453 case LL_LEASE_UNLCK:
3454 RETURN(ll_file_unlock_lease(file, ioc, arg));
3459 CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
3461 /* apply for lease */
3462 if (ioc->lil_flags & LL_LEASE_RESYNC)
3463 open_flags = MDS_OPEN_RESYNC;
3464 och = ll_lease_open(inode, file, fmode, open_flags);
3466 RETURN(PTR_ERR(och));
3468 if (ioc->lil_flags & LL_LEASE_RESYNC) {
3469 rc = ll_lease_file_resync(och, inode, arg);
3471 ll_lease_close(och, inode, NULL);
3474 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
3476 ll_lease_close(och, inode, NULL);
3482 mutex_lock(&lli->lli_och_mutex);
3483 if (fd->fd_lease_och == NULL) {
3484 fd->fd_lease_och = och;
3487 mutex_unlock(&lli->lli_och_mutex);
3489 /* impossible now that only excl is supported for now */
3490 ll_lease_close(och, inode, &lease_broken);
3496 static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
3498 struct ll_inode_info *lli = ll_i2info(inode);
3499 struct ll_sb_info *sbi = ll_i2sbi(inode);
3500 __u64 now = ktime_get_real_seconds();
3503 spin_lock(&lli->lli_heat_lock);
3504 heat->lh_flags = lli->lli_heat_flags;
3505 for (i = 0; i < heat->lh_count; i++)
3506 heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
3507 now, sbi->ll_heat_decay_weight,
3508 sbi->ll_heat_period_second);
3509 spin_unlock(&lli->lli_heat_lock);
3512 static int ll_heat_set(struct inode *inode, enum lu_heat_flag flags)
3514 struct ll_inode_info *lli = ll_i2info(inode);
3517 spin_lock(&lli->lli_heat_lock);
3518 if (flags & LU_HEAT_FLAG_CLEAR)
3519 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
3521 if (flags & LU_HEAT_FLAG_OFF)
3522 lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
3524 lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
3526 spin_unlock(&lli->lli_heat_lock);
3532 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3534 struct inode *inode = file_inode(file);
3535 struct ll_file_data *fd = file->private_data;
3539 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
3540 PFID(ll_inode2fid(inode)), inode, cmd);
3541 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
3543 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
3544 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
3548 case LL_IOC_GETFLAGS:
3549 /* Get the current value of the file flags */
3550 return put_user(fd->fd_flags, (int __user *)arg);
3551 case LL_IOC_SETFLAGS:
3552 case LL_IOC_CLRFLAGS:
3553 /* Set or clear specific file flags */
3554 /* XXX This probably needs checks to ensure the flags are
3555 * not abused, and to handle any flag side effects.
3557 if (get_user(flags, (int __user *) arg))
3560 if (cmd == LL_IOC_SETFLAGS) {
3561 if ((flags & LL_FILE_IGNORE_LOCK) &&
3562 !(file->f_flags & O_DIRECT)) {
3563 CERROR("%s: unable to disable locking on "
3564 "non-O_DIRECT file\n", current->comm);
3568 fd->fd_flags |= flags;
3570 fd->fd_flags &= ~flags;
3573 case LL_IOC_LOV_SETSTRIPE:
3574 case LL_IOC_LOV_SETSTRIPE_NEW:
3575 RETURN(ll_lov_setstripe(inode, file, (void __user *)arg));
3576 case LL_IOC_LOV_SETEA:
3577 RETURN(ll_lov_setea(inode, file, (void __user *)arg));
3578 case LL_IOC_LOV_SWAP_LAYOUTS: {
3580 struct lustre_swap_layouts lsl;
3582 if (copy_from_user(&lsl, (char __user *)arg,
3583 sizeof(struct lustre_swap_layouts)))
3586 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
3589 file2 = fget(lsl.sl_fd);
3593 /* O_WRONLY or O_RDWR */
3594 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
3595 GOTO(out, rc = -EPERM);
3597 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
3598 struct inode *inode2;
3599 struct ll_inode_info *lli;
3600 struct obd_client_handle *och = NULL;
3602 lli = ll_i2info(inode);
3603 mutex_lock(&lli->lli_och_mutex);
3604 if (fd->fd_lease_och != NULL) {
3605 och = fd->fd_lease_och;
3606 fd->fd_lease_och = NULL;
3608 mutex_unlock(&lli->lli_och_mutex);
3610 GOTO(out, rc = -ENOLCK);
3611 inode2 = file_inode(file2);
3612 rc = ll_swap_layouts_close(och, inode, inode2);
3614 rc = ll_swap_layouts(file, file2, &lsl);
3620 case LL_IOC_LOV_GETSTRIPE:
3621 case LL_IOC_LOV_GETSTRIPE_NEW:
3622 RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
3623 case FS_IOC_GETFLAGS:
3624 case FS_IOC_SETFLAGS:
3625 RETURN(ll_iocontrol(inode, file, cmd, arg));
3626 case FSFILT_IOC_GETVERSION:
3627 case FS_IOC_GETVERSION:
3628 RETURN(put_user(inode->i_generation, (int __user *)arg));
3629 /* We need to special case any other ioctls we want to handle,
3630 * to send them to the MDS/OST as appropriate and to properly
3631 * network encode the arg field. */
3632 case FS_IOC_SETVERSION:
3635 case LL_IOC_GROUP_LOCK:
3636 RETURN(ll_get_grouplock(inode, file, arg));
3637 case LL_IOC_GROUP_UNLOCK:
3638 RETURN(ll_put_grouplock(inode, file, arg));
3639 case IOC_OBD_STATFS:
3640 RETURN(ll_obd_statfs(inode, (void __user *)arg));
3642 case LL_IOC_FLUSHCTX:
3643 RETURN(ll_flush_ctx(inode));
3644 case LL_IOC_PATH2FID: {
3645 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
3646 sizeof(struct lu_fid)))
3651 case LL_IOC_GETPARENT:
3652 RETURN(ll_getparent(file, (struct getparent __user *)arg));
3654 case OBD_IOC_FID2PATH:
3655 RETURN(ll_fid2path(inode, (void __user *)arg));
3656 case LL_IOC_DATA_VERSION: {
3657 struct ioc_data_version idv;
3660 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
3663 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
3664 rc = ll_ioc_data_version(inode, &idv);
3667 copy_to_user((char __user *)arg, &idv, sizeof(idv)))
3673 case LL_IOC_GET_MDTIDX: {
3676 mdtidx = ll_get_mdt_idx(inode);
3680 if (put_user((int)mdtidx, (int __user *)arg))
3685 case OBD_IOC_GETDTNAME:
3686 case OBD_IOC_GETMDNAME:
3687 RETURN(ll_get_obd_name(inode, cmd, arg));
3688 case LL_IOC_HSM_STATE_GET: {
3689 struct md_op_data *op_data;
3690 struct hsm_user_state *hus;
3697 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3698 LUSTRE_OPC_ANY, hus);
3699 if (IS_ERR(op_data)) {
3701 RETURN(PTR_ERR(op_data));
3704 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
3707 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
3710 ll_finish_md_op_data(op_data);
3714 case LL_IOC_HSM_STATE_SET: {
3715 struct hsm_state_set *hss;
3722 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
3727 rc = ll_hsm_state_set(inode, hss);
3732 case LL_IOC_HSM_ACTION: {
3733 struct md_op_data *op_data;
3734 struct hsm_current_action *hca;
3741 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3742 LUSTRE_OPC_ANY, hca);
3743 if (IS_ERR(op_data)) {
3745 RETURN(PTR_ERR(op_data));
3748 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
3751 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
3754 ll_finish_md_op_data(op_data);
3758 case LL_IOC_SET_LEASE_OLD: {
3759 struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
3761 RETURN(ll_file_set_lease(file, &ioc, 0));
3763 case LL_IOC_SET_LEASE: {
3764 struct ll_ioc_lease ioc;
3766 if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
3769 RETURN(ll_file_set_lease(file, &ioc, arg));
3771 case LL_IOC_GET_LEASE: {
3772 struct ll_inode_info *lli = ll_i2info(inode);
3773 struct ldlm_lock *lock = NULL;
3776 mutex_lock(&lli->lli_och_mutex);
3777 if (fd->fd_lease_och != NULL) {
3778 struct obd_client_handle *och = fd->fd_lease_och;
3780 lock = ldlm_handle2lock(&och->och_lease_handle);
3782 lock_res_and_lock(lock);
3783 if (!ldlm_is_cancel(lock))
3784 fmode = och->och_flags;
3786 unlock_res_and_lock(lock);
3787 LDLM_LOCK_PUT(lock);
3790 mutex_unlock(&lli->lli_och_mutex);
3792 RETURN(ll_lease_type_from_fmode(fmode));
3794 case LL_IOC_HSM_IMPORT: {
3795 struct hsm_user_import *hui;
3801 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
3806 rc = ll_hsm_import(inode, file, hui);
3811 case LL_IOC_FUTIMES_3: {
3812 struct ll_futimes_3 lfu;
3814 if (copy_from_user(&lfu,
3815 (const struct ll_futimes_3 __user *)arg,
3819 RETURN(ll_file_futimes_3(file, &lfu));
3821 case LL_IOC_LADVISE: {
3822 struct llapi_ladvise_hdr *k_ladvise_hdr;
3823 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
3826 int alloc_size = sizeof(*k_ladvise_hdr);
3829 u_ladvise_hdr = (void __user *)arg;
3830 OBD_ALLOC_PTR(k_ladvise_hdr);
3831 if (k_ladvise_hdr == NULL)
3834 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
3835 GOTO(out_ladvise, rc = -EFAULT);
3837 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
3838 k_ladvise_hdr->lah_count < 1)
3839 GOTO(out_ladvise, rc = -EINVAL);
3841 num_advise = k_ladvise_hdr->lah_count;
3842 if (num_advise >= LAH_COUNT_MAX)
3843 GOTO(out_ladvise, rc = -EFBIG);
3845 OBD_FREE_PTR(k_ladvise_hdr);
3846 alloc_size = offsetof(typeof(*k_ladvise_hdr),
3847 lah_advise[num_advise]);
3848 OBD_ALLOC(k_ladvise_hdr, alloc_size);
3849 if (k_ladvise_hdr == NULL)
3853 * TODO: submit multiple advices to one server in a single RPC
3855 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
3856 GOTO(out_ladvise, rc = -EFAULT);
3858 for (i = 0; i < num_advise; i++) {
3859 struct llapi_lu_ladvise *k_ladvise =
3860 &k_ladvise_hdr->lah_advise[i];
3861 struct llapi_lu_ladvise __user *u_ladvise =
3862 &u_ladvise_hdr->lah_advise[i];
3864 rc = ll_ladvise_sanity(inode, k_ladvise);
3866 GOTO(out_ladvise, rc);
3868 switch (k_ladvise->lla_advice) {
3869 case LU_LADVISE_LOCKNOEXPAND:
3870 rc = ll_lock_noexpand(file,
3871 k_ladvise->lla_peradvice_flags);
3872 GOTO(out_ladvise, rc);
3873 case LU_LADVISE_LOCKAHEAD:
3875 rc = ll_file_lock_ahead(file, k_ladvise);
3878 GOTO(out_ladvise, rc);
3881 &u_ladvise->lla_lockahead_result))
3882 GOTO(out_ladvise, rc = -EFAULT);
3885 rc = ll_ladvise(inode, file,
3886 k_ladvise_hdr->lah_flags,
3889 GOTO(out_ladvise, rc);
3896 OBD_FREE(k_ladvise_hdr, alloc_size);
3899 case LL_IOC_FLR_SET_MIRROR: {
3900 /* mirror I/O must be direct to avoid polluting page cache
3902 if (!(file->f_flags & O_DIRECT))
3905 fd->fd_designated_mirror = (__u32)arg;
3908 case FS_IOC_FSGETXATTR:
3909 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
3910 case FS_IOC_FSSETXATTR:
3911 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
3913 RETURN(put_user(PAGE_SIZE, (int __user *)arg));
3914 case LL_IOC_HEAT_GET: {
3915 struct lu_heat uheat;
3916 struct lu_heat *heat;
3919 if (copy_from_user(&uheat, (void __user *)arg, sizeof(uheat)))
3922 if (uheat.lh_count > OBD_HEAT_COUNT)
3923 uheat.lh_count = OBD_HEAT_COUNT;
3925 size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
3926 OBD_ALLOC(heat, size);
3930 heat->lh_count = uheat.lh_count;
3931 ll_heat_get(inode, heat);
3932 rc = copy_to_user((char __user *)arg, heat, size);
3933 OBD_FREE(heat, size);
3934 RETURN(rc ? -EFAULT : 0);
3936 case LL_IOC_HEAT_SET: {
3939 if (copy_from_user(&flags, (void __user *)arg, sizeof(flags)))
3942 rc = ll_heat_set(inode, flags);
3945 case LL_IOC_PCC_DETACH: {
3946 struct lu_pcc_detach *detach;
3948 OBD_ALLOC_PTR(detach);
3952 if (copy_from_user(detach,
3953 (const struct lu_pcc_detach __user *)arg,
3955 GOTO(out_detach_free, rc = -EFAULT);
3957 if (!S_ISREG(inode->i_mode))
3958 GOTO(out_detach_free, rc = -EINVAL);
3960 if (!inode_owner_or_capable(inode))
3961 GOTO(out_detach_free, rc = -EPERM);
3963 rc = pcc_ioctl_detach(inode, detach->pccd_opt);
3965 OBD_FREE_PTR(detach);
3968 case LL_IOC_PCC_STATE: {
3969 struct lu_pcc_state __user *ustate =
3970 (struct lu_pcc_state __user *)arg;
3971 struct lu_pcc_state *state;
3973 OBD_ALLOC_PTR(state);
3977 if (copy_from_user(state, ustate, sizeof(*state)))
3978 GOTO(out_state, rc = -EFAULT);
3980 rc = pcc_ioctl_state(file, inode, state);
3982 GOTO(out_state, rc);
3984 if (copy_to_user(ustate, state, sizeof(*state)))
3985 GOTO(out_state, rc = -EFAULT);
3988 OBD_FREE_PTR(state);
3992 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
3993 (void __user *)arg));
3997 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
3999 struct inode *inode = file_inode(file);
4000 loff_t retval, eof = 0;
4001 ktime_t kstart = ktime_get();
4004 retval = offset + ((origin == SEEK_END) ? i_size_read(inode) :
4005 (origin == SEEK_CUR) ? file->f_pos : 0);
4006 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
4007 PFID(ll_inode2fid(inode)), inode, retval, retval,
4010 if (origin == SEEK_END || origin == SEEK_HOLE || origin == SEEK_DATA) {
4011 retval = ll_glimpse_size(inode);
4014 eof = i_size_read(inode);
4017 retval = generic_file_llseek_size(file, offset, origin,
4018 ll_file_maxbytes(inode), eof);
4020 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK,
4021 ktime_us_delta(ktime_get(), kstart));
4025 static int ll_flush(struct file *file, fl_owner_t id)
4027 struct inode *inode = file_inode(file);
4028 struct ll_inode_info *lli = ll_i2info(inode);
4029 struct ll_file_data *fd = file->private_data;
4032 LASSERT(!S_ISDIR(inode->i_mode));
4034 /* catch async errors that were recorded back when async writeback
4035 * failed for pages in this mapping. */
4036 rc = lli->lli_async_rc;
4037 lli->lli_async_rc = 0;
4038 if (lli->lli_clob != NULL) {
4039 err = lov_read_and_clear_async_rc(lli->lli_clob);
4044 /* The application has been told write failure already.
4045 * Do not report failure again. */
4046 if (fd->fd_write_failed)
4048 return rc ? -EIO : 0;
4052 * Called to make sure a portion of file has been written out.
4053 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
4055 * Return how many pages have been written.
4057 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
4058 enum cl_fsync_mode mode, int ignore_layout)
4062 struct cl_fsync_io *fio;
4067 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
4068 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
4071 env = cl_env_get(&refcheck);
4073 RETURN(PTR_ERR(env));
4075 io = vvp_env_thread_io(env);
4076 io->ci_obj = ll_i2info(inode)->lli_clob;
4077 io->ci_ignore_layout = ignore_layout;
4079 /* initialize parameters for sync */
4080 fio = &io->u.ci_fsync;
4081 fio->fi_start = start;
4083 fio->fi_fid = ll_inode2fid(inode);
4084 fio->fi_mode = mode;
4085 fio->fi_nr_written = 0;
4087 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
4088 result = cl_io_loop(env, io);
4090 result = io->ci_result;
4092 result = fio->fi_nr_written;
4093 cl_io_fini(env, io);
4094 cl_env_put(env, &refcheck);
4100 * When dentry is provided (the 'else' case), file_dentry() may be
4101 * null and dentry must be used directly rather than pulled from
4102 * file_dentry() as is done otherwise.
4105 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
4107 struct dentry *dentry = file_dentry(file);
4108 struct inode *inode = dentry->d_inode;
4109 struct ll_inode_info *lli = ll_i2info(inode);
4110 struct ptlrpc_request *req;
4111 ktime_t kstart = ktime_get();
4117 "VFS Op:inode="DFID"(%p), start %lld, end %lld, datasync %d\n",
4118 PFID(ll_inode2fid(inode)), inode, start, end, datasync);
4120 /* fsync's caller has already called _fdata{sync,write}, we want
4121 * that IO to finish before calling the osc and mdc sync methods */
4122 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
4125 /* catch async errors that were recorded back when async writeback
4126 * failed for pages in this mapping. */
4127 if (!S_ISDIR(inode->i_mode)) {
4128 err = lli->lli_async_rc;
4129 lli->lli_async_rc = 0;
4132 if (lli->lli_clob != NULL) {
4133 err = lov_read_and_clear_async_rc(lli->lli_clob);
4139 err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
4143 ptlrpc_req_finished(req);
4145 if (S_ISREG(inode->i_mode)) {
4146 struct ll_file_data *fd = file->private_data;
4149 /* Sync metadata on MDT first, and then sync the cached data
4152 err = pcc_fsync(file, start, end, datasync, &cached);
4154 err = cl_sync_file_range(inode, start, end,
4156 if (rc == 0 && err < 0)
4159 fd->fd_write_failed = true;
4161 fd->fd_write_failed = false;
4164 inode_unlock(inode);
4167 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC,
4168 ktime_us_delta(ktime_get(), kstart));
4173 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
4175 struct inode *inode = file_inode(file);
4176 struct ll_sb_info *sbi = ll_i2sbi(inode);
4177 struct ldlm_enqueue_info einfo = {
4178 .ei_type = LDLM_FLOCK,
4179 .ei_cb_cp = ldlm_flock_completion_ast,
4180 .ei_cbdata = file_lock,
4182 struct md_op_data *op_data;
4183 struct lustre_handle lockh = { 0 };
4184 union ldlm_policy_data flock = { { 0 } };
4185 int fl_type = file_lock->fl_type;
4186 ktime_t kstart = ktime_get();
4192 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
4193 PFID(ll_inode2fid(inode)), file_lock);
4195 if (file_lock->fl_flags & FL_FLOCK) {
4196 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
4197 /* flocks are whole-file locks */
4198 flock.l_flock.end = OFFSET_MAX;
4199 /* For flocks owner is determined by the local file desctiptor*/
4200 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
4201 } else if (file_lock->fl_flags & FL_POSIX) {
4202 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
4203 flock.l_flock.start = file_lock->fl_start;
4204 flock.l_flock.end = file_lock->fl_end;
4208 flock.l_flock.pid = file_lock->fl_pid;
4210 #if defined(HAVE_LM_COMPARE_OWNER) || defined(lm_compare_owner)
4211 /* Somewhat ugly workaround for svc lockd.
4212 * lockd installs custom fl_lmops->lm_compare_owner that checks
4213 * for the fl_owner to be the same (which it always is on local node
4214 * I guess between lockd processes) and then compares pid.
4215 * As such we assign pid to the owner field to make it all work,
4216 * conflict with normal locks is unlikely since pid space and
4217 * pointer space for current->files are not intersecting */
4218 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
4219 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
4224 einfo.ei_mode = LCK_PR;
4227 /* An unlock request may or may not have any relation to
4228 * existing locks so we may not be able to pass a lock handle
4229 * via a normal ldlm_lock_cancel() request. The request may even
4230 * unlock a byte range in the middle of an existing lock. In
4231 * order to process an unlock request we need all of the same
4232 * information that is given with a normal read or write record
4233 * lock request. To avoid creating another ldlm unlock (cancel)
4234 * message we'll treat a LCK_NL flock request as an unlock. */
4235 einfo.ei_mode = LCK_NL;
4238 einfo.ei_mode = LCK_PW;
4241 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
4256 flags = LDLM_FL_BLOCK_NOWAIT;
4262 flags = LDLM_FL_TEST_LOCK;
4265 CERROR("unknown fcntl lock command: %d\n", cmd);
4269 /* Save the old mode so that if the mode in the lock changes we
4270 * can decrement the appropriate reader or writer refcount. */
4271 file_lock->fl_type = einfo.ei_mode;
4273 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4274 LUSTRE_OPC_ANY, NULL);
4275 if (IS_ERR(op_data))
4276 RETURN(PTR_ERR(op_data));
4278 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
4279 "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
4280 flock.l_flock.pid, flags, einfo.ei_mode,
4281 flock.l_flock.start, flock.l_flock.end);
4283 rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
4286 /* Restore the file lock type if not TEST lock. */
4287 if (!(flags & LDLM_FL_TEST_LOCK))
4288 file_lock->fl_type = fl_type;
4290 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
4291 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
4292 !(flags & LDLM_FL_TEST_LOCK))
4293 rc2 = locks_lock_file_wait(file, file_lock);
4295 if ((file_lock->fl_flags & FL_FLOCK) &&
4296 (rc == 0 || file_lock->fl_type == F_UNLCK))
4297 rc2 = flock_lock_file_wait(file, file_lock);
4298 if ((file_lock->fl_flags & FL_POSIX) &&
4299 (rc == 0 || file_lock->fl_type == F_UNLCK) &&
4300 !(flags & LDLM_FL_TEST_LOCK))
4301 rc2 = posix_lock_file_wait(file, file_lock);
4302 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
4304 if (rc2 && file_lock->fl_type != F_UNLCK) {
4305 einfo.ei_mode = LCK_NL;
4306 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
4311 ll_finish_md_op_data(op_data);
4314 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK,
4315 ktime_us_delta(ktime_get(), kstart));
4319 int ll_get_fid_by_name(struct inode *parent, const char *name,
4320 int namelen, struct lu_fid *fid,
4321 struct inode **inode)
4323 struct md_op_data *op_data = NULL;
4324 struct mdt_body *body;
4325 struct ptlrpc_request *req;
4329 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
4330 LUSTRE_OPC_ANY, NULL);
4331 if (IS_ERR(op_data))
4332 RETURN(PTR_ERR(op_data));
4334 op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
4335 rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
4336 ll_finish_md_op_data(op_data);
4340 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4342 GOTO(out_req, rc = -EFAULT);
4344 *fid = body->mbo_fid1;
4347 rc = ll_prep_inode(inode, req, parent->i_sb, NULL);
4349 ptlrpc_req_finished(req);
4353 int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
4356 struct dentry *dchild = NULL;
4357 struct inode *child_inode = NULL;
4358 struct md_op_data *op_data;
4359 struct ptlrpc_request *request = NULL;
4360 struct obd_client_handle *och = NULL;
4362 struct mdt_body *body;
4363 __u64 data_version = 0;
4364 size_t namelen = strlen(name);
4365 int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
4369 CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
4370 PFID(ll_inode2fid(parent)), name,
4371 lum->lum_stripe_offset, lum->lum_stripe_count);
4373 if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
4374 lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
4375 lustre_swab_lmv_user_md(lum);
4377 /* Get child FID first */
4378 qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
4381 dchild = d_lookup(file_dentry(file), &qstr);
4383 if (dchild->d_inode)
4384 child_inode = igrab(dchild->d_inode);
4389 rc = ll_get_fid_by_name(parent, name, namelen, NULL,
4398 if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
4399 OBD_CONNECT2_DIR_MIGRATE)) {
4400 if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
4401 ll_dir_striped(child_inode)) {
4402 CERROR("%s: MDT doesn't support stripe directory "
4403 "migration!\n", ll_i2sbi(parent)->ll_fsname);
4404 GOTO(out_iput, rc = -EOPNOTSUPP);
4409 * lfs migrate command needs to be blocked on the client
4410 * by checking the migrate FID against the FID of the
4413 if (child_inode == parent->i_sb->s_root->d_inode)
4414 GOTO(out_iput, rc = -EINVAL);
4416 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
4417 child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
4418 if (IS_ERR(op_data))
4419 GOTO(out_iput, rc = PTR_ERR(op_data));
4421 inode_lock(child_inode);
4422 op_data->op_fid3 = *ll_inode2fid(child_inode);
4423 if (!fid_is_sane(&op_data->op_fid3)) {
4424 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
4425 ll_i2sbi(parent)->ll_fsname, name,
4426 PFID(&op_data->op_fid3));
4427 GOTO(out_unlock, rc = -EINVAL);
4430 op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
4431 op_data->op_data = lum;
4432 op_data->op_data_size = lumlen;
4435 if (S_ISREG(child_inode->i_mode)) {
4436 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
4440 GOTO(out_unlock, rc);
4443 rc = ll_data_version(child_inode, &data_version,
4446 GOTO(out_close, rc);
4448 op_data->op_open_handle = och->och_open_handle;
4449 op_data->op_data_version = data_version;
4450 op_data->op_lease_handle = och->och_lease_handle;
4451 op_data->op_bias |= MDS_CLOSE_MIGRATE;
4453 spin_lock(&och->och_mod->mod_open_req->rq_lock);
4454 och->och_mod->mod_open_req->rq_replay = 0;
4455 spin_unlock(&och->och_mod->mod_open_req->rq_lock);
4458 rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name, namelen,
4459 name, namelen, &request);
4461 LASSERT(request != NULL);
4462 ll_update_times(request, parent);
4465 if (rc == 0 || rc == -EAGAIN) {
4466 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
4467 LASSERT(body != NULL);
4469 /* If the server does release layout lock, then we cleanup
4470 * the client och here, otherwise release it in out_close: */
4471 if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
4472 obd_mod_put(och->och_mod);
4473 md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
4475 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
4481 if (request != NULL) {
4482 ptlrpc_req_finished(request);
4486 /* Try again if the lease has cancelled. */
4487 if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
4492 ll_lease_close(och, child_inode, NULL);
4494 clear_nlink(child_inode);
4496 inode_unlock(child_inode);
4497 ll_finish_md_op_data(op_data);
4504 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
4506 struct ll_file_data *fd = file->private_data;
4510 * In order to avoid flood of warning messages, only print one message
4511 * for one file. And the entire message rate on the client is limited
4512 * by CDEBUG_LIMIT too.
4514 if (!(fd->fd_flags & LL_FILE_FLOCK_WARNING)) {
4515 fd->fd_flags |= LL_FILE_FLOCK_WARNING;
4516 CDEBUG_LIMIT(D_TTY | D_CONSOLE,
4517 "flock disabled, mount with '-o [local]flock' to enable\r\n");
4523 * test if some locks matching bits and l_req_mode are acquired
4524 * - bits can be in different locks
4525 * - if found clear the common lock bits in *bits
4526 * - the bits not found, are kept in *bits
4528 * \param bits [IN] searched lock bits [IN]
4529 * \param l_req_mode [IN] searched lock mode
4530 * \retval boolean, true iff all bits are found
4532 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
4534 struct lustre_handle lockh;
4535 union ldlm_policy_data policy;
4536 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
4537 (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
4546 fid = &ll_i2info(inode)->lli_fid;
4547 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
4548 ldlm_lockname[mode]);
4550 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
4551 for (i = 0; i < MDS_INODELOCK_NUMBITS && *bits != 0; i++) {
4552 policy.l_inodebits.bits = *bits & BIT(i);
4553 if (policy.l_inodebits.bits == 0)
4556 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
4557 &policy, mode, &lockh)) {
4558 struct ldlm_lock *lock;
4560 lock = ldlm_handle2lock(&lockh);
4563 ~(lock->l_policy_data.l_inodebits.bits);
4564 LDLM_LOCK_PUT(lock);
4566 *bits &= ~policy.l_inodebits.bits;
4573 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
4574 struct lustre_handle *lockh, __u64 flags,
4575 enum ldlm_mode mode)
4577 union ldlm_policy_data policy = { .l_inodebits = { bits } };
4582 fid = &ll_i2info(inode)->lli_fid;
4583 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
4585 rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
4586 fid, LDLM_IBITS, &policy, mode, lockh);
4591 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
4593 /* Already unlinked. Just update nlink and return success */
4594 if (rc == -ENOENT) {
4596 /* If it is striped directory, and there is bad stripe
4597 * Let's revalidate the dentry again, instead of returning
4599 if (ll_dir_striped(inode))
4602 /* This path cannot be hit for regular files unless in
4603 * case of obscure races, so no need to to validate
4605 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
4607 } else if (rc != 0) {
4608 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
4609 "%s: revalidate FID "DFID" error: rc = %d\n",
4610 ll_i2sbi(inode)->ll_fsname,
4611 PFID(ll_inode2fid(inode)), rc);
4617 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
4619 struct inode *inode = dentry->d_inode;
4620 struct obd_export *exp = ll_i2mdexp(inode);
4621 struct lookup_intent oit = {
4624 struct ptlrpc_request *req = NULL;
4625 struct md_op_data *op_data;
4629 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
4630 PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
4632 /* Call getattr by fid, so do not provide name at all. */
4633 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
4634 LUSTRE_OPC_ANY, NULL);
4635 if (IS_ERR(op_data))
4636 RETURN(PTR_ERR(op_data));
4638 rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
4639 ll_finish_md_op_data(op_data);
4641 rc = ll_inode_revalidate_fini(inode, rc);
4645 rc = ll_revalidate_it_finish(req, &oit, dentry);
4647 ll_intent_release(&oit);
4651 /* Unlinked? Unhash dentry, so it is not picked up later by
4652 * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
4653 * here to preserve get_cwd functionality on 2.6.
4655 if (!dentry->d_inode->i_nlink) {
4656 spin_lock(&inode->i_lock);
4657 d_lustre_invalidate(dentry, 0);
4658 spin_unlock(&inode->i_lock);
4661 ll_lookup_finish_locks(&oit, dentry);
4663 ptlrpc_req_finished(req);
4668 static int ll_merge_md_attr(struct inode *inode)
4670 struct ll_inode_info *lli = ll_i2info(inode);
4671 struct cl_attr attr = { 0 };
4674 LASSERT(lli->lli_lsm_md != NULL);
4676 if (!lmv_dir_striped(lli->lli_lsm_md))
4679 down_read(&lli->lli_lsm_sem);
4680 rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
4681 &attr, ll_md_blocking_ast);
4682 up_read(&lli->lli_lsm_sem);
4686 set_nlink(inode, attr.cat_nlink);
4687 inode->i_blocks = attr.cat_blocks;
4688 i_size_write(inode, attr.cat_size);
4690 ll_i2info(inode)->lli_atime = attr.cat_atime;
4691 ll_i2info(inode)->lli_mtime = attr.cat_mtime;
4692 ll_i2info(inode)->lli_ctime = attr.cat_ctime;
4697 int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
4700 struct inode *inode = de->d_inode;
4701 struct ll_sb_info *sbi = ll_i2sbi(inode);
4702 struct ll_inode_info *lli = ll_i2info(inode);
4703 struct inode *dir = de->d_parent->d_inode;
4704 bool need_glimpse = true;
4705 ktime_t kstart = ktime_get();
4708 /* The OST object(s) determine the file size, blocks and mtime. */
4709 if (!(request_mask & STATX_SIZE || request_mask & STATX_BLOCKS ||
4710 request_mask & STATX_MTIME))
4711 need_glimpse = false;
4713 if (dentry_may_statahead(dir, de))
4714 ll_start_statahead(dir, de, need_glimpse &&
4715 !(flags & AT_STATX_DONT_SYNC));
4717 if (flags & AT_STATX_DONT_SYNC)
4718 GOTO(fill_attr, rc = 0);
4720 rc = ll_inode_revalidate(de, IT_GETATTR);
4724 if (S_ISREG(inode->i_mode)) {
4728 GOTO(fill_attr, rc);
4730 rc = pcc_inode_getattr(inode, request_mask, flags, &cached);
4731 if (cached && rc < 0)
4735 GOTO(fill_attr, rc);
4738 * If the returned attr is masked with OBD_MD_FLSIZE &
4739 * OBD_MD_FLBLOCKS & OBD_MD_FLMTIME, it means that the file size
4740 * or blocks obtained from MDT is strictly correct, and the file
4741 * is usually not being modified by clients, and the [a|m|c]time
4742 * got from MDT is also strictly correct.
4743 * Under this circumstance, it does not need to send glimpse
4744 * RPCs to OSTs for file attributes such as the size and blocks.
4746 if (lli->lli_attr_valid & OBD_MD_FLSIZE &&
4747 lli->lli_attr_valid & OBD_MD_FLBLOCKS &&
4748 lli->lli_attr_valid & OBD_MD_FLMTIME) {
4749 inode->i_mtime.tv_sec = lli->lli_mtime;
4750 if (lli->lli_attr_valid & OBD_MD_FLATIME)
4751 inode->i_atime.tv_sec = lli->lli_atime;
4752 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
4753 inode->i_ctime.tv_sec = lli->lli_ctime;
4754 GOTO(fill_attr, rc);
4757 /* In case of restore, the MDT has the right size and has
4758 * already send it back without granting the layout lock,
4759 * inode is up-to-date so glimpse is useless.
4760 * Also to glimpse we need the layout, in case of a running
4761 * restore the MDT holds the layout lock so the glimpse will
4762 * block up to the end of restore (getattr will block)
4764 if (!ll_file_test_flag(lli, LLIF_FILE_RESTORING)) {
4765 rc = ll_glimpse_size(inode);
4770 /* If object isn't regular a file then don't validate size. */
4771 if (ll_dir_striped(inode)) {
4772 rc = ll_merge_md_attr(inode);
4777 if (lli->lli_attr_valid & OBD_MD_FLATIME)
4778 inode->i_atime.tv_sec = lli->lli_atime;
4779 if (lli->lli_attr_valid & OBD_MD_FLMTIME)
4780 inode->i_mtime.tv_sec = lli->lli_mtime;
4781 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
4782 inode->i_ctime.tv_sec = lli->lli_ctime;
4786 OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
4788 if (ll_need_32bit_api(sbi)) {
4789 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
4790 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
4791 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
4793 stat->ino = inode->i_ino;
4794 stat->dev = inode->i_sb->s_dev;
4795 stat->rdev = inode->i_rdev;
4798 stat->mode = inode->i_mode;
4799 stat->uid = inode->i_uid;
4800 stat->gid = inode->i_gid;
4801 stat->atime = inode->i_atime;
4802 stat->mtime = inode->i_mtime;
4803 stat->ctime = inode->i_ctime;
4804 /* stat->blksize is used to tell about preferred IO size */
4805 if (sbi->ll_stat_blksize)
4806 stat->blksize = sbi->ll_stat_blksize;
4807 else if (S_ISREG(inode->i_mode))
4808 stat->blksize = 1 << min(PTLRPC_MAX_BRW_BITS + 1,
4809 LL_MAX_BLKSIZE_BITS);
4811 stat->blksize = 1 << inode->i_sb->s_blocksize_bits;
4813 stat->nlink = inode->i_nlink;
4814 stat->size = i_size_read(inode);
4815 stat->blocks = inode->i_blocks;
4817 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
4818 if (flags & AT_STATX_DONT_SYNC) {
4819 if (stat->size == 0 &&
4820 lli->lli_attr_valid & OBD_MD_FLLAZYSIZE)
4821 stat->size = lli->lli_lazysize;
4822 if (stat->blocks == 0 &&
4823 lli->lli_attr_valid & OBD_MD_FLLAZYBLOCKS)
4824 stat->blocks = lli->lli_lazyblocks;
4827 if (lli->lli_attr_valid & OBD_MD_FLBTIME) {
4828 stat->result_mask |= STATX_BTIME;
4829 stat->btime.tv_sec = lli->lli_btime;
4832 stat->attributes_mask = STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
4833 stat->attributes |= ll_inode_to_ext_flags(inode->i_flags);
4834 stat->result_mask &= request_mask;
4837 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR,
4838 ktime_us_delta(ktime_get(), kstart));
4843 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
4844 int ll_getattr(const struct path *path, struct kstat *stat,
4845 u32 request_mask, unsigned int flags)
4847 return ll_getattr_dentry(path->dentry, stat, request_mask, flags);
4850 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
4852 return ll_getattr_dentry(de, stat, STATX_BASIC_STATS,
4853 AT_STATX_SYNC_AS_STAT);
4857 int cl_falloc(struct inode *inode, int mode, loff_t offset, loff_t len)
4862 int rc; loff_t sa_falloc_end;
4863 loff_t size = i_size_read(inode);
4867 env = cl_env_get(&refcheck);
4869 RETURN(PTR_ERR(env));
4871 io = vvp_env_thread_io(env);
4872 io->ci_obj = ll_i2info(inode)->lli_clob;
4873 io->ci_verify_layout = 1;
4874 io->u.ci_setattr.sa_parent_fid = lu_object_fid(&io->ci_obj->co_lu);
4875 io->u.ci_setattr.sa_falloc_mode = mode;
4876 io->u.ci_setattr.sa_falloc_offset = offset;
4877 io->u.ci_setattr.sa_falloc_len = len;
4878 io->u.ci_setattr.sa_falloc_end = io->u.ci_setattr.sa_falloc_offset +
4879 io->u.ci_setattr.sa_falloc_len;
4880 io->u.ci_setattr.sa_subtype = CL_SETATTR_FALLOCATE;
4881 sa_falloc_end = io->u.ci_setattr.sa_falloc_end;
4882 if (sa_falloc_end > size) {
4883 /* Check new size against VFS/VM file size limit and rlimit */
4884 rc = inode_newsize_ok(inode, sa_falloc_end);
4887 if (sa_falloc_end > ll_file_maxbytes(inode)) {
4888 CDEBUG(D_INODE, "file size too large %llu > %llu\n",
4889 (unsigned long long)(sa_falloc_end),
4890 ll_file_maxbytes(inode));
4894 io->u.ci_setattr.sa_attr.lvb_size = sa_falloc_end;
4895 if (!(mode & FALLOC_FL_KEEP_SIZE))
4896 io->u.ci_setattr.sa_avalid |= ATTR_SIZE;
4898 io->u.ci_setattr.sa_attr.lvb_size = size;
4902 if (cl_io_init(env, io, CIT_SETATTR, io->ci_obj) == 0)
4903 rc = cl_io_loop(env, io);
4907 cl_io_fini(env, io);
4908 if (unlikely(io->ci_need_restart))
4912 cl_env_put(env, &refcheck);
4916 long ll_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
4918 struct inode *inode = filp->f_path.dentry->d_inode;
4922 * Only mode == 0 (which is standard prealloc) is supported now.
4923 * Punch is not supported yet.
4925 if (mode & ~FALLOC_FL_KEEP_SIZE)
4926 RETURN(-EOPNOTSUPP);
4928 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FALLOCATE, 1);
4930 rc = cl_falloc(inode, mode, offset, len);
4935 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4936 __u64 start, __u64 len)
4940 struct fiemap *fiemap;
4941 unsigned int extent_count = fieinfo->fi_extents_max;
4943 num_bytes = sizeof(*fiemap) + (extent_count *
4944 sizeof(struct fiemap_extent));
4945 OBD_ALLOC_LARGE(fiemap, num_bytes);
4950 fiemap->fm_flags = fieinfo->fi_flags;
4951 fiemap->fm_extent_count = fieinfo->fi_extents_max;
4952 fiemap->fm_start = start;
4953 fiemap->fm_length = len;
4954 if (extent_count > 0 &&
4955 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
4956 sizeof(struct fiemap_extent)) != 0)
4957 GOTO(out, rc = -EFAULT);
4959 rc = ll_do_fiemap(inode, fiemap, num_bytes);
4961 fieinfo->fi_flags = fiemap->fm_flags;
4962 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
4963 if (extent_count > 0 &&
4964 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
4965 fiemap->fm_mapped_extents *
4966 sizeof(struct fiemap_extent)) != 0)
4967 GOTO(out, rc = -EFAULT);
4969 OBD_FREE_LARGE(fiemap, num_bytes);
4973 struct posix_acl *ll_get_acl(struct inode *inode, int type)
4975 struct ll_inode_info *lli = ll_i2info(inode);
4976 struct posix_acl *acl = NULL;
4979 spin_lock(&lli->lli_lock);
4980 /* VFS' acl_permission_check->check_acl will release the refcount */
4981 acl = posix_acl_dup(lli->lli_posix_acl);
4982 spin_unlock(&lli->lli_lock);
4987 #ifdef HAVE_IOP_SET_ACL
4988 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
4989 int ll_set_acl(struct inode *inode, struct posix_acl *acl, int type)
4991 struct ll_sb_info *sbi = ll_i2sbi(inode);
4992 struct ptlrpc_request *req = NULL;
4993 const char *name = NULL;
4995 size_t value_size = 0;
5000 case ACL_TYPE_ACCESS:
5001 name = XATTR_NAME_POSIX_ACL_ACCESS;
5003 rc = posix_acl_update_mode(inode, &inode->i_mode, &acl);
5006 case ACL_TYPE_DEFAULT:
5007 name = XATTR_NAME_POSIX_ACL_DEFAULT;
5008 if (!S_ISDIR(inode->i_mode))
5009 rc = acl ? -EACCES : 0;
5020 value_size = posix_acl_xattr_size(acl->a_count);
5021 value = kmalloc(value_size, GFP_NOFS);
5023 GOTO(out, rc = -ENOMEM);
5025 rc = posix_acl_to_xattr(&init_user_ns, acl, value, value_size);
5027 GOTO(out_value, rc);
5030 rc = md_setxattr(sbi->ll_md_exp, ll_inode2fid(inode),
5031 value ? OBD_MD_FLXATTR : OBD_MD_FLXATTRRM,
5032 name, value, value_size, 0, 0, &req);
5034 ptlrpc_req_finished(req);
5039 forget_cached_acl(inode, type);
5041 set_cached_acl(inode, type, acl);
5044 #endif /* CONFIG_LUSTRE_FS_POSIX_ACL */
5045 #endif /* HAVE_IOP_SET_ACL */
5047 int ll_inode_permission(struct inode *inode, int mask)
5050 struct ll_sb_info *sbi;
5051 struct root_squash_info *squash;
5052 struct cred *cred = NULL;
5053 const struct cred *old_cred = NULL;
5055 bool squash_id = false;
5056 ktime_t kstart = ktime_get();
5060 if (mask & MAY_NOT_BLOCK)
5064 * as root inode are NOT getting validated in lookup operation,
5065 * need to do it before permission check.
5068 if (inode == inode->i_sb->s_root->d_inode) {
5069 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
5074 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
5075 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
5077 /* squash fsuid/fsgid if needed */
5078 sbi = ll_i2sbi(inode);
5079 squash = &sbi->ll_squash;
5080 if (unlikely(squash->rsi_uid != 0 &&
5081 uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
5082 !(sbi->ll_flags & LL_SBI_NOROOTSQUASH))) {
5086 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
5087 __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
5088 squash->rsi_uid, squash->rsi_gid);
5090 /* update current process's credentials
5091 * and FS capability */
5092 cred = prepare_creds();
5096 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
5097 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
5098 for (cap = 0; cap < sizeof(cfs_cap_t) * 8; cap++) {
5099 if (BIT(cap) & CFS_CAP_FS_MASK)
5100 cap_lower(cred->cap_effective, cap);
5102 old_cred = override_creds(cred);
5105 rc = generic_permission(inode, mask);
5106 /* restore current process's credentials and FS capability */
5108 revert_creds(old_cred);
5113 ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM,
5114 ktime_us_delta(ktime_get(), kstart));
5119 /* -o localflock - only provides locally consistent flock locks */
5120 struct file_operations ll_file_operations = {
5121 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5122 # ifdef HAVE_SYNC_READ_WRITE
5123 .read = new_sync_read,
5124 .write = new_sync_write,
5126 .read_iter = ll_file_read_iter,
5127 .write_iter = ll_file_write_iter,
5128 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5129 .read = ll_file_read,
5130 .aio_read = ll_file_aio_read,
5131 .write = ll_file_write,
5132 .aio_write = ll_file_aio_write,
5133 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5134 .unlocked_ioctl = ll_file_ioctl,
5135 .open = ll_file_open,
5136 .release = ll_file_release,
5137 .mmap = ll_file_mmap,
5138 .llseek = ll_file_seek,
5139 .splice_read = ll_file_splice_read,
5142 .fallocate = ll_fallocate,
5145 struct file_operations ll_file_operations_flock = {
5146 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5147 # ifdef HAVE_SYNC_READ_WRITE
5148 .read = new_sync_read,
5149 .write = new_sync_write,
5150 # endif /* HAVE_SYNC_READ_WRITE */
5151 .read_iter = ll_file_read_iter,
5152 .write_iter = ll_file_write_iter,
5153 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5154 .read = ll_file_read,
5155 .aio_read = ll_file_aio_read,
5156 .write = ll_file_write,
5157 .aio_write = ll_file_aio_write,
5158 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5159 .unlocked_ioctl = ll_file_ioctl,
5160 .open = ll_file_open,
5161 .release = ll_file_release,
5162 .mmap = ll_file_mmap,
5163 .llseek = ll_file_seek,
5164 .splice_read = ll_file_splice_read,
5167 .flock = ll_file_flock,
5168 .lock = ll_file_flock,
5169 .fallocate = ll_fallocate,
5172 /* These are for -o noflock - to return ENOSYS on flock calls */
5173 struct file_operations ll_file_operations_noflock = {
5174 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5175 # ifdef HAVE_SYNC_READ_WRITE
5176 .read = new_sync_read,
5177 .write = new_sync_write,
5178 # endif /* HAVE_SYNC_READ_WRITE */
5179 .read_iter = ll_file_read_iter,
5180 .write_iter = ll_file_write_iter,
5181 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5182 .read = ll_file_read,
5183 .aio_read = ll_file_aio_read,
5184 .write = ll_file_write,
5185 .aio_write = ll_file_aio_write,
5186 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5187 .unlocked_ioctl = ll_file_ioctl,
5188 .open = ll_file_open,
5189 .release = ll_file_release,
5190 .mmap = ll_file_mmap,
5191 .llseek = ll_file_seek,
5192 .splice_read = ll_file_splice_read,
5195 .flock = ll_file_noflock,
5196 .lock = ll_file_noflock,
5197 .fallocate = ll_fallocate,
5200 struct inode_operations ll_file_inode_operations = {
5201 .setattr = ll_setattr,
5202 .getattr = ll_getattr,
5203 .permission = ll_inode_permission,
5204 #ifdef HAVE_IOP_XATTR
5205 .setxattr = ll_setxattr,
5206 .getxattr = ll_getxattr,
5207 .removexattr = ll_removexattr,
5209 .listxattr = ll_listxattr,
5210 .fiemap = ll_fiemap,
5211 .get_acl = ll_get_acl,
5212 #ifdef HAVE_IOP_SET_ACL
5213 .set_acl = ll_set_acl,
5217 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
5219 struct ll_inode_info *lli = ll_i2info(inode);
5220 struct cl_object *obj = lli->lli_clob;
5229 env = cl_env_get(&refcheck);
5231 RETURN(PTR_ERR(env));
5233 rc = cl_conf_set(env, lli->lli_clob, conf);
5237 if (conf->coc_opc == OBJECT_CONF_SET) {
5238 struct ldlm_lock *lock = conf->coc_lock;
5239 struct cl_layout cl = {
5243 LASSERT(lock != NULL);
5244 LASSERT(ldlm_has_layout(lock));
5246 /* it can only be allowed to match after layout is
5247 * applied to inode otherwise false layout would be
5248 * seen. Applying layout shoud happen before dropping
5249 * the intent lock. */
5250 ldlm_lock_allow_match(lock);
5252 rc = cl_object_layout_get(env, obj, &cl);
5257 DFID": layout version change: %u -> %u\n",
5258 PFID(&lli->lli_fid), ll_layout_version_get(lli),
5260 ll_layout_version_set(lli, cl.cl_layout_gen);
5264 cl_env_put(env, &refcheck);
5269 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
5270 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
5273 struct ll_sb_info *sbi = ll_i2sbi(inode);
5274 struct ptlrpc_request *req;
5281 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
5282 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
5283 lock->l_lvb_data, lock->l_lvb_len);
5285 if (lock->l_lvb_data != NULL)
5288 /* if layout lock was granted right away, the layout is returned
5289 * within DLM_LVB of dlm reply; otherwise if the lock was ever
5290 * blocked and then granted via completion ast, we have to fetch
5291 * layout here. Please note that we can't use the LVB buffer in
5292 * completion AST because it doesn't have a large enough buffer */
5293 rc = ll_get_default_mdsize(sbi, &lmmsize);
5297 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), OBD_MD_FLXATTR,
5298 XATTR_NAME_LOV, lmmsize, &req);
5301 GOTO(out, rc = 0); /* empty layout */
5308 if (lmmsize == 0) /* empty layout */
5311 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
5313 GOTO(out, rc = -EFAULT);
5315 OBD_ALLOC_LARGE(lvbdata, lmmsize);
5316 if (lvbdata == NULL)
5317 GOTO(out, rc = -ENOMEM);
5319 memcpy(lvbdata, lmm, lmmsize);
5320 lock_res_and_lock(lock);
5321 if (unlikely(lock->l_lvb_data == NULL)) {
5322 lock->l_lvb_type = LVB_T_LAYOUT;
5323 lock->l_lvb_data = lvbdata;
5324 lock->l_lvb_len = lmmsize;
5327 unlock_res_and_lock(lock);
5330 OBD_FREE_LARGE(lvbdata, lmmsize);
5335 ptlrpc_req_finished(req);
5340 * Apply the layout to the inode. Layout lock is held and will be released
5343 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
5344 struct inode *inode)
5346 struct ll_inode_info *lli = ll_i2info(inode);
5347 struct ll_sb_info *sbi = ll_i2sbi(inode);
5348 struct ldlm_lock *lock;
5349 struct cl_object_conf conf;
5352 bool wait_layout = false;
5355 LASSERT(lustre_handle_is_used(lockh));
5357 lock = ldlm_handle2lock(lockh);
5358 LASSERT(lock != NULL);
5359 LASSERT(ldlm_has_layout(lock));
5361 LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
5362 PFID(&lli->lli_fid), inode);
5364 /* in case this is a caching lock and reinstate with new inode */
5365 md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
5367 lock_res_and_lock(lock);
5368 lvb_ready = ldlm_is_lvb_ready(lock);
5369 unlock_res_and_lock(lock);
5371 /* checking lvb_ready is racy but this is okay. The worst case is
5372 * that multi processes may configure the file on the same time. */
5376 rc = ll_layout_fetch(inode, lock);
5380 /* for layout lock, lmm is stored in lock's lvb.
5381 * lvb_data is immutable if the lock is held so it's safe to access it
5384 * set layout to file. Unlikely this will fail as old layout was
5385 * surely eliminated */
5386 memset(&conf, 0, sizeof conf);
5387 conf.coc_opc = OBJECT_CONF_SET;
5388 conf.coc_inode = inode;
5389 conf.coc_lock = lock;
5390 conf.u.coc_layout.lb_buf = lock->l_lvb_data;
5391 conf.u.coc_layout.lb_len = lock->l_lvb_len;
5392 rc = ll_layout_conf(inode, &conf);
5394 /* refresh layout failed, need to wait */
5395 wait_layout = rc == -EBUSY;
5398 LDLM_LOCK_PUT(lock);
5399 ldlm_lock_decref(lockh, mode);
5401 /* wait for IO to complete if it's still being used. */
5403 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
5404 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5406 memset(&conf, 0, sizeof conf);
5407 conf.coc_opc = OBJECT_CONF_WAIT;
5408 conf.coc_inode = inode;
5409 rc = ll_layout_conf(inode, &conf);
5413 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
5414 sbi->ll_fsname, PFID(&lli->lli_fid), rc);
5420 * Issue layout intent RPC to MDS.
5421 * \param inode [in] file inode
5422 * \param intent [in] layout intent
5424 * \retval 0 on success
5425 * \retval < 0 error code
5427 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
5429 struct ll_inode_info *lli = ll_i2info(inode);
5430 struct ll_sb_info *sbi = ll_i2sbi(inode);
5431 struct md_op_data *op_data;
5432 struct lookup_intent it;
5433 struct ptlrpc_request *req;
5437 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
5438 0, 0, LUSTRE_OPC_ANY, NULL);
5439 if (IS_ERR(op_data))
5440 RETURN(PTR_ERR(op_data));
5442 op_data->op_data = intent;
5443 op_data->op_data_size = sizeof(*intent);
5445 memset(&it, 0, sizeof(it));
5446 it.it_op = IT_LAYOUT;
5447 if (intent->li_opc == LAYOUT_INTENT_WRITE ||
5448 intent->li_opc == LAYOUT_INTENT_TRUNC)
5449 it.it_flags = FMODE_WRITE;
5451 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
5452 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5454 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
5455 &ll_md_blocking_ast, 0);
5456 if (it.it_request != NULL)
5457 ptlrpc_req_finished(it.it_request);
5458 it.it_request = NULL;
5460 ll_finish_md_op_data(op_data);
5462 /* set lock data in case this is a new lock */
5464 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
5466 ll_intent_drop_lock(&it);
5472 * This function checks if there exists a LAYOUT lock on the client side,
5473 * or enqueues it if it doesn't have one in cache.
5475 * This function will not hold layout lock so it may be revoked any time after
5476 * this function returns. Any operations depend on layout should be redone
5479 * This function should be called before lov_io_init() to get an uptodate
5480 * layout version, the caller should save the version number and after IO
5481 * is finished, this function should be called again to verify that layout
5482 * is not changed during IO time.
5484 int ll_layout_refresh(struct inode *inode, __u32 *gen)
5486 struct ll_inode_info *lli = ll_i2info(inode);
5487 struct ll_sb_info *sbi = ll_i2sbi(inode);
5488 struct lustre_handle lockh;
5489 struct layout_intent intent = {
5490 .li_opc = LAYOUT_INTENT_ACCESS,
5492 enum ldlm_mode mode;
5496 *gen = ll_layout_version_get(lli);
5497 if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != CL_LAYOUT_GEN_NONE)
5501 LASSERT(fid_is_sane(ll_inode2fid(inode)));
5502 LASSERT(S_ISREG(inode->i_mode));
5504 /* take layout lock mutex to enqueue layout lock exclusively. */
5505 mutex_lock(&lli->lli_layout_mutex);
5508 /* mostly layout lock is caching on the local side, so try to
5509 * match it before grabbing layout lock mutex. */
5510 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
5511 LCK_CR | LCK_CW | LCK_PR |
5513 if (mode != 0) { /* hit cached lock */
5514 rc = ll_layout_lock_set(&lockh, mode, inode);
5520 rc = ll_layout_intent(inode, &intent);
5526 *gen = ll_layout_version_get(lli);
5527 mutex_unlock(&lli->lli_layout_mutex);
5533 * Issue layout intent RPC indicating where in a file an IO is about to write.
5535 * \param[in] inode file inode.
5536 * \param[in] ext write range with start offset of fille in bytes where
5537 * an IO is about to write, and exclusive end offset in
5540 * \retval 0 on success
5541 * \retval < 0 error code
5543 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
5544 struct lu_extent *ext)
5546 struct layout_intent intent = {
5548 .li_extent.e_start = ext->e_start,
5549 .li_extent.e_end = ext->e_end,
5554 rc = ll_layout_intent(inode, &intent);
5560 * This function send a restore request to the MDT
5562 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
5564 struct hsm_user_request *hur;
5568 len = sizeof(struct hsm_user_request) +
5569 sizeof(struct hsm_user_item);
5570 OBD_ALLOC(hur, len);
5574 hur->hur_request.hr_action = HUA_RESTORE;
5575 hur->hur_request.hr_archive_id = 0;
5576 hur->hur_request.hr_flags = 0;
5577 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
5578 sizeof(hur->hur_user_item[0].hui_fid));
5579 hur->hur_user_item[0].hui_extent.offset = offset;
5580 hur->hur_user_item[0].hui_extent.length = length;
5581 hur->hur_request.hr_itemcount = 1;
5582 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,