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/
33 * Author: Peter Braam <braam@clusterfs.com>
34 * Author: Phil Schwan <phil@clusterfs.com>
35 * Author: Andreas Dilger <adilger@clusterfs.com>
38 #define DEBUG_SUBSYSTEM S_LLITE
39 #include <lustre_dlm.h>
40 #include <linux/pagemap.h>
41 #include <linux/file.h>
42 #include <linux/sched.h>
43 #include <linux/user_namespace.h>
44 #include <linux/uidgid.h>
45 #include <linux/falloc.h>
46 #include <linux/ktime.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 /* In case of encrypted file without the key, visible size was rounded
108 * up to next LUSTRE_ENCRYPTION_UNIT_SIZE, and clear text size was
109 * stored into lli_lazysize in ll_merge_attr(), so set proper file size
110 * now that we are closing.
112 if (llcrypt_require_key(inode) == -ENOKEY &&
113 ll_i2info(inode)->lli_attr_valid & OBD_MD_FLLAZYSIZE)
114 op_data->op_attr.ia_size = ll_i2info(inode)->lli_lazysize;
116 op_data->op_attr.ia_size = i_size_read(inode);
117 op_data->op_attr.ia_valid |= (ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
118 ATTR_MTIME | ATTR_MTIME_SET |
120 op_data->op_xvalid |= OP_XVALID_CTIME_SET;
121 op_data->op_attr_blocks = inode->i_blocks;
122 op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
123 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
124 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
125 op_data->op_open_handle = och->och_open_handle;
127 if (och->och_flags & FMODE_WRITE &&
128 test_and_clear_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags))
129 /* For HSM: if inode data has been modified, pack it so that
130 * MDT can set data dirty flag in the archive. */
131 op_data->op_bias |= MDS_DATA_MODIFIED;
137 * Perform a close, possibly with a bias.
138 * The meaning of "data" depends on the value of "bias".
140 * If \a bias is MDS_HSM_RELEASE then \a data is a pointer to the data version.
141 * If \a bias is MDS_CLOSE_LAYOUT_SWAP then \a data is a pointer to the inode to
144 static int ll_close_inode_openhandle(struct inode *inode,
145 struct obd_client_handle *och,
146 enum mds_op_bias bias, void *data)
148 struct obd_export *md_exp = ll_i2mdexp(inode);
149 const struct ll_inode_info *lli = ll_i2info(inode);
150 struct md_op_data *op_data;
151 struct ptlrpc_request *req = NULL;
155 if (class_exp2obd(md_exp) == NULL) {
156 CERROR("%s: invalid MDC connection handle closing "DFID"\n",
157 ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
161 OBD_ALLOC_PTR(op_data);
162 /* We leak openhandle and request here on error, but not much to be
163 * done in OOM case since app won't retry close on error either. */
165 GOTO(out, rc = -ENOMEM);
167 ll_prepare_close(inode, op_data, och);
169 case MDS_CLOSE_LAYOUT_MERGE:
170 /* merge blocks from the victim inode */
171 op_data->op_attr_blocks += ((struct inode *)data)->i_blocks;
172 op_data->op_attr.ia_valid |= ATTR_SIZE;
173 op_data->op_xvalid |= OP_XVALID_BLOCKS;
175 case MDS_CLOSE_LAYOUT_SPLIT:
176 case MDS_CLOSE_LAYOUT_SWAP: {
177 struct split_param *sp = data;
179 LASSERT(data != NULL);
180 op_data->op_bias |= bias;
181 op_data->op_data_version = 0;
182 op_data->op_lease_handle = och->och_lease_handle;
183 if (bias == MDS_CLOSE_LAYOUT_SPLIT) {
184 op_data->op_fid2 = *ll_inode2fid(sp->sp_inode);
185 op_data->op_mirror_id = sp->sp_mirror_id;
187 op_data->op_fid2 = *ll_inode2fid(data);
192 case MDS_CLOSE_RESYNC_DONE: {
193 struct ll_ioc_lease *ioc = data;
195 LASSERT(data != NULL);
196 op_data->op_attr_blocks +=
197 ioc->lil_count * op_data->op_attr_blocks;
198 op_data->op_attr.ia_valid |= ATTR_SIZE;
199 op_data->op_xvalid |= OP_XVALID_BLOCKS;
200 op_data->op_bias |= MDS_CLOSE_RESYNC_DONE;
202 op_data->op_lease_handle = och->och_lease_handle;
203 op_data->op_data = &ioc->lil_ids[0];
204 op_data->op_data_size =
205 ioc->lil_count * sizeof(ioc->lil_ids[0]);
209 case MDS_PCC_ATTACH: {
210 struct pcc_param *param = data;
212 LASSERT(data != NULL);
213 op_data->op_bias |= MDS_HSM_RELEASE | MDS_PCC_ATTACH;
214 op_data->op_archive_id = param->pa_archive_id;
215 op_data->op_data_version = param->pa_data_version;
216 op_data->op_lease_handle = och->och_lease_handle;
220 case MDS_HSM_RELEASE:
221 LASSERT(data != NULL);
222 op_data->op_bias |= MDS_HSM_RELEASE;
223 op_data->op_data_version = *(__u64 *)data;
224 op_data->op_lease_handle = och->och_lease_handle;
225 op_data->op_attr.ia_valid |= ATTR_SIZE;
226 op_data->op_xvalid |= OP_XVALID_BLOCKS;
230 LASSERT(data == NULL);
234 if (!(op_data->op_attr.ia_valid & ATTR_SIZE))
235 op_data->op_xvalid |= OP_XVALID_LAZYSIZE;
236 if (!(op_data->op_xvalid & OP_XVALID_BLOCKS))
237 op_data->op_xvalid |= OP_XVALID_LAZYBLOCKS;
239 rc = md_close(md_exp, op_data, och->och_mod, &req);
240 if (rc != 0 && rc != -EINTR)
241 CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
242 md_exp->exp_obd->obd_name, PFID(&lli->lli_fid), rc);
244 if (rc == 0 && op_data->op_bias & bias) {
245 struct mdt_body *body;
247 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
248 if (!(body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED))
251 if (bias & MDS_PCC_ATTACH) {
252 struct pcc_param *param = data;
254 param->pa_layout_gen = body->mbo_layout_gen;
258 ll_finish_md_op_data(op_data);
262 md_clear_open_replay_data(md_exp, och);
263 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
266 ptlrpc_req_finished(req); /* This is close request */
270 int ll_md_real_close(struct inode *inode, fmode_t fmode)
272 struct ll_inode_info *lli = ll_i2info(inode);
273 struct obd_client_handle **och_p;
274 struct obd_client_handle *och;
279 if (fmode & FMODE_WRITE) {
280 och_p = &lli->lli_mds_write_och;
281 och_usecount = &lli->lli_open_fd_write_count;
282 } else if (fmode & FMODE_EXEC) {
283 och_p = &lli->lli_mds_exec_och;
284 och_usecount = &lli->lli_open_fd_exec_count;
286 LASSERT(fmode & FMODE_READ);
287 och_p = &lli->lli_mds_read_och;
288 och_usecount = &lli->lli_open_fd_read_count;
291 mutex_lock(&lli->lli_och_mutex);
292 if (*och_usecount > 0) {
293 /* There are still users of this handle, so skip
295 mutex_unlock(&lli->lli_och_mutex);
301 mutex_unlock(&lli->lli_och_mutex);
304 /* There might be a race and this handle may already
306 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
312 static int ll_md_close(struct inode *inode, struct file *file)
314 union ldlm_policy_data policy = {
315 .l_inodebits = { MDS_INODELOCK_OPEN },
317 __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
318 struct ll_file_data *fd = file->private_data;
319 struct ll_inode_info *lli = ll_i2info(inode);
320 struct lustre_handle lockh;
321 enum ldlm_mode lockmode;
325 /* clear group lock, if present */
326 if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
327 ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid);
329 mutex_lock(&lli->lli_och_mutex);
330 if (fd->fd_lease_och != NULL) {
332 struct obd_client_handle *lease_och;
334 lease_och = fd->fd_lease_och;
335 fd->fd_lease_och = NULL;
336 mutex_unlock(&lli->lli_och_mutex);
338 /* Usually the lease is not released when the
339 * application crashed, we need to release here. */
340 rc = ll_lease_close(lease_och, inode, &lease_broken);
342 mutex_lock(&lli->lli_och_mutex);
344 CDEBUG_LIMIT(rc ? D_ERROR : D_INODE,
345 "Clean up lease "DFID" %d/%d\n",
346 PFID(&lli->lli_fid), rc, lease_broken);
349 if (fd->fd_och != NULL) {
350 struct obd_client_handle *och;
354 mutex_unlock(&lli->lli_och_mutex);
356 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
360 /* Let's see if we have good enough OPEN lock on the file and if
361 we can skip talking to MDS */
362 if (fd->fd_omode & FMODE_WRITE) {
364 LASSERT(lli->lli_open_fd_write_count);
365 lli->lli_open_fd_write_count--;
366 } else if (fd->fd_omode & FMODE_EXEC) {
368 LASSERT(lli->lli_open_fd_exec_count);
369 lli->lli_open_fd_exec_count--;
372 LASSERT(lli->lli_open_fd_read_count);
373 lli->lli_open_fd_read_count--;
375 mutex_unlock(&lli->lli_och_mutex);
377 /* LU-4398: do not cache write open lock if the file has exec bit */
378 if ((lockmode == LCK_CW && inode->i_mode & S_IXUGO) ||
379 !md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
380 LDLM_IBITS, &policy, lockmode, &lockh))
381 rc = ll_md_real_close(inode, fd->fd_omode);
384 file->private_data = NULL;
385 ll_file_data_put(fd);
390 /* While this returns an error code, fput() the caller does not, so we need
391 * to make every effort to clean up all of our state here. Also, applications
392 * rarely check close errors and even if an error is returned they will not
393 * re-try the close call.
395 int ll_file_release(struct inode *inode, struct file *file)
397 struct ll_file_data *fd;
398 struct ll_sb_info *sbi = ll_i2sbi(inode);
399 struct ll_inode_info *lli = ll_i2info(inode);
400 ktime_t kstart = ktime_get();
405 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
406 PFID(ll_inode2fid(inode)), inode);
408 fd = file->private_data;
411 /* The last ref on @file, maybe not the the owner pid of statahead,
412 * because parent and child process can share the same file handle. */
413 if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd)
414 ll_deauthorize_statahead(inode, fd);
416 if (is_root_inode(inode)) {
417 file->private_data = NULL;
418 ll_file_data_put(fd);
422 pcc_file_release(inode, file);
424 if (!S_ISDIR(inode->i_mode)) {
425 if (lli->lli_clob != NULL)
426 lov_read_and_clear_async_rc(lli->lli_clob);
427 lli->lli_async_rc = 0;
430 lli->lli_close_fd_time = ktime_get();
432 rc = ll_md_close(inode, file);
434 if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
435 libcfs_debug_dumplog();
438 if (!rc && !is_root_inode(inode))
439 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE,
440 ktime_us_delta(ktime_get(), kstart));
444 static inline int ll_dom_readpage(void *data, struct page *page)
446 /* since ll_dom_readpage is a page cache helper, it is safe to assume
447 * mapping and host pointers are set here
450 struct niobuf_local *lnb = data;
454 inode = page2inode(page);
456 kaddr = kmap_atomic(page);
457 memcpy(kaddr, lnb->lnb_data, lnb->lnb_len);
458 if (lnb->lnb_len < PAGE_SIZE)
459 memset(kaddr + lnb->lnb_len, 0,
460 PAGE_SIZE - lnb->lnb_len);
461 flush_dcache_page(page);
462 SetPageUptodate(page);
463 kunmap_atomic(kaddr);
465 if (inode && IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
466 if (!llcrypt_has_encryption_key(inode))
467 CDEBUG(D_SEC, "no enc key for "DFID"\n",
468 PFID(ll_inode2fid(inode)));
470 unsigned int offs = 0;
472 while (offs < PAGE_SIZE) {
473 /* decrypt only if page is not empty */
474 if (memcmp(page_address(page) + offs,
475 page_address(ZERO_PAGE(0)),
476 LUSTRE_ENCRYPTION_UNIT_SIZE) == 0)
479 rc = llcrypt_decrypt_pagecache_blocks(page,
480 LUSTRE_ENCRYPTION_UNIT_SIZE,
485 offs += LUSTRE_ENCRYPTION_UNIT_SIZE;
494 void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req)
498 struct ll_inode_info *lli = ll_i2info(inode);
499 struct cl_object *obj = lli->lli_clob;
500 struct address_space *mapping = inode->i_mapping;
502 struct niobuf_remote *rnb;
503 struct mdt_body *body;
505 unsigned long index, start;
506 struct niobuf_local lnb;
515 if (!req_capsule_field_present(&req->rq_pill, &RMF_NIOBUF_INLINE,
519 rnb = req_capsule_server_get(&req->rq_pill, &RMF_NIOBUF_INLINE);
520 if (rnb == NULL || rnb->rnb_len == 0)
523 /* LU-11595: Server may return whole file and that is OK always or
524 * it may return just file tail and its offset must be aligned with
525 * client PAGE_SIZE to be used on that client, if server's PAGE_SIZE is
526 * smaller then offset may be not aligned and that data is just ignored.
528 if (rnb->rnb_offset & ~PAGE_MASK)
531 /* Server returns whole file or just file tail if it fills in reply
532 * buffer, in both cases total size should be equal to the file size.
534 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
535 if (rnb->rnb_offset + rnb->rnb_len != body->mbo_dom_size &&
536 !(inode && IS_ENCRYPTED(inode))) {
537 CERROR("%s: server returns off/len %llu/%u but size %llu\n",
538 ll_i2sbi(inode)->ll_fsname, rnb->rnb_offset,
539 rnb->rnb_len, body->mbo_dom_size);
543 env = cl_env_get(&refcheck);
546 io = vvp_env_thread_io(env);
548 io->ci_ignore_layout = 1;
549 rc = cl_io_init(env, io, CIT_MISC, obj);
553 CDEBUG(D_INFO, "Get data along with open at %llu len %i, size %llu\n",
554 rnb->rnb_offset, rnb->rnb_len, body->mbo_dom_size);
556 data = (char *)rnb + sizeof(*rnb);
558 lnb.lnb_file_offset = rnb->rnb_offset;
559 start = lnb.lnb_file_offset >> PAGE_SHIFT;
561 LASSERT((lnb.lnb_file_offset & ~PAGE_MASK) == 0);
562 lnb.lnb_page_offset = 0;
564 struct cl_page *page;
566 lnb.lnb_data = data + (index << PAGE_SHIFT);
567 lnb.lnb_len = rnb->rnb_len - (index << PAGE_SHIFT);
568 if (lnb.lnb_len > PAGE_SIZE)
569 lnb.lnb_len = PAGE_SIZE;
571 vmpage = read_cache_page(mapping, index + start,
572 ll_dom_readpage, &lnb);
573 if (IS_ERR(vmpage)) {
574 CWARN("%s: cannot fill page %lu for "DFID
575 " with data: rc = %li\n",
576 ll_i2sbi(inode)->ll_fsname, index + start,
577 PFID(lu_object_fid(&obj->co_lu)),
582 if (vmpage->mapping == NULL) {
585 /* page was truncated */
588 /* attach VM page to CL page cache */
589 page = cl_page_find(env, obj, vmpage->index, vmpage,
592 ClearPageUptodate(vmpage);
597 SetPageUptodate(vmpage);
598 cl_page_put(env, page);
602 } while (rnb->rnb_len > (index << PAGE_SHIFT));
606 cl_env_put(env, &refcheck);
611 static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
612 struct lookup_intent *itp)
614 struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
615 struct dentry *parent = de->d_parent;
618 struct md_op_data *op_data;
619 struct ptlrpc_request *req = NULL;
623 LASSERT(parent != NULL);
624 LASSERT(itp->it_flags & MDS_OPEN_BY_FID);
626 /* if server supports open-by-fid, or file name is invalid, don't pack
627 * name in open request */
628 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_OPEN_BY_NAME) ||
629 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID)) {
631 len = de->d_name.len;
632 name = kmalloc(len + 1, GFP_NOFS);
637 spin_lock(&de->d_lock);
638 if (len != de->d_name.len) {
639 spin_unlock(&de->d_lock);
643 memcpy(name, de->d_name.name, len);
645 spin_unlock(&de->d_lock);
647 if (!lu_name_is_valid_2(name, len)) {
653 op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
654 name, len, 0, LUSTRE_OPC_OPEN, NULL);
655 if (IS_ERR(op_data)) {
657 RETURN(PTR_ERR(op_data));
659 op_data->op_data = lmm;
660 op_data->op_data_size = lmmsize;
662 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_OPEN_DELAY, cfs_fail_val);
664 rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
665 &ll_md_blocking_ast, 0);
667 ll_finish_md_op_data(op_data);
669 /* reason for keep own exit path - don`t flood log
670 * with messages with -ESTALE errors.
672 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
673 it_open_error(DISP_OPEN_OPEN, itp))
675 ll_release_openhandle(de, itp);
679 if (it_disposition(itp, DISP_LOOKUP_NEG))
680 GOTO(out, rc = -ENOENT);
682 if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
683 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
684 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
688 rc = ll_prep_inode(&de->d_inode, &req->rq_pill, NULL, itp);
690 if (!rc && itp->it_lock_mode) {
693 /* If we got a lock back and it has a LOOKUP bit set,
694 * make sure the dentry is marked as valid so we can find it.
695 * We don't need to care about actual hashing since other bits
696 * of kernel will deal with that later.
698 ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, &bits);
699 if (bits & MDS_INODELOCK_LOOKUP) {
700 d_lustre_revalidate(de);
701 ll_update_dir_depth(parent->d_inode, de->d_inode);
704 /* if DoM bit returned along with LAYOUT bit then there
705 * can be read-on-open data returned.
707 if (bits & MDS_INODELOCK_DOM && bits & MDS_INODELOCK_LAYOUT)
708 ll_dom_finish_open(de->d_inode, req);
712 ptlrpc_req_finished(req);
713 ll_intent_drop_lock(itp);
715 /* We did open by fid, but by the time we got to the server, the object
716 * disappeared. This is possible if the object was unlinked, but it's
717 * also possible if the object was unlinked by a rename. In the case
718 * of an object renamed over our existing one, we can't fail this open.
719 * O_CREAT also goes through this path if we had an existing dentry,
720 * and it's obviously wrong to return ENOENT for O_CREAT.
722 * Instead let's return -ESTALE, and the VFS will retry the open with
723 * LOOKUP_REVAL, which we catch in ll_revalidate_dentry and fail to
724 * revalidate, causing a lookup. This causes extra lookups in the case
725 * where we had a dentry in cache but the file is being unlinked and we
726 * lose the race with unlink, but this should be very rare.
734 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
735 struct obd_client_handle *och)
737 struct mdt_body *body;
739 body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
740 och->och_open_handle = body->mbo_open_handle;
741 och->och_fid = body->mbo_fid1;
742 och->och_lease_handle.cookie = it->it_lock_handle;
743 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
744 och->och_flags = it->it_flags;
746 return md_set_open_replay_data(md_exp, och, it);
749 static int ll_local_open(struct file *file, struct lookup_intent *it,
750 struct ll_file_data *fd, struct obd_client_handle *och)
752 struct inode *inode = file_inode(file);
755 LASSERT(!file->private_data);
762 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
767 file->private_data = fd;
768 ll_readahead_init(inode, &fd->fd_ras);
769 fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
774 void ll_track_file_opens(struct inode *inode)
776 struct ll_inode_info *lli = ll_i2info(inode);
777 struct ll_sb_info *sbi = ll_i2sbi(inode);
779 /* do not skew results with delays from never-opened inodes */
780 if (ktime_to_ns(lli->lli_close_fd_time))
781 ll_stats_ops_tally(sbi, LPROC_LL_INODE_OPCLTM,
782 ktime_us_delta(ktime_get(), lli->lli_close_fd_time));
784 if (ktime_after(ktime_get(),
785 ktime_add_ms(lli->lli_close_fd_time,
786 sbi->ll_oc_max_ms))) {
787 lli->lli_open_fd_count = 1;
788 lli->lli_close_fd_time = ns_to_ktime(0);
790 lli->lli_open_fd_count++;
793 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_OCOUNT,
794 lli->lli_open_fd_count);
797 /* Open a file, and (for the very first open) create objects on the OSTs at
798 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
799 * creation or open until ll_lov_setstripe() ioctl is called.
801 * If we already have the stripe MD locally then we don't request it in
802 * md_open(), by passing a lmm_size = 0.
804 * It is up to the application to ensure no other processes open this file
805 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
806 * used. We might be able to avoid races of that sort by getting lli_open_sem
807 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
808 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
810 int ll_file_open(struct inode *inode, struct file *file)
812 struct ll_inode_info *lli = ll_i2info(inode);
813 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
814 .it_flags = file->f_flags };
815 struct obd_client_handle **och_p = NULL;
816 __u64 *och_usecount = NULL;
817 struct ll_file_data *fd;
818 ktime_t kstart = ktime_get();
822 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
823 PFID(ll_inode2fid(inode)), inode, file->f_flags);
825 it = file->private_data; /* XXX: compat macro */
826 file->private_data = NULL; /* prevent ll_local_open assertion */
828 if (S_ISREG(inode->i_mode)) {
829 rc = ll_file_open_encrypt(inode, file);
831 if (it && it->it_disposition)
832 ll_release_openhandle(file_dentry(file), it);
833 GOTO(out_nofiledata, rc);
837 fd = ll_file_data_get();
839 GOTO(out_nofiledata, rc = -ENOMEM);
842 if (S_ISDIR(inode->i_mode))
843 ll_authorize_statahead(inode, fd);
845 ll_track_file_opens(inode);
846 if (is_root_inode(inode)) {
847 file->private_data = fd;
851 if (!it || !it->it_disposition) {
852 /* Convert f_flags into access mode. We cannot use file->f_mode,
853 * because everything but O_ACCMODE mask was stripped from
855 if ((oit.it_flags + 1) & O_ACCMODE)
857 if (file->f_flags & O_TRUNC)
858 oit.it_flags |= FMODE_WRITE;
860 /* kernel only call f_op->open in dentry_open. filp_open calls
861 * dentry_open after call to open_namei that checks permissions.
862 * Only nfsd_open call dentry_open directly without checking
863 * permissions and because of that this code below is safe.
865 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
866 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
868 /* We do not want O_EXCL here, presumably we opened the file
869 * already? XXX - NFS implications? */
870 oit.it_flags &= ~O_EXCL;
872 /* bug20584, if "it_flags" contains O_CREAT, the file will be
873 * created if necessary, then "IT_CREAT" should be set to keep
874 * consistent with it */
875 if (oit.it_flags & O_CREAT)
876 oit.it_op |= IT_CREAT;
882 /* Let's see if we have file open on MDS already. */
883 if (it->it_flags & FMODE_WRITE) {
884 och_p = &lli->lli_mds_write_och;
885 och_usecount = &lli->lli_open_fd_write_count;
886 } else if (it->it_flags & FMODE_EXEC) {
887 och_p = &lli->lli_mds_exec_och;
888 och_usecount = &lli->lli_open_fd_exec_count;
890 och_p = &lli->lli_mds_read_och;
891 och_usecount = &lli->lli_open_fd_read_count;
894 mutex_lock(&lli->lli_och_mutex);
895 if (*och_p) { /* Open handle is present */
896 if (it_disposition(it, DISP_OPEN_OPEN)) {
897 /* Well, there's extra open request that we do not need,
898 * let's close it somehow. This will decref request. */
899 rc = it_open_error(DISP_OPEN_OPEN, it);
901 mutex_unlock(&lli->lli_och_mutex);
902 GOTO(out_openerr, rc);
905 ll_release_openhandle(file_dentry(file), it);
909 rc = ll_local_open(file, it, fd, NULL);
912 mutex_unlock(&lli->lli_och_mutex);
913 GOTO(out_openerr, rc);
916 LASSERT(*och_usecount == 0);
917 if (!it->it_disposition) {
918 struct dentry *dentry = file_dentry(file);
919 struct ll_sb_info *sbi = ll_i2sbi(inode);
920 struct ll_dentry_data *ldd;
922 /* We cannot just request lock handle now, new ELC code
923 * means that one of other OPEN locks for this file
924 * could be cancelled, and since blocking ast handler
925 * would attempt to grab och_mutex as well, that would
926 * result in a deadlock
928 mutex_unlock(&lli->lli_och_mutex);
930 * Normally called under two situations:
932 * 2. A race/condition on MDS resulting in no open
933 * handle to be returned from LOOKUP|OPEN request,
934 * for example if the target entry was a symlink.
936 * In NFS path we know there's pathologic behavior
937 * so we always enable open lock caching when coming
938 * from there. It's detected by setting a flag in
941 * After reaching number of opens of this inode
942 * we always ask for an open lock on it to handle
943 * bad userspace actors that open and close files
944 * in a loop for absolutely no good reason
947 ldd = ll_d2d(dentry);
948 if (filename_is_volatile(dentry->d_name.name,
951 /* There really is nothing here, but this
952 * make this more readable I think.
953 * We do not want openlock for volatile
954 * files under any circumstances
956 } else if (ldd && ldd->lld_nfs_dentry) {
957 /* NFS path. This also happens to catch
958 * open by fh files I guess
960 it->it_flags |= MDS_OPEN_LOCK;
961 /* clear the flag for future lookups */
962 ldd->lld_nfs_dentry = 0;
963 } else if (sbi->ll_oc_thrsh_count > 0) {
964 /* Take MDS_OPEN_LOCK with many opens */
965 if (lli->lli_open_fd_count >=
966 sbi->ll_oc_thrsh_count)
967 it->it_flags |= MDS_OPEN_LOCK;
969 /* If this is open after we just closed */
970 else if (ktime_before(ktime_get(),
971 ktime_add_ms(lli->lli_close_fd_time,
972 sbi->ll_oc_thrsh_ms)))
973 it->it_flags |= MDS_OPEN_LOCK;
977 * Always specify MDS_OPEN_BY_FID because we don't want
978 * to get file with different fid.
980 it->it_flags |= MDS_OPEN_BY_FID;
981 rc = ll_intent_file_open(dentry, NULL, 0, it);
983 GOTO(out_openerr, rc);
987 OBD_ALLOC(*och_p, sizeof(struct obd_client_handle));
989 GOTO(out_och_free, rc = -ENOMEM);
993 /* md_intent_lock() didn't get a request ref if there was an
994 * open error, so don't do cleanup on the request here
996 /* XXX (green): Should not we bail out on any error here, not
997 * just open error? */
998 rc = it_open_error(DISP_OPEN_OPEN, it);
1000 GOTO(out_och_free, rc);
1002 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
1003 "inode %p: disposition %x, status %d\n", inode,
1004 it_disposition(it, ~0), it->it_status);
1006 rc = ll_local_open(file, it, fd, *och_p);
1008 GOTO(out_och_free, rc);
1011 rc = pcc_file_open(inode, file);
1013 GOTO(out_och_free, rc);
1015 mutex_unlock(&lli->lli_och_mutex);
1019 /* Must do this outside lli_och_mutex lock to prevent deadlock where
1020 different kind of OPEN lock for this same inode gets cancelled
1021 by ldlm_cancel_lru */
1022 if (!S_ISREG(inode->i_mode))
1023 GOTO(out_och_free, rc);
1024 cl_lov_delay_create_clear(&file->f_flags);
1025 GOTO(out_och_free, rc);
1029 if (och_p && *och_p) {
1030 OBD_FREE(*och_p, sizeof(struct obd_client_handle));
1031 *och_p = NULL; /* OBD_FREE writes some magic there */
1034 mutex_unlock(&lli->lli_och_mutex);
1037 if (lli->lli_opendir_key == fd)
1038 ll_deauthorize_statahead(inode, fd);
1041 ll_file_data_put(fd);
1043 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN,
1044 ktime_us_delta(ktime_get(), kstart));
1048 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
1049 ptlrpc_req_finished(it->it_request);
1050 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1056 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
1057 struct ldlm_lock_desc *desc, void *data, int flag)
1060 struct lustre_handle lockh;
1064 case LDLM_CB_BLOCKING:
1065 ldlm_lock2handle(lock, &lockh);
1066 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1068 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
1072 case LDLM_CB_CANCELING:
1080 * When setting a lease on a file, we take ownership of the lli_mds_*_och
1081 * and save it as fd->fd_och so as to force client to reopen the file even
1082 * if it has an open lock in cache already.
1084 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
1085 struct lustre_handle *old_open_handle)
1087 struct ll_inode_info *lli = ll_i2info(inode);
1088 struct ll_file_data *fd = file->private_data;
1089 struct obd_client_handle **och_p;
1090 __u64 *och_usecount;
1094 /* Get the openhandle of the file */
1095 mutex_lock(&lli->lli_och_mutex);
1096 if (fd->fd_lease_och != NULL)
1097 GOTO(out_unlock, rc = -EBUSY);
1099 if (fd->fd_och == NULL) {
1100 if (file->f_mode & FMODE_WRITE) {
1101 LASSERT(lli->lli_mds_write_och != NULL);
1102 och_p = &lli->lli_mds_write_och;
1103 och_usecount = &lli->lli_open_fd_write_count;
1105 LASSERT(lli->lli_mds_read_och != NULL);
1106 och_p = &lli->lli_mds_read_och;
1107 och_usecount = &lli->lli_open_fd_read_count;
1110 if (*och_usecount > 1)
1111 GOTO(out_unlock, rc = -EBUSY);
1113 fd->fd_och = *och_p;
1118 *old_open_handle = fd->fd_och->och_open_handle;
1122 mutex_unlock(&lli->lli_och_mutex);
1127 * Release ownership on lli_mds_*_och when putting back a file lease.
1129 static int ll_lease_och_release(struct inode *inode, struct file *file)
1131 struct ll_inode_info *lli = ll_i2info(inode);
1132 struct ll_file_data *fd = file->private_data;
1133 struct obd_client_handle **och_p;
1134 struct obd_client_handle *old_och = NULL;
1135 __u64 *och_usecount;
1139 mutex_lock(&lli->lli_och_mutex);
1140 if (file->f_mode & FMODE_WRITE) {
1141 och_p = &lli->lli_mds_write_och;
1142 och_usecount = &lli->lli_open_fd_write_count;
1144 och_p = &lli->lli_mds_read_och;
1145 och_usecount = &lli->lli_open_fd_read_count;
1148 /* The file may have been open by another process (broken lease) so
1149 * *och_p is not NULL. In this case we should simply increase usecount
1152 if (*och_p != NULL) {
1153 old_och = fd->fd_och;
1156 *och_p = fd->fd_och;
1160 mutex_unlock(&lli->lli_och_mutex);
1162 if (old_och != NULL)
1163 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
1169 * Acquire a lease and open the file.
1171 static struct obd_client_handle *
1172 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
1175 struct lookup_intent it = { .it_op = IT_OPEN };
1176 struct ll_sb_info *sbi = ll_i2sbi(inode);
1177 struct md_op_data *op_data;
1178 struct ptlrpc_request *req = NULL;
1179 struct lustre_handle old_open_handle = { 0 };
1180 struct obd_client_handle *och = NULL;
1185 if (fmode != FMODE_WRITE && fmode != FMODE_READ)
1186 RETURN(ERR_PTR(-EINVAL));
1189 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
1190 RETURN(ERR_PTR(-EPERM));
1192 rc = ll_lease_och_acquire(inode, file, &old_open_handle);
1194 RETURN(ERR_PTR(rc));
1199 RETURN(ERR_PTR(-ENOMEM));
1201 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
1202 LUSTRE_OPC_ANY, NULL);
1203 if (IS_ERR(op_data))
1204 GOTO(out, rc = PTR_ERR(op_data));
1206 /* To tell the MDT this openhandle is from the same owner */
1207 op_data->op_open_handle = old_open_handle;
1209 it.it_flags = fmode | open_flags;
1210 it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
1211 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
1212 &ll_md_blocking_lease_ast,
1213 /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
1214 * it can be cancelled which may mislead applications that the lease is
1216 * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
1217 * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
1218 * doesn't deal with openhandle, so normal openhandle will be leaked. */
1219 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
1220 ll_finish_md_op_data(op_data);
1221 ptlrpc_req_finished(req);
1223 GOTO(out_release_it, rc);
1225 if (it_disposition(&it, DISP_LOOKUP_NEG))
1226 GOTO(out_release_it, rc = -ENOENT);
1228 rc = it_open_error(DISP_OPEN_OPEN, &it);
1230 GOTO(out_release_it, rc);
1232 LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
1233 rc = ll_och_fill(sbi->ll_md_exp, &it, och);
1235 GOTO(out_release_it, rc);
1237 if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
1238 GOTO(out_close, rc = -EOPNOTSUPP);
1240 /* already get lease, handle lease lock */
1241 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
1242 if (!it.it_lock_mode ||
1243 !(it.it_lock_bits & MDS_INODELOCK_OPEN)) {
1244 /* open lock must return for lease */
1245 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
1246 PFID(ll_inode2fid(inode)), it.it_lock_mode,
1248 GOTO(out_close, rc = -EPROTO);
1251 ll_intent_release(&it);
1255 /* Cancel open lock */
1256 if (it.it_lock_mode != 0) {
1257 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
1259 it.it_lock_mode = 0;
1260 och->och_lease_handle.cookie = 0ULL;
1262 rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
1264 CERROR("%s: error closing file "DFID": %d\n",
1265 sbi->ll_fsname, PFID(&ll_i2info(inode)->lli_fid), rc2);
1266 och = NULL; /* och has been freed in ll_close_inode_openhandle() */
1268 ll_intent_release(&it);
1272 RETURN(ERR_PTR(rc));
1276 * Check whether a layout swap can be done between two inodes.
1278 * \param[in] inode1 First inode to check
1279 * \param[in] inode2 Second inode to check
1281 * \retval 0 on success, layout swap can be performed between both inodes
1282 * \retval negative error code if requirements are not met
1284 static int ll_check_swap_layouts_validity(struct inode *inode1,
1285 struct inode *inode2)
1287 if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
1290 if (inode_permission(&init_user_ns, inode1, MAY_WRITE) ||
1291 inode_permission(&init_user_ns, inode2, MAY_WRITE))
1294 if (inode1->i_sb != inode2->i_sb)
1300 static int ll_swap_layouts_close(struct obd_client_handle *och,
1301 struct inode *inode, struct inode *inode2)
1303 const struct lu_fid *fid1 = ll_inode2fid(inode);
1304 const struct lu_fid *fid2;
1308 CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
1309 ll_i2sbi(inode)->ll_fsname, PFID(fid1));
1311 rc = ll_check_swap_layouts_validity(inode, inode2);
1313 GOTO(out_free_och, rc);
1315 /* We now know that inode2 is a lustre inode */
1316 fid2 = ll_inode2fid(inode2);
1318 rc = lu_fid_cmp(fid1, fid2);
1320 GOTO(out_free_och, rc = -EINVAL);
1322 /* Close the file and {swap,merge} layouts between inode & inode2.
1323 * NB: lease lock handle is released in mdc_close_layout_swap_pack()
1324 * because we still need it to pack l_remote_handle to MDT. */
1325 rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
1328 och = NULL; /* freed in ll_close_inode_openhandle() */
1338 * Release lease and close the file.
1339 * It will check if the lease has ever broken.
1341 static int ll_lease_close_intent(struct obd_client_handle *och,
1342 struct inode *inode,
1343 bool *lease_broken, enum mds_op_bias bias,
1346 struct ldlm_lock *lock;
1347 bool cancelled = true;
1351 lock = ldlm_handle2lock(&och->och_lease_handle);
1353 lock_res_and_lock(lock);
1354 cancelled = ldlm_is_cancel(lock);
1355 unlock_res_and_lock(lock);
1356 LDLM_LOCK_PUT(lock);
1359 CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
1360 PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
1362 if (lease_broken != NULL)
1363 *lease_broken = cancelled;
1365 if (!cancelled && !bias)
1366 ldlm_cli_cancel(&och->och_lease_handle, 0);
1368 if (cancelled) { /* no need to excute intent */
1373 rc = ll_close_inode_openhandle(inode, och, bias, data);
1377 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
1380 return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
1384 * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
1386 static int ll_lease_file_resync(struct obd_client_handle *och,
1387 struct inode *inode, unsigned long arg)
1389 struct ll_sb_info *sbi = ll_i2sbi(inode);
1390 struct md_op_data *op_data;
1391 struct ll_ioc_lease_id ioc;
1392 __u64 data_version_unused;
1396 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1397 LUSTRE_OPC_ANY, NULL);
1398 if (IS_ERR(op_data))
1399 RETURN(PTR_ERR(op_data));
1401 if (copy_from_user(&ioc, (struct ll_ioc_lease_id __user *)arg,
1405 /* before starting file resync, it's necessary to clean up page cache
1406 * in client memory, otherwise once the layout version is increased,
1407 * writing back cached data will be denied the OSTs. */
1408 rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
1412 op_data->op_lease_handle = och->och_lease_handle;
1413 op_data->op_mirror_id = ioc.lil_mirror_id;
1414 rc = md_file_resync(sbi->ll_md_exp, op_data);
1420 ll_finish_md_op_data(op_data);
1424 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1426 struct ll_inode_info *lli = ll_i2info(inode);
1427 struct cl_object *obj = lli->lli_clob;
1428 struct cl_attr *attr = vvp_env_thread_attr(env);
1436 ll_inode_size_lock(inode);
1438 /* Merge timestamps the most recently obtained from MDS with
1439 * timestamps obtained from OSTs.
1441 * Do not overwrite atime of inode because it may be refreshed
1442 * by file_accessed() function. If the read was served by cache
1443 * data, there is no RPC to be sent so that atime may not be
1444 * transferred to OSTs at all. MDT only updates atime at close time
1445 * if it's at least 'mdd.*.atime_diff' older.
1446 * All in all, the atime in Lustre does not strictly comply with
1447 * POSIX. Solving this problem needs to send an RPC to MDT for each
1448 * read, this will hurt performance.
1450 if (test_and_clear_bit(LLIF_UPDATE_ATIME, &lli->lli_flags) ||
1451 inode->i_atime.tv_sec < lli->lli_atime)
1452 inode->i_atime.tv_sec = lli->lli_atime;
1454 inode->i_mtime.tv_sec = lli->lli_mtime;
1455 inode->i_ctime.tv_sec = lli->lli_ctime;
1457 mtime = inode->i_mtime.tv_sec;
1458 atime = inode->i_atime.tv_sec;
1459 ctime = inode->i_ctime.tv_sec;
1461 cl_object_attr_lock(obj);
1462 if (OBD_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
1465 rc = cl_object_attr_get(env, obj, attr);
1466 cl_object_attr_unlock(obj);
1469 GOTO(out_size_unlock, rc = (rc == -ENODATA ? 0 : rc));
1471 if (atime < attr->cat_atime)
1472 atime = attr->cat_atime;
1474 if (ctime < attr->cat_ctime)
1475 ctime = attr->cat_ctime;
1477 if (mtime < attr->cat_mtime)
1478 mtime = attr->cat_mtime;
1480 CDEBUG(D_VFSTRACE, DFID" updating i_size %llu\n",
1481 PFID(&lli->lli_fid), attr->cat_size);
1483 if (llcrypt_require_key(inode) == -ENOKEY) {
1484 /* Without the key, round up encrypted file size to next
1485 * LUSTRE_ENCRYPTION_UNIT_SIZE. Clear text size is put in
1486 * lli_lazysize for proper file size setting at close time.
1488 lli->lli_attr_valid |= OBD_MD_FLLAZYSIZE;
1489 lli->lli_lazysize = attr->cat_size;
1490 attr->cat_size = round_up(attr->cat_size,
1491 LUSTRE_ENCRYPTION_UNIT_SIZE);
1493 i_size_write(inode, attr->cat_size);
1494 inode->i_blocks = attr->cat_blocks;
1496 inode->i_mtime.tv_sec = mtime;
1497 inode->i_atime.tv_sec = atime;
1498 inode->i_ctime.tv_sec = ctime;
1501 ll_inode_size_unlock(inode);
1507 * Set designated mirror for I/O.
1509 * So far only read, write, and truncated can support to issue I/O to
1510 * designated mirror.
1512 void ll_io_set_mirror(struct cl_io *io, const struct file *file)
1514 struct ll_file_data *fd = file->private_data;
1516 /* clear layout version for generic(non-resync) I/O in case it carries
1517 * stale layout version due to I/O restart */
1518 io->ci_layout_version = 0;
1520 /* FLR: disable non-delay for designated mirror I/O because obviously
1521 * only one mirror is available */
1522 if (fd->fd_designated_mirror > 0) {
1524 io->ci_designated_mirror = fd->fd_designated_mirror;
1525 io->ci_layout_version = fd->fd_layout_version;
1528 CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
1529 file->f_path.dentry->d_name.name, io->ci_designated_mirror);
1532 static bool file_is_noatime(const struct file *file)
1534 const struct vfsmount *mnt = file->f_path.mnt;
1535 const struct inode *inode = file_inode((struct file *)file);
1537 /* Adapted from file_accessed() and touch_atime().*/
1538 if (file->f_flags & O_NOATIME)
1541 if (inode->i_flags & S_NOATIME)
1544 if (IS_NOATIME(inode))
1547 if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1550 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1553 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
1559 void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot,
1560 struct vvp_io_args *args)
1562 struct inode *inode = file_inode(file);
1563 struct ll_file_data *fd = file->private_data;
1565 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1566 io->ci_lock_no_expand = fd->ll_lock_no_expand;
1568 if (iot == CIT_WRITE) {
1569 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1570 io->u.ci_wr.wr_sync = !!(file->f_flags & O_SYNC ||
1571 file->f_flags & O_DIRECT ||
1573 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1574 io->u.ci_wr.wr_sync |= !!(args &&
1575 (args->u.normal.via_iocb->ki_flags &
1581 io->ci_iocb_nowait = !!(args &&
1582 (args->u.normal.via_iocb->ki_flags &
1586 io->ci_obj = ll_i2info(inode)->lli_clob;
1587 io->ci_lockreq = CILR_MAYBE;
1588 if (ll_file_nolock(file)) {
1589 io->ci_lockreq = CILR_NEVER;
1590 io->ci_no_srvlock = 1;
1591 } else if (file->f_flags & O_APPEND) {
1592 io->ci_lockreq = CILR_MANDATORY;
1594 io->ci_noatime = file_is_noatime(file);
1595 io->ci_async_readahead = false;
1597 /* FLR: only use non-delay I/O for read as there is only one
1598 * avaliable mirror for write. */
1599 io->ci_ndelay = !(iot == CIT_WRITE);
1601 ll_io_set_mirror(io, file);
1604 static void ll_heat_add(struct inode *inode, enum cl_io_type iot,
1607 struct ll_inode_info *lli = ll_i2info(inode);
1608 struct ll_sb_info *sbi = ll_i2sbi(inode);
1609 enum obd_heat_type sample_type;
1610 enum obd_heat_type iobyte_type;
1611 __u64 now = ktime_get_real_seconds();
1613 if (!ll_sbi_has_file_heat(sbi) ||
1614 lli->lli_heat_flags & LU_HEAT_FLAG_OFF)
1617 if (iot == CIT_READ) {
1618 sample_type = OBD_HEAT_READSAMPLE;
1619 iobyte_type = OBD_HEAT_READBYTE;
1620 } else if (iot == CIT_WRITE) {
1621 sample_type = OBD_HEAT_WRITESAMPLE;
1622 iobyte_type = OBD_HEAT_WRITEBYTE;
1627 spin_lock(&lli->lli_heat_lock);
1628 obd_heat_add(&lli->lli_heat_instances[sample_type], now, 1,
1629 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1630 obd_heat_add(&lli->lli_heat_instances[iobyte_type], now, count,
1631 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1632 spin_unlock(&lli->lli_heat_lock);
1636 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1637 struct file *file, enum cl_io_type iot,
1638 loff_t *ppos, size_t count)
1640 struct vvp_io *vio = vvp_env_io(env);
1641 struct inode *inode = file_inode(file);
1642 struct ll_inode_info *lli = ll_i2info(inode);
1643 struct ll_sb_info *sbi = ll_i2sbi(inode);
1644 struct ll_file_data *fd = file->private_data;
1645 struct range_lock range;
1646 bool range_locked = false;
1651 unsigned int retried = 0, dio_lock = 0;
1652 bool is_aio = false;
1653 bool is_parallel_dio = false;
1654 struct cl_dio_aio *ci_dio_aio = NULL;
1656 bool partial_io = false;
1657 size_t max_io_pages, max_cached_pages;
1661 CDEBUG(D_VFSTRACE, "%s: %s ppos: %llu, count: %zu\n",
1662 file_dentry(file)->d_name.name,
1663 iot == CIT_READ ? "read" : "write", *ppos, count);
1665 max_io_pages = PTLRPC_MAX_BRW_PAGES * OBD_MAX_RIF_DEFAULT;
1666 max_cached_pages = sbi->ll_cache->ccc_lru_max;
1667 if (max_io_pages > (max_cached_pages >> 2))
1668 max_io_pages = max_cached_pages >> 2;
1670 io = vvp_env_thread_io(env);
1671 if (file->f_flags & O_DIRECT) {
1672 if (file->f_flags & O_APPEND)
1674 if (!is_sync_kiocb(args->u.normal.via_iocb))
1677 /* the kernel does not support AIO on pipes, and parallel DIO
1678 * uses part of the AIO path, so we must not do parallel dio
1681 is_parallel_dio = !iov_iter_is_pipe(args->u.normal.via_iter) &&
1684 if (!ll_sbi_has_parallel_dio(sbi))
1685 is_parallel_dio = false;
1687 ci_dio_aio = cl_dio_aio_alloc(args->u.normal.via_iocb,
1688 ll_i2info(inode)->lli_clob, is_aio);
1690 GOTO(out, rc = -ENOMEM);
1695 * IO block size need be aware of cached page limit, otherwise
1696 * if we have small max_cached_mb but large block IO issued, io
1697 * could not be finished and blocked whole client.
1699 if (file->f_flags & O_DIRECT)
1702 per_bytes = min(max_io_pages << PAGE_SHIFT, count);
1703 partial_io = per_bytes < count;
1704 io = vvp_env_thread_io(env);
1705 ll_io_init(io, file, iot, args);
1706 io->ci_dio_aio = ci_dio_aio;
1707 io->ci_dio_lock = dio_lock;
1708 io->ci_ndelay_tried = retried;
1709 io->ci_parallel_dio = is_parallel_dio;
1711 if (cl_io_rw_init(env, io, iot, *ppos, per_bytes) == 0) {
1712 if (file->f_flags & O_APPEND)
1713 range_lock_init(&range, 0, LUSTRE_EOF);
1715 range_lock_init(&range, *ppos, *ppos + per_bytes - 1);
1717 vio->vui_fd = file->private_data;
1718 vio->vui_iter = args->u.normal.via_iter;
1719 vio->vui_iocb = args->u.normal.via_iocb;
1720 /* Direct IO reads must also take range lock,
1721 * or multiple reads will try to work on the same pages
1722 * See LU-6227 for details.
1724 if (((iot == CIT_WRITE) ||
1725 (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1726 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1727 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1729 rc = range_lock(&lli->lli_write_tree, &range);
1733 range_locked = true;
1736 ll_cl_add(inode, env, io, LCC_RW);
1737 rc = cl_io_loop(env, io);
1738 ll_cl_remove(inode, env);
1740 /* cl_io_rw_init() handled IO */
1744 if (io->ci_dio_aio && !is_aio) {
1745 struct cl_sync_io *anchor = &io->ci_dio_aio->cda_sync;
1747 /* for dio, EIOCBQUEUED is an implementation detail,
1748 * and we don't return it to userspace
1750 if (rc == -EIOCBQUEUED)
1753 /* N/B: parallel DIO may be disabled during i/o submission;
1754 * if that occurs, I/O shifts to sync, so it's all resolved
1755 * before we get here, and this wait call completes
1758 rc2 = cl_sync_io_wait_recycle(env, anchor, 0, 0);
1764 CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1766 range_unlock(&lli->lli_write_tree, &range);
1767 range_locked = false;
1771 * In order to move forward AIO, ci_nob was increased,
1772 * but that doesn't mean io have been finished, it just
1773 * means io have been submited, we will always return
1774 * EIOCBQUEUED to the caller, So we could only return
1775 * number of bytes in non-AIO case.
1777 if (io->ci_nob > 0) {
1780 result += io->ci_nob;
1781 *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
1786 count -= io->ci_nob;
1788 /* prepare IO restart */
1790 args->u.normal.via_iter = vio->vui_iter;
1794 * Reexpand iov count because it was zero
1797 iov_iter_reexpand(vio->vui_iter, count);
1798 if (per_bytes == io->ci_nob)
1799 io->ci_need_restart = 1;
1803 cl_io_fini(env, io);
1806 "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1807 file->f_path.dentry->d_name.name,
1808 iot, rc, result, io->ci_need_restart);
1810 if ((rc == 0 || rc == -ENODATA || rc == -ENOLCK) &&
1811 count > 0 && io->ci_need_restart) {
1813 "%s: restart %s from %lld, count: %zu, ret: %zd, rc: %d\n",
1814 file_dentry(file)->d_name.name,
1815 iot == CIT_READ ? "read" : "write",
1816 *ppos, count, result, rc);
1817 /* preserve the tried count for FLR */
1818 retried = io->ci_ndelay_tried;
1819 dio_lock = io->ci_dio_lock;
1823 if (io->ci_dio_aio) {
1825 * VFS will call aio_complete() if no -EIOCBQUEUED
1826 * is returned for AIO, so we can not call aio_complete()
1829 * NB: This is safe because the atomic_dec_and_lock in
1830 * cl_sync_io_init has implicit memory barriers, so this will
1831 * be seen by whichever thread completes the DIO/AIO, even if
1834 if (rc != -EIOCBQUEUED)
1835 io->ci_dio_aio->cda_no_aio_complete = 1;
1837 * Drop one extra reference so that end_io() could be
1838 * called for this IO context, we could call it after
1839 * we make sure all AIO requests have been proceed.
1841 cl_sync_io_note(env, &io->ci_dio_aio->cda_sync,
1842 rc == -EIOCBQUEUED ? 0 : rc);
1844 LASSERT(io->ci_dio_aio->cda_creator_free);
1845 cl_dio_aio_free(env, io->ci_dio_aio);
1846 io->ci_dio_aio = NULL;
1850 if (iot == CIT_READ) {
1852 ll_stats_ops_tally(ll_i2sbi(inode),
1853 LPROC_LL_READ_BYTES, result);
1854 } else if (iot == CIT_WRITE) {
1856 ll_stats_ops_tally(ll_i2sbi(inode),
1857 LPROC_LL_WRITE_BYTES, result);
1858 fd->fd_write_failed = false;
1859 } else if (result == 0 && rc == 0) {
1862 fd->fd_write_failed = true;
1864 fd->fd_write_failed = false;
1865 } else if (rc != -ERESTARTSYS) {
1866 fd->fd_write_failed = true;
1870 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1872 ll_heat_add(inode, iot, result);
1874 RETURN(result > 0 ? result : rc);
1878 * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1879 * especially for small I/O.
1881 * To serve a read request, CLIO has to create and initialize a cl_io and
1882 * then request DLM lock. This has turned out to have siginificant overhead
1883 * and affects the performance of small I/O dramatically.
1885 * It's not necessary to create a cl_io for each I/O. Under the help of read
1886 * ahead, most of the pages being read are already in memory cache and we can
1887 * read those pages directly because if the pages exist, the corresponding DLM
1888 * lock must exist so that page content must be valid.
1890 * In fast read implementation, the llite speculatively finds and reads pages
1891 * in memory cache. There are three scenarios for fast read:
1892 * - If the page exists and is uptodate, kernel VM will provide the data and
1893 * CLIO won't be intervened;
1894 * - If the page was brought into memory by read ahead, it will be exported
1895 * and read ahead parameters will be updated;
1896 * - Otherwise the page is not in memory, we can't do fast read. Therefore,
1897 * it will go back and invoke normal read, i.e., a cl_io will be created
1898 * and DLM lock will be requested.
1900 * POSIX compliance: posix standard states that read is intended to be atomic.
1901 * Lustre read implementation is in line with Linux kernel read implementation
1902 * and neither of them complies with POSIX standard in this matter. Fast read
1903 * doesn't make the situation worse on single node but it may interleave write
1904 * results from multiple nodes due to short read handling in ll_file_aio_read().
1906 * \param env - lu_env
1907 * \param iocb - kiocb from kernel
1908 * \param iter - user space buffers where the data will be copied
1910 * \retval - number of bytes have been read, or error code if error occurred.
1913 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1917 if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1920 /* NB: we can't do direct IO for fast read because it will need a lock
1921 * to make IO engine happy. */
1922 if (iocb->ki_filp->f_flags & O_DIRECT)
1925 result = generic_file_read_iter(iocb, iter);
1927 /* If the first page is not in cache, generic_file_aio_read() will be
1928 * returned with -ENODATA.
1929 * See corresponding code in ll_readpage(). */
1930 if (result == -ENODATA)
1934 ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
1935 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1936 LPROC_LL_READ_BYTES, result);
1943 * Confine read iter lest read beyond the EOF
1945 * \param iocb [in] kernel iocb
1946 * \param to [in] reader iov_iter
1948 * \retval <0 failure
1950 * \retval >0 @iocb->ki_pos has passed the EOF
1952 static int file_read_confine_iter(struct lu_env *env, struct kiocb *iocb,
1953 struct iov_iter *to)
1955 struct cl_attr *attr = vvp_env_thread_attr(env);
1956 struct file *file = iocb->ki_filp;
1957 struct inode *inode = file_inode(file);
1958 struct ll_inode_info *lli = ll_i2info(inode);
1959 loff_t read_end = iocb->ki_pos + iov_iter_count(to);
1964 cl_object_attr_lock(lli->lli_clob);
1965 rc = cl_object_attr_get(env, lli->lli_clob, attr);
1966 cl_object_attr_unlock(lli->lli_clob);
1970 kms = attr->cat_kms;
1971 /* if read beyond end-of-file, adjust read count */
1972 if (kms > 0 && (iocb->ki_pos >= kms || read_end > kms)) {
1973 rc = ll_glimpse_size(inode);
1977 size = i_size_read(inode);
1978 if (iocb->ki_pos >= size || read_end > size) {
1980 "%s: read [%llu, %llu] over eof, kms %llu, file_size %llu.\n",
1981 file_dentry(file)->d_name.name,
1982 iocb->ki_pos, read_end, kms, size);
1984 if (iocb->ki_pos >= size)
1987 if (read_end > size)
1988 iov_iter_truncate(to, size - iocb->ki_pos);
1996 * Read from a file (through the page cache).
1998 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
2001 struct vvp_io_args *args;
2002 struct file *file = iocb->ki_filp;
2006 ktime_t kstart = ktime_get();
2008 bool stale_data = false;
2012 CDEBUG(D_VFSTRACE|D_IOTRACE, "file %s:"DFID", ppos: %lld, count: %zu\n",
2013 file_dentry(file)->d_name.name,
2014 PFID(ll_inode2fid(file_inode(file))), iocb->ki_pos,
2015 iov_iter_count(to));
2017 if (!iov_iter_count(to))
2020 env = cl_env_get(&refcheck);
2022 RETURN(PTR_ERR(env));
2024 result = file_read_confine_iter(env, iocb, to);
2027 else if (result > 0)
2031 * Currently when PCC read failed, we do not fall back to the
2032 * normal read path, just return the error.
2033 * The resaon is that: for RW-PCC, the file data may be modified
2034 * in the PCC and inconsistent with the data on OSTs (or file
2035 * data has been removed from the Lustre file system), at this
2036 * time, fallback to the normal read path may read the wrong
2038 * TODO: for RO-PCC (readonly PCC), fall back to normal read
2039 * path: read data from data copy on OSTs.
2041 result = pcc_file_read_iter(iocb, to, &cached);
2045 ll_ras_enter(file, iocb->ki_pos, iov_iter_count(to));
2047 result = ll_do_fast_read(iocb, to);
2048 if (result < 0 || iov_iter_count(to) == 0)
2051 args = ll_env_args(env);
2052 args->u.normal.via_iter = to;
2053 args->u.normal.via_iocb = iocb;
2055 rc2 = ll_file_io_generic(env, args, file, CIT_READ,
2056 &iocb->ki_pos, iov_iter_count(to));
2059 else if (result == 0)
2063 cl_env_put(env, &refcheck);
2065 if (stale_data && result > 0) {
2067 * we've reached EOF before the read, the data read are cached
2070 iov_iter_truncate(to, 0);
2075 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
2076 file->private_data, iocb->ki_pos, result,
2078 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_READ,
2079 ktime_us_delta(ktime_get(), kstart));
2083 "COMPLETED: file %s:"DFID", ppos: %lld, count: %zu\n",
2084 file_dentry(file)->d_name.name,
2085 PFID(ll_inode2fid(file_inode(file))), iocb->ki_pos,
2086 iov_iter_count(to));
2092 * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
2093 * If a page is already in the page cache and dirty (and some other things -
2094 * See ll_tiny_write_begin for the instantiation of these rules), then we can
2095 * write to it without doing a full I/O, because Lustre already knows about it
2096 * and will write it out. This saves a lot of processing time.
2098 * All writes here are within one page, so exclusion is handled by the page
2099 * lock on the vm page. We do not do tiny writes for writes which touch
2100 * multiple pages because it's very unlikely multiple sequential pages are
2101 * are already dirty.
2103 * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
2104 * and are unlikely to be to already dirty pages.
2106 * Attribute updates are important here, we do them in ll_tiny_write_end.
2108 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
2110 ssize_t count = iov_iter_count(iter);
2111 struct file *file = iocb->ki_filp;
2112 struct inode *inode = file_inode(file);
2113 bool lock_inode = !IS_NOSEC(inode);
2118 /* Restrict writes to single page and < PAGE_SIZE. See comment at top
2119 * of function for why.
2121 if (count >= PAGE_SIZE ||
2122 (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
2125 if (unlikely(lock_inode))
2127 result = __generic_file_write_iter(iocb, iter);
2129 if (unlikely(lock_inode))
2130 inode_unlock(inode);
2132 /* If the page is not already dirty, ll_tiny_write_begin returns
2133 * -ENODATA. We continue on to normal write.
2135 if (result == -ENODATA)
2139 ll_heat_add(inode, CIT_WRITE, result);
2140 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
2143 CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
2149 * Write to a file (through the page cache).
2151 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2153 struct vvp_io_args *args;
2155 ssize_t rc_tiny = 0, rc_normal;
2156 struct file *file = iocb->ki_filp;
2159 ktime_t kstart = ktime_get();
2164 CDEBUG(D_VFSTRACE|D_IOTRACE, "file %s:"DFID", ppos: %lld, count: %zu\n",
2165 file_dentry(file)->d_name.name,
2166 PFID(ll_inode2fid(file_inode(file))), iocb->ki_pos,
2167 iov_iter_count(from));
2169 if (!iov_iter_count(from))
2170 GOTO(out, rc_normal = 0);
2173 * When PCC write failed, we usually do not fall back to the normal
2174 * write path, just return the error. But there is a special case when
2175 * returned error code is -ENOSPC due to running out of space on PCC HSM
2176 * bakcend. At this time, it will fall back to normal I/O path and
2177 * retry the I/O. As the file is in HSM released state, it will restore
2178 * the file data to OSTs first and redo the write again. And the
2179 * restore process will revoke the layout lock and detach the file
2180 * from PCC cache automatically.
2182 result = pcc_file_write_iter(iocb, from, &cached);
2183 if (cached && result != -ENOSPC && result != -EDQUOT)
2184 GOTO(out, rc_normal = result);
2186 /* NB: we can't do direct IO for tiny writes because they use the page
2187 * cache, we can't do sync writes because tiny writes can't flush
2188 * pages, and we can't do append writes because we can't guarantee the
2189 * required DLM locks are held to protect file size.
2191 if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(file))) &&
2192 !(file->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
2193 rc_tiny = ll_do_tiny_write(iocb, from);
2195 /* In case of error, go on and try normal write - Only stop if tiny
2196 * write completed I/O.
2198 if (iov_iter_count(from) == 0)
2199 GOTO(out, rc_normal = rc_tiny);
2201 env = cl_env_get(&refcheck);
2203 RETURN(PTR_ERR(env));
2205 args = ll_env_args(env);
2206 args->u.normal.via_iter = from;
2207 args->u.normal.via_iocb = iocb;
2209 rc_normal = ll_file_io_generic(env, args, file, CIT_WRITE,
2210 &iocb->ki_pos, iov_iter_count(from));
2212 /* On success, combine bytes written. */
2213 if (rc_tiny >= 0 && rc_normal > 0)
2214 rc_normal += rc_tiny;
2215 /* On error, only return error from normal write if tiny write did not
2216 * write any bytes. Otherwise return bytes written by tiny write.
2218 else if (rc_tiny > 0)
2219 rc_normal = rc_tiny;
2221 cl_env_put(env, &refcheck);
2223 if (rc_normal > 0) {
2224 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
2225 file->private_data, iocb->ki_pos,
2227 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_WRITE,
2228 ktime_us_delta(ktime_get(), kstart));
2232 "COMPLETED: file %s:"DFID", ppos: %lld, count: %zu\n",
2233 file_dentry(file)->d_name.name,
2234 PFID(ll_inode2fid(file_inode(file))), iocb->ki_pos,
2235 iov_iter_count(from));
2240 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
2242 * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
2244 static int ll_file_get_iov_count(const struct iovec *iov,
2245 unsigned long *nr_segs, size_t *count,
2251 for (seg = 0; seg < *nr_segs; seg++) {
2252 const struct iovec *iv = &iov[seg];
2255 * If any segment has a negative length, or the cumulative
2256 * length ever wraps negative then return -EINVAL.
2259 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
2261 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
2266 cnt -= iv->iov_len; /* This segment is no good */
2273 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2274 unsigned long nr_segs, loff_t pos)
2281 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_READ);
2288 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2289 iov_iter_init(&to, READ, iov, nr_segs, iov_count);
2290 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2291 iov_iter_init(&to, iov, nr_segs, iov_count, 0);
2292 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2294 result = ll_file_read_iter(iocb, &to);
2299 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
2302 struct iovec iov = { .iov_base = buf, .iov_len = count };
2311 init_sync_kiocb(&kiocb, file);
2312 kiocb.ki_pos = *ppos;
2313 #ifdef HAVE_KIOCB_KI_LEFT
2314 kiocb.ki_left = count;
2315 #elif defined(HAVE_KI_NBYTES)
2316 kiocb.i_nbytes = count;
2319 result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
2320 *ppos = kiocb.ki_pos;
2326 * Write to a file (through the page cache).
2329 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2330 unsigned long nr_segs, loff_t pos)
2332 struct iov_iter from;
2337 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_WRITE);
2344 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2345 iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
2346 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2347 iov_iter_init(&from, iov, nr_segs, iov_count, 0);
2348 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2350 result = ll_file_write_iter(iocb, &from);
2355 static ssize_t ll_file_write(struct file *file, const char __user *buf,
2356 size_t count, loff_t *ppos)
2358 struct iovec iov = { .iov_base = (void __user *)buf,
2368 init_sync_kiocb(&kiocb, file);
2369 kiocb.ki_pos = *ppos;
2370 #ifdef HAVE_KIOCB_KI_LEFT
2371 kiocb.ki_left = count;
2372 #elif defined(HAVE_KI_NBYTES)
2373 kiocb.ki_nbytes = count;
2376 result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
2377 *ppos = kiocb.ki_pos;
2381 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
2383 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
2384 __u64 flags, struct lov_user_md *lum, int lum_size)
2386 struct lookup_intent oit = {
2388 .it_flags = flags | MDS_OPEN_BY_FID,
2393 if ((__swab32(lum->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
2394 le32_to_cpu(LOV_MAGIC_MAGIC)) {
2395 /* this code will only exist for big-endian systems */
2396 lustre_swab_lov_user_md(lum, 0);
2399 ll_inode_size_lock(inode);
2400 rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
2402 GOTO(out_unlock, rc);
2404 ll_release_openhandle(dentry, &oit);
2407 ll_inode_size_unlock(inode);
2408 ll_intent_release(&oit);
2413 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
2414 struct lov_mds_md **lmmp, int *lmm_size,
2415 struct ptlrpc_request **request)
2417 struct ll_sb_info *sbi = ll_i2sbi(inode);
2418 struct mdt_body *body;
2419 struct lov_mds_md *lmm = NULL;
2420 struct ptlrpc_request *req = NULL;
2421 struct md_op_data *op_data;
2426 rc = ll_get_default_mdsize(sbi, &lmmsize);
2430 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
2431 strlen(filename), lmmsize,
2432 LUSTRE_OPC_ANY, NULL);
2433 if (IS_ERR(op_data))
2434 RETURN(PTR_ERR(op_data));
2436 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
2437 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
2438 ll_finish_md_op_data(op_data);
2440 CDEBUG(D_INFO, "md_getattr_name failed on %s: rc %d\n",
2445 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2446 LASSERT(body != NULL); /* checked by mdc_getattr_name */
2448 lmmsize = body->mbo_eadatasize;
2450 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
2452 GOTO(out, rc = -ENODATA);
2454 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
2455 LASSERT(lmm != NULL);
2457 if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
2458 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
2459 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1) &&
2460 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_FOREIGN))
2461 GOTO(out, rc = -EPROTO);
2464 * This is coming from the MDS, so is probably in
2465 * little endian. We convert it to host endian before
2466 * passing it to userspace.
2468 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) {
2469 int stripe_count = 0;
2471 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
2472 lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2473 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
2474 if (le32_to_cpu(lmm->lmm_pattern) &
2475 LOV_PATTERN_F_RELEASED)
2477 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
2479 /* if function called for directory - we should
2480 * avoid swab not existent lsm objects
2482 if (lmm->lmm_magic == LOV_MAGIC_V1 &&
2483 S_ISREG(body->mbo_mode))
2484 lustre_swab_lov_user_md_objects(
2485 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
2487 else if (lmm->lmm_magic == LOV_MAGIC_V3 &&
2488 S_ISREG(body->mbo_mode))
2489 lustre_swab_lov_user_md_objects(
2490 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
2492 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1)) {
2493 lustre_swab_lov_comp_md_v1(
2494 (struct lov_comp_md_v1 *)lmm);
2498 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
2499 struct lov_comp_md_v1 *comp_v1 = NULL;
2500 struct lov_comp_md_entry_v1 *ent;
2501 struct lov_user_md_v1 *v1;
2505 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2506 /* Dump the striping information */
2507 for (; i < comp_v1->lcm_entry_count; i++) {
2508 ent = &comp_v1->lcm_entries[i];
2509 off = ent->lcme_offset;
2510 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2512 "comp[%d]: stripe_count=%u, stripe_size=%u\n",
2513 i, v1->lmm_stripe_count, v1->lmm_stripe_size);
2517 * Return valid stripe_count and stripe_size instead of 0 for
2518 * DoM files to avoid divide-by-zero for older userspace that
2519 * calls this ioctl, e.g. lustre ADIO driver.
2521 if (lmm->lmm_stripe_count == 0)
2522 lmm->lmm_stripe_count = 1;
2523 if (lmm->lmm_stripe_size == 0) {
2524 /* Since the first component of the file data is placed
2525 * on the MDT for faster access, the stripe_size of the
2526 * second one is always that applications which are
2529 if (lmm->lmm_pattern == LOV_PATTERN_MDT)
2530 i = comp_v1->lcm_entry_count > 1 ? 1 : 0;
2532 i = comp_v1->lcm_entry_count > 1 ?
2533 comp_v1->lcm_entry_count - 1 : 0;
2534 ent = &comp_v1->lcm_entries[i];
2535 off = ent->lcme_offset;
2536 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2537 lmm->lmm_stripe_size = v1->lmm_stripe_size;
2542 *lmm_size = lmmsize;
2547 static int ll_lov_setea(struct inode *inode, struct file *file,
2550 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
2551 struct lov_user_md *lump;
2552 int lum_size = sizeof(struct lov_user_md) +
2553 sizeof(struct lov_user_ost_data);
2557 if (!capable(CAP_SYS_ADMIN))
2560 OBD_ALLOC_LARGE(lump, lum_size);
2564 if (copy_from_user(lump, arg, lum_size))
2565 GOTO(out_lump, rc = -EFAULT);
2567 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
2569 cl_lov_delay_create_clear(&file->f_flags);
2572 OBD_FREE_LARGE(lump, lum_size);
2576 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
2583 env = cl_env_get(&refcheck);
2585 RETURN(PTR_ERR(env));
2587 rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
2588 cl_env_put(env, &refcheck);
2592 static int ll_lov_setstripe(struct inode *inode, struct file *file,
2595 struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
2596 struct lov_user_md *klum;
2598 __u64 flags = FMODE_WRITE;
2601 rc = ll_copy_user_md(lum, &klum);
2606 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2611 rc = put_user(0, &lum->lmm_stripe_count);
2615 rc = ll_layout_refresh(inode, &gen);
2619 rc = ll_file_getstripe(inode, arg, lum_size);
2620 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode) &&
2621 ll_i2info(inode)->lli_clob) {
2622 struct iattr attr = { 0 };
2624 rc = cl_setattr_ost(ll_i2info(inode)->lli_clob, &attr,
2625 OP_XVALID_FLAGS, LUSTRE_ENCRYPT_FL);
2628 cl_lov_delay_create_clear(&file->f_flags);
2631 OBD_FREE_LARGE(klum, lum_size);
2636 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2638 struct ll_inode_info *lli = ll_i2info(inode);
2639 struct cl_object *obj = lli->lli_clob;
2640 struct ll_file_data *fd = file->private_data;
2641 struct ll_grouplock grouplock;
2646 CWARN("group id for group lock must not be 0\n");
2650 if (ll_file_nolock(file))
2651 RETURN(-EOPNOTSUPP);
2653 read_lock(&lli->lli_lock);
2654 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2655 CWARN("group lock already existed with gid %lu\n",
2656 fd->fd_grouplock.lg_gid);
2657 read_unlock(&lli->lli_lock);
2660 LASSERT(fd->fd_grouplock.lg_lock == NULL);
2661 read_unlock(&lli->lli_lock);
2664 * XXX: group lock needs to protect all OST objects while PFL
2665 * can add new OST objects during the IO, so we'd instantiate
2666 * all OST objects before getting its group lock.
2671 struct cl_layout cl = {
2672 .cl_is_composite = false,
2674 struct lu_extent ext = {
2676 .e_end = OBD_OBJECT_EOF,
2679 env = cl_env_get(&refcheck);
2681 RETURN(PTR_ERR(env));
2683 rc = cl_object_layout_get(env, obj, &cl);
2684 if (rc >= 0 && cl.cl_is_composite)
2685 rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2688 cl_env_put(env, &refcheck);
2693 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2694 arg, (file->f_flags & O_NONBLOCK), &grouplock);
2698 write_lock(&lli->lli_lock);
2699 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2700 write_unlock(&lli->lli_lock);
2701 CERROR("another thread just won the race\n");
2702 cl_put_grouplock(&grouplock);
2706 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2707 fd->fd_grouplock = grouplock;
2708 write_unlock(&lli->lli_lock);
2710 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2714 static int ll_put_grouplock(struct inode *inode, struct file *file,
2717 struct ll_inode_info *lli = ll_i2info(inode);
2718 struct ll_file_data *fd = file->private_data;
2719 struct ll_grouplock grouplock;
2722 write_lock(&lli->lli_lock);
2723 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2724 write_unlock(&lli->lli_lock);
2725 CWARN("no group lock held\n");
2729 LASSERT(fd->fd_grouplock.lg_lock != NULL);
2731 if (fd->fd_grouplock.lg_gid != arg) {
2732 CWARN("group lock %lu doesn't match current id %lu\n",
2733 arg, fd->fd_grouplock.lg_gid);
2734 write_unlock(&lli->lli_lock);
2738 grouplock = fd->fd_grouplock;
2739 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2740 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2741 write_unlock(&lli->lli_lock);
2743 cl_put_grouplock(&grouplock);
2744 CDEBUG(D_INFO, "group lock %lu released\n", arg);
2749 * Close inode open handle
2751 * \param dentry [in] dentry which contains the inode
2752 * \param it [in,out] intent which contains open info and result
2755 * \retval <0 failure
2757 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2759 struct inode *inode = dentry->d_inode;
2760 struct obd_client_handle *och;
2766 /* Root ? Do nothing. */
2767 if (is_root_inode(inode))
2770 /* No open handle to close? Move away */
2771 if (!it_disposition(it, DISP_OPEN_OPEN))
2774 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2776 OBD_ALLOC(och, sizeof(*och));
2778 GOTO(out, rc = -ENOMEM);
2780 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2784 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2786 /* this one is in place of ll_file_open */
2787 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2788 ptlrpc_req_finished(it->it_request);
2789 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2795 * Get size for inode for which FIEMAP mapping is requested.
2796 * Make the FIEMAP get_info call and returns the result.
2797 * \param fiemap kernel buffer to hold extens
2798 * \param num_bytes kernel buffer size
2800 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2806 struct ll_fiemap_info_key fmkey = { .lfik_name = KEY_FIEMAP, };
2809 /* Checks for fiemap flags */
2810 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2811 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2815 /* Check for FIEMAP_FLAG_SYNC */
2816 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2817 rc = filemap_fdatawrite(inode->i_mapping);
2822 env = cl_env_get(&refcheck);
2824 RETURN(PTR_ERR(env));
2826 if (i_size_read(inode) == 0) {
2827 rc = ll_glimpse_size(inode);
2832 fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLPROJID;
2833 obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2834 obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2836 /* If filesize is 0, then there would be no objects for mapping */
2837 if (fmkey.lfik_oa.o_size == 0) {
2838 fiemap->fm_mapped_extents = 0;
2842 fmkey.lfik_fiemap = *fiemap;
2844 rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2845 &fmkey, fiemap, &num_bytes);
2847 cl_env_put(env, &refcheck);
2851 int ll_fid2path(struct inode *inode, void __user *arg)
2853 struct obd_export *exp = ll_i2mdexp(inode);
2854 const struct getinfo_fid2path __user *gfin = arg;
2856 struct getinfo_fid2path *gfout;
2862 if (!capable(CAP_DAC_READ_SEARCH) &&
2863 !test_bit(LL_SBI_USER_FID2PATH, ll_i2sbi(inode)->ll_flags))
2866 /* Only need to get the buflen */
2867 if (get_user(pathlen, &gfin->gf_pathlen))
2870 if (pathlen > PATH_MAX)
2873 outsize = sizeof(*gfout) + pathlen;
2874 OBD_ALLOC(gfout, outsize);
2878 if (copy_from_user(gfout, arg, sizeof(*gfout)))
2879 GOTO(gf_free, rc = -EFAULT);
2880 /* append root FID after gfout to let MDT know the root FID so that it
2881 * can lookup the correct path, this is mainly for fileset.
2882 * old server without fileset mount support will ignore this. */
2883 *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
2885 /* Call mdc_iocontrol */
2886 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
2890 if (copy_to_user(arg, gfout, outsize))
2894 OBD_FREE(gfout, outsize);
2899 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
2901 struct cl_object *obj = ll_i2info(inode)->lli_clob;
2909 ioc->idv_version = 0;
2910 ioc->idv_layout_version = UINT_MAX;
2912 /* If no file object initialized, we consider its version is 0. */
2916 env = cl_env_get(&refcheck);
2918 RETURN(PTR_ERR(env));
2920 io = vvp_env_thread_io(env);
2922 io->u.ci_data_version.dv_data_version = 0;
2923 io->u.ci_data_version.dv_layout_version = UINT_MAX;
2924 io->u.ci_data_version.dv_flags = ioc->idv_flags;
2927 if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
2928 result = cl_io_loop(env, io);
2930 result = io->ci_result;
2932 ioc->idv_version = io->u.ci_data_version.dv_data_version;
2933 ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
2935 cl_io_fini(env, io);
2937 if (unlikely(io->ci_need_restart))
2940 cl_env_put(env, &refcheck);
2946 * Read the data_version for inode.
2948 * This value is computed using stripe object version on OST.
2949 * Version is computed using server side locking.
2951 * @param flags if do sync on the OST side;
2953 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
2954 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
2956 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
2958 struct ioc_data_version ioc = { .idv_flags = flags };
2961 rc = ll_ioc_data_version(inode, &ioc);
2963 *data_version = ioc.idv_version;
2969 * Trigger a HSM release request for the provided inode.
2971 int ll_hsm_release(struct inode *inode)
2974 struct obd_client_handle *och = NULL;
2975 __u64 data_version = 0;
2981 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2982 ll_i2sbi(inode)->ll_fsname,
2983 PFID(&ll_i2info(inode)->lli_fid));
2985 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2987 GOTO(out, rc = PTR_ERR(och));
2989 /* Grab latest data_version and [am]time values */
2990 rc = ll_data_version(inode, &data_version,
2991 LL_DV_WR_FLUSH | LL_DV_SZ_UPDATE);
2995 env = cl_env_get(&refcheck);
2997 GOTO(out, rc = PTR_ERR(env));
2999 rc = ll_merge_attr(env, inode);
3000 cl_env_put(env, &refcheck);
3002 /* If error happen, we have the wrong size for a file.
3008 /* Release the file.
3009 * NB: lease lock handle is released in mdc_hsm_release_pack() because
3010 * we still need it to pack l_remote_handle to MDT. */
3011 rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
3017 if (och != NULL && !IS_ERR(och)) /* close the file */
3018 ll_lease_close(och, inode, NULL);
3023 struct ll_swap_stack {
3026 struct inode *inode1;
3027 struct inode *inode2;
3032 static int ll_swap_layouts(struct file *file1, struct file *file2,
3033 struct lustre_swap_layouts *lsl)
3035 struct mdc_swap_layouts msl;
3036 struct md_op_data *op_data;
3039 struct ll_swap_stack *llss = NULL;
3042 OBD_ALLOC_PTR(llss);
3046 llss->inode1 = file_inode(file1);
3047 llss->inode2 = file_inode(file2);
3049 rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
3053 /* we use 2 bool because it is easier to swap than 2 bits */
3054 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
3055 llss->check_dv1 = true;
3057 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
3058 llss->check_dv2 = true;
3060 /* we cannot use lsl->sl_dvX directly because we may swap them */
3061 llss->dv1 = lsl->sl_dv1;
3062 llss->dv2 = lsl->sl_dv2;
3064 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
3065 if (rc == 0) /* same file, done! */
3068 if (rc < 0) { /* sequentialize it */
3069 swap(llss->inode1, llss->inode2);
3071 swap(llss->dv1, llss->dv2);
3072 swap(llss->check_dv1, llss->check_dv2);
3076 if (gid != 0) { /* application asks to flush dirty cache */
3077 rc = ll_get_grouplock(llss->inode1, file1, gid);
3081 rc = ll_get_grouplock(llss->inode2, file2, gid);
3083 ll_put_grouplock(llss->inode1, file1, gid);
3088 /* ultimate check, before swaping the layouts we check if
3089 * dataversion has changed (if requested) */
3090 if (llss->check_dv1) {
3091 rc = ll_data_version(llss->inode1, &dv, 0);
3094 if (dv != llss->dv1)
3095 GOTO(putgl, rc = -EAGAIN);
3098 if (llss->check_dv2) {
3099 rc = ll_data_version(llss->inode2, &dv, 0);
3102 if (dv != llss->dv2)
3103 GOTO(putgl, rc = -EAGAIN);
3106 /* struct md_op_data is used to send the swap args to the mdt
3107 * only flags is missing, so we use struct mdc_swap_layouts
3108 * through the md_op_data->op_data */
3109 /* flags from user space have to be converted before they are send to
3110 * server, no flag is sent today, they are only used on the client */
3113 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
3114 0, LUSTRE_OPC_ANY, &msl);
3115 if (IS_ERR(op_data))
3116 GOTO(free, rc = PTR_ERR(op_data));
3118 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
3119 sizeof(*op_data), op_data, NULL);
3120 ll_finish_md_op_data(op_data);
3127 ll_put_grouplock(llss->inode2, file2, gid);
3128 ll_put_grouplock(llss->inode1, file1, gid);
3138 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
3140 struct obd_export *exp = ll_i2mdexp(inode);
3141 struct md_op_data *op_data;
3145 /* Detect out-of range masks */
3146 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
3149 /* Non-root users are forbidden to set or clear flags which are
3150 * NOT defined in HSM_USER_MASK. */
3151 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
3152 !capable(CAP_SYS_ADMIN))
3155 if (!exp_connect_archive_id_array(exp)) {
3156 /* Detect out-of range archive id */
3157 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
3158 (hss->hss_archive_id > LL_HSM_ORIGIN_MAX_ARCHIVE))
3162 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3163 LUSTRE_OPC_ANY, hss);
3164 if (IS_ERR(op_data))
3165 RETURN(PTR_ERR(op_data));
3167 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, exp, sizeof(*op_data),
3170 ll_finish_md_op_data(op_data);
3175 static int ll_hsm_import(struct inode *inode, struct file *file,
3176 struct hsm_user_import *hui)
3178 struct hsm_state_set *hss = NULL;
3179 struct iattr *attr = NULL;
3183 if (!S_ISREG(inode->i_mode))
3189 GOTO(out, rc = -ENOMEM);
3191 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
3192 hss->hss_archive_id = hui->hui_archive_id;
3193 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
3194 rc = ll_hsm_state_set(inode, hss);
3198 OBD_ALLOC_PTR(attr);
3200 GOTO(out, rc = -ENOMEM);
3202 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
3203 attr->ia_mode |= S_IFREG;
3204 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
3205 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
3206 attr->ia_size = hui->hui_size;
3207 attr->ia_mtime.tv_sec = hui->hui_mtime;
3208 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
3209 attr->ia_atime.tv_sec = hui->hui_atime;
3210 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
3212 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
3213 ATTR_UID | ATTR_GID |
3214 ATTR_MTIME | ATTR_MTIME_SET |
3215 ATTR_ATIME | ATTR_ATIME_SET;
3219 rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
3223 inode_unlock(inode);
3235 static inline long ll_lease_type_from_fmode(fmode_t fmode)
3237 return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
3238 ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
3241 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
3243 struct inode *inode = file_inode(file);
3245 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
3246 ATTR_MTIME | ATTR_MTIME_SET |
3249 .tv_sec = lfu->lfu_atime_sec,
3250 .tv_nsec = lfu->lfu_atime_nsec,
3253 .tv_sec = lfu->lfu_mtime_sec,
3254 .tv_nsec = lfu->lfu_mtime_nsec,
3257 .tv_sec = lfu->lfu_ctime_sec,
3258 .tv_nsec = lfu->lfu_ctime_nsec,
3264 if (!capable(CAP_SYS_ADMIN))
3267 if (!S_ISREG(inode->i_mode))
3271 rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
3273 inode_unlock(inode);
3278 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
3281 case MODE_READ_USER:
3283 case MODE_WRITE_USER:
3290 static const char *const user_lockname[] = LOCK_MODE_NAMES;
3292 /* Used to allow the upper layers of the client to request an LDLM lock
3293 * without doing an actual read or write.
3295 * Used for ladvise lockahead to manually request specific locks.
3297 * \param[in] file file this ladvise lock request is on
3298 * \param[in] ladvise ladvise struct describing this lock request
3300 * \retval 0 success, no detailed result available (sync requests
3301 * and requests sent to the server [not handled locally]
3302 * cannot return detailed results)
3303 * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
3304 * see definitions for details.
3305 * \retval negative negative errno on error
3307 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
3309 struct lu_env *env = NULL;
3310 struct cl_io *io = NULL;
3311 struct cl_lock *lock = NULL;
3312 struct cl_lock_descr *descr = NULL;
3313 struct dentry *dentry = file->f_path.dentry;
3314 struct inode *inode = dentry->d_inode;
3315 enum cl_lock_mode cl_mode;
3316 off_t start = ladvise->lla_start;
3317 off_t end = ladvise->lla_end;
3324 "Lock request: file=%pd, inode=%p, mode=%s start=%llu, end=%llu\n",
3325 dentry, dentry->d_inode,
3326 user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
3329 cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
3331 GOTO(out, result = cl_mode);
3333 /* Get IO environment */
3334 result = cl_io_get(inode, &env, &io, &refcheck);
3338 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
3341 * nothing to do for this io. This currently happens when
3342 * stripe sub-object's are not yet created.
3344 result = io->ci_result;
3345 } else if (result == 0) {
3346 lock = vvp_env_lock(env);
3347 descr = &lock->cll_descr;
3349 descr->cld_obj = io->ci_obj;
3350 /* Convert byte offsets to pages */
3351 descr->cld_start = cl_index(io->ci_obj, start);
3352 descr->cld_end = cl_index(io->ci_obj, end);
3353 descr->cld_mode = cl_mode;
3354 /* CEF_MUST is used because we do not want to convert a
3355 * lockahead request to a lockless lock */
3356 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND;
3358 if (ladvise->lla_peradvice_flags & LF_ASYNC)
3359 descr->cld_enq_flags |= CEF_SPECULATIVE;
3361 result = cl_lock_request(env, io, lock);
3363 /* On success, we need to release the lock */
3365 cl_lock_release(env, lock);
3367 cl_io_fini(env, io);
3368 cl_env_put(env, &refcheck);
3370 /* -ECANCELED indicates a matching lock with a different extent
3371 * was already present, and -EEXIST indicates a matching lock
3372 * on exactly the same extent was already present.
3373 * We convert them to positive values for userspace to make
3374 * recognizing true errors easier.
3375 * Note we can only return these detailed results on async requests,
3376 * as sync requests look the same as i/o requests for locking. */
3377 if (result == -ECANCELED)
3378 result = LLA_RESULT_DIFFERENT;
3379 else if (result == -EEXIST)
3380 result = LLA_RESULT_SAME;
3385 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
3387 static int ll_ladvise_sanity(struct inode *inode,
3388 struct llapi_lu_ladvise *ladvise)
3390 struct ll_sb_info *sbi = ll_i2sbi(inode);
3391 enum lu_ladvise_type advice = ladvise->lla_advice;
3392 /* Note the peradvice flags is a 32 bit field, so per advice flags must
3393 * be in the first 32 bits of enum ladvise_flags */
3394 __u32 flags = ladvise->lla_peradvice_flags;
3395 /* 3 lines at 80 characters per line, should be plenty */
3398 if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
3401 "%s: advice with value '%d' not recognized, last supported advice is %s (value '%d'): rc = %d\n",
3402 sbi->ll_fsname, advice,
3403 ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
3407 /* Per-advice checks */
3409 case LU_LADVISE_LOCKNOEXPAND:
3410 if (flags & ~LF_LOCKNOEXPAND_MASK) {
3412 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3413 "rc = %d\n", sbi->ll_fsname, flags,
3414 ladvise_names[advice], rc);
3418 case LU_LADVISE_LOCKAHEAD:
3419 /* Currently only READ and WRITE modes can be requested */
3420 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
3421 ladvise->lla_lockahead_mode == 0) {
3423 CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
3424 "rc = %d\n", sbi->ll_fsname,
3425 ladvise->lla_lockahead_mode,
3426 ladvise_names[advice], rc);
3430 case LU_LADVISE_WILLREAD:
3431 case LU_LADVISE_DONTNEED:
3433 /* Note fall through above - These checks apply to all advices
3434 * except LOCKNOEXPAND */
3435 if (flags & ~LF_DEFAULT_MASK) {
3437 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3438 "rc = %d\n", sbi->ll_fsname, flags,
3439 ladvise_names[advice], rc);
3442 if (ladvise->lla_start >= ladvise->lla_end) {
3444 CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
3445 "for %s: rc = %d\n", sbi->ll_fsname,
3446 ladvise->lla_start, ladvise->lla_end,
3447 ladvise_names[advice], rc);
3459 * Give file access advices
3461 * The ladvise interface is similar to Linux fadvise() system call, except it
3462 * forwards the advices directly from Lustre client to server. The server side
3463 * codes will apply appropriate read-ahead and caching techniques for the
3464 * corresponding files.
3466 * A typical workload for ladvise is e.g. a bunch of different clients are
3467 * doing small random reads of a file, so prefetching pages into OSS cache
3468 * with big linear reads before the random IO is a net benefit. Fetching
3469 * all that data into each client cache with fadvise() may not be, due to
3470 * much more data being sent to the client.
3472 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
3473 struct llapi_lu_ladvise *ladvise)
3477 struct cl_ladvise_io *lio;
3482 env = cl_env_get(&refcheck);
3484 RETURN(PTR_ERR(env));
3486 io = vvp_env_thread_io(env);
3487 io->ci_obj = ll_i2info(inode)->lli_clob;
3489 /* initialize parameters for ladvise */
3490 lio = &io->u.ci_ladvise;
3491 lio->li_start = ladvise->lla_start;
3492 lio->li_end = ladvise->lla_end;
3493 lio->li_fid = ll_inode2fid(inode);
3494 lio->li_advice = ladvise->lla_advice;
3495 lio->li_flags = flags;
3497 if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
3498 rc = cl_io_loop(env, io);
3502 cl_io_fini(env, io);
3503 cl_env_put(env, &refcheck);
3507 static int ll_lock_noexpand(struct file *file, int flags)
3509 struct ll_file_data *fd = file->private_data;
3511 fd->ll_lock_no_expand = !(flags & LF_UNSET);
3516 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
3519 struct fsxattr fsxattr;
3521 if (copy_from_user(&fsxattr,
3522 (const struct fsxattr __user *)arg,
3526 fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
3527 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
3528 fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
3529 fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
3530 if (copy_to_user((struct fsxattr __user *)arg,
3531 &fsxattr, sizeof(fsxattr)))
3537 int ll_ioctl_check_project(struct inode *inode, __u32 xflags,
3541 * Project Quota ID state is only allowed to change from within the init
3542 * namespace. Enforce that restriction only if we are trying to change
3543 * the quota ID state. Everything else is allowed in user namespaces.
3545 if (current_user_ns() == &init_user_ns) {
3547 * Caller is allowed to change the project ID. if it is being
3548 * changed, make sure that the new value is valid.
3550 if (ll_i2info(inode)->lli_projid != projid &&
3551 !projid_valid(make_kprojid(&init_user_ns, projid)))
3557 if (ll_i2info(inode)->lli_projid != projid)
3560 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags)) {
3561 if (!(xflags & FS_XFLAG_PROJINHERIT))
3564 if (xflags & FS_XFLAG_PROJINHERIT)
3571 static int ll_set_project(struct inode *inode, __u32 xflags, __u32 projid)
3573 struct ptlrpc_request *req = NULL;
3574 struct md_op_data *op_data;
3575 struct cl_object *obj;
3576 unsigned int inode_flags;
3579 CDEBUG(D_QUOTA, DFID" xflags=%x projid=%u\n",
3580 PFID(ll_inode2fid(inode)), xflags, projid);
3581 rc = ll_ioctl_check_project(inode, xflags, projid);
3585 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3586 LUSTRE_OPC_ANY, NULL);
3587 if (IS_ERR(op_data))
3588 RETURN(PTR_ERR(op_data));
3590 inode_flags = ll_xflags_to_inode_flags(xflags);
3591 op_data->op_attr_flags = ll_inode_to_ext_flags(inode_flags);
3592 if (xflags & FS_XFLAG_PROJINHERIT)
3593 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
3595 /* pass projid to md_op_data */
3596 op_data->op_projid = projid;
3598 op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
3599 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL, 0, &req);
3600 ptlrpc_req_finished(req);
3602 GOTO(out_fsxattr, rc);
3603 ll_update_inode_flags(inode, op_data->op_attr_flags);
3605 /* Avoid OST RPC if this is only ioctl setting project inherit flag */
3606 if (xflags == 0 || xflags == FS_XFLAG_PROJINHERIT)
3607 GOTO(out_fsxattr, rc);
3609 obj = ll_i2info(inode)->lli_clob;
3611 struct iattr attr = { 0 };
3613 rc = cl_setattr_ost(obj, &attr, OP_XVALID_FLAGS, xflags);
3617 ll_finish_md_op_data(op_data);
3621 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
3624 struct fsxattr fsxattr;
3628 if (copy_from_user(&fsxattr,
3629 (const struct fsxattr __user *)arg,
3633 RETURN(ll_set_project(inode, fsxattr.fsx_xflags,
3634 fsxattr.fsx_projid));
3637 int ll_ioctl_project(struct file *file, unsigned int cmd,
3640 struct lu_project lu_project;
3641 struct dentry *dentry = file_dentry(file);
3642 struct inode *inode = file_inode(file);
3643 struct dentry *child_dentry = NULL;
3644 int rc = 0, name_len;
3646 if (copy_from_user(&lu_project,
3647 (const struct lu_project __user *)arg,
3648 sizeof(lu_project)))
3651 /* apply child dentry if name is valid */
3652 name_len = strnlen(lu_project.project_name, NAME_MAX);
3653 if (name_len > 0 && name_len <= NAME_MAX) {
3655 child_dentry = lookup_one_len(lu_project.project_name,
3657 inode_unlock(inode);
3658 if (IS_ERR(child_dentry)) {
3659 rc = PTR_ERR(child_dentry);
3662 inode = child_dentry->d_inode;
3667 } else if (name_len > NAME_MAX) {
3672 switch (lu_project.project_type) {
3673 case LU_PROJECT_SET:
3674 rc = ll_set_project(inode, lu_project.project_xflags,
3675 lu_project.project_id);
3677 case LU_PROJECT_GET:
3678 lu_project.project_xflags =
3679 ll_inode_flags_to_xflags(inode->i_flags);
3680 if (test_bit(LLIF_PROJECT_INHERIT,
3681 &ll_i2info(inode)->lli_flags))
3682 lu_project.project_xflags |= FS_XFLAG_PROJINHERIT;
3683 lu_project.project_id = ll_i2info(inode)->lli_projid;
3684 if (copy_to_user((struct lu_project __user *)arg,
3685 &lu_project, sizeof(lu_project))) {
3695 if (!IS_ERR_OR_NULL(child_dentry))
3700 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
3703 struct inode *inode = file_inode(file);
3704 struct ll_file_data *fd = file->private_data;
3705 struct ll_inode_info *lli = ll_i2info(inode);
3706 struct obd_client_handle *och = NULL;
3707 struct split_param sp;
3708 struct pcc_param param;
3709 bool lease_broken = false;
3711 enum mds_op_bias bias = 0;
3713 struct file *layout_file = NULL;
3715 size_t data_size = 0;
3716 bool attached = false;
3721 mutex_lock(&lli->lli_och_mutex);
3722 if (fd->fd_lease_och != NULL) {
3723 och = fd->fd_lease_och;
3724 fd->fd_lease_och = NULL;
3726 mutex_unlock(&lli->lli_och_mutex);
3731 fmode = och->och_flags;
3733 switch (ioc->lil_flags) {
3734 case LL_LEASE_RESYNC_DONE:
3735 if (ioc->lil_count > IOC_IDS_MAX)
3736 GOTO(out_lease_close, rc = -EINVAL);
3738 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
3739 OBD_ALLOC(data, data_size);
3741 GOTO(out_lease_close, rc = -ENOMEM);
3743 if (copy_from_user(data, (void __user *)arg, data_size))
3744 GOTO(out_lease_close, rc = -EFAULT);
3746 bias = MDS_CLOSE_RESYNC_DONE;
3748 case LL_LEASE_LAYOUT_MERGE: {
3750 if (ioc->lil_count != 1)
3751 GOTO(out_lease_close, rc = -EINVAL);
3753 arg += sizeof(*ioc);
3754 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3755 GOTO(out_lease_close, rc = -EFAULT);
3757 layout_file = fget(fdv);
3759 GOTO(out_lease_close, rc = -EBADF);
3761 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
3762 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
3763 GOTO(out_lease_close, rc = -EPERM);
3765 data = file_inode(layout_file);
3766 bias = MDS_CLOSE_LAYOUT_MERGE;
3769 case LL_LEASE_LAYOUT_SPLIT: {
3772 if (ioc->lil_count != 2)
3773 GOTO(out_lease_close, rc = -EINVAL);
3775 arg += sizeof(*ioc);
3776 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3777 GOTO(out_lease_close, rc = -EFAULT);
3779 arg += sizeof(__u32);
3780 if (copy_from_user(&mirror_id, (void __user *)arg,
3782 GOTO(out_lease_close, rc = -EFAULT);
3784 layout_file = fget(fdv);
3786 GOTO(out_lease_close, rc = -EBADF);
3788 /* if layout_file == file, it means to destroy the mirror */
3789 sp.sp_inode = file_inode(layout_file);
3790 sp.sp_mirror_id = (__u16)mirror_id;
3792 bias = MDS_CLOSE_LAYOUT_SPLIT;
3795 case LL_LEASE_PCC_ATTACH:
3796 if (ioc->lil_count != 1)
3799 if (IS_ENCRYPTED(inode))
3800 RETURN(-EOPNOTSUPP);
3802 arg += sizeof(*ioc);
3803 if (copy_from_user(¶m.pa_archive_id, (void __user *)arg,
3805 GOTO(out_lease_close, rc2 = -EFAULT);
3807 rc2 = pcc_readwrite_attach(file, inode, param.pa_archive_id);
3809 GOTO(out_lease_close, rc2);
3812 /* Grab latest data version */
3813 rc2 = ll_data_version(inode, ¶m.pa_data_version,
3816 GOTO(out_lease_close, rc2);
3819 bias = MDS_PCC_ATTACH;
3822 /* without close intent */
3827 rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
3831 rc = ll_lease_och_release(inode, file);
3840 if (ioc->lil_flags == LL_LEASE_RESYNC_DONE && data)
3841 OBD_FREE(data, data_size);
3846 if (ioc->lil_flags == LL_LEASE_PCC_ATTACH) {
3849 rc = pcc_readwrite_attach_fini(file, inode,
3850 param.pa_layout_gen,
3855 ll_layout_refresh(inode, &fd->fd_layout_version);
3858 rc = ll_lease_type_from_fmode(fmode);
3862 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
3865 struct inode *inode = file_inode(file);
3866 struct ll_inode_info *lli = ll_i2info(inode);
3867 struct ll_file_data *fd = file->private_data;
3868 struct obd_client_handle *och = NULL;
3869 __u64 open_flags = 0;
3875 switch (ioc->lil_mode) {
3876 case LL_LEASE_WRLCK:
3877 if (!(file->f_mode & FMODE_WRITE))
3879 fmode = FMODE_WRITE;
3881 case LL_LEASE_RDLCK:
3882 if (!(file->f_mode & FMODE_READ))
3886 case LL_LEASE_UNLCK:
3887 RETURN(ll_file_unlock_lease(file, ioc, arg));
3892 CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
3894 /* apply for lease */
3895 if (ioc->lil_flags & LL_LEASE_RESYNC)
3896 open_flags = MDS_OPEN_RESYNC;
3897 och = ll_lease_open(inode, file, fmode, open_flags);
3899 RETURN(PTR_ERR(och));
3901 if (ioc->lil_flags & LL_LEASE_RESYNC) {
3902 rc = ll_lease_file_resync(och, inode, arg);
3904 ll_lease_close(och, inode, NULL);
3907 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
3909 ll_lease_close(och, inode, NULL);
3915 mutex_lock(&lli->lli_och_mutex);
3916 if (fd->fd_lease_och == NULL) {
3917 fd->fd_lease_och = och;
3920 mutex_unlock(&lli->lli_och_mutex);
3922 /* impossible now that only excl is supported for now */
3923 ll_lease_close(och, inode, &lease_broken);
3929 static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
3931 struct ll_inode_info *lli = ll_i2info(inode);
3932 struct ll_sb_info *sbi = ll_i2sbi(inode);
3933 __u64 now = ktime_get_real_seconds();
3936 spin_lock(&lli->lli_heat_lock);
3937 heat->lh_flags = lli->lli_heat_flags;
3938 for (i = 0; i < heat->lh_count; i++)
3939 heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
3940 now, sbi->ll_heat_decay_weight,
3941 sbi->ll_heat_period_second);
3942 spin_unlock(&lli->lli_heat_lock);
3945 static int ll_heat_set(struct inode *inode, enum lu_heat_flag flags)
3947 struct ll_inode_info *lli = ll_i2info(inode);
3950 spin_lock(&lli->lli_heat_lock);
3951 if (flags & LU_HEAT_FLAG_CLEAR)
3952 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
3954 if (flags & LU_HEAT_FLAG_OFF)
3955 lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
3957 lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
3959 spin_unlock(&lli->lli_heat_lock);
3965 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3967 struct inode *inode = file_inode(file);
3968 struct ll_file_data *fd = file->private_data;
3972 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
3973 PFID(ll_inode2fid(inode)), inode, cmd);
3974 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
3976 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
3977 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
3981 case LL_IOC_GETFLAGS:
3982 /* Get the current value of the file flags */
3983 return put_user(fd->fd_flags, (int __user *)arg);
3984 case LL_IOC_SETFLAGS:
3985 case LL_IOC_CLRFLAGS:
3986 /* Set or clear specific file flags */
3987 /* XXX This probably needs checks to ensure the flags are
3988 * not abused, and to handle any flag side effects.
3990 if (get_user(flags, (int __user *) arg))
3993 if (cmd == LL_IOC_SETFLAGS) {
3994 if ((flags & LL_FILE_IGNORE_LOCK) &&
3995 !(file->f_flags & O_DIRECT)) {
3996 CERROR("%s: unable to disable locking on "
3997 "non-O_DIRECT file\n", current->comm);
4001 fd->fd_flags |= flags;
4003 fd->fd_flags &= ~flags;
4006 case LL_IOC_LOV_SETSTRIPE:
4007 case LL_IOC_LOV_SETSTRIPE_NEW:
4008 RETURN(ll_lov_setstripe(inode, file, (void __user *)arg));
4009 case LL_IOC_LOV_SETEA:
4010 RETURN(ll_lov_setea(inode, file, (void __user *)arg));
4011 case LL_IOC_LOV_SWAP_LAYOUTS: {
4013 struct lustre_swap_layouts lsl;
4015 if (copy_from_user(&lsl, (char __user *)arg,
4016 sizeof(struct lustre_swap_layouts)))
4019 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
4022 file2 = fget(lsl.sl_fd);
4026 /* O_WRONLY or O_RDWR */
4027 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
4028 GOTO(out, rc = -EPERM);
4030 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
4031 struct inode *inode2;
4032 struct ll_inode_info *lli;
4033 struct obd_client_handle *och = NULL;
4035 lli = ll_i2info(inode);
4036 mutex_lock(&lli->lli_och_mutex);
4037 if (fd->fd_lease_och != NULL) {
4038 och = fd->fd_lease_och;
4039 fd->fd_lease_och = NULL;
4041 mutex_unlock(&lli->lli_och_mutex);
4043 GOTO(out, rc = -ENOLCK);
4044 inode2 = file_inode(file2);
4045 rc = ll_swap_layouts_close(och, inode, inode2);
4047 rc = ll_swap_layouts(file, file2, &lsl);
4053 case LL_IOC_LOV_GETSTRIPE:
4054 case LL_IOC_LOV_GETSTRIPE_NEW:
4055 RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
4056 case FS_IOC_GETFLAGS:
4057 case FS_IOC_SETFLAGS:
4058 RETURN(ll_iocontrol(inode, file, cmd, arg));
4059 case FSFILT_IOC_GETVERSION:
4060 case FS_IOC_GETVERSION:
4061 RETURN(put_user(inode->i_generation, (int __user *)arg));
4062 /* We need to special case any other ioctls we want to handle,
4063 * to send them to the MDS/OST as appropriate and to properly
4064 * network encode the arg field. */
4065 case FS_IOC_SETVERSION:
4068 case LL_IOC_GROUP_LOCK:
4069 RETURN(ll_get_grouplock(inode, file, arg));
4070 case LL_IOC_GROUP_UNLOCK:
4071 RETURN(ll_put_grouplock(inode, file, arg));
4072 case IOC_OBD_STATFS:
4073 RETURN(ll_obd_statfs(inode, (void __user *)arg));
4075 case LL_IOC_FLUSHCTX:
4076 RETURN(ll_flush_ctx(inode));
4077 case LL_IOC_PATH2FID: {
4078 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
4079 sizeof(struct lu_fid)))
4084 case LL_IOC_GETPARENT:
4085 RETURN(ll_getparent(file, (struct getparent __user *)arg));
4087 case OBD_IOC_FID2PATH:
4088 RETURN(ll_fid2path(inode, (void __user *)arg));
4089 case LL_IOC_DATA_VERSION: {
4090 struct ioc_data_version idv;
4093 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
4096 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
4097 rc = ll_ioc_data_version(inode, &idv);
4100 copy_to_user((char __user *)arg, &idv, sizeof(idv)))
4106 case LL_IOC_GET_MDTIDX: {
4109 mdtidx = ll_get_mdt_idx(inode);
4113 if (put_user((int)mdtidx, (int __user *)arg))
4118 case OBD_IOC_GETNAME_OLD:
4119 case OBD_IOC_GETDTNAME:
4120 case OBD_IOC_GETMDNAME:
4121 RETURN(ll_get_obd_name(inode, cmd, arg));
4122 case LL_IOC_HSM_STATE_GET: {
4123 struct md_op_data *op_data;
4124 struct hsm_user_state *hus;
4131 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4132 LUSTRE_OPC_ANY, hus);
4133 if (IS_ERR(op_data)) {
4135 RETURN(PTR_ERR(op_data));
4138 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4141 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
4144 ll_finish_md_op_data(op_data);
4148 case LL_IOC_HSM_STATE_SET: {
4149 struct hsm_state_set *hss;
4156 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
4161 rc = ll_hsm_state_set(inode, hss);
4166 case LL_IOC_HSM_ACTION: {
4167 struct md_op_data *op_data;
4168 struct hsm_current_action *hca;
4176 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4177 LUSTRE_OPC_ANY, hca);
4178 if (IS_ERR(op_data)) {
4180 RETURN(PTR_ERR(op_data));
4183 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4186 GOTO(skip_copy, rc);
4188 /* The hsm_current_action retreived from the server could
4189 * contain corrupt information. If it is incorrect data collect
4190 * debug information. We still send the data even if incorrect
4191 * to user land to handle.
4193 action = hsm_user_action2name(hca->hca_action);
4194 if (strcmp(action, "UNKNOWN") == 0 ||
4195 hca->hca_state > HPS_DONE) {
4197 "HSM current state %s action %s, offset = %llu, length %llu\n",
4198 hsm_progress_state2name(hca->hca_state), action,
4199 hca->hca_location.offset, hca->hca_location.length);
4202 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
4205 ll_finish_md_op_data(op_data);
4209 case LL_IOC_SET_LEASE_OLD: {
4210 struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
4212 RETURN(ll_file_set_lease(file, &ioc, 0));
4214 case LL_IOC_SET_LEASE: {
4215 struct ll_ioc_lease ioc;
4217 if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
4220 RETURN(ll_file_set_lease(file, &ioc, arg));
4222 case LL_IOC_GET_LEASE: {
4223 struct ll_inode_info *lli = ll_i2info(inode);
4224 struct ldlm_lock *lock = NULL;
4227 mutex_lock(&lli->lli_och_mutex);
4228 if (fd->fd_lease_och != NULL) {
4229 struct obd_client_handle *och = fd->fd_lease_och;
4231 lock = ldlm_handle2lock(&och->och_lease_handle);
4233 lock_res_and_lock(lock);
4234 if (!ldlm_is_cancel(lock))
4235 fmode = och->och_flags;
4237 unlock_res_and_lock(lock);
4238 LDLM_LOCK_PUT(lock);
4241 mutex_unlock(&lli->lli_och_mutex);
4243 RETURN(ll_lease_type_from_fmode(fmode));
4245 case LL_IOC_HSM_IMPORT: {
4246 struct hsm_user_import *hui;
4252 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
4257 rc = ll_hsm_import(inode, file, hui);
4262 case LL_IOC_FUTIMES_3: {
4263 struct ll_futimes_3 lfu;
4265 if (copy_from_user(&lfu,
4266 (const struct ll_futimes_3 __user *)arg,
4270 RETURN(ll_file_futimes_3(file, &lfu));
4272 case LL_IOC_LADVISE: {
4273 struct llapi_ladvise_hdr *k_ladvise_hdr;
4274 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
4277 int alloc_size = sizeof(*k_ladvise_hdr);
4280 u_ladvise_hdr = (void __user *)arg;
4281 OBD_ALLOC_PTR(k_ladvise_hdr);
4282 if (k_ladvise_hdr == NULL)
4285 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4286 GOTO(out_ladvise, rc = -EFAULT);
4288 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
4289 k_ladvise_hdr->lah_count < 1)
4290 GOTO(out_ladvise, rc = -EINVAL);
4292 num_advise = k_ladvise_hdr->lah_count;
4293 if (num_advise >= LAH_COUNT_MAX)
4294 GOTO(out_ladvise, rc = -EFBIG);
4296 OBD_FREE_PTR(k_ladvise_hdr);
4297 alloc_size = offsetof(typeof(*k_ladvise_hdr),
4298 lah_advise[num_advise]);
4299 OBD_ALLOC(k_ladvise_hdr, alloc_size);
4300 if (k_ladvise_hdr == NULL)
4304 * TODO: submit multiple advices to one server in a single RPC
4306 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4307 GOTO(out_ladvise, rc = -EFAULT);
4309 for (i = 0; i < num_advise; i++) {
4310 struct llapi_lu_ladvise *k_ladvise =
4311 &k_ladvise_hdr->lah_advise[i];
4312 struct llapi_lu_ladvise __user *u_ladvise =
4313 &u_ladvise_hdr->lah_advise[i];
4315 rc = ll_ladvise_sanity(inode, k_ladvise);
4317 GOTO(out_ladvise, rc);
4319 switch (k_ladvise->lla_advice) {
4320 case LU_LADVISE_LOCKNOEXPAND:
4321 rc = ll_lock_noexpand(file,
4322 k_ladvise->lla_peradvice_flags);
4323 GOTO(out_ladvise, rc);
4324 case LU_LADVISE_LOCKAHEAD:
4326 rc = ll_file_lock_ahead(file, k_ladvise);
4329 GOTO(out_ladvise, rc);
4332 &u_ladvise->lla_lockahead_result))
4333 GOTO(out_ladvise, rc = -EFAULT);
4336 rc = ll_ladvise(inode, file,
4337 k_ladvise_hdr->lah_flags,
4340 GOTO(out_ladvise, rc);
4347 OBD_FREE(k_ladvise_hdr, alloc_size);
4350 case LL_IOC_FLR_SET_MIRROR: {
4351 /* mirror I/O must be direct to avoid polluting page cache
4353 if (!(file->f_flags & O_DIRECT))
4356 fd->fd_designated_mirror = (__u32)arg;
4359 case FS_IOC_FSGETXATTR:
4360 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
4361 case FS_IOC_FSSETXATTR:
4362 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
4363 case LL_IOC_PROJECT:
4364 RETURN(ll_ioctl_project(file, cmd, arg));
4366 RETURN(put_user(PAGE_SIZE, (int __user *)arg));
4367 case LL_IOC_HEAT_GET: {
4368 struct lu_heat uheat;
4369 struct lu_heat *heat;
4372 if (copy_from_user(&uheat, (void __user *)arg, sizeof(uheat)))
4375 if (uheat.lh_count > OBD_HEAT_COUNT)
4376 uheat.lh_count = OBD_HEAT_COUNT;
4378 size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
4379 OBD_ALLOC(heat, size);
4383 heat->lh_count = uheat.lh_count;
4384 ll_heat_get(inode, heat);
4385 rc = copy_to_user((char __user *)arg, heat, size);
4386 OBD_FREE(heat, size);
4387 RETURN(rc ? -EFAULT : 0);
4389 case LL_IOC_HEAT_SET: {
4392 if (copy_from_user(&flags, (void __user *)arg, sizeof(flags)))
4395 rc = ll_heat_set(inode, flags);
4398 case LL_IOC_PCC_DETACH: {
4399 struct lu_pcc_detach *detach;
4401 OBD_ALLOC_PTR(detach);
4405 if (copy_from_user(detach,
4406 (const struct lu_pcc_detach __user *)arg,
4408 GOTO(out_detach_free, rc = -EFAULT);
4410 if (!S_ISREG(inode->i_mode))
4411 GOTO(out_detach_free, rc = -EINVAL);
4413 if (!inode_owner_or_capable(&init_user_ns, inode))
4414 GOTO(out_detach_free, rc = -EPERM);
4416 rc = pcc_ioctl_detach(inode, detach->pccd_opt);
4418 OBD_FREE_PTR(detach);
4421 case LL_IOC_PCC_STATE: {
4422 struct lu_pcc_state __user *ustate =
4423 (struct lu_pcc_state __user *)arg;
4424 struct lu_pcc_state *state;
4426 OBD_ALLOC_PTR(state);
4430 if (copy_from_user(state, ustate, sizeof(*state)))
4431 GOTO(out_state, rc = -EFAULT);
4433 rc = pcc_ioctl_state(file, inode, state);
4435 GOTO(out_state, rc);
4437 if (copy_to_user(ustate, state, sizeof(*state)))
4438 GOTO(out_state, rc = -EFAULT);
4441 OBD_FREE_PTR(state);
4444 #ifdef HAVE_LUSTRE_CRYPTO
4445 case LL_IOC_SET_ENCRYPTION_POLICY:
4446 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4448 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
4449 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
4450 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4452 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
4453 case LL_IOC_ADD_ENCRYPTION_KEY:
4454 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4456 return llcrypt_ioctl_add_key(file, (void __user *)arg);
4457 case LL_IOC_REMOVE_ENCRYPTION_KEY:
4458 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4460 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
4461 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4462 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4464 return llcrypt_ioctl_remove_key_all_users(file,
4465 (void __user *)arg);
4466 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
4467 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4469 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
4472 case LL_IOC_UNLOCK_FOREIGN: {
4473 struct dentry *dentry = file_dentry(file);
4475 /* if not a foreign symlink do nothing */
4476 if (ll_foreign_is_removable(dentry, true)) {
4478 "prevent unlink of non-foreign file ("DFID")\n",
4479 PFID(ll_inode2fid(inode)));
4480 RETURN(-EOPNOTSUPP);
4486 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
4487 (void __user *)arg));
4491 loff_t ll_lseek(struct file *file, loff_t offset, int whence)
4493 struct inode *inode = file_inode(file);
4496 struct cl_lseek_io *lsio;
4503 env = cl_env_get(&refcheck);
4505 RETURN(PTR_ERR(env));
4507 io = vvp_env_thread_io(env);
4508 io->ci_obj = ll_i2info(inode)->lli_clob;
4509 ll_io_set_mirror(io, file);
4511 lsio = &io->u.ci_lseek;
4512 lsio->ls_start = offset;
4513 lsio->ls_whence = whence;
4514 lsio->ls_result = -ENXIO;
4517 rc = cl_io_init(env, io, CIT_LSEEK, io->ci_obj);
4519 struct vvp_io *vio = vvp_env_io(env);
4521 vio->vui_fd = file->private_data;
4522 rc = cl_io_loop(env, io);
4526 retval = rc ? : lsio->ls_result;
4527 cl_io_fini(env, io);
4528 } while (unlikely(io->ci_need_restart));
4530 cl_env_put(env, &refcheck);
4532 /* Without the key, SEEK_HOLE return value has to be
4533 * rounded up to next LUSTRE_ENCRYPTION_UNIT_SIZE.
4535 if (llcrypt_require_key(inode) == -ENOKEY && whence == SEEK_HOLE)
4536 retval = round_up(retval, LUSTRE_ENCRYPTION_UNIT_SIZE);
4541 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
4543 struct inode *inode = file_inode(file);
4544 loff_t retval = offset, eof = 0;
4545 ktime_t kstart = ktime_get();
4549 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
4550 PFID(ll_inode2fid(inode)), inode, retval, retval,
4553 if (origin == SEEK_END) {
4554 retval = ll_glimpse_size(inode);
4557 eof = i_size_read(inode);
4560 if (origin == SEEK_HOLE || origin == SEEK_DATA) {
4564 /* flush local cache first if any */
4565 cl_sync_file_range(inode, offset, OBD_OBJECT_EOF,
4568 retval = ll_lseek(file, offset, origin);
4571 retval = vfs_setpos(file, retval, ll_file_maxbytes(inode));
4573 retval = generic_file_llseek_size(file, offset, origin,
4574 ll_file_maxbytes(inode), eof);
4577 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK,
4578 ktime_us_delta(ktime_get(), kstart));
4582 static int ll_flush(struct file *file, fl_owner_t id)
4584 struct inode *inode = file_inode(file);
4585 struct ll_inode_info *lli = ll_i2info(inode);
4586 struct ll_file_data *fd = file->private_data;
4589 LASSERT(!S_ISDIR(inode->i_mode));
4591 /* catch async errors that were recorded back when async writeback
4592 * failed for pages in this mapping. */
4593 rc = lli->lli_async_rc;
4594 lli->lli_async_rc = 0;
4595 if (lli->lli_clob != NULL) {
4596 err = lov_read_and_clear_async_rc(lli->lli_clob);
4601 /* The application has been told write failure already.
4602 * Do not report failure again. */
4603 if (fd->fd_write_failed)
4605 return rc ? -EIO : 0;
4609 * Called to make sure a portion of file has been written out.
4610 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
4612 * Return how many pages have been written.
4614 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
4615 enum cl_fsync_mode mode, int ignore_layout)
4619 struct cl_fsync_io *fio;
4624 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
4625 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
4628 env = cl_env_get(&refcheck);
4630 RETURN(PTR_ERR(env));
4632 io = vvp_env_thread_io(env);
4633 io->ci_obj = ll_i2info(inode)->lli_clob;
4634 io->ci_ignore_layout = ignore_layout;
4636 /* initialize parameters for sync */
4637 fio = &io->u.ci_fsync;
4638 fio->fi_start = start;
4640 fio->fi_fid = ll_inode2fid(inode);
4641 fio->fi_mode = mode;
4642 fio->fi_nr_written = 0;
4644 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
4645 result = cl_io_loop(env, io);
4647 result = io->ci_result;
4649 result = fio->fi_nr_written;
4650 cl_io_fini(env, io);
4651 cl_env_put(env, &refcheck);
4657 * When dentry is provided (the 'else' case), file_dentry() may be
4658 * null and dentry must be used directly rather than pulled from
4659 * file_dentry() as is done otherwise.
4662 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
4664 struct dentry *dentry = file_dentry(file);
4665 struct inode *inode = dentry->d_inode;
4666 struct ll_inode_info *lli = ll_i2info(inode);
4667 struct ptlrpc_request *req;
4668 ktime_t kstart = ktime_get();
4674 "VFS Op:inode="DFID"(%p), start %lld, end %lld, datasync %d\n",
4675 PFID(ll_inode2fid(inode)), inode, start, end, datasync);
4677 /* fsync's caller has already called _fdata{sync,write}, we want
4678 * that IO to finish before calling the osc and mdc sync methods */
4679 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
4681 /* catch async errors that were recorded back when async writeback
4682 * failed for pages in this mapping. */
4683 if (!S_ISDIR(inode->i_mode)) {
4684 err = lli->lli_async_rc;
4685 lli->lli_async_rc = 0;
4688 if (lli->lli_clob != NULL) {
4689 err = lov_read_and_clear_async_rc(lli->lli_clob);
4695 err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
4699 ptlrpc_req_finished(req);
4701 if (S_ISREG(inode->i_mode)) {
4702 struct ll_file_data *fd = file->private_data;
4705 /* Sync metadata on MDT first, and then sync the cached data
4708 err = pcc_fsync(file, start, end, datasync, &cached);
4710 err = cl_sync_file_range(inode, start, end,
4712 if (rc == 0 && err < 0)
4715 fd->fd_write_failed = true;
4717 fd->fd_write_failed = false;
4721 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC,
4722 ktime_us_delta(ktime_get(), kstart));
4727 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
4729 struct inode *inode = file_inode(file);
4730 struct ll_sb_info *sbi = ll_i2sbi(inode);
4731 struct ldlm_enqueue_info einfo = {
4732 .ei_type = LDLM_FLOCK,
4733 .ei_cb_cp = ldlm_flock_completion_ast,
4734 .ei_cbdata = file_lock,
4736 struct md_op_data *op_data;
4737 struct lustre_handle lockh = { 0 };
4738 union ldlm_policy_data flock = { { 0 } };
4739 int fl_type = file_lock->fl_type;
4740 ktime_t kstart = ktime_get();
4746 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
4747 PFID(ll_inode2fid(inode)), file_lock);
4749 if (file_lock->fl_flags & FL_FLOCK) {
4750 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
4751 /* flocks are whole-file locks */
4752 flock.l_flock.end = OFFSET_MAX;
4753 /* For flocks owner is determined by the local file desctiptor*/
4754 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
4755 } else if (file_lock->fl_flags & FL_POSIX) {
4756 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
4757 flock.l_flock.start = file_lock->fl_start;
4758 flock.l_flock.end = file_lock->fl_end;
4762 flock.l_flock.pid = file_lock->fl_pid;
4764 #if defined(HAVE_LM_COMPARE_OWNER) || defined(lm_compare_owner)
4765 /* Somewhat ugly workaround for svc lockd.
4766 * lockd installs custom fl_lmops->lm_compare_owner that checks
4767 * for the fl_owner to be the same (which it always is on local node
4768 * I guess between lockd processes) and then compares pid.
4769 * As such we assign pid to the owner field to make it all work,
4770 * conflict with normal locks is unlikely since pid space and
4771 * pointer space for current->files are not intersecting */
4772 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
4773 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
4778 einfo.ei_mode = LCK_PR;
4781 /* An unlock request may or may not have any relation to
4782 * existing locks so we may not be able to pass a lock handle
4783 * via a normal ldlm_lock_cancel() request. The request may even
4784 * unlock a byte range in the middle of an existing lock. In
4785 * order to process an unlock request we need all of the same
4786 * information that is given with a normal read or write record
4787 * lock request. To avoid creating another ldlm unlock (cancel)
4788 * message we'll treat a LCK_NL flock request as an unlock. */
4789 einfo.ei_mode = LCK_NL;
4792 einfo.ei_mode = LCK_PW;
4795 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
4810 flags = LDLM_FL_BLOCK_NOWAIT;
4816 flags = LDLM_FL_TEST_LOCK;
4819 CERROR("unknown fcntl lock command: %d\n", cmd);
4823 /* Save the old mode so that if the mode in the lock changes we
4824 * can decrement the appropriate reader or writer refcount. */
4825 file_lock->fl_type = einfo.ei_mode;
4827 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4828 LUSTRE_OPC_ANY, NULL);
4829 if (IS_ERR(op_data))
4830 RETURN(PTR_ERR(op_data));
4832 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
4833 "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
4834 flock.l_flock.pid, flags, einfo.ei_mode,
4835 flock.l_flock.start, flock.l_flock.end);
4837 rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
4840 /* Restore the file lock type if not TEST lock. */
4841 if (!(flags & LDLM_FL_TEST_LOCK))
4842 file_lock->fl_type = fl_type;
4844 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
4845 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
4846 !(flags & LDLM_FL_TEST_LOCK))
4847 rc2 = locks_lock_file_wait(file, file_lock);
4849 if ((file_lock->fl_flags & FL_FLOCK) &&
4850 (rc == 0 || file_lock->fl_type == F_UNLCK))
4851 rc2 = flock_lock_file_wait(file, file_lock);
4852 if ((file_lock->fl_flags & FL_POSIX) &&
4853 (rc == 0 || file_lock->fl_type == F_UNLCK) &&
4854 !(flags & LDLM_FL_TEST_LOCK))
4855 rc2 = posix_lock_file_wait(file, file_lock);
4856 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
4858 if (rc2 && file_lock->fl_type != F_UNLCK) {
4859 einfo.ei_mode = LCK_NL;
4860 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
4865 ll_finish_md_op_data(op_data);
4868 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK,
4869 ktime_us_delta(ktime_get(), kstart));
4873 int ll_get_fid_by_name(struct inode *parent, const char *name,
4874 int namelen, struct lu_fid *fid,
4875 struct inode **inode)
4877 struct md_op_data *op_data = NULL;
4878 struct mdt_body *body;
4879 struct ptlrpc_request *req;
4883 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
4884 LUSTRE_OPC_ANY, NULL);
4885 if (IS_ERR(op_data))
4886 RETURN(PTR_ERR(op_data));
4888 op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
4889 rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
4890 ll_finish_md_op_data(op_data);
4894 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4896 GOTO(out_req, rc = -EFAULT);
4898 *fid = body->mbo_fid1;
4901 rc = ll_prep_inode(inode, &req->rq_pill, parent->i_sb, NULL);
4903 ptlrpc_req_finished(req);
4907 int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
4908 const char *name, __u32 flags)
4910 struct dentry *dchild = NULL;
4911 struct inode *child_inode = NULL;
4912 struct md_op_data *op_data;
4913 struct ptlrpc_request *request = NULL;
4914 struct obd_client_handle *och = NULL;
4916 struct mdt_body *body;
4917 __u64 data_version = 0;
4918 size_t namelen = strlen(name);
4919 int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
4923 CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
4924 PFID(ll_inode2fid(parent)), name,
4925 lum->lum_stripe_offset, lum->lum_stripe_count);
4927 if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
4928 lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
4929 lustre_swab_lmv_user_md(lum);
4931 /* Get child FID first */
4932 qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
4935 dchild = d_lookup(file_dentry(file), &qstr);
4937 if (dchild->d_inode)
4938 child_inode = igrab(dchild->d_inode);
4943 rc = ll_get_fid_by_name(parent, name, namelen, NULL,
4952 if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
4953 OBD_CONNECT2_DIR_MIGRATE)) {
4954 if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
4955 ll_dir_striped(child_inode)) {
4956 CERROR("%s: MDT doesn't support stripe directory "
4957 "migration!\n", ll_i2sbi(parent)->ll_fsname);
4958 GOTO(out_iput, rc = -EOPNOTSUPP);
4963 * lfs migrate command needs to be blocked on the client
4964 * by checking the migrate FID against the FID of the
4967 if (is_root_inode(child_inode))
4968 GOTO(out_iput, rc = -EINVAL);
4970 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
4971 child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
4972 if (IS_ERR(op_data))
4973 GOTO(out_iput, rc = PTR_ERR(op_data));
4975 inode_lock(child_inode);
4976 op_data->op_fid3 = *ll_inode2fid(child_inode);
4977 if (!fid_is_sane(&op_data->op_fid3)) {
4978 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
4979 ll_i2sbi(parent)->ll_fsname, name,
4980 PFID(&op_data->op_fid3));
4981 GOTO(out_unlock, rc = -EINVAL);
4984 op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
4985 op_data->op_data = lum;
4986 op_data->op_data_size = lumlen;
4988 /* migrate dirent only for subdirs if MDS_MIGRATE_NSONLY set */
4989 if (S_ISDIR(child_inode->i_mode) && (flags & MDS_MIGRATE_NSONLY) &&
4990 lmv_dir_layout_changing(ll_i2info(parent)->lli_lsm_md))
4991 op_data->op_bias |= MDS_MIGRATE_NSONLY;
4994 if (S_ISREG(child_inode->i_mode)) {
4995 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
4999 GOTO(out_unlock, rc);
5002 rc = ll_data_version(child_inode, &data_version,
5005 GOTO(out_close, rc);
5007 op_data->op_open_handle = och->och_open_handle;
5008 op_data->op_data_version = data_version;
5009 op_data->op_lease_handle = och->och_lease_handle;
5010 op_data->op_bias |= MDS_CLOSE_MIGRATE;
5012 spin_lock(&och->och_mod->mod_open_req->rq_lock);
5013 och->och_mod->mod_open_req->rq_replay = 0;
5014 spin_unlock(&och->och_mod->mod_open_req->rq_lock);
5017 rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data,
5018 op_data->op_name, op_data->op_namelen,
5019 op_data->op_name, op_data->op_namelen, &request);
5021 LASSERT(request != NULL);
5022 ll_update_times(request, parent);
5025 if (rc == 0 || rc == -EAGAIN) {
5026 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
5027 LASSERT(body != NULL);
5029 /* If the server does release layout lock, then we cleanup
5030 * the client och here, otherwise release it in out_close: */
5031 if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
5032 obd_mod_put(och->och_mod);
5033 md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
5035 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
5041 if (request != NULL) {
5042 ptlrpc_req_finished(request);
5046 /* Try again if the lease has cancelled. */
5047 if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
5052 ll_lease_close(och, child_inode, NULL);
5054 clear_nlink(child_inode);
5056 inode_unlock(child_inode);
5057 ll_finish_md_op_data(op_data);
5064 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
5066 struct ll_file_data *fd = file->private_data;
5070 * In order to avoid flood of warning messages, only print one message
5071 * for one file. And the entire message rate on the client is limited
5072 * by CDEBUG_LIMIT too.
5074 if (!(fd->fd_flags & LL_FILE_FLOCK_WARNING)) {
5075 fd->fd_flags |= LL_FILE_FLOCK_WARNING;
5076 CDEBUG_LIMIT(D_CONSOLE,
5077 "flock disabled, mount with '-o [local]flock' to enable\r\n");
5083 * test if some locks matching bits and l_req_mode are acquired
5084 * - bits can be in different locks
5085 * - if found clear the common lock bits in *bits
5086 * - the bits not found, are kept in *bits
5088 * \param bits [IN] searched lock bits [IN]
5089 * \param l_req_mode [IN] searched lock mode
5090 * \retval boolean, true iff all bits are found
5092 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
5094 struct lustre_handle lockh;
5095 union ldlm_policy_data policy;
5096 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
5097 (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
5106 fid = &ll_i2info(inode)->lli_fid;
5107 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
5108 ldlm_lockname[mode]);
5110 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
5111 for (i = 0; i < MDS_INODELOCK_NUMBITS && *bits != 0; i++) {
5112 policy.l_inodebits.bits = *bits & BIT(i);
5113 if (policy.l_inodebits.bits == 0)
5116 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
5117 &policy, mode, &lockh)) {
5118 struct ldlm_lock *lock;
5120 lock = ldlm_handle2lock(&lockh);
5123 ~(lock->l_policy_data.l_inodebits.bits);
5124 LDLM_LOCK_PUT(lock);
5126 *bits &= ~policy.l_inodebits.bits;
5133 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
5134 struct lustre_handle *lockh, __u64 flags,
5135 enum ldlm_mode mode)
5137 union ldlm_policy_data policy = { .l_inodebits = { bits } };
5142 fid = &ll_i2info(inode)->lli_fid;
5143 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
5145 rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
5146 fid, LDLM_IBITS, &policy, mode, lockh);
5151 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
5153 /* Already unlinked. Just update nlink and return success */
5154 if (rc == -ENOENT) {
5156 /* If it is striped directory, and there is bad stripe
5157 * Let's revalidate the dentry again, instead of returning
5159 if (ll_dir_striped(inode))
5162 /* This path cannot be hit for regular files unless in
5163 * case of obscure races, so no need to to validate
5165 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
5167 } else if (rc != 0) {
5168 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
5169 "%s: revalidate FID "DFID" error: rc = %d\n",
5170 ll_i2sbi(inode)->ll_fsname,
5171 PFID(ll_inode2fid(inode)), rc);
5177 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
5179 struct inode *parent;
5180 struct inode *inode = dentry->d_inode;
5181 struct obd_export *exp = ll_i2mdexp(inode);
5182 struct lookup_intent oit = {
5185 struct ptlrpc_request *req = NULL;
5186 struct md_op_data *op_data;
5187 const char *name = NULL;
5192 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
5193 PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
5195 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID) {
5196 parent = dentry->d_parent->d_inode;
5197 name = dentry->d_name.name;
5198 namelen = dentry->d_name.len;
5203 op_data = ll_prep_md_op_data(NULL, parent, inode, name, namelen, 0,
5204 LUSTRE_OPC_ANY, NULL);
5205 if (IS_ERR(op_data))
5206 RETURN(PTR_ERR(op_data));
5208 /* Call getattr by fid */
5209 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID)
5210 op_data->op_flags = MF_GETATTR_BY_FID;
5211 rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
5212 ll_finish_md_op_data(op_data);
5214 rc = ll_inode_revalidate_fini(inode, rc);
5218 rc = ll_revalidate_it_finish(req, &oit, dentry);
5220 ll_intent_release(&oit);
5224 /* Unlinked? Unhash dentry, so it is not picked up later by
5225 * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
5226 * here to preserve get_cwd functionality on 2.6.
5228 if (!dentry->d_inode->i_nlink)
5229 d_lustre_invalidate(dentry);
5231 ll_lookup_finish_locks(&oit, dentry);
5233 ptlrpc_req_finished(req);
5238 static int ll_merge_md_attr(struct inode *inode)
5240 struct ll_inode_info *lli = ll_i2info(inode);
5241 struct cl_attr attr = { 0 };
5244 if (!lli->lli_lsm_md)
5247 down_read(&lli->lli_lsm_sem);
5248 if (!lmv_dir_striped(lli->lli_lsm_md)) {
5249 up_read(&lli->lli_lsm_sem);
5252 rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
5253 &attr, ll_md_blocking_ast);
5254 up_read(&lli->lli_lsm_sem);
5258 spin_lock(&inode->i_lock);
5259 set_nlink(inode, attr.cat_nlink);
5260 spin_unlock(&inode->i_lock);
5262 inode->i_blocks = attr.cat_blocks;
5263 i_size_write(inode, attr.cat_size);
5265 ll_i2info(inode)->lli_atime = attr.cat_atime;
5266 ll_i2info(inode)->lli_mtime = attr.cat_mtime;
5267 ll_i2info(inode)->lli_ctime = attr.cat_ctime;
5272 int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
5273 unsigned int flags, bool foreign)
5275 struct inode *inode = de->d_inode;
5276 struct ll_sb_info *sbi = ll_i2sbi(inode);
5277 struct ll_inode_info *lli = ll_i2info(inode);
5278 struct inode *dir = de->d_parent->d_inode;
5279 bool need_glimpse = true;
5280 ktime_t kstart = ktime_get();
5283 /* The OST object(s) determine the file size, blocks and mtime. */
5284 if (!(request_mask & STATX_SIZE || request_mask & STATX_BLOCKS ||
5285 request_mask & STATX_MTIME))
5286 need_glimpse = false;
5288 if (dentry_may_statahead(dir, de))
5289 ll_start_statahead(dir, de, need_glimpse &&
5290 !(flags & AT_STATX_DONT_SYNC));
5292 if (flags & AT_STATX_DONT_SYNC)
5293 GOTO(fill_attr, rc = 0);
5295 rc = ll_inode_revalidate(de, IT_GETATTR);
5299 /* foreign file/dir are always of zero length, so don't
5300 * need to validate size.
5302 if (S_ISREG(inode->i_mode) && !foreign) {
5306 GOTO(fill_attr, rc);
5308 rc = pcc_inode_getattr(inode, request_mask, flags, &cached);
5309 if (cached && rc < 0)
5313 GOTO(fill_attr, rc);
5316 * If the returned attr is masked with OBD_MD_FLSIZE &
5317 * OBD_MD_FLBLOCKS & OBD_MD_FLMTIME, it means that the file size
5318 * or blocks obtained from MDT is strictly correct, and the file
5319 * is usually not being modified by clients, and the [a|m|c]time
5320 * got from MDT is also strictly correct.
5321 * Under this circumstance, it does not need to send glimpse
5322 * RPCs to OSTs for file attributes such as the size and blocks.
5324 if (lli->lli_attr_valid & OBD_MD_FLSIZE &&
5325 lli->lli_attr_valid & OBD_MD_FLBLOCKS &&
5326 lli->lli_attr_valid & OBD_MD_FLMTIME) {
5327 inode->i_mtime.tv_sec = lli->lli_mtime;
5328 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5329 inode->i_atime.tv_sec = lli->lli_atime;
5330 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5331 inode->i_ctime.tv_sec = lli->lli_ctime;
5332 GOTO(fill_attr, rc);
5335 /* In case of restore, the MDT has the right size and has
5336 * already send it back without granting the layout lock,
5337 * inode is up-to-date so glimpse is useless.
5338 * Also to glimpse we need the layout, in case of a running
5339 * restore the MDT holds the layout lock so the glimpse will
5340 * block up to the end of restore (getattr will block)
5342 if (!test_bit(LLIF_FILE_RESTORING, &lli->lli_flags)) {
5343 rc = ll_glimpse_size(inode);
5348 /* If object isn't regular a file then don't validate size. */
5349 /* foreign dir is not striped dir */
5350 if (ll_dir_striped(inode) && !foreign) {
5351 rc = ll_merge_md_attr(inode);
5356 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5357 inode->i_atime.tv_sec = lli->lli_atime;
5358 if (lli->lli_attr_valid & OBD_MD_FLMTIME)
5359 inode->i_mtime.tv_sec = lli->lli_mtime;
5360 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5361 inode->i_ctime.tv_sec = lli->lli_ctime;
5365 OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
5367 if (ll_need_32bit_api(sbi)) {
5368 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
5369 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
5370 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
5372 stat->ino = inode->i_ino;
5373 stat->dev = inode->i_sb->s_dev;
5374 stat->rdev = inode->i_rdev;
5377 /* foreign symlink to be exposed as a real symlink */
5379 stat->mode = inode->i_mode;
5381 stat->mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
5383 stat->uid = inode->i_uid;
5384 stat->gid = inode->i_gid;
5385 stat->atime = inode->i_atime;
5386 stat->mtime = inode->i_mtime;
5387 stat->ctime = inode->i_ctime;
5388 /* stat->blksize is used to tell about preferred IO size */
5389 if (sbi->ll_stat_blksize)
5390 stat->blksize = sbi->ll_stat_blksize;
5391 else if (S_ISREG(inode->i_mode))
5392 stat->blksize = 1 << min(PTLRPC_MAX_BRW_BITS + 1,
5393 LL_MAX_BLKSIZE_BITS);
5395 stat->blksize = 1 << inode->i_sb->s_blocksize_bits;
5397 stat->nlink = inode->i_nlink;
5398 stat->size = i_size_read(inode);
5399 stat->blocks = inode->i_blocks;
5401 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_INODEOPS_ENHANCED_GETATTR)
5402 if (flags & AT_STATX_DONT_SYNC) {
5403 if (stat->size == 0 &&
5404 lli->lli_attr_valid & OBD_MD_FLLAZYSIZE)
5405 stat->size = lli->lli_lazysize;
5406 if (stat->blocks == 0 &&
5407 lli->lli_attr_valid & OBD_MD_FLLAZYBLOCKS)
5408 stat->blocks = lli->lli_lazyblocks;
5411 if (lli->lli_attr_valid & OBD_MD_FLBTIME) {
5412 stat->result_mask |= STATX_BTIME;
5413 stat->btime.tv_sec = lli->lli_btime;
5416 stat->attributes_mask = STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
5417 #ifdef HAVE_LUSTRE_CRYPTO
5418 stat->attributes_mask |= STATX_ATTR_ENCRYPTED;
5420 stat->attributes |= ll_inode_to_ext_flags(inode->i_flags);
5421 stat->result_mask &= request_mask;
5424 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR,
5425 ktime_us_delta(ktime_get(), kstart));
5430 #if defined(HAVE_USER_NAMESPACE_ARG) || defined(HAVE_INODEOPS_ENHANCED_GETATTR)
5431 int ll_getattr(struct user_namespace *mnt_userns, const struct path *path,
5432 struct kstat *stat, u32 request_mask, unsigned int flags)
5434 return ll_getattr_dentry(path->dentry, stat, request_mask, flags,
5438 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
5440 return ll_getattr_dentry(de, stat, STATX_BASIC_STATS,
5441 AT_STATX_SYNC_AS_STAT, false);
5445 int cl_falloc(struct file *file, struct inode *inode, int mode, loff_t offset,
5448 loff_t size = i_size_read(inode);
5456 env = cl_env_get(&refcheck);
5458 RETURN(PTR_ERR(env));
5460 io = vvp_env_thread_io(env);
5461 io->ci_obj = ll_i2info(inode)->lli_clob;
5462 ll_io_set_mirror(io, file);
5464 io->ci_verify_layout = 1;
5465 io->u.ci_setattr.sa_parent_fid = lu_object_fid(&io->ci_obj->co_lu);
5466 io->u.ci_setattr.sa_falloc_mode = mode;
5467 io->u.ci_setattr.sa_falloc_offset = offset;
5468 io->u.ci_setattr.sa_falloc_end = offset + len;
5469 io->u.ci_setattr.sa_subtype = CL_SETATTR_FALLOCATE;
5471 CDEBUG(D_INODE, "UID %u GID %u PRJID %u\n",
5472 from_kuid(&init_user_ns, inode->i_uid),
5473 from_kgid(&init_user_ns, inode->i_gid),
5474 ll_i2info(inode)->lli_projid);
5476 io->u.ci_setattr.sa_falloc_uid = from_kuid(&init_user_ns, inode->i_uid);
5477 io->u.ci_setattr.sa_falloc_gid = from_kgid(&init_user_ns, inode->i_gid);
5478 io->u.ci_setattr.sa_falloc_projid = ll_i2info(inode)->lli_projid;
5480 if (io->u.ci_setattr.sa_falloc_end > size) {
5481 loff_t newsize = io->u.ci_setattr.sa_falloc_end;
5483 /* Check new size against VFS/VM file size limit and rlimit */
5484 rc = inode_newsize_ok(inode, newsize);
5487 if (newsize > ll_file_maxbytes(inode)) {
5488 CDEBUG(D_INODE, "file size too large %llu > %llu\n",
5489 (unsigned long long)newsize,
5490 ll_file_maxbytes(inode));
5497 rc = cl_io_init(env, io, CIT_SETATTR, io->ci_obj);
5499 rc = cl_io_loop(env, io);
5502 cl_io_fini(env, io);
5503 } while (unlikely(io->ci_need_restart));
5506 cl_env_put(env, &refcheck);
5510 long ll_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
5512 struct inode *inode = file_inode(filp);
5515 if (offset < 0 || len <= 0)
5518 * Encrypted inodes can't handle collapse range or zero range or insert
5519 * range since we would need to re-encrypt blocks with a different IV or
5520 * XTS tweak (which are based on the logical block number).
5521 * Similar to what ext4 does.
5523 if (IS_ENCRYPTED(inode) &&
5524 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
5525 FALLOC_FL_ZERO_RANGE)))
5526 RETURN(-EOPNOTSUPP);
5529 * mode == 0 (which is standard prealloc) and PUNCH is supported
5530 * Rest of mode options are not supported yet.
5532 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
5533 RETURN(-EOPNOTSUPP);
5535 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FALLOCATE, 1);
5537 rc = cl_falloc(filp, inode, mode, offset, len);
5539 * ENOTSUPP (524) is an NFSv3 specific error code erroneously
5540 * used by Lustre in several places. Retuning it here would
5541 * confuse applications that explicity test for EOPNOTSUPP
5542 * (95) and fall back to ftruncate().
5544 if (rc == -ENOTSUPP)
5550 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5551 __u64 start, __u64 len)
5555 struct fiemap *fiemap;
5556 unsigned int extent_count = fieinfo->fi_extents_max;
5558 num_bytes = sizeof(*fiemap) + (extent_count *
5559 sizeof(struct fiemap_extent));
5560 OBD_ALLOC_LARGE(fiemap, num_bytes);
5565 fiemap->fm_flags = fieinfo->fi_flags;
5566 fiemap->fm_extent_count = fieinfo->fi_extents_max;
5567 fiemap->fm_start = start;
5568 fiemap->fm_length = len;
5569 if (extent_count > 0 &&
5570 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
5571 sizeof(struct fiemap_extent)) != 0)
5572 GOTO(out, rc = -EFAULT);
5574 rc = ll_do_fiemap(inode, fiemap, num_bytes);
5576 if (IS_ENCRYPTED(inode)) {
5579 for (i = 0; i < fiemap->fm_mapped_extents; i++)
5580 fiemap->fm_extents[i].fe_flags |=
5581 FIEMAP_EXTENT_DATA_ENCRYPTED |
5582 FIEMAP_EXTENT_ENCODED;
5585 fieinfo->fi_flags = fiemap->fm_flags;
5586 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
5587 if (extent_count > 0 &&
5588 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
5589 fiemap->fm_mapped_extents *
5590 sizeof(struct fiemap_extent)) != 0)
5591 GOTO(out, rc = -EFAULT);
5593 OBD_FREE_LARGE(fiemap, num_bytes);
5597 int ll_inode_permission(struct user_namespace *mnt_userns, struct inode *inode,
5601 struct ll_sb_info *sbi;
5602 struct root_squash_info *squash;
5603 struct cred *cred = NULL;
5604 const struct cred *old_cred = NULL;
5605 bool squash_id = false;
5606 ktime_t kstart = ktime_get();
5610 if (mask & MAY_NOT_BLOCK)
5614 * as root inode are NOT getting validated in lookup operation,
5615 * need to do it before permission check.
5618 if (is_root_inode(inode)) {
5619 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
5624 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
5625 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
5627 /* squash fsuid/fsgid if needed */
5628 sbi = ll_i2sbi(inode);
5629 squash = &sbi->ll_squash;
5630 if (unlikely(squash->rsi_uid != 0 &&
5631 uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
5632 !test_bit(LL_SBI_NOROOTSQUASH, sbi->ll_flags))) {
5636 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
5637 __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
5638 squash->rsi_uid, squash->rsi_gid);
5640 /* update current process's credentials
5641 * and FS capability */
5642 cred = prepare_creds();
5646 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
5647 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
5648 cred->cap_effective = cap_drop_nfsd_set(cred->cap_effective);
5649 cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
5651 old_cred = override_creds(cred);
5654 rc = generic_permission(mnt_userns, inode, mask);
5655 /* restore current process's credentials and FS capability */
5657 revert_creds(old_cred);
5662 ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM,
5663 ktime_us_delta(ktime_get(), kstart));
5668 /* -o localflock - only provides locally consistent flock locks */
5669 static const struct file_operations ll_file_operations = {
5670 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5671 # ifdef HAVE_SYNC_READ_WRITE
5672 .read = new_sync_read,
5673 .write = new_sync_write,
5675 .read_iter = ll_file_read_iter,
5676 .write_iter = ll_file_write_iter,
5677 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5678 .read = ll_file_read,
5679 .aio_read = ll_file_aio_read,
5680 .write = ll_file_write,
5681 .aio_write = ll_file_aio_write,
5682 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5683 .unlocked_ioctl = ll_file_ioctl,
5684 .open = ll_file_open,
5685 .release = ll_file_release,
5686 .mmap = ll_file_mmap,
5687 .llseek = ll_file_seek,
5688 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5689 .splice_read = generic_file_splice_read,
5691 .splice_read = pcc_file_splice_read,
5695 .fallocate = ll_fallocate,
5698 static const struct file_operations ll_file_operations_flock = {
5699 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5700 # ifdef HAVE_SYNC_READ_WRITE
5701 .read = new_sync_read,
5702 .write = new_sync_write,
5703 # endif /* HAVE_SYNC_READ_WRITE */
5704 .read_iter = ll_file_read_iter,
5705 .write_iter = ll_file_write_iter,
5706 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5707 .read = ll_file_read,
5708 .aio_read = ll_file_aio_read,
5709 .write = ll_file_write,
5710 .aio_write = ll_file_aio_write,
5711 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5712 .unlocked_ioctl = ll_file_ioctl,
5713 .open = ll_file_open,
5714 .release = ll_file_release,
5715 .mmap = ll_file_mmap,
5716 .llseek = ll_file_seek,
5717 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5718 .splice_read = generic_file_splice_read,
5720 .splice_read = pcc_file_splice_read,
5724 .flock = ll_file_flock,
5725 .lock = ll_file_flock,
5726 .fallocate = ll_fallocate,
5729 /* These are for -o noflock - to return ENOSYS on flock calls */
5730 static const struct file_operations ll_file_operations_noflock = {
5731 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5732 # ifdef HAVE_SYNC_READ_WRITE
5733 .read = new_sync_read,
5734 .write = new_sync_write,
5735 # endif /* HAVE_SYNC_READ_WRITE */
5736 .read_iter = ll_file_read_iter,
5737 .write_iter = ll_file_write_iter,
5738 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5739 .read = ll_file_read,
5740 .aio_read = ll_file_aio_read,
5741 .write = ll_file_write,
5742 .aio_write = ll_file_aio_write,
5743 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5744 .unlocked_ioctl = ll_file_ioctl,
5745 .open = ll_file_open,
5746 .release = ll_file_release,
5747 .mmap = ll_file_mmap,
5748 .llseek = ll_file_seek,
5749 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5750 .splice_read = generic_file_splice_read,
5752 .splice_read = pcc_file_splice_read,
5756 .flock = ll_file_noflock,
5757 .lock = ll_file_noflock,
5758 .fallocate = ll_fallocate,
5761 const struct inode_operations ll_file_inode_operations = {
5762 .setattr = ll_setattr,
5763 .getattr = ll_getattr,
5764 .permission = ll_inode_permission,
5765 #ifdef HAVE_IOP_XATTR
5766 .setxattr = ll_setxattr,
5767 .getxattr = ll_getxattr,
5768 .removexattr = ll_removexattr,
5770 .listxattr = ll_listxattr,
5771 .fiemap = ll_fiemap,
5772 .get_acl = ll_get_acl,
5773 #ifdef HAVE_IOP_SET_ACL
5774 .set_acl = ll_set_acl,
5778 const struct file_operations *ll_select_file_operations(struct ll_sb_info *sbi)
5780 const struct file_operations *fops = &ll_file_operations_noflock;
5782 if (test_bit(LL_SBI_FLOCK, sbi->ll_flags))
5783 fops = &ll_file_operations_flock;
5784 else if (test_bit(LL_SBI_LOCALFLOCK, sbi->ll_flags))
5785 fops = &ll_file_operations;
5790 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
5792 struct ll_inode_info *lli = ll_i2info(inode);
5793 struct cl_object *obj = lli->lli_clob;
5802 env = cl_env_get(&refcheck);
5804 RETURN(PTR_ERR(env));
5806 rc = cl_conf_set(env, lli->lli_clob, conf);
5810 if (conf->coc_opc == OBJECT_CONF_SET) {
5811 struct ldlm_lock *lock = conf->coc_lock;
5812 struct cl_layout cl = {
5816 LASSERT(lock != NULL);
5817 LASSERT(ldlm_has_layout(lock));
5819 /* it can only be allowed to match after layout is
5820 * applied to inode otherwise false layout would be
5821 * seen. Applying layout shoud happen before dropping
5822 * the intent lock. */
5823 ldlm_lock_allow_match(lock);
5825 rc = cl_object_layout_get(env, obj, &cl);
5830 DFID": layout version change: %u -> %u\n",
5831 PFID(&lli->lli_fid), ll_layout_version_get(lli),
5833 ll_layout_version_set(lli, cl.cl_layout_gen);
5837 cl_env_put(env, &refcheck);
5839 RETURN(rc < 0 ? rc : 0);
5842 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
5843 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
5846 struct ll_sb_info *sbi = ll_i2sbi(inode);
5847 struct ptlrpc_request *req;
5854 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
5855 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
5856 lock->l_lvb_data, lock->l_lvb_len);
5858 if (lock->l_lvb_data != NULL)
5861 /* if layout lock was granted right away, the layout is returned
5862 * within DLM_LVB of dlm reply; otherwise if the lock was ever
5863 * blocked and then granted via completion ast, we have to fetch
5864 * layout here. Please note that we can't use the LVB buffer in
5865 * completion AST because it doesn't have a large enough buffer */
5866 rc = ll_get_default_mdsize(sbi, &lmmsize);
5870 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), OBD_MD_FLXATTR,
5871 XATTR_NAME_LOV, lmmsize, &req);
5874 GOTO(out, rc = 0); /* empty layout */
5881 if (lmmsize == 0) /* empty layout */
5884 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
5886 GOTO(out, rc = -EFAULT);
5888 OBD_ALLOC_LARGE(lvbdata, lmmsize);
5889 if (lvbdata == NULL)
5890 GOTO(out, rc = -ENOMEM);
5892 memcpy(lvbdata, lmm, lmmsize);
5893 lock_res_and_lock(lock);
5894 if (unlikely(lock->l_lvb_data == NULL)) {
5895 lock->l_lvb_type = LVB_T_LAYOUT;
5896 lock->l_lvb_data = lvbdata;
5897 lock->l_lvb_len = lmmsize;
5900 unlock_res_and_lock(lock);
5903 OBD_FREE_LARGE(lvbdata, lmmsize);
5908 ptlrpc_req_finished(req);
5913 * Apply the layout to the inode. Layout lock is held and will be released
5916 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
5917 struct inode *inode)
5919 struct ll_inode_info *lli = ll_i2info(inode);
5920 struct ll_sb_info *sbi = ll_i2sbi(inode);
5921 struct ldlm_lock *lock;
5922 struct cl_object_conf conf;
5925 bool wait_layout = false;
5928 LASSERT(lustre_handle_is_used(lockh));
5930 lock = ldlm_handle2lock(lockh);
5931 LASSERT(lock != NULL);
5933 if (!ldlm_has_layout(lock))
5934 GOTO(out, rc = -EAGAIN);
5936 LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
5937 PFID(&lli->lli_fid), inode);
5939 /* in case this is a caching lock and reinstate with new inode */
5940 md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
5942 lock_res_and_lock(lock);
5943 lvb_ready = ldlm_is_lvb_ready(lock);
5944 unlock_res_and_lock(lock);
5946 /* checking lvb_ready is racy but this is okay. The worst case is
5947 * that multi processes may configure the file on the same time. */
5951 rc = ll_layout_fetch(inode, lock);
5955 /* for layout lock, lmm is stored in lock's lvb.
5956 * lvb_data is immutable if the lock is held so it's safe to access it
5959 * set layout to file. Unlikely this will fail as old layout was
5960 * surely eliminated */
5961 memset(&conf, 0, sizeof conf);
5962 conf.coc_opc = OBJECT_CONF_SET;
5963 conf.coc_inode = inode;
5964 conf.coc_lock = lock;
5965 conf.u.coc_layout.lb_buf = lock->l_lvb_data;
5966 conf.u.coc_layout.lb_len = lock->l_lvb_len;
5967 rc = ll_layout_conf(inode, &conf);
5969 /* refresh layout failed, need to wait */
5970 wait_layout = rc == -EBUSY;
5973 LDLM_LOCK_PUT(lock);
5974 ldlm_lock_decref(lockh, mode);
5976 /* wait for IO to complete if it's still being used. */
5978 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
5979 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5981 memset(&conf, 0, sizeof conf);
5982 conf.coc_opc = OBJECT_CONF_WAIT;
5983 conf.coc_inode = inode;
5984 rc = ll_layout_conf(inode, &conf);
5988 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
5989 sbi->ll_fsname, PFID(&lli->lli_fid), rc);
5995 * Issue layout intent RPC to MDS.
5996 * \param inode [in] file inode
5997 * \param intent [in] layout intent
5999 * \retval 0 on success
6000 * \retval < 0 error code
6002 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
6004 struct ll_inode_info *lli = ll_i2info(inode);
6005 struct ll_sb_info *sbi = ll_i2sbi(inode);
6006 struct md_op_data *op_data;
6007 struct lookup_intent it;
6008 struct ptlrpc_request *req;
6012 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
6013 0, 0, LUSTRE_OPC_ANY, NULL);
6014 if (IS_ERR(op_data))
6015 RETURN(PTR_ERR(op_data));
6017 op_data->op_data = intent;
6018 op_data->op_data_size = sizeof(*intent);
6020 memset(&it, 0, sizeof(it));
6021 it.it_op = IT_LAYOUT;
6022 if (intent->li_opc == LAYOUT_INTENT_WRITE ||
6023 intent->li_opc == LAYOUT_INTENT_TRUNC)
6024 it.it_flags = FMODE_WRITE;
6026 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
6027 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
6029 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
6030 &ll_md_blocking_ast, 0);
6031 if (it.it_request != NULL)
6032 ptlrpc_req_finished(it.it_request);
6033 it.it_request = NULL;
6035 ll_finish_md_op_data(op_data);
6037 /* set lock data in case this is a new lock */
6039 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
6041 ll_intent_drop_lock(&it);
6047 * This function checks if there exists a LAYOUT lock on the client side,
6048 * or enqueues it if it doesn't have one in cache.
6050 * This function will not hold layout lock so it may be revoked any time after
6051 * this function returns. Any operations depend on layout should be redone
6054 * This function should be called before lov_io_init() to get an uptodate
6055 * layout version, the caller should save the version number and after IO
6056 * is finished, this function should be called again to verify that layout
6057 * is not changed during IO time.
6059 int ll_layout_refresh(struct inode *inode, __u32 *gen)
6061 struct ll_inode_info *lli = ll_i2info(inode);
6062 struct ll_sb_info *sbi = ll_i2sbi(inode);
6063 struct lustre_handle lockh;
6064 struct layout_intent intent = {
6065 .li_opc = LAYOUT_INTENT_ACCESS,
6067 enum ldlm_mode mode;
6071 *gen = ll_layout_version_get(lli);
6072 if (!test_bit(LL_SBI_LAYOUT_LOCK, sbi->ll_flags) ||
6073 *gen != CL_LAYOUT_GEN_NONE)
6077 LASSERT(fid_is_sane(ll_inode2fid(inode)));
6078 LASSERT(S_ISREG(inode->i_mode));
6080 /* take layout lock mutex to enqueue layout lock exclusively. */
6081 mutex_lock(&lli->lli_layout_mutex);
6084 /* mostly layout lock is caching on the local side, so try to
6085 * match it before grabbing layout lock mutex. */
6086 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
6087 LCK_CR | LCK_CW | LCK_PR |
6089 if (mode != 0) { /* hit cached lock */
6090 rc = ll_layout_lock_set(&lockh, mode, inode);
6096 rc = ll_layout_intent(inode, &intent);
6102 *gen = ll_layout_version_get(lli);
6103 mutex_unlock(&lli->lli_layout_mutex);
6109 * Issue layout intent RPC indicating where in a file an IO is about to write.
6111 * \param[in] inode file inode.
6112 * \param[in] ext write range with start offset of fille in bytes where
6113 * an IO is about to write, and exclusive end offset in
6116 * \retval 0 on success
6117 * \retval < 0 error code
6119 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
6120 struct lu_extent *ext)
6122 struct layout_intent intent = {
6124 .li_extent.e_start = ext->e_start,
6125 .li_extent.e_end = ext->e_end,
6130 rc = ll_layout_intent(inode, &intent);
6136 * This function send a restore request to the MDT
6138 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
6140 struct hsm_user_request *hur;
6144 len = sizeof(struct hsm_user_request) +
6145 sizeof(struct hsm_user_item);
6146 OBD_ALLOC(hur, len);
6150 hur->hur_request.hr_action = HUA_RESTORE;
6151 hur->hur_request.hr_archive_id = 0;
6152 hur->hur_request.hr_flags = 0;
6153 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
6154 sizeof(hur->hur_user_item[0].hui_fid));
6155 hur->hur_user_item[0].hui_extent.offset = offset;
6156 hur->hur_user_item[0].hui_extent.length = length;
6157 hur->hur_request.hr_itemcount = 1;
6158 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,