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 rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
640 &ll_md_blocking_ast, 0);
642 ll_finish_md_op_data(op_data);
644 /* reason for keep own exit path - don`t flood log
645 * with messages with -ESTALE errors.
647 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
648 it_open_error(DISP_OPEN_OPEN, itp))
650 ll_release_openhandle(de, itp);
654 if (it_disposition(itp, DISP_LOOKUP_NEG))
655 GOTO(out, rc = -ENOENT);
657 if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
658 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
659 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
663 rc = ll_prep_inode(&de->d_inode, &req->rq_pill, NULL, itp);
665 if (!rc && itp->it_lock_mode) {
668 /* If we got a lock back and it has a LOOKUP bit set,
669 * make sure the dentry is marked as valid so we can find it.
670 * We don't need to care about actual hashing since other bits
671 * of kernel will deal with that later.
673 ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, &bits);
674 if (bits & MDS_INODELOCK_LOOKUP)
675 d_lustre_revalidate(de);
676 /* if DoM bit returned along with LAYOUT bit then there
677 * can be read-on-open data returned.
679 if (bits & MDS_INODELOCK_DOM && bits & MDS_INODELOCK_LAYOUT)
680 ll_dom_finish_open(de->d_inode, req);
684 ptlrpc_req_finished(req);
685 ll_intent_drop_lock(itp);
687 /* We did open by fid, but by the time we got to the server,
688 * the object disappeared. If this is a create, we cannot really
689 * tell the userspace that the file it was trying to create
690 * does not exist. Instead let's return -ESTALE, and the VFS will
691 * retry the create with LOOKUP_REVAL that we are going to catch
692 * in ll_revalidate_dentry() and use lookup then.
694 if (rc == -ENOENT && itp->it_op & IT_CREAT)
700 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
701 struct obd_client_handle *och)
703 struct mdt_body *body;
705 body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
706 och->och_open_handle = body->mbo_open_handle;
707 och->och_fid = body->mbo_fid1;
708 och->och_lease_handle.cookie = it->it_lock_handle;
709 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
710 och->och_flags = it->it_flags;
712 return md_set_open_replay_data(md_exp, och, it);
715 static int ll_local_open(struct file *file, struct lookup_intent *it,
716 struct ll_file_data *fd, struct obd_client_handle *och)
718 struct inode *inode = file_inode(file);
721 LASSERT(!file->private_data);
728 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
733 file->private_data = fd;
734 ll_readahead_init(inode, &fd->fd_ras);
735 fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
736 /* turn off the kernel's read-ahead */
737 file->f_ra.ra_pages = 0;
739 /* ll_cl_context initialize */
740 rwlock_init(&fd->fd_lock);
741 INIT_LIST_HEAD(&fd->fd_lccs);
746 void ll_track_file_opens(struct inode *inode)
748 struct ll_inode_info *lli = ll_i2info(inode);
749 struct ll_sb_info *sbi = ll_i2sbi(inode);
751 /* do not skew results with delays from never-opened inodes */
752 if (ktime_to_ns(lli->lli_close_fd_time))
753 ll_stats_ops_tally(sbi, LPROC_LL_INODE_OPCLTM,
754 ktime_us_delta(ktime_get(), lli->lli_close_fd_time));
756 if (ktime_after(ktime_get(),
757 ktime_add_ms(lli->lli_close_fd_time,
758 sbi->ll_oc_max_ms))) {
759 lli->lli_open_fd_count = 1;
760 lli->lli_close_fd_time = ns_to_ktime(0);
762 lli->lli_open_fd_count++;
765 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_OCOUNT,
766 lli->lli_open_fd_count);
769 /* Open a file, and (for the very first open) create objects on the OSTs at
770 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
771 * creation or open until ll_lov_setstripe() ioctl is called.
773 * If we already have the stripe MD locally then we don't request it in
774 * md_open(), by passing a lmm_size = 0.
776 * It is up to the application to ensure no other processes open this file
777 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
778 * used. We might be able to avoid races of that sort by getting lli_open_sem
779 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
780 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
782 int ll_file_open(struct inode *inode, struct file *file)
784 struct ll_inode_info *lli = ll_i2info(inode);
785 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
786 .it_flags = file->f_flags };
787 struct obd_client_handle **och_p = NULL;
788 __u64 *och_usecount = NULL;
789 struct ll_file_data *fd;
790 ktime_t kstart = ktime_get();
794 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
795 PFID(ll_inode2fid(inode)), inode, file->f_flags);
797 it = file->private_data; /* XXX: compat macro */
798 file->private_data = NULL; /* prevent ll_local_open assertion */
800 if (S_ISREG(inode->i_mode)) {
801 rc = llcrypt_file_open(inode, file);
803 GOTO(out_nofiledata, rc);
806 fd = ll_file_data_get();
808 GOTO(out_nofiledata, rc = -ENOMEM);
811 if (S_ISDIR(inode->i_mode))
812 ll_authorize_statahead(inode, fd);
814 ll_track_file_opens(inode);
815 if (is_root_inode(inode)) {
816 file->private_data = fd;
820 if (!it || !it->it_disposition) {
821 /* Convert f_flags into access mode. We cannot use file->f_mode,
822 * because everything but O_ACCMODE mask was stripped from
824 if ((oit.it_flags + 1) & O_ACCMODE)
826 if (file->f_flags & O_TRUNC)
827 oit.it_flags |= FMODE_WRITE;
829 /* kernel only call f_op->open in dentry_open. filp_open calls
830 * dentry_open after call to open_namei that checks permissions.
831 * Only nfsd_open call dentry_open directly without checking
832 * permissions and because of that this code below is safe.
834 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
835 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
837 /* We do not want O_EXCL here, presumably we opened the file
838 * already? XXX - NFS implications? */
839 oit.it_flags &= ~O_EXCL;
841 /* bug20584, if "it_flags" contains O_CREAT, the file will be
842 * created if necessary, then "IT_CREAT" should be set to keep
843 * consistent with it */
844 if (oit.it_flags & O_CREAT)
845 oit.it_op |= IT_CREAT;
851 /* Let's see if we have file open on MDS already. */
852 if (it->it_flags & FMODE_WRITE) {
853 och_p = &lli->lli_mds_write_och;
854 och_usecount = &lli->lli_open_fd_write_count;
855 } else if (it->it_flags & FMODE_EXEC) {
856 och_p = &lli->lli_mds_exec_och;
857 och_usecount = &lli->lli_open_fd_exec_count;
859 och_p = &lli->lli_mds_read_och;
860 och_usecount = &lli->lli_open_fd_read_count;
863 mutex_lock(&lli->lli_och_mutex);
864 if (*och_p) { /* Open handle is present */
865 if (it_disposition(it, DISP_OPEN_OPEN)) {
866 /* Well, there's extra open request that we do not need,
867 * let's close it somehow. This will decref request. */
868 rc = it_open_error(DISP_OPEN_OPEN, it);
870 mutex_unlock(&lli->lli_och_mutex);
871 GOTO(out_openerr, rc);
874 ll_release_openhandle(file_dentry(file), it);
878 rc = ll_local_open(file, it, fd, NULL);
881 mutex_unlock(&lli->lli_och_mutex);
882 GOTO(out_openerr, rc);
885 LASSERT(*och_usecount == 0);
886 if (!it->it_disposition) {
887 struct dentry *dentry = file_dentry(file);
888 struct ll_sb_info *sbi = ll_i2sbi(inode);
889 struct ll_dentry_data *ldd;
891 /* We cannot just request lock handle now, new ELC code
892 * means that one of other OPEN locks for this file
893 * could be cancelled, and since blocking ast handler
894 * would attempt to grab och_mutex as well, that would
895 * result in a deadlock
897 mutex_unlock(&lli->lli_och_mutex);
899 * Normally called under two situations:
901 * 2. A race/condition on MDS resulting in no open
902 * handle to be returned from LOOKUP|OPEN request,
903 * for example if the target entry was a symlink.
905 * In NFS path we know there's pathologic behavior
906 * so we always enable open lock caching when coming
907 * from there. It's detected by setting a flag in
910 * After reaching number of opens of this inode
911 * we always ask for an open lock on it to handle
912 * bad userspace actors that open and close files
913 * in a loop for absolutely no good reason
916 ldd = ll_d2d(dentry);
917 if (filename_is_volatile(dentry->d_name.name,
920 /* There really is nothing here, but this
921 * make this more readable I think.
922 * We do not want openlock for volatile
923 * files under any circumstances
925 } else if (ldd && ldd->lld_nfs_dentry) {
926 /* NFS path. This also happens to catch
927 * open by fh files I guess
929 it->it_flags |= MDS_OPEN_LOCK;
930 /* clear the flag for future lookups */
931 ldd->lld_nfs_dentry = 0;
932 } else if (sbi->ll_oc_thrsh_count > 0) {
933 /* Take MDS_OPEN_LOCK with many opens */
934 if (lli->lli_open_fd_count >=
935 sbi->ll_oc_thrsh_count)
936 it->it_flags |= MDS_OPEN_LOCK;
938 /* If this is open after we just closed */
939 else if (ktime_before(ktime_get(),
940 ktime_add_ms(lli->lli_close_fd_time,
941 sbi->ll_oc_thrsh_ms)))
942 it->it_flags |= MDS_OPEN_LOCK;
946 * Always specify MDS_OPEN_BY_FID because we don't want
947 * to get file with different fid.
949 it->it_flags |= MDS_OPEN_BY_FID;
950 rc = ll_intent_file_open(dentry, NULL, 0, it);
952 GOTO(out_openerr, rc);
956 OBD_ALLOC(*och_p, sizeof(struct obd_client_handle));
958 GOTO(out_och_free, rc = -ENOMEM);
962 /* md_intent_lock() didn't get a request ref if there was an
963 * open error, so don't do cleanup on the request here
965 /* XXX (green): Should not we bail out on any error here, not
966 * just open error? */
967 rc = it_open_error(DISP_OPEN_OPEN, it);
969 GOTO(out_och_free, rc);
971 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
972 "inode %p: disposition %x, status %d\n", inode,
973 it_disposition(it, ~0), it->it_status);
975 rc = ll_local_open(file, it, fd, *och_p);
977 GOTO(out_och_free, rc);
980 rc = pcc_file_open(inode, file);
982 GOTO(out_och_free, rc);
984 mutex_unlock(&lli->lli_och_mutex);
988 /* Must do this outside lli_och_mutex lock to prevent deadlock where
989 different kind of OPEN lock for this same inode gets cancelled
990 by ldlm_cancel_lru */
991 if (!S_ISREG(inode->i_mode))
992 GOTO(out_och_free, rc);
993 cl_lov_delay_create_clear(&file->f_flags);
994 GOTO(out_och_free, rc);
998 if (och_p && *och_p) {
999 OBD_FREE(*och_p, sizeof(struct obd_client_handle));
1000 *och_p = NULL; /* OBD_FREE writes some magic there */
1003 mutex_unlock(&lli->lli_och_mutex);
1006 if (lli->lli_opendir_key == fd)
1007 ll_deauthorize_statahead(inode, fd);
1010 ll_file_data_put(fd);
1012 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN,
1013 ktime_us_delta(ktime_get(), kstart));
1017 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
1018 ptlrpc_req_finished(it->it_request);
1019 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1025 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
1026 struct ldlm_lock_desc *desc, void *data, int flag)
1029 struct lustre_handle lockh;
1033 case LDLM_CB_BLOCKING:
1034 ldlm_lock2handle(lock, &lockh);
1035 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1037 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
1041 case LDLM_CB_CANCELING:
1049 * When setting a lease on a file, we take ownership of the lli_mds_*_och
1050 * and save it as fd->fd_och so as to force client to reopen the file even
1051 * if it has an open lock in cache already.
1053 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
1054 struct lustre_handle *old_open_handle)
1056 struct ll_inode_info *lli = ll_i2info(inode);
1057 struct ll_file_data *fd = file->private_data;
1058 struct obd_client_handle **och_p;
1059 __u64 *och_usecount;
1063 /* Get the openhandle of the file */
1064 mutex_lock(&lli->lli_och_mutex);
1065 if (fd->fd_lease_och != NULL)
1066 GOTO(out_unlock, rc = -EBUSY);
1068 if (fd->fd_och == NULL) {
1069 if (file->f_mode & FMODE_WRITE) {
1070 LASSERT(lli->lli_mds_write_och != NULL);
1071 och_p = &lli->lli_mds_write_och;
1072 och_usecount = &lli->lli_open_fd_write_count;
1074 LASSERT(lli->lli_mds_read_och != NULL);
1075 och_p = &lli->lli_mds_read_och;
1076 och_usecount = &lli->lli_open_fd_read_count;
1079 if (*och_usecount > 1)
1080 GOTO(out_unlock, rc = -EBUSY);
1082 fd->fd_och = *och_p;
1087 *old_open_handle = fd->fd_och->och_open_handle;
1091 mutex_unlock(&lli->lli_och_mutex);
1096 * Release ownership on lli_mds_*_och when putting back a file lease.
1098 static int ll_lease_och_release(struct inode *inode, struct file *file)
1100 struct ll_inode_info *lli = ll_i2info(inode);
1101 struct ll_file_data *fd = file->private_data;
1102 struct obd_client_handle **och_p;
1103 struct obd_client_handle *old_och = NULL;
1104 __u64 *och_usecount;
1108 mutex_lock(&lli->lli_och_mutex);
1109 if (file->f_mode & FMODE_WRITE) {
1110 och_p = &lli->lli_mds_write_och;
1111 och_usecount = &lli->lli_open_fd_write_count;
1113 och_p = &lli->lli_mds_read_och;
1114 och_usecount = &lli->lli_open_fd_read_count;
1117 /* The file may have been open by another process (broken lease) so
1118 * *och_p is not NULL. In this case we should simply increase usecount
1121 if (*och_p != NULL) {
1122 old_och = fd->fd_och;
1125 *och_p = fd->fd_och;
1129 mutex_unlock(&lli->lli_och_mutex);
1131 if (old_och != NULL)
1132 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
1138 * Acquire a lease and open the file.
1140 static struct obd_client_handle *
1141 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
1144 struct lookup_intent it = { .it_op = IT_OPEN };
1145 struct ll_sb_info *sbi = ll_i2sbi(inode);
1146 struct md_op_data *op_data;
1147 struct ptlrpc_request *req = NULL;
1148 struct lustre_handle old_open_handle = { 0 };
1149 struct obd_client_handle *och = NULL;
1154 if (fmode != FMODE_WRITE && fmode != FMODE_READ)
1155 RETURN(ERR_PTR(-EINVAL));
1158 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
1159 RETURN(ERR_PTR(-EPERM));
1161 rc = ll_lease_och_acquire(inode, file, &old_open_handle);
1163 RETURN(ERR_PTR(rc));
1168 RETURN(ERR_PTR(-ENOMEM));
1170 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
1171 LUSTRE_OPC_ANY, NULL);
1172 if (IS_ERR(op_data))
1173 GOTO(out, rc = PTR_ERR(op_data));
1175 /* To tell the MDT this openhandle is from the same owner */
1176 op_data->op_open_handle = old_open_handle;
1178 it.it_flags = fmode | open_flags;
1179 it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
1180 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
1181 &ll_md_blocking_lease_ast,
1182 /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
1183 * it can be cancelled which may mislead applications that the lease is
1185 * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
1186 * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
1187 * doesn't deal with openhandle, so normal openhandle will be leaked. */
1188 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
1189 ll_finish_md_op_data(op_data);
1190 ptlrpc_req_finished(req);
1192 GOTO(out_release_it, rc);
1194 if (it_disposition(&it, DISP_LOOKUP_NEG))
1195 GOTO(out_release_it, rc = -ENOENT);
1197 rc = it_open_error(DISP_OPEN_OPEN, &it);
1199 GOTO(out_release_it, rc);
1201 LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
1202 rc = ll_och_fill(sbi->ll_md_exp, &it, och);
1204 GOTO(out_release_it, rc);
1206 if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
1207 GOTO(out_close, rc = -EOPNOTSUPP);
1209 /* already get lease, handle lease lock */
1210 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
1211 if (!it.it_lock_mode ||
1212 !(it.it_lock_bits & MDS_INODELOCK_OPEN)) {
1213 /* open lock must return for lease */
1214 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
1215 PFID(ll_inode2fid(inode)), it.it_lock_mode,
1217 GOTO(out_close, rc = -EPROTO);
1220 ll_intent_release(&it);
1224 /* Cancel open lock */
1225 if (it.it_lock_mode != 0) {
1226 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
1228 it.it_lock_mode = 0;
1229 och->och_lease_handle.cookie = 0ULL;
1231 rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
1233 CERROR("%s: error closing file "DFID": %d\n",
1234 sbi->ll_fsname, PFID(&ll_i2info(inode)->lli_fid), rc2);
1235 och = NULL; /* och has been freed in ll_close_inode_openhandle() */
1237 ll_intent_release(&it);
1241 RETURN(ERR_PTR(rc));
1245 * Check whether a layout swap can be done between two inodes.
1247 * \param[in] inode1 First inode to check
1248 * \param[in] inode2 Second inode to check
1250 * \retval 0 on success, layout swap can be performed between both inodes
1251 * \retval negative error code if requirements are not met
1253 static int ll_check_swap_layouts_validity(struct inode *inode1,
1254 struct inode *inode2)
1256 if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
1259 if (inode_permission(inode1, MAY_WRITE) ||
1260 inode_permission(inode2, MAY_WRITE))
1263 if (inode1->i_sb != inode2->i_sb)
1269 static int ll_swap_layouts_close(struct obd_client_handle *och,
1270 struct inode *inode, struct inode *inode2)
1272 const struct lu_fid *fid1 = ll_inode2fid(inode);
1273 const struct lu_fid *fid2;
1277 CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
1278 ll_i2sbi(inode)->ll_fsname, PFID(fid1));
1280 rc = ll_check_swap_layouts_validity(inode, inode2);
1282 GOTO(out_free_och, rc);
1284 /* We now know that inode2 is a lustre inode */
1285 fid2 = ll_inode2fid(inode2);
1287 rc = lu_fid_cmp(fid1, fid2);
1289 GOTO(out_free_och, rc = -EINVAL);
1291 /* Close the file and {swap,merge} layouts between inode & inode2.
1292 * NB: lease lock handle is released in mdc_close_layout_swap_pack()
1293 * because we still need it to pack l_remote_handle to MDT. */
1294 rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
1297 och = NULL; /* freed in ll_close_inode_openhandle() */
1307 * Release lease and close the file.
1308 * It will check if the lease has ever broken.
1310 static int ll_lease_close_intent(struct obd_client_handle *och,
1311 struct inode *inode,
1312 bool *lease_broken, enum mds_op_bias bias,
1315 struct ldlm_lock *lock;
1316 bool cancelled = true;
1320 lock = ldlm_handle2lock(&och->och_lease_handle);
1322 lock_res_and_lock(lock);
1323 cancelled = ldlm_is_cancel(lock);
1324 unlock_res_and_lock(lock);
1325 LDLM_LOCK_PUT(lock);
1328 CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
1329 PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
1331 if (lease_broken != NULL)
1332 *lease_broken = cancelled;
1334 if (!cancelled && !bias)
1335 ldlm_cli_cancel(&och->och_lease_handle, 0);
1337 if (cancelled) { /* no need to excute intent */
1342 rc = ll_close_inode_openhandle(inode, och, bias, data);
1346 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
1349 return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
1353 * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
1355 static int ll_lease_file_resync(struct obd_client_handle *och,
1356 struct inode *inode, unsigned long arg)
1358 struct ll_sb_info *sbi = ll_i2sbi(inode);
1359 struct md_op_data *op_data;
1360 struct ll_ioc_lease_id ioc;
1361 __u64 data_version_unused;
1365 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1366 LUSTRE_OPC_ANY, NULL);
1367 if (IS_ERR(op_data))
1368 RETURN(PTR_ERR(op_data));
1370 if (copy_from_user(&ioc, (struct ll_ioc_lease_id __user *)arg,
1374 /* before starting file resync, it's necessary to clean up page cache
1375 * in client memory, otherwise once the layout version is increased,
1376 * writing back cached data will be denied the OSTs. */
1377 rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
1381 op_data->op_lease_handle = och->och_lease_handle;
1382 op_data->op_mirror_id = ioc.lil_mirror_id;
1383 rc = md_file_resync(sbi->ll_md_exp, op_data);
1389 ll_finish_md_op_data(op_data);
1393 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1395 struct ll_inode_info *lli = ll_i2info(inode);
1396 struct cl_object *obj = lli->lli_clob;
1397 struct cl_attr *attr = vvp_env_thread_attr(env);
1405 ll_inode_size_lock(inode);
1407 /* Merge timestamps the most recently obtained from MDS with
1408 * timestamps obtained from OSTs.
1410 * Do not overwrite atime of inode because it may be refreshed
1411 * by file_accessed() function. If the read was served by cache
1412 * data, there is no RPC to be sent so that atime may not be
1413 * transferred to OSTs at all. MDT only updates atime at close time
1414 * if it's at least 'mdd.*.atime_diff' older.
1415 * All in all, the atime in Lustre does not strictly comply with
1416 * POSIX. Solving this problem needs to send an RPC to MDT for each
1417 * read, this will hurt performance.
1419 if (test_and_clear_bit(LLIF_UPDATE_ATIME, &lli->lli_flags) ||
1420 inode->i_atime.tv_sec < lli->lli_atime)
1421 inode->i_atime.tv_sec = lli->lli_atime;
1423 inode->i_mtime.tv_sec = lli->lli_mtime;
1424 inode->i_ctime.tv_sec = lli->lli_ctime;
1426 mtime = inode->i_mtime.tv_sec;
1427 atime = inode->i_atime.tv_sec;
1428 ctime = inode->i_ctime.tv_sec;
1430 cl_object_attr_lock(obj);
1431 if (OBD_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
1434 rc = cl_object_attr_get(env, obj, attr);
1435 cl_object_attr_unlock(obj);
1438 GOTO(out_size_unlock, rc = (rc == -ENODATA ? 0 : rc));
1440 if (atime < attr->cat_atime)
1441 atime = attr->cat_atime;
1443 if (ctime < attr->cat_ctime)
1444 ctime = attr->cat_ctime;
1446 if (mtime < attr->cat_mtime)
1447 mtime = attr->cat_mtime;
1449 CDEBUG(D_VFSTRACE, DFID" updating i_size %llu\n",
1450 PFID(&lli->lli_fid), attr->cat_size);
1452 i_size_write(inode, attr->cat_size);
1453 inode->i_blocks = attr->cat_blocks;
1455 inode->i_mtime.tv_sec = mtime;
1456 inode->i_atime.tv_sec = atime;
1457 inode->i_ctime.tv_sec = ctime;
1460 ll_inode_size_unlock(inode);
1466 * Set designated mirror for I/O.
1468 * So far only read, write, and truncated can support to issue I/O to
1469 * designated mirror.
1471 void ll_io_set_mirror(struct cl_io *io, const struct file *file)
1473 struct ll_file_data *fd = file->private_data;
1475 /* clear layout version for generic(non-resync) I/O in case it carries
1476 * stale layout version due to I/O restart */
1477 io->ci_layout_version = 0;
1479 /* FLR: disable non-delay for designated mirror I/O because obviously
1480 * only one mirror is available */
1481 if (fd->fd_designated_mirror > 0) {
1483 io->ci_designated_mirror = fd->fd_designated_mirror;
1484 io->ci_layout_version = fd->fd_layout_version;
1487 CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
1488 file->f_path.dentry->d_name.name, io->ci_designated_mirror);
1491 static bool file_is_noatime(const struct file *file)
1493 const struct vfsmount *mnt = file->f_path.mnt;
1494 const struct inode *inode = file_inode((struct file *)file);
1496 /* Adapted from file_accessed() and touch_atime().*/
1497 if (file->f_flags & O_NOATIME)
1500 if (inode->i_flags & S_NOATIME)
1503 if (IS_NOATIME(inode))
1506 if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1509 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1512 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
1518 void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot,
1519 struct vvp_io_args *args)
1521 struct inode *inode = file_inode(file);
1522 struct ll_file_data *fd = file->private_data;
1524 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1525 io->ci_lock_no_expand = fd->ll_lock_no_expand;
1527 if (iot == CIT_WRITE) {
1528 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1529 io->u.ci_wr.wr_sync = !!(file->f_flags & O_SYNC ||
1530 file->f_flags & O_DIRECT ||
1532 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1533 io->u.ci_wr.wr_sync |= !!(args &&
1534 (args->u.normal.via_iocb->ki_flags &
1539 io->ci_obj = ll_i2info(inode)->lli_clob;
1540 io->ci_lockreq = CILR_MAYBE;
1541 if (ll_file_nolock(file)) {
1542 io->ci_lockreq = CILR_NEVER;
1543 io->ci_no_srvlock = 1;
1544 } else if (file->f_flags & O_APPEND) {
1545 io->ci_lockreq = CILR_MANDATORY;
1547 io->ci_noatime = file_is_noatime(file);
1548 io->ci_async_readahead = false;
1550 /* FLR: only use non-delay I/O for read as there is only one
1551 * avaliable mirror for write. */
1552 io->ci_ndelay = !(iot == CIT_WRITE);
1554 ll_io_set_mirror(io, file);
1557 static void ll_heat_add(struct inode *inode, enum cl_io_type iot,
1560 struct ll_inode_info *lli = ll_i2info(inode);
1561 struct ll_sb_info *sbi = ll_i2sbi(inode);
1562 enum obd_heat_type sample_type;
1563 enum obd_heat_type iobyte_type;
1564 __u64 now = ktime_get_real_seconds();
1566 if (!ll_sbi_has_file_heat(sbi) ||
1567 lli->lli_heat_flags & LU_HEAT_FLAG_OFF)
1570 if (iot == CIT_READ) {
1571 sample_type = OBD_HEAT_READSAMPLE;
1572 iobyte_type = OBD_HEAT_READBYTE;
1573 } else if (iot == CIT_WRITE) {
1574 sample_type = OBD_HEAT_WRITESAMPLE;
1575 iobyte_type = OBD_HEAT_WRITEBYTE;
1580 spin_lock(&lli->lli_heat_lock);
1581 obd_heat_add(&lli->lli_heat_instances[sample_type], now, 1,
1582 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1583 obd_heat_add(&lli->lli_heat_instances[iobyte_type], now, count,
1584 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1585 spin_unlock(&lli->lli_heat_lock);
1589 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1590 struct file *file, enum cl_io_type iot,
1591 loff_t *ppos, size_t count)
1593 struct vvp_io *vio = vvp_env_io(env);
1594 struct inode *inode = file_inode(file);
1595 struct ll_inode_info *lli = ll_i2info(inode);
1596 struct ll_sb_info *sbi = ll_i2sbi(inode);
1597 struct ll_file_data *fd = file->private_data;
1598 struct range_lock range;
1599 bool range_locked = false;
1604 unsigned int retried = 0, dio_lock = 0;
1605 bool is_aio = false;
1606 bool is_parallel_dio = false;
1607 struct cl_dio_aio *ci_aio = NULL;
1609 bool partial_io = false;
1610 size_t max_io_pages, max_cached_pages;
1614 CDEBUG(D_VFSTRACE, "%s: %s ppos: %llu, count: %zu\n",
1615 file_dentry(file)->d_name.name,
1616 iot == CIT_READ ? "read" : "write", *ppos, count);
1618 max_io_pages = PTLRPC_MAX_BRW_PAGES * OBD_MAX_RIF_DEFAULT;
1619 max_cached_pages = sbi->ll_cache->ccc_lru_max;
1620 if (max_io_pages > (max_cached_pages >> 2))
1621 max_io_pages = max_cached_pages >> 2;
1623 io = vvp_env_thread_io(env);
1624 if (file->f_flags & O_DIRECT) {
1625 if (!is_sync_kiocb(args->u.normal.via_iocb))
1628 /* the kernel does not support AIO on pipes, and parallel DIO
1629 * uses part of the AIO path, so we must not do parallel dio
1632 is_parallel_dio = !iov_iter_is_pipe(args->u.normal.via_iter) &&
1635 if (!ll_sbi_has_parallel_dio(sbi))
1636 is_parallel_dio = false;
1638 ci_aio = cl_aio_alloc(args->u.normal.via_iocb,
1639 ll_i2info(inode)->lli_clob);
1641 GOTO(out, rc = -ENOMEM);
1646 * IO block size need be aware of cached page limit, otherwise
1647 * if we have small max_cached_mb but large block IO issued, io
1648 * could not be finished and blocked whole client.
1650 if (file->f_flags & O_DIRECT)
1653 per_bytes = min(max_io_pages << PAGE_SHIFT, count);
1654 partial_io = per_bytes < count;
1655 io = vvp_env_thread_io(env);
1656 ll_io_init(io, file, iot, args);
1657 io->ci_aio = ci_aio;
1658 io->ci_dio_lock = dio_lock;
1659 io->ci_ndelay_tried = retried;
1660 io->ci_parallel_dio = is_parallel_dio;
1662 if (cl_io_rw_init(env, io, iot, *ppos, per_bytes) == 0) {
1663 if (file->f_flags & O_APPEND)
1664 range_lock_init(&range, 0, LUSTRE_EOF);
1666 range_lock_init(&range, *ppos, *ppos + per_bytes - 1);
1668 vio->vui_fd = file->private_data;
1669 vio->vui_iter = args->u.normal.via_iter;
1670 vio->vui_iocb = args->u.normal.via_iocb;
1671 /* Direct IO reads must also take range lock,
1672 * or multiple reads will try to work on the same pages
1673 * See LU-6227 for details.
1675 if (((iot == CIT_WRITE) ||
1676 (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1677 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1678 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1680 rc = range_lock(&lli->lli_write_tree, &range);
1684 range_locked = true;
1687 ll_cl_add(file, env, io, LCC_RW);
1688 rc = cl_io_loop(env, io);
1689 ll_cl_remove(file, env);
1691 if (range_locked && !is_parallel_dio) {
1692 CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1694 range_unlock(&lli->lli_write_tree, &range);
1695 range_locked = false;
1698 /* cl_io_rw_init() handled IO */
1702 /* N/B: parallel DIO may be disabled during i/o submission;
1703 * if that occurs, async RPCs are resolved before we get here, and this
1704 * wait call completes immediately.
1706 if (is_parallel_dio) {
1707 struct cl_sync_io *anchor = &io->ci_aio->cda_sync;
1709 /* for dio, EIOCBQUEUED is an implementation detail,
1710 * and we don't return it to userspace
1712 if (rc == -EIOCBQUEUED)
1715 rc2 = cl_sync_io_wait_recycle(env, anchor, 0, 0);
1720 range_unlock(&lli->lli_write_tree, &range);
1721 range_locked = false;
1726 * In order to move forward AIO, ci_nob was increased,
1727 * but that doesn't mean io have been finished, it just
1728 * means io have been submited, we will always return
1729 * EIOCBQUEUED to the caller, So we could only return
1730 * number of bytes in non-AIO case.
1732 if (io->ci_nob > 0) {
1735 result += io->ci_nob;
1736 *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
1741 count -= io->ci_nob;
1743 /* prepare IO restart */
1745 args->u.normal.via_iter = vio->vui_iter;
1749 * Reexpand iov count because it was zero
1752 iov_iter_reexpand(vio->vui_iter, count);
1753 if (per_bytes == io->ci_nob)
1754 io->ci_need_restart = 1;
1758 cl_io_fini(env, io);
1761 "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1762 file->f_path.dentry->d_name.name,
1763 iot, rc, result, io->ci_need_restart);
1765 if ((rc == 0 || rc == -ENODATA || rc == -ENOLCK) &&
1766 count > 0 && io->ci_need_restart) {
1768 "%s: restart %s from %lld, count: %zu, ret: %zd, rc: %d\n",
1769 file_dentry(file)->d_name.name,
1770 iot == CIT_READ ? "read" : "write",
1771 *ppos, count, result, rc);
1772 /* preserve the tried count for FLR */
1773 retried = io->ci_ndelay_tried;
1774 dio_lock = io->ci_dio_lock;
1780 * VFS will call aio_complete() if no -EIOCBQUEUED
1781 * is returned for AIO, so we can not call aio_complete()
1784 if (rc != -EIOCBQUEUED)
1785 io->ci_aio->cda_no_aio_complete = 1;
1787 * Drop one extra reference so that end_io() could be
1788 * called for this IO context, we could call it after
1789 * we make sure all AIO requests have been proceed.
1791 cl_sync_io_note(env, &io->ci_aio->cda_sync,
1792 rc == -EIOCBQUEUED ? 0 : rc);
1794 cl_aio_free(env, io->ci_aio);
1799 if (iot == CIT_READ) {
1801 ll_stats_ops_tally(ll_i2sbi(inode),
1802 LPROC_LL_READ_BYTES, result);
1803 } else if (iot == CIT_WRITE) {
1805 ll_stats_ops_tally(ll_i2sbi(inode),
1806 LPROC_LL_WRITE_BYTES, result);
1807 fd->fd_write_failed = false;
1808 } else if (result == 0 && rc == 0) {
1811 fd->fd_write_failed = true;
1813 fd->fd_write_failed = false;
1814 } else if (rc != -ERESTARTSYS) {
1815 fd->fd_write_failed = true;
1819 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1821 ll_heat_add(inode, iot, result);
1823 RETURN(result > 0 ? result : rc);
1827 * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1828 * especially for small I/O.
1830 * To serve a read request, CLIO has to create and initialize a cl_io and
1831 * then request DLM lock. This has turned out to have siginificant overhead
1832 * and affects the performance of small I/O dramatically.
1834 * It's not necessary to create a cl_io for each I/O. Under the help of read
1835 * ahead, most of the pages being read are already in memory cache and we can
1836 * read those pages directly because if the pages exist, the corresponding DLM
1837 * lock must exist so that page content must be valid.
1839 * In fast read implementation, the llite speculatively finds and reads pages
1840 * in memory cache. There are three scenarios for fast read:
1841 * - If the page exists and is uptodate, kernel VM will provide the data and
1842 * CLIO won't be intervened;
1843 * - If the page was brought into memory by read ahead, it will be exported
1844 * and read ahead parameters will be updated;
1845 * - Otherwise the page is not in memory, we can't do fast read. Therefore,
1846 * it will go back and invoke normal read, i.e., a cl_io will be created
1847 * and DLM lock will be requested.
1849 * POSIX compliance: posix standard states that read is intended to be atomic.
1850 * Lustre read implementation is in line with Linux kernel read implementation
1851 * and neither of them complies with POSIX standard in this matter. Fast read
1852 * doesn't make the situation worse on single node but it may interleave write
1853 * results from multiple nodes due to short read handling in ll_file_aio_read().
1855 * \param env - lu_env
1856 * \param iocb - kiocb from kernel
1857 * \param iter - user space buffers where the data will be copied
1859 * \retval - number of bytes have been read, or error code if error occurred.
1862 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1866 if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1869 /* NB: we can't do direct IO for fast read because it will need a lock
1870 * to make IO engine happy. */
1871 if (iocb->ki_filp->f_flags & O_DIRECT)
1874 result = generic_file_read_iter(iocb, iter);
1876 /* If the first page is not in cache, generic_file_aio_read() will be
1877 * returned with -ENODATA.
1878 * See corresponding code in ll_readpage(). */
1879 if (result == -ENODATA)
1883 ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
1884 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1885 LPROC_LL_READ_BYTES, result);
1892 * Read from a file (through the page cache).
1894 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1897 struct vvp_io_args *args;
1898 struct file *file = iocb->ki_filp;
1902 ktime_t kstart = ktime_get();
1905 if (!iov_iter_count(to))
1909 * Currently when PCC read failed, we do not fall back to the
1910 * normal read path, just return the error.
1911 * The resaon is that: for RW-PCC, the file data may be modified
1912 * in the PCC and inconsistent with the data on OSTs (or file
1913 * data has been removed from the Lustre file system), at this
1914 * time, fallback to the normal read path may read the wrong
1916 * TODO: for RO-PCC (readonly PCC), fall back to normal read
1917 * path: read data from data copy on OSTs.
1919 result = pcc_file_read_iter(iocb, to, &cached);
1923 ll_ras_enter(file, iocb->ki_pos, iov_iter_count(to));
1925 result = ll_do_fast_read(iocb, to);
1926 if (result < 0 || iov_iter_count(to) == 0)
1929 env = cl_env_get(&refcheck);
1931 return PTR_ERR(env);
1933 args = ll_env_args(env);
1934 args->u.normal.via_iter = to;
1935 args->u.normal.via_iocb = iocb;
1937 rc2 = ll_file_io_generic(env, args, file, CIT_READ,
1938 &iocb->ki_pos, iov_iter_count(to));
1941 else if (result == 0)
1944 cl_env_put(env, &refcheck);
1947 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
1948 file->private_data, iocb->ki_pos, result,
1950 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_READ,
1951 ktime_us_delta(ktime_get(), kstart));
1958 * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
1959 * If a page is already in the page cache and dirty (and some other things -
1960 * See ll_tiny_write_begin for the instantiation of these rules), then we can
1961 * write to it without doing a full I/O, because Lustre already knows about it
1962 * and will write it out. This saves a lot of processing time.
1964 * All writes here are within one page, so exclusion is handled by the page
1965 * lock on the vm page. We do not do tiny writes for writes which touch
1966 * multiple pages because it's very unlikely multiple sequential pages are
1967 * are already dirty.
1969 * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
1970 * and are unlikely to be to already dirty pages.
1972 * Attribute updates are important here, we do them in ll_tiny_write_end.
1974 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
1976 ssize_t count = iov_iter_count(iter);
1977 struct file *file = iocb->ki_filp;
1978 struct inode *inode = file_inode(file);
1979 bool lock_inode = !IS_NOSEC(inode);
1984 /* Restrict writes to single page and < PAGE_SIZE. See comment at top
1985 * of function for why.
1987 if (count >= PAGE_SIZE ||
1988 (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
1991 if (unlikely(lock_inode))
1993 result = __generic_file_write_iter(iocb, iter);
1995 if (unlikely(lock_inode))
1996 inode_unlock(inode);
1998 /* If the page is not already dirty, ll_tiny_write_begin returns
1999 * -ENODATA. We continue on to normal write.
2001 if (result == -ENODATA)
2005 ll_heat_add(inode, CIT_WRITE, result);
2006 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_WRITE_BYTES,
2008 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
2011 CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
2017 * Write to a file (through the page cache).
2019 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2021 struct vvp_io_args *args;
2023 ssize_t rc_tiny = 0, rc_normal;
2024 struct file *file = iocb->ki_filp;
2027 ktime_t kstart = ktime_get();
2032 if (!iov_iter_count(from))
2033 GOTO(out, rc_normal = 0);
2036 * When PCC write failed, we usually do not fall back to the normal
2037 * write path, just return the error. But there is a special case when
2038 * returned error code is -ENOSPC due to running out of space on PCC HSM
2039 * bakcend. At this time, it will fall back to normal I/O path and
2040 * retry the I/O. As the file is in HSM released state, it will restore
2041 * the file data to OSTs first and redo the write again. And the
2042 * restore process will revoke the layout lock and detach the file
2043 * from PCC cache automatically.
2045 result = pcc_file_write_iter(iocb, from, &cached);
2046 if (cached && result != -ENOSPC && result != -EDQUOT)
2047 GOTO(out, rc_normal = result);
2049 /* NB: we can't do direct IO for tiny writes because they use the page
2050 * cache, we can't do sync writes because tiny writes can't flush
2051 * pages, and we can't do append writes because we can't guarantee the
2052 * required DLM locks are held to protect file size.
2054 if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(file))) &&
2055 !(file->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
2056 rc_tiny = ll_do_tiny_write(iocb, from);
2058 /* In case of error, go on and try normal write - Only stop if tiny
2059 * write completed I/O.
2061 if (iov_iter_count(from) == 0)
2062 GOTO(out, rc_normal = rc_tiny);
2064 env = cl_env_get(&refcheck);
2066 return PTR_ERR(env);
2068 args = ll_env_args(env);
2069 args->u.normal.via_iter = from;
2070 args->u.normal.via_iocb = iocb;
2072 rc_normal = ll_file_io_generic(env, args, file, CIT_WRITE,
2073 &iocb->ki_pos, iov_iter_count(from));
2075 /* On success, combine bytes written. */
2076 if (rc_tiny >= 0 && rc_normal > 0)
2077 rc_normal += rc_tiny;
2078 /* On error, only return error from normal write if tiny write did not
2079 * write any bytes. Otherwise return bytes written by tiny write.
2081 else if (rc_tiny > 0)
2082 rc_normal = rc_tiny;
2084 cl_env_put(env, &refcheck);
2086 if (rc_normal > 0) {
2087 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
2088 file->private_data, iocb->ki_pos,
2090 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_WRITE,
2091 ktime_us_delta(ktime_get(), kstart));
2097 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
2099 * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
2101 static int ll_file_get_iov_count(const struct iovec *iov,
2102 unsigned long *nr_segs, size_t *count,
2108 for (seg = 0; seg < *nr_segs; seg++) {
2109 const struct iovec *iv = &iov[seg];
2112 * If any segment has a negative length, or the cumulative
2113 * length ever wraps negative then return -EINVAL.
2116 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
2118 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
2123 cnt -= iv->iov_len; /* This segment is no good */
2130 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2131 unsigned long nr_segs, loff_t pos)
2138 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_READ);
2145 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2146 iov_iter_init(&to, READ, iov, nr_segs, iov_count);
2147 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2148 iov_iter_init(&to, iov, nr_segs, iov_count, 0);
2149 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2151 result = ll_file_read_iter(iocb, &to);
2156 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
2159 struct iovec iov = { .iov_base = buf, .iov_len = count };
2168 init_sync_kiocb(&kiocb, file);
2169 kiocb.ki_pos = *ppos;
2170 #ifdef HAVE_KIOCB_KI_LEFT
2171 kiocb.ki_left = count;
2172 #elif defined(HAVE_KI_NBYTES)
2173 kiocb.i_nbytes = count;
2176 result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
2177 *ppos = kiocb.ki_pos;
2183 * Write to a file (through the page cache).
2186 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2187 unsigned long nr_segs, loff_t pos)
2189 struct iov_iter from;
2194 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_WRITE);
2201 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2202 iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
2203 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2204 iov_iter_init(&from, iov, nr_segs, iov_count, 0);
2205 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2207 result = ll_file_write_iter(iocb, &from);
2212 static ssize_t ll_file_write(struct file *file, const char __user *buf,
2213 size_t count, loff_t *ppos)
2215 struct iovec iov = { .iov_base = (void __user *)buf,
2225 init_sync_kiocb(&kiocb, file);
2226 kiocb.ki_pos = *ppos;
2227 #ifdef HAVE_KIOCB_KI_LEFT
2228 kiocb.ki_left = count;
2229 #elif defined(HAVE_KI_NBYTES)
2230 kiocb.ki_nbytes = count;
2233 result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
2234 *ppos = kiocb.ki_pos;
2238 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
2240 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
2241 __u64 flags, struct lov_user_md *lum, int lum_size)
2243 struct lookup_intent oit = {
2245 .it_flags = flags | MDS_OPEN_BY_FID,
2250 if ((__swab32(lum->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
2251 le32_to_cpu(LOV_MAGIC_MAGIC)) {
2252 /* this code will only exist for big-endian systems */
2253 lustre_swab_lov_user_md(lum, 0);
2256 ll_inode_size_lock(inode);
2257 rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
2259 GOTO(out_unlock, rc);
2261 ll_release_openhandle(dentry, &oit);
2264 ll_inode_size_unlock(inode);
2265 ll_intent_release(&oit);
2270 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
2271 struct lov_mds_md **lmmp, int *lmm_size,
2272 struct ptlrpc_request **request)
2274 struct ll_sb_info *sbi = ll_i2sbi(inode);
2275 struct mdt_body *body;
2276 struct lov_mds_md *lmm = NULL;
2277 struct ptlrpc_request *req = NULL;
2278 struct md_op_data *op_data;
2283 rc = ll_get_default_mdsize(sbi, &lmmsize);
2287 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
2288 strlen(filename), lmmsize,
2289 LUSTRE_OPC_ANY, NULL);
2290 if (IS_ERR(op_data))
2291 RETURN(PTR_ERR(op_data));
2293 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
2294 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
2295 ll_finish_md_op_data(op_data);
2297 CDEBUG(D_INFO, "md_getattr_name failed "
2298 "on %s: rc %d\n", filename, rc);
2302 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2303 LASSERT(body != NULL); /* checked by mdc_getattr_name */
2305 lmmsize = body->mbo_eadatasize;
2307 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
2309 GOTO(out, rc = -ENODATA);
2311 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
2312 LASSERT(lmm != NULL);
2314 if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
2315 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
2316 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1) &&
2317 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_FOREIGN))
2318 GOTO(out, rc = -EPROTO);
2321 * This is coming from the MDS, so is probably in
2322 * little endian. We convert it to host endian before
2323 * passing it to userspace.
2325 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) {
2326 int stripe_count = 0;
2328 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
2329 lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2330 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
2331 if (le32_to_cpu(lmm->lmm_pattern) &
2332 LOV_PATTERN_F_RELEASED)
2334 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
2336 /* if function called for directory - we should
2337 * avoid swab not existent lsm objects
2339 if (lmm->lmm_magic == LOV_MAGIC_V1 &&
2340 S_ISREG(body->mbo_mode))
2341 lustre_swab_lov_user_md_objects(
2342 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
2344 else if (lmm->lmm_magic == LOV_MAGIC_V3 &&
2345 S_ISREG(body->mbo_mode))
2346 lustre_swab_lov_user_md_objects(
2347 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
2349 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1)) {
2350 lustre_swab_lov_comp_md_v1(
2351 (struct lov_comp_md_v1 *)lmm);
2355 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
2356 struct lov_comp_md_v1 *comp_v1 = NULL;
2357 struct lov_comp_md_entry_v1 *ent;
2358 struct lov_user_md_v1 *v1;
2362 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2363 /* Dump the striping information */
2364 for (; i < comp_v1->lcm_entry_count; i++) {
2365 ent = &comp_v1->lcm_entries[i];
2366 off = ent->lcme_offset;
2367 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2369 "comp[%d]: stripe_count=%u, stripe_size=%u\n",
2370 i, v1->lmm_stripe_count, v1->lmm_stripe_size);
2374 * Return valid stripe_count and stripe_size instead of 0 for
2375 * DoM files to avoid divide-by-zero for older userspace that
2376 * calls this ioctl, e.g. lustre ADIO driver.
2378 if (lmm->lmm_stripe_count == 0)
2379 lmm->lmm_stripe_count = 1;
2380 if (lmm->lmm_stripe_size == 0) {
2381 /* Since the first component of the file data is placed
2382 * on the MDT for faster access, the stripe_size of the
2383 * second one is always that applications which are
2386 if (lmm->lmm_pattern == LOV_PATTERN_MDT)
2387 i = comp_v1->lcm_entry_count > 1 ? 1 : 0;
2389 i = comp_v1->lcm_entry_count > 1 ?
2390 comp_v1->lcm_entry_count - 1 : 0;
2391 ent = &comp_v1->lcm_entries[i];
2392 off = ent->lcme_offset;
2393 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2394 lmm->lmm_stripe_size = v1->lmm_stripe_size;
2399 *lmm_size = lmmsize;
2404 static int ll_lov_setea(struct inode *inode, struct file *file,
2407 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
2408 struct lov_user_md *lump;
2409 int lum_size = sizeof(struct lov_user_md) +
2410 sizeof(struct lov_user_ost_data);
2414 if (!capable(CAP_SYS_ADMIN))
2417 OBD_ALLOC_LARGE(lump, lum_size);
2421 if (copy_from_user(lump, arg, lum_size))
2422 GOTO(out_lump, rc = -EFAULT);
2424 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
2426 cl_lov_delay_create_clear(&file->f_flags);
2429 OBD_FREE_LARGE(lump, lum_size);
2433 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
2440 env = cl_env_get(&refcheck);
2442 RETURN(PTR_ERR(env));
2444 rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
2445 cl_env_put(env, &refcheck);
2449 static int ll_lov_setstripe(struct inode *inode, struct file *file,
2452 struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
2453 struct lov_user_md *klum;
2455 __u64 flags = FMODE_WRITE;
2458 rc = ll_copy_user_md(lum, &klum);
2463 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2468 rc = put_user(0, &lum->lmm_stripe_count);
2472 rc = ll_layout_refresh(inode, &gen);
2476 rc = ll_file_getstripe(inode, arg, lum_size);
2477 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode) &&
2478 ll_i2info(inode)->lli_clob) {
2479 struct iattr attr = { 0 };
2481 rc = cl_setattr_ost(ll_i2info(inode)->lli_clob, &attr,
2482 OP_XVALID_FLAGS, LUSTRE_ENCRYPT_FL);
2485 cl_lov_delay_create_clear(&file->f_flags);
2488 OBD_FREE_LARGE(klum, lum_size);
2494 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2496 struct ll_inode_info *lli = ll_i2info(inode);
2497 struct cl_object *obj = lli->lli_clob;
2498 struct ll_file_data *fd = file->private_data;
2499 struct ll_grouplock grouplock;
2504 CWARN("group id for group lock must not be 0\n");
2508 if (ll_file_nolock(file))
2509 RETURN(-EOPNOTSUPP);
2511 if (file->f_flags & O_NONBLOCK) {
2512 if (!mutex_trylock(&lli->lli_group_mutex))
2515 mutex_lock(&lli->lli_group_mutex);
2517 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2518 CWARN("group lock already existed with gid %lu\n",
2519 fd->fd_grouplock.lg_gid);
2520 GOTO(out, rc = -EINVAL);
2522 if (arg != lli->lli_group_gid && lli->lli_group_users != 0) {
2523 if (file->f_flags & O_NONBLOCK)
2524 GOTO(out, rc = -EAGAIN);
2525 mutex_unlock(&lli->lli_group_mutex);
2526 wait_var_event(&lli->lli_group_users, !lli->lli_group_users);
2527 GOTO(retry, rc = 0);
2529 LASSERT(fd->fd_grouplock.lg_lock == NULL);
2532 * XXX: group lock needs to protect all OST objects while PFL
2533 * can add new OST objects during the IO, so we'd instantiate
2534 * all OST objects before getting its group lock.
2539 struct cl_layout cl = {
2540 .cl_is_composite = false,
2542 struct lu_extent ext = {
2544 .e_end = OBD_OBJECT_EOF,
2547 env = cl_env_get(&refcheck);
2549 GOTO(out, rc = PTR_ERR(env));
2551 rc = cl_object_layout_get(env, obj, &cl);
2552 if (rc >= 0 && cl.cl_is_composite)
2553 rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2556 cl_env_put(env, &refcheck);
2561 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2562 arg, (file->f_flags & O_NONBLOCK), &grouplock);
2567 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2568 fd->fd_grouplock = grouplock;
2569 if (lli->lli_group_users == 0)
2570 lli->lli_group_gid = grouplock.lg_gid;
2571 lli->lli_group_users++;
2573 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2575 mutex_unlock(&lli->lli_group_mutex);
2580 static int ll_put_grouplock(struct inode *inode, struct file *file,
2583 struct ll_inode_info *lli = ll_i2info(inode);
2584 struct ll_file_data *fd = file->private_data;
2585 struct ll_grouplock grouplock;
2589 mutex_lock(&lli->lli_group_mutex);
2590 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2591 CWARN("no group lock held\n");
2592 GOTO(out, rc = -EINVAL);
2595 LASSERT(fd->fd_grouplock.lg_lock != NULL);
2597 if (fd->fd_grouplock.lg_gid != arg) {
2598 CWARN("group lock %lu doesn't match current id %lu\n",
2599 arg, fd->fd_grouplock.lg_gid);
2600 GOTO(out, rc = -EINVAL);
2603 grouplock = fd->fd_grouplock;
2604 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2605 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2607 cl_put_grouplock(&grouplock);
2609 lli->lli_group_users--;
2610 if (lli->lli_group_users == 0) {
2611 lli->lli_group_gid = 0;
2612 wake_up_var(&lli->lli_group_users);
2614 CDEBUG(D_INFO, "group lock %lu released\n", arg);
2617 mutex_unlock(&lli->lli_group_mutex);
2623 * Close inode open handle
2625 * \param dentry [in] dentry which contains the inode
2626 * \param it [in,out] intent which contains open info and result
2629 * \retval <0 failure
2631 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2633 struct inode *inode = dentry->d_inode;
2634 struct obd_client_handle *och;
2640 /* Root ? Do nothing. */
2641 if (is_root_inode(inode))
2644 /* No open handle to close? Move away */
2645 if (!it_disposition(it, DISP_OPEN_OPEN))
2648 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2650 OBD_ALLOC(och, sizeof(*och));
2652 GOTO(out, rc = -ENOMEM);
2654 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2658 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2660 /* this one is in place of ll_file_open */
2661 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2662 ptlrpc_req_finished(it->it_request);
2663 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2669 * Get size for inode for which FIEMAP mapping is requested.
2670 * Make the FIEMAP get_info call and returns the result.
2671 * \param fiemap kernel buffer to hold extens
2672 * \param num_bytes kernel buffer size
2674 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2680 struct ll_fiemap_info_key fmkey = { .lfik_name = KEY_FIEMAP, };
2683 /* Checks for fiemap flags */
2684 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2685 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2689 /* Check for FIEMAP_FLAG_SYNC */
2690 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2691 rc = filemap_fdatawrite(inode->i_mapping);
2696 env = cl_env_get(&refcheck);
2698 RETURN(PTR_ERR(env));
2700 if (i_size_read(inode) == 0) {
2701 rc = ll_glimpse_size(inode);
2706 fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2707 obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2708 obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2710 /* If filesize is 0, then there would be no objects for mapping */
2711 if (fmkey.lfik_oa.o_size == 0) {
2712 fiemap->fm_mapped_extents = 0;
2716 fmkey.lfik_fiemap = *fiemap;
2718 rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2719 &fmkey, fiemap, &num_bytes);
2721 cl_env_put(env, &refcheck);
2725 int ll_fid2path(struct inode *inode, void __user *arg)
2727 struct obd_export *exp = ll_i2mdexp(inode);
2728 const struct getinfo_fid2path __user *gfin = arg;
2730 struct getinfo_fid2path *gfout;
2736 if (!capable(CAP_DAC_READ_SEARCH) &&
2737 !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2740 /* Only need to get the buflen */
2741 if (get_user(pathlen, &gfin->gf_pathlen))
2744 if (pathlen > PATH_MAX)
2747 outsize = sizeof(*gfout) + pathlen;
2748 OBD_ALLOC(gfout, outsize);
2752 if (copy_from_user(gfout, arg, sizeof(*gfout)))
2753 GOTO(gf_free, rc = -EFAULT);
2754 /* append root FID after gfout to let MDT know the root FID so that it
2755 * can lookup the correct path, this is mainly for fileset.
2756 * old server without fileset mount support will ignore this. */
2757 *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
2759 /* Call mdc_iocontrol */
2760 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
2764 if (copy_to_user(arg, gfout, outsize))
2768 OBD_FREE(gfout, outsize);
2773 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
2775 struct cl_object *obj = ll_i2info(inode)->lli_clob;
2783 ioc->idv_version = 0;
2784 ioc->idv_layout_version = UINT_MAX;
2786 /* If no file object initialized, we consider its version is 0. */
2790 env = cl_env_get(&refcheck);
2792 RETURN(PTR_ERR(env));
2794 io = vvp_env_thread_io(env);
2796 io->u.ci_data_version.dv_data_version = 0;
2797 io->u.ci_data_version.dv_layout_version = UINT_MAX;
2798 io->u.ci_data_version.dv_flags = ioc->idv_flags;
2801 if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
2802 result = cl_io_loop(env, io);
2804 result = io->ci_result;
2806 ioc->idv_version = io->u.ci_data_version.dv_data_version;
2807 ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
2809 cl_io_fini(env, io);
2811 if (unlikely(io->ci_need_restart))
2814 cl_env_put(env, &refcheck);
2820 * Read the data_version for inode.
2822 * This value is computed using stripe object version on OST.
2823 * Version is computed using server side locking.
2825 * @param flags if do sync on the OST side;
2827 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
2828 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
2830 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
2832 struct ioc_data_version ioc = { .idv_flags = flags };
2835 rc = ll_ioc_data_version(inode, &ioc);
2837 *data_version = ioc.idv_version;
2843 * Trigger a HSM release request for the provided inode.
2845 int ll_hsm_release(struct inode *inode)
2848 struct obd_client_handle *och = NULL;
2849 __u64 data_version = 0;
2854 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2855 ll_i2sbi(inode)->ll_fsname,
2856 PFID(&ll_i2info(inode)->lli_fid));
2858 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2860 GOTO(out, rc = PTR_ERR(och));
2862 /* Grab latest data_version and [am]time values */
2863 rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
2867 env = cl_env_get(&refcheck);
2869 GOTO(out, rc = PTR_ERR(env));
2871 rc = ll_merge_attr(env, inode);
2872 cl_env_put(env, &refcheck);
2874 /* If error happen, we have the wrong size for a file.
2880 /* Release the file.
2881 * NB: lease lock handle is released in mdc_hsm_release_pack() because
2882 * we still need it to pack l_remote_handle to MDT. */
2883 rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
2889 if (och != NULL && !IS_ERR(och)) /* close the file */
2890 ll_lease_close(och, inode, NULL);
2895 struct ll_swap_stack {
2898 struct inode *inode1;
2899 struct inode *inode2;
2904 static int ll_swap_layouts(struct file *file1, struct file *file2,
2905 struct lustre_swap_layouts *lsl)
2907 struct mdc_swap_layouts msl;
2908 struct md_op_data *op_data;
2911 struct ll_swap_stack *llss = NULL;
2914 OBD_ALLOC_PTR(llss);
2918 llss->inode1 = file_inode(file1);
2919 llss->inode2 = file_inode(file2);
2921 rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
2925 /* we use 2 bool because it is easier to swap than 2 bits */
2926 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2927 llss->check_dv1 = true;
2929 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2930 llss->check_dv2 = true;
2932 /* we cannot use lsl->sl_dvX directly because we may swap them */
2933 llss->dv1 = lsl->sl_dv1;
2934 llss->dv2 = lsl->sl_dv2;
2936 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2937 if (rc == 0) /* same file, done! */
2940 if (rc < 0) { /* sequentialize it */
2941 swap(llss->inode1, llss->inode2);
2943 swap(llss->dv1, llss->dv2);
2944 swap(llss->check_dv1, llss->check_dv2);
2948 if (gid != 0) { /* application asks to flush dirty cache */
2949 rc = ll_get_grouplock(llss->inode1, file1, gid);
2953 rc = ll_get_grouplock(llss->inode2, file2, gid);
2955 ll_put_grouplock(llss->inode1, file1, gid);
2960 /* ultimate check, before swaping the layouts we check if
2961 * dataversion has changed (if requested) */
2962 if (llss->check_dv1) {
2963 rc = ll_data_version(llss->inode1, &dv, 0);
2966 if (dv != llss->dv1)
2967 GOTO(putgl, rc = -EAGAIN);
2970 if (llss->check_dv2) {
2971 rc = ll_data_version(llss->inode2, &dv, 0);
2974 if (dv != llss->dv2)
2975 GOTO(putgl, rc = -EAGAIN);
2978 /* struct md_op_data is used to send the swap args to the mdt
2979 * only flags is missing, so we use struct mdc_swap_layouts
2980 * through the md_op_data->op_data */
2981 /* flags from user space have to be converted before they are send to
2982 * server, no flag is sent today, they are only used on the client */
2985 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
2986 0, LUSTRE_OPC_ANY, &msl);
2987 if (IS_ERR(op_data))
2988 GOTO(free, rc = PTR_ERR(op_data));
2990 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
2991 sizeof(*op_data), op_data, NULL);
2992 ll_finish_md_op_data(op_data);
2999 ll_put_grouplock(llss->inode2, file2, gid);
3000 ll_put_grouplock(llss->inode1, file1, gid);
3010 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
3012 struct obd_export *exp = ll_i2mdexp(inode);
3013 struct md_op_data *op_data;
3017 /* Detect out-of range masks */
3018 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
3021 /* Non-root users are forbidden to set or clear flags which are
3022 * NOT defined in HSM_USER_MASK. */
3023 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
3024 !capable(CAP_SYS_ADMIN))
3027 if (!exp_connect_archive_id_array(exp)) {
3028 /* Detect out-of range archive id */
3029 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
3030 (hss->hss_archive_id > LL_HSM_ORIGIN_MAX_ARCHIVE))
3034 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3035 LUSTRE_OPC_ANY, hss);
3036 if (IS_ERR(op_data))
3037 RETURN(PTR_ERR(op_data));
3039 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, exp, sizeof(*op_data),
3042 ll_finish_md_op_data(op_data);
3047 static int ll_hsm_import(struct inode *inode, struct file *file,
3048 struct hsm_user_import *hui)
3050 struct hsm_state_set *hss = NULL;
3051 struct iattr *attr = NULL;
3055 if (!S_ISREG(inode->i_mode))
3061 GOTO(out, rc = -ENOMEM);
3063 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
3064 hss->hss_archive_id = hui->hui_archive_id;
3065 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
3066 rc = ll_hsm_state_set(inode, hss);
3070 OBD_ALLOC_PTR(attr);
3072 GOTO(out, rc = -ENOMEM);
3074 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
3075 attr->ia_mode |= S_IFREG;
3076 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
3077 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
3078 attr->ia_size = hui->hui_size;
3079 attr->ia_mtime.tv_sec = hui->hui_mtime;
3080 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
3081 attr->ia_atime.tv_sec = hui->hui_atime;
3082 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
3084 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
3085 ATTR_UID | ATTR_GID |
3086 ATTR_MTIME | ATTR_MTIME_SET |
3087 ATTR_ATIME | ATTR_ATIME_SET;
3091 rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
3095 inode_unlock(inode);
3107 static inline long ll_lease_type_from_fmode(fmode_t fmode)
3109 return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
3110 ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
3113 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
3115 struct inode *inode = file_inode(file);
3117 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
3118 ATTR_MTIME | ATTR_MTIME_SET |
3121 .tv_sec = lfu->lfu_atime_sec,
3122 .tv_nsec = lfu->lfu_atime_nsec,
3125 .tv_sec = lfu->lfu_mtime_sec,
3126 .tv_nsec = lfu->lfu_mtime_nsec,
3129 .tv_sec = lfu->lfu_ctime_sec,
3130 .tv_nsec = lfu->lfu_ctime_nsec,
3136 if (!capable(CAP_SYS_ADMIN))
3139 if (!S_ISREG(inode->i_mode))
3143 rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
3145 inode_unlock(inode);
3150 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
3153 case MODE_READ_USER:
3155 case MODE_WRITE_USER:
3162 static const char *const user_lockname[] = LOCK_MODE_NAMES;
3164 /* Used to allow the upper layers of the client to request an LDLM lock
3165 * without doing an actual read or write.
3167 * Used for ladvise lockahead to manually request specific locks.
3169 * \param[in] file file this ladvise lock request is on
3170 * \param[in] ladvise ladvise struct describing this lock request
3172 * \retval 0 success, no detailed result available (sync requests
3173 * and requests sent to the server [not handled locally]
3174 * cannot return detailed results)
3175 * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
3176 * see definitions for details.
3177 * \retval negative negative errno on error
3179 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
3181 struct lu_env *env = NULL;
3182 struct cl_io *io = NULL;
3183 struct cl_lock *lock = NULL;
3184 struct cl_lock_descr *descr = NULL;
3185 struct dentry *dentry = file->f_path.dentry;
3186 struct inode *inode = dentry->d_inode;
3187 enum cl_lock_mode cl_mode;
3188 off_t start = ladvise->lla_start;
3189 off_t end = ladvise->lla_end;
3196 "Lock request: file=%pd, inode=%p, mode=%s start=%llu, end=%llu\n",
3197 dentry, dentry->d_inode,
3198 user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
3201 cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
3203 GOTO(out, result = cl_mode);
3205 /* Get IO environment */
3206 result = cl_io_get(inode, &env, &io, &refcheck);
3210 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
3213 * nothing to do for this io. This currently happens when
3214 * stripe sub-object's are not yet created.
3216 result = io->ci_result;
3217 } else if (result == 0) {
3218 lock = vvp_env_lock(env);
3219 descr = &lock->cll_descr;
3221 descr->cld_obj = io->ci_obj;
3222 /* Convert byte offsets to pages */
3223 descr->cld_start = cl_index(io->ci_obj, start);
3224 descr->cld_end = cl_index(io->ci_obj, end);
3225 descr->cld_mode = cl_mode;
3226 /* CEF_MUST is used because we do not want to convert a
3227 * lockahead request to a lockless lock */
3228 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND;
3230 if (ladvise->lla_peradvice_flags & LF_ASYNC)
3231 descr->cld_enq_flags |= CEF_SPECULATIVE;
3233 result = cl_lock_request(env, io, lock);
3235 /* On success, we need to release the lock */
3237 cl_lock_release(env, lock);
3239 cl_io_fini(env, io);
3240 cl_env_put(env, &refcheck);
3242 /* -ECANCELED indicates a matching lock with a different extent
3243 * was already present, and -EEXIST indicates a matching lock
3244 * on exactly the same extent was already present.
3245 * We convert them to positive values for userspace to make
3246 * recognizing true errors easier.
3247 * Note we can only return these detailed results on async requests,
3248 * as sync requests look the same as i/o requests for locking. */
3249 if (result == -ECANCELED)
3250 result = LLA_RESULT_DIFFERENT;
3251 else if (result == -EEXIST)
3252 result = LLA_RESULT_SAME;
3257 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
3259 static int ll_ladvise_sanity(struct inode *inode,
3260 struct llapi_lu_ladvise *ladvise)
3262 struct ll_sb_info *sbi = ll_i2sbi(inode);
3263 enum lu_ladvise_type advice = ladvise->lla_advice;
3264 /* Note the peradvice flags is a 32 bit field, so per advice flags must
3265 * be in the first 32 bits of enum ladvise_flags */
3266 __u32 flags = ladvise->lla_peradvice_flags;
3267 /* 3 lines at 80 characters per line, should be plenty */
3270 if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
3273 "%s: advice with value '%d' not recognized, last supported advice is %s (value '%d'): rc = %d\n",
3274 sbi->ll_fsname, advice,
3275 ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
3279 /* Per-advice checks */
3281 case LU_LADVISE_LOCKNOEXPAND:
3282 if (flags & ~LF_LOCKNOEXPAND_MASK) {
3284 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3285 "rc = %d\n", sbi->ll_fsname, flags,
3286 ladvise_names[advice], rc);
3290 case LU_LADVISE_LOCKAHEAD:
3291 /* Currently only READ and WRITE modes can be requested */
3292 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
3293 ladvise->lla_lockahead_mode == 0) {
3295 CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
3296 "rc = %d\n", sbi->ll_fsname,
3297 ladvise->lla_lockahead_mode,
3298 ladvise_names[advice], rc);
3302 case LU_LADVISE_WILLREAD:
3303 case LU_LADVISE_DONTNEED:
3305 /* Note fall through above - These checks apply to all advices
3306 * except LOCKNOEXPAND */
3307 if (flags & ~LF_DEFAULT_MASK) {
3309 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3310 "rc = %d\n", sbi->ll_fsname, flags,
3311 ladvise_names[advice], rc);
3314 if (ladvise->lla_start >= ladvise->lla_end) {
3316 CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
3317 "for %s: rc = %d\n", sbi->ll_fsname,
3318 ladvise->lla_start, ladvise->lla_end,
3319 ladvise_names[advice], rc);
3331 * Give file access advices
3333 * The ladvise interface is similar to Linux fadvise() system call, except it
3334 * forwards the advices directly from Lustre client to server. The server side
3335 * codes will apply appropriate read-ahead and caching techniques for the
3336 * corresponding files.
3338 * A typical workload for ladvise is e.g. a bunch of different clients are
3339 * doing small random reads of a file, so prefetching pages into OSS cache
3340 * with big linear reads before the random IO is a net benefit. Fetching
3341 * all that data into each client cache with fadvise() may not be, due to
3342 * much more data being sent to the client.
3344 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
3345 struct llapi_lu_ladvise *ladvise)
3349 struct cl_ladvise_io *lio;
3354 env = cl_env_get(&refcheck);
3356 RETURN(PTR_ERR(env));
3358 io = vvp_env_thread_io(env);
3359 io->ci_obj = ll_i2info(inode)->lli_clob;
3361 /* initialize parameters for ladvise */
3362 lio = &io->u.ci_ladvise;
3363 lio->li_start = ladvise->lla_start;
3364 lio->li_end = ladvise->lla_end;
3365 lio->li_fid = ll_inode2fid(inode);
3366 lio->li_advice = ladvise->lla_advice;
3367 lio->li_flags = flags;
3369 if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
3370 rc = cl_io_loop(env, io);
3374 cl_io_fini(env, io);
3375 cl_env_put(env, &refcheck);
3379 static int ll_lock_noexpand(struct file *file, int flags)
3381 struct ll_file_data *fd = file->private_data;
3383 fd->ll_lock_no_expand = !(flags & LF_UNSET);
3388 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
3391 struct fsxattr fsxattr;
3393 if (copy_from_user(&fsxattr,
3394 (const struct fsxattr __user *)arg,
3398 fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
3399 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
3400 fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
3401 fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
3402 if (copy_to_user((struct fsxattr __user *)arg,
3403 &fsxattr, sizeof(fsxattr)))
3409 int ll_ioctl_check_project(struct inode *inode, __u32 xflags,
3413 * Project Quota ID state is only allowed to change from within the init
3414 * namespace. Enforce that restriction only if we are trying to change
3415 * the quota ID state. Everything else is allowed in user namespaces.
3417 if (current_user_ns() == &init_user_ns) {
3419 * Caller is allowed to change the project ID. if it is being
3420 * changed, make sure that the new value is valid.
3422 if (ll_i2info(inode)->lli_projid != projid &&
3423 !projid_valid(make_kprojid(&init_user_ns, projid)))
3429 if (ll_i2info(inode)->lli_projid != projid)
3432 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags)) {
3433 if (!(xflags & FS_XFLAG_PROJINHERIT))
3436 if (xflags & FS_XFLAG_PROJINHERIT)
3443 static int ll_set_project(struct inode *inode, __u32 xflags, __u32 projid)
3445 struct md_op_data *op_data;
3446 struct ptlrpc_request *req = NULL;
3447 struct cl_object *obj;
3448 unsigned int inode_flags;
3451 rc = ll_ioctl_check_project(inode, xflags, projid);
3455 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3456 LUSTRE_OPC_ANY, NULL);
3457 if (IS_ERR(op_data))
3458 RETURN(PTR_ERR(op_data));
3460 inode_flags = ll_xflags_to_inode_flags(xflags);
3461 op_data->op_attr_flags = ll_inode_to_ext_flags(inode_flags);
3462 if (xflags & FS_XFLAG_PROJINHERIT)
3463 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
3464 op_data->op_projid = projid;
3465 op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
3466 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL, 0, &req);
3467 ptlrpc_req_finished(req);
3469 GOTO(out_fsxattr, rc);
3470 ll_update_inode_flags(inode, op_data->op_attr_flags);
3472 /* Avoid OST RPC if this is only ioctl setting project inherit flag */
3473 if (xflags == 0 || xflags == FS_XFLAG_PROJINHERIT)
3474 GOTO(out_fsxattr, rc);
3476 obj = ll_i2info(inode)->lli_clob;
3478 struct iattr attr = { 0 };
3480 rc = cl_setattr_ost(obj, &attr, OP_XVALID_FLAGS, xflags);
3484 ll_finish_md_op_data(op_data);
3488 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
3491 struct fsxattr fsxattr;
3495 if (copy_from_user(&fsxattr,
3496 (const struct fsxattr __user *)arg,
3500 RETURN(ll_set_project(inode, fsxattr.fsx_xflags,
3501 fsxattr.fsx_projid));
3504 int ll_ioctl_project(struct file *file, unsigned int cmd,
3507 struct lu_project lu_project;
3508 struct dentry *dentry = file_dentry(file);
3509 struct inode *inode = file_inode(file);
3510 struct dentry *child_dentry = NULL;
3511 int rc = 0, name_len;
3513 if (copy_from_user(&lu_project,
3514 (const struct lu_project __user *)arg,
3515 sizeof(lu_project)))
3518 /* apply child dentry if name is valid */
3519 name_len = strnlen(lu_project.project_name, NAME_MAX);
3520 if (name_len > 0 && name_len <= NAME_MAX) {
3522 child_dentry = lookup_one_len(lu_project.project_name,
3524 inode_unlock(inode);
3525 if (IS_ERR(child_dentry)) {
3526 rc = PTR_ERR(child_dentry);
3529 inode = child_dentry->d_inode;
3534 } else if (name_len > NAME_MAX) {
3539 switch (lu_project.project_type) {
3540 case LU_PROJECT_SET:
3541 rc = ll_set_project(inode, lu_project.project_xflags,
3542 lu_project.project_id);
3544 case LU_PROJECT_GET:
3545 lu_project.project_xflags =
3546 ll_inode_flags_to_xflags(inode->i_flags);
3547 if (test_bit(LLIF_PROJECT_INHERIT,
3548 &ll_i2info(inode)->lli_flags))
3549 lu_project.project_xflags |= FS_XFLAG_PROJINHERIT;
3550 lu_project.project_id = ll_i2info(inode)->lli_projid;
3551 if (copy_to_user((struct lu_project __user *)arg,
3552 &lu_project, sizeof(lu_project))) {
3562 if (!IS_ERR_OR_NULL(child_dentry))
3567 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
3570 struct inode *inode = file_inode(file);
3571 struct ll_file_data *fd = file->private_data;
3572 struct ll_inode_info *lli = ll_i2info(inode);
3573 struct obd_client_handle *och = NULL;
3574 struct split_param sp;
3575 struct pcc_param param;
3576 bool lease_broken = false;
3578 enum mds_op_bias bias = 0;
3579 struct file *layout_file = NULL;
3581 size_t data_size = 0;
3582 bool attached = false;
3587 mutex_lock(&lli->lli_och_mutex);
3588 if (fd->fd_lease_och != NULL) {
3589 och = fd->fd_lease_och;
3590 fd->fd_lease_och = NULL;
3592 mutex_unlock(&lli->lli_och_mutex);
3597 fmode = och->och_flags;
3599 switch (ioc->lil_flags) {
3600 case LL_LEASE_RESYNC_DONE:
3601 if (ioc->lil_count > IOC_IDS_MAX)
3602 GOTO(out_lease_close, rc = -EINVAL);
3604 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
3605 OBD_ALLOC(data, data_size);
3607 GOTO(out_lease_close, rc = -ENOMEM);
3609 if (copy_from_user(data, (void __user *)arg, data_size))
3610 GOTO(out_lease_close, rc = -EFAULT);
3612 bias = MDS_CLOSE_RESYNC_DONE;
3614 case LL_LEASE_LAYOUT_MERGE: {
3617 if (ioc->lil_count != 1)
3618 GOTO(out_lease_close, rc = -EINVAL);
3620 arg += sizeof(*ioc);
3621 if (copy_from_user(&fd, (void __user *)arg, sizeof(__u32)))
3622 GOTO(out_lease_close, rc = -EFAULT);
3624 layout_file = fget(fd);
3626 GOTO(out_lease_close, rc = -EBADF);
3628 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
3629 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
3630 GOTO(out_lease_close, rc = -EPERM);
3632 data = file_inode(layout_file);
3633 bias = MDS_CLOSE_LAYOUT_MERGE;
3636 case LL_LEASE_LAYOUT_SPLIT: {
3640 if (ioc->lil_count != 2)
3641 GOTO(out_lease_close, rc = -EINVAL);
3643 arg += sizeof(*ioc);
3644 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3645 GOTO(out_lease_close, rc = -EFAULT);
3647 arg += sizeof(__u32);
3648 if (copy_from_user(&mirror_id, (void __user *)arg,
3650 GOTO(out_lease_close, rc = -EFAULT);
3652 layout_file = fget(fdv);
3654 GOTO(out_lease_close, rc = -EBADF);
3656 /* if layout_file == file, it means to destroy the mirror */
3657 sp.sp_inode = file_inode(layout_file);
3658 sp.sp_mirror_id = (__u16)mirror_id;
3660 bias = MDS_CLOSE_LAYOUT_SPLIT;
3663 case LL_LEASE_PCC_ATTACH:
3664 if (ioc->lil_count != 1)
3667 arg += sizeof(*ioc);
3668 if (copy_from_user(¶m.pa_archive_id, (void __user *)arg,
3670 GOTO(out_lease_close, rc2 = -EFAULT);
3672 rc2 = pcc_readwrite_attach(file, inode, param.pa_archive_id);
3674 GOTO(out_lease_close, rc2);
3677 /* Grab latest data version */
3678 rc2 = ll_data_version(inode, ¶m.pa_data_version,
3681 GOTO(out_lease_close, rc2);
3684 bias = MDS_PCC_ATTACH;
3687 /* without close intent */
3692 rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
3696 rc = ll_lease_och_release(inode, file);
3705 switch (ioc->lil_flags) {
3706 case LL_LEASE_RESYNC_DONE:
3708 OBD_FREE(data, data_size);
3710 case LL_LEASE_LAYOUT_MERGE:
3711 case LL_LEASE_LAYOUT_SPLIT:
3715 ll_layout_refresh(inode, &fd->fd_layout_version);
3717 case LL_LEASE_PCC_ATTACH:
3720 rc = pcc_readwrite_attach_fini(file, inode,
3721 param.pa_layout_gen,
3728 rc = ll_lease_type_from_fmode(fmode);
3732 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
3735 struct inode *inode = file_inode(file);
3736 struct ll_inode_info *lli = ll_i2info(inode);
3737 struct ll_file_data *fd = file->private_data;
3738 struct obd_client_handle *och = NULL;
3739 __u64 open_flags = 0;
3745 switch (ioc->lil_mode) {
3746 case LL_LEASE_WRLCK:
3747 if (!(file->f_mode & FMODE_WRITE))
3749 fmode = FMODE_WRITE;
3751 case LL_LEASE_RDLCK:
3752 if (!(file->f_mode & FMODE_READ))
3756 case LL_LEASE_UNLCK:
3757 RETURN(ll_file_unlock_lease(file, ioc, arg));
3762 CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
3764 /* apply for lease */
3765 if (ioc->lil_flags & LL_LEASE_RESYNC)
3766 open_flags = MDS_OPEN_RESYNC;
3767 och = ll_lease_open(inode, file, fmode, open_flags);
3769 RETURN(PTR_ERR(och));
3771 if (ioc->lil_flags & LL_LEASE_RESYNC) {
3772 rc = ll_lease_file_resync(och, inode, arg);
3774 ll_lease_close(och, inode, NULL);
3777 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
3779 ll_lease_close(och, inode, NULL);
3785 mutex_lock(&lli->lli_och_mutex);
3786 if (fd->fd_lease_och == NULL) {
3787 fd->fd_lease_och = och;
3790 mutex_unlock(&lli->lli_och_mutex);
3792 /* impossible now that only excl is supported for now */
3793 ll_lease_close(och, inode, &lease_broken);
3799 static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
3801 struct ll_inode_info *lli = ll_i2info(inode);
3802 struct ll_sb_info *sbi = ll_i2sbi(inode);
3803 __u64 now = ktime_get_real_seconds();
3806 spin_lock(&lli->lli_heat_lock);
3807 heat->lh_flags = lli->lli_heat_flags;
3808 for (i = 0; i < heat->lh_count; i++)
3809 heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
3810 now, sbi->ll_heat_decay_weight,
3811 sbi->ll_heat_period_second);
3812 spin_unlock(&lli->lli_heat_lock);
3815 static int ll_heat_set(struct inode *inode, enum lu_heat_flag flags)
3817 struct ll_inode_info *lli = ll_i2info(inode);
3820 spin_lock(&lli->lli_heat_lock);
3821 if (flags & LU_HEAT_FLAG_CLEAR)
3822 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
3824 if (flags & LU_HEAT_FLAG_OFF)
3825 lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
3827 lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
3829 spin_unlock(&lli->lli_heat_lock);
3835 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3837 struct inode *inode = file_inode(file);
3838 struct ll_file_data *fd = file->private_data;
3842 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
3843 PFID(ll_inode2fid(inode)), inode, cmd);
3844 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
3846 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
3847 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
3851 case LL_IOC_GETFLAGS:
3852 /* Get the current value of the file flags */
3853 return put_user(fd->fd_flags, (int __user *)arg);
3854 case LL_IOC_SETFLAGS:
3855 case LL_IOC_CLRFLAGS:
3856 /* Set or clear specific file flags */
3857 /* XXX This probably needs checks to ensure the flags are
3858 * not abused, and to handle any flag side effects.
3860 if (get_user(flags, (int __user *) arg))
3863 if (cmd == LL_IOC_SETFLAGS) {
3864 if ((flags & LL_FILE_IGNORE_LOCK) &&
3865 !(file->f_flags & O_DIRECT)) {
3866 CERROR("%s: unable to disable locking on "
3867 "non-O_DIRECT file\n", current->comm);
3871 fd->fd_flags |= flags;
3873 fd->fd_flags &= ~flags;
3876 case LL_IOC_LOV_SETSTRIPE:
3877 case LL_IOC_LOV_SETSTRIPE_NEW:
3878 RETURN(ll_lov_setstripe(inode, file, (void __user *)arg));
3879 case LL_IOC_LOV_SETEA:
3880 RETURN(ll_lov_setea(inode, file, (void __user *)arg));
3881 case LL_IOC_LOV_SWAP_LAYOUTS: {
3883 struct lustre_swap_layouts lsl;
3885 if (copy_from_user(&lsl, (char __user *)arg,
3886 sizeof(struct lustre_swap_layouts)))
3889 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
3892 file2 = fget(lsl.sl_fd);
3896 /* O_WRONLY or O_RDWR */
3897 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
3898 GOTO(out, rc = -EPERM);
3900 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
3901 struct inode *inode2;
3902 struct ll_inode_info *lli;
3903 struct obd_client_handle *och = NULL;
3905 lli = ll_i2info(inode);
3906 mutex_lock(&lli->lli_och_mutex);
3907 if (fd->fd_lease_och != NULL) {
3908 och = fd->fd_lease_och;
3909 fd->fd_lease_och = NULL;
3911 mutex_unlock(&lli->lli_och_mutex);
3913 GOTO(out, rc = -ENOLCK);
3914 inode2 = file_inode(file2);
3915 rc = ll_swap_layouts_close(och, inode, inode2);
3917 rc = ll_swap_layouts(file, file2, &lsl);
3923 case LL_IOC_LOV_GETSTRIPE:
3924 case LL_IOC_LOV_GETSTRIPE_NEW:
3925 RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
3926 case FS_IOC_GETFLAGS:
3927 case FS_IOC_SETFLAGS:
3928 RETURN(ll_iocontrol(inode, file, cmd, arg));
3929 case FSFILT_IOC_GETVERSION:
3930 case FS_IOC_GETVERSION:
3931 RETURN(put_user(inode->i_generation, (int __user *)arg));
3932 /* We need to special case any other ioctls we want to handle,
3933 * to send them to the MDS/OST as appropriate and to properly
3934 * network encode the arg field. */
3935 case FS_IOC_SETVERSION:
3938 case LL_IOC_GROUP_LOCK:
3939 RETURN(ll_get_grouplock(inode, file, arg));
3940 case LL_IOC_GROUP_UNLOCK:
3941 RETURN(ll_put_grouplock(inode, file, arg));
3942 case IOC_OBD_STATFS:
3943 RETURN(ll_obd_statfs(inode, (void __user *)arg));
3945 case LL_IOC_FLUSHCTX:
3946 RETURN(ll_flush_ctx(inode));
3947 case LL_IOC_PATH2FID: {
3948 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
3949 sizeof(struct lu_fid)))
3954 case LL_IOC_GETPARENT:
3955 RETURN(ll_getparent(file, (struct getparent __user *)arg));
3957 case OBD_IOC_FID2PATH:
3958 RETURN(ll_fid2path(inode, (void __user *)arg));
3959 case LL_IOC_DATA_VERSION: {
3960 struct ioc_data_version idv;
3963 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
3966 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
3967 rc = ll_ioc_data_version(inode, &idv);
3970 copy_to_user((char __user *)arg, &idv, sizeof(idv)))
3976 case LL_IOC_GET_MDTIDX: {
3979 mdtidx = ll_get_mdt_idx(inode);
3983 if (put_user((int)mdtidx, (int __user *)arg))
3988 case OBD_IOC_GETNAME_OLD:
3990 case OBD_IOC_GETDTNAME:
3992 case OBD_IOC_GETMDNAME:
3993 RETURN(ll_get_obd_name(inode, cmd, arg));
3994 case LL_IOC_HSM_STATE_GET: {
3995 struct md_op_data *op_data;
3996 struct hsm_user_state *hus;
4003 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4004 LUSTRE_OPC_ANY, hus);
4005 if (IS_ERR(op_data)) {
4007 RETURN(PTR_ERR(op_data));
4010 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4013 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
4016 ll_finish_md_op_data(op_data);
4020 case LL_IOC_HSM_STATE_SET: {
4021 struct hsm_state_set *hss;
4028 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
4033 rc = ll_hsm_state_set(inode, hss);
4038 case LL_IOC_HSM_ACTION: {
4039 struct md_op_data *op_data;
4040 struct hsm_current_action *hca;
4047 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4048 LUSTRE_OPC_ANY, hca);
4049 if (IS_ERR(op_data)) {
4051 RETURN(PTR_ERR(op_data));
4054 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4057 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
4060 ll_finish_md_op_data(op_data);
4064 case LL_IOC_SET_LEASE_OLD: {
4065 struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
4067 RETURN(ll_file_set_lease(file, &ioc, 0));
4069 case LL_IOC_SET_LEASE: {
4070 struct ll_ioc_lease ioc;
4072 if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
4075 RETURN(ll_file_set_lease(file, &ioc, arg));
4077 case LL_IOC_GET_LEASE: {
4078 struct ll_inode_info *lli = ll_i2info(inode);
4079 struct ldlm_lock *lock = NULL;
4082 mutex_lock(&lli->lli_och_mutex);
4083 if (fd->fd_lease_och != NULL) {
4084 struct obd_client_handle *och = fd->fd_lease_och;
4086 lock = ldlm_handle2lock(&och->och_lease_handle);
4088 lock_res_and_lock(lock);
4089 if (!ldlm_is_cancel(lock))
4090 fmode = och->och_flags;
4092 unlock_res_and_lock(lock);
4093 LDLM_LOCK_PUT(lock);
4096 mutex_unlock(&lli->lli_och_mutex);
4098 RETURN(ll_lease_type_from_fmode(fmode));
4100 case LL_IOC_HSM_IMPORT: {
4101 struct hsm_user_import *hui;
4107 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
4112 rc = ll_hsm_import(inode, file, hui);
4117 case LL_IOC_FUTIMES_3: {
4118 struct ll_futimes_3 lfu;
4120 if (copy_from_user(&lfu,
4121 (const struct ll_futimes_3 __user *)arg,
4125 RETURN(ll_file_futimes_3(file, &lfu));
4127 case LL_IOC_LADVISE: {
4128 struct llapi_ladvise_hdr *k_ladvise_hdr;
4129 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
4132 int alloc_size = sizeof(*k_ladvise_hdr);
4135 u_ladvise_hdr = (void __user *)arg;
4136 OBD_ALLOC_PTR(k_ladvise_hdr);
4137 if (k_ladvise_hdr == NULL)
4140 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4141 GOTO(out_ladvise, rc = -EFAULT);
4143 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
4144 k_ladvise_hdr->lah_count < 1)
4145 GOTO(out_ladvise, rc = -EINVAL);
4147 num_advise = k_ladvise_hdr->lah_count;
4148 if (num_advise >= LAH_COUNT_MAX)
4149 GOTO(out_ladvise, rc = -EFBIG);
4151 OBD_FREE_PTR(k_ladvise_hdr);
4152 alloc_size = offsetof(typeof(*k_ladvise_hdr),
4153 lah_advise[num_advise]);
4154 OBD_ALLOC(k_ladvise_hdr, alloc_size);
4155 if (k_ladvise_hdr == NULL)
4159 * TODO: submit multiple advices to one server in a single RPC
4161 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4162 GOTO(out_ladvise, rc = -EFAULT);
4164 for (i = 0; i < num_advise; i++) {
4165 struct llapi_lu_ladvise *k_ladvise =
4166 &k_ladvise_hdr->lah_advise[i];
4167 struct llapi_lu_ladvise __user *u_ladvise =
4168 &u_ladvise_hdr->lah_advise[i];
4170 rc = ll_ladvise_sanity(inode, k_ladvise);
4172 GOTO(out_ladvise, rc);
4174 switch (k_ladvise->lla_advice) {
4175 case LU_LADVISE_LOCKNOEXPAND:
4176 rc = ll_lock_noexpand(file,
4177 k_ladvise->lla_peradvice_flags);
4178 GOTO(out_ladvise, rc);
4179 case LU_LADVISE_LOCKAHEAD:
4181 rc = ll_file_lock_ahead(file, k_ladvise);
4184 GOTO(out_ladvise, rc);
4187 &u_ladvise->lla_lockahead_result))
4188 GOTO(out_ladvise, rc = -EFAULT);
4191 rc = ll_ladvise(inode, file,
4192 k_ladvise_hdr->lah_flags,
4195 GOTO(out_ladvise, rc);
4202 OBD_FREE(k_ladvise_hdr, alloc_size);
4205 case LL_IOC_FLR_SET_MIRROR: {
4206 /* mirror I/O must be direct to avoid polluting page cache
4208 if (!(file->f_flags & O_DIRECT))
4211 fd->fd_designated_mirror = (__u32)arg;
4214 case FS_IOC_FSGETXATTR:
4215 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
4216 case FS_IOC_FSSETXATTR:
4217 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
4218 case LL_IOC_PROJECT:
4219 RETURN(ll_ioctl_project(file, cmd, arg));
4221 RETURN(put_user(PAGE_SIZE, (int __user *)arg));
4222 case LL_IOC_HEAT_GET: {
4223 struct lu_heat uheat;
4224 struct lu_heat *heat;
4227 if (copy_from_user(&uheat, (void __user *)arg, sizeof(uheat)))
4230 if (uheat.lh_count > OBD_HEAT_COUNT)
4231 uheat.lh_count = OBD_HEAT_COUNT;
4233 size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
4234 OBD_ALLOC(heat, size);
4238 heat->lh_count = uheat.lh_count;
4239 ll_heat_get(inode, heat);
4240 rc = copy_to_user((char __user *)arg, heat, size);
4241 OBD_FREE(heat, size);
4242 RETURN(rc ? -EFAULT : 0);
4244 case LL_IOC_HEAT_SET: {
4247 if (copy_from_user(&flags, (void __user *)arg, sizeof(flags)))
4250 rc = ll_heat_set(inode, flags);
4253 case LL_IOC_PCC_DETACH: {
4254 struct lu_pcc_detach *detach;
4256 OBD_ALLOC_PTR(detach);
4260 if (copy_from_user(detach,
4261 (const struct lu_pcc_detach __user *)arg,
4263 GOTO(out_detach_free, rc = -EFAULT);
4265 if (!S_ISREG(inode->i_mode))
4266 GOTO(out_detach_free, rc = -EINVAL);
4268 if (!inode_owner_or_capable(inode))
4269 GOTO(out_detach_free, rc = -EPERM);
4271 rc = pcc_ioctl_detach(inode, detach->pccd_opt);
4273 OBD_FREE_PTR(detach);
4276 case LL_IOC_PCC_STATE: {
4277 struct lu_pcc_state __user *ustate =
4278 (struct lu_pcc_state __user *)arg;
4279 struct lu_pcc_state *state;
4281 OBD_ALLOC_PTR(state);
4285 if (copy_from_user(state, ustate, sizeof(*state)))
4286 GOTO(out_state, rc = -EFAULT);
4288 rc = pcc_ioctl_state(file, inode, state);
4290 GOTO(out_state, rc);
4292 if (copy_to_user(ustate, state, sizeof(*state)))
4293 GOTO(out_state, rc = -EFAULT);
4296 OBD_FREE_PTR(state);
4299 #ifdef HAVE_LUSTRE_CRYPTO
4300 case LL_IOC_SET_ENCRYPTION_POLICY:
4301 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4303 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
4304 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
4305 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4307 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
4308 case LL_IOC_ADD_ENCRYPTION_KEY:
4309 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4311 return llcrypt_ioctl_add_key(file, (void __user *)arg);
4312 case LL_IOC_REMOVE_ENCRYPTION_KEY:
4313 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4315 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
4316 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4317 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4319 return llcrypt_ioctl_remove_key_all_users(file,
4320 (void __user *)arg);
4321 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
4322 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4324 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
4327 case LL_IOC_UNLOCK_FOREIGN: {
4328 struct dentry *dentry = file_dentry(file);
4330 /* if not a foreign symlink do nothing */
4331 if (ll_foreign_is_removable(dentry, true)) {
4333 "prevent unlink of non-foreign file ("DFID")\n",
4334 PFID(ll_inode2fid(inode)));
4335 RETURN(-EOPNOTSUPP);
4341 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
4342 (void __user *)arg));
4346 loff_t ll_lseek(struct file *file, loff_t offset, int whence)
4348 struct inode *inode = file_inode(file);
4351 struct cl_lseek_io *lsio;
4358 env = cl_env_get(&refcheck);
4360 RETURN(PTR_ERR(env));
4362 io = vvp_env_thread_io(env);
4363 io->ci_obj = ll_i2info(inode)->lli_clob;
4364 ll_io_set_mirror(io, file);
4366 lsio = &io->u.ci_lseek;
4367 lsio->ls_start = offset;
4368 lsio->ls_whence = whence;
4369 lsio->ls_result = -ENXIO;
4372 rc = cl_io_init(env, io, CIT_LSEEK, io->ci_obj);
4374 struct vvp_io *vio = vvp_env_io(env);
4376 vio->vui_fd = file->private_data;
4377 rc = cl_io_loop(env, io);
4381 retval = rc ? : lsio->ls_result;
4382 cl_io_fini(env, io);
4383 } while (unlikely(io->ci_need_restart));
4385 cl_env_put(env, &refcheck);
4390 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
4392 struct inode *inode = file_inode(file);
4393 loff_t retval = offset, eof = 0;
4394 ktime_t kstart = ktime_get();
4398 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
4399 PFID(ll_inode2fid(inode)), inode, retval, retval,
4402 if (origin == SEEK_END) {
4403 retval = ll_glimpse_size(inode);
4406 eof = i_size_read(inode);
4409 if (origin == SEEK_HOLE || origin == SEEK_DATA) {
4413 /* flush local cache first if any */
4414 cl_sync_file_range(inode, offset, OBD_OBJECT_EOF,
4417 retval = ll_lseek(file, offset, origin);
4420 retval = vfs_setpos(file, retval, ll_file_maxbytes(inode));
4422 retval = generic_file_llseek_size(file, offset, origin,
4423 ll_file_maxbytes(inode), eof);
4426 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK,
4427 ktime_us_delta(ktime_get(), kstart));
4431 static int ll_flush(struct file *file, fl_owner_t id)
4433 struct inode *inode = file_inode(file);
4434 struct ll_inode_info *lli = ll_i2info(inode);
4435 struct ll_file_data *fd = file->private_data;
4438 LASSERT(!S_ISDIR(inode->i_mode));
4440 /* catch async errors that were recorded back when async writeback
4441 * failed for pages in this mapping. */
4442 rc = lli->lli_async_rc;
4443 lli->lli_async_rc = 0;
4444 if (lli->lli_clob != NULL) {
4445 err = lov_read_and_clear_async_rc(lli->lli_clob);
4450 /* The application has been told write failure already.
4451 * Do not report failure again. */
4452 if (fd->fd_write_failed)
4454 return rc ? -EIO : 0;
4458 * Called to make sure a portion of file has been written out.
4459 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
4461 * Return how many pages have been written.
4463 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
4464 enum cl_fsync_mode mode, int ignore_layout)
4468 struct cl_fsync_io *fio;
4473 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
4474 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
4477 env = cl_env_get(&refcheck);
4479 RETURN(PTR_ERR(env));
4481 io = vvp_env_thread_io(env);
4482 io->ci_obj = ll_i2info(inode)->lli_clob;
4483 io->ci_ignore_layout = ignore_layout;
4485 /* initialize parameters for sync */
4486 fio = &io->u.ci_fsync;
4487 fio->fi_start = start;
4489 fio->fi_fid = ll_inode2fid(inode);
4490 fio->fi_mode = mode;
4491 fio->fi_nr_written = 0;
4493 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
4494 result = cl_io_loop(env, io);
4496 result = io->ci_result;
4498 result = fio->fi_nr_written;
4499 cl_io_fini(env, io);
4500 cl_env_put(env, &refcheck);
4506 * When dentry is provided (the 'else' case), file_dentry() may be
4507 * null and dentry must be used directly rather than pulled from
4508 * file_dentry() as is done otherwise.
4511 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
4513 struct dentry *dentry = file_dentry(file);
4514 struct inode *inode = dentry->d_inode;
4515 struct ll_inode_info *lli = ll_i2info(inode);
4516 struct ptlrpc_request *req;
4517 ktime_t kstart = ktime_get();
4523 "VFS Op:inode="DFID"(%p), start %lld, end %lld, datasync %d\n",
4524 PFID(ll_inode2fid(inode)), inode, start, end, datasync);
4526 /* fsync's caller has already called _fdata{sync,write}, we want
4527 * that IO to finish before calling the osc and mdc sync methods */
4528 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
4531 /* catch async errors that were recorded back when async writeback
4532 * failed for pages in this mapping. */
4533 if (!S_ISDIR(inode->i_mode)) {
4534 err = lli->lli_async_rc;
4535 lli->lli_async_rc = 0;
4538 if (lli->lli_clob != NULL) {
4539 err = lov_read_and_clear_async_rc(lli->lli_clob);
4545 err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
4549 ptlrpc_req_finished(req);
4551 if (S_ISREG(inode->i_mode)) {
4552 struct ll_file_data *fd = file->private_data;
4555 /* Sync metadata on MDT first, and then sync the cached data
4558 err = pcc_fsync(file, start, end, datasync, &cached);
4560 err = cl_sync_file_range(inode, start, end,
4562 if (rc == 0 && err < 0)
4565 fd->fd_write_failed = true;
4567 fd->fd_write_failed = false;
4570 inode_unlock(inode);
4573 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC,
4574 ktime_us_delta(ktime_get(), kstart));
4579 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
4581 struct inode *inode = file_inode(file);
4582 struct ll_sb_info *sbi = ll_i2sbi(inode);
4583 struct ldlm_enqueue_info einfo = {
4584 .ei_type = LDLM_FLOCK,
4585 .ei_cb_cp = ldlm_flock_completion_ast,
4586 .ei_cbdata = file_lock,
4588 struct md_op_data *op_data;
4589 struct lustre_handle lockh = { 0 };
4590 union ldlm_policy_data flock = { { 0 } };
4591 int fl_type = file_lock->fl_type;
4592 ktime_t kstart = ktime_get();
4598 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
4599 PFID(ll_inode2fid(inode)), file_lock);
4601 if (file_lock->fl_flags & FL_FLOCK) {
4602 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
4603 /* flocks are whole-file locks */
4604 flock.l_flock.end = OFFSET_MAX;
4605 /* For flocks owner is determined by the local file desctiptor*/
4606 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
4607 } else if (file_lock->fl_flags & FL_POSIX) {
4608 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
4609 flock.l_flock.start = file_lock->fl_start;
4610 flock.l_flock.end = file_lock->fl_end;
4614 flock.l_flock.pid = file_lock->fl_pid;
4616 #if defined(HAVE_LM_COMPARE_OWNER) || defined(lm_compare_owner)
4617 /* Somewhat ugly workaround for svc lockd.
4618 * lockd installs custom fl_lmops->lm_compare_owner that checks
4619 * for the fl_owner to be the same (which it always is on local node
4620 * I guess between lockd processes) and then compares pid.
4621 * As such we assign pid to the owner field to make it all work,
4622 * conflict with normal locks is unlikely since pid space and
4623 * pointer space for current->files are not intersecting */
4624 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
4625 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
4630 einfo.ei_mode = LCK_PR;
4633 /* An unlock request may or may not have any relation to
4634 * existing locks so we may not be able to pass a lock handle
4635 * via a normal ldlm_lock_cancel() request. The request may even
4636 * unlock a byte range in the middle of an existing lock. In
4637 * order to process an unlock request we need all of the same
4638 * information that is given with a normal read or write record
4639 * lock request. To avoid creating another ldlm unlock (cancel)
4640 * message we'll treat a LCK_NL flock request as an unlock. */
4641 einfo.ei_mode = LCK_NL;
4644 einfo.ei_mode = LCK_PW;
4647 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
4662 flags = LDLM_FL_BLOCK_NOWAIT;
4668 flags = LDLM_FL_TEST_LOCK;
4671 CERROR("unknown fcntl lock command: %d\n", cmd);
4675 /* Save the old mode so that if the mode in the lock changes we
4676 * can decrement the appropriate reader or writer refcount. */
4677 file_lock->fl_type = einfo.ei_mode;
4679 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4680 LUSTRE_OPC_ANY, NULL);
4681 if (IS_ERR(op_data))
4682 RETURN(PTR_ERR(op_data));
4684 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
4685 "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
4686 flock.l_flock.pid, flags, einfo.ei_mode,
4687 flock.l_flock.start, flock.l_flock.end);
4689 rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
4692 /* Restore the file lock type if not TEST lock. */
4693 if (!(flags & LDLM_FL_TEST_LOCK))
4694 file_lock->fl_type = fl_type;
4696 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
4697 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
4698 !(flags & LDLM_FL_TEST_LOCK))
4699 rc2 = locks_lock_file_wait(file, file_lock);
4701 if ((file_lock->fl_flags & FL_FLOCK) &&
4702 (rc == 0 || file_lock->fl_type == F_UNLCK))
4703 rc2 = flock_lock_file_wait(file, file_lock);
4704 if ((file_lock->fl_flags & FL_POSIX) &&
4705 (rc == 0 || file_lock->fl_type == F_UNLCK) &&
4706 !(flags & LDLM_FL_TEST_LOCK))
4707 rc2 = posix_lock_file_wait(file, file_lock);
4708 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
4710 if (rc2 && file_lock->fl_type != F_UNLCK) {
4711 einfo.ei_mode = LCK_NL;
4712 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
4717 ll_finish_md_op_data(op_data);
4720 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK,
4721 ktime_us_delta(ktime_get(), kstart));
4725 int ll_get_fid_by_name(struct inode *parent, const char *name,
4726 int namelen, struct lu_fid *fid,
4727 struct inode **inode)
4729 struct md_op_data *op_data = NULL;
4730 struct mdt_body *body;
4731 struct ptlrpc_request *req;
4735 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
4736 LUSTRE_OPC_ANY, NULL);
4737 if (IS_ERR(op_data))
4738 RETURN(PTR_ERR(op_data));
4740 op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
4741 rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
4742 ll_finish_md_op_data(op_data);
4746 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4748 GOTO(out_req, rc = -EFAULT);
4750 *fid = body->mbo_fid1;
4753 rc = ll_prep_inode(inode, &req->rq_pill, parent->i_sb, NULL);
4755 ptlrpc_req_finished(req);
4759 int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
4762 struct dentry *dchild = NULL;
4763 struct inode *child_inode = NULL;
4764 struct md_op_data *op_data;
4765 struct ptlrpc_request *request = NULL;
4766 struct obd_client_handle *och = NULL;
4768 struct mdt_body *body;
4769 __u64 data_version = 0;
4770 size_t namelen = strlen(name);
4771 int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
4775 CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
4776 PFID(ll_inode2fid(parent)), name,
4777 lum->lum_stripe_offset, lum->lum_stripe_count);
4779 if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
4780 lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
4781 lustre_swab_lmv_user_md(lum);
4783 /* Get child FID first */
4784 qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
4787 dchild = d_lookup(file_dentry(file), &qstr);
4789 if (dchild->d_inode)
4790 child_inode = igrab(dchild->d_inode);
4795 rc = ll_get_fid_by_name(parent, name, namelen, NULL,
4804 if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
4805 OBD_CONNECT2_DIR_MIGRATE)) {
4806 if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
4807 ll_dir_striped(child_inode)) {
4808 CERROR("%s: MDT doesn't support stripe directory "
4809 "migration!\n", ll_i2sbi(parent)->ll_fsname);
4810 GOTO(out_iput, rc = -EOPNOTSUPP);
4815 * lfs migrate command needs to be blocked on the client
4816 * by checking the migrate FID against the FID of the
4819 if (is_root_inode(child_inode))
4820 GOTO(out_iput, rc = -EINVAL);
4822 if (IS_ENCRYPTED(child_inode)) {
4823 rc = llcrypt_get_encryption_info(child_inode);
4826 if (!llcrypt_has_encryption_key(child_inode)) {
4827 CDEBUG(D_SEC, "no enc key for "DFID"\n",
4828 PFID(ll_inode2fid(child_inode)));
4829 GOTO(out_iput, rc = -ENOKEY);
4831 if (unlikely(!llcrypt_policy_has_filename_enc(child_inode))) {
4833 "cannot migrate old format encrypted "DFID", please move to new enc dir first\n",
4834 PFID(ll_inode2fid(child_inode)));
4835 GOTO(out_iput, rc = -EUCLEAN);
4839 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
4840 child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
4841 if (IS_ERR(op_data))
4842 GOTO(out_iput, rc = PTR_ERR(op_data));
4844 inode_lock(child_inode);
4845 op_data->op_fid3 = *ll_inode2fid(child_inode);
4846 if (!fid_is_sane(&op_data->op_fid3)) {
4847 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
4848 ll_i2sbi(parent)->ll_fsname, name,
4849 PFID(&op_data->op_fid3));
4850 GOTO(out_unlock, rc = -EINVAL);
4853 op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
4854 op_data->op_data = lum;
4855 op_data->op_data_size = lumlen;
4858 if (S_ISREG(child_inode->i_mode)) {
4859 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
4863 GOTO(out_unlock, rc);
4866 rc = ll_data_version(child_inode, &data_version,
4869 GOTO(out_close, rc);
4871 op_data->op_open_handle = och->och_open_handle;
4872 op_data->op_data_version = data_version;
4873 op_data->op_lease_handle = och->och_lease_handle;
4874 op_data->op_bias |= MDS_CLOSE_MIGRATE;
4876 spin_lock(&och->och_mod->mod_open_req->rq_lock);
4877 och->och_mod->mod_open_req->rq_replay = 0;
4878 spin_unlock(&och->och_mod->mod_open_req->rq_lock);
4881 rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name, namelen,
4882 name, namelen, &request);
4884 LASSERT(request != NULL);
4885 ll_update_times(request, parent);
4888 if (rc == 0 || rc == -EAGAIN) {
4889 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
4890 LASSERT(body != NULL);
4892 /* If the server does release layout lock, then we cleanup
4893 * the client och here, otherwise release it in out_close: */
4894 if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
4895 obd_mod_put(och->och_mod);
4896 md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
4898 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
4904 if (request != NULL) {
4905 ptlrpc_req_finished(request);
4909 /* Try again if the lease has cancelled. */
4910 if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
4915 ll_lease_close(och, child_inode, NULL);
4917 clear_nlink(child_inode);
4919 inode_unlock(child_inode);
4920 ll_finish_md_op_data(op_data);
4927 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
4929 struct ll_file_data *fd = file->private_data;
4933 * In order to avoid flood of warning messages, only print one message
4934 * for one file. And the entire message rate on the client is limited
4935 * by CDEBUG_LIMIT too.
4937 if (!(fd->fd_flags & LL_FILE_FLOCK_WARNING)) {
4938 fd->fd_flags |= LL_FILE_FLOCK_WARNING;
4939 CDEBUG_LIMIT(D_TTY | D_CONSOLE,
4940 "flock disabled, mount with '-o [local]flock' to enable\r\n");
4946 * test if some locks matching bits and l_req_mode are acquired
4947 * - bits can be in different locks
4948 * - if found clear the common lock bits in *bits
4949 * - the bits not found, are kept in *bits
4951 * \param bits [IN] searched lock bits [IN]
4952 * \param l_req_mode [IN] searched lock mode
4953 * \retval boolean, true iff all bits are found
4955 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
4957 struct lustre_handle lockh;
4958 union ldlm_policy_data policy;
4959 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
4960 (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
4969 fid = &ll_i2info(inode)->lli_fid;
4970 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
4971 ldlm_lockname[mode]);
4973 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
4974 for (i = 0; i < MDS_INODELOCK_NUMBITS && *bits != 0; i++) {
4975 policy.l_inodebits.bits = *bits & BIT(i);
4976 if (policy.l_inodebits.bits == 0)
4979 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
4980 &policy, mode, &lockh)) {
4981 struct ldlm_lock *lock;
4983 lock = ldlm_handle2lock(&lockh);
4986 ~(lock->l_policy_data.l_inodebits.bits);
4987 LDLM_LOCK_PUT(lock);
4989 *bits &= ~policy.l_inodebits.bits;
4996 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
4997 struct lustre_handle *lockh, __u64 flags,
4998 enum ldlm_mode mode)
5000 union ldlm_policy_data policy = { .l_inodebits = { bits } };
5005 fid = &ll_i2info(inode)->lli_fid;
5006 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
5008 rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
5009 fid, LDLM_IBITS, &policy, mode, lockh);
5014 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
5016 /* Already unlinked. Just update nlink and return success */
5017 if (rc == -ENOENT) {
5019 /* If it is striped directory, and there is bad stripe
5020 * Let's revalidate the dentry again, instead of returning
5022 if (ll_dir_striped(inode))
5025 /* This path cannot be hit for regular files unless in
5026 * case of obscure races, so no need to to validate
5028 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
5030 } else if (rc != 0) {
5031 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
5032 "%s: revalidate FID "DFID" error: rc = %d\n",
5033 ll_i2sbi(inode)->ll_fsname,
5034 PFID(ll_inode2fid(inode)), rc);
5040 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
5042 struct inode *parent;
5043 struct inode *inode = dentry->d_inode;
5044 struct obd_export *exp = ll_i2mdexp(inode);
5045 struct lookup_intent oit = {
5048 struct ptlrpc_request *req = NULL;
5049 struct md_op_data *op_data;
5050 const char *name = NULL;
5055 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
5056 PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
5058 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID) {
5059 parent = dentry->d_parent->d_inode;
5060 name = dentry->d_name.name;
5061 namelen = dentry->d_name.len;
5066 op_data = ll_prep_md_op_data(NULL, parent, inode, name, namelen, 0,
5067 LUSTRE_OPC_ANY, NULL);
5068 if (IS_ERR(op_data))
5069 RETURN(PTR_ERR(op_data));
5071 /* Call getattr by fid */
5072 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID)
5073 op_data->op_flags = MF_GETATTR_BY_FID;
5074 rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
5075 ll_finish_md_op_data(op_data);
5077 rc = ll_inode_revalidate_fini(inode, rc);
5081 rc = ll_revalidate_it_finish(req, &oit, dentry);
5083 ll_intent_release(&oit);
5087 /* Unlinked? Unhash dentry, so it is not picked up later by
5088 * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
5089 * here to preserve get_cwd functionality on 2.6.
5091 if (!dentry->d_inode->i_nlink)
5092 d_lustre_invalidate(dentry);
5094 ll_lookup_finish_locks(&oit, dentry);
5096 ptlrpc_req_finished(req);
5101 static int ll_merge_md_attr(struct inode *inode)
5103 struct ll_inode_info *lli = ll_i2info(inode);
5104 struct cl_attr attr = { 0 };
5107 LASSERT(lli->lli_lsm_md != NULL);
5109 if (!lmv_dir_striped(lli->lli_lsm_md))
5112 down_read(&lli->lli_lsm_sem);
5113 rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
5114 &attr, ll_md_blocking_ast);
5115 up_read(&lli->lli_lsm_sem);
5119 set_nlink(inode, attr.cat_nlink);
5120 inode->i_blocks = attr.cat_blocks;
5121 i_size_write(inode, attr.cat_size);
5123 ll_i2info(inode)->lli_atime = attr.cat_atime;
5124 ll_i2info(inode)->lli_mtime = attr.cat_mtime;
5125 ll_i2info(inode)->lli_ctime = attr.cat_ctime;
5130 int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
5131 unsigned int flags, bool foreign)
5133 struct inode *inode = de->d_inode;
5134 struct ll_sb_info *sbi = ll_i2sbi(inode);
5135 struct ll_inode_info *lli = ll_i2info(inode);
5136 struct inode *dir = de->d_parent->d_inode;
5137 bool need_glimpse = true;
5138 ktime_t kstart = ktime_get();
5141 /* The OST object(s) determine the file size, blocks and mtime. */
5142 if (!(request_mask & STATX_SIZE || request_mask & STATX_BLOCKS ||
5143 request_mask & STATX_MTIME))
5144 need_glimpse = false;
5146 if (dentry_may_statahead(dir, de))
5147 ll_start_statahead(dir, de, need_glimpse &&
5148 !(flags & AT_STATX_DONT_SYNC));
5150 if (flags & AT_STATX_DONT_SYNC)
5151 GOTO(fill_attr, rc = 0);
5153 rc = ll_inode_revalidate(de, IT_GETATTR);
5157 /* foreign file/dir are always of zero length, so don't
5158 * need to validate size.
5160 if (S_ISREG(inode->i_mode) && !foreign) {
5164 GOTO(fill_attr, rc);
5166 rc = pcc_inode_getattr(inode, request_mask, flags, &cached);
5167 if (cached && rc < 0)
5171 GOTO(fill_attr, rc);
5174 * If the returned attr is masked with OBD_MD_FLSIZE &
5175 * OBD_MD_FLBLOCKS & OBD_MD_FLMTIME, it means that the file size
5176 * or blocks obtained from MDT is strictly correct, and the file
5177 * is usually not being modified by clients, and the [a|m|c]time
5178 * got from MDT is also strictly correct.
5179 * Under this circumstance, it does not need to send glimpse
5180 * RPCs to OSTs for file attributes such as the size and blocks.
5182 if (lli->lli_attr_valid & OBD_MD_FLSIZE &&
5183 lli->lli_attr_valid & OBD_MD_FLBLOCKS &&
5184 lli->lli_attr_valid & OBD_MD_FLMTIME) {
5185 inode->i_mtime.tv_sec = lli->lli_mtime;
5186 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5187 inode->i_atime.tv_sec = lli->lli_atime;
5188 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5189 inode->i_ctime.tv_sec = lli->lli_ctime;
5190 GOTO(fill_attr, rc);
5193 /* In case of restore, the MDT has the right size and has
5194 * already send it back without granting the layout lock,
5195 * inode is up-to-date so glimpse is useless.
5196 * Also to glimpse we need the layout, in case of a running
5197 * restore the MDT holds the layout lock so the glimpse will
5198 * block up to the end of restore (getattr will block)
5200 if (!test_bit(LLIF_FILE_RESTORING, &lli->lli_flags)) {
5201 rc = ll_glimpse_size(inode);
5206 /* If object isn't regular a file then don't validate size. */
5207 /* foreign dir is not striped dir */
5208 if (ll_dir_striped(inode) && !foreign) {
5209 rc = ll_merge_md_attr(inode);
5214 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5215 inode->i_atime.tv_sec = lli->lli_atime;
5216 if (lli->lli_attr_valid & OBD_MD_FLMTIME)
5217 inode->i_mtime.tv_sec = lli->lli_mtime;
5218 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5219 inode->i_ctime.tv_sec = lli->lli_ctime;
5223 OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
5225 if (ll_need_32bit_api(sbi)) {
5226 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
5227 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
5228 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
5230 stat->ino = inode->i_ino;
5231 stat->dev = inode->i_sb->s_dev;
5232 stat->rdev = inode->i_rdev;
5235 /* foreign symlink to be exposed as a real symlink */
5237 stat->mode = inode->i_mode;
5239 stat->mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
5241 stat->uid = inode->i_uid;
5242 stat->gid = inode->i_gid;
5243 stat->atime = inode->i_atime;
5244 stat->mtime = inode->i_mtime;
5245 stat->ctime = inode->i_ctime;
5246 /* stat->blksize is used to tell about preferred IO size */
5247 if (sbi->ll_stat_blksize)
5248 stat->blksize = sbi->ll_stat_blksize;
5249 else if (S_ISREG(inode->i_mode))
5250 stat->blksize = 1 << min(PTLRPC_MAX_BRW_BITS + 1,
5251 LL_MAX_BLKSIZE_BITS);
5253 stat->blksize = 1 << inode->i_sb->s_blocksize_bits;
5255 stat->nlink = inode->i_nlink;
5256 stat->size = i_size_read(inode);
5257 stat->blocks = inode->i_blocks;
5259 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5260 if (flags & AT_STATX_DONT_SYNC) {
5261 if (stat->size == 0 &&
5262 lli->lli_attr_valid & OBD_MD_FLLAZYSIZE)
5263 stat->size = lli->lli_lazysize;
5264 if (stat->blocks == 0 &&
5265 lli->lli_attr_valid & OBD_MD_FLLAZYBLOCKS)
5266 stat->blocks = lli->lli_lazyblocks;
5269 if (lli->lli_attr_valid & OBD_MD_FLBTIME) {
5270 stat->result_mask |= STATX_BTIME;
5271 stat->btime.tv_sec = lli->lli_btime;
5274 stat->attributes_mask = STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
5275 stat->attributes |= ll_inode_to_ext_flags(inode->i_flags);
5276 stat->result_mask &= request_mask;
5279 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR,
5280 ktime_us_delta(ktime_get(), kstart));
5285 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5286 int ll_getattr(const struct path *path, struct kstat *stat,
5287 u32 request_mask, unsigned int flags)
5289 return ll_getattr_dentry(path->dentry, stat, request_mask, flags,
5293 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
5295 return ll_getattr_dentry(de, stat, STATX_BASIC_STATS,
5296 AT_STATX_SYNC_AS_STAT, false);
5300 int cl_falloc(struct inode *inode, int mode, loff_t offset, loff_t len)
5306 loff_t size = i_size_read(inode);
5310 env = cl_env_get(&refcheck);
5312 RETURN(PTR_ERR(env));
5314 io = vvp_env_thread_io(env);
5315 io->ci_obj = ll_i2info(inode)->lli_clob;
5316 io->ci_verify_layout = 1;
5317 io->u.ci_setattr.sa_parent_fid = lu_object_fid(&io->ci_obj->co_lu);
5318 io->u.ci_setattr.sa_falloc_mode = mode;
5319 io->u.ci_setattr.sa_falloc_offset = offset;
5320 io->u.ci_setattr.sa_falloc_end = offset + len;
5321 io->u.ci_setattr.sa_subtype = CL_SETATTR_FALLOCATE;
5322 if (io->u.ci_setattr.sa_falloc_end > size) {
5323 loff_t newsize = io->u.ci_setattr.sa_falloc_end;
5325 /* Check new size against VFS/VM file size limit and rlimit */
5326 rc = inode_newsize_ok(inode, newsize);
5329 if (newsize > ll_file_maxbytes(inode)) {
5330 CDEBUG(D_INODE, "file size too large %llu > %llu\n",
5331 (unsigned long long)newsize,
5332 ll_file_maxbytes(inode));
5339 rc = cl_io_init(env, io, CIT_SETATTR, io->ci_obj);
5341 rc = cl_io_loop(env, io);
5344 cl_io_fini(env, io);
5345 } while (unlikely(io->ci_need_restart));
5348 cl_env_put(env, &refcheck);
5352 long ll_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
5354 struct inode *inode = filp->f_path.dentry->d_inode;
5357 if (offset < 0 || len <= 0)
5360 * Encrypted inodes can't handle collapse range or zero range or insert
5361 * range since we would need to re-encrypt blocks with a different IV or
5362 * XTS tweak (which are based on the logical block number).
5363 * Similar to what ext4 does.
5365 if (IS_ENCRYPTED(inode) &&
5366 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
5367 FALLOC_FL_ZERO_RANGE)))
5368 RETURN(-EOPNOTSUPP);
5371 * mode == 0 (which is standard prealloc) and PUNCH is supported
5372 * Rest of mode options are not supported yet.
5374 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
5375 RETURN(-EOPNOTSUPP);
5377 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FALLOCATE, 1);
5379 rc = cl_falloc(inode, mode, offset, len);
5381 * ENOTSUPP (524) is an NFSv3 specific error code erroneously
5382 * used by Lustre in several places. Retuning it here would
5383 * confuse applications that explicity test for EOPNOTSUPP
5384 * (95) and fall back to ftruncate().
5386 if (rc == -ENOTSUPP)
5392 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5393 __u64 start, __u64 len)
5397 struct fiemap *fiemap;
5398 unsigned int extent_count = fieinfo->fi_extents_max;
5400 num_bytes = sizeof(*fiemap) + (extent_count *
5401 sizeof(struct fiemap_extent));
5402 OBD_ALLOC_LARGE(fiemap, num_bytes);
5407 fiemap->fm_flags = fieinfo->fi_flags;
5408 fiemap->fm_extent_count = fieinfo->fi_extents_max;
5409 fiemap->fm_start = start;
5410 fiemap->fm_length = len;
5411 if (extent_count > 0 &&
5412 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
5413 sizeof(struct fiemap_extent)) != 0)
5414 GOTO(out, rc = -EFAULT);
5416 rc = ll_do_fiemap(inode, fiemap, num_bytes);
5418 if (IS_ENCRYPTED(inode)) {
5421 for (i = 0; i < fiemap->fm_mapped_extents; i++)
5422 fiemap->fm_extents[i].fe_flags |=
5423 FIEMAP_EXTENT_DATA_ENCRYPTED |
5424 FIEMAP_EXTENT_ENCODED;
5427 fieinfo->fi_flags = fiemap->fm_flags;
5428 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
5429 if (extent_count > 0 &&
5430 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
5431 fiemap->fm_mapped_extents *
5432 sizeof(struct fiemap_extent)) != 0)
5433 GOTO(out, rc = -EFAULT);
5435 OBD_FREE_LARGE(fiemap, num_bytes);
5439 int ll_inode_permission(struct inode *inode, int mask)
5442 struct ll_sb_info *sbi;
5443 struct root_squash_info *squash;
5444 struct cred *cred = NULL;
5445 const struct cred *old_cred = NULL;
5446 bool squash_id = false;
5447 ktime_t kstart = ktime_get();
5451 if (mask & MAY_NOT_BLOCK)
5455 * as root inode are NOT getting validated in lookup operation,
5456 * need to do it before permission check.
5459 if (is_root_inode(inode)) {
5460 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
5465 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
5466 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
5468 /* squash fsuid/fsgid if needed */
5469 sbi = ll_i2sbi(inode);
5470 squash = &sbi->ll_squash;
5471 if (unlikely(squash->rsi_uid != 0 &&
5472 uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
5473 !(sbi->ll_flags & LL_SBI_NOROOTSQUASH))) {
5477 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
5478 __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
5479 squash->rsi_uid, squash->rsi_gid);
5481 /* update current process's credentials
5482 * and FS capability */
5483 cred = prepare_creds();
5487 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
5488 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
5489 cred->cap_effective = cap_drop_nfsd_set(cred->cap_effective);
5490 cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
5492 old_cred = override_creds(cred);
5495 rc = generic_permission(inode, mask);
5496 /* restore current process's credentials and FS capability */
5498 revert_creds(old_cred);
5503 ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM,
5504 ktime_us_delta(ktime_get(), kstart));
5509 /* -o localflock - only provides locally consistent flock locks */
5510 static const struct file_operations ll_file_operations = {
5511 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5512 # ifdef HAVE_SYNC_READ_WRITE
5513 .read = new_sync_read,
5514 .write = new_sync_write,
5516 .read_iter = ll_file_read_iter,
5517 .write_iter = ll_file_write_iter,
5518 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5519 .read = ll_file_read,
5520 .aio_read = ll_file_aio_read,
5521 .write = ll_file_write,
5522 .aio_write = ll_file_aio_write,
5523 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5524 .unlocked_ioctl = ll_file_ioctl,
5525 .open = ll_file_open,
5526 .release = ll_file_release,
5527 .mmap = ll_file_mmap,
5528 .llseek = ll_file_seek,
5529 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5530 .splice_read = generic_file_splice_read,
5532 .splice_read = pcc_file_splice_read,
5536 .fallocate = ll_fallocate,
5539 static const struct file_operations ll_file_operations_flock = {
5540 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5541 # ifdef HAVE_SYNC_READ_WRITE
5542 .read = new_sync_read,
5543 .write = new_sync_write,
5544 # endif /* HAVE_SYNC_READ_WRITE */
5545 .read_iter = ll_file_read_iter,
5546 .write_iter = ll_file_write_iter,
5547 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5548 .read = ll_file_read,
5549 .aio_read = ll_file_aio_read,
5550 .write = ll_file_write,
5551 .aio_write = ll_file_aio_write,
5552 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5553 .unlocked_ioctl = ll_file_ioctl,
5554 .open = ll_file_open,
5555 .release = ll_file_release,
5556 .mmap = ll_file_mmap,
5557 .llseek = ll_file_seek,
5558 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5559 .splice_read = generic_file_splice_read,
5561 .splice_read = pcc_file_splice_read,
5565 .flock = ll_file_flock,
5566 .lock = ll_file_flock,
5567 .fallocate = ll_fallocate,
5570 /* These are for -o noflock - to return ENOSYS on flock calls */
5571 static const struct file_operations ll_file_operations_noflock = {
5572 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5573 # ifdef HAVE_SYNC_READ_WRITE
5574 .read = new_sync_read,
5575 .write = new_sync_write,
5576 # endif /* HAVE_SYNC_READ_WRITE */
5577 .read_iter = ll_file_read_iter,
5578 .write_iter = ll_file_write_iter,
5579 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5580 .read = ll_file_read,
5581 .aio_read = ll_file_aio_read,
5582 .write = ll_file_write,
5583 .aio_write = ll_file_aio_write,
5584 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5585 .unlocked_ioctl = ll_file_ioctl,
5586 .open = ll_file_open,
5587 .release = ll_file_release,
5588 .mmap = ll_file_mmap,
5589 .llseek = ll_file_seek,
5590 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5591 .splice_read = generic_file_splice_read,
5593 .splice_read = pcc_file_splice_read,
5597 .flock = ll_file_noflock,
5598 .lock = ll_file_noflock,
5599 .fallocate = ll_fallocate,
5602 const struct inode_operations ll_file_inode_operations = {
5603 .setattr = ll_setattr,
5604 .getattr = ll_getattr,
5605 .permission = ll_inode_permission,
5606 #ifdef HAVE_IOP_XATTR
5607 .setxattr = ll_setxattr,
5608 .getxattr = ll_getxattr,
5609 .removexattr = ll_removexattr,
5611 .listxattr = ll_listxattr,
5612 .fiemap = ll_fiemap,
5613 .get_acl = ll_get_acl,
5614 #ifdef HAVE_IOP_SET_ACL
5615 .set_acl = ll_set_acl,
5619 const struct file_operations *ll_select_file_operations(struct ll_sb_info *sbi)
5621 const struct file_operations *fops = &ll_file_operations_noflock;
5623 if (sbi->ll_flags & LL_SBI_FLOCK)
5624 fops = &ll_file_operations_flock;
5625 else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
5626 fops = &ll_file_operations;
5631 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
5633 struct ll_inode_info *lli = ll_i2info(inode);
5634 struct cl_object *obj = lli->lli_clob;
5643 env = cl_env_get(&refcheck);
5645 RETURN(PTR_ERR(env));
5647 rc = cl_conf_set(env, lli->lli_clob, conf);
5651 if (conf->coc_opc == OBJECT_CONF_SET) {
5652 struct ldlm_lock *lock = conf->coc_lock;
5653 struct cl_layout cl = {
5657 LASSERT(lock != NULL);
5658 LASSERT(ldlm_has_layout(lock));
5660 /* it can only be allowed to match after layout is
5661 * applied to inode otherwise false layout would be
5662 * seen. Applying layout shoud happen before dropping
5663 * the intent lock. */
5664 ldlm_lock_allow_match(lock);
5666 rc = cl_object_layout_get(env, obj, &cl);
5671 DFID": layout version change: %u -> %u\n",
5672 PFID(&lli->lli_fid), ll_layout_version_get(lli),
5674 ll_layout_version_set(lli, cl.cl_layout_gen);
5678 cl_env_put(env, &refcheck);
5680 RETURN(rc < 0 ? rc : 0);
5683 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
5684 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
5687 struct ll_sb_info *sbi = ll_i2sbi(inode);
5688 struct ptlrpc_request *req;
5695 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
5696 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
5697 lock->l_lvb_data, lock->l_lvb_len);
5699 if (lock->l_lvb_data != NULL)
5702 /* if layout lock was granted right away, the layout is returned
5703 * within DLM_LVB of dlm reply; otherwise if the lock was ever
5704 * blocked and then granted via completion ast, we have to fetch
5705 * layout here. Please note that we can't use the LVB buffer in
5706 * completion AST because it doesn't have a large enough buffer */
5707 rc = ll_get_default_mdsize(sbi, &lmmsize);
5711 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), OBD_MD_FLXATTR,
5712 XATTR_NAME_LOV, lmmsize, &req);
5715 GOTO(out, rc = 0); /* empty layout */
5722 if (lmmsize == 0) /* empty layout */
5725 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
5727 GOTO(out, rc = -EFAULT);
5729 OBD_ALLOC_LARGE(lvbdata, lmmsize);
5730 if (lvbdata == NULL)
5731 GOTO(out, rc = -ENOMEM);
5733 memcpy(lvbdata, lmm, lmmsize);
5734 lock_res_and_lock(lock);
5735 if (unlikely(lock->l_lvb_data == NULL)) {
5736 lock->l_lvb_type = LVB_T_LAYOUT;
5737 lock->l_lvb_data = lvbdata;
5738 lock->l_lvb_len = lmmsize;
5741 unlock_res_and_lock(lock);
5744 OBD_FREE_LARGE(lvbdata, lmmsize);
5749 ptlrpc_req_finished(req);
5754 * Apply the layout to the inode. Layout lock is held and will be released
5757 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
5758 struct inode *inode)
5760 struct ll_inode_info *lli = ll_i2info(inode);
5761 struct ll_sb_info *sbi = ll_i2sbi(inode);
5762 struct ldlm_lock *lock;
5763 struct cl_object_conf conf;
5766 bool wait_layout = false;
5769 LASSERT(lustre_handle_is_used(lockh));
5771 lock = ldlm_handle2lock(lockh);
5772 LASSERT(lock != NULL);
5774 if (!ldlm_has_layout(lock))
5775 GOTO(out, rc = -EAGAIN);
5777 LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
5778 PFID(&lli->lli_fid), inode);
5780 /* in case this is a caching lock and reinstate with new inode */
5781 md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
5783 lock_res_and_lock(lock);
5784 lvb_ready = ldlm_is_lvb_ready(lock);
5785 unlock_res_and_lock(lock);
5787 /* checking lvb_ready is racy but this is okay. The worst case is
5788 * that multi processes may configure the file on the same time. */
5792 rc = ll_layout_fetch(inode, lock);
5796 /* for layout lock, lmm is stored in lock's lvb.
5797 * lvb_data is immutable if the lock is held so it's safe to access it
5800 * set layout to file. Unlikely this will fail as old layout was
5801 * surely eliminated */
5802 memset(&conf, 0, sizeof conf);
5803 conf.coc_opc = OBJECT_CONF_SET;
5804 conf.coc_inode = inode;
5805 conf.coc_lock = lock;
5806 conf.u.coc_layout.lb_buf = lock->l_lvb_data;
5807 conf.u.coc_layout.lb_len = lock->l_lvb_len;
5808 rc = ll_layout_conf(inode, &conf);
5810 /* refresh layout failed, need to wait */
5811 wait_layout = rc == -EBUSY;
5814 LDLM_LOCK_PUT(lock);
5815 ldlm_lock_decref(lockh, mode);
5817 /* wait for IO to complete if it's still being used. */
5819 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
5820 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5822 memset(&conf, 0, sizeof conf);
5823 conf.coc_opc = OBJECT_CONF_WAIT;
5824 conf.coc_inode = inode;
5825 rc = ll_layout_conf(inode, &conf);
5829 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
5830 sbi->ll_fsname, PFID(&lli->lli_fid), rc);
5836 * Issue layout intent RPC to MDS.
5837 * \param inode [in] file inode
5838 * \param intent [in] layout intent
5840 * \retval 0 on success
5841 * \retval < 0 error code
5843 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
5845 struct ll_inode_info *lli = ll_i2info(inode);
5846 struct ll_sb_info *sbi = ll_i2sbi(inode);
5847 struct md_op_data *op_data;
5848 struct lookup_intent it;
5849 struct ptlrpc_request *req;
5853 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
5854 0, 0, LUSTRE_OPC_ANY, NULL);
5855 if (IS_ERR(op_data))
5856 RETURN(PTR_ERR(op_data));
5858 op_data->op_data = intent;
5859 op_data->op_data_size = sizeof(*intent);
5861 memset(&it, 0, sizeof(it));
5862 it.it_op = IT_LAYOUT;
5863 if (intent->li_opc == LAYOUT_INTENT_WRITE ||
5864 intent->li_opc == LAYOUT_INTENT_TRUNC)
5865 it.it_flags = FMODE_WRITE;
5867 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
5868 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5870 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
5871 &ll_md_blocking_ast, 0);
5872 if (it.it_request != NULL)
5873 ptlrpc_req_finished(it.it_request);
5874 it.it_request = NULL;
5876 ll_finish_md_op_data(op_data);
5878 /* set lock data in case this is a new lock */
5880 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
5882 ll_intent_drop_lock(&it);
5888 * This function checks if there exists a LAYOUT lock on the client side,
5889 * or enqueues it if it doesn't have one in cache.
5891 * This function will not hold layout lock so it may be revoked any time after
5892 * this function returns. Any operations depend on layout should be redone
5895 * This function should be called before lov_io_init() to get an uptodate
5896 * layout version, the caller should save the version number and after IO
5897 * is finished, this function should be called again to verify that layout
5898 * is not changed during IO time.
5900 int ll_layout_refresh(struct inode *inode, __u32 *gen)
5902 struct ll_inode_info *lli = ll_i2info(inode);
5903 struct ll_sb_info *sbi = ll_i2sbi(inode);
5904 struct lustre_handle lockh;
5905 struct layout_intent intent = {
5906 .li_opc = LAYOUT_INTENT_ACCESS,
5908 enum ldlm_mode mode;
5912 *gen = ll_layout_version_get(lli);
5913 if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != CL_LAYOUT_GEN_NONE)
5917 LASSERT(fid_is_sane(ll_inode2fid(inode)));
5918 LASSERT(S_ISREG(inode->i_mode));
5920 /* take layout lock mutex to enqueue layout lock exclusively. */
5921 mutex_lock(&lli->lli_layout_mutex);
5924 /* mostly layout lock is caching on the local side, so try to
5925 * match it before grabbing layout lock mutex. */
5926 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
5927 LCK_CR | LCK_CW | LCK_PR |
5929 if (mode != 0) { /* hit cached lock */
5930 rc = ll_layout_lock_set(&lockh, mode, inode);
5936 rc = ll_layout_intent(inode, &intent);
5942 *gen = ll_layout_version_get(lli);
5943 mutex_unlock(&lli->lli_layout_mutex);
5949 * Issue layout intent RPC indicating where in a file an IO is about to write.
5951 * \param[in] inode file inode.
5952 * \param[in] ext write range with start offset of fille in bytes where
5953 * an IO is about to write, and exclusive end offset in
5956 * \retval 0 on success
5957 * \retval < 0 error code
5959 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
5960 struct lu_extent *ext)
5962 struct layout_intent intent = {
5964 .li_extent.e_start = ext->e_start,
5965 .li_extent.e_end = ext->e_end,
5970 rc = ll_layout_intent(inode, &intent);
5976 * This function send a restore request to the MDT
5978 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
5980 struct hsm_user_request *hur;
5984 len = sizeof(struct hsm_user_request) +
5985 sizeof(struct hsm_user_item);
5986 OBD_ALLOC(hur, len);
5990 hur->hur_request.hr_action = HUA_RESTORE;
5991 hur->hur_request.hr_archive_id = 0;
5992 hur->hur_request.hr_flags = 0;
5993 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
5994 sizeof(hur->hur_user_item[0].hui_fid));
5995 hur->hur_user_item[0].hui_extent.offset = offset;
5996 hur->hur_user_item[0].hui_extent.length = length;
5997 hur->hur_request.hr_itemcount = 1;
5998 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,