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;
1602 unsigned int retried = 0, dio_lock = 0;
1603 bool is_aio = false;
1604 struct cl_dio_aio *ci_aio = NULL;
1606 bool partial_io = false;
1607 size_t max_io_pages, max_cached_pages;
1611 CDEBUG(D_VFSTRACE, "%s: %s ppos: %llu, count: %zu\n",
1612 file_dentry(file)->d_name.name,
1613 iot == CIT_READ ? "read" : "write", *ppos, count);
1615 max_io_pages = PTLRPC_MAX_BRW_PAGES * OBD_MAX_RIF_DEFAULT;
1616 max_cached_pages = sbi->ll_cache->ccc_lru_max;
1617 if (max_io_pages > (max_cached_pages >> 2))
1618 max_io_pages = max_cached_pages >> 2;
1620 io = vvp_env_thread_io(env);
1621 if (file->f_flags & O_DIRECT) {
1622 if (!is_sync_kiocb(args->u.normal.via_iocb))
1624 ci_aio = cl_aio_alloc(args->u.normal.via_iocb);
1626 GOTO(out, rc = -ENOMEM);
1631 * IO block size need be aware of cached page limit, otherwise
1632 * if we have small max_cached_mb but large block IO issued, io
1633 * could not be finished and blocked whole client.
1635 if (file->f_flags & O_DIRECT)
1638 per_bytes = min(max_io_pages << PAGE_SHIFT, count);
1639 partial_io = per_bytes < count;
1640 io = vvp_env_thread_io(env);
1641 ll_io_init(io, file, iot, args);
1642 io->ci_aio = ci_aio;
1643 io->ci_dio_lock = dio_lock;
1644 io->ci_ndelay_tried = retried;
1646 if (cl_io_rw_init(env, io, iot, *ppos, per_bytes) == 0) {
1647 bool range_locked = false;
1649 if (file->f_flags & O_APPEND)
1650 range_lock_init(&range, 0, LUSTRE_EOF);
1652 range_lock_init(&range, *ppos, *ppos + per_bytes - 1);
1654 vio->vui_fd = file->private_data;
1655 vio->vui_iter = args->u.normal.via_iter;
1656 vio->vui_iocb = args->u.normal.via_iocb;
1657 /* Direct IO reads must also take range lock,
1658 * or multiple reads will try to work on the same pages
1659 * See LU-6227 for details.
1661 if (((iot == CIT_WRITE) ||
1662 (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1663 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1664 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1666 rc = range_lock(&lli->lli_write_tree, &range);
1670 range_locked = true;
1673 ll_cl_add(file, env, io, LCC_RW);
1674 rc = cl_io_loop(env, io);
1675 ll_cl_remove(file, env);
1678 CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1680 range_unlock(&lli->lli_write_tree, &range);
1683 /* cl_io_rw_init() handled IO */
1688 * In order to move forward AIO, ci_nob was increased,
1689 * but that doesn't mean io have been finished, it just
1690 * means io have been submited, we will always return
1691 * EIOCBQUEUED to the caller, So we could only return
1692 * number of bytes in non-AIO case.
1694 if (io->ci_nob > 0) {
1696 result += io->ci_nob;
1697 *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
1699 count -= io->ci_nob;
1701 /* prepare IO restart */
1703 args->u.normal.via_iter = vio->vui_iter;
1707 * Reexpand iov count because it was zero
1710 iov_iter_reexpand(vio->vui_iter, count);
1711 if (per_bytes == io->ci_nob)
1712 io->ci_need_restart = 1;
1716 cl_io_fini(env, io);
1719 "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1720 file->f_path.dentry->d_name.name,
1721 iot, rc, result, io->ci_need_restart);
1723 if ((rc == 0 || rc == -ENODATA || rc == -ENOLCK) &&
1724 count > 0 && io->ci_need_restart) {
1726 "%s: restart %s from %lld, count: %zu, ret: %zd, rc: %d\n",
1727 file_dentry(file)->d_name.name,
1728 iot == CIT_READ ? "read" : "write",
1729 *ppos, count, result, rc);
1730 /* preserve the tried count for FLR */
1731 retried = io->ci_ndelay_tried;
1732 dio_lock = io->ci_dio_lock;
1738 * VFS will call aio_complete() if no -EIOCBQUEUED
1739 * is returned for AIO, so we can not call aio_complete()
1742 if (rc != -EIOCBQUEUED)
1743 io->ci_aio->cda_no_aio_complete = 1;
1745 * Drop one extra reference so that end_io() could be
1746 * called for this IO context, we could call it after
1747 * we make sure all AIO requests have been proceed.
1749 cl_sync_io_note(env, &io->ci_aio->cda_sync,
1750 rc == -EIOCBQUEUED ? 0 : rc);
1752 cl_aio_free(io->ci_aio);
1757 if (iot == CIT_READ) {
1759 ll_stats_ops_tally(ll_i2sbi(inode),
1760 LPROC_LL_READ_BYTES, result);
1761 } else if (iot == CIT_WRITE) {
1763 ll_stats_ops_tally(ll_i2sbi(inode),
1764 LPROC_LL_WRITE_BYTES, result);
1765 fd->fd_write_failed = false;
1766 } else if (result == 0 && rc == 0) {
1769 fd->fd_write_failed = true;
1771 fd->fd_write_failed = false;
1772 } else if (rc != -ERESTARTSYS) {
1773 fd->fd_write_failed = true;
1777 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1779 ll_heat_add(inode, iot, result);
1781 RETURN(result > 0 ? result : rc);
1785 * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1786 * especially for small I/O.
1788 * To serve a read request, CLIO has to create and initialize a cl_io and
1789 * then request DLM lock. This has turned out to have siginificant overhead
1790 * and affects the performance of small I/O dramatically.
1792 * It's not necessary to create a cl_io for each I/O. Under the help of read
1793 * ahead, most of the pages being read are already in memory cache and we can
1794 * read those pages directly because if the pages exist, the corresponding DLM
1795 * lock must exist so that page content must be valid.
1797 * In fast read implementation, the llite speculatively finds and reads pages
1798 * in memory cache. There are three scenarios for fast read:
1799 * - If the page exists and is uptodate, kernel VM will provide the data and
1800 * CLIO won't be intervened;
1801 * - If the page was brought into memory by read ahead, it will be exported
1802 * and read ahead parameters will be updated;
1803 * - Otherwise the page is not in memory, we can't do fast read. Therefore,
1804 * it will go back and invoke normal read, i.e., a cl_io will be created
1805 * and DLM lock will be requested.
1807 * POSIX compliance: posix standard states that read is intended to be atomic.
1808 * Lustre read implementation is in line with Linux kernel read implementation
1809 * and neither of them complies with POSIX standard in this matter. Fast read
1810 * doesn't make the situation worse on single node but it may interleave write
1811 * results from multiple nodes due to short read handling in ll_file_aio_read().
1813 * \param env - lu_env
1814 * \param iocb - kiocb from kernel
1815 * \param iter - user space buffers where the data will be copied
1817 * \retval - number of bytes have been read, or error code if error occurred.
1820 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1824 if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1827 /* NB: we can't do direct IO for fast read because it will need a lock
1828 * to make IO engine happy. */
1829 if (iocb->ki_filp->f_flags & O_DIRECT)
1832 result = generic_file_read_iter(iocb, iter);
1834 /* If the first page is not in cache, generic_file_aio_read() will be
1835 * returned with -ENODATA.
1836 * See corresponding code in ll_readpage(). */
1837 if (result == -ENODATA)
1841 ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
1842 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1843 LPROC_LL_READ_BYTES, result);
1850 * Read from a file (through the page cache).
1852 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1855 struct vvp_io_args *args;
1856 struct file *file = iocb->ki_filp;
1860 ktime_t kstart = ktime_get();
1863 if (!iov_iter_count(to))
1867 * Currently when PCC read failed, we do not fall back to the
1868 * normal read path, just return the error.
1869 * The resaon is that: for RW-PCC, the file data may be modified
1870 * in the PCC and inconsistent with the data on OSTs (or file
1871 * data has been removed from the Lustre file system), at this
1872 * time, fallback to the normal read path may read the wrong
1874 * TODO: for RO-PCC (readonly PCC), fall back to normal read
1875 * path: read data from data copy on OSTs.
1877 result = pcc_file_read_iter(iocb, to, &cached);
1881 ll_ras_enter(file, iocb->ki_pos, iov_iter_count(to));
1883 result = ll_do_fast_read(iocb, to);
1884 if (result < 0 || iov_iter_count(to) == 0)
1887 env = cl_env_get(&refcheck);
1889 return PTR_ERR(env);
1891 args = ll_env_args(env);
1892 args->u.normal.via_iter = to;
1893 args->u.normal.via_iocb = iocb;
1895 rc2 = ll_file_io_generic(env, args, file, CIT_READ,
1896 &iocb->ki_pos, iov_iter_count(to));
1899 else if (result == 0)
1902 cl_env_put(env, &refcheck);
1905 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
1906 file->private_data, iocb->ki_pos, result,
1908 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_READ,
1909 ktime_us_delta(ktime_get(), kstart));
1916 * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
1917 * If a page is already in the page cache and dirty (and some other things -
1918 * See ll_tiny_write_begin for the instantiation of these rules), then we can
1919 * write to it without doing a full I/O, because Lustre already knows about it
1920 * and will write it out. This saves a lot of processing time.
1922 * All writes here are within one page, so exclusion is handled by the page
1923 * lock on the vm page. We do not do tiny writes for writes which touch
1924 * multiple pages because it's very unlikely multiple sequential pages are
1925 * are already dirty.
1927 * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
1928 * and are unlikely to be to already dirty pages.
1930 * Attribute updates are important here, we do them in ll_tiny_write_end.
1932 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
1934 ssize_t count = iov_iter_count(iter);
1935 struct file *file = iocb->ki_filp;
1936 struct inode *inode = file_inode(file);
1937 bool lock_inode = !IS_NOSEC(inode);
1942 /* Restrict writes to single page and < PAGE_SIZE. See comment at top
1943 * of function for why.
1945 if (count >= PAGE_SIZE ||
1946 (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
1949 if (unlikely(lock_inode))
1951 result = __generic_file_write_iter(iocb, iter);
1953 if (unlikely(lock_inode))
1954 inode_unlock(inode);
1956 /* If the page is not already dirty, ll_tiny_write_begin returns
1957 * -ENODATA. We continue on to normal write.
1959 if (result == -ENODATA)
1963 ll_heat_add(inode, CIT_WRITE, result);
1964 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_WRITE_BYTES,
1966 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
1969 CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
1975 * Write to a file (through the page cache).
1977 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1979 struct vvp_io_args *args;
1981 ssize_t rc_tiny = 0, rc_normal;
1982 struct file *file = iocb->ki_filp;
1985 ktime_t kstart = ktime_get();
1990 if (!iov_iter_count(from))
1991 GOTO(out, rc_normal = 0);
1994 * When PCC write failed, we usually do not fall back to the normal
1995 * write path, just return the error. But there is a special case when
1996 * returned error code is -ENOSPC due to running out of space on PCC HSM
1997 * bakcend. At this time, it will fall back to normal I/O path and
1998 * retry the I/O. As the file is in HSM released state, it will restore
1999 * the file data to OSTs first and redo the write again. And the
2000 * restore process will revoke the layout lock and detach the file
2001 * from PCC cache automatically.
2003 result = pcc_file_write_iter(iocb, from, &cached);
2004 if (cached && result != -ENOSPC && result != -EDQUOT)
2005 GOTO(out, rc_normal = result);
2007 /* NB: we can't do direct IO for tiny writes because they use the page
2008 * cache, we can't do sync writes because tiny writes can't flush
2009 * pages, and we can't do append writes because we can't guarantee the
2010 * required DLM locks are held to protect file size.
2012 if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(file))) &&
2013 !(file->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
2014 rc_tiny = ll_do_tiny_write(iocb, from);
2016 /* In case of error, go on and try normal write - Only stop if tiny
2017 * write completed I/O.
2019 if (iov_iter_count(from) == 0)
2020 GOTO(out, rc_normal = rc_tiny);
2022 env = cl_env_get(&refcheck);
2024 return PTR_ERR(env);
2026 args = ll_env_args(env);
2027 args->u.normal.via_iter = from;
2028 args->u.normal.via_iocb = iocb;
2030 rc_normal = ll_file_io_generic(env, args, file, CIT_WRITE,
2031 &iocb->ki_pos, iov_iter_count(from));
2033 /* On success, combine bytes written. */
2034 if (rc_tiny >= 0 && rc_normal > 0)
2035 rc_normal += rc_tiny;
2036 /* On error, only return error from normal write if tiny write did not
2037 * write any bytes. Otherwise return bytes written by tiny write.
2039 else if (rc_tiny > 0)
2040 rc_normal = rc_tiny;
2042 cl_env_put(env, &refcheck);
2044 if (rc_normal > 0) {
2045 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
2046 file->private_data, iocb->ki_pos,
2048 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_WRITE,
2049 ktime_us_delta(ktime_get(), kstart));
2055 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
2057 * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
2059 static int ll_file_get_iov_count(const struct iovec *iov,
2060 unsigned long *nr_segs, size_t *count,
2066 for (seg = 0; seg < *nr_segs; seg++) {
2067 const struct iovec *iv = &iov[seg];
2070 * If any segment has a negative length, or the cumulative
2071 * length ever wraps negative then return -EINVAL.
2074 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
2076 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
2081 cnt -= iv->iov_len; /* This segment is no good */
2088 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2089 unsigned long nr_segs, loff_t pos)
2096 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_READ);
2103 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2104 iov_iter_init(&to, READ, iov, nr_segs, iov_count);
2105 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2106 iov_iter_init(&to, iov, nr_segs, iov_count, 0);
2107 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2109 result = ll_file_read_iter(iocb, &to);
2114 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
2117 struct iovec iov = { .iov_base = buf, .iov_len = count };
2126 init_sync_kiocb(&kiocb, file);
2127 kiocb.ki_pos = *ppos;
2128 #ifdef HAVE_KIOCB_KI_LEFT
2129 kiocb.ki_left = count;
2130 #elif defined(HAVE_KI_NBYTES)
2131 kiocb.i_nbytes = count;
2134 result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
2135 *ppos = kiocb.ki_pos;
2141 * Write to a file (through the page cache).
2144 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2145 unsigned long nr_segs, loff_t pos)
2147 struct iov_iter from;
2152 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_WRITE);
2159 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2160 iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
2161 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2162 iov_iter_init(&from, iov, nr_segs, iov_count, 0);
2163 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2165 result = ll_file_write_iter(iocb, &from);
2170 static ssize_t ll_file_write(struct file *file, const char __user *buf,
2171 size_t count, loff_t *ppos)
2173 struct iovec iov = { .iov_base = (void __user *)buf,
2183 init_sync_kiocb(&kiocb, file);
2184 kiocb.ki_pos = *ppos;
2185 #ifdef HAVE_KIOCB_KI_LEFT
2186 kiocb.ki_left = count;
2187 #elif defined(HAVE_KI_NBYTES)
2188 kiocb.ki_nbytes = count;
2191 result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
2192 *ppos = kiocb.ki_pos;
2196 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
2198 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
2199 __u64 flags, struct lov_user_md *lum, int lum_size)
2201 struct lookup_intent oit = {
2203 .it_flags = flags | MDS_OPEN_BY_FID,
2208 if ((__swab32(lum->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
2209 le32_to_cpu(LOV_MAGIC_MAGIC)) {
2210 /* this code will only exist for big-endian systems */
2211 lustre_swab_lov_user_md(lum, 0);
2214 ll_inode_size_lock(inode);
2215 rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
2217 GOTO(out_unlock, rc);
2219 ll_release_openhandle(dentry, &oit);
2222 ll_inode_size_unlock(inode);
2223 ll_intent_release(&oit);
2228 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
2229 struct lov_mds_md **lmmp, int *lmm_size,
2230 struct ptlrpc_request **request)
2232 struct ll_sb_info *sbi = ll_i2sbi(inode);
2233 struct mdt_body *body;
2234 struct lov_mds_md *lmm = NULL;
2235 struct ptlrpc_request *req = NULL;
2236 struct md_op_data *op_data;
2241 rc = ll_get_default_mdsize(sbi, &lmmsize);
2245 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
2246 strlen(filename), lmmsize,
2247 LUSTRE_OPC_ANY, NULL);
2248 if (IS_ERR(op_data))
2249 RETURN(PTR_ERR(op_data));
2251 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
2252 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
2253 ll_finish_md_op_data(op_data);
2255 CDEBUG(D_INFO, "md_getattr_name failed "
2256 "on %s: rc %d\n", filename, rc);
2260 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2261 LASSERT(body != NULL); /* checked by mdc_getattr_name */
2263 lmmsize = body->mbo_eadatasize;
2265 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
2267 GOTO(out, rc = -ENODATA);
2269 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
2270 LASSERT(lmm != NULL);
2272 if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
2273 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
2274 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1) &&
2275 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_FOREIGN))
2276 GOTO(out, rc = -EPROTO);
2279 * This is coming from the MDS, so is probably in
2280 * little endian. We convert it to host endian before
2281 * passing it to userspace.
2283 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) {
2284 int stripe_count = 0;
2286 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
2287 lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2288 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
2289 if (le32_to_cpu(lmm->lmm_pattern) &
2290 LOV_PATTERN_F_RELEASED)
2292 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
2294 /* if function called for directory - we should
2295 * avoid swab not existent lsm objects
2297 if (lmm->lmm_magic == LOV_MAGIC_V1 &&
2298 S_ISREG(body->mbo_mode))
2299 lustre_swab_lov_user_md_objects(
2300 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
2302 else if (lmm->lmm_magic == LOV_MAGIC_V3 &&
2303 S_ISREG(body->mbo_mode))
2304 lustre_swab_lov_user_md_objects(
2305 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
2307 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1)) {
2308 lustre_swab_lov_comp_md_v1(
2309 (struct lov_comp_md_v1 *)lmm);
2313 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
2314 struct lov_comp_md_v1 *comp_v1 = NULL;
2315 struct lov_comp_md_entry_v1 *ent;
2316 struct lov_user_md_v1 *v1;
2320 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2321 /* Dump the striping information */
2322 for (; i < comp_v1->lcm_entry_count; i++) {
2323 ent = &comp_v1->lcm_entries[i];
2324 off = ent->lcme_offset;
2325 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2327 "comp[%d]: stripe_count=%u, stripe_size=%u\n",
2328 i, v1->lmm_stripe_count, v1->lmm_stripe_size);
2332 * Return valid stripe_count and stripe_size instead of 0 for
2333 * DoM files to avoid divide-by-zero for older userspace that
2334 * calls this ioctl, e.g. lustre ADIO driver.
2336 if (lmm->lmm_stripe_count == 0)
2337 lmm->lmm_stripe_count = 1;
2338 if (lmm->lmm_stripe_size == 0) {
2339 /* Since the first component of the file data is placed
2340 * on the MDT for faster access, the stripe_size of the
2341 * second one is always that applications which are
2344 if (lmm->lmm_pattern == LOV_PATTERN_MDT)
2345 i = comp_v1->lcm_entry_count > 1 ? 1 : 0;
2347 i = comp_v1->lcm_entry_count > 1 ?
2348 comp_v1->lcm_entry_count - 1 : 0;
2349 ent = &comp_v1->lcm_entries[i];
2350 off = ent->lcme_offset;
2351 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2352 lmm->lmm_stripe_size = v1->lmm_stripe_size;
2357 *lmm_size = lmmsize;
2362 static int ll_lov_setea(struct inode *inode, struct file *file,
2365 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
2366 struct lov_user_md *lump;
2367 int lum_size = sizeof(struct lov_user_md) +
2368 sizeof(struct lov_user_ost_data);
2372 if (!capable(CAP_SYS_ADMIN))
2375 OBD_ALLOC_LARGE(lump, lum_size);
2379 if (copy_from_user(lump, arg, lum_size))
2380 GOTO(out_lump, rc = -EFAULT);
2382 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
2384 cl_lov_delay_create_clear(&file->f_flags);
2387 OBD_FREE_LARGE(lump, lum_size);
2391 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
2398 env = cl_env_get(&refcheck);
2400 RETURN(PTR_ERR(env));
2402 rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
2403 cl_env_put(env, &refcheck);
2407 static int ll_lov_setstripe(struct inode *inode, struct file *file,
2410 struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
2411 struct lov_user_md *klum;
2413 __u64 flags = FMODE_WRITE;
2416 rc = ll_copy_user_md(lum, &klum);
2421 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2426 rc = put_user(0, &lum->lmm_stripe_count);
2430 rc = ll_layout_refresh(inode, &gen);
2434 rc = ll_file_getstripe(inode, arg, lum_size);
2435 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode) &&
2436 ll_i2info(inode)->lli_clob) {
2437 struct iattr attr = { 0 };
2439 rc = cl_setattr_ost(ll_i2info(inode)->lli_clob, &attr,
2440 OP_XVALID_FLAGS, LUSTRE_ENCRYPT_FL);
2443 cl_lov_delay_create_clear(&file->f_flags);
2446 OBD_FREE_LARGE(klum, lum_size);
2452 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2454 struct ll_inode_info *lli = ll_i2info(inode);
2455 struct cl_object *obj = lli->lli_clob;
2456 struct ll_file_data *fd = file->private_data;
2457 struct ll_grouplock grouplock;
2462 CWARN("group id for group lock must not be 0\n");
2466 if (ll_file_nolock(file))
2467 RETURN(-EOPNOTSUPP);
2469 if (file->f_flags & O_NONBLOCK) {
2470 if (!mutex_trylock(&lli->lli_group_mutex))
2473 mutex_lock(&lli->lli_group_mutex);
2475 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2476 CWARN("group lock already existed with gid %lu\n",
2477 fd->fd_grouplock.lg_gid);
2478 GOTO(out, rc = -EINVAL);
2480 if (arg != lli->lli_group_gid && lli->lli_group_users != 0) {
2481 if (file->f_flags & O_NONBLOCK)
2482 GOTO(out, rc = -EAGAIN);
2483 mutex_unlock(&lli->lli_group_mutex);
2484 wait_var_event(&lli->lli_group_users, !lli->lli_group_users);
2485 GOTO(retry, rc = 0);
2487 LASSERT(fd->fd_grouplock.lg_lock == NULL);
2490 * XXX: group lock needs to protect all OST objects while PFL
2491 * can add new OST objects during the IO, so we'd instantiate
2492 * all OST objects before getting its group lock.
2497 struct cl_layout cl = {
2498 .cl_is_composite = false,
2500 struct lu_extent ext = {
2502 .e_end = OBD_OBJECT_EOF,
2505 env = cl_env_get(&refcheck);
2507 GOTO(out, rc = PTR_ERR(env));
2509 rc = cl_object_layout_get(env, obj, &cl);
2510 if (rc >= 0 && cl.cl_is_composite)
2511 rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2514 cl_env_put(env, &refcheck);
2519 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2520 arg, (file->f_flags & O_NONBLOCK), &grouplock);
2525 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2526 fd->fd_grouplock = grouplock;
2527 if (lli->lli_group_users == 0)
2528 lli->lli_group_gid = grouplock.lg_gid;
2529 lli->lli_group_users++;
2531 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2533 mutex_unlock(&lli->lli_group_mutex);
2538 static int ll_put_grouplock(struct inode *inode, struct file *file,
2541 struct ll_inode_info *lli = ll_i2info(inode);
2542 struct ll_file_data *fd = file->private_data;
2543 struct ll_grouplock grouplock;
2547 mutex_lock(&lli->lli_group_mutex);
2548 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2549 CWARN("no group lock held\n");
2550 GOTO(out, rc = -EINVAL);
2553 LASSERT(fd->fd_grouplock.lg_lock != NULL);
2555 if (fd->fd_grouplock.lg_gid != arg) {
2556 CWARN("group lock %lu doesn't match current id %lu\n",
2557 arg, fd->fd_grouplock.lg_gid);
2558 GOTO(out, rc = -EINVAL);
2561 grouplock = fd->fd_grouplock;
2562 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2563 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2565 cl_put_grouplock(&grouplock);
2567 lli->lli_group_users--;
2568 if (lli->lli_group_users == 0) {
2569 lli->lli_group_gid = 0;
2570 wake_up_var(&lli->lli_group_users);
2572 CDEBUG(D_INFO, "group lock %lu released\n", arg);
2575 mutex_unlock(&lli->lli_group_mutex);
2581 * Close inode open handle
2583 * \param dentry [in] dentry which contains the inode
2584 * \param it [in,out] intent which contains open info and result
2587 * \retval <0 failure
2589 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2591 struct inode *inode = dentry->d_inode;
2592 struct obd_client_handle *och;
2598 /* Root ? Do nothing. */
2599 if (is_root_inode(inode))
2602 /* No open handle to close? Move away */
2603 if (!it_disposition(it, DISP_OPEN_OPEN))
2606 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2608 OBD_ALLOC(och, sizeof(*och));
2610 GOTO(out, rc = -ENOMEM);
2612 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2616 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2618 /* this one is in place of ll_file_open */
2619 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2620 ptlrpc_req_finished(it->it_request);
2621 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2627 * Get size for inode for which FIEMAP mapping is requested.
2628 * Make the FIEMAP get_info call and returns the result.
2629 * \param fiemap kernel buffer to hold extens
2630 * \param num_bytes kernel buffer size
2632 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2638 struct ll_fiemap_info_key fmkey = { .lfik_name = KEY_FIEMAP, };
2641 /* Checks for fiemap flags */
2642 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2643 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2647 /* Check for FIEMAP_FLAG_SYNC */
2648 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2649 rc = filemap_fdatawrite(inode->i_mapping);
2654 env = cl_env_get(&refcheck);
2656 RETURN(PTR_ERR(env));
2658 if (i_size_read(inode) == 0) {
2659 rc = ll_glimpse_size(inode);
2664 fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2665 obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2666 obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2668 /* If filesize is 0, then there would be no objects for mapping */
2669 if (fmkey.lfik_oa.o_size == 0) {
2670 fiemap->fm_mapped_extents = 0;
2674 fmkey.lfik_fiemap = *fiemap;
2676 rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2677 &fmkey, fiemap, &num_bytes);
2679 cl_env_put(env, &refcheck);
2683 int ll_fid2path(struct inode *inode, void __user *arg)
2685 struct obd_export *exp = ll_i2mdexp(inode);
2686 const struct getinfo_fid2path __user *gfin = arg;
2688 struct getinfo_fid2path *gfout;
2694 if (!capable(CAP_DAC_READ_SEARCH) &&
2695 !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2698 /* Only need to get the buflen */
2699 if (get_user(pathlen, &gfin->gf_pathlen))
2702 if (pathlen > PATH_MAX)
2705 outsize = sizeof(*gfout) + pathlen;
2706 OBD_ALLOC(gfout, outsize);
2710 if (copy_from_user(gfout, arg, sizeof(*gfout)))
2711 GOTO(gf_free, rc = -EFAULT);
2712 /* append root FID after gfout to let MDT know the root FID so that it
2713 * can lookup the correct path, this is mainly for fileset.
2714 * old server without fileset mount support will ignore this. */
2715 *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
2717 /* Call mdc_iocontrol */
2718 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
2722 if (copy_to_user(arg, gfout, outsize))
2726 OBD_FREE(gfout, outsize);
2731 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
2733 struct cl_object *obj = ll_i2info(inode)->lli_clob;
2741 ioc->idv_version = 0;
2742 ioc->idv_layout_version = UINT_MAX;
2744 /* If no file object initialized, we consider its version is 0. */
2748 env = cl_env_get(&refcheck);
2750 RETURN(PTR_ERR(env));
2752 io = vvp_env_thread_io(env);
2754 io->u.ci_data_version.dv_data_version = 0;
2755 io->u.ci_data_version.dv_layout_version = UINT_MAX;
2756 io->u.ci_data_version.dv_flags = ioc->idv_flags;
2759 if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
2760 result = cl_io_loop(env, io);
2762 result = io->ci_result;
2764 ioc->idv_version = io->u.ci_data_version.dv_data_version;
2765 ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
2767 cl_io_fini(env, io);
2769 if (unlikely(io->ci_need_restart))
2772 cl_env_put(env, &refcheck);
2778 * Read the data_version for inode.
2780 * This value is computed using stripe object version on OST.
2781 * Version is computed using server side locking.
2783 * @param flags if do sync on the OST side;
2785 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
2786 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
2788 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
2790 struct ioc_data_version ioc = { .idv_flags = flags };
2793 rc = ll_ioc_data_version(inode, &ioc);
2795 *data_version = ioc.idv_version;
2801 * Trigger a HSM release request for the provided inode.
2803 int ll_hsm_release(struct inode *inode)
2806 struct obd_client_handle *och = NULL;
2807 __u64 data_version = 0;
2812 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2813 ll_i2sbi(inode)->ll_fsname,
2814 PFID(&ll_i2info(inode)->lli_fid));
2816 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2818 GOTO(out, rc = PTR_ERR(och));
2820 /* Grab latest data_version and [am]time values */
2821 rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
2825 env = cl_env_get(&refcheck);
2827 GOTO(out, rc = PTR_ERR(env));
2829 rc = ll_merge_attr(env, inode);
2830 cl_env_put(env, &refcheck);
2832 /* If error happen, we have the wrong size for a file.
2838 /* Release the file.
2839 * NB: lease lock handle is released in mdc_hsm_release_pack() because
2840 * we still need it to pack l_remote_handle to MDT. */
2841 rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
2847 if (och != NULL && !IS_ERR(och)) /* close the file */
2848 ll_lease_close(och, inode, NULL);
2853 struct ll_swap_stack {
2856 struct inode *inode1;
2857 struct inode *inode2;
2862 static int ll_swap_layouts(struct file *file1, struct file *file2,
2863 struct lustre_swap_layouts *lsl)
2865 struct mdc_swap_layouts msl;
2866 struct md_op_data *op_data;
2869 struct ll_swap_stack *llss = NULL;
2872 OBD_ALLOC_PTR(llss);
2876 llss->inode1 = file_inode(file1);
2877 llss->inode2 = file_inode(file2);
2879 rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
2883 /* we use 2 bool because it is easier to swap than 2 bits */
2884 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2885 llss->check_dv1 = true;
2887 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2888 llss->check_dv2 = true;
2890 /* we cannot use lsl->sl_dvX directly because we may swap them */
2891 llss->dv1 = lsl->sl_dv1;
2892 llss->dv2 = lsl->sl_dv2;
2894 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2895 if (rc == 0) /* same file, done! */
2898 if (rc < 0) { /* sequentialize it */
2899 swap(llss->inode1, llss->inode2);
2901 swap(llss->dv1, llss->dv2);
2902 swap(llss->check_dv1, llss->check_dv2);
2906 if (gid != 0) { /* application asks to flush dirty cache */
2907 rc = ll_get_grouplock(llss->inode1, file1, gid);
2911 rc = ll_get_grouplock(llss->inode2, file2, gid);
2913 ll_put_grouplock(llss->inode1, file1, gid);
2918 /* ultimate check, before swaping the layouts we check if
2919 * dataversion has changed (if requested) */
2920 if (llss->check_dv1) {
2921 rc = ll_data_version(llss->inode1, &dv, 0);
2924 if (dv != llss->dv1)
2925 GOTO(putgl, rc = -EAGAIN);
2928 if (llss->check_dv2) {
2929 rc = ll_data_version(llss->inode2, &dv, 0);
2932 if (dv != llss->dv2)
2933 GOTO(putgl, rc = -EAGAIN);
2936 /* struct md_op_data is used to send the swap args to the mdt
2937 * only flags is missing, so we use struct mdc_swap_layouts
2938 * through the md_op_data->op_data */
2939 /* flags from user space have to be converted before they are send to
2940 * server, no flag is sent today, they are only used on the client */
2943 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
2944 0, LUSTRE_OPC_ANY, &msl);
2945 if (IS_ERR(op_data))
2946 GOTO(free, rc = PTR_ERR(op_data));
2948 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
2949 sizeof(*op_data), op_data, NULL);
2950 ll_finish_md_op_data(op_data);
2957 ll_put_grouplock(llss->inode2, file2, gid);
2958 ll_put_grouplock(llss->inode1, file1, gid);
2968 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
2970 struct obd_export *exp = ll_i2mdexp(inode);
2971 struct md_op_data *op_data;
2975 /* Detect out-of range masks */
2976 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
2979 /* Non-root users are forbidden to set or clear flags which are
2980 * NOT defined in HSM_USER_MASK. */
2981 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
2982 !capable(CAP_SYS_ADMIN))
2985 if (!exp_connect_archive_id_array(exp)) {
2986 /* Detect out-of range archive id */
2987 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
2988 (hss->hss_archive_id > LL_HSM_ORIGIN_MAX_ARCHIVE))
2992 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2993 LUSTRE_OPC_ANY, hss);
2994 if (IS_ERR(op_data))
2995 RETURN(PTR_ERR(op_data));
2997 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, exp, sizeof(*op_data),
3000 ll_finish_md_op_data(op_data);
3005 static int ll_hsm_import(struct inode *inode, struct file *file,
3006 struct hsm_user_import *hui)
3008 struct hsm_state_set *hss = NULL;
3009 struct iattr *attr = NULL;
3013 if (!S_ISREG(inode->i_mode))
3019 GOTO(out, rc = -ENOMEM);
3021 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
3022 hss->hss_archive_id = hui->hui_archive_id;
3023 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
3024 rc = ll_hsm_state_set(inode, hss);
3028 OBD_ALLOC_PTR(attr);
3030 GOTO(out, rc = -ENOMEM);
3032 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
3033 attr->ia_mode |= S_IFREG;
3034 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
3035 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
3036 attr->ia_size = hui->hui_size;
3037 attr->ia_mtime.tv_sec = hui->hui_mtime;
3038 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
3039 attr->ia_atime.tv_sec = hui->hui_atime;
3040 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
3042 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
3043 ATTR_UID | ATTR_GID |
3044 ATTR_MTIME | ATTR_MTIME_SET |
3045 ATTR_ATIME | ATTR_ATIME_SET;
3049 rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
3053 inode_unlock(inode);
3065 static inline long ll_lease_type_from_fmode(fmode_t fmode)
3067 return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
3068 ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
3071 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
3073 struct inode *inode = file_inode(file);
3075 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
3076 ATTR_MTIME | ATTR_MTIME_SET |
3079 .tv_sec = lfu->lfu_atime_sec,
3080 .tv_nsec = lfu->lfu_atime_nsec,
3083 .tv_sec = lfu->lfu_mtime_sec,
3084 .tv_nsec = lfu->lfu_mtime_nsec,
3087 .tv_sec = lfu->lfu_ctime_sec,
3088 .tv_nsec = lfu->lfu_ctime_nsec,
3094 if (!capable(CAP_SYS_ADMIN))
3097 if (!S_ISREG(inode->i_mode))
3101 rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
3103 inode_unlock(inode);
3108 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
3111 case MODE_READ_USER:
3113 case MODE_WRITE_USER:
3120 static const char *const user_lockname[] = LOCK_MODE_NAMES;
3122 /* Used to allow the upper layers of the client to request an LDLM lock
3123 * without doing an actual read or write.
3125 * Used for ladvise lockahead to manually request specific locks.
3127 * \param[in] file file this ladvise lock request is on
3128 * \param[in] ladvise ladvise struct describing this lock request
3130 * \retval 0 success, no detailed result available (sync requests
3131 * and requests sent to the server [not handled locally]
3132 * cannot return detailed results)
3133 * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
3134 * see definitions for details.
3135 * \retval negative negative errno on error
3137 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
3139 struct lu_env *env = NULL;
3140 struct cl_io *io = NULL;
3141 struct cl_lock *lock = NULL;
3142 struct cl_lock_descr *descr = NULL;
3143 struct dentry *dentry = file->f_path.dentry;
3144 struct inode *inode = dentry->d_inode;
3145 enum cl_lock_mode cl_mode;
3146 off_t start = ladvise->lla_start;
3147 off_t end = ladvise->lla_end;
3154 "Lock request: file=%pd, inode=%p, mode=%s start=%llu, end=%llu\n",
3155 dentry, dentry->d_inode,
3156 user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
3159 cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
3161 GOTO(out, result = cl_mode);
3163 /* Get IO environment */
3164 result = cl_io_get(inode, &env, &io, &refcheck);
3168 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
3171 * nothing to do for this io. This currently happens when
3172 * stripe sub-object's are not yet created.
3174 result = io->ci_result;
3175 } else if (result == 0) {
3176 lock = vvp_env_lock(env);
3177 descr = &lock->cll_descr;
3179 descr->cld_obj = io->ci_obj;
3180 /* Convert byte offsets to pages */
3181 descr->cld_start = cl_index(io->ci_obj, start);
3182 descr->cld_end = cl_index(io->ci_obj, end);
3183 descr->cld_mode = cl_mode;
3184 /* CEF_MUST is used because we do not want to convert a
3185 * lockahead request to a lockless lock */
3186 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND;
3188 if (ladvise->lla_peradvice_flags & LF_ASYNC)
3189 descr->cld_enq_flags |= CEF_SPECULATIVE;
3191 result = cl_lock_request(env, io, lock);
3193 /* On success, we need to release the lock */
3195 cl_lock_release(env, lock);
3197 cl_io_fini(env, io);
3198 cl_env_put(env, &refcheck);
3200 /* -ECANCELED indicates a matching lock with a different extent
3201 * was already present, and -EEXIST indicates a matching lock
3202 * on exactly the same extent was already present.
3203 * We convert them to positive values for userspace to make
3204 * recognizing true errors easier.
3205 * Note we can only return these detailed results on async requests,
3206 * as sync requests look the same as i/o requests for locking. */
3207 if (result == -ECANCELED)
3208 result = LLA_RESULT_DIFFERENT;
3209 else if (result == -EEXIST)
3210 result = LLA_RESULT_SAME;
3215 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
3217 static int ll_ladvise_sanity(struct inode *inode,
3218 struct llapi_lu_ladvise *ladvise)
3220 struct ll_sb_info *sbi = ll_i2sbi(inode);
3221 enum lu_ladvise_type advice = ladvise->lla_advice;
3222 /* Note the peradvice flags is a 32 bit field, so per advice flags must
3223 * be in the first 32 bits of enum ladvise_flags */
3224 __u32 flags = ladvise->lla_peradvice_flags;
3225 /* 3 lines at 80 characters per line, should be plenty */
3228 if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
3231 "%s: advice with value '%d' not recognized, last supported advice is %s (value '%d'): rc = %d\n",
3232 sbi->ll_fsname, advice,
3233 ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
3237 /* Per-advice checks */
3239 case LU_LADVISE_LOCKNOEXPAND:
3240 if (flags & ~LF_LOCKNOEXPAND_MASK) {
3242 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3243 "rc = %d\n", sbi->ll_fsname, flags,
3244 ladvise_names[advice], rc);
3248 case LU_LADVISE_LOCKAHEAD:
3249 /* Currently only READ and WRITE modes can be requested */
3250 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
3251 ladvise->lla_lockahead_mode == 0) {
3253 CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
3254 "rc = %d\n", sbi->ll_fsname,
3255 ladvise->lla_lockahead_mode,
3256 ladvise_names[advice], rc);
3260 case LU_LADVISE_WILLREAD:
3261 case LU_LADVISE_DONTNEED:
3263 /* Note fall through above - These checks apply to all advices
3264 * except LOCKNOEXPAND */
3265 if (flags & ~LF_DEFAULT_MASK) {
3267 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3268 "rc = %d\n", sbi->ll_fsname, flags,
3269 ladvise_names[advice], rc);
3272 if (ladvise->lla_start >= ladvise->lla_end) {
3274 CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
3275 "for %s: rc = %d\n", sbi->ll_fsname,
3276 ladvise->lla_start, ladvise->lla_end,
3277 ladvise_names[advice], rc);
3289 * Give file access advices
3291 * The ladvise interface is similar to Linux fadvise() system call, except it
3292 * forwards the advices directly from Lustre client to server. The server side
3293 * codes will apply appropriate read-ahead and caching techniques for the
3294 * corresponding files.
3296 * A typical workload for ladvise is e.g. a bunch of different clients are
3297 * doing small random reads of a file, so prefetching pages into OSS cache
3298 * with big linear reads before the random IO is a net benefit. Fetching
3299 * all that data into each client cache with fadvise() may not be, due to
3300 * much more data being sent to the client.
3302 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
3303 struct llapi_lu_ladvise *ladvise)
3307 struct cl_ladvise_io *lio;
3312 env = cl_env_get(&refcheck);
3314 RETURN(PTR_ERR(env));
3316 io = vvp_env_thread_io(env);
3317 io->ci_obj = ll_i2info(inode)->lli_clob;
3319 /* initialize parameters for ladvise */
3320 lio = &io->u.ci_ladvise;
3321 lio->li_start = ladvise->lla_start;
3322 lio->li_end = ladvise->lla_end;
3323 lio->li_fid = ll_inode2fid(inode);
3324 lio->li_advice = ladvise->lla_advice;
3325 lio->li_flags = flags;
3327 if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
3328 rc = cl_io_loop(env, io);
3332 cl_io_fini(env, io);
3333 cl_env_put(env, &refcheck);
3337 static int ll_lock_noexpand(struct file *file, int flags)
3339 struct ll_file_data *fd = file->private_data;
3341 fd->ll_lock_no_expand = !(flags & LF_UNSET);
3346 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
3349 struct fsxattr fsxattr;
3351 if (copy_from_user(&fsxattr,
3352 (const struct fsxattr __user *)arg,
3356 fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
3357 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
3358 fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
3359 fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
3360 if (copy_to_user((struct fsxattr __user *)arg,
3361 &fsxattr, sizeof(fsxattr)))
3367 int ll_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
3370 * Project Quota ID state is only allowed to change from within the init
3371 * namespace. Enforce that restriction only if we are trying to change
3372 * the quota ID state. Everything else is allowed in user namespaces.
3374 if (current_user_ns() == &init_user_ns)
3377 if (ll_i2info(inode)->lli_projid != fa->fsx_projid)
3380 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags)) {
3381 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
3384 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
3391 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
3394 struct md_op_data *op_data;
3395 struct ptlrpc_request *req = NULL;
3396 struct fsxattr fsxattr;
3397 struct cl_object *obj;
3398 unsigned int inode_flags;
3401 if (copy_from_user(&fsxattr,
3402 (const struct fsxattr __user *)arg,
3406 rc = ll_ioctl_check_project(inode, &fsxattr);
3410 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3411 LUSTRE_OPC_ANY, NULL);
3412 if (IS_ERR(op_data))
3413 RETURN(PTR_ERR(op_data));
3415 inode_flags = ll_xflags_to_inode_flags(fsxattr.fsx_xflags);
3416 op_data->op_attr_flags = ll_inode_to_ext_flags(inode_flags);
3417 if (fsxattr.fsx_xflags & FS_XFLAG_PROJINHERIT)
3418 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
3419 op_data->op_projid = fsxattr.fsx_projid;
3420 op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
3421 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL, 0, &req);
3422 ptlrpc_req_finished(req);
3424 GOTO(out_fsxattr, rc);
3425 ll_update_inode_flags(inode, op_data->op_attr_flags);
3427 /* Avoid OST RPC if this is only ioctl setting project inherit flag */
3428 if (fsxattr.fsx_xflags == 0 ||
3429 fsxattr.fsx_xflags == FS_XFLAG_PROJINHERIT)
3430 GOTO(out_fsxattr, rc);
3432 obj = ll_i2info(inode)->lli_clob;
3434 struct iattr attr = { 0 };
3436 rc = cl_setattr_ost(obj, &attr, OP_XVALID_FLAGS,
3437 fsxattr.fsx_xflags);
3441 ll_finish_md_op_data(op_data);
3445 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
3448 struct inode *inode = file_inode(file);
3449 struct ll_file_data *fd = file->private_data;
3450 struct ll_inode_info *lli = ll_i2info(inode);
3451 struct obd_client_handle *och = NULL;
3452 struct split_param sp;
3453 struct pcc_param param;
3454 bool lease_broken = false;
3456 enum mds_op_bias bias = 0;
3457 struct file *layout_file = NULL;
3459 size_t data_size = 0;
3460 bool attached = false;
3465 mutex_lock(&lli->lli_och_mutex);
3466 if (fd->fd_lease_och != NULL) {
3467 och = fd->fd_lease_och;
3468 fd->fd_lease_och = NULL;
3470 mutex_unlock(&lli->lli_och_mutex);
3475 fmode = och->och_flags;
3477 switch (ioc->lil_flags) {
3478 case LL_LEASE_RESYNC_DONE:
3479 if (ioc->lil_count > IOC_IDS_MAX)
3480 GOTO(out_lease_close, rc = -EINVAL);
3482 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
3483 OBD_ALLOC(data, data_size);
3485 GOTO(out_lease_close, rc = -ENOMEM);
3487 if (copy_from_user(data, (void __user *)arg, data_size))
3488 GOTO(out_lease_close, rc = -EFAULT);
3490 bias = MDS_CLOSE_RESYNC_DONE;
3492 case LL_LEASE_LAYOUT_MERGE: {
3495 if (ioc->lil_count != 1)
3496 GOTO(out_lease_close, rc = -EINVAL);
3498 arg += sizeof(*ioc);
3499 if (copy_from_user(&fd, (void __user *)arg, sizeof(__u32)))
3500 GOTO(out_lease_close, rc = -EFAULT);
3502 layout_file = fget(fd);
3504 GOTO(out_lease_close, rc = -EBADF);
3506 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
3507 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
3508 GOTO(out_lease_close, rc = -EPERM);
3510 data = file_inode(layout_file);
3511 bias = MDS_CLOSE_LAYOUT_MERGE;
3514 case LL_LEASE_LAYOUT_SPLIT: {
3518 if (ioc->lil_count != 2)
3519 GOTO(out_lease_close, rc = -EINVAL);
3521 arg += sizeof(*ioc);
3522 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3523 GOTO(out_lease_close, rc = -EFAULT);
3525 arg += sizeof(__u32);
3526 if (copy_from_user(&mirror_id, (void __user *)arg,
3528 GOTO(out_lease_close, rc = -EFAULT);
3530 layout_file = fget(fdv);
3532 GOTO(out_lease_close, rc = -EBADF);
3534 /* if layout_file == file, it means to destroy the mirror */
3535 sp.sp_inode = file_inode(layout_file);
3536 sp.sp_mirror_id = (__u16)mirror_id;
3538 bias = MDS_CLOSE_LAYOUT_SPLIT;
3541 case LL_LEASE_PCC_ATTACH:
3542 if (ioc->lil_count != 1)
3545 arg += sizeof(*ioc);
3546 if (copy_from_user(¶m.pa_archive_id, (void __user *)arg,
3548 GOTO(out_lease_close, rc2 = -EFAULT);
3550 rc2 = pcc_readwrite_attach(file, inode, param.pa_archive_id);
3552 GOTO(out_lease_close, rc2);
3555 /* Grab latest data version */
3556 rc2 = ll_data_version(inode, ¶m.pa_data_version,
3559 GOTO(out_lease_close, rc2);
3562 bias = MDS_PCC_ATTACH;
3565 /* without close intent */
3570 rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
3574 rc = ll_lease_och_release(inode, file);
3583 switch (ioc->lil_flags) {
3584 case LL_LEASE_RESYNC_DONE:
3586 OBD_FREE(data, data_size);
3588 case LL_LEASE_LAYOUT_MERGE:
3589 case LL_LEASE_LAYOUT_SPLIT:
3593 ll_layout_refresh(inode, &fd->fd_layout_version);
3595 case LL_LEASE_PCC_ATTACH:
3598 rc = pcc_readwrite_attach_fini(file, inode,
3599 param.pa_layout_gen,
3606 rc = ll_lease_type_from_fmode(fmode);
3610 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
3613 struct inode *inode = file_inode(file);
3614 struct ll_inode_info *lli = ll_i2info(inode);
3615 struct ll_file_data *fd = file->private_data;
3616 struct obd_client_handle *och = NULL;
3617 __u64 open_flags = 0;
3623 switch (ioc->lil_mode) {
3624 case LL_LEASE_WRLCK:
3625 if (!(file->f_mode & FMODE_WRITE))
3627 fmode = FMODE_WRITE;
3629 case LL_LEASE_RDLCK:
3630 if (!(file->f_mode & FMODE_READ))
3634 case LL_LEASE_UNLCK:
3635 RETURN(ll_file_unlock_lease(file, ioc, arg));
3640 CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
3642 /* apply for lease */
3643 if (ioc->lil_flags & LL_LEASE_RESYNC)
3644 open_flags = MDS_OPEN_RESYNC;
3645 och = ll_lease_open(inode, file, fmode, open_flags);
3647 RETURN(PTR_ERR(och));
3649 if (ioc->lil_flags & LL_LEASE_RESYNC) {
3650 rc = ll_lease_file_resync(och, inode, arg);
3652 ll_lease_close(och, inode, NULL);
3655 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
3657 ll_lease_close(och, inode, NULL);
3663 mutex_lock(&lli->lli_och_mutex);
3664 if (fd->fd_lease_och == NULL) {
3665 fd->fd_lease_och = och;
3668 mutex_unlock(&lli->lli_och_mutex);
3670 /* impossible now that only excl is supported for now */
3671 ll_lease_close(och, inode, &lease_broken);
3677 static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
3679 struct ll_inode_info *lli = ll_i2info(inode);
3680 struct ll_sb_info *sbi = ll_i2sbi(inode);
3681 __u64 now = ktime_get_real_seconds();
3684 spin_lock(&lli->lli_heat_lock);
3685 heat->lh_flags = lli->lli_heat_flags;
3686 for (i = 0; i < heat->lh_count; i++)
3687 heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
3688 now, sbi->ll_heat_decay_weight,
3689 sbi->ll_heat_period_second);
3690 spin_unlock(&lli->lli_heat_lock);
3693 static int ll_heat_set(struct inode *inode, enum lu_heat_flag flags)
3695 struct ll_inode_info *lli = ll_i2info(inode);
3698 spin_lock(&lli->lli_heat_lock);
3699 if (flags & LU_HEAT_FLAG_CLEAR)
3700 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
3702 if (flags & LU_HEAT_FLAG_OFF)
3703 lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
3705 lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
3707 spin_unlock(&lli->lli_heat_lock);
3713 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3715 struct inode *inode = file_inode(file);
3716 struct ll_file_data *fd = file->private_data;
3720 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
3721 PFID(ll_inode2fid(inode)), inode, cmd);
3722 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
3724 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
3725 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
3729 case LL_IOC_GETFLAGS:
3730 /* Get the current value of the file flags */
3731 return put_user(fd->fd_flags, (int __user *)arg);
3732 case LL_IOC_SETFLAGS:
3733 case LL_IOC_CLRFLAGS:
3734 /* Set or clear specific file flags */
3735 /* XXX This probably needs checks to ensure the flags are
3736 * not abused, and to handle any flag side effects.
3738 if (get_user(flags, (int __user *) arg))
3741 if (cmd == LL_IOC_SETFLAGS) {
3742 if ((flags & LL_FILE_IGNORE_LOCK) &&
3743 !(file->f_flags & O_DIRECT)) {
3744 CERROR("%s: unable to disable locking on "
3745 "non-O_DIRECT file\n", current->comm);
3749 fd->fd_flags |= flags;
3751 fd->fd_flags &= ~flags;
3754 case LL_IOC_LOV_SETSTRIPE:
3755 case LL_IOC_LOV_SETSTRIPE_NEW:
3756 RETURN(ll_lov_setstripe(inode, file, (void __user *)arg));
3757 case LL_IOC_LOV_SETEA:
3758 RETURN(ll_lov_setea(inode, file, (void __user *)arg));
3759 case LL_IOC_LOV_SWAP_LAYOUTS: {
3761 struct lustre_swap_layouts lsl;
3763 if (copy_from_user(&lsl, (char __user *)arg,
3764 sizeof(struct lustre_swap_layouts)))
3767 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
3770 file2 = fget(lsl.sl_fd);
3774 /* O_WRONLY or O_RDWR */
3775 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
3776 GOTO(out, rc = -EPERM);
3778 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
3779 struct inode *inode2;
3780 struct ll_inode_info *lli;
3781 struct obd_client_handle *och = NULL;
3783 lli = ll_i2info(inode);
3784 mutex_lock(&lli->lli_och_mutex);
3785 if (fd->fd_lease_och != NULL) {
3786 och = fd->fd_lease_och;
3787 fd->fd_lease_och = NULL;
3789 mutex_unlock(&lli->lli_och_mutex);
3791 GOTO(out, rc = -ENOLCK);
3792 inode2 = file_inode(file2);
3793 rc = ll_swap_layouts_close(och, inode, inode2);
3795 rc = ll_swap_layouts(file, file2, &lsl);
3801 case LL_IOC_LOV_GETSTRIPE:
3802 case LL_IOC_LOV_GETSTRIPE_NEW:
3803 RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
3804 case FS_IOC_GETFLAGS:
3805 case FS_IOC_SETFLAGS:
3806 RETURN(ll_iocontrol(inode, file, cmd, arg));
3807 case FSFILT_IOC_GETVERSION:
3808 case FS_IOC_GETVERSION:
3809 RETURN(put_user(inode->i_generation, (int __user *)arg));
3810 /* We need to special case any other ioctls we want to handle,
3811 * to send them to the MDS/OST as appropriate and to properly
3812 * network encode the arg field. */
3813 case FS_IOC_SETVERSION:
3816 case LL_IOC_GROUP_LOCK:
3817 RETURN(ll_get_grouplock(inode, file, arg));
3818 case LL_IOC_GROUP_UNLOCK:
3819 RETURN(ll_put_grouplock(inode, file, arg));
3820 case IOC_OBD_STATFS:
3821 RETURN(ll_obd_statfs(inode, (void __user *)arg));
3823 case LL_IOC_FLUSHCTX:
3824 RETURN(ll_flush_ctx(inode));
3825 case LL_IOC_PATH2FID: {
3826 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
3827 sizeof(struct lu_fid)))
3832 case LL_IOC_GETPARENT:
3833 RETURN(ll_getparent(file, (struct getparent __user *)arg));
3835 case OBD_IOC_FID2PATH:
3836 RETURN(ll_fid2path(inode, (void __user *)arg));
3837 case LL_IOC_DATA_VERSION: {
3838 struct ioc_data_version idv;
3841 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
3844 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
3845 rc = ll_ioc_data_version(inode, &idv);
3848 copy_to_user((char __user *)arg, &idv, sizeof(idv)))
3854 case LL_IOC_GET_MDTIDX: {
3857 mdtidx = ll_get_mdt_idx(inode);
3861 if (put_user((int)mdtidx, (int __user *)arg))
3866 case OBD_IOC_GETNAME_OLD:
3868 case OBD_IOC_GETDTNAME:
3870 case OBD_IOC_GETMDNAME:
3871 RETURN(ll_get_obd_name(inode, cmd, arg));
3872 case LL_IOC_HSM_STATE_GET: {
3873 struct md_op_data *op_data;
3874 struct hsm_user_state *hus;
3881 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3882 LUSTRE_OPC_ANY, hus);
3883 if (IS_ERR(op_data)) {
3885 RETURN(PTR_ERR(op_data));
3888 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
3891 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
3894 ll_finish_md_op_data(op_data);
3898 case LL_IOC_HSM_STATE_SET: {
3899 struct hsm_state_set *hss;
3906 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
3911 rc = ll_hsm_state_set(inode, hss);
3916 case LL_IOC_HSM_ACTION: {
3917 struct md_op_data *op_data;
3918 struct hsm_current_action *hca;
3925 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3926 LUSTRE_OPC_ANY, hca);
3927 if (IS_ERR(op_data)) {
3929 RETURN(PTR_ERR(op_data));
3932 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
3935 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
3938 ll_finish_md_op_data(op_data);
3942 case LL_IOC_SET_LEASE_OLD: {
3943 struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
3945 RETURN(ll_file_set_lease(file, &ioc, 0));
3947 case LL_IOC_SET_LEASE: {
3948 struct ll_ioc_lease ioc;
3950 if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
3953 RETURN(ll_file_set_lease(file, &ioc, arg));
3955 case LL_IOC_GET_LEASE: {
3956 struct ll_inode_info *lli = ll_i2info(inode);
3957 struct ldlm_lock *lock = NULL;
3960 mutex_lock(&lli->lli_och_mutex);
3961 if (fd->fd_lease_och != NULL) {
3962 struct obd_client_handle *och = fd->fd_lease_och;
3964 lock = ldlm_handle2lock(&och->och_lease_handle);
3966 lock_res_and_lock(lock);
3967 if (!ldlm_is_cancel(lock))
3968 fmode = och->och_flags;
3970 unlock_res_and_lock(lock);
3971 LDLM_LOCK_PUT(lock);
3974 mutex_unlock(&lli->lli_och_mutex);
3976 RETURN(ll_lease_type_from_fmode(fmode));
3978 case LL_IOC_HSM_IMPORT: {
3979 struct hsm_user_import *hui;
3985 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
3990 rc = ll_hsm_import(inode, file, hui);
3995 case LL_IOC_FUTIMES_3: {
3996 struct ll_futimes_3 lfu;
3998 if (copy_from_user(&lfu,
3999 (const struct ll_futimes_3 __user *)arg,
4003 RETURN(ll_file_futimes_3(file, &lfu));
4005 case LL_IOC_LADVISE: {
4006 struct llapi_ladvise_hdr *k_ladvise_hdr;
4007 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
4010 int alloc_size = sizeof(*k_ladvise_hdr);
4013 u_ladvise_hdr = (void __user *)arg;
4014 OBD_ALLOC_PTR(k_ladvise_hdr);
4015 if (k_ladvise_hdr == NULL)
4018 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4019 GOTO(out_ladvise, rc = -EFAULT);
4021 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
4022 k_ladvise_hdr->lah_count < 1)
4023 GOTO(out_ladvise, rc = -EINVAL);
4025 num_advise = k_ladvise_hdr->lah_count;
4026 if (num_advise >= LAH_COUNT_MAX)
4027 GOTO(out_ladvise, rc = -EFBIG);
4029 OBD_FREE_PTR(k_ladvise_hdr);
4030 alloc_size = offsetof(typeof(*k_ladvise_hdr),
4031 lah_advise[num_advise]);
4032 OBD_ALLOC(k_ladvise_hdr, alloc_size);
4033 if (k_ladvise_hdr == NULL)
4037 * TODO: submit multiple advices to one server in a single RPC
4039 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4040 GOTO(out_ladvise, rc = -EFAULT);
4042 for (i = 0; i < num_advise; i++) {
4043 struct llapi_lu_ladvise *k_ladvise =
4044 &k_ladvise_hdr->lah_advise[i];
4045 struct llapi_lu_ladvise __user *u_ladvise =
4046 &u_ladvise_hdr->lah_advise[i];
4048 rc = ll_ladvise_sanity(inode, k_ladvise);
4050 GOTO(out_ladvise, rc);
4052 switch (k_ladvise->lla_advice) {
4053 case LU_LADVISE_LOCKNOEXPAND:
4054 rc = ll_lock_noexpand(file,
4055 k_ladvise->lla_peradvice_flags);
4056 GOTO(out_ladvise, rc);
4057 case LU_LADVISE_LOCKAHEAD:
4059 rc = ll_file_lock_ahead(file, k_ladvise);
4062 GOTO(out_ladvise, rc);
4065 &u_ladvise->lla_lockahead_result))
4066 GOTO(out_ladvise, rc = -EFAULT);
4069 rc = ll_ladvise(inode, file,
4070 k_ladvise_hdr->lah_flags,
4073 GOTO(out_ladvise, rc);
4080 OBD_FREE(k_ladvise_hdr, alloc_size);
4083 case LL_IOC_FLR_SET_MIRROR: {
4084 /* mirror I/O must be direct to avoid polluting page cache
4086 if (!(file->f_flags & O_DIRECT))
4089 fd->fd_designated_mirror = (__u32)arg;
4092 case FS_IOC_FSGETXATTR:
4093 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
4094 case FS_IOC_FSSETXATTR:
4095 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
4097 RETURN(put_user(PAGE_SIZE, (int __user *)arg));
4098 case LL_IOC_HEAT_GET: {
4099 struct lu_heat uheat;
4100 struct lu_heat *heat;
4103 if (copy_from_user(&uheat, (void __user *)arg, sizeof(uheat)))
4106 if (uheat.lh_count > OBD_HEAT_COUNT)
4107 uheat.lh_count = OBD_HEAT_COUNT;
4109 size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
4110 OBD_ALLOC(heat, size);
4114 heat->lh_count = uheat.lh_count;
4115 ll_heat_get(inode, heat);
4116 rc = copy_to_user((char __user *)arg, heat, size);
4117 OBD_FREE(heat, size);
4118 RETURN(rc ? -EFAULT : 0);
4120 case LL_IOC_HEAT_SET: {
4123 if (copy_from_user(&flags, (void __user *)arg, sizeof(flags)))
4126 rc = ll_heat_set(inode, flags);
4129 case LL_IOC_PCC_DETACH: {
4130 struct lu_pcc_detach *detach;
4132 OBD_ALLOC_PTR(detach);
4136 if (copy_from_user(detach,
4137 (const struct lu_pcc_detach __user *)arg,
4139 GOTO(out_detach_free, rc = -EFAULT);
4141 if (!S_ISREG(inode->i_mode))
4142 GOTO(out_detach_free, rc = -EINVAL);
4144 if (!inode_owner_or_capable(inode))
4145 GOTO(out_detach_free, rc = -EPERM);
4147 rc = pcc_ioctl_detach(inode, detach->pccd_opt);
4149 OBD_FREE_PTR(detach);
4152 case LL_IOC_PCC_STATE: {
4153 struct lu_pcc_state __user *ustate =
4154 (struct lu_pcc_state __user *)arg;
4155 struct lu_pcc_state *state;
4157 OBD_ALLOC_PTR(state);
4161 if (copy_from_user(state, ustate, sizeof(*state)))
4162 GOTO(out_state, rc = -EFAULT);
4164 rc = pcc_ioctl_state(file, inode, state);
4166 GOTO(out_state, rc);
4168 if (copy_to_user(ustate, state, sizeof(*state)))
4169 GOTO(out_state, rc = -EFAULT);
4172 OBD_FREE_PTR(state);
4175 #ifdef HAVE_LUSTRE_CRYPTO
4176 case LL_IOC_SET_ENCRYPTION_POLICY:
4177 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4179 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
4180 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
4181 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4183 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
4184 case LL_IOC_ADD_ENCRYPTION_KEY:
4185 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4187 return llcrypt_ioctl_add_key(file, (void __user *)arg);
4188 case LL_IOC_REMOVE_ENCRYPTION_KEY:
4189 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4191 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
4192 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4193 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4195 return llcrypt_ioctl_remove_key_all_users(file,
4196 (void __user *)arg);
4197 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
4198 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4200 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
4203 case LL_IOC_UNLOCK_FOREIGN: {
4204 struct dentry *dentry = file_dentry(file);
4206 /* if not a foreign symlink do nothing */
4207 if (ll_foreign_is_removable(dentry, true)) {
4209 "prevent unlink of non-foreign file ("DFID")\n",
4210 PFID(ll_inode2fid(inode)));
4211 RETURN(-EOPNOTSUPP);
4217 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
4218 (void __user *)arg));
4222 loff_t ll_lseek(struct file *file, loff_t offset, int whence)
4224 struct inode *inode = file_inode(file);
4227 struct cl_lseek_io *lsio;
4234 env = cl_env_get(&refcheck);
4236 RETURN(PTR_ERR(env));
4238 io = vvp_env_thread_io(env);
4239 io->ci_obj = ll_i2info(inode)->lli_clob;
4240 ll_io_set_mirror(io, file);
4242 lsio = &io->u.ci_lseek;
4243 lsio->ls_start = offset;
4244 lsio->ls_whence = whence;
4245 lsio->ls_result = -ENXIO;
4248 rc = cl_io_init(env, io, CIT_LSEEK, io->ci_obj);
4250 struct vvp_io *vio = vvp_env_io(env);
4252 vio->vui_fd = file->private_data;
4253 rc = cl_io_loop(env, io);
4257 retval = rc ? : lsio->ls_result;
4258 cl_io_fini(env, io);
4259 } while (unlikely(io->ci_need_restart));
4261 cl_env_put(env, &refcheck);
4266 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
4268 struct inode *inode = file_inode(file);
4269 loff_t retval = offset, eof = 0;
4270 ktime_t kstart = ktime_get();
4274 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
4275 PFID(ll_inode2fid(inode)), inode, retval, retval,
4278 if (origin == SEEK_END) {
4279 retval = ll_glimpse_size(inode);
4282 eof = i_size_read(inode);
4285 if (origin == SEEK_HOLE || origin == SEEK_DATA) {
4289 /* flush local cache first if any */
4290 cl_sync_file_range(inode, offset, OBD_OBJECT_EOF,
4293 retval = ll_lseek(file, offset, origin);
4296 retval = vfs_setpos(file, retval, ll_file_maxbytes(inode));
4298 retval = generic_file_llseek_size(file, offset, origin,
4299 ll_file_maxbytes(inode), eof);
4302 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK,
4303 ktime_us_delta(ktime_get(), kstart));
4307 static int ll_flush(struct file *file, fl_owner_t id)
4309 struct inode *inode = file_inode(file);
4310 struct ll_inode_info *lli = ll_i2info(inode);
4311 struct ll_file_data *fd = file->private_data;
4314 LASSERT(!S_ISDIR(inode->i_mode));
4316 /* catch async errors that were recorded back when async writeback
4317 * failed for pages in this mapping. */
4318 rc = lli->lli_async_rc;
4319 lli->lli_async_rc = 0;
4320 if (lli->lli_clob != NULL) {
4321 err = lov_read_and_clear_async_rc(lli->lli_clob);
4326 /* The application has been told write failure already.
4327 * Do not report failure again. */
4328 if (fd->fd_write_failed)
4330 return rc ? -EIO : 0;
4334 * Called to make sure a portion of file has been written out.
4335 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
4337 * Return how many pages have been written.
4339 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
4340 enum cl_fsync_mode mode, int ignore_layout)
4344 struct cl_fsync_io *fio;
4349 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
4350 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
4353 env = cl_env_get(&refcheck);
4355 RETURN(PTR_ERR(env));
4357 io = vvp_env_thread_io(env);
4358 io->ci_obj = ll_i2info(inode)->lli_clob;
4359 io->ci_ignore_layout = ignore_layout;
4361 /* initialize parameters for sync */
4362 fio = &io->u.ci_fsync;
4363 fio->fi_start = start;
4365 fio->fi_fid = ll_inode2fid(inode);
4366 fio->fi_mode = mode;
4367 fio->fi_nr_written = 0;
4369 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
4370 result = cl_io_loop(env, io);
4372 result = io->ci_result;
4374 result = fio->fi_nr_written;
4375 cl_io_fini(env, io);
4376 cl_env_put(env, &refcheck);
4382 * When dentry is provided (the 'else' case), file_dentry() may be
4383 * null and dentry must be used directly rather than pulled from
4384 * file_dentry() as is done otherwise.
4387 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
4389 struct dentry *dentry = file_dentry(file);
4390 struct inode *inode = dentry->d_inode;
4391 struct ll_inode_info *lli = ll_i2info(inode);
4392 struct ptlrpc_request *req;
4393 ktime_t kstart = ktime_get();
4399 "VFS Op:inode="DFID"(%p), start %lld, end %lld, datasync %d\n",
4400 PFID(ll_inode2fid(inode)), inode, start, end, datasync);
4402 /* fsync's caller has already called _fdata{sync,write}, we want
4403 * that IO to finish before calling the osc and mdc sync methods */
4404 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
4407 /* catch async errors that were recorded back when async writeback
4408 * failed for pages in this mapping. */
4409 if (!S_ISDIR(inode->i_mode)) {
4410 err = lli->lli_async_rc;
4411 lli->lli_async_rc = 0;
4414 if (lli->lli_clob != NULL) {
4415 err = lov_read_and_clear_async_rc(lli->lli_clob);
4421 err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
4425 ptlrpc_req_finished(req);
4427 if (S_ISREG(inode->i_mode)) {
4428 struct ll_file_data *fd = file->private_data;
4431 /* Sync metadata on MDT first, and then sync the cached data
4434 err = pcc_fsync(file, start, end, datasync, &cached);
4436 err = cl_sync_file_range(inode, start, end,
4438 if (rc == 0 && err < 0)
4441 fd->fd_write_failed = true;
4443 fd->fd_write_failed = false;
4446 inode_unlock(inode);
4449 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC,
4450 ktime_us_delta(ktime_get(), kstart));
4455 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
4457 struct inode *inode = file_inode(file);
4458 struct ll_sb_info *sbi = ll_i2sbi(inode);
4459 struct ldlm_enqueue_info einfo = {
4460 .ei_type = LDLM_FLOCK,
4461 .ei_cb_cp = ldlm_flock_completion_ast,
4462 .ei_cbdata = file_lock,
4464 struct md_op_data *op_data;
4465 struct lustre_handle lockh = { 0 };
4466 union ldlm_policy_data flock = { { 0 } };
4467 int fl_type = file_lock->fl_type;
4468 ktime_t kstart = ktime_get();
4474 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
4475 PFID(ll_inode2fid(inode)), file_lock);
4477 if (file_lock->fl_flags & FL_FLOCK) {
4478 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
4479 /* flocks are whole-file locks */
4480 flock.l_flock.end = OFFSET_MAX;
4481 /* For flocks owner is determined by the local file desctiptor*/
4482 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
4483 } else if (file_lock->fl_flags & FL_POSIX) {
4484 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
4485 flock.l_flock.start = file_lock->fl_start;
4486 flock.l_flock.end = file_lock->fl_end;
4490 flock.l_flock.pid = file_lock->fl_pid;
4492 #if defined(HAVE_LM_COMPARE_OWNER) || defined(lm_compare_owner)
4493 /* Somewhat ugly workaround for svc lockd.
4494 * lockd installs custom fl_lmops->lm_compare_owner that checks
4495 * for the fl_owner to be the same (which it always is on local node
4496 * I guess between lockd processes) and then compares pid.
4497 * As such we assign pid to the owner field to make it all work,
4498 * conflict with normal locks is unlikely since pid space and
4499 * pointer space for current->files are not intersecting */
4500 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
4501 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
4506 einfo.ei_mode = LCK_PR;
4509 /* An unlock request may or may not have any relation to
4510 * existing locks so we may not be able to pass a lock handle
4511 * via a normal ldlm_lock_cancel() request. The request may even
4512 * unlock a byte range in the middle of an existing lock. In
4513 * order to process an unlock request we need all of the same
4514 * information that is given with a normal read or write record
4515 * lock request. To avoid creating another ldlm unlock (cancel)
4516 * message we'll treat a LCK_NL flock request as an unlock. */
4517 einfo.ei_mode = LCK_NL;
4520 einfo.ei_mode = LCK_PW;
4523 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
4538 flags = LDLM_FL_BLOCK_NOWAIT;
4544 flags = LDLM_FL_TEST_LOCK;
4547 CERROR("unknown fcntl lock command: %d\n", cmd);
4551 /* Save the old mode so that if the mode in the lock changes we
4552 * can decrement the appropriate reader or writer refcount. */
4553 file_lock->fl_type = einfo.ei_mode;
4555 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4556 LUSTRE_OPC_ANY, NULL);
4557 if (IS_ERR(op_data))
4558 RETURN(PTR_ERR(op_data));
4560 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
4561 "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
4562 flock.l_flock.pid, flags, einfo.ei_mode,
4563 flock.l_flock.start, flock.l_flock.end);
4565 rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
4568 /* Restore the file lock type if not TEST lock. */
4569 if (!(flags & LDLM_FL_TEST_LOCK))
4570 file_lock->fl_type = fl_type;
4572 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
4573 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
4574 !(flags & LDLM_FL_TEST_LOCK))
4575 rc2 = locks_lock_file_wait(file, file_lock);
4577 if ((file_lock->fl_flags & FL_FLOCK) &&
4578 (rc == 0 || file_lock->fl_type == F_UNLCK))
4579 rc2 = flock_lock_file_wait(file, file_lock);
4580 if ((file_lock->fl_flags & FL_POSIX) &&
4581 (rc == 0 || file_lock->fl_type == F_UNLCK) &&
4582 !(flags & LDLM_FL_TEST_LOCK))
4583 rc2 = posix_lock_file_wait(file, file_lock);
4584 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
4586 if (rc2 && file_lock->fl_type != F_UNLCK) {
4587 einfo.ei_mode = LCK_NL;
4588 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
4593 ll_finish_md_op_data(op_data);
4596 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK,
4597 ktime_us_delta(ktime_get(), kstart));
4601 int ll_get_fid_by_name(struct inode *parent, const char *name,
4602 int namelen, struct lu_fid *fid,
4603 struct inode **inode)
4605 struct md_op_data *op_data = NULL;
4606 struct mdt_body *body;
4607 struct ptlrpc_request *req;
4611 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
4612 LUSTRE_OPC_ANY, NULL);
4613 if (IS_ERR(op_data))
4614 RETURN(PTR_ERR(op_data));
4616 op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
4617 rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
4618 ll_finish_md_op_data(op_data);
4622 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4624 GOTO(out_req, rc = -EFAULT);
4626 *fid = body->mbo_fid1;
4629 rc = ll_prep_inode(inode, &req->rq_pill, parent->i_sb, NULL);
4631 ptlrpc_req_finished(req);
4635 int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
4638 struct dentry *dchild = NULL;
4639 struct inode *child_inode = NULL;
4640 struct md_op_data *op_data;
4641 struct ptlrpc_request *request = NULL;
4642 struct obd_client_handle *och = NULL;
4644 struct mdt_body *body;
4645 __u64 data_version = 0;
4646 size_t namelen = strlen(name);
4647 int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
4651 CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
4652 PFID(ll_inode2fid(parent)), name,
4653 lum->lum_stripe_offset, lum->lum_stripe_count);
4655 if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
4656 lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
4657 lustre_swab_lmv_user_md(lum);
4659 /* Get child FID first */
4660 qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
4663 dchild = d_lookup(file_dentry(file), &qstr);
4665 if (dchild->d_inode)
4666 child_inode = igrab(dchild->d_inode);
4671 rc = ll_get_fid_by_name(parent, name, namelen, NULL,
4680 if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
4681 OBD_CONNECT2_DIR_MIGRATE)) {
4682 if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
4683 ll_dir_striped(child_inode)) {
4684 CERROR("%s: MDT doesn't support stripe directory "
4685 "migration!\n", ll_i2sbi(parent)->ll_fsname);
4686 GOTO(out_iput, rc = -EOPNOTSUPP);
4691 * lfs migrate command needs to be blocked on the client
4692 * by checking the migrate FID against the FID of the
4695 if (is_root_inode(child_inode))
4696 GOTO(out_iput, rc = -EINVAL);
4698 if (IS_ENCRYPTED(child_inode)) {
4699 rc = llcrypt_get_encryption_info(child_inode);
4702 if (!llcrypt_has_encryption_key(child_inode)) {
4703 CDEBUG(D_SEC, "no enc key for "DFID"\n",
4704 PFID(ll_inode2fid(child_inode)));
4705 GOTO(out_iput, rc = -ENOKEY);
4709 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
4710 child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
4711 if (IS_ERR(op_data))
4712 GOTO(out_iput, rc = PTR_ERR(op_data));
4714 inode_lock(child_inode);
4715 op_data->op_fid3 = *ll_inode2fid(child_inode);
4716 if (!fid_is_sane(&op_data->op_fid3)) {
4717 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
4718 ll_i2sbi(parent)->ll_fsname, name,
4719 PFID(&op_data->op_fid3));
4720 GOTO(out_unlock, rc = -EINVAL);
4723 op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
4724 op_data->op_data = lum;
4725 op_data->op_data_size = lumlen;
4728 if (S_ISREG(child_inode->i_mode)) {
4729 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
4733 GOTO(out_unlock, rc);
4736 rc = ll_data_version(child_inode, &data_version,
4739 GOTO(out_close, rc);
4741 op_data->op_open_handle = och->och_open_handle;
4742 op_data->op_data_version = data_version;
4743 op_data->op_lease_handle = och->och_lease_handle;
4744 op_data->op_bias |= MDS_CLOSE_MIGRATE;
4746 spin_lock(&och->och_mod->mod_open_req->rq_lock);
4747 och->och_mod->mod_open_req->rq_replay = 0;
4748 spin_unlock(&och->och_mod->mod_open_req->rq_lock);
4751 rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name, namelen,
4752 name, namelen, &request);
4754 LASSERT(request != NULL);
4755 ll_update_times(request, parent);
4758 if (rc == 0 || rc == -EAGAIN) {
4759 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
4760 LASSERT(body != NULL);
4762 /* If the server does release layout lock, then we cleanup
4763 * the client och here, otherwise release it in out_close: */
4764 if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
4765 obd_mod_put(och->och_mod);
4766 md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
4768 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
4774 if (request != NULL) {
4775 ptlrpc_req_finished(request);
4779 /* Try again if the lease has cancelled. */
4780 if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
4785 ll_lease_close(och, child_inode, NULL);
4787 clear_nlink(child_inode);
4789 inode_unlock(child_inode);
4790 ll_finish_md_op_data(op_data);
4797 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
4799 struct ll_file_data *fd = file->private_data;
4803 * In order to avoid flood of warning messages, only print one message
4804 * for one file. And the entire message rate on the client is limited
4805 * by CDEBUG_LIMIT too.
4807 if (!(fd->fd_flags & LL_FILE_FLOCK_WARNING)) {
4808 fd->fd_flags |= LL_FILE_FLOCK_WARNING;
4809 CDEBUG_LIMIT(D_TTY | D_CONSOLE,
4810 "flock disabled, mount with '-o [local]flock' to enable\r\n");
4816 * test if some locks matching bits and l_req_mode are acquired
4817 * - bits can be in different locks
4818 * - if found clear the common lock bits in *bits
4819 * - the bits not found, are kept in *bits
4821 * \param bits [IN] searched lock bits [IN]
4822 * \param l_req_mode [IN] searched lock mode
4823 * \retval boolean, true iff all bits are found
4825 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
4827 struct lustre_handle lockh;
4828 union ldlm_policy_data policy;
4829 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
4830 (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
4839 fid = &ll_i2info(inode)->lli_fid;
4840 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
4841 ldlm_lockname[mode]);
4843 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
4844 for (i = 0; i < MDS_INODELOCK_NUMBITS && *bits != 0; i++) {
4845 policy.l_inodebits.bits = *bits & BIT(i);
4846 if (policy.l_inodebits.bits == 0)
4849 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
4850 &policy, mode, &lockh)) {
4851 struct ldlm_lock *lock;
4853 lock = ldlm_handle2lock(&lockh);
4856 ~(lock->l_policy_data.l_inodebits.bits);
4857 LDLM_LOCK_PUT(lock);
4859 *bits &= ~policy.l_inodebits.bits;
4866 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
4867 struct lustre_handle *lockh, __u64 flags,
4868 enum ldlm_mode mode)
4870 union ldlm_policy_data policy = { .l_inodebits = { bits } };
4875 fid = &ll_i2info(inode)->lli_fid;
4876 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
4878 rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
4879 fid, LDLM_IBITS, &policy, mode, lockh);
4884 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
4886 /* Already unlinked. Just update nlink and return success */
4887 if (rc == -ENOENT) {
4889 /* If it is striped directory, and there is bad stripe
4890 * Let's revalidate the dentry again, instead of returning
4892 if (ll_dir_striped(inode))
4895 /* This path cannot be hit for regular files unless in
4896 * case of obscure races, so no need to to validate
4898 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
4900 } else if (rc != 0) {
4901 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
4902 "%s: revalidate FID "DFID" error: rc = %d\n",
4903 ll_i2sbi(inode)->ll_fsname,
4904 PFID(ll_inode2fid(inode)), rc);
4910 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
4912 struct inode *parent;
4913 struct inode *inode = dentry->d_inode;
4914 struct obd_export *exp = ll_i2mdexp(inode);
4915 struct lookup_intent oit = {
4918 struct ptlrpc_request *req = NULL;
4919 struct md_op_data *op_data;
4920 const char *name = NULL;
4925 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
4926 PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
4928 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID) {
4929 parent = dentry->d_parent->d_inode;
4930 name = dentry->d_name.name;
4931 namelen = dentry->d_name.len;
4936 op_data = ll_prep_md_op_data(NULL, parent, inode, name, namelen, 0,
4937 LUSTRE_OPC_ANY, NULL);
4938 if (IS_ERR(op_data))
4939 RETURN(PTR_ERR(op_data));
4941 /* Call getattr by fid */
4942 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID)
4943 op_data->op_flags = MF_GETATTR_BY_FID;
4944 rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
4945 ll_finish_md_op_data(op_data);
4947 rc = ll_inode_revalidate_fini(inode, rc);
4951 rc = ll_revalidate_it_finish(req, &oit, dentry);
4953 ll_intent_release(&oit);
4957 /* Unlinked? Unhash dentry, so it is not picked up later by
4958 * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
4959 * here to preserve get_cwd functionality on 2.6.
4961 if (!dentry->d_inode->i_nlink)
4962 d_lustre_invalidate(dentry);
4964 ll_lookup_finish_locks(&oit, dentry);
4966 ptlrpc_req_finished(req);
4971 static int ll_merge_md_attr(struct inode *inode)
4973 struct ll_inode_info *lli = ll_i2info(inode);
4974 struct cl_attr attr = { 0 };
4977 LASSERT(lli->lli_lsm_md != NULL);
4979 if (!lmv_dir_striped(lli->lli_lsm_md))
4982 down_read(&lli->lli_lsm_sem);
4983 rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
4984 &attr, ll_md_blocking_ast);
4985 up_read(&lli->lli_lsm_sem);
4989 set_nlink(inode, attr.cat_nlink);
4990 inode->i_blocks = attr.cat_blocks;
4991 i_size_write(inode, attr.cat_size);
4993 ll_i2info(inode)->lli_atime = attr.cat_atime;
4994 ll_i2info(inode)->lli_mtime = attr.cat_mtime;
4995 ll_i2info(inode)->lli_ctime = attr.cat_ctime;
5000 int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
5001 unsigned int flags, bool foreign)
5003 struct inode *inode = de->d_inode;
5004 struct ll_sb_info *sbi = ll_i2sbi(inode);
5005 struct ll_inode_info *lli = ll_i2info(inode);
5006 struct inode *dir = de->d_parent->d_inode;
5007 bool need_glimpse = true;
5008 ktime_t kstart = ktime_get();
5011 /* The OST object(s) determine the file size, blocks and mtime. */
5012 if (!(request_mask & STATX_SIZE || request_mask & STATX_BLOCKS ||
5013 request_mask & STATX_MTIME))
5014 need_glimpse = false;
5016 if (dentry_may_statahead(dir, de))
5017 ll_start_statahead(dir, de, need_glimpse &&
5018 !(flags & AT_STATX_DONT_SYNC));
5020 if (flags & AT_STATX_DONT_SYNC)
5021 GOTO(fill_attr, rc = 0);
5023 rc = ll_inode_revalidate(de, IT_GETATTR);
5027 /* foreign file/dir are always of zero length, so don't
5028 * need to validate size.
5030 if (S_ISREG(inode->i_mode) && !foreign) {
5034 GOTO(fill_attr, rc);
5036 rc = pcc_inode_getattr(inode, request_mask, flags, &cached);
5037 if (cached && rc < 0)
5041 GOTO(fill_attr, rc);
5044 * If the returned attr is masked with OBD_MD_FLSIZE &
5045 * OBD_MD_FLBLOCKS & OBD_MD_FLMTIME, it means that the file size
5046 * or blocks obtained from MDT is strictly correct, and the file
5047 * is usually not being modified by clients, and the [a|m|c]time
5048 * got from MDT is also strictly correct.
5049 * Under this circumstance, it does not need to send glimpse
5050 * RPCs to OSTs for file attributes such as the size and blocks.
5052 if (lli->lli_attr_valid & OBD_MD_FLSIZE &&
5053 lli->lli_attr_valid & OBD_MD_FLBLOCKS &&
5054 lli->lli_attr_valid & OBD_MD_FLMTIME) {
5055 inode->i_mtime.tv_sec = lli->lli_mtime;
5056 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5057 inode->i_atime.tv_sec = lli->lli_atime;
5058 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5059 inode->i_ctime.tv_sec = lli->lli_ctime;
5060 GOTO(fill_attr, rc);
5063 /* In case of restore, the MDT has the right size and has
5064 * already send it back without granting the layout lock,
5065 * inode is up-to-date so glimpse is useless.
5066 * Also to glimpse we need the layout, in case of a running
5067 * restore the MDT holds the layout lock so the glimpse will
5068 * block up to the end of restore (getattr will block)
5070 if (!test_bit(LLIF_FILE_RESTORING, &lli->lli_flags)) {
5071 rc = ll_glimpse_size(inode);
5076 /* If object isn't regular a file then don't validate size. */
5077 /* foreign dir is not striped dir */
5078 if (ll_dir_striped(inode) && !foreign) {
5079 rc = ll_merge_md_attr(inode);
5084 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5085 inode->i_atime.tv_sec = lli->lli_atime;
5086 if (lli->lli_attr_valid & OBD_MD_FLMTIME)
5087 inode->i_mtime.tv_sec = lli->lli_mtime;
5088 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5089 inode->i_ctime.tv_sec = lli->lli_ctime;
5093 OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
5095 if (ll_need_32bit_api(sbi)) {
5096 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
5097 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
5098 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
5100 stat->ino = inode->i_ino;
5101 stat->dev = inode->i_sb->s_dev;
5102 stat->rdev = inode->i_rdev;
5105 /* foreign symlink to be exposed as a real symlink */
5107 stat->mode = inode->i_mode;
5109 stat->mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
5111 stat->uid = inode->i_uid;
5112 stat->gid = inode->i_gid;
5113 stat->atime = inode->i_atime;
5114 stat->mtime = inode->i_mtime;
5115 stat->ctime = inode->i_ctime;
5116 /* stat->blksize is used to tell about preferred IO size */
5117 if (sbi->ll_stat_blksize)
5118 stat->blksize = sbi->ll_stat_blksize;
5119 else if (S_ISREG(inode->i_mode))
5120 stat->blksize = 1 << min(PTLRPC_MAX_BRW_BITS + 1,
5121 LL_MAX_BLKSIZE_BITS);
5123 stat->blksize = 1 << inode->i_sb->s_blocksize_bits;
5125 stat->nlink = inode->i_nlink;
5126 stat->size = i_size_read(inode);
5127 stat->blocks = inode->i_blocks;
5129 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5130 if (flags & AT_STATX_DONT_SYNC) {
5131 if (stat->size == 0 &&
5132 lli->lli_attr_valid & OBD_MD_FLLAZYSIZE)
5133 stat->size = lli->lli_lazysize;
5134 if (stat->blocks == 0 &&
5135 lli->lli_attr_valid & OBD_MD_FLLAZYBLOCKS)
5136 stat->blocks = lli->lli_lazyblocks;
5139 if (lli->lli_attr_valid & OBD_MD_FLBTIME) {
5140 stat->result_mask |= STATX_BTIME;
5141 stat->btime.tv_sec = lli->lli_btime;
5144 stat->attributes_mask = STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
5145 stat->attributes |= ll_inode_to_ext_flags(inode->i_flags);
5146 stat->result_mask &= request_mask;
5149 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR,
5150 ktime_us_delta(ktime_get(), kstart));
5155 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5156 int ll_getattr(const struct path *path, struct kstat *stat,
5157 u32 request_mask, unsigned int flags)
5159 return ll_getattr_dentry(path->dentry, stat, request_mask, flags,
5163 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
5165 return ll_getattr_dentry(de, stat, STATX_BASIC_STATS,
5166 AT_STATX_SYNC_AS_STAT, false);
5170 int cl_falloc(struct inode *inode, int mode, loff_t offset, loff_t len)
5176 loff_t size = i_size_read(inode);
5180 env = cl_env_get(&refcheck);
5182 RETURN(PTR_ERR(env));
5184 io = vvp_env_thread_io(env);
5185 io->ci_obj = ll_i2info(inode)->lli_clob;
5186 io->ci_verify_layout = 1;
5187 io->u.ci_setattr.sa_parent_fid = lu_object_fid(&io->ci_obj->co_lu);
5188 io->u.ci_setattr.sa_falloc_mode = mode;
5189 io->u.ci_setattr.sa_falloc_offset = offset;
5190 io->u.ci_setattr.sa_falloc_end = offset + len;
5191 io->u.ci_setattr.sa_subtype = CL_SETATTR_FALLOCATE;
5192 if (io->u.ci_setattr.sa_falloc_end > size) {
5193 loff_t newsize = io->u.ci_setattr.sa_falloc_end;
5195 /* Check new size against VFS/VM file size limit and rlimit */
5196 rc = inode_newsize_ok(inode, newsize);
5199 if (newsize > ll_file_maxbytes(inode)) {
5200 CDEBUG(D_INODE, "file size too large %llu > %llu\n",
5201 (unsigned long long)newsize,
5202 ll_file_maxbytes(inode));
5209 rc = cl_io_init(env, io, CIT_SETATTR, io->ci_obj);
5211 rc = cl_io_loop(env, io);
5214 cl_io_fini(env, io);
5215 } while (unlikely(io->ci_need_restart));
5218 cl_env_put(env, &refcheck);
5222 long ll_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
5224 struct inode *inode = filp->f_path.dentry->d_inode;
5227 if (offset < 0 || len <= 0)
5230 * Encrypted inodes can't handle collapse range or zero range or insert
5231 * range since we would need to re-encrypt blocks with a different IV or
5232 * XTS tweak (which are based on the logical block number).
5233 * Similar to what ext4 does.
5235 if (IS_ENCRYPTED(inode) &&
5236 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
5237 FALLOC_FL_ZERO_RANGE)))
5238 RETURN(-EOPNOTSUPP);
5241 * mode == 0 (which is standard prealloc) and PUNCH is supported
5242 * Rest of mode options are not supported yet.
5244 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
5245 RETURN(-EOPNOTSUPP);
5247 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FALLOCATE, 1);
5249 rc = cl_falloc(inode, mode, offset, len);
5251 * ENOTSUPP (524) is an NFSv3 specific error code erroneously
5252 * used by Lustre in several places. Retuning it here would
5253 * confuse applications that explicity test for EOPNOTSUPP
5254 * (95) and fall back to ftruncate().
5256 if (rc == -ENOTSUPP)
5262 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5263 __u64 start, __u64 len)
5267 struct fiemap *fiemap;
5268 unsigned int extent_count = fieinfo->fi_extents_max;
5270 num_bytes = sizeof(*fiemap) + (extent_count *
5271 sizeof(struct fiemap_extent));
5272 OBD_ALLOC_LARGE(fiemap, num_bytes);
5277 fiemap->fm_flags = fieinfo->fi_flags;
5278 fiemap->fm_extent_count = fieinfo->fi_extents_max;
5279 fiemap->fm_start = start;
5280 fiemap->fm_length = len;
5281 if (extent_count > 0 &&
5282 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
5283 sizeof(struct fiemap_extent)) != 0)
5284 GOTO(out, rc = -EFAULT);
5286 rc = ll_do_fiemap(inode, fiemap, num_bytes);
5288 if (IS_ENCRYPTED(inode)) {
5291 for (i = 0; i < fiemap->fm_mapped_extents; i++)
5292 fiemap->fm_extents[i].fe_flags |=
5293 FIEMAP_EXTENT_DATA_ENCRYPTED |
5294 FIEMAP_EXTENT_ENCODED;
5297 fieinfo->fi_flags = fiemap->fm_flags;
5298 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
5299 if (extent_count > 0 &&
5300 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
5301 fiemap->fm_mapped_extents *
5302 sizeof(struct fiemap_extent)) != 0)
5303 GOTO(out, rc = -EFAULT);
5305 OBD_FREE_LARGE(fiemap, num_bytes);
5309 int ll_inode_permission(struct inode *inode, int mask)
5312 struct ll_sb_info *sbi;
5313 struct root_squash_info *squash;
5314 struct cred *cred = NULL;
5315 const struct cred *old_cred = NULL;
5316 bool squash_id = false;
5317 ktime_t kstart = ktime_get();
5321 if (mask & MAY_NOT_BLOCK)
5325 * as root inode are NOT getting validated in lookup operation,
5326 * need to do it before permission check.
5329 if (is_root_inode(inode)) {
5330 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
5335 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
5336 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
5338 /* squash fsuid/fsgid if needed */
5339 sbi = ll_i2sbi(inode);
5340 squash = &sbi->ll_squash;
5341 if (unlikely(squash->rsi_uid != 0 &&
5342 uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
5343 !(sbi->ll_flags & LL_SBI_NOROOTSQUASH))) {
5347 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
5348 __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
5349 squash->rsi_uid, squash->rsi_gid);
5351 /* update current process's credentials
5352 * and FS capability */
5353 cred = prepare_creds();
5357 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
5358 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
5359 cred->cap_effective = cap_drop_nfsd_set(cred->cap_effective);
5360 cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
5362 old_cred = override_creds(cred);
5365 rc = generic_permission(inode, mask);
5366 /* restore current process's credentials and FS capability */
5368 revert_creds(old_cred);
5373 ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM,
5374 ktime_us_delta(ktime_get(), kstart));
5379 /* -o localflock - only provides locally consistent flock locks */
5380 static const struct file_operations ll_file_operations = {
5381 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5382 # ifdef HAVE_SYNC_READ_WRITE
5383 .read = new_sync_read,
5384 .write = new_sync_write,
5386 .read_iter = ll_file_read_iter,
5387 .write_iter = ll_file_write_iter,
5388 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5389 .read = ll_file_read,
5390 .aio_read = ll_file_aio_read,
5391 .write = ll_file_write,
5392 .aio_write = ll_file_aio_write,
5393 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5394 .unlocked_ioctl = ll_file_ioctl,
5395 .open = ll_file_open,
5396 .release = ll_file_release,
5397 .mmap = ll_file_mmap,
5398 .llseek = ll_file_seek,
5399 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5400 .splice_read = generic_file_splice_read,
5402 .splice_read = pcc_file_splice_read,
5406 .fallocate = ll_fallocate,
5409 static const struct file_operations ll_file_operations_flock = {
5410 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5411 # ifdef HAVE_SYNC_READ_WRITE
5412 .read = new_sync_read,
5413 .write = new_sync_write,
5414 # endif /* HAVE_SYNC_READ_WRITE */
5415 .read_iter = ll_file_read_iter,
5416 .write_iter = ll_file_write_iter,
5417 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5418 .read = ll_file_read,
5419 .aio_read = ll_file_aio_read,
5420 .write = ll_file_write,
5421 .aio_write = ll_file_aio_write,
5422 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5423 .unlocked_ioctl = ll_file_ioctl,
5424 .open = ll_file_open,
5425 .release = ll_file_release,
5426 .mmap = ll_file_mmap,
5427 .llseek = ll_file_seek,
5428 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5429 .splice_read = generic_file_splice_read,
5431 .splice_read = pcc_file_splice_read,
5435 .flock = ll_file_flock,
5436 .lock = ll_file_flock,
5437 .fallocate = ll_fallocate,
5440 /* These are for -o noflock - to return ENOSYS on flock calls */
5441 static const struct file_operations ll_file_operations_noflock = {
5442 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5443 # ifdef HAVE_SYNC_READ_WRITE
5444 .read = new_sync_read,
5445 .write = new_sync_write,
5446 # endif /* HAVE_SYNC_READ_WRITE */
5447 .read_iter = ll_file_read_iter,
5448 .write_iter = ll_file_write_iter,
5449 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5450 .read = ll_file_read,
5451 .aio_read = ll_file_aio_read,
5452 .write = ll_file_write,
5453 .aio_write = ll_file_aio_write,
5454 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5455 .unlocked_ioctl = ll_file_ioctl,
5456 .open = ll_file_open,
5457 .release = ll_file_release,
5458 .mmap = ll_file_mmap,
5459 .llseek = ll_file_seek,
5460 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5461 .splice_read = generic_file_splice_read,
5463 .splice_read = pcc_file_splice_read,
5467 .flock = ll_file_noflock,
5468 .lock = ll_file_noflock,
5469 .fallocate = ll_fallocate,
5472 const struct inode_operations ll_file_inode_operations = {
5473 .setattr = ll_setattr,
5474 .getattr = ll_getattr,
5475 .permission = ll_inode_permission,
5476 #ifdef HAVE_IOP_XATTR
5477 .setxattr = ll_setxattr,
5478 .getxattr = ll_getxattr,
5479 .removexattr = ll_removexattr,
5481 .listxattr = ll_listxattr,
5482 .fiemap = ll_fiemap,
5483 .get_acl = ll_get_acl,
5484 #ifdef HAVE_IOP_SET_ACL
5485 .set_acl = ll_set_acl,
5489 const struct file_operations *ll_select_file_operations(struct ll_sb_info *sbi)
5491 const struct file_operations *fops = &ll_file_operations_noflock;
5493 if (sbi->ll_flags & LL_SBI_FLOCK)
5494 fops = &ll_file_operations_flock;
5495 else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
5496 fops = &ll_file_operations;
5501 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
5503 struct ll_inode_info *lli = ll_i2info(inode);
5504 struct cl_object *obj = lli->lli_clob;
5513 env = cl_env_get(&refcheck);
5515 RETURN(PTR_ERR(env));
5517 rc = cl_conf_set(env, lli->lli_clob, conf);
5521 if (conf->coc_opc == OBJECT_CONF_SET) {
5522 struct ldlm_lock *lock = conf->coc_lock;
5523 struct cl_layout cl = {
5527 LASSERT(lock != NULL);
5528 LASSERT(ldlm_has_layout(lock));
5530 /* it can only be allowed to match after layout is
5531 * applied to inode otherwise false layout would be
5532 * seen. Applying layout shoud happen before dropping
5533 * the intent lock. */
5534 ldlm_lock_allow_match(lock);
5536 rc = cl_object_layout_get(env, obj, &cl);
5541 DFID": layout version change: %u -> %u\n",
5542 PFID(&lli->lli_fid), ll_layout_version_get(lli),
5544 ll_layout_version_set(lli, cl.cl_layout_gen);
5548 cl_env_put(env, &refcheck);
5550 RETURN(rc < 0 ? rc : 0);
5553 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
5554 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
5557 struct ll_sb_info *sbi = ll_i2sbi(inode);
5558 struct ptlrpc_request *req;
5565 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
5566 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
5567 lock->l_lvb_data, lock->l_lvb_len);
5569 if (lock->l_lvb_data != NULL)
5572 /* if layout lock was granted right away, the layout is returned
5573 * within DLM_LVB of dlm reply; otherwise if the lock was ever
5574 * blocked and then granted via completion ast, we have to fetch
5575 * layout here. Please note that we can't use the LVB buffer in
5576 * completion AST because it doesn't have a large enough buffer */
5577 rc = ll_get_default_mdsize(sbi, &lmmsize);
5581 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), OBD_MD_FLXATTR,
5582 XATTR_NAME_LOV, lmmsize, &req);
5585 GOTO(out, rc = 0); /* empty layout */
5592 if (lmmsize == 0) /* empty layout */
5595 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
5597 GOTO(out, rc = -EFAULT);
5599 OBD_ALLOC_LARGE(lvbdata, lmmsize);
5600 if (lvbdata == NULL)
5601 GOTO(out, rc = -ENOMEM);
5603 memcpy(lvbdata, lmm, lmmsize);
5604 lock_res_and_lock(lock);
5605 if (unlikely(lock->l_lvb_data == NULL)) {
5606 lock->l_lvb_type = LVB_T_LAYOUT;
5607 lock->l_lvb_data = lvbdata;
5608 lock->l_lvb_len = lmmsize;
5611 unlock_res_and_lock(lock);
5614 OBD_FREE_LARGE(lvbdata, lmmsize);
5619 ptlrpc_req_finished(req);
5624 * Apply the layout to the inode. Layout lock is held and will be released
5627 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
5628 struct inode *inode)
5630 struct ll_inode_info *lli = ll_i2info(inode);
5631 struct ll_sb_info *sbi = ll_i2sbi(inode);
5632 struct ldlm_lock *lock;
5633 struct cl_object_conf conf;
5636 bool wait_layout = false;
5639 LASSERT(lustre_handle_is_used(lockh));
5641 lock = ldlm_handle2lock(lockh);
5642 LASSERT(lock != NULL);
5643 LASSERT(ldlm_has_layout(lock));
5645 LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
5646 PFID(&lli->lli_fid), inode);
5648 /* in case this is a caching lock and reinstate with new inode */
5649 md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
5651 lock_res_and_lock(lock);
5652 lvb_ready = ldlm_is_lvb_ready(lock);
5653 unlock_res_and_lock(lock);
5655 /* checking lvb_ready is racy but this is okay. The worst case is
5656 * that multi processes may configure the file on the same time. */
5660 rc = ll_layout_fetch(inode, lock);
5664 /* for layout lock, lmm is stored in lock's lvb.
5665 * lvb_data is immutable if the lock is held so it's safe to access it
5668 * set layout to file. Unlikely this will fail as old layout was
5669 * surely eliminated */
5670 memset(&conf, 0, sizeof conf);
5671 conf.coc_opc = OBJECT_CONF_SET;
5672 conf.coc_inode = inode;
5673 conf.coc_lock = lock;
5674 conf.u.coc_layout.lb_buf = lock->l_lvb_data;
5675 conf.u.coc_layout.lb_len = lock->l_lvb_len;
5676 rc = ll_layout_conf(inode, &conf);
5678 /* refresh layout failed, need to wait */
5679 wait_layout = rc == -EBUSY;
5682 LDLM_LOCK_PUT(lock);
5683 ldlm_lock_decref(lockh, mode);
5685 /* wait for IO to complete if it's still being used. */
5687 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
5688 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5690 memset(&conf, 0, sizeof conf);
5691 conf.coc_opc = OBJECT_CONF_WAIT;
5692 conf.coc_inode = inode;
5693 rc = ll_layout_conf(inode, &conf);
5697 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
5698 sbi->ll_fsname, PFID(&lli->lli_fid), rc);
5704 * Issue layout intent RPC to MDS.
5705 * \param inode [in] file inode
5706 * \param intent [in] layout intent
5708 * \retval 0 on success
5709 * \retval < 0 error code
5711 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
5713 struct ll_inode_info *lli = ll_i2info(inode);
5714 struct ll_sb_info *sbi = ll_i2sbi(inode);
5715 struct md_op_data *op_data;
5716 struct lookup_intent it;
5717 struct ptlrpc_request *req;
5721 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
5722 0, 0, LUSTRE_OPC_ANY, NULL);
5723 if (IS_ERR(op_data))
5724 RETURN(PTR_ERR(op_data));
5726 op_data->op_data = intent;
5727 op_data->op_data_size = sizeof(*intent);
5729 memset(&it, 0, sizeof(it));
5730 it.it_op = IT_LAYOUT;
5731 if (intent->li_opc == LAYOUT_INTENT_WRITE ||
5732 intent->li_opc == LAYOUT_INTENT_TRUNC)
5733 it.it_flags = FMODE_WRITE;
5735 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
5736 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5738 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
5739 &ll_md_blocking_ast, 0);
5740 if (it.it_request != NULL)
5741 ptlrpc_req_finished(it.it_request);
5742 it.it_request = NULL;
5744 ll_finish_md_op_data(op_data);
5746 /* set lock data in case this is a new lock */
5748 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
5750 ll_intent_drop_lock(&it);
5756 * This function checks if there exists a LAYOUT lock on the client side,
5757 * or enqueues it if it doesn't have one in cache.
5759 * This function will not hold layout lock so it may be revoked any time after
5760 * this function returns. Any operations depend on layout should be redone
5763 * This function should be called before lov_io_init() to get an uptodate
5764 * layout version, the caller should save the version number and after IO
5765 * is finished, this function should be called again to verify that layout
5766 * is not changed during IO time.
5768 int ll_layout_refresh(struct inode *inode, __u32 *gen)
5770 struct ll_inode_info *lli = ll_i2info(inode);
5771 struct ll_sb_info *sbi = ll_i2sbi(inode);
5772 struct lustre_handle lockh;
5773 struct layout_intent intent = {
5774 .li_opc = LAYOUT_INTENT_ACCESS,
5776 enum ldlm_mode mode;
5780 *gen = ll_layout_version_get(lli);
5781 if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != CL_LAYOUT_GEN_NONE)
5785 LASSERT(fid_is_sane(ll_inode2fid(inode)));
5786 LASSERT(S_ISREG(inode->i_mode));
5788 /* take layout lock mutex to enqueue layout lock exclusively. */
5789 mutex_lock(&lli->lli_layout_mutex);
5792 /* mostly layout lock is caching on the local side, so try to
5793 * match it before grabbing layout lock mutex. */
5794 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
5795 LCK_CR | LCK_CW | LCK_PR |
5797 if (mode != 0) { /* hit cached lock */
5798 rc = ll_layout_lock_set(&lockh, mode, inode);
5804 rc = ll_layout_intent(inode, &intent);
5810 *gen = ll_layout_version_get(lli);
5811 mutex_unlock(&lli->lli_layout_mutex);
5817 * Issue layout intent RPC indicating where in a file an IO is about to write.
5819 * \param[in] inode file inode.
5820 * \param[in] ext write range with start offset of fille in bytes where
5821 * an IO is about to write, and exclusive end offset in
5824 * \retval 0 on success
5825 * \retval < 0 error code
5827 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
5828 struct lu_extent *ext)
5830 struct layout_intent intent = {
5832 .li_extent.e_start = ext->e_start,
5833 .li_extent.e_end = ext->e_end,
5838 rc = ll_layout_intent(inode, &intent);
5844 * This function send a restore request to the MDT
5846 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
5848 struct hsm_user_request *hur;
5852 len = sizeof(struct hsm_user_request) +
5853 sizeof(struct hsm_user_item);
5854 OBD_ALLOC(hur, len);
5858 hur->hur_request.hr_action = HUA_RESTORE;
5859 hur->hur_request.hr_archive_id = 0;
5860 hur->hur_request.hr_flags = 0;
5861 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
5862 sizeof(hur->hur_user_item[0].hui_fid));
5863 hur->hur_user_item[0].hui_extent.offset = offset;
5864 hur->hur_user_item[0].hui_extent.length = length;
5865 hur->hur_request.hr_itemcount = 1;
5866 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,