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 <uapi/linux/llcrypt.h>
50 #include <lustre_swab.h>
52 #include "cl_object.h"
53 #include "llite_internal.h"
54 #include "vvp_internal.h"
57 struct inode *sp_inode;
62 __u64 pa_data_version;
68 ll_put_grouplock(struct inode *inode, struct file *file, unsigned long arg);
70 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
73 static struct ll_file_data *ll_file_data_get(void)
75 struct ll_file_data *fd;
77 OBD_SLAB_ALLOC_PTR_GFP(fd, ll_file_data_slab, GFP_NOFS);
81 fd->fd_write_failed = false;
82 pcc_file_init(&fd->fd_pcc_file);
87 static void ll_file_data_put(struct ll_file_data *fd)
90 OBD_SLAB_FREE_PTR(fd, ll_file_data_slab);
94 * Packs all the attributes into @op_data for the CLOSE rpc.
96 static void ll_prepare_close(struct inode *inode, struct md_op_data *op_data,
97 struct obd_client_handle *och)
101 ll_prep_md_op_data(op_data, inode, NULL, NULL,
102 0, 0, LUSTRE_OPC_ANY, NULL);
104 op_data->op_attr.ia_mode = inode->i_mode;
105 op_data->op_attr.ia_atime = inode->i_atime;
106 op_data->op_attr.ia_mtime = inode->i_mtime;
107 op_data->op_attr.ia_ctime = inode->i_ctime;
108 op_data->op_attr.ia_size = i_size_read(inode);
109 op_data->op_attr.ia_valid |= (ATTR_MODE | ATTR_ATIME | ATTR_ATIME_SET |
110 ATTR_MTIME | ATTR_MTIME_SET |
112 op_data->op_xvalid |= OP_XVALID_CTIME_SET;
113 op_data->op_attr_blocks = inode->i_blocks;
114 op_data->op_attr_flags = ll_inode_to_ext_flags(inode->i_flags);
115 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
116 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
117 op_data->op_open_handle = och->och_open_handle;
119 if (och->och_flags & FMODE_WRITE &&
120 test_and_clear_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags))
121 /* For HSM: if inode data has been modified, pack it so that
122 * MDT can set data dirty flag in the archive. */
123 op_data->op_bias |= MDS_DATA_MODIFIED;
129 * Perform a close, possibly with a bias.
130 * The meaning of "data" depends on the value of "bias".
132 * If \a bias is MDS_HSM_RELEASE then \a data is a pointer to the data version.
133 * If \a bias is MDS_CLOSE_LAYOUT_SWAP then \a data is a pointer to the inode to
136 static int ll_close_inode_openhandle(struct inode *inode,
137 struct obd_client_handle *och,
138 enum mds_op_bias bias, void *data)
140 struct obd_export *md_exp = ll_i2mdexp(inode);
141 const struct ll_inode_info *lli = ll_i2info(inode);
142 struct md_op_data *op_data;
143 struct ptlrpc_request *req = NULL;
147 if (class_exp2obd(md_exp) == NULL) {
148 CERROR("%s: invalid MDC connection handle closing "DFID"\n",
149 ll_i2sbi(inode)->ll_fsname, PFID(&lli->lli_fid));
153 OBD_ALLOC_PTR(op_data);
154 /* We leak openhandle and request here on error, but not much to be
155 * done in OOM case since app won't retry close on error either. */
157 GOTO(out, rc = -ENOMEM);
159 ll_prepare_close(inode, op_data, och);
161 case MDS_CLOSE_LAYOUT_MERGE:
162 /* merge blocks from the victim inode */
163 op_data->op_attr_blocks += ((struct inode *)data)->i_blocks;
164 op_data->op_attr.ia_valid |= ATTR_SIZE;
165 op_data->op_xvalid |= OP_XVALID_BLOCKS;
167 case MDS_CLOSE_LAYOUT_SPLIT:
168 case MDS_CLOSE_LAYOUT_SWAP: {
169 struct split_param *sp = data;
171 LASSERT(data != NULL);
172 op_data->op_bias |= bias;
173 op_data->op_data_version = 0;
174 op_data->op_lease_handle = och->och_lease_handle;
175 if (bias == MDS_CLOSE_LAYOUT_SPLIT) {
176 op_data->op_fid2 = *ll_inode2fid(sp->sp_inode);
177 op_data->op_mirror_id = sp->sp_mirror_id;
179 op_data->op_fid2 = *ll_inode2fid(data);
184 case MDS_CLOSE_RESYNC_DONE: {
185 struct ll_ioc_lease *ioc = data;
187 LASSERT(data != NULL);
188 op_data->op_attr_blocks +=
189 ioc->lil_count * op_data->op_attr_blocks;
190 op_data->op_attr.ia_valid |= ATTR_SIZE;
191 op_data->op_xvalid |= OP_XVALID_BLOCKS;
192 op_data->op_bias |= MDS_CLOSE_RESYNC_DONE;
194 op_data->op_lease_handle = och->och_lease_handle;
195 op_data->op_data = &ioc->lil_ids[0];
196 op_data->op_data_size =
197 ioc->lil_count * sizeof(ioc->lil_ids[0]);
201 case MDS_PCC_ATTACH: {
202 struct pcc_param *param = data;
204 LASSERT(data != NULL);
205 op_data->op_bias |= MDS_HSM_RELEASE | MDS_PCC_ATTACH;
206 op_data->op_archive_id = param->pa_archive_id;
207 op_data->op_data_version = param->pa_data_version;
208 op_data->op_lease_handle = och->och_lease_handle;
212 case MDS_HSM_RELEASE:
213 LASSERT(data != NULL);
214 op_data->op_bias |= MDS_HSM_RELEASE;
215 op_data->op_data_version = *(__u64 *)data;
216 op_data->op_lease_handle = och->och_lease_handle;
217 op_data->op_attr.ia_valid |= ATTR_SIZE;
218 op_data->op_xvalid |= OP_XVALID_BLOCKS;
222 LASSERT(data == NULL);
226 if (!(op_data->op_attr.ia_valid & ATTR_SIZE))
227 op_data->op_xvalid |= OP_XVALID_LAZYSIZE;
228 if (!(op_data->op_xvalid & OP_XVALID_BLOCKS))
229 op_data->op_xvalid |= OP_XVALID_LAZYBLOCKS;
231 rc = md_close(md_exp, op_data, och->och_mod, &req);
232 if (rc != 0 && rc != -EINTR)
233 CERROR("%s: inode "DFID" mdc close failed: rc = %d\n",
234 md_exp->exp_obd->obd_name, PFID(&lli->lli_fid), rc);
236 if (rc == 0 && op_data->op_bias & bias) {
237 struct mdt_body *body;
239 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
240 if (!(body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED))
243 if (bias & MDS_PCC_ATTACH) {
244 struct pcc_param *param = data;
246 param->pa_layout_gen = body->mbo_layout_gen;
250 ll_finish_md_op_data(op_data);
254 md_clear_open_replay_data(md_exp, och);
255 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
258 ptlrpc_req_finished(req); /* This is close request */
262 int ll_md_real_close(struct inode *inode, fmode_t fmode)
264 struct ll_inode_info *lli = ll_i2info(inode);
265 struct obd_client_handle **och_p;
266 struct obd_client_handle *och;
271 if (fmode & FMODE_WRITE) {
272 och_p = &lli->lli_mds_write_och;
273 och_usecount = &lli->lli_open_fd_write_count;
274 } else if (fmode & FMODE_EXEC) {
275 och_p = &lli->lli_mds_exec_och;
276 och_usecount = &lli->lli_open_fd_exec_count;
278 LASSERT(fmode & FMODE_READ);
279 och_p = &lli->lli_mds_read_och;
280 och_usecount = &lli->lli_open_fd_read_count;
283 mutex_lock(&lli->lli_och_mutex);
284 if (*och_usecount > 0) {
285 /* There are still users of this handle, so skip
287 mutex_unlock(&lli->lli_och_mutex);
293 mutex_unlock(&lli->lli_och_mutex);
296 /* There might be a race and this handle may already
298 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
304 static int ll_md_close(struct inode *inode, struct file *file)
306 union ldlm_policy_data policy = {
307 .l_inodebits = { MDS_INODELOCK_OPEN },
309 __u64 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_TEST_LOCK;
310 struct ll_file_data *fd = file->private_data;
311 struct ll_inode_info *lli = ll_i2info(inode);
312 struct lustre_handle lockh;
313 enum ldlm_mode lockmode;
317 /* clear group lock, if present */
318 if (unlikely(fd->fd_flags & LL_FILE_GROUP_LOCKED))
319 ll_put_grouplock(inode, file, fd->fd_grouplock.lg_gid);
321 if (fd->fd_lease_och != NULL) {
324 /* Usually the lease is not released when the
325 * application crashed, we need to release here. */
326 rc = ll_lease_close(fd->fd_lease_och, inode, &lease_broken);
327 CDEBUG_LIMIT(rc ? D_ERROR : D_INODE,
328 "Clean up lease "DFID" %d/%d\n",
329 PFID(&lli->lli_fid), rc, lease_broken);
331 fd->fd_lease_och = NULL;
334 if (fd->fd_och != NULL) {
335 rc = ll_close_inode_openhandle(inode, fd->fd_och, 0, NULL);
340 /* Let's see if we have good enough OPEN lock on the file and if
341 we can skip talking to MDS */
342 mutex_lock(&lli->lli_och_mutex);
343 if (fd->fd_omode & FMODE_WRITE) {
345 LASSERT(lli->lli_open_fd_write_count);
346 lli->lli_open_fd_write_count--;
347 } else if (fd->fd_omode & FMODE_EXEC) {
349 LASSERT(lli->lli_open_fd_exec_count);
350 lli->lli_open_fd_exec_count--;
353 LASSERT(lli->lli_open_fd_read_count);
354 lli->lli_open_fd_read_count--;
356 mutex_unlock(&lli->lli_och_mutex);
358 /* LU-4398: do not cache write open lock if the file has exec bit */
359 if ((lockmode == LCK_CW && inode->i_mode & S_IXUGO) ||
360 !md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
361 LDLM_IBITS, &policy, lockmode, &lockh))
362 rc = ll_md_real_close(inode, fd->fd_omode);
365 file->private_data = NULL;
366 ll_file_data_put(fd);
371 /* While this returns an error code, fput() the caller does not, so we need
372 * to make every effort to clean up all of our state here. Also, applications
373 * rarely check close errors and even if an error is returned they will not
374 * re-try the close call.
376 int ll_file_release(struct inode *inode, struct file *file)
378 struct ll_file_data *fd;
379 struct ll_sb_info *sbi = ll_i2sbi(inode);
380 struct ll_inode_info *lli = ll_i2info(inode);
381 ktime_t kstart = ktime_get();
386 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
387 PFID(ll_inode2fid(inode)), inode);
389 fd = file->private_data;
392 /* The last ref on @file, maybe not the the owner pid of statahead,
393 * because parent and child process can share the same file handle. */
394 if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd)
395 ll_deauthorize_statahead(inode, fd);
397 if (is_root_inode(inode)) {
398 file->private_data = NULL;
399 ll_file_data_put(fd);
403 pcc_file_release(inode, file);
405 if (!S_ISDIR(inode->i_mode)) {
406 if (lli->lli_clob != NULL)
407 lov_read_and_clear_async_rc(lli->lli_clob);
408 lli->lli_async_rc = 0;
411 lli->lli_close_fd_time = ktime_get();
413 rc = ll_md_close(inode, file);
415 if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
416 libcfs_debug_dumplog();
419 if (!rc && !is_root_inode(inode))
420 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE,
421 ktime_us_delta(ktime_get(), kstart));
425 static inline int ll_dom_readpage(void *data, struct page *page)
427 struct niobuf_local *lnb = data;
431 struct inode *inode = page2inode(page);
433 kaddr = kmap_atomic(page);
434 memcpy(kaddr, lnb->lnb_data, lnb->lnb_len);
435 if (lnb->lnb_len < PAGE_SIZE)
436 memset(kaddr + lnb->lnb_len, 0,
437 PAGE_SIZE - lnb->lnb_len);
438 flush_dcache_page(page);
439 SetPageUptodate(page);
440 kunmap_atomic(kaddr);
442 if (inode && IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
443 if (!llcrypt_has_encryption_key(inode))
444 CDEBUG(D_SEC, "no enc key for "DFID"\n",
445 PFID(ll_inode2fid(inode)));
447 unsigned int offs = 0;
449 while (offs < PAGE_SIZE) {
450 /* decrypt only if page is not empty */
451 if (memcmp(page_address(page) + offs,
452 page_address(ZERO_PAGE(0)),
453 LUSTRE_ENCRYPTION_UNIT_SIZE) == 0)
456 rc = llcrypt_decrypt_pagecache_blocks(page,
457 LUSTRE_ENCRYPTION_UNIT_SIZE,
462 offs += LUSTRE_ENCRYPTION_UNIT_SIZE;
471 void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req)
475 struct ll_inode_info *lli = ll_i2info(inode);
476 struct cl_object *obj = lli->lli_clob;
477 struct address_space *mapping = inode->i_mapping;
479 struct niobuf_remote *rnb;
480 struct mdt_body *body;
482 unsigned long index, start;
483 struct niobuf_local lnb;
492 if (!req_capsule_field_present(&req->rq_pill, &RMF_NIOBUF_INLINE,
496 rnb = req_capsule_server_get(&req->rq_pill, &RMF_NIOBUF_INLINE);
497 if (rnb == NULL || rnb->rnb_len == 0)
500 /* LU-11595: Server may return whole file and that is OK always or
501 * it may return just file tail and its offset must be aligned with
502 * client PAGE_SIZE to be used on that client, if server's PAGE_SIZE is
503 * smaller then offset may be not aligned and that data is just ignored.
505 if (rnb->rnb_offset & ~PAGE_MASK)
508 /* Server returns whole file or just file tail if it fills in reply
509 * buffer, in both cases total size should be equal to the file size.
511 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
512 if (rnb->rnb_offset + rnb->rnb_len != body->mbo_dom_size &&
513 !(inode && IS_ENCRYPTED(inode))) {
514 CERROR("%s: server returns off/len %llu/%u but size %llu\n",
515 ll_i2sbi(inode)->ll_fsname, rnb->rnb_offset,
516 rnb->rnb_len, body->mbo_dom_size);
520 env = cl_env_get(&refcheck);
523 io = vvp_env_thread_io(env);
525 io->ci_ignore_layout = 1;
526 rc = cl_io_init(env, io, CIT_MISC, obj);
530 CDEBUG(D_INFO, "Get data along with open at %llu len %i, size %llu\n",
531 rnb->rnb_offset, rnb->rnb_len, body->mbo_dom_size);
533 data = (char *)rnb + sizeof(*rnb);
535 lnb.lnb_file_offset = rnb->rnb_offset;
536 start = lnb.lnb_file_offset >> PAGE_SHIFT;
538 LASSERT((lnb.lnb_file_offset & ~PAGE_MASK) == 0);
539 lnb.lnb_page_offset = 0;
541 struct cl_page *page;
543 lnb.lnb_data = data + (index << PAGE_SHIFT);
544 lnb.lnb_len = rnb->rnb_len - (index << PAGE_SHIFT);
545 if (lnb.lnb_len > PAGE_SIZE)
546 lnb.lnb_len = PAGE_SIZE;
548 vmpage = read_cache_page(mapping, index + start,
549 ll_dom_readpage, &lnb);
550 if (IS_ERR(vmpage)) {
551 CWARN("%s: cannot fill page %lu for "DFID
552 " with data: rc = %li\n",
553 ll_i2sbi(inode)->ll_fsname, index + start,
554 PFID(lu_object_fid(&obj->co_lu)),
559 if (vmpage->mapping == NULL) {
562 /* page was truncated */
565 /* attach VM page to CL page cache */
566 page = cl_page_find(env, obj, vmpage->index, vmpage,
569 ClearPageUptodate(vmpage);
574 cl_page_export(env, page, 1);
575 cl_page_put(env, page);
579 } while (rnb->rnb_len > (index << PAGE_SHIFT));
583 cl_env_put(env, &refcheck);
588 static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
589 struct lookup_intent *itp)
591 struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
592 struct dentry *parent = de->d_parent;
595 struct md_op_data *op_data;
596 struct ptlrpc_request *req = NULL;
600 LASSERT(parent != NULL);
601 LASSERT(itp->it_flags & MDS_OPEN_BY_FID);
603 /* if server supports open-by-fid, or file name is invalid, don't pack
604 * name in open request */
605 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_OPEN_BY_NAME) ||
606 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID)) {
608 len = de->d_name.len;
609 name = kmalloc(len + 1, GFP_NOFS);
614 spin_lock(&de->d_lock);
615 if (len != de->d_name.len) {
616 spin_unlock(&de->d_lock);
620 memcpy(name, de->d_name.name, len);
622 spin_unlock(&de->d_lock);
624 if (!lu_name_is_valid_2(name, len)) {
630 op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
631 name, len, 0, LUSTRE_OPC_ANY, NULL);
632 if (IS_ERR(op_data)) {
634 RETURN(PTR_ERR(op_data));
636 op_data->op_data = lmm;
637 op_data->op_data_size = lmmsize;
639 OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_OPEN_DELAY, cfs_fail_val);
641 rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
642 &ll_md_blocking_ast, 0);
644 ll_finish_md_op_data(op_data);
646 /* reason for keep own exit path - don`t flood log
647 * with messages with -ESTALE errors.
649 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
650 it_open_error(DISP_OPEN_OPEN, itp))
652 ll_release_openhandle(de, itp);
656 if (it_disposition(itp, DISP_LOOKUP_NEG))
657 GOTO(out, rc = -ENOENT);
659 if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
660 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
661 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
665 rc = ll_prep_inode(&de->d_inode, &req->rq_pill, NULL, itp);
667 if (!rc && itp->it_lock_mode) {
670 /* If we got a lock back and it has a LOOKUP bit set,
671 * make sure the dentry is marked as valid so we can find it.
672 * We don't need to care about actual hashing since other bits
673 * of kernel will deal with that later.
675 ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, &bits);
676 if (bits & MDS_INODELOCK_LOOKUP) {
677 d_lustre_revalidate(de);
678 ll_update_dir_depth(parent->d_inode, de->d_inode);
681 /* if DoM bit returned along with LAYOUT bit then there
682 * can be read-on-open data returned.
684 if (bits & MDS_INODELOCK_DOM && bits & MDS_INODELOCK_LAYOUT)
685 ll_dom_finish_open(de->d_inode, req);
689 ptlrpc_req_finished(req);
690 ll_intent_drop_lock(itp);
692 /* We did open by fid, but by the time we got to the server, the object
693 * disappeared. This is possible if the object was unlinked, but it's
694 * also possible if the object was unlinked by a rename. In the case
695 * of an object renamed over our existing one, we can't fail this open.
696 * O_CREAT also goes through this path if we had an existing dentry,
697 * and it's obviously wrong to return ENOENT for O_CREAT.
699 * Instead let's return -ESTALE, and the VFS will retry the open with
700 * LOOKUP_REVAL, which we catch in ll_revalidate_dentry and fail to
701 * revalidate, causing a lookup. This causes extra lookups in the case
702 * where we had a dentry in cache but the file is being unlinked and we
703 * lose the race with unlink, but this should be very rare.
711 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
712 struct obd_client_handle *och)
714 struct mdt_body *body;
716 body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
717 och->och_open_handle = body->mbo_open_handle;
718 och->och_fid = body->mbo_fid1;
719 och->och_lease_handle.cookie = it->it_lock_handle;
720 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
721 och->och_flags = it->it_flags;
723 return md_set_open_replay_data(md_exp, och, it);
726 static int ll_local_open(struct file *file, struct lookup_intent *it,
727 struct ll_file_data *fd, struct obd_client_handle *och)
729 struct inode *inode = file_inode(file);
732 LASSERT(!file->private_data);
739 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
744 file->private_data = fd;
745 ll_readahead_init(inode, &fd->fd_ras);
746 fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
747 /* turn off the kernel's read-ahead */
748 file->f_ra.ra_pages = 0;
750 /* ll_cl_context initialize */
751 rwlock_init(&fd->fd_lock);
752 INIT_LIST_HEAD(&fd->fd_lccs);
757 void ll_track_file_opens(struct inode *inode)
759 struct ll_inode_info *lli = ll_i2info(inode);
760 struct ll_sb_info *sbi = ll_i2sbi(inode);
762 /* do not skew results with delays from never-opened inodes */
763 if (ktime_to_ns(lli->lli_close_fd_time))
764 ll_stats_ops_tally(sbi, LPROC_LL_INODE_OPCLTM,
765 ktime_us_delta(ktime_get(), lli->lli_close_fd_time));
767 if (ktime_after(ktime_get(),
768 ktime_add_ms(lli->lli_close_fd_time,
769 sbi->ll_oc_max_ms))) {
770 lli->lli_open_fd_count = 1;
771 lli->lli_close_fd_time = ns_to_ktime(0);
773 lli->lli_open_fd_count++;
776 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_OCOUNT,
777 lli->lli_open_fd_count);
780 /* Open a file, and (for the very first open) create objects on the OSTs at
781 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
782 * creation or open until ll_lov_setstripe() ioctl is called.
784 * If we already have the stripe MD locally then we don't request it in
785 * md_open(), by passing a lmm_size = 0.
787 * It is up to the application to ensure no other processes open this file
788 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
789 * used. We might be able to avoid races of that sort by getting lli_open_sem
790 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
791 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
793 int ll_file_open(struct inode *inode, struct file *file)
795 struct ll_inode_info *lli = ll_i2info(inode);
796 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
797 .it_flags = file->f_flags };
798 struct obd_client_handle **och_p = NULL;
799 __u64 *och_usecount = NULL;
800 struct ll_file_data *fd;
801 ktime_t kstart = ktime_get();
805 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
806 PFID(ll_inode2fid(inode)), inode, file->f_flags);
808 it = file->private_data; /* XXX: compat macro */
809 file->private_data = NULL; /* prevent ll_local_open assertion */
811 if (S_ISREG(inode->i_mode)) {
812 rc = llcrypt_file_open(inode, file);
814 GOTO(out_nofiledata, rc);
817 fd = ll_file_data_get();
819 GOTO(out_nofiledata, rc = -ENOMEM);
822 if (S_ISDIR(inode->i_mode))
823 ll_authorize_statahead(inode, fd);
825 ll_track_file_opens(inode);
826 if (is_root_inode(inode)) {
827 file->private_data = fd;
831 if (!it || !it->it_disposition) {
832 /* Convert f_flags into access mode. We cannot use file->f_mode,
833 * because everything but O_ACCMODE mask was stripped from
835 if ((oit.it_flags + 1) & O_ACCMODE)
837 if (file->f_flags & O_TRUNC)
838 oit.it_flags |= FMODE_WRITE;
840 /* kernel only call f_op->open in dentry_open. filp_open calls
841 * dentry_open after call to open_namei that checks permissions.
842 * Only nfsd_open call dentry_open directly without checking
843 * permissions and because of that this code below is safe.
845 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
846 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
848 /* We do not want O_EXCL here, presumably we opened the file
849 * already? XXX - NFS implications? */
850 oit.it_flags &= ~O_EXCL;
852 /* bug20584, if "it_flags" contains O_CREAT, the file will be
853 * created if necessary, then "IT_CREAT" should be set to keep
854 * consistent with it */
855 if (oit.it_flags & O_CREAT)
856 oit.it_op |= IT_CREAT;
862 /* Let's see if we have file open on MDS already. */
863 if (it->it_flags & FMODE_WRITE) {
864 och_p = &lli->lli_mds_write_och;
865 och_usecount = &lli->lli_open_fd_write_count;
866 } else if (it->it_flags & FMODE_EXEC) {
867 och_p = &lli->lli_mds_exec_och;
868 och_usecount = &lli->lli_open_fd_exec_count;
870 och_p = &lli->lli_mds_read_och;
871 och_usecount = &lli->lli_open_fd_read_count;
874 mutex_lock(&lli->lli_och_mutex);
875 if (*och_p) { /* Open handle is present */
876 if (it_disposition(it, DISP_OPEN_OPEN)) {
877 /* Well, there's extra open request that we do not need,
878 * let's close it somehow. This will decref request. */
879 rc = it_open_error(DISP_OPEN_OPEN, it);
881 mutex_unlock(&lli->lli_och_mutex);
882 GOTO(out_openerr, rc);
885 ll_release_openhandle(file_dentry(file), it);
889 rc = ll_local_open(file, it, fd, NULL);
892 mutex_unlock(&lli->lli_och_mutex);
893 GOTO(out_openerr, rc);
896 LASSERT(*och_usecount == 0);
897 if (!it->it_disposition) {
898 struct dentry *dentry = file_dentry(file);
899 struct ll_sb_info *sbi = ll_i2sbi(inode);
900 struct ll_dentry_data *ldd;
902 /* We cannot just request lock handle now, new ELC code
903 * means that one of other OPEN locks for this file
904 * could be cancelled, and since blocking ast handler
905 * would attempt to grab och_mutex as well, that would
906 * result in a deadlock
908 mutex_unlock(&lli->lli_och_mutex);
910 * Normally called under two situations:
912 * 2. A race/condition on MDS resulting in no open
913 * handle to be returned from LOOKUP|OPEN request,
914 * for example if the target entry was a symlink.
916 * In NFS path we know there's pathologic behavior
917 * so we always enable open lock caching when coming
918 * from there. It's detected by setting a flag in
921 * After reaching number of opens of this inode
922 * we always ask for an open lock on it to handle
923 * bad userspace actors that open and close files
924 * in a loop for absolutely no good reason
927 ldd = ll_d2d(dentry);
928 if (filename_is_volatile(dentry->d_name.name,
931 /* There really is nothing here, but this
932 * make this more readable I think.
933 * We do not want openlock for volatile
934 * files under any circumstances
936 } else if (ldd && ldd->lld_nfs_dentry) {
937 /* NFS path. This also happens to catch
938 * open by fh files I guess
940 it->it_flags |= MDS_OPEN_LOCK;
941 /* clear the flag for future lookups */
942 ldd->lld_nfs_dentry = 0;
943 } else if (sbi->ll_oc_thrsh_count > 0) {
944 /* Take MDS_OPEN_LOCK with many opens */
945 if (lli->lli_open_fd_count >=
946 sbi->ll_oc_thrsh_count)
947 it->it_flags |= MDS_OPEN_LOCK;
949 /* If this is open after we just closed */
950 else if (ktime_before(ktime_get(),
951 ktime_add_ms(lli->lli_close_fd_time,
952 sbi->ll_oc_thrsh_ms)))
953 it->it_flags |= MDS_OPEN_LOCK;
957 * Always specify MDS_OPEN_BY_FID because we don't want
958 * to get file with different fid.
960 it->it_flags |= MDS_OPEN_BY_FID;
961 rc = ll_intent_file_open(dentry, NULL, 0, it);
963 GOTO(out_openerr, rc);
967 OBD_ALLOC(*och_p, sizeof(struct obd_client_handle));
969 GOTO(out_och_free, rc = -ENOMEM);
973 /* md_intent_lock() didn't get a request ref if there was an
974 * open error, so don't do cleanup on the request here
976 /* XXX (green): Should not we bail out on any error here, not
977 * just open error? */
978 rc = it_open_error(DISP_OPEN_OPEN, it);
980 GOTO(out_och_free, rc);
982 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
983 "inode %p: disposition %x, status %d\n", inode,
984 it_disposition(it, ~0), it->it_status);
986 rc = ll_local_open(file, it, fd, *och_p);
988 GOTO(out_och_free, rc);
991 rc = pcc_file_open(inode, file);
993 GOTO(out_och_free, rc);
995 mutex_unlock(&lli->lli_och_mutex);
999 /* Must do this outside lli_och_mutex lock to prevent deadlock where
1000 different kind of OPEN lock for this same inode gets cancelled
1001 by ldlm_cancel_lru */
1002 if (!S_ISREG(inode->i_mode))
1003 GOTO(out_och_free, rc);
1004 cl_lov_delay_create_clear(&file->f_flags);
1005 GOTO(out_och_free, rc);
1009 if (och_p && *och_p) {
1010 OBD_FREE(*och_p, sizeof(struct obd_client_handle));
1011 *och_p = NULL; /* OBD_FREE writes some magic there */
1014 mutex_unlock(&lli->lli_och_mutex);
1017 if (lli->lli_opendir_key == fd)
1018 ll_deauthorize_statahead(inode, fd);
1021 ll_file_data_put(fd);
1023 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN,
1024 ktime_us_delta(ktime_get(), kstart));
1028 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
1029 ptlrpc_req_finished(it->it_request);
1030 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1036 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
1037 struct ldlm_lock_desc *desc, void *data, int flag)
1040 struct lustre_handle lockh;
1044 case LDLM_CB_BLOCKING:
1045 ldlm_lock2handle(lock, &lockh);
1046 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1048 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
1052 case LDLM_CB_CANCELING:
1060 * When setting a lease on a file, we take ownership of the lli_mds_*_och
1061 * and save it as fd->fd_och so as to force client to reopen the file even
1062 * if it has an open lock in cache already.
1064 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
1065 struct lustre_handle *old_open_handle)
1067 struct ll_inode_info *lli = ll_i2info(inode);
1068 struct ll_file_data *fd = file->private_data;
1069 struct obd_client_handle **och_p;
1070 __u64 *och_usecount;
1074 /* Get the openhandle of the file */
1075 mutex_lock(&lli->lli_och_mutex);
1076 if (fd->fd_lease_och != NULL)
1077 GOTO(out_unlock, rc = -EBUSY);
1079 if (fd->fd_och == NULL) {
1080 if (file->f_mode & FMODE_WRITE) {
1081 LASSERT(lli->lli_mds_write_och != NULL);
1082 och_p = &lli->lli_mds_write_och;
1083 och_usecount = &lli->lli_open_fd_write_count;
1085 LASSERT(lli->lli_mds_read_och != NULL);
1086 och_p = &lli->lli_mds_read_och;
1087 och_usecount = &lli->lli_open_fd_read_count;
1090 if (*och_usecount > 1)
1091 GOTO(out_unlock, rc = -EBUSY);
1093 fd->fd_och = *och_p;
1098 *old_open_handle = fd->fd_och->och_open_handle;
1102 mutex_unlock(&lli->lli_och_mutex);
1107 * Release ownership on lli_mds_*_och when putting back a file lease.
1109 static int ll_lease_och_release(struct inode *inode, struct file *file)
1111 struct ll_inode_info *lli = ll_i2info(inode);
1112 struct ll_file_data *fd = file->private_data;
1113 struct obd_client_handle **och_p;
1114 struct obd_client_handle *old_och = NULL;
1115 __u64 *och_usecount;
1119 mutex_lock(&lli->lli_och_mutex);
1120 if (file->f_mode & FMODE_WRITE) {
1121 och_p = &lli->lli_mds_write_och;
1122 och_usecount = &lli->lli_open_fd_write_count;
1124 och_p = &lli->lli_mds_read_och;
1125 och_usecount = &lli->lli_open_fd_read_count;
1128 /* The file may have been open by another process (broken lease) so
1129 * *och_p is not NULL. In this case we should simply increase usecount
1132 if (*och_p != NULL) {
1133 old_och = fd->fd_och;
1136 *och_p = fd->fd_och;
1140 mutex_unlock(&lli->lli_och_mutex);
1142 if (old_och != NULL)
1143 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
1149 * Acquire a lease and open the file.
1151 static struct obd_client_handle *
1152 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
1155 struct lookup_intent it = { .it_op = IT_OPEN };
1156 struct ll_sb_info *sbi = ll_i2sbi(inode);
1157 struct md_op_data *op_data;
1158 struct ptlrpc_request *req = NULL;
1159 struct lustre_handle old_open_handle = { 0 };
1160 struct obd_client_handle *och = NULL;
1165 if (fmode != FMODE_WRITE && fmode != FMODE_READ)
1166 RETURN(ERR_PTR(-EINVAL));
1169 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
1170 RETURN(ERR_PTR(-EPERM));
1172 rc = ll_lease_och_acquire(inode, file, &old_open_handle);
1174 RETURN(ERR_PTR(rc));
1179 RETURN(ERR_PTR(-ENOMEM));
1181 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
1182 LUSTRE_OPC_ANY, NULL);
1183 if (IS_ERR(op_data))
1184 GOTO(out, rc = PTR_ERR(op_data));
1186 /* To tell the MDT this openhandle is from the same owner */
1187 op_data->op_open_handle = old_open_handle;
1189 it.it_flags = fmode | open_flags;
1190 it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
1191 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
1192 &ll_md_blocking_lease_ast,
1193 /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
1194 * it can be cancelled which may mislead applications that the lease is
1196 * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
1197 * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
1198 * doesn't deal with openhandle, so normal openhandle will be leaked. */
1199 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
1200 ll_finish_md_op_data(op_data);
1201 ptlrpc_req_finished(req);
1203 GOTO(out_release_it, rc);
1205 if (it_disposition(&it, DISP_LOOKUP_NEG))
1206 GOTO(out_release_it, rc = -ENOENT);
1208 rc = it_open_error(DISP_OPEN_OPEN, &it);
1210 GOTO(out_release_it, rc);
1212 LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
1213 rc = ll_och_fill(sbi->ll_md_exp, &it, och);
1215 GOTO(out_release_it, rc);
1217 if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
1218 GOTO(out_close, rc = -EOPNOTSUPP);
1220 /* already get lease, handle lease lock */
1221 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
1222 if (!it.it_lock_mode ||
1223 !(it.it_lock_bits & MDS_INODELOCK_OPEN)) {
1224 /* open lock must return for lease */
1225 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
1226 PFID(ll_inode2fid(inode)), it.it_lock_mode,
1228 GOTO(out_close, rc = -EPROTO);
1231 ll_intent_release(&it);
1235 /* Cancel open lock */
1236 if (it.it_lock_mode != 0) {
1237 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
1239 it.it_lock_mode = 0;
1240 och->och_lease_handle.cookie = 0ULL;
1242 rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
1244 CERROR("%s: error closing file "DFID": %d\n",
1245 sbi->ll_fsname, PFID(&ll_i2info(inode)->lli_fid), rc2);
1246 och = NULL; /* och has been freed in ll_close_inode_openhandle() */
1248 ll_intent_release(&it);
1252 RETURN(ERR_PTR(rc));
1256 * Check whether a layout swap can be done between two inodes.
1258 * \param[in] inode1 First inode to check
1259 * \param[in] inode2 Second inode to check
1261 * \retval 0 on success, layout swap can be performed between both inodes
1262 * \retval negative error code if requirements are not met
1264 static int ll_check_swap_layouts_validity(struct inode *inode1,
1265 struct inode *inode2)
1267 if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
1270 if (inode_permission(inode1, MAY_WRITE) ||
1271 inode_permission(inode2, MAY_WRITE))
1274 if (inode1->i_sb != inode2->i_sb)
1280 static int ll_swap_layouts_close(struct obd_client_handle *och,
1281 struct inode *inode, struct inode *inode2)
1283 const struct lu_fid *fid1 = ll_inode2fid(inode);
1284 const struct lu_fid *fid2;
1288 CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
1289 ll_i2sbi(inode)->ll_fsname, PFID(fid1));
1291 rc = ll_check_swap_layouts_validity(inode, inode2);
1293 GOTO(out_free_och, rc);
1295 /* We now know that inode2 is a lustre inode */
1296 fid2 = ll_inode2fid(inode2);
1298 rc = lu_fid_cmp(fid1, fid2);
1300 GOTO(out_free_och, rc = -EINVAL);
1302 /* Close the file and {swap,merge} layouts between inode & inode2.
1303 * NB: lease lock handle is released in mdc_close_layout_swap_pack()
1304 * because we still need it to pack l_remote_handle to MDT. */
1305 rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
1308 och = NULL; /* freed in ll_close_inode_openhandle() */
1318 * Release lease and close the file.
1319 * It will check if the lease has ever broken.
1321 static int ll_lease_close_intent(struct obd_client_handle *och,
1322 struct inode *inode,
1323 bool *lease_broken, enum mds_op_bias bias,
1326 struct ldlm_lock *lock;
1327 bool cancelled = true;
1331 lock = ldlm_handle2lock(&och->och_lease_handle);
1333 lock_res_and_lock(lock);
1334 cancelled = ldlm_is_cancel(lock);
1335 unlock_res_and_lock(lock);
1336 LDLM_LOCK_PUT(lock);
1339 CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
1340 PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
1342 if (lease_broken != NULL)
1343 *lease_broken = cancelled;
1345 if (!cancelled && !bias)
1346 ldlm_cli_cancel(&och->och_lease_handle, 0);
1348 if (cancelled) { /* no need to excute intent */
1353 rc = ll_close_inode_openhandle(inode, och, bias, data);
1357 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
1360 return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
1364 * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
1366 static int ll_lease_file_resync(struct obd_client_handle *och,
1367 struct inode *inode, unsigned long arg)
1369 struct ll_sb_info *sbi = ll_i2sbi(inode);
1370 struct md_op_data *op_data;
1371 struct ll_ioc_lease_id ioc;
1372 __u64 data_version_unused;
1376 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1377 LUSTRE_OPC_ANY, NULL);
1378 if (IS_ERR(op_data))
1379 RETURN(PTR_ERR(op_data));
1381 if (copy_from_user(&ioc, (struct ll_ioc_lease_id __user *)arg,
1385 /* before starting file resync, it's necessary to clean up page cache
1386 * in client memory, otherwise once the layout version is increased,
1387 * writing back cached data will be denied the OSTs. */
1388 rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
1392 op_data->op_lease_handle = och->och_lease_handle;
1393 op_data->op_mirror_id = ioc.lil_mirror_id;
1394 rc = md_file_resync(sbi->ll_md_exp, op_data);
1400 ll_finish_md_op_data(op_data);
1404 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1406 struct ll_inode_info *lli = ll_i2info(inode);
1407 struct cl_object *obj = lli->lli_clob;
1408 struct cl_attr *attr = vvp_env_thread_attr(env);
1416 ll_inode_size_lock(inode);
1418 /* Merge timestamps the most recently obtained from MDS with
1419 * timestamps obtained from OSTs.
1421 * Do not overwrite atime of inode because it may be refreshed
1422 * by file_accessed() function. If the read was served by cache
1423 * data, there is no RPC to be sent so that atime may not be
1424 * transferred to OSTs at all. MDT only updates atime at close time
1425 * if it's at least 'mdd.*.atime_diff' older.
1426 * All in all, the atime in Lustre does not strictly comply with
1427 * POSIX. Solving this problem needs to send an RPC to MDT for each
1428 * read, this will hurt performance.
1430 if (test_and_clear_bit(LLIF_UPDATE_ATIME, &lli->lli_flags) ||
1431 inode->i_atime.tv_sec < lli->lli_atime)
1432 inode->i_atime.tv_sec = lli->lli_atime;
1434 inode->i_mtime.tv_sec = lli->lli_mtime;
1435 inode->i_ctime.tv_sec = lli->lli_ctime;
1437 mtime = inode->i_mtime.tv_sec;
1438 atime = inode->i_atime.tv_sec;
1439 ctime = inode->i_ctime.tv_sec;
1441 cl_object_attr_lock(obj);
1442 if (OBD_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
1445 rc = cl_object_attr_get(env, obj, attr);
1446 cl_object_attr_unlock(obj);
1449 GOTO(out_size_unlock, rc = (rc == -ENODATA ? 0 : rc));
1451 if (atime < attr->cat_atime)
1452 atime = attr->cat_atime;
1454 if (ctime < attr->cat_ctime)
1455 ctime = attr->cat_ctime;
1457 if (mtime < attr->cat_mtime)
1458 mtime = attr->cat_mtime;
1460 CDEBUG(D_VFSTRACE, DFID" updating i_size %llu\n",
1461 PFID(&lli->lli_fid), attr->cat_size);
1463 i_size_write(inode, attr->cat_size);
1464 inode->i_blocks = attr->cat_blocks;
1466 inode->i_mtime.tv_sec = mtime;
1467 inode->i_atime.tv_sec = atime;
1468 inode->i_ctime.tv_sec = ctime;
1471 ll_inode_size_unlock(inode);
1477 * Set designated mirror for I/O.
1479 * So far only read, write, and truncated can support to issue I/O to
1480 * designated mirror.
1482 void ll_io_set_mirror(struct cl_io *io, const struct file *file)
1484 struct ll_file_data *fd = file->private_data;
1486 /* clear layout version for generic(non-resync) I/O in case it carries
1487 * stale layout version due to I/O restart */
1488 io->ci_layout_version = 0;
1490 /* FLR: disable non-delay for designated mirror I/O because obviously
1491 * only one mirror is available */
1492 if (fd->fd_designated_mirror > 0) {
1494 io->ci_designated_mirror = fd->fd_designated_mirror;
1495 io->ci_layout_version = fd->fd_layout_version;
1498 CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
1499 file->f_path.dentry->d_name.name, io->ci_designated_mirror);
1502 static bool file_is_noatime(const struct file *file)
1504 const struct vfsmount *mnt = file->f_path.mnt;
1505 const struct inode *inode = file_inode((struct file *)file);
1507 /* Adapted from file_accessed() and touch_atime().*/
1508 if (file->f_flags & O_NOATIME)
1511 if (inode->i_flags & S_NOATIME)
1514 if (IS_NOATIME(inode))
1517 if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1520 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1523 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
1529 void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot,
1530 struct vvp_io_args *args)
1532 struct inode *inode = file_inode(file);
1533 struct ll_file_data *fd = file->private_data;
1535 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1536 io->ci_lock_no_expand = fd->ll_lock_no_expand;
1538 if (iot == CIT_WRITE) {
1539 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1540 io->u.ci_wr.wr_sync = !!(file->f_flags & O_SYNC ||
1541 file->f_flags & O_DIRECT ||
1543 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1544 io->u.ci_wr.wr_sync |= !!(args &&
1545 (args->u.normal.via_iocb->ki_flags &
1550 io->ci_obj = ll_i2info(inode)->lli_clob;
1551 io->ci_lockreq = CILR_MAYBE;
1552 if (ll_file_nolock(file)) {
1553 io->ci_lockreq = CILR_NEVER;
1554 io->ci_no_srvlock = 1;
1555 } else if (file->f_flags & O_APPEND) {
1556 io->ci_lockreq = CILR_MANDATORY;
1558 io->ci_noatime = file_is_noatime(file);
1559 io->ci_async_readahead = false;
1561 /* FLR: only use non-delay I/O for read as there is only one
1562 * avaliable mirror for write. */
1563 io->ci_ndelay = !(iot == CIT_WRITE);
1565 ll_io_set_mirror(io, file);
1568 static void ll_heat_add(struct inode *inode, enum cl_io_type iot,
1571 struct ll_inode_info *lli = ll_i2info(inode);
1572 struct ll_sb_info *sbi = ll_i2sbi(inode);
1573 enum obd_heat_type sample_type;
1574 enum obd_heat_type iobyte_type;
1575 __u64 now = ktime_get_real_seconds();
1577 if (!ll_sbi_has_file_heat(sbi) ||
1578 lli->lli_heat_flags & LU_HEAT_FLAG_OFF)
1581 if (iot == CIT_READ) {
1582 sample_type = OBD_HEAT_READSAMPLE;
1583 iobyte_type = OBD_HEAT_READBYTE;
1584 } else if (iot == CIT_WRITE) {
1585 sample_type = OBD_HEAT_WRITESAMPLE;
1586 iobyte_type = OBD_HEAT_WRITEBYTE;
1591 spin_lock(&lli->lli_heat_lock);
1592 obd_heat_add(&lli->lli_heat_instances[sample_type], now, 1,
1593 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1594 obd_heat_add(&lli->lli_heat_instances[iobyte_type], now, count,
1595 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1596 spin_unlock(&lli->lli_heat_lock);
1600 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1601 struct file *file, enum cl_io_type iot,
1602 loff_t *ppos, size_t count)
1604 struct vvp_io *vio = vvp_env_io(env);
1605 struct inode *inode = file_inode(file);
1606 struct ll_inode_info *lli = ll_i2info(inode);
1607 struct ll_sb_info *sbi = ll_i2sbi(inode);
1608 struct ll_file_data *fd = file->private_data;
1609 struct range_lock range;
1610 bool range_locked = false;
1615 unsigned int retried = 0, dio_lock = 0;
1616 bool is_aio = false;
1617 bool is_parallel_dio = false;
1618 struct cl_dio_aio *ci_aio = NULL;
1620 bool partial_io = false;
1621 size_t max_io_pages, max_cached_pages;
1625 CDEBUG(D_VFSTRACE, "%s: %s ppos: %llu, count: %zu\n",
1626 file_dentry(file)->d_name.name,
1627 iot == CIT_READ ? "read" : "write", *ppos, count);
1629 max_io_pages = PTLRPC_MAX_BRW_PAGES * OBD_MAX_RIF_DEFAULT;
1630 max_cached_pages = sbi->ll_cache->ccc_lru_max;
1631 if (max_io_pages > (max_cached_pages >> 2))
1632 max_io_pages = max_cached_pages >> 2;
1634 io = vvp_env_thread_io(env);
1635 if (file->f_flags & O_DIRECT) {
1636 if (!is_sync_kiocb(args->u.normal.via_iocb))
1639 /* the kernel does not support AIO on pipes, and parallel DIO
1640 * uses part of the AIO path, so we must not do parallel dio
1643 is_parallel_dio = !iov_iter_is_pipe(args->u.normal.via_iter) &&
1646 if (!ll_sbi_has_parallel_dio(sbi))
1647 is_parallel_dio = false;
1649 ci_aio = cl_aio_alloc(args->u.normal.via_iocb,
1650 ll_i2info(inode)->lli_clob);
1652 GOTO(out, rc = -ENOMEM);
1657 * IO block size need be aware of cached page limit, otherwise
1658 * if we have small max_cached_mb but large block IO issued, io
1659 * could not be finished and blocked whole client.
1661 if (file->f_flags & O_DIRECT)
1664 per_bytes = min(max_io_pages << PAGE_SHIFT, count);
1665 partial_io = per_bytes < count;
1666 io = vvp_env_thread_io(env);
1667 ll_io_init(io, file, iot, args);
1668 io->ci_aio = ci_aio;
1669 io->ci_dio_lock = dio_lock;
1670 io->ci_ndelay_tried = retried;
1671 io->ci_parallel_dio = is_parallel_dio;
1673 if (cl_io_rw_init(env, io, iot, *ppos, per_bytes) == 0) {
1674 if (file->f_flags & O_APPEND)
1675 range_lock_init(&range, 0, LUSTRE_EOF);
1677 range_lock_init(&range, *ppos, *ppos + per_bytes - 1);
1679 vio->vui_fd = file->private_data;
1680 vio->vui_iter = args->u.normal.via_iter;
1681 vio->vui_iocb = args->u.normal.via_iocb;
1682 /* Direct IO reads must also take range lock,
1683 * or multiple reads will try to work on the same pages
1684 * See LU-6227 for details.
1686 if (((iot == CIT_WRITE) ||
1687 (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1688 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1689 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1691 rc = range_lock(&lli->lli_write_tree, &range);
1695 range_locked = true;
1698 ll_cl_add(file, env, io, LCC_RW);
1699 rc = cl_io_loop(env, io);
1700 ll_cl_remove(file, env);
1702 if (range_locked && !is_parallel_dio) {
1703 CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1705 range_unlock(&lli->lli_write_tree, &range);
1706 range_locked = false;
1709 /* cl_io_rw_init() handled IO */
1713 /* N/B: parallel DIO may be disabled during i/o submission;
1714 * if that occurs, async RPCs are resolved before we get here, and this
1715 * wait call completes immediately.
1717 if (is_parallel_dio) {
1718 struct cl_sync_io *anchor = &io->ci_aio->cda_sync;
1720 /* for dio, EIOCBQUEUED is an implementation detail,
1721 * and we don't return it to userspace
1723 if (rc == -EIOCBQUEUED)
1726 rc2 = cl_sync_io_wait_recycle(env, anchor, 0, 0);
1731 range_unlock(&lli->lli_write_tree, &range);
1732 range_locked = false;
1737 * In order to move forward AIO, ci_nob was increased,
1738 * but that doesn't mean io have been finished, it just
1739 * means io have been submited, we will always return
1740 * EIOCBQUEUED to the caller, So we could only return
1741 * number of bytes in non-AIO case.
1743 if (io->ci_nob > 0) {
1746 result += io->ci_nob;
1747 *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
1752 count -= io->ci_nob;
1754 /* prepare IO restart */
1756 args->u.normal.via_iter = vio->vui_iter;
1760 * Reexpand iov count because it was zero
1763 iov_iter_reexpand(vio->vui_iter, count);
1764 if (per_bytes == io->ci_nob)
1765 io->ci_need_restart = 1;
1769 cl_io_fini(env, io);
1772 "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1773 file->f_path.dentry->d_name.name,
1774 iot, rc, result, io->ci_need_restart);
1776 if ((rc == 0 || rc == -ENODATA || rc == -ENOLCK) &&
1777 count > 0 && io->ci_need_restart) {
1779 "%s: restart %s from %lld, count: %zu, ret: %zd, rc: %d\n",
1780 file_dentry(file)->d_name.name,
1781 iot == CIT_READ ? "read" : "write",
1782 *ppos, count, result, rc);
1783 /* preserve the tried count for FLR */
1784 retried = io->ci_ndelay_tried;
1785 dio_lock = io->ci_dio_lock;
1791 * VFS will call aio_complete() if no -EIOCBQUEUED
1792 * is returned for AIO, so we can not call aio_complete()
1795 if (rc != -EIOCBQUEUED)
1796 io->ci_aio->cda_no_aio_complete = 1;
1798 * Drop one extra reference so that end_io() could be
1799 * called for this IO context, we could call it after
1800 * we make sure all AIO requests have been proceed.
1802 cl_sync_io_note(env, &io->ci_aio->cda_sync,
1803 rc == -EIOCBQUEUED ? 0 : rc);
1805 cl_aio_free(env, io->ci_aio);
1810 if (iot == CIT_READ) {
1812 ll_stats_ops_tally(ll_i2sbi(inode),
1813 LPROC_LL_READ_BYTES, result);
1814 } else if (iot == CIT_WRITE) {
1816 ll_stats_ops_tally(ll_i2sbi(inode),
1817 LPROC_LL_WRITE_BYTES, result);
1818 fd->fd_write_failed = false;
1819 } else if (result == 0 && rc == 0) {
1822 fd->fd_write_failed = true;
1824 fd->fd_write_failed = false;
1825 } else if (rc != -ERESTARTSYS) {
1826 fd->fd_write_failed = true;
1830 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1832 ll_heat_add(inode, iot, result);
1834 RETURN(result > 0 ? result : rc);
1838 * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1839 * especially for small I/O.
1841 * To serve a read request, CLIO has to create and initialize a cl_io and
1842 * then request DLM lock. This has turned out to have siginificant overhead
1843 * and affects the performance of small I/O dramatically.
1845 * It's not necessary to create a cl_io for each I/O. Under the help of read
1846 * ahead, most of the pages being read are already in memory cache and we can
1847 * read those pages directly because if the pages exist, the corresponding DLM
1848 * lock must exist so that page content must be valid.
1850 * In fast read implementation, the llite speculatively finds and reads pages
1851 * in memory cache. There are three scenarios for fast read:
1852 * - If the page exists and is uptodate, kernel VM will provide the data and
1853 * CLIO won't be intervened;
1854 * - If the page was brought into memory by read ahead, it will be exported
1855 * and read ahead parameters will be updated;
1856 * - Otherwise the page is not in memory, we can't do fast read. Therefore,
1857 * it will go back and invoke normal read, i.e., a cl_io will be created
1858 * and DLM lock will be requested.
1860 * POSIX compliance: posix standard states that read is intended to be atomic.
1861 * Lustre read implementation is in line with Linux kernel read implementation
1862 * and neither of them complies with POSIX standard in this matter. Fast read
1863 * doesn't make the situation worse on single node but it may interleave write
1864 * results from multiple nodes due to short read handling in ll_file_aio_read().
1866 * \param env - lu_env
1867 * \param iocb - kiocb from kernel
1868 * \param iter - user space buffers where the data will be copied
1870 * \retval - number of bytes have been read, or error code if error occurred.
1873 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1877 if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1880 /* NB: we can't do direct IO for fast read because it will need a lock
1881 * to make IO engine happy. */
1882 if (iocb->ki_filp->f_flags & O_DIRECT)
1885 result = generic_file_read_iter(iocb, iter);
1887 /* If the first page is not in cache, generic_file_aio_read() will be
1888 * returned with -ENODATA.
1889 * See corresponding code in ll_readpage(). */
1890 if (result == -ENODATA)
1894 ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
1895 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1896 LPROC_LL_READ_BYTES, result);
1903 * Read from a file (through the page cache).
1905 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1908 struct vvp_io_args *args;
1909 struct file *file = iocb->ki_filp;
1913 ktime_t kstart = ktime_get();
1916 if (!iov_iter_count(to))
1920 * Currently when PCC read failed, we do not fall back to the
1921 * normal read path, just return the error.
1922 * The resaon is that: for RW-PCC, the file data may be modified
1923 * in the PCC and inconsistent with the data on OSTs (or file
1924 * data has been removed from the Lustre file system), at this
1925 * time, fallback to the normal read path may read the wrong
1927 * TODO: for RO-PCC (readonly PCC), fall back to normal read
1928 * path: read data from data copy on OSTs.
1930 result = pcc_file_read_iter(iocb, to, &cached);
1934 ll_ras_enter(file, iocb->ki_pos, iov_iter_count(to));
1936 result = ll_do_fast_read(iocb, to);
1937 if (result < 0 || iov_iter_count(to) == 0)
1940 env = cl_env_get(&refcheck);
1942 return PTR_ERR(env);
1944 args = ll_env_args(env);
1945 args->u.normal.via_iter = to;
1946 args->u.normal.via_iocb = iocb;
1948 rc2 = ll_file_io_generic(env, args, file, CIT_READ,
1949 &iocb->ki_pos, iov_iter_count(to));
1952 else if (result == 0)
1955 cl_env_put(env, &refcheck);
1958 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
1959 file->private_data, iocb->ki_pos, result,
1961 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_READ,
1962 ktime_us_delta(ktime_get(), kstart));
1969 * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
1970 * If a page is already in the page cache and dirty (and some other things -
1971 * See ll_tiny_write_begin for the instantiation of these rules), then we can
1972 * write to it without doing a full I/O, because Lustre already knows about it
1973 * and will write it out. This saves a lot of processing time.
1975 * All writes here are within one page, so exclusion is handled by the page
1976 * lock on the vm page. We do not do tiny writes for writes which touch
1977 * multiple pages because it's very unlikely multiple sequential pages are
1978 * are already dirty.
1980 * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
1981 * and are unlikely to be to already dirty pages.
1983 * Attribute updates are important here, we do them in ll_tiny_write_end.
1985 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
1987 ssize_t count = iov_iter_count(iter);
1988 struct file *file = iocb->ki_filp;
1989 struct inode *inode = file_inode(file);
1990 bool lock_inode = !IS_NOSEC(inode);
1995 /* Restrict writes to single page and < PAGE_SIZE. See comment at top
1996 * of function for why.
1998 if (count >= PAGE_SIZE ||
1999 (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
2002 if (unlikely(lock_inode))
2004 result = __generic_file_write_iter(iocb, iter);
2006 if (unlikely(lock_inode))
2007 inode_unlock(inode);
2009 /* If the page is not already dirty, ll_tiny_write_begin returns
2010 * -ENODATA. We continue on to normal write.
2012 if (result == -ENODATA)
2016 ll_heat_add(inode, CIT_WRITE, result);
2017 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_WRITE_BYTES,
2019 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
2022 CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
2028 * Write to a file (through the page cache).
2030 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2032 struct vvp_io_args *args;
2034 ssize_t rc_tiny = 0, rc_normal;
2035 struct file *file = iocb->ki_filp;
2038 ktime_t kstart = ktime_get();
2043 if (!iov_iter_count(from))
2044 GOTO(out, rc_normal = 0);
2047 * When PCC write failed, we usually do not fall back to the normal
2048 * write path, just return the error. But there is a special case when
2049 * returned error code is -ENOSPC due to running out of space on PCC HSM
2050 * bakcend. At this time, it will fall back to normal I/O path and
2051 * retry the I/O. As the file is in HSM released state, it will restore
2052 * the file data to OSTs first and redo the write again. And the
2053 * restore process will revoke the layout lock and detach the file
2054 * from PCC cache automatically.
2056 result = pcc_file_write_iter(iocb, from, &cached);
2057 if (cached && result != -ENOSPC && result != -EDQUOT)
2058 GOTO(out, rc_normal = result);
2060 /* NB: we can't do direct IO for tiny writes because they use the page
2061 * cache, we can't do sync writes because tiny writes can't flush
2062 * pages, and we can't do append writes because we can't guarantee the
2063 * required DLM locks are held to protect file size.
2065 if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(file))) &&
2066 !(file->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
2067 rc_tiny = ll_do_tiny_write(iocb, from);
2069 /* In case of error, go on and try normal write - Only stop if tiny
2070 * write completed I/O.
2072 if (iov_iter_count(from) == 0)
2073 GOTO(out, rc_normal = rc_tiny);
2075 env = cl_env_get(&refcheck);
2077 return PTR_ERR(env);
2079 args = ll_env_args(env);
2080 args->u.normal.via_iter = from;
2081 args->u.normal.via_iocb = iocb;
2083 rc_normal = ll_file_io_generic(env, args, file, CIT_WRITE,
2084 &iocb->ki_pos, iov_iter_count(from));
2086 /* On success, combine bytes written. */
2087 if (rc_tiny >= 0 && rc_normal > 0)
2088 rc_normal += rc_tiny;
2089 /* On error, only return error from normal write if tiny write did not
2090 * write any bytes. Otherwise return bytes written by tiny write.
2092 else if (rc_tiny > 0)
2093 rc_normal = rc_tiny;
2095 cl_env_put(env, &refcheck);
2097 if (rc_normal > 0) {
2098 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
2099 file->private_data, iocb->ki_pos,
2101 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_WRITE,
2102 ktime_us_delta(ktime_get(), kstart));
2108 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
2110 * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
2112 static int ll_file_get_iov_count(const struct iovec *iov,
2113 unsigned long *nr_segs, size_t *count,
2119 for (seg = 0; seg < *nr_segs; seg++) {
2120 const struct iovec *iv = &iov[seg];
2123 * If any segment has a negative length, or the cumulative
2124 * length ever wraps negative then return -EINVAL.
2127 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
2129 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
2134 cnt -= iv->iov_len; /* This segment is no good */
2141 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2142 unsigned long nr_segs, loff_t pos)
2149 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_READ);
2156 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2157 iov_iter_init(&to, READ, iov, nr_segs, iov_count);
2158 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2159 iov_iter_init(&to, iov, nr_segs, iov_count, 0);
2160 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2162 result = ll_file_read_iter(iocb, &to);
2167 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
2170 struct iovec iov = { .iov_base = buf, .iov_len = count };
2179 init_sync_kiocb(&kiocb, file);
2180 kiocb.ki_pos = *ppos;
2181 #ifdef HAVE_KIOCB_KI_LEFT
2182 kiocb.ki_left = count;
2183 #elif defined(HAVE_KI_NBYTES)
2184 kiocb.i_nbytes = count;
2187 result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
2188 *ppos = kiocb.ki_pos;
2194 * Write to a file (through the page cache).
2197 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2198 unsigned long nr_segs, loff_t pos)
2200 struct iov_iter from;
2205 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_WRITE);
2212 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2213 iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
2214 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2215 iov_iter_init(&from, iov, nr_segs, iov_count, 0);
2216 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2218 result = ll_file_write_iter(iocb, &from);
2223 static ssize_t ll_file_write(struct file *file, const char __user *buf,
2224 size_t count, loff_t *ppos)
2226 struct iovec iov = { .iov_base = (void __user *)buf,
2236 init_sync_kiocb(&kiocb, file);
2237 kiocb.ki_pos = *ppos;
2238 #ifdef HAVE_KIOCB_KI_LEFT
2239 kiocb.ki_left = count;
2240 #elif defined(HAVE_KI_NBYTES)
2241 kiocb.ki_nbytes = count;
2244 result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
2245 *ppos = kiocb.ki_pos;
2249 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
2251 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
2252 __u64 flags, struct lov_user_md *lum, int lum_size)
2254 struct lookup_intent oit = {
2256 .it_flags = flags | MDS_OPEN_BY_FID,
2261 if ((__swab32(lum->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
2262 le32_to_cpu(LOV_MAGIC_MAGIC)) {
2263 /* this code will only exist for big-endian systems */
2264 lustre_swab_lov_user_md(lum, 0);
2267 ll_inode_size_lock(inode);
2268 rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
2270 GOTO(out_unlock, rc);
2272 ll_release_openhandle(dentry, &oit);
2275 ll_inode_size_unlock(inode);
2276 ll_intent_release(&oit);
2281 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
2282 struct lov_mds_md **lmmp, int *lmm_size,
2283 struct ptlrpc_request **request)
2285 struct ll_sb_info *sbi = ll_i2sbi(inode);
2286 struct mdt_body *body;
2287 struct lov_mds_md *lmm = NULL;
2288 struct ptlrpc_request *req = NULL;
2289 struct md_op_data *op_data;
2294 rc = ll_get_default_mdsize(sbi, &lmmsize);
2298 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
2299 strlen(filename), lmmsize,
2300 LUSTRE_OPC_ANY, NULL);
2301 if (IS_ERR(op_data))
2302 RETURN(PTR_ERR(op_data));
2304 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
2305 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
2306 ll_finish_md_op_data(op_data);
2308 CDEBUG(D_INFO, "md_getattr_name failed "
2309 "on %s: rc %d\n", filename, rc);
2313 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2314 LASSERT(body != NULL); /* checked by mdc_getattr_name */
2316 lmmsize = body->mbo_eadatasize;
2318 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
2320 GOTO(out, rc = -ENODATA);
2322 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
2323 LASSERT(lmm != NULL);
2325 if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
2326 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
2327 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1) &&
2328 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_FOREIGN))
2329 GOTO(out, rc = -EPROTO);
2332 * This is coming from the MDS, so is probably in
2333 * little endian. We convert it to host endian before
2334 * passing it to userspace.
2336 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) {
2337 int stripe_count = 0;
2339 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
2340 lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2341 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
2342 if (le32_to_cpu(lmm->lmm_pattern) &
2343 LOV_PATTERN_F_RELEASED)
2345 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
2347 /* if function called for directory - we should
2348 * avoid swab not existent lsm objects
2350 if (lmm->lmm_magic == LOV_MAGIC_V1 &&
2351 S_ISREG(body->mbo_mode))
2352 lustre_swab_lov_user_md_objects(
2353 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
2355 else if (lmm->lmm_magic == LOV_MAGIC_V3 &&
2356 S_ISREG(body->mbo_mode))
2357 lustre_swab_lov_user_md_objects(
2358 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
2360 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1)) {
2361 lustre_swab_lov_comp_md_v1(
2362 (struct lov_comp_md_v1 *)lmm);
2366 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
2367 struct lov_comp_md_v1 *comp_v1 = NULL;
2368 struct lov_comp_md_entry_v1 *ent;
2369 struct lov_user_md_v1 *v1;
2373 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2374 /* Dump the striping information */
2375 for (; i < comp_v1->lcm_entry_count; i++) {
2376 ent = &comp_v1->lcm_entries[i];
2377 off = ent->lcme_offset;
2378 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2380 "comp[%d]: stripe_count=%u, stripe_size=%u\n",
2381 i, v1->lmm_stripe_count, v1->lmm_stripe_size);
2385 * Return valid stripe_count and stripe_size instead of 0 for
2386 * DoM files to avoid divide-by-zero for older userspace that
2387 * calls this ioctl, e.g. lustre ADIO driver.
2389 if (lmm->lmm_stripe_count == 0)
2390 lmm->lmm_stripe_count = 1;
2391 if (lmm->lmm_stripe_size == 0) {
2392 /* Since the first component of the file data is placed
2393 * on the MDT for faster access, the stripe_size of the
2394 * second one is always that applications which are
2397 if (lmm->lmm_pattern == LOV_PATTERN_MDT)
2398 i = comp_v1->lcm_entry_count > 1 ? 1 : 0;
2400 i = comp_v1->lcm_entry_count > 1 ?
2401 comp_v1->lcm_entry_count - 1 : 0;
2402 ent = &comp_v1->lcm_entries[i];
2403 off = ent->lcme_offset;
2404 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2405 lmm->lmm_stripe_size = v1->lmm_stripe_size;
2410 *lmm_size = lmmsize;
2415 static int ll_lov_setea(struct inode *inode, struct file *file,
2418 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
2419 struct lov_user_md *lump;
2420 int lum_size = sizeof(struct lov_user_md) +
2421 sizeof(struct lov_user_ost_data);
2425 if (!capable(CAP_SYS_ADMIN))
2428 OBD_ALLOC_LARGE(lump, lum_size);
2432 if (copy_from_user(lump, arg, lum_size))
2433 GOTO(out_lump, rc = -EFAULT);
2435 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
2437 cl_lov_delay_create_clear(&file->f_flags);
2440 OBD_FREE_LARGE(lump, lum_size);
2444 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
2451 env = cl_env_get(&refcheck);
2453 RETURN(PTR_ERR(env));
2455 rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
2456 cl_env_put(env, &refcheck);
2460 static int ll_lov_setstripe(struct inode *inode, struct file *file,
2463 struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
2464 struct lov_user_md *klum;
2466 __u64 flags = FMODE_WRITE;
2469 rc = ll_copy_user_md(lum, &klum);
2474 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2479 rc = put_user(0, &lum->lmm_stripe_count);
2483 rc = ll_layout_refresh(inode, &gen);
2487 rc = ll_file_getstripe(inode, arg, lum_size);
2488 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode) &&
2489 ll_i2info(inode)->lli_clob) {
2490 struct iattr attr = { 0 };
2492 rc = cl_setattr_ost(ll_i2info(inode)->lli_clob, &attr,
2493 OP_XVALID_FLAGS, LUSTRE_ENCRYPT_FL);
2496 cl_lov_delay_create_clear(&file->f_flags);
2499 OBD_FREE_LARGE(klum, lum_size);
2505 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2507 struct ll_inode_info *lli = ll_i2info(inode);
2508 struct cl_object *obj = lli->lli_clob;
2509 struct ll_file_data *fd = file->private_data;
2510 struct ll_grouplock grouplock;
2515 CWARN("group id for group lock must not be 0\n");
2519 if (ll_file_nolock(file))
2520 RETURN(-EOPNOTSUPP);
2522 if (file->f_flags & O_NONBLOCK) {
2523 if (!mutex_trylock(&lli->lli_group_mutex))
2526 mutex_lock(&lli->lli_group_mutex);
2528 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2529 CWARN("group lock already existed with gid %lu\n",
2530 fd->fd_grouplock.lg_gid);
2531 GOTO(out, rc = -EINVAL);
2533 if (arg != lli->lli_group_gid && lli->lli_group_users != 0) {
2534 if (file->f_flags & O_NONBLOCK)
2535 GOTO(out, rc = -EAGAIN);
2536 mutex_unlock(&lli->lli_group_mutex);
2537 wait_var_event(&lli->lli_group_users, !lli->lli_group_users);
2538 GOTO(retry, rc = 0);
2540 LASSERT(fd->fd_grouplock.lg_lock == NULL);
2543 * XXX: group lock needs to protect all OST objects while PFL
2544 * can add new OST objects during the IO, so we'd instantiate
2545 * all OST objects before getting its group lock.
2550 struct cl_layout cl = {
2551 .cl_is_composite = false,
2553 struct lu_extent ext = {
2555 .e_end = OBD_OBJECT_EOF,
2558 env = cl_env_get(&refcheck);
2560 GOTO(out, rc = PTR_ERR(env));
2562 rc = cl_object_layout_get(env, obj, &cl);
2563 if (rc >= 0 && cl.cl_is_composite)
2564 rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2567 cl_env_put(env, &refcheck);
2572 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2573 arg, (file->f_flags & O_NONBLOCK), &grouplock);
2578 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2579 fd->fd_grouplock = grouplock;
2580 if (lli->lli_group_users == 0)
2581 lli->lli_group_gid = grouplock.lg_gid;
2582 lli->lli_group_users++;
2584 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2586 mutex_unlock(&lli->lli_group_mutex);
2591 static int ll_put_grouplock(struct inode *inode, struct file *file,
2594 struct ll_inode_info *lli = ll_i2info(inode);
2595 struct ll_file_data *fd = file->private_data;
2596 struct ll_grouplock grouplock;
2600 mutex_lock(&lli->lli_group_mutex);
2601 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2602 CWARN("no group lock held\n");
2603 GOTO(out, rc = -EINVAL);
2606 LASSERT(fd->fd_grouplock.lg_lock != NULL);
2608 if (fd->fd_grouplock.lg_gid != arg) {
2609 CWARN("group lock %lu doesn't match current id %lu\n",
2610 arg, fd->fd_grouplock.lg_gid);
2611 GOTO(out, rc = -EINVAL);
2614 grouplock = fd->fd_grouplock;
2615 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2616 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2618 cl_put_grouplock(&grouplock);
2620 lli->lli_group_users--;
2621 if (lli->lli_group_users == 0) {
2622 lli->lli_group_gid = 0;
2623 wake_up_var(&lli->lli_group_users);
2625 CDEBUG(D_INFO, "group lock %lu released\n", arg);
2628 mutex_unlock(&lli->lli_group_mutex);
2634 * Close inode open handle
2636 * \param dentry [in] dentry which contains the inode
2637 * \param it [in,out] intent which contains open info and result
2640 * \retval <0 failure
2642 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2644 struct inode *inode = dentry->d_inode;
2645 struct obd_client_handle *och;
2651 /* Root ? Do nothing. */
2652 if (is_root_inode(inode))
2655 /* No open handle to close? Move away */
2656 if (!it_disposition(it, DISP_OPEN_OPEN))
2659 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2661 OBD_ALLOC(och, sizeof(*och));
2663 GOTO(out, rc = -ENOMEM);
2665 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2669 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2671 /* this one is in place of ll_file_open */
2672 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2673 ptlrpc_req_finished(it->it_request);
2674 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2680 * Get size for inode for which FIEMAP mapping is requested.
2681 * Make the FIEMAP get_info call and returns the result.
2682 * \param fiemap kernel buffer to hold extens
2683 * \param num_bytes kernel buffer size
2685 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2691 struct ll_fiemap_info_key fmkey = { .lfik_name = KEY_FIEMAP, };
2694 /* Checks for fiemap flags */
2695 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2696 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2700 /* Check for FIEMAP_FLAG_SYNC */
2701 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2702 rc = filemap_fdatawrite(inode->i_mapping);
2707 env = cl_env_get(&refcheck);
2709 RETURN(PTR_ERR(env));
2711 if (i_size_read(inode) == 0) {
2712 rc = ll_glimpse_size(inode);
2717 fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2718 obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2719 obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2721 /* If filesize is 0, then there would be no objects for mapping */
2722 if (fmkey.lfik_oa.o_size == 0) {
2723 fiemap->fm_mapped_extents = 0;
2727 fmkey.lfik_fiemap = *fiemap;
2729 rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2730 &fmkey, fiemap, &num_bytes);
2732 cl_env_put(env, &refcheck);
2736 int ll_fid2path(struct inode *inode, void __user *arg)
2738 struct obd_export *exp = ll_i2mdexp(inode);
2739 const struct getinfo_fid2path __user *gfin = arg;
2741 struct getinfo_fid2path *gfout;
2747 if (!capable(CAP_DAC_READ_SEARCH) &&
2748 !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2751 /* Only need to get the buflen */
2752 if (get_user(pathlen, &gfin->gf_pathlen))
2755 if (pathlen > PATH_MAX)
2758 outsize = sizeof(*gfout) + pathlen;
2759 OBD_ALLOC(gfout, outsize);
2763 if (copy_from_user(gfout, arg, sizeof(*gfout)))
2764 GOTO(gf_free, rc = -EFAULT);
2765 /* append root FID after gfout to let MDT know the root FID so that it
2766 * can lookup the correct path, this is mainly for fileset.
2767 * old server without fileset mount support will ignore this. */
2768 *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
2770 /* Call mdc_iocontrol */
2771 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
2775 if (copy_to_user(arg, gfout, outsize))
2779 OBD_FREE(gfout, outsize);
2784 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
2786 struct cl_object *obj = ll_i2info(inode)->lli_clob;
2794 ioc->idv_version = 0;
2795 ioc->idv_layout_version = UINT_MAX;
2797 /* If no file object initialized, we consider its version is 0. */
2801 env = cl_env_get(&refcheck);
2803 RETURN(PTR_ERR(env));
2805 io = vvp_env_thread_io(env);
2807 io->u.ci_data_version.dv_data_version = 0;
2808 io->u.ci_data_version.dv_layout_version = UINT_MAX;
2809 io->u.ci_data_version.dv_flags = ioc->idv_flags;
2812 if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
2813 result = cl_io_loop(env, io);
2815 result = io->ci_result;
2817 ioc->idv_version = io->u.ci_data_version.dv_data_version;
2818 ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
2820 cl_io_fini(env, io);
2822 if (unlikely(io->ci_need_restart))
2825 cl_env_put(env, &refcheck);
2831 * Read the data_version for inode.
2833 * This value is computed using stripe object version on OST.
2834 * Version is computed using server side locking.
2836 * @param flags if do sync on the OST side;
2838 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
2839 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
2841 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
2843 struct ioc_data_version ioc = { .idv_flags = flags };
2846 rc = ll_ioc_data_version(inode, &ioc);
2848 *data_version = ioc.idv_version;
2854 * Trigger a HSM release request for the provided inode.
2856 int ll_hsm_release(struct inode *inode)
2859 struct obd_client_handle *och = NULL;
2860 __u64 data_version = 0;
2865 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2866 ll_i2sbi(inode)->ll_fsname,
2867 PFID(&ll_i2info(inode)->lli_fid));
2869 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2871 GOTO(out, rc = PTR_ERR(och));
2873 /* Grab latest data_version and [am]time values */
2874 rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
2878 env = cl_env_get(&refcheck);
2880 GOTO(out, rc = PTR_ERR(env));
2882 rc = ll_merge_attr(env, inode);
2883 cl_env_put(env, &refcheck);
2885 /* If error happen, we have the wrong size for a file.
2891 /* Release the file.
2892 * NB: lease lock handle is released in mdc_hsm_release_pack() because
2893 * we still need it to pack l_remote_handle to MDT. */
2894 rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
2900 if (och != NULL && !IS_ERR(och)) /* close the file */
2901 ll_lease_close(och, inode, NULL);
2906 struct ll_swap_stack {
2909 struct inode *inode1;
2910 struct inode *inode2;
2915 static int ll_swap_layouts(struct file *file1, struct file *file2,
2916 struct lustre_swap_layouts *lsl)
2918 struct mdc_swap_layouts msl;
2919 struct md_op_data *op_data;
2922 struct ll_swap_stack *llss = NULL;
2925 OBD_ALLOC_PTR(llss);
2929 llss->inode1 = file_inode(file1);
2930 llss->inode2 = file_inode(file2);
2932 rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
2936 /* we use 2 bool because it is easier to swap than 2 bits */
2937 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2938 llss->check_dv1 = true;
2940 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2941 llss->check_dv2 = true;
2943 /* we cannot use lsl->sl_dvX directly because we may swap them */
2944 llss->dv1 = lsl->sl_dv1;
2945 llss->dv2 = lsl->sl_dv2;
2947 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2948 if (rc == 0) /* same file, done! */
2951 if (rc < 0) { /* sequentialize it */
2952 swap(llss->inode1, llss->inode2);
2954 swap(llss->dv1, llss->dv2);
2955 swap(llss->check_dv1, llss->check_dv2);
2959 if (gid != 0) { /* application asks to flush dirty cache */
2960 rc = ll_get_grouplock(llss->inode1, file1, gid);
2964 rc = ll_get_grouplock(llss->inode2, file2, gid);
2966 ll_put_grouplock(llss->inode1, file1, gid);
2971 /* ultimate check, before swaping the layouts we check if
2972 * dataversion has changed (if requested) */
2973 if (llss->check_dv1) {
2974 rc = ll_data_version(llss->inode1, &dv, 0);
2977 if (dv != llss->dv1)
2978 GOTO(putgl, rc = -EAGAIN);
2981 if (llss->check_dv2) {
2982 rc = ll_data_version(llss->inode2, &dv, 0);
2985 if (dv != llss->dv2)
2986 GOTO(putgl, rc = -EAGAIN);
2989 /* struct md_op_data is used to send the swap args to the mdt
2990 * only flags is missing, so we use struct mdc_swap_layouts
2991 * through the md_op_data->op_data */
2992 /* flags from user space have to be converted before they are send to
2993 * server, no flag is sent today, they are only used on the client */
2996 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
2997 0, LUSTRE_OPC_ANY, &msl);
2998 if (IS_ERR(op_data))
2999 GOTO(free, rc = PTR_ERR(op_data));
3001 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
3002 sizeof(*op_data), op_data, NULL);
3003 ll_finish_md_op_data(op_data);
3010 ll_put_grouplock(llss->inode2, file2, gid);
3011 ll_put_grouplock(llss->inode1, file1, gid);
3021 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
3023 struct obd_export *exp = ll_i2mdexp(inode);
3024 struct md_op_data *op_data;
3028 /* Detect out-of range masks */
3029 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
3032 /* Non-root users are forbidden to set or clear flags which are
3033 * NOT defined in HSM_USER_MASK. */
3034 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
3035 !capable(CAP_SYS_ADMIN))
3038 if (!exp_connect_archive_id_array(exp)) {
3039 /* Detect out-of range archive id */
3040 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
3041 (hss->hss_archive_id > LL_HSM_ORIGIN_MAX_ARCHIVE))
3045 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3046 LUSTRE_OPC_ANY, hss);
3047 if (IS_ERR(op_data))
3048 RETURN(PTR_ERR(op_data));
3050 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, exp, sizeof(*op_data),
3053 ll_finish_md_op_data(op_data);
3058 static int ll_hsm_import(struct inode *inode, struct file *file,
3059 struct hsm_user_import *hui)
3061 struct hsm_state_set *hss = NULL;
3062 struct iattr *attr = NULL;
3066 if (!S_ISREG(inode->i_mode))
3072 GOTO(out, rc = -ENOMEM);
3074 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
3075 hss->hss_archive_id = hui->hui_archive_id;
3076 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
3077 rc = ll_hsm_state_set(inode, hss);
3081 OBD_ALLOC_PTR(attr);
3083 GOTO(out, rc = -ENOMEM);
3085 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
3086 attr->ia_mode |= S_IFREG;
3087 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
3088 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
3089 attr->ia_size = hui->hui_size;
3090 attr->ia_mtime.tv_sec = hui->hui_mtime;
3091 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
3092 attr->ia_atime.tv_sec = hui->hui_atime;
3093 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
3095 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
3096 ATTR_UID | ATTR_GID |
3097 ATTR_MTIME | ATTR_MTIME_SET |
3098 ATTR_ATIME | ATTR_ATIME_SET;
3102 rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
3106 inode_unlock(inode);
3118 static inline long ll_lease_type_from_fmode(fmode_t fmode)
3120 return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
3121 ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
3124 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
3126 struct inode *inode = file_inode(file);
3128 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
3129 ATTR_MTIME | ATTR_MTIME_SET |
3132 .tv_sec = lfu->lfu_atime_sec,
3133 .tv_nsec = lfu->lfu_atime_nsec,
3136 .tv_sec = lfu->lfu_mtime_sec,
3137 .tv_nsec = lfu->lfu_mtime_nsec,
3140 .tv_sec = lfu->lfu_ctime_sec,
3141 .tv_nsec = lfu->lfu_ctime_nsec,
3147 if (!capable(CAP_SYS_ADMIN))
3150 if (!S_ISREG(inode->i_mode))
3154 rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
3156 inode_unlock(inode);
3161 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
3164 case MODE_READ_USER:
3166 case MODE_WRITE_USER:
3173 static const char *const user_lockname[] = LOCK_MODE_NAMES;
3175 /* Used to allow the upper layers of the client to request an LDLM lock
3176 * without doing an actual read or write.
3178 * Used for ladvise lockahead to manually request specific locks.
3180 * \param[in] file file this ladvise lock request is on
3181 * \param[in] ladvise ladvise struct describing this lock request
3183 * \retval 0 success, no detailed result available (sync requests
3184 * and requests sent to the server [not handled locally]
3185 * cannot return detailed results)
3186 * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
3187 * see definitions for details.
3188 * \retval negative negative errno on error
3190 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
3192 struct lu_env *env = NULL;
3193 struct cl_io *io = NULL;
3194 struct cl_lock *lock = NULL;
3195 struct cl_lock_descr *descr = NULL;
3196 struct dentry *dentry = file->f_path.dentry;
3197 struct inode *inode = dentry->d_inode;
3198 enum cl_lock_mode cl_mode;
3199 off_t start = ladvise->lla_start;
3200 off_t end = ladvise->lla_end;
3207 "Lock request: file=%pd, inode=%p, mode=%s start=%llu, end=%llu\n",
3208 dentry, dentry->d_inode,
3209 user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
3212 cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
3214 GOTO(out, result = cl_mode);
3216 /* Get IO environment */
3217 result = cl_io_get(inode, &env, &io, &refcheck);
3221 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
3224 * nothing to do for this io. This currently happens when
3225 * stripe sub-object's are not yet created.
3227 result = io->ci_result;
3228 } else if (result == 0) {
3229 lock = vvp_env_lock(env);
3230 descr = &lock->cll_descr;
3232 descr->cld_obj = io->ci_obj;
3233 /* Convert byte offsets to pages */
3234 descr->cld_start = cl_index(io->ci_obj, start);
3235 descr->cld_end = cl_index(io->ci_obj, end);
3236 descr->cld_mode = cl_mode;
3237 /* CEF_MUST is used because we do not want to convert a
3238 * lockahead request to a lockless lock */
3239 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND;
3241 if (ladvise->lla_peradvice_flags & LF_ASYNC)
3242 descr->cld_enq_flags |= CEF_SPECULATIVE;
3244 result = cl_lock_request(env, io, lock);
3246 /* On success, we need to release the lock */
3248 cl_lock_release(env, lock);
3250 cl_io_fini(env, io);
3251 cl_env_put(env, &refcheck);
3253 /* -ECANCELED indicates a matching lock with a different extent
3254 * was already present, and -EEXIST indicates a matching lock
3255 * on exactly the same extent was already present.
3256 * We convert them to positive values for userspace to make
3257 * recognizing true errors easier.
3258 * Note we can only return these detailed results on async requests,
3259 * as sync requests look the same as i/o requests for locking. */
3260 if (result == -ECANCELED)
3261 result = LLA_RESULT_DIFFERENT;
3262 else if (result == -EEXIST)
3263 result = LLA_RESULT_SAME;
3268 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
3270 static int ll_ladvise_sanity(struct inode *inode,
3271 struct llapi_lu_ladvise *ladvise)
3273 struct ll_sb_info *sbi = ll_i2sbi(inode);
3274 enum lu_ladvise_type advice = ladvise->lla_advice;
3275 /* Note the peradvice flags is a 32 bit field, so per advice flags must
3276 * be in the first 32 bits of enum ladvise_flags */
3277 __u32 flags = ladvise->lla_peradvice_flags;
3278 /* 3 lines at 80 characters per line, should be plenty */
3281 if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
3284 "%s: advice with value '%d' not recognized, last supported advice is %s (value '%d'): rc = %d\n",
3285 sbi->ll_fsname, advice,
3286 ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
3290 /* Per-advice checks */
3292 case LU_LADVISE_LOCKNOEXPAND:
3293 if (flags & ~LF_LOCKNOEXPAND_MASK) {
3295 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3296 "rc = %d\n", sbi->ll_fsname, flags,
3297 ladvise_names[advice], rc);
3301 case LU_LADVISE_LOCKAHEAD:
3302 /* Currently only READ and WRITE modes can be requested */
3303 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
3304 ladvise->lla_lockahead_mode == 0) {
3306 CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
3307 "rc = %d\n", sbi->ll_fsname,
3308 ladvise->lla_lockahead_mode,
3309 ladvise_names[advice], rc);
3313 case LU_LADVISE_WILLREAD:
3314 case LU_LADVISE_DONTNEED:
3316 /* Note fall through above - These checks apply to all advices
3317 * except LOCKNOEXPAND */
3318 if (flags & ~LF_DEFAULT_MASK) {
3320 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3321 "rc = %d\n", sbi->ll_fsname, flags,
3322 ladvise_names[advice], rc);
3325 if (ladvise->lla_start >= ladvise->lla_end) {
3327 CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
3328 "for %s: rc = %d\n", sbi->ll_fsname,
3329 ladvise->lla_start, ladvise->lla_end,
3330 ladvise_names[advice], rc);
3342 * Give file access advices
3344 * The ladvise interface is similar to Linux fadvise() system call, except it
3345 * forwards the advices directly from Lustre client to server. The server side
3346 * codes will apply appropriate read-ahead and caching techniques for the
3347 * corresponding files.
3349 * A typical workload for ladvise is e.g. a bunch of different clients are
3350 * doing small random reads of a file, so prefetching pages into OSS cache
3351 * with big linear reads before the random IO is a net benefit. Fetching
3352 * all that data into each client cache with fadvise() may not be, due to
3353 * much more data being sent to the client.
3355 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
3356 struct llapi_lu_ladvise *ladvise)
3360 struct cl_ladvise_io *lio;
3365 env = cl_env_get(&refcheck);
3367 RETURN(PTR_ERR(env));
3369 io = vvp_env_thread_io(env);
3370 io->ci_obj = ll_i2info(inode)->lli_clob;
3372 /* initialize parameters for ladvise */
3373 lio = &io->u.ci_ladvise;
3374 lio->li_start = ladvise->lla_start;
3375 lio->li_end = ladvise->lla_end;
3376 lio->li_fid = ll_inode2fid(inode);
3377 lio->li_advice = ladvise->lla_advice;
3378 lio->li_flags = flags;
3380 if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
3381 rc = cl_io_loop(env, io);
3385 cl_io_fini(env, io);
3386 cl_env_put(env, &refcheck);
3390 static int ll_lock_noexpand(struct file *file, int flags)
3392 struct ll_file_data *fd = file->private_data;
3394 fd->ll_lock_no_expand = !(flags & LF_UNSET);
3399 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
3402 struct fsxattr fsxattr;
3404 if (copy_from_user(&fsxattr,
3405 (const struct fsxattr __user *)arg,
3409 fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
3410 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
3411 fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
3412 fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
3413 if (copy_to_user((struct fsxattr __user *)arg,
3414 &fsxattr, sizeof(fsxattr)))
3420 int ll_ioctl_check_project(struct inode *inode, __u32 xflags,
3424 * Project Quota ID state is only allowed to change from within the init
3425 * namespace. Enforce that restriction only if we are trying to change
3426 * the quota ID state. Everything else is allowed in user namespaces.
3428 if (current_user_ns() == &init_user_ns) {
3430 * Caller is allowed to change the project ID. if it is being
3431 * changed, make sure that the new value is valid.
3433 if (ll_i2info(inode)->lli_projid != projid &&
3434 !projid_valid(make_kprojid(&init_user_ns, projid)))
3440 if (ll_i2info(inode)->lli_projid != projid)
3443 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags)) {
3444 if (!(xflags & FS_XFLAG_PROJINHERIT))
3447 if (xflags & FS_XFLAG_PROJINHERIT)
3454 static int ll_set_project(struct inode *inode, __u32 xflags, __u32 projid)
3456 struct md_op_data *op_data;
3457 struct ptlrpc_request *req = NULL;
3458 struct cl_object *obj;
3459 unsigned int inode_flags;
3462 rc = ll_ioctl_check_project(inode, xflags, projid);
3466 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3467 LUSTRE_OPC_ANY, NULL);
3468 if (IS_ERR(op_data))
3469 RETURN(PTR_ERR(op_data));
3471 inode_flags = ll_xflags_to_inode_flags(xflags);
3472 op_data->op_attr_flags = ll_inode_to_ext_flags(inode_flags);
3473 if (xflags & FS_XFLAG_PROJINHERIT)
3474 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
3475 op_data->op_projid = projid;
3476 op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
3477 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL, 0, &req);
3478 ptlrpc_req_finished(req);
3480 GOTO(out_fsxattr, rc);
3481 ll_update_inode_flags(inode, op_data->op_attr_flags);
3483 /* Avoid OST RPC if this is only ioctl setting project inherit flag */
3484 if (xflags == 0 || xflags == FS_XFLAG_PROJINHERIT)
3485 GOTO(out_fsxattr, rc);
3487 obj = ll_i2info(inode)->lli_clob;
3489 struct iattr attr = { 0 };
3491 rc = cl_setattr_ost(obj, &attr, OP_XVALID_FLAGS, xflags);
3495 ll_finish_md_op_data(op_data);
3499 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
3502 struct fsxattr fsxattr;
3506 if (copy_from_user(&fsxattr,
3507 (const struct fsxattr __user *)arg,
3511 RETURN(ll_set_project(inode, fsxattr.fsx_xflags,
3512 fsxattr.fsx_projid));
3515 int ll_ioctl_project(struct file *file, unsigned int cmd,
3518 struct lu_project lu_project;
3519 struct dentry *dentry = file_dentry(file);
3520 struct inode *inode = file_inode(file);
3521 struct dentry *child_dentry = NULL;
3522 int rc = 0, name_len;
3524 if (copy_from_user(&lu_project,
3525 (const struct lu_project __user *)arg,
3526 sizeof(lu_project)))
3529 /* apply child dentry if name is valid */
3530 name_len = strnlen(lu_project.project_name, NAME_MAX);
3531 if (name_len > 0 && name_len <= NAME_MAX) {
3533 child_dentry = lookup_one_len(lu_project.project_name,
3535 inode_unlock(inode);
3536 if (IS_ERR(child_dentry)) {
3537 rc = PTR_ERR(child_dentry);
3540 inode = child_dentry->d_inode;
3545 } else if (name_len > NAME_MAX) {
3550 switch (lu_project.project_type) {
3551 case LU_PROJECT_SET:
3552 rc = ll_set_project(inode, lu_project.project_xflags,
3553 lu_project.project_id);
3555 case LU_PROJECT_GET:
3556 lu_project.project_xflags =
3557 ll_inode_flags_to_xflags(inode->i_flags);
3558 if (test_bit(LLIF_PROJECT_INHERIT,
3559 &ll_i2info(inode)->lli_flags))
3560 lu_project.project_xflags |= FS_XFLAG_PROJINHERIT;
3561 lu_project.project_id = ll_i2info(inode)->lli_projid;
3562 if (copy_to_user((struct lu_project __user *)arg,
3563 &lu_project, sizeof(lu_project))) {
3573 if (!IS_ERR_OR_NULL(child_dentry))
3578 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
3581 struct inode *inode = file_inode(file);
3582 struct ll_file_data *fd = file->private_data;
3583 struct ll_inode_info *lli = ll_i2info(inode);
3584 struct obd_client_handle *och = NULL;
3585 struct split_param sp;
3586 struct pcc_param param;
3587 bool lease_broken = false;
3589 enum mds_op_bias bias = 0;
3590 struct file *layout_file = NULL;
3592 size_t data_size = 0;
3593 bool attached = false;
3598 mutex_lock(&lli->lli_och_mutex);
3599 if (fd->fd_lease_och != NULL) {
3600 och = fd->fd_lease_och;
3601 fd->fd_lease_och = NULL;
3603 mutex_unlock(&lli->lli_och_mutex);
3608 fmode = och->och_flags;
3610 switch (ioc->lil_flags) {
3611 case LL_LEASE_RESYNC_DONE:
3612 if (ioc->lil_count > IOC_IDS_MAX)
3613 GOTO(out_lease_close, rc = -EINVAL);
3615 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
3616 OBD_ALLOC(data, data_size);
3618 GOTO(out_lease_close, rc = -ENOMEM);
3620 if (copy_from_user(data, (void __user *)arg, data_size))
3621 GOTO(out_lease_close, rc = -EFAULT);
3623 bias = MDS_CLOSE_RESYNC_DONE;
3625 case LL_LEASE_LAYOUT_MERGE: {
3628 if (ioc->lil_count != 1)
3629 GOTO(out_lease_close, rc = -EINVAL);
3631 arg += sizeof(*ioc);
3632 if (copy_from_user(&fd, (void __user *)arg, sizeof(__u32)))
3633 GOTO(out_lease_close, rc = -EFAULT);
3635 layout_file = fget(fd);
3637 GOTO(out_lease_close, rc = -EBADF);
3639 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
3640 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
3641 GOTO(out_lease_close, rc = -EPERM);
3643 data = file_inode(layout_file);
3644 bias = MDS_CLOSE_LAYOUT_MERGE;
3647 case LL_LEASE_LAYOUT_SPLIT: {
3651 if (ioc->lil_count != 2)
3652 GOTO(out_lease_close, rc = -EINVAL);
3654 arg += sizeof(*ioc);
3655 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3656 GOTO(out_lease_close, rc = -EFAULT);
3658 arg += sizeof(__u32);
3659 if (copy_from_user(&mirror_id, (void __user *)arg,
3661 GOTO(out_lease_close, rc = -EFAULT);
3663 layout_file = fget(fdv);
3665 GOTO(out_lease_close, rc = -EBADF);
3667 /* if layout_file == file, it means to destroy the mirror */
3668 sp.sp_inode = file_inode(layout_file);
3669 sp.sp_mirror_id = (__u16)mirror_id;
3671 bias = MDS_CLOSE_LAYOUT_SPLIT;
3674 case LL_LEASE_PCC_ATTACH:
3675 if (ioc->lil_count != 1)
3678 arg += sizeof(*ioc);
3679 if (copy_from_user(¶m.pa_archive_id, (void __user *)arg,
3681 GOTO(out_lease_close, rc2 = -EFAULT);
3683 rc2 = pcc_readwrite_attach(file, inode, param.pa_archive_id);
3685 GOTO(out_lease_close, rc2);
3688 /* Grab latest data version */
3689 rc2 = ll_data_version(inode, ¶m.pa_data_version,
3692 GOTO(out_lease_close, rc2);
3695 bias = MDS_PCC_ATTACH;
3698 /* without close intent */
3703 rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
3707 rc = ll_lease_och_release(inode, file);
3716 switch (ioc->lil_flags) {
3717 case LL_LEASE_RESYNC_DONE:
3719 OBD_FREE(data, data_size);
3721 case LL_LEASE_LAYOUT_MERGE:
3722 case LL_LEASE_LAYOUT_SPLIT:
3726 ll_layout_refresh(inode, &fd->fd_layout_version);
3728 case LL_LEASE_PCC_ATTACH:
3731 rc = pcc_readwrite_attach_fini(file, inode,
3732 param.pa_layout_gen,
3739 rc = ll_lease_type_from_fmode(fmode);
3743 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
3746 struct inode *inode = file_inode(file);
3747 struct ll_inode_info *lli = ll_i2info(inode);
3748 struct ll_file_data *fd = file->private_data;
3749 struct obd_client_handle *och = NULL;
3750 __u64 open_flags = 0;
3756 switch (ioc->lil_mode) {
3757 case LL_LEASE_WRLCK:
3758 if (!(file->f_mode & FMODE_WRITE))
3760 fmode = FMODE_WRITE;
3762 case LL_LEASE_RDLCK:
3763 if (!(file->f_mode & FMODE_READ))
3767 case LL_LEASE_UNLCK:
3768 RETURN(ll_file_unlock_lease(file, ioc, arg));
3773 CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
3775 /* apply for lease */
3776 if (ioc->lil_flags & LL_LEASE_RESYNC)
3777 open_flags = MDS_OPEN_RESYNC;
3778 och = ll_lease_open(inode, file, fmode, open_flags);
3780 RETURN(PTR_ERR(och));
3782 if (ioc->lil_flags & LL_LEASE_RESYNC) {
3783 rc = ll_lease_file_resync(och, inode, arg);
3785 ll_lease_close(och, inode, NULL);
3788 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
3790 ll_lease_close(och, inode, NULL);
3796 mutex_lock(&lli->lli_och_mutex);
3797 if (fd->fd_lease_och == NULL) {
3798 fd->fd_lease_och = och;
3801 mutex_unlock(&lli->lli_och_mutex);
3803 /* impossible now that only excl is supported for now */
3804 ll_lease_close(och, inode, &lease_broken);
3810 static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
3812 struct ll_inode_info *lli = ll_i2info(inode);
3813 struct ll_sb_info *sbi = ll_i2sbi(inode);
3814 __u64 now = ktime_get_real_seconds();
3817 spin_lock(&lli->lli_heat_lock);
3818 heat->lh_flags = lli->lli_heat_flags;
3819 for (i = 0; i < heat->lh_count; i++)
3820 heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
3821 now, sbi->ll_heat_decay_weight,
3822 sbi->ll_heat_period_second);
3823 spin_unlock(&lli->lli_heat_lock);
3826 static int ll_heat_set(struct inode *inode, enum lu_heat_flag flags)
3828 struct ll_inode_info *lli = ll_i2info(inode);
3831 spin_lock(&lli->lli_heat_lock);
3832 if (flags & LU_HEAT_FLAG_CLEAR)
3833 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
3835 if (flags & LU_HEAT_FLAG_OFF)
3836 lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
3838 lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
3840 spin_unlock(&lli->lli_heat_lock);
3846 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3848 struct inode *inode = file_inode(file);
3849 struct ll_file_data *fd = file->private_data;
3853 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
3854 PFID(ll_inode2fid(inode)), inode, cmd);
3855 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
3857 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
3858 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
3862 case LL_IOC_GETFLAGS:
3863 /* Get the current value of the file flags */
3864 return put_user(fd->fd_flags, (int __user *)arg);
3865 case LL_IOC_SETFLAGS:
3866 case LL_IOC_CLRFLAGS:
3867 /* Set or clear specific file flags */
3868 /* XXX This probably needs checks to ensure the flags are
3869 * not abused, and to handle any flag side effects.
3871 if (get_user(flags, (int __user *) arg))
3874 if (cmd == LL_IOC_SETFLAGS) {
3875 if ((flags & LL_FILE_IGNORE_LOCK) &&
3876 !(file->f_flags & O_DIRECT)) {
3877 CERROR("%s: unable to disable locking on "
3878 "non-O_DIRECT file\n", current->comm);
3882 fd->fd_flags |= flags;
3884 fd->fd_flags &= ~flags;
3887 case LL_IOC_LOV_SETSTRIPE:
3888 case LL_IOC_LOV_SETSTRIPE_NEW:
3889 RETURN(ll_lov_setstripe(inode, file, (void __user *)arg));
3890 case LL_IOC_LOV_SETEA:
3891 RETURN(ll_lov_setea(inode, file, (void __user *)arg));
3892 case LL_IOC_LOV_SWAP_LAYOUTS: {
3894 struct lustre_swap_layouts lsl;
3896 if (copy_from_user(&lsl, (char __user *)arg,
3897 sizeof(struct lustre_swap_layouts)))
3900 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
3903 file2 = fget(lsl.sl_fd);
3907 /* O_WRONLY or O_RDWR */
3908 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
3909 GOTO(out, rc = -EPERM);
3911 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
3912 struct inode *inode2;
3913 struct ll_inode_info *lli;
3914 struct obd_client_handle *och = NULL;
3916 lli = ll_i2info(inode);
3917 mutex_lock(&lli->lli_och_mutex);
3918 if (fd->fd_lease_och != NULL) {
3919 och = fd->fd_lease_och;
3920 fd->fd_lease_och = NULL;
3922 mutex_unlock(&lli->lli_och_mutex);
3924 GOTO(out, rc = -ENOLCK);
3925 inode2 = file_inode(file2);
3926 rc = ll_swap_layouts_close(och, inode, inode2);
3928 rc = ll_swap_layouts(file, file2, &lsl);
3934 case LL_IOC_LOV_GETSTRIPE:
3935 case LL_IOC_LOV_GETSTRIPE_NEW:
3936 RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
3937 case FS_IOC_GETFLAGS:
3938 case FS_IOC_SETFLAGS:
3939 RETURN(ll_iocontrol(inode, file, cmd, arg));
3940 case FSFILT_IOC_GETVERSION:
3941 case FS_IOC_GETVERSION:
3942 RETURN(put_user(inode->i_generation, (int __user *)arg));
3943 /* We need to special case any other ioctls we want to handle,
3944 * to send them to the MDS/OST as appropriate and to properly
3945 * network encode the arg field. */
3946 case FS_IOC_SETVERSION:
3949 case LL_IOC_GROUP_LOCK:
3950 RETURN(ll_get_grouplock(inode, file, arg));
3951 case LL_IOC_GROUP_UNLOCK:
3952 RETURN(ll_put_grouplock(inode, file, arg));
3953 case IOC_OBD_STATFS:
3954 RETURN(ll_obd_statfs(inode, (void __user *)arg));
3956 case LL_IOC_FLUSHCTX:
3957 RETURN(ll_flush_ctx(inode));
3958 case LL_IOC_PATH2FID: {
3959 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
3960 sizeof(struct lu_fid)))
3965 case LL_IOC_GETPARENT:
3966 RETURN(ll_getparent(file, (struct getparent __user *)arg));
3968 case OBD_IOC_FID2PATH:
3969 RETURN(ll_fid2path(inode, (void __user *)arg));
3970 case LL_IOC_DATA_VERSION: {
3971 struct ioc_data_version idv;
3974 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
3977 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
3978 rc = ll_ioc_data_version(inode, &idv);
3981 copy_to_user((char __user *)arg, &idv, sizeof(idv)))
3987 case LL_IOC_GET_MDTIDX: {
3990 mdtidx = ll_get_mdt_idx(inode);
3994 if (put_user((int)mdtidx, (int __user *)arg))
3999 case OBD_IOC_GETNAME_OLD:
4001 case OBD_IOC_GETDTNAME:
4003 case OBD_IOC_GETMDNAME:
4004 RETURN(ll_get_obd_name(inode, cmd, arg));
4005 case LL_IOC_HSM_STATE_GET: {
4006 struct md_op_data *op_data;
4007 struct hsm_user_state *hus;
4014 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4015 LUSTRE_OPC_ANY, hus);
4016 if (IS_ERR(op_data)) {
4018 RETURN(PTR_ERR(op_data));
4021 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4024 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
4027 ll_finish_md_op_data(op_data);
4031 case LL_IOC_HSM_STATE_SET: {
4032 struct hsm_state_set *hss;
4039 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
4044 rc = ll_hsm_state_set(inode, hss);
4049 case LL_IOC_HSM_ACTION: {
4050 struct md_op_data *op_data;
4051 struct hsm_current_action *hca;
4058 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4059 LUSTRE_OPC_ANY, hca);
4060 if (IS_ERR(op_data)) {
4062 RETURN(PTR_ERR(op_data));
4065 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4068 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
4071 ll_finish_md_op_data(op_data);
4075 case LL_IOC_SET_LEASE_OLD: {
4076 struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
4078 RETURN(ll_file_set_lease(file, &ioc, 0));
4080 case LL_IOC_SET_LEASE: {
4081 struct ll_ioc_lease ioc;
4083 if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
4086 RETURN(ll_file_set_lease(file, &ioc, arg));
4088 case LL_IOC_GET_LEASE: {
4089 struct ll_inode_info *lli = ll_i2info(inode);
4090 struct ldlm_lock *lock = NULL;
4093 mutex_lock(&lli->lli_och_mutex);
4094 if (fd->fd_lease_och != NULL) {
4095 struct obd_client_handle *och = fd->fd_lease_och;
4097 lock = ldlm_handle2lock(&och->och_lease_handle);
4099 lock_res_and_lock(lock);
4100 if (!ldlm_is_cancel(lock))
4101 fmode = och->och_flags;
4103 unlock_res_and_lock(lock);
4104 LDLM_LOCK_PUT(lock);
4107 mutex_unlock(&lli->lli_och_mutex);
4109 RETURN(ll_lease_type_from_fmode(fmode));
4111 case LL_IOC_HSM_IMPORT: {
4112 struct hsm_user_import *hui;
4118 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
4123 rc = ll_hsm_import(inode, file, hui);
4128 case LL_IOC_FUTIMES_3: {
4129 struct ll_futimes_3 lfu;
4131 if (copy_from_user(&lfu,
4132 (const struct ll_futimes_3 __user *)arg,
4136 RETURN(ll_file_futimes_3(file, &lfu));
4138 case LL_IOC_LADVISE: {
4139 struct llapi_ladvise_hdr *k_ladvise_hdr;
4140 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
4143 int alloc_size = sizeof(*k_ladvise_hdr);
4146 u_ladvise_hdr = (void __user *)arg;
4147 OBD_ALLOC_PTR(k_ladvise_hdr);
4148 if (k_ladvise_hdr == NULL)
4151 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4152 GOTO(out_ladvise, rc = -EFAULT);
4154 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
4155 k_ladvise_hdr->lah_count < 1)
4156 GOTO(out_ladvise, rc = -EINVAL);
4158 num_advise = k_ladvise_hdr->lah_count;
4159 if (num_advise >= LAH_COUNT_MAX)
4160 GOTO(out_ladvise, rc = -EFBIG);
4162 OBD_FREE_PTR(k_ladvise_hdr);
4163 alloc_size = offsetof(typeof(*k_ladvise_hdr),
4164 lah_advise[num_advise]);
4165 OBD_ALLOC(k_ladvise_hdr, alloc_size);
4166 if (k_ladvise_hdr == NULL)
4170 * TODO: submit multiple advices to one server in a single RPC
4172 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4173 GOTO(out_ladvise, rc = -EFAULT);
4175 for (i = 0; i < num_advise; i++) {
4176 struct llapi_lu_ladvise *k_ladvise =
4177 &k_ladvise_hdr->lah_advise[i];
4178 struct llapi_lu_ladvise __user *u_ladvise =
4179 &u_ladvise_hdr->lah_advise[i];
4181 rc = ll_ladvise_sanity(inode, k_ladvise);
4183 GOTO(out_ladvise, rc);
4185 switch (k_ladvise->lla_advice) {
4186 case LU_LADVISE_LOCKNOEXPAND:
4187 rc = ll_lock_noexpand(file,
4188 k_ladvise->lla_peradvice_flags);
4189 GOTO(out_ladvise, rc);
4190 case LU_LADVISE_LOCKAHEAD:
4192 rc = ll_file_lock_ahead(file, k_ladvise);
4195 GOTO(out_ladvise, rc);
4198 &u_ladvise->lla_lockahead_result))
4199 GOTO(out_ladvise, rc = -EFAULT);
4202 rc = ll_ladvise(inode, file,
4203 k_ladvise_hdr->lah_flags,
4206 GOTO(out_ladvise, rc);
4213 OBD_FREE(k_ladvise_hdr, alloc_size);
4216 case LL_IOC_FLR_SET_MIRROR: {
4217 /* mirror I/O must be direct to avoid polluting page cache
4219 if (!(file->f_flags & O_DIRECT))
4222 fd->fd_designated_mirror = (__u32)arg;
4225 case FS_IOC_FSGETXATTR:
4226 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
4227 case FS_IOC_FSSETXATTR:
4228 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
4229 case LL_IOC_PROJECT:
4230 RETURN(ll_ioctl_project(file, cmd, arg));
4232 RETURN(put_user(PAGE_SIZE, (int __user *)arg));
4233 case LL_IOC_HEAT_GET: {
4234 struct lu_heat uheat;
4235 struct lu_heat *heat;
4238 if (copy_from_user(&uheat, (void __user *)arg, sizeof(uheat)))
4241 if (uheat.lh_count > OBD_HEAT_COUNT)
4242 uheat.lh_count = OBD_HEAT_COUNT;
4244 size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
4245 OBD_ALLOC(heat, size);
4249 heat->lh_count = uheat.lh_count;
4250 ll_heat_get(inode, heat);
4251 rc = copy_to_user((char __user *)arg, heat, size);
4252 OBD_FREE(heat, size);
4253 RETURN(rc ? -EFAULT : 0);
4255 case LL_IOC_HEAT_SET: {
4258 if (copy_from_user(&flags, (void __user *)arg, sizeof(flags)))
4261 rc = ll_heat_set(inode, flags);
4264 case LL_IOC_PCC_DETACH: {
4265 struct lu_pcc_detach *detach;
4267 OBD_ALLOC_PTR(detach);
4271 if (copy_from_user(detach,
4272 (const struct lu_pcc_detach __user *)arg,
4274 GOTO(out_detach_free, rc = -EFAULT);
4276 if (!S_ISREG(inode->i_mode))
4277 GOTO(out_detach_free, rc = -EINVAL);
4279 if (!inode_owner_or_capable(inode))
4280 GOTO(out_detach_free, rc = -EPERM);
4282 rc = pcc_ioctl_detach(inode, detach->pccd_opt);
4284 OBD_FREE_PTR(detach);
4287 case LL_IOC_PCC_STATE: {
4288 struct lu_pcc_state __user *ustate =
4289 (struct lu_pcc_state __user *)arg;
4290 struct lu_pcc_state *state;
4292 OBD_ALLOC_PTR(state);
4296 if (copy_from_user(state, ustate, sizeof(*state)))
4297 GOTO(out_state, rc = -EFAULT);
4299 rc = pcc_ioctl_state(file, inode, state);
4301 GOTO(out_state, rc);
4303 if (copy_to_user(ustate, state, sizeof(*state)))
4304 GOTO(out_state, rc = -EFAULT);
4307 OBD_FREE_PTR(state);
4310 #ifdef HAVE_LUSTRE_CRYPTO
4311 case LL_IOC_SET_ENCRYPTION_POLICY:
4312 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4314 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
4315 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
4316 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4318 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
4319 case LL_IOC_ADD_ENCRYPTION_KEY:
4320 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4322 return llcrypt_ioctl_add_key(file, (void __user *)arg);
4323 case LL_IOC_REMOVE_ENCRYPTION_KEY:
4324 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4326 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
4327 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4328 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4330 return llcrypt_ioctl_remove_key_all_users(file,
4331 (void __user *)arg);
4332 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
4333 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4335 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
4338 case LL_IOC_UNLOCK_FOREIGN: {
4339 struct dentry *dentry = file_dentry(file);
4341 /* if not a foreign symlink do nothing */
4342 if (ll_foreign_is_removable(dentry, true)) {
4344 "prevent unlink of non-foreign file ("DFID")\n",
4345 PFID(ll_inode2fid(inode)));
4346 RETURN(-EOPNOTSUPP);
4352 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
4353 (void __user *)arg));
4357 loff_t ll_lseek(struct file *file, loff_t offset, int whence)
4359 struct inode *inode = file_inode(file);
4362 struct cl_lseek_io *lsio;
4369 env = cl_env_get(&refcheck);
4371 RETURN(PTR_ERR(env));
4373 io = vvp_env_thread_io(env);
4374 io->ci_obj = ll_i2info(inode)->lli_clob;
4375 ll_io_set_mirror(io, file);
4377 lsio = &io->u.ci_lseek;
4378 lsio->ls_start = offset;
4379 lsio->ls_whence = whence;
4380 lsio->ls_result = -ENXIO;
4383 rc = cl_io_init(env, io, CIT_LSEEK, io->ci_obj);
4385 struct vvp_io *vio = vvp_env_io(env);
4387 vio->vui_fd = file->private_data;
4388 rc = cl_io_loop(env, io);
4392 retval = rc ? : lsio->ls_result;
4393 cl_io_fini(env, io);
4394 } while (unlikely(io->ci_need_restart));
4396 cl_env_put(env, &refcheck);
4401 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
4403 struct inode *inode = file_inode(file);
4404 loff_t retval = offset, eof = 0;
4405 ktime_t kstart = ktime_get();
4409 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
4410 PFID(ll_inode2fid(inode)), inode, retval, retval,
4413 if (origin == SEEK_END) {
4414 retval = ll_glimpse_size(inode);
4417 eof = i_size_read(inode);
4420 if (origin == SEEK_HOLE || origin == SEEK_DATA) {
4424 /* flush local cache first if any */
4425 cl_sync_file_range(inode, offset, OBD_OBJECT_EOF,
4428 retval = ll_lseek(file, offset, origin);
4431 retval = vfs_setpos(file, retval, ll_file_maxbytes(inode));
4433 retval = generic_file_llseek_size(file, offset, origin,
4434 ll_file_maxbytes(inode), eof);
4437 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK,
4438 ktime_us_delta(ktime_get(), kstart));
4442 static int ll_flush(struct file *file, fl_owner_t id)
4444 struct inode *inode = file_inode(file);
4445 struct ll_inode_info *lli = ll_i2info(inode);
4446 struct ll_file_data *fd = file->private_data;
4449 LASSERT(!S_ISDIR(inode->i_mode));
4451 /* catch async errors that were recorded back when async writeback
4452 * failed for pages in this mapping. */
4453 rc = lli->lli_async_rc;
4454 lli->lli_async_rc = 0;
4455 if (lli->lli_clob != NULL) {
4456 err = lov_read_and_clear_async_rc(lli->lli_clob);
4461 /* The application has been told write failure already.
4462 * Do not report failure again. */
4463 if (fd->fd_write_failed)
4465 return rc ? -EIO : 0;
4469 * Called to make sure a portion of file has been written out.
4470 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
4472 * Return how many pages have been written.
4474 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
4475 enum cl_fsync_mode mode, int ignore_layout)
4479 struct cl_fsync_io *fio;
4484 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
4485 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
4488 env = cl_env_get(&refcheck);
4490 RETURN(PTR_ERR(env));
4492 io = vvp_env_thread_io(env);
4493 io->ci_obj = ll_i2info(inode)->lli_clob;
4494 io->ci_ignore_layout = ignore_layout;
4496 /* initialize parameters for sync */
4497 fio = &io->u.ci_fsync;
4498 fio->fi_start = start;
4500 fio->fi_fid = ll_inode2fid(inode);
4501 fio->fi_mode = mode;
4502 fio->fi_nr_written = 0;
4504 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
4505 result = cl_io_loop(env, io);
4507 result = io->ci_result;
4509 result = fio->fi_nr_written;
4510 cl_io_fini(env, io);
4511 cl_env_put(env, &refcheck);
4517 * When dentry is provided (the 'else' case), file_dentry() may be
4518 * null and dentry must be used directly rather than pulled from
4519 * file_dentry() as is done otherwise.
4522 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
4524 struct dentry *dentry = file_dentry(file);
4525 struct inode *inode = dentry->d_inode;
4526 struct ll_inode_info *lli = ll_i2info(inode);
4527 struct ptlrpc_request *req;
4528 ktime_t kstart = ktime_get();
4534 "VFS Op:inode="DFID"(%p), start %lld, end %lld, datasync %d\n",
4535 PFID(ll_inode2fid(inode)), inode, start, end, datasync);
4537 /* fsync's caller has already called _fdata{sync,write}, we want
4538 * that IO to finish before calling the osc and mdc sync methods */
4539 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
4541 /* catch async errors that were recorded back when async writeback
4542 * failed for pages in this mapping. */
4543 if (!S_ISDIR(inode->i_mode)) {
4544 err = lli->lli_async_rc;
4545 lli->lli_async_rc = 0;
4548 if (lli->lli_clob != NULL) {
4549 err = lov_read_and_clear_async_rc(lli->lli_clob);
4555 err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
4559 ptlrpc_req_finished(req);
4561 if (S_ISREG(inode->i_mode)) {
4562 struct ll_file_data *fd = file->private_data;
4565 /* Sync metadata on MDT first, and then sync the cached data
4568 err = pcc_fsync(file, start, end, datasync, &cached);
4570 err = cl_sync_file_range(inode, start, end,
4572 if (rc == 0 && err < 0)
4575 fd->fd_write_failed = true;
4577 fd->fd_write_failed = false;
4581 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC,
4582 ktime_us_delta(ktime_get(), kstart));
4587 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
4589 struct inode *inode = file_inode(file);
4590 struct ll_sb_info *sbi = ll_i2sbi(inode);
4591 struct ldlm_enqueue_info einfo = {
4592 .ei_type = LDLM_FLOCK,
4593 .ei_cb_cp = ldlm_flock_completion_ast,
4594 .ei_cbdata = file_lock,
4596 struct md_op_data *op_data;
4597 struct lustre_handle lockh = { 0 };
4598 union ldlm_policy_data flock = { { 0 } };
4599 int fl_type = file_lock->fl_type;
4600 ktime_t kstart = ktime_get();
4606 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
4607 PFID(ll_inode2fid(inode)), file_lock);
4609 if (file_lock->fl_flags & FL_FLOCK) {
4610 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
4611 /* flocks are whole-file locks */
4612 flock.l_flock.end = OFFSET_MAX;
4613 /* For flocks owner is determined by the local file desctiptor*/
4614 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
4615 } else if (file_lock->fl_flags & FL_POSIX) {
4616 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
4617 flock.l_flock.start = file_lock->fl_start;
4618 flock.l_flock.end = file_lock->fl_end;
4622 flock.l_flock.pid = file_lock->fl_pid;
4624 #if defined(HAVE_LM_COMPARE_OWNER) || defined(lm_compare_owner)
4625 /* Somewhat ugly workaround for svc lockd.
4626 * lockd installs custom fl_lmops->lm_compare_owner that checks
4627 * for the fl_owner to be the same (which it always is on local node
4628 * I guess between lockd processes) and then compares pid.
4629 * As such we assign pid to the owner field to make it all work,
4630 * conflict with normal locks is unlikely since pid space and
4631 * pointer space for current->files are not intersecting */
4632 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
4633 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
4638 einfo.ei_mode = LCK_PR;
4641 /* An unlock request may or may not have any relation to
4642 * existing locks so we may not be able to pass a lock handle
4643 * via a normal ldlm_lock_cancel() request. The request may even
4644 * unlock a byte range in the middle of an existing lock. In
4645 * order to process an unlock request we need all of the same
4646 * information that is given with a normal read or write record
4647 * lock request. To avoid creating another ldlm unlock (cancel)
4648 * message we'll treat a LCK_NL flock request as an unlock. */
4649 einfo.ei_mode = LCK_NL;
4652 einfo.ei_mode = LCK_PW;
4655 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
4670 flags = LDLM_FL_BLOCK_NOWAIT;
4676 flags = LDLM_FL_TEST_LOCK;
4679 CERROR("unknown fcntl lock command: %d\n", cmd);
4683 /* Save the old mode so that if the mode in the lock changes we
4684 * can decrement the appropriate reader or writer refcount. */
4685 file_lock->fl_type = einfo.ei_mode;
4687 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4688 LUSTRE_OPC_ANY, NULL);
4689 if (IS_ERR(op_data))
4690 RETURN(PTR_ERR(op_data));
4692 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
4693 "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
4694 flock.l_flock.pid, flags, einfo.ei_mode,
4695 flock.l_flock.start, flock.l_flock.end);
4697 rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
4700 /* Restore the file lock type if not TEST lock. */
4701 if (!(flags & LDLM_FL_TEST_LOCK))
4702 file_lock->fl_type = fl_type;
4704 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
4705 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
4706 !(flags & LDLM_FL_TEST_LOCK))
4707 rc2 = locks_lock_file_wait(file, file_lock);
4709 if ((file_lock->fl_flags & FL_FLOCK) &&
4710 (rc == 0 || file_lock->fl_type == F_UNLCK))
4711 rc2 = flock_lock_file_wait(file, file_lock);
4712 if ((file_lock->fl_flags & FL_POSIX) &&
4713 (rc == 0 || file_lock->fl_type == F_UNLCK) &&
4714 !(flags & LDLM_FL_TEST_LOCK))
4715 rc2 = posix_lock_file_wait(file, file_lock);
4716 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
4718 if (rc2 && file_lock->fl_type != F_UNLCK) {
4719 einfo.ei_mode = LCK_NL;
4720 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
4725 ll_finish_md_op_data(op_data);
4728 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK,
4729 ktime_us_delta(ktime_get(), kstart));
4733 int ll_get_fid_by_name(struct inode *parent, const char *name,
4734 int namelen, struct lu_fid *fid,
4735 struct inode **inode)
4737 struct md_op_data *op_data = NULL;
4738 struct mdt_body *body;
4739 struct ptlrpc_request *req;
4743 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
4744 LUSTRE_OPC_ANY, NULL);
4745 if (IS_ERR(op_data))
4746 RETURN(PTR_ERR(op_data));
4748 op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
4749 rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
4750 ll_finish_md_op_data(op_data);
4754 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4756 GOTO(out_req, rc = -EFAULT);
4758 *fid = body->mbo_fid1;
4761 rc = ll_prep_inode(inode, &req->rq_pill, parent->i_sb, NULL);
4763 ptlrpc_req_finished(req);
4767 int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
4770 struct dentry *dchild = NULL;
4771 struct inode *child_inode = NULL;
4772 struct md_op_data *op_data;
4773 struct ptlrpc_request *request = NULL;
4774 struct obd_client_handle *och = NULL;
4776 struct mdt_body *body;
4777 __u64 data_version = 0;
4778 size_t namelen = strlen(name);
4779 int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
4783 CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
4784 PFID(ll_inode2fid(parent)), name,
4785 lum->lum_stripe_offset, lum->lum_stripe_count);
4787 if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
4788 lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
4789 lustre_swab_lmv_user_md(lum);
4791 /* Get child FID first */
4792 qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
4795 dchild = d_lookup(file_dentry(file), &qstr);
4797 if (dchild->d_inode)
4798 child_inode = igrab(dchild->d_inode);
4803 rc = ll_get_fid_by_name(parent, name, namelen, NULL,
4812 if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
4813 OBD_CONNECT2_DIR_MIGRATE)) {
4814 if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
4815 ll_dir_striped(child_inode)) {
4816 CERROR("%s: MDT doesn't support stripe directory "
4817 "migration!\n", ll_i2sbi(parent)->ll_fsname);
4818 GOTO(out_iput, rc = -EOPNOTSUPP);
4823 * lfs migrate command needs to be blocked on the client
4824 * by checking the migrate FID against the FID of the
4827 if (is_root_inode(child_inode))
4828 GOTO(out_iput, rc = -EINVAL);
4830 if (IS_ENCRYPTED(child_inode)) {
4831 rc = llcrypt_get_encryption_info(child_inode);
4834 if (!llcrypt_has_encryption_key(child_inode)) {
4835 CDEBUG(D_SEC, "no enc key for "DFID"\n",
4836 PFID(ll_inode2fid(child_inode)));
4837 GOTO(out_iput, rc = -ENOKEY);
4839 if (unlikely(!llcrypt_policy_has_filename_enc(child_inode))) {
4841 "cannot migrate old format encrypted "DFID", please move to new enc dir first\n",
4842 PFID(ll_inode2fid(child_inode)));
4843 GOTO(out_iput, rc = -EUCLEAN);
4847 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
4848 child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
4849 if (IS_ERR(op_data))
4850 GOTO(out_iput, rc = PTR_ERR(op_data));
4852 inode_lock(child_inode);
4853 op_data->op_fid3 = *ll_inode2fid(child_inode);
4854 if (!fid_is_sane(&op_data->op_fid3)) {
4855 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
4856 ll_i2sbi(parent)->ll_fsname, name,
4857 PFID(&op_data->op_fid3));
4858 GOTO(out_unlock, rc = -EINVAL);
4861 op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
4862 op_data->op_data = lum;
4863 op_data->op_data_size = lumlen;
4866 if (S_ISREG(child_inode->i_mode)) {
4867 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
4871 GOTO(out_unlock, rc);
4874 rc = ll_data_version(child_inode, &data_version,
4877 GOTO(out_close, rc);
4879 op_data->op_open_handle = och->och_open_handle;
4880 op_data->op_data_version = data_version;
4881 op_data->op_lease_handle = och->och_lease_handle;
4882 op_data->op_bias |= MDS_CLOSE_MIGRATE;
4884 spin_lock(&och->och_mod->mod_open_req->rq_lock);
4885 och->och_mod->mod_open_req->rq_replay = 0;
4886 spin_unlock(&och->och_mod->mod_open_req->rq_lock);
4889 rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name, namelen,
4890 name, namelen, &request);
4892 LASSERT(request != NULL);
4893 ll_update_times(request, parent);
4896 if (rc == 0 || rc == -EAGAIN) {
4897 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
4898 LASSERT(body != NULL);
4900 /* If the server does release layout lock, then we cleanup
4901 * the client och here, otherwise release it in out_close: */
4902 if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
4903 obd_mod_put(och->och_mod);
4904 md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
4906 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
4912 if (request != NULL) {
4913 ptlrpc_req_finished(request);
4917 /* Try again if the lease has cancelled. */
4918 if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
4923 ll_lease_close(och, child_inode, NULL);
4925 clear_nlink(child_inode);
4927 inode_unlock(child_inode);
4928 ll_finish_md_op_data(op_data);
4935 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
4937 struct ll_file_data *fd = file->private_data;
4941 * In order to avoid flood of warning messages, only print one message
4942 * for one file. And the entire message rate on the client is limited
4943 * by CDEBUG_LIMIT too.
4945 if (!(fd->fd_flags & LL_FILE_FLOCK_WARNING)) {
4946 fd->fd_flags |= LL_FILE_FLOCK_WARNING;
4947 CDEBUG_LIMIT(D_TTY | D_CONSOLE,
4948 "flock disabled, mount with '-o [local]flock' to enable\r\n");
4954 * test if some locks matching bits and l_req_mode are acquired
4955 * - bits can be in different locks
4956 * - if found clear the common lock bits in *bits
4957 * - the bits not found, are kept in *bits
4959 * \param bits [IN] searched lock bits [IN]
4960 * \param l_req_mode [IN] searched lock mode
4961 * \retval boolean, true iff all bits are found
4963 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
4965 struct lustre_handle lockh;
4966 union ldlm_policy_data policy;
4967 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
4968 (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
4977 fid = &ll_i2info(inode)->lli_fid;
4978 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
4979 ldlm_lockname[mode]);
4981 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
4982 for (i = 0; i < MDS_INODELOCK_NUMBITS && *bits != 0; i++) {
4983 policy.l_inodebits.bits = *bits & BIT(i);
4984 if (policy.l_inodebits.bits == 0)
4987 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
4988 &policy, mode, &lockh)) {
4989 struct ldlm_lock *lock;
4991 lock = ldlm_handle2lock(&lockh);
4994 ~(lock->l_policy_data.l_inodebits.bits);
4995 LDLM_LOCK_PUT(lock);
4997 *bits &= ~policy.l_inodebits.bits;
5004 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
5005 struct lustre_handle *lockh, __u64 flags,
5006 enum ldlm_mode mode)
5008 union ldlm_policy_data policy = { .l_inodebits = { bits } };
5013 fid = &ll_i2info(inode)->lli_fid;
5014 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
5016 rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
5017 fid, LDLM_IBITS, &policy, mode, lockh);
5022 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
5024 /* Already unlinked. Just update nlink and return success */
5025 if (rc == -ENOENT) {
5027 /* If it is striped directory, and there is bad stripe
5028 * Let's revalidate the dentry again, instead of returning
5030 if (ll_dir_striped(inode))
5033 /* This path cannot be hit for regular files unless in
5034 * case of obscure races, so no need to to validate
5036 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
5038 } else if (rc != 0) {
5039 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
5040 "%s: revalidate FID "DFID" error: rc = %d\n",
5041 ll_i2sbi(inode)->ll_fsname,
5042 PFID(ll_inode2fid(inode)), rc);
5048 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
5050 struct inode *parent;
5051 struct inode *inode = dentry->d_inode;
5052 struct obd_export *exp = ll_i2mdexp(inode);
5053 struct lookup_intent oit = {
5056 struct ptlrpc_request *req = NULL;
5057 struct md_op_data *op_data;
5058 const char *name = NULL;
5063 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
5064 PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
5066 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID) {
5067 parent = dentry->d_parent->d_inode;
5068 name = dentry->d_name.name;
5069 namelen = dentry->d_name.len;
5074 op_data = ll_prep_md_op_data(NULL, parent, inode, name, namelen, 0,
5075 LUSTRE_OPC_ANY, NULL);
5076 if (IS_ERR(op_data))
5077 RETURN(PTR_ERR(op_data));
5079 /* Call getattr by fid */
5080 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID)
5081 op_data->op_flags = MF_GETATTR_BY_FID;
5082 rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
5083 ll_finish_md_op_data(op_data);
5085 rc = ll_inode_revalidate_fini(inode, rc);
5089 rc = ll_revalidate_it_finish(req, &oit, dentry);
5091 ll_intent_release(&oit);
5095 /* Unlinked? Unhash dentry, so it is not picked up later by
5096 * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
5097 * here to preserve get_cwd functionality on 2.6.
5099 if (!dentry->d_inode->i_nlink)
5100 d_lustre_invalidate(dentry);
5102 ll_lookup_finish_locks(&oit, dentry);
5104 ptlrpc_req_finished(req);
5109 static int ll_merge_md_attr(struct inode *inode)
5111 struct ll_inode_info *lli = ll_i2info(inode);
5112 struct cl_attr attr = { 0 };
5115 LASSERT(lli->lli_lsm_md != NULL);
5117 if (!lmv_dir_striped(lli->lli_lsm_md))
5120 down_read(&lli->lli_lsm_sem);
5121 rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
5122 &attr, ll_md_blocking_ast);
5123 up_read(&lli->lli_lsm_sem);
5127 set_nlink(inode, attr.cat_nlink);
5128 inode->i_blocks = attr.cat_blocks;
5129 i_size_write(inode, attr.cat_size);
5131 ll_i2info(inode)->lli_atime = attr.cat_atime;
5132 ll_i2info(inode)->lli_mtime = attr.cat_mtime;
5133 ll_i2info(inode)->lli_ctime = attr.cat_ctime;
5138 int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
5139 unsigned int flags, bool foreign)
5141 struct inode *inode = de->d_inode;
5142 struct ll_sb_info *sbi = ll_i2sbi(inode);
5143 struct ll_inode_info *lli = ll_i2info(inode);
5144 struct inode *dir = de->d_parent->d_inode;
5145 bool need_glimpse = true;
5146 ktime_t kstart = ktime_get();
5149 /* The OST object(s) determine the file size, blocks and mtime. */
5150 if (!(request_mask & STATX_SIZE || request_mask & STATX_BLOCKS ||
5151 request_mask & STATX_MTIME))
5152 need_glimpse = false;
5154 if (dentry_may_statahead(dir, de))
5155 ll_start_statahead(dir, de, need_glimpse &&
5156 !(flags & AT_STATX_DONT_SYNC));
5158 if (flags & AT_STATX_DONT_SYNC)
5159 GOTO(fill_attr, rc = 0);
5161 rc = ll_inode_revalidate(de, IT_GETATTR);
5165 /* foreign file/dir are always of zero length, so don't
5166 * need to validate size.
5168 if (S_ISREG(inode->i_mode) && !foreign) {
5172 GOTO(fill_attr, rc);
5174 rc = pcc_inode_getattr(inode, request_mask, flags, &cached);
5175 if (cached && rc < 0)
5179 GOTO(fill_attr, rc);
5182 * If the returned attr is masked with OBD_MD_FLSIZE &
5183 * OBD_MD_FLBLOCKS & OBD_MD_FLMTIME, it means that the file size
5184 * or blocks obtained from MDT is strictly correct, and the file
5185 * is usually not being modified by clients, and the [a|m|c]time
5186 * got from MDT is also strictly correct.
5187 * Under this circumstance, it does not need to send glimpse
5188 * RPCs to OSTs for file attributes such as the size and blocks.
5190 if (lli->lli_attr_valid & OBD_MD_FLSIZE &&
5191 lli->lli_attr_valid & OBD_MD_FLBLOCKS &&
5192 lli->lli_attr_valid & OBD_MD_FLMTIME) {
5193 inode->i_mtime.tv_sec = lli->lli_mtime;
5194 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5195 inode->i_atime.tv_sec = lli->lli_atime;
5196 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5197 inode->i_ctime.tv_sec = lli->lli_ctime;
5198 GOTO(fill_attr, rc);
5201 /* In case of restore, the MDT has the right size and has
5202 * already send it back without granting the layout lock,
5203 * inode is up-to-date so glimpse is useless.
5204 * Also to glimpse we need the layout, in case of a running
5205 * restore the MDT holds the layout lock so the glimpse will
5206 * block up to the end of restore (getattr will block)
5208 if (!test_bit(LLIF_FILE_RESTORING, &lli->lli_flags)) {
5209 rc = ll_glimpse_size(inode);
5214 /* If object isn't regular a file then don't validate size. */
5215 /* foreign dir is not striped dir */
5216 if (ll_dir_striped(inode) && !foreign) {
5217 rc = ll_merge_md_attr(inode);
5222 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5223 inode->i_atime.tv_sec = lli->lli_atime;
5224 if (lli->lli_attr_valid & OBD_MD_FLMTIME)
5225 inode->i_mtime.tv_sec = lli->lli_mtime;
5226 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5227 inode->i_ctime.tv_sec = lli->lli_ctime;
5231 OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
5233 if (ll_need_32bit_api(sbi)) {
5234 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
5235 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
5236 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
5238 stat->ino = inode->i_ino;
5239 stat->dev = inode->i_sb->s_dev;
5240 stat->rdev = inode->i_rdev;
5243 /* foreign symlink to be exposed as a real symlink */
5245 stat->mode = inode->i_mode;
5247 stat->mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
5249 stat->uid = inode->i_uid;
5250 stat->gid = inode->i_gid;
5251 stat->atime = inode->i_atime;
5252 stat->mtime = inode->i_mtime;
5253 stat->ctime = inode->i_ctime;
5254 /* stat->blksize is used to tell about preferred IO size */
5255 if (sbi->ll_stat_blksize)
5256 stat->blksize = sbi->ll_stat_blksize;
5257 else if (S_ISREG(inode->i_mode))
5258 stat->blksize = 1 << min(PTLRPC_MAX_BRW_BITS + 1,
5259 LL_MAX_BLKSIZE_BITS);
5261 stat->blksize = 1 << inode->i_sb->s_blocksize_bits;
5263 stat->nlink = inode->i_nlink;
5264 stat->size = i_size_read(inode);
5265 stat->blocks = inode->i_blocks;
5267 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5268 if (flags & AT_STATX_DONT_SYNC) {
5269 if (stat->size == 0 &&
5270 lli->lli_attr_valid & OBD_MD_FLLAZYSIZE)
5271 stat->size = lli->lli_lazysize;
5272 if (stat->blocks == 0 &&
5273 lli->lli_attr_valid & OBD_MD_FLLAZYBLOCKS)
5274 stat->blocks = lli->lli_lazyblocks;
5277 if (lli->lli_attr_valid & OBD_MD_FLBTIME) {
5278 stat->result_mask |= STATX_BTIME;
5279 stat->btime.tv_sec = lli->lli_btime;
5282 stat->attributes_mask = STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
5283 stat->attributes |= ll_inode_to_ext_flags(inode->i_flags);
5284 stat->result_mask &= request_mask;
5287 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR,
5288 ktime_us_delta(ktime_get(), kstart));
5293 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5294 int ll_getattr(const struct path *path, struct kstat *stat,
5295 u32 request_mask, unsigned int flags)
5297 return ll_getattr_dentry(path->dentry, stat, request_mask, flags,
5301 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
5303 return ll_getattr_dentry(de, stat, STATX_BASIC_STATS,
5304 AT_STATX_SYNC_AS_STAT, false);
5308 int cl_falloc(struct inode *inode, int mode, loff_t offset, loff_t len)
5314 loff_t size = i_size_read(inode);
5318 env = cl_env_get(&refcheck);
5320 RETURN(PTR_ERR(env));
5322 io = vvp_env_thread_io(env);
5323 io->ci_obj = ll_i2info(inode)->lli_clob;
5324 io->ci_verify_layout = 1;
5325 io->u.ci_setattr.sa_parent_fid = lu_object_fid(&io->ci_obj->co_lu);
5326 io->u.ci_setattr.sa_falloc_mode = mode;
5327 io->u.ci_setattr.sa_falloc_offset = offset;
5328 io->u.ci_setattr.sa_falloc_end = offset + len;
5329 io->u.ci_setattr.sa_subtype = CL_SETATTR_FALLOCATE;
5330 if (io->u.ci_setattr.sa_falloc_end > size) {
5331 loff_t newsize = io->u.ci_setattr.sa_falloc_end;
5333 /* Check new size against VFS/VM file size limit and rlimit */
5334 rc = inode_newsize_ok(inode, newsize);
5337 if (newsize > ll_file_maxbytes(inode)) {
5338 CDEBUG(D_INODE, "file size too large %llu > %llu\n",
5339 (unsigned long long)newsize,
5340 ll_file_maxbytes(inode));
5347 rc = cl_io_init(env, io, CIT_SETATTR, io->ci_obj);
5349 rc = cl_io_loop(env, io);
5352 cl_io_fini(env, io);
5353 } while (unlikely(io->ci_need_restart));
5356 cl_env_put(env, &refcheck);
5360 long ll_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
5362 struct inode *inode = filp->f_path.dentry->d_inode;
5365 if (offset < 0 || len <= 0)
5368 * Encrypted inodes can't handle collapse range or zero range or insert
5369 * range since we would need to re-encrypt blocks with a different IV or
5370 * XTS tweak (which are based on the logical block number).
5371 * Similar to what ext4 does.
5373 if (IS_ENCRYPTED(inode) &&
5374 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
5375 FALLOC_FL_ZERO_RANGE)))
5376 RETURN(-EOPNOTSUPP);
5379 * mode == 0 (which is standard prealloc) and PUNCH is supported
5380 * Rest of mode options are not supported yet.
5382 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
5383 RETURN(-EOPNOTSUPP);
5385 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FALLOCATE, 1);
5387 rc = cl_falloc(inode, mode, offset, len);
5389 * ENOTSUPP (524) is an NFSv3 specific error code erroneously
5390 * used by Lustre in several places. Retuning it here would
5391 * confuse applications that explicity test for EOPNOTSUPP
5392 * (95) and fall back to ftruncate().
5394 if (rc == -ENOTSUPP)
5400 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5401 __u64 start, __u64 len)
5405 struct fiemap *fiemap;
5406 unsigned int extent_count = fieinfo->fi_extents_max;
5408 num_bytes = sizeof(*fiemap) + (extent_count *
5409 sizeof(struct fiemap_extent));
5410 OBD_ALLOC_LARGE(fiemap, num_bytes);
5415 fiemap->fm_flags = fieinfo->fi_flags;
5416 fiemap->fm_extent_count = fieinfo->fi_extents_max;
5417 fiemap->fm_start = start;
5418 fiemap->fm_length = len;
5419 if (extent_count > 0 &&
5420 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
5421 sizeof(struct fiemap_extent)) != 0)
5422 GOTO(out, rc = -EFAULT);
5424 rc = ll_do_fiemap(inode, fiemap, num_bytes);
5426 if (IS_ENCRYPTED(inode)) {
5429 for (i = 0; i < fiemap->fm_mapped_extents; i++)
5430 fiemap->fm_extents[i].fe_flags |=
5431 FIEMAP_EXTENT_DATA_ENCRYPTED |
5432 FIEMAP_EXTENT_ENCODED;
5435 fieinfo->fi_flags = fiemap->fm_flags;
5436 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
5437 if (extent_count > 0 &&
5438 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
5439 fiemap->fm_mapped_extents *
5440 sizeof(struct fiemap_extent)) != 0)
5441 GOTO(out, rc = -EFAULT);
5443 OBD_FREE_LARGE(fiemap, num_bytes);
5447 int ll_inode_permission(struct inode *inode, int mask)
5450 struct ll_sb_info *sbi;
5451 struct root_squash_info *squash;
5452 struct cred *cred = NULL;
5453 const struct cred *old_cred = NULL;
5454 bool squash_id = false;
5455 ktime_t kstart = ktime_get();
5459 if (mask & MAY_NOT_BLOCK)
5463 * as root inode are NOT getting validated in lookup operation,
5464 * need to do it before permission check.
5467 if (is_root_inode(inode)) {
5468 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
5473 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
5474 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
5476 /* squash fsuid/fsgid if needed */
5477 sbi = ll_i2sbi(inode);
5478 squash = &sbi->ll_squash;
5479 if (unlikely(squash->rsi_uid != 0 &&
5480 uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
5481 !(sbi->ll_flags & LL_SBI_NOROOTSQUASH))) {
5485 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
5486 __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
5487 squash->rsi_uid, squash->rsi_gid);
5489 /* update current process's credentials
5490 * and FS capability */
5491 cred = prepare_creds();
5495 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
5496 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
5497 cred->cap_effective = cap_drop_nfsd_set(cred->cap_effective);
5498 cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
5500 old_cred = override_creds(cred);
5503 rc = generic_permission(inode, mask);
5504 /* restore current process's credentials and FS capability */
5506 revert_creds(old_cred);
5511 ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM,
5512 ktime_us_delta(ktime_get(), kstart));
5517 /* -o localflock - only provides locally consistent flock locks */
5518 static const struct file_operations ll_file_operations = {
5519 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5520 # ifdef HAVE_SYNC_READ_WRITE
5521 .read = new_sync_read,
5522 .write = new_sync_write,
5524 .read_iter = ll_file_read_iter,
5525 .write_iter = ll_file_write_iter,
5526 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5527 .read = ll_file_read,
5528 .aio_read = ll_file_aio_read,
5529 .write = ll_file_write,
5530 .aio_write = ll_file_aio_write,
5531 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5532 .unlocked_ioctl = ll_file_ioctl,
5533 .open = ll_file_open,
5534 .release = ll_file_release,
5535 .mmap = ll_file_mmap,
5536 .llseek = ll_file_seek,
5537 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5538 .splice_read = generic_file_splice_read,
5540 .splice_read = pcc_file_splice_read,
5544 .fallocate = ll_fallocate,
5547 static const struct file_operations ll_file_operations_flock = {
5548 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5549 # ifdef HAVE_SYNC_READ_WRITE
5550 .read = new_sync_read,
5551 .write = new_sync_write,
5552 # endif /* HAVE_SYNC_READ_WRITE */
5553 .read_iter = ll_file_read_iter,
5554 .write_iter = ll_file_write_iter,
5555 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5556 .read = ll_file_read,
5557 .aio_read = ll_file_aio_read,
5558 .write = ll_file_write,
5559 .aio_write = ll_file_aio_write,
5560 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5561 .unlocked_ioctl = ll_file_ioctl,
5562 .open = ll_file_open,
5563 .release = ll_file_release,
5564 .mmap = ll_file_mmap,
5565 .llseek = ll_file_seek,
5566 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5567 .splice_read = generic_file_splice_read,
5569 .splice_read = pcc_file_splice_read,
5573 .flock = ll_file_flock,
5574 .lock = ll_file_flock,
5575 .fallocate = ll_fallocate,
5578 /* These are for -o noflock - to return ENOSYS on flock calls */
5579 static const struct file_operations ll_file_operations_noflock = {
5580 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5581 # ifdef HAVE_SYNC_READ_WRITE
5582 .read = new_sync_read,
5583 .write = new_sync_write,
5584 # endif /* HAVE_SYNC_READ_WRITE */
5585 .read_iter = ll_file_read_iter,
5586 .write_iter = ll_file_write_iter,
5587 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5588 .read = ll_file_read,
5589 .aio_read = ll_file_aio_read,
5590 .write = ll_file_write,
5591 .aio_write = ll_file_aio_write,
5592 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5593 .unlocked_ioctl = ll_file_ioctl,
5594 .open = ll_file_open,
5595 .release = ll_file_release,
5596 .mmap = ll_file_mmap,
5597 .llseek = ll_file_seek,
5598 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5599 .splice_read = generic_file_splice_read,
5601 .splice_read = pcc_file_splice_read,
5605 .flock = ll_file_noflock,
5606 .lock = ll_file_noflock,
5607 .fallocate = ll_fallocate,
5610 const struct inode_operations ll_file_inode_operations = {
5611 .setattr = ll_setattr,
5612 .getattr = ll_getattr,
5613 .permission = ll_inode_permission,
5614 #ifdef HAVE_IOP_XATTR
5615 .setxattr = ll_setxattr,
5616 .getxattr = ll_getxattr,
5617 .removexattr = ll_removexattr,
5619 .listxattr = ll_listxattr,
5620 .fiemap = ll_fiemap,
5621 .get_acl = ll_get_acl,
5622 #ifdef HAVE_IOP_SET_ACL
5623 .set_acl = ll_set_acl,
5627 const struct file_operations *ll_select_file_operations(struct ll_sb_info *sbi)
5629 const struct file_operations *fops = &ll_file_operations_noflock;
5631 if (sbi->ll_flags & LL_SBI_FLOCK)
5632 fops = &ll_file_operations_flock;
5633 else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
5634 fops = &ll_file_operations;
5639 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
5641 struct ll_inode_info *lli = ll_i2info(inode);
5642 struct cl_object *obj = lli->lli_clob;
5651 env = cl_env_get(&refcheck);
5653 RETURN(PTR_ERR(env));
5655 rc = cl_conf_set(env, lli->lli_clob, conf);
5659 if (conf->coc_opc == OBJECT_CONF_SET) {
5660 struct ldlm_lock *lock = conf->coc_lock;
5661 struct cl_layout cl = {
5665 LASSERT(lock != NULL);
5666 LASSERT(ldlm_has_layout(lock));
5668 /* it can only be allowed to match after layout is
5669 * applied to inode otherwise false layout would be
5670 * seen. Applying layout shoud happen before dropping
5671 * the intent lock. */
5672 ldlm_lock_allow_match(lock);
5674 rc = cl_object_layout_get(env, obj, &cl);
5679 DFID": layout version change: %u -> %u\n",
5680 PFID(&lli->lli_fid), ll_layout_version_get(lli),
5682 ll_layout_version_set(lli, cl.cl_layout_gen);
5686 cl_env_put(env, &refcheck);
5688 RETURN(rc < 0 ? rc : 0);
5691 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
5692 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
5695 struct ll_sb_info *sbi = ll_i2sbi(inode);
5696 struct ptlrpc_request *req;
5703 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
5704 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
5705 lock->l_lvb_data, lock->l_lvb_len);
5707 if (lock->l_lvb_data != NULL)
5710 /* if layout lock was granted right away, the layout is returned
5711 * within DLM_LVB of dlm reply; otherwise if the lock was ever
5712 * blocked and then granted via completion ast, we have to fetch
5713 * layout here. Please note that we can't use the LVB buffer in
5714 * completion AST because it doesn't have a large enough buffer */
5715 rc = ll_get_default_mdsize(sbi, &lmmsize);
5719 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), OBD_MD_FLXATTR,
5720 XATTR_NAME_LOV, lmmsize, &req);
5723 GOTO(out, rc = 0); /* empty layout */
5730 if (lmmsize == 0) /* empty layout */
5733 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
5735 GOTO(out, rc = -EFAULT);
5737 OBD_ALLOC_LARGE(lvbdata, lmmsize);
5738 if (lvbdata == NULL)
5739 GOTO(out, rc = -ENOMEM);
5741 memcpy(lvbdata, lmm, lmmsize);
5742 lock_res_and_lock(lock);
5743 if (unlikely(lock->l_lvb_data == NULL)) {
5744 lock->l_lvb_type = LVB_T_LAYOUT;
5745 lock->l_lvb_data = lvbdata;
5746 lock->l_lvb_len = lmmsize;
5749 unlock_res_and_lock(lock);
5752 OBD_FREE_LARGE(lvbdata, lmmsize);
5757 ptlrpc_req_finished(req);
5762 * Apply the layout to the inode. Layout lock is held and will be released
5765 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
5766 struct inode *inode)
5768 struct ll_inode_info *lli = ll_i2info(inode);
5769 struct ll_sb_info *sbi = ll_i2sbi(inode);
5770 struct ldlm_lock *lock;
5771 struct cl_object_conf conf;
5774 bool wait_layout = false;
5777 LASSERT(lustre_handle_is_used(lockh));
5779 lock = ldlm_handle2lock(lockh);
5780 LASSERT(lock != NULL);
5782 if (!ldlm_has_layout(lock))
5783 GOTO(out, rc = -EAGAIN);
5785 LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
5786 PFID(&lli->lli_fid), inode);
5788 /* in case this is a caching lock and reinstate with new inode */
5789 md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
5791 lock_res_and_lock(lock);
5792 lvb_ready = ldlm_is_lvb_ready(lock);
5793 unlock_res_and_lock(lock);
5795 /* checking lvb_ready is racy but this is okay. The worst case is
5796 * that multi processes may configure the file on the same time. */
5800 rc = ll_layout_fetch(inode, lock);
5804 /* for layout lock, lmm is stored in lock's lvb.
5805 * lvb_data is immutable if the lock is held so it's safe to access it
5808 * set layout to file. Unlikely this will fail as old layout was
5809 * surely eliminated */
5810 memset(&conf, 0, sizeof conf);
5811 conf.coc_opc = OBJECT_CONF_SET;
5812 conf.coc_inode = inode;
5813 conf.coc_lock = lock;
5814 conf.u.coc_layout.lb_buf = lock->l_lvb_data;
5815 conf.u.coc_layout.lb_len = lock->l_lvb_len;
5816 rc = ll_layout_conf(inode, &conf);
5818 /* refresh layout failed, need to wait */
5819 wait_layout = rc == -EBUSY;
5822 LDLM_LOCK_PUT(lock);
5823 ldlm_lock_decref(lockh, mode);
5825 /* wait for IO to complete if it's still being used. */
5827 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
5828 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5830 memset(&conf, 0, sizeof conf);
5831 conf.coc_opc = OBJECT_CONF_WAIT;
5832 conf.coc_inode = inode;
5833 rc = ll_layout_conf(inode, &conf);
5837 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
5838 sbi->ll_fsname, PFID(&lli->lli_fid), rc);
5844 * Issue layout intent RPC to MDS.
5845 * \param inode [in] file inode
5846 * \param intent [in] layout intent
5848 * \retval 0 on success
5849 * \retval < 0 error code
5851 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
5853 struct ll_inode_info *lli = ll_i2info(inode);
5854 struct ll_sb_info *sbi = ll_i2sbi(inode);
5855 struct md_op_data *op_data;
5856 struct lookup_intent it;
5857 struct ptlrpc_request *req;
5861 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
5862 0, 0, LUSTRE_OPC_ANY, NULL);
5863 if (IS_ERR(op_data))
5864 RETURN(PTR_ERR(op_data));
5866 op_data->op_data = intent;
5867 op_data->op_data_size = sizeof(*intent);
5869 memset(&it, 0, sizeof(it));
5870 it.it_op = IT_LAYOUT;
5871 if (intent->li_opc == LAYOUT_INTENT_WRITE ||
5872 intent->li_opc == LAYOUT_INTENT_TRUNC)
5873 it.it_flags = FMODE_WRITE;
5875 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
5876 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5878 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
5879 &ll_md_blocking_ast, 0);
5880 if (it.it_request != NULL)
5881 ptlrpc_req_finished(it.it_request);
5882 it.it_request = NULL;
5884 ll_finish_md_op_data(op_data);
5886 /* set lock data in case this is a new lock */
5888 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
5890 ll_intent_drop_lock(&it);
5896 * This function checks if there exists a LAYOUT lock on the client side,
5897 * or enqueues it if it doesn't have one in cache.
5899 * This function will not hold layout lock so it may be revoked any time after
5900 * this function returns. Any operations depend on layout should be redone
5903 * This function should be called before lov_io_init() to get an uptodate
5904 * layout version, the caller should save the version number and after IO
5905 * is finished, this function should be called again to verify that layout
5906 * is not changed during IO time.
5908 int ll_layout_refresh(struct inode *inode, __u32 *gen)
5910 struct ll_inode_info *lli = ll_i2info(inode);
5911 struct ll_sb_info *sbi = ll_i2sbi(inode);
5912 struct lustre_handle lockh;
5913 struct layout_intent intent = {
5914 .li_opc = LAYOUT_INTENT_ACCESS,
5916 enum ldlm_mode mode;
5920 *gen = ll_layout_version_get(lli);
5921 if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != CL_LAYOUT_GEN_NONE)
5925 LASSERT(fid_is_sane(ll_inode2fid(inode)));
5926 LASSERT(S_ISREG(inode->i_mode));
5928 /* take layout lock mutex to enqueue layout lock exclusively. */
5929 mutex_lock(&lli->lli_layout_mutex);
5932 /* mostly layout lock is caching on the local side, so try to
5933 * match it before grabbing layout lock mutex. */
5934 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
5935 LCK_CR | LCK_CW | LCK_PR |
5937 if (mode != 0) { /* hit cached lock */
5938 rc = ll_layout_lock_set(&lockh, mode, inode);
5944 rc = ll_layout_intent(inode, &intent);
5950 *gen = ll_layout_version_get(lli);
5951 mutex_unlock(&lli->lli_layout_mutex);
5957 * Issue layout intent RPC indicating where in a file an IO is about to write.
5959 * \param[in] inode file inode.
5960 * \param[in] ext write range with start offset of fille in bytes where
5961 * an IO is about to write, and exclusive end offset in
5964 * \retval 0 on success
5965 * \retval < 0 error code
5967 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
5968 struct lu_extent *ext)
5970 struct layout_intent intent = {
5972 .li_extent.e_start = ext->e_start,
5973 .li_extent.e_end = ext->e_end,
5978 rc = ll_layout_intent(inode, &intent);
5984 * This function send a restore request to the MDT
5986 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
5988 struct hsm_user_request *hur;
5992 len = sizeof(struct hsm_user_request) +
5993 sizeof(struct hsm_user_item);
5994 OBD_ALLOC(hur, len);
5998 hur->hur_request.hr_action = HUA_RESTORE;
5999 hur->hur_request.hr_archive_id = 0;
6000 hur->hur_request.hr_flags = 0;
6001 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
6002 sizeof(hur->hur_user_item[0].hui_fid));
6003 hur->hur_user_item[0].hui_extent.offset = offset;
6004 hur->hur_user_item[0].hui_extent.length = length;
6005 hur->hur_request.hr_itemcount = 1;
6006 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,