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 ll_update_dir_depth(parent->d_inode, de->d_inode);
679 /* if DoM bit returned along with LAYOUT bit then there
680 * can be read-on-open data returned.
682 if (bits & MDS_INODELOCK_DOM && bits & MDS_INODELOCK_LAYOUT)
683 ll_dom_finish_open(de->d_inode, req);
687 ptlrpc_req_finished(req);
688 ll_intent_drop_lock(itp);
690 /* We did open by fid, but by the time we got to the server,
691 * the object disappeared. If this is a create, we cannot really
692 * tell the userspace that the file it was trying to create
693 * does not exist. Instead let's return -ESTALE, and the VFS will
694 * retry the create with LOOKUP_REVAL that we are going to catch
695 * in ll_revalidate_dentry() and use lookup then.
697 if (rc == -ENOENT && itp->it_op & IT_CREAT)
703 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
704 struct obd_client_handle *och)
706 struct mdt_body *body;
708 body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
709 och->och_open_handle = body->mbo_open_handle;
710 och->och_fid = body->mbo_fid1;
711 och->och_lease_handle.cookie = it->it_lock_handle;
712 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
713 och->och_flags = it->it_flags;
715 return md_set_open_replay_data(md_exp, och, it);
718 static int ll_local_open(struct file *file, struct lookup_intent *it,
719 struct ll_file_data *fd, struct obd_client_handle *och)
721 struct inode *inode = file_inode(file);
724 LASSERT(!file->private_data);
731 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
736 file->private_data = fd;
737 ll_readahead_init(inode, &fd->fd_ras);
738 fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
739 /* turn off the kernel's read-ahead */
740 file->f_ra.ra_pages = 0;
742 /* ll_cl_context initialize */
743 rwlock_init(&fd->fd_lock);
744 INIT_LIST_HEAD(&fd->fd_lccs);
749 void ll_track_file_opens(struct inode *inode)
751 struct ll_inode_info *lli = ll_i2info(inode);
752 struct ll_sb_info *sbi = ll_i2sbi(inode);
754 /* do not skew results with delays from never-opened inodes */
755 if (ktime_to_ns(lli->lli_close_fd_time))
756 ll_stats_ops_tally(sbi, LPROC_LL_INODE_OPCLTM,
757 ktime_us_delta(ktime_get(), lli->lli_close_fd_time));
759 if (ktime_after(ktime_get(),
760 ktime_add_ms(lli->lli_close_fd_time,
761 sbi->ll_oc_max_ms))) {
762 lli->lli_open_fd_count = 1;
763 lli->lli_close_fd_time = ns_to_ktime(0);
765 lli->lli_open_fd_count++;
768 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_INODE_OCOUNT,
769 lli->lli_open_fd_count);
772 /* Open a file, and (for the very first open) create objects on the OSTs at
773 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
774 * creation or open until ll_lov_setstripe() ioctl is called.
776 * If we already have the stripe MD locally then we don't request it in
777 * md_open(), by passing a lmm_size = 0.
779 * It is up to the application to ensure no other processes open this file
780 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
781 * used. We might be able to avoid races of that sort by getting lli_open_sem
782 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
783 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
785 int ll_file_open(struct inode *inode, struct file *file)
787 struct ll_inode_info *lli = ll_i2info(inode);
788 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
789 .it_flags = file->f_flags };
790 struct obd_client_handle **och_p = NULL;
791 __u64 *och_usecount = NULL;
792 struct ll_file_data *fd;
793 ktime_t kstart = ktime_get();
797 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
798 PFID(ll_inode2fid(inode)), inode, file->f_flags);
800 it = file->private_data; /* XXX: compat macro */
801 file->private_data = NULL; /* prevent ll_local_open assertion */
803 if (S_ISREG(inode->i_mode)) {
804 rc = llcrypt_file_open(inode, file);
806 GOTO(out_nofiledata, rc);
809 fd = ll_file_data_get();
811 GOTO(out_nofiledata, rc = -ENOMEM);
814 if (S_ISDIR(inode->i_mode))
815 ll_authorize_statahead(inode, fd);
817 ll_track_file_opens(inode);
818 if (is_root_inode(inode)) {
819 file->private_data = fd;
823 if (!it || !it->it_disposition) {
824 /* Convert f_flags into access mode. We cannot use file->f_mode,
825 * because everything but O_ACCMODE mask was stripped from
827 if ((oit.it_flags + 1) & O_ACCMODE)
829 if (file->f_flags & O_TRUNC)
830 oit.it_flags |= FMODE_WRITE;
832 /* kernel only call f_op->open in dentry_open. filp_open calls
833 * dentry_open after call to open_namei that checks permissions.
834 * Only nfsd_open call dentry_open directly without checking
835 * permissions and because of that this code below is safe.
837 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
838 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
840 /* We do not want O_EXCL here, presumably we opened the file
841 * already? XXX - NFS implications? */
842 oit.it_flags &= ~O_EXCL;
844 /* bug20584, if "it_flags" contains O_CREAT, the file will be
845 * created if necessary, then "IT_CREAT" should be set to keep
846 * consistent with it */
847 if (oit.it_flags & O_CREAT)
848 oit.it_op |= IT_CREAT;
854 /* Let's see if we have file open on MDS already. */
855 if (it->it_flags & FMODE_WRITE) {
856 och_p = &lli->lli_mds_write_och;
857 och_usecount = &lli->lli_open_fd_write_count;
858 } else if (it->it_flags & FMODE_EXEC) {
859 och_p = &lli->lli_mds_exec_och;
860 och_usecount = &lli->lli_open_fd_exec_count;
862 och_p = &lli->lli_mds_read_och;
863 och_usecount = &lli->lli_open_fd_read_count;
866 mutex_lock(&lli->lli_och_mutex);
867 if (*och_p) { /* Open handle is present */
868 if (it_disposition(it, DISP_OPEN_OPEN)) {
869 /* Well, there's extra open request that we do not need,
870 * let's close it somehow. This will decref request. */
871 rc = it_open_error(DISP_OPEN_OPEN, it);
873 mutex_unlock(&lli->lli_och_mutex);
874 GOTO(out_openerr, rc);
877 ll_release_openhandle(file_dentry(file), it);
881 rc = ll_local_open(file, it, fd, NULL);
884 mutex_unlock(&lli->lli_och_mutex);
885 GOTO(out_openerr, rc);
888 LASSERT(*och_usecount == 0);
889 if (!it->it_disposition) {
890 struct dentry *dentry = file_dentry(file);
891 struct ll_sb_info *sbi = ll_i2sbi(inode);
892 struct ll_dentry_data *ldd;
894 /* We cannot just request lock handle now, new ELC code
895 * means that one of other OPEN locks for this file
896 * could be cancelled, and since blocking ast handler
897 * would attempt to grab och_mutex as well, that would
898 * result in a deadlock
900 mutex_unlock(&lli->lli_och_mutex);
902 * Normally called under two situations:
904 * 2. A race/condition on MDS resulting in no open
905 * handle to be returned from LOOKUP|OPEN request,
906 * for example if the target entry was a symlink.
908 * In NFS path we know there's pathologic behavior
909 * so we always enable open lock caching when coming
910 * from there. It's detected by setting a flag in
913 * After reaching number of opens of this inode
914 * we always ask for an open lock on it to handle
915 * bad userspace actors that open and close files
916 * in a loop for absolutely no good reason
919 ldd = ll_d2d(dentry);
920 if (filename_is_volatile(dentry->d_name.name,
923 /* There really is nothing here, but this
924 * make this more readable I think.
925 * We do not want openlock for volatile
926 * files under any circumstances
928 } else if (ldd && ldd->lld_nfs_dentry) {
929 /* NFS path. This also happens to catch
930 * open by fh files I guess
932 it->it_flags |= MDS_OPEN_LOCK;
933 /* clear the flag for future lookups */
934 ldd->lld_nfs_dentry = 0;
935 } else if (sbi->ll_oc_thrsh_count > 0) {
936 /* Take MDS_OPEN_LOCK with many opens */
937 if (lli->lli_open_fd_count >=
938 sbi->ll_oc_thrsh_count)
939 it->it_flags |= MDS_OPEN_LOCK;
941 /* If this is open after we just closed */
942 else if (ktime_before(ktime_get(),
943 ktime_add_ms(lli->lli_close_fd_time,
944 sbi->ll_oc_thrsh_ms)))
945 it->it_flags |= MDS_OPEN_LOCK;
949 * Always specify MDS_OPEN_BY_FID because we don't want
950 * to get file with different fid.
952 it->it_flags |= MDS_OPEN_BY_FID;
953 rc = ll_intent_file_open(dentry, NULL, 0, it);
955 GOTO(out_openerr, rc);
959 OBD_ALLOC(*och_p, sizeof(struct obd_client_handle));
961 GOTO(out_och_free, rc = -ENOMEM);
965 /* md_intent_lock() didn't get a request ref if there was an
966 * open error, so don't do cleanup on the request here
968 /* XXX (green): Should not we bail out on any error here, not
969 * just open error? */
970 rc = it_open_error(DISP_OPEN_OPEN, it);
972 GOTO(out_och_free, rc);
974 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
975 "inode %p: disposition %x, status %d\n", inode,
976 it_disposition(it, ~0), it->it_status);
978 rc = ll_local_open(file, it, fd, *och_p);
980 GOTO(out_och_free, rc);
983 rc = pcc_file_open(inode, file);
985 GOTO(out_och_free, rc);
987 mutex_unlock(&lli->lli_och_mutex);
991 /* Must do this outside lli_och_mutex lock to prevent deadlock where
992 different kind of OPEN lock for this same inode gets cancelled
993 by ldlm_cancel_lru */
994 if (!S_ISREG(inode->i_mode))
995 GOTO(out_och_free, rc);
996 cl_lov_delay_create_clear(&file->f_flags);
997 GOTO(out_och_free, rc);
1001 if (och_p && *och_p) {
1002 OBD_FREE(*och_p, sizeof(struct obd_client_handle));
1003 *och_p = NULL; /* OBD_FREE writes some magic there */
1006 mutex_unlock(&lli->lli_och_mutex);
1009 if (lli->lli_opendir_key == fd)
1010 ll_deauthorize_statahead(inode, fd);
1013 ll_file_data_put(fd);
1015 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN,
1016 ktime_us_delta(ktime_get(), kstart));
1020 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
1021 ptlrpc_req_finished(it->it_request);
1022 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
1028 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
1029 struct ldlm_lock_desc *desc, void *data, int flag)
1032 struct lustre_handle lockh;
1036 case LDLM_CB_BLOCKING:
1037 ldlm_lock2handle(lock, &lockh);
1038 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
1040 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
1044 case LDLM_CB_CANCELING:
1052 * When setting a lease on a file, we take ownership of the lli_mds_*_och
1053 * and save it as fd->fd_och so as to force client to reopen the file even
1054 * if it has an open lock in cache already.
1056 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
1057 struct lustre_handle *old_open_handle)
1059 struct ll_inode_info *lli = ll_i2info(inode);
1060 struct ll_file_data *fd = file->private_data;
1061 struct obd_client_handle **och_p;
1062 __u64 *och_usecount;
1066 /* Get the openhandle of the file */
1067 mutex_lock(&lli->lli_och_mutex);
1068 if (fd->fd_lease_och != NULL)
1069 GOTO(out_unlock, rc = -EBUSY);
1071 if (fd->fd_och == NULL) {
1072 if (file->f_mode & FMODE_WRITE) {
1073 LASSERT(lli->lli_mds_write_och != NULL);
1074 och_p = &lli->lli_mds_write_och;
1075 och_usecount = &lli->lli_open_fd_write_count;
1077 LASSERT(lli->lli_mds_read_och != NULL);
1078 och_p = &lli->lli_mds_read_och;
1079 och_usecount = &lli->lli_open_fd_read_count;
1082 if (*och_usecount > 1)
1083 GOTO(out_unlock, rc = -EBUSY);
1085 fd->fd_och = *och_p;
1090 *old_open_handle = fd->fd_och->och_open_handle;
1094 mutex_unlock(&lli->lli_och_mutex);
1099 * Release ownership on lli_mds_*_och when putting back a file lease.
1101 static int ll_lease_och_release(struct inode *inode, struct file *file)
1103 struct ll_inode_info *lli = ll_i2info(inode);
1104 struct ll_file_data *fd = file->private_data;
1105 struct obd_client_handle **och_p;
1106 struct obd_client_handle *old_och = NULL;
1107 __u64 *och_usecount;
1111 mutex_lock(&lli->lli_och_mutex);
1112 if (file->f_mode & FMODE_WRITE) {
1113 och_p = &lli->lli_mds_write_och;
1114 och_usecount = &lli->lli_open_fd_write_count;
1116 och_p = &lli->lli_mds_read_och;
1117 och_usecount = &lli->lli_open_fd_read_count;
1120 /* The file may have been open by another process (broken lease) so
1121 * *och_p is not NULL. In this case we should simply increase usecount
1124 if (*och_p != NULL) {
1125 old_och = fd->fd_och;
1128 *och_p = fd->fd_och;
1132 mutex_unlock(&lli->lli_och_mutex);
1134 if (old_och != NULL)
1135 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
1141 * Acquire a lease and open the file.
1143 static struct obd_client_handle *
1144 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
1147 struct lookup_intent it = { .it_op = IT_OPEN };
1148 struct ll_sb_info *sbi = ll_i2sbi(inode);
1149 struct md_op_data *op_data;
1150 struct ptlrpc_request *req = NULL;
1151 struct lustre_handle old_open_handle = { 0 };
1152 struct obd_client_handle *och = NULL;
1157 if (fmode != FMODE_WRITE && fmode != FMODE_READ)
1158 RETURN(ERR_PTR(-EINVAL));
1161 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
1162 RETURN(ERR_PTR(-EPERM));
1164 rc = ll_lease_och_acquire(inode, file, &old_open_handle);
1166 RETURN(ERR_PTR(rc));
1171 RETURN(ERR_PTR(-ENOMEM));
1173 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
1174 LUSTRE_OPC_ANY, NULL);
1175 if (IS_ERR(op_data))
1176 GOTO(out, rc = PTR_ERR(op_data));
1178 /* To tell the MDT this openhandle is from the same owner */
1179 op_data->op_open_handle = old_open_handle;
1181 it.it_flags = fmode | open_flags;
1182 it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
1183 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
1184 &ll_md_blocking_lease_ast,
1185 /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
1186 * it can be cancelled which may mislead applications that the lease is
1188 * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
1189 * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
1190 * doesn't deal with openhandle, so normal openhandle will be leaked. */
1191 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
1192 ll_finish_md_op_data(op_data);
1193 ptlrpc_req_finished(req);
1195 GOTO(out_release_it, rc);
1197 if (it_disposition(&it, DISP_LOOKUP_NEG))
1198 GOTO(out_release_it, rc = -ENOENT);
1200 rc = it_open_error(DISP_OPEN_OPEN, &it);
1202 GOTO(out_release_it, rc);
1204 LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
1205 rc = ll_och_fill(sbi->ll_md_exp, &it, och);
1207 GOTO(out_release_it, rc);
1209 if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
1210 GOTO(out_close, rc = -EOPNOTSUPP);
1212 /* already get lease, handle lease lock */
1213 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
1214 if (!it.it_lock_mode ||
1215 !(it.it_lock_bits & MDS_INODELOCK_OPEN)) {
1216 /* open lock must return for lease */
1217 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
1218 PFID(ll_inode2fid(inode)), it.it_lock_mode,
1220 GOTO(out_close, rc = -EPROTO);
1223 ll_intent_release(&it);
1227 /* Cancel open lock */
1228 if (it.it_lock_mode != 0) {
1229 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
1231 it.it_lock_mode = 0;
1232 och->och_lease_handle.cookie = 0ULL;
1234 rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
1236 CERROR("%s: error closing file "DFID": %d\n",
1237 sbi->ll_fsname, PFID(&ll_i2info(inode)->lli_fid), rc2);
1238 och = NULL; /* och has been freed in ll_close_inode_openhandle() */
1240 ll_intent_release(&it);
1244 RETURN(ERR_PTR(rc));
1248 * Check whether a layout swap can be done between two inodes.
1250 * \param[in] inode1 First inode to check
1251 * \param[in] inode2 Second inode to check
1253 * \retval 0 on success, layout swap can be performed between both inodes
1254 * \retval negative error code if requirements are not met
1256 static int ll_check_swap_layouts_validity(struct inode *inode1,
1257 struct inode *inode2)
1259 if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
1262 if (inode_permission(inode1, MAY_WRITE) ||
1263 inode_permission(inode2, MAY_WRITE))
1266 if (inode1->i_sb != inode2->i_sb)
1272 static int ll_swap_layouts_close(struct obd_client_handle *och,
1273 struct inode *inode, struct inode *inode2)
1275 const struct lu_fid *fid1 = ll_inode2fid(inode);
1276 const struct lu_fid *fid2;
1280 CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
1281 ll_i2sbi(inode)->ll_fsname, PFID(fid1));
1283 rc = ll_check_swap_layouts_validity(inode, inode2);
1285 GOTO(out_free_och, rc);
1287 /* We now know that inode2 is a lustre inode */
1288 fid2 = ll_inode2fid(inode2);
1290 rc = lu_fid_cmp(fid1, fid2);
1292 GOTO(out_free_och, rc = -EINVAL);
1294 /* Close the file and {swap,merge} layouts between inode & inode2.
1295 * NB: lease lock handle is released in mdc_close_layout_swap_pack()
1296 * because we still need it to pack l_remote_handle to MDT. */
1297 rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
1300 och = NULL; /* freed in ll_close_inode_openhandle() */
1310 * Release lease and close the file.
1311 * It will check if the lease has ever broken.
1313 static int ll_lease_close_intent(struct obd_client_handle *och,
1314 struct inode *inode,
1315 bool *lease_broken, enum mds_op_bias bias,
1318 struct ldlm_lock *lock;
1319 bool cancelled = true;
1323 lock = ldlm_handle2lock(&och->och_lease_handle);
1325 lock_res_and_lock(lock);
1326 cancelled = ldlm_is_cancel(lock);
1327 unlock_res_and_lock(lock);
1328 LDLM_LOCK_PUT(lock);
1331 CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
1332 PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
1334 if (lease_broken != NULL)
1335 *lease_broken = cancelled;
1337 if (!cancelled && !bias)
1338 ldlm_cli_cancel(&och->och_lease_handle, 0);
1340 if (cancelled) { /* no need to excute intent */
1345 rc = ll_close_inode_openhandle(inode, och, bias, data);
1349 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
1352 return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
1356 * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
1358 static int ll_lease_file_resync(struct obd_client_handle *och,
1359 struct inode *inode, unsigned long arg)
1361 struct ll_sb_info *sbi = ll_i2sbi(inode);
1362 struct md_op_data *op_data;
1363 struct ll_ioc_lease_id ioc;
1364 __u64 data_version_unused;
1368 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1369 LUSTRE_OPC_ANY, NULL);
1370 if (IS_ERR(op_data))
1371 RETURN(PTR_ERR(op_data));
1373 if (copy_from_user(&ioc, (struct ll_ioc_lease_id __user *)arg,
1377 /* before starting file resync, it's necessary to clean up page cache
1378 * in client memory, otherwise once the layout version is increased,
1379 * writing back cached data will be denied the OSTs. */
1380 rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
1384 op_data->op_lease_handle = och->och_lease_handle;
1385 op_data->op_mirror_id = ioc.lil_mirror_id;
1386 rc = md_file_resync(sbi->ll_md_exp, op_data);
1392 ll_finish_md_op_data(op_data);
1396 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1398 struct ll_inode_info *lli = ll_i2info(inode);
1399 struct cl_object *obj = lli->lli_clob;
1400 struct cl_attr *attr = vvp_env_thread_attr(env);
1408 ll_inode_size_lock(inode);
1410 /* Merge timestamps the most recently obtained from MDS with
1411 * timestamps obtained from OSTs.
1413 * Do not overwrite atime of inode because it may be refreshed
1414 * by file_accessed() function. If the read was served by cache
1415 * data, there is no RPC to be sent so that atime may not be
1416 * transferred to OSTs at all. MDT only updates atime at close time
1417 * if it's at least 'mdd.*.atime_diff' older.
1418 * All in all, the atime in Lustre does not strictly comply with
1419 * POSIX. Solving this problem needs to send an RPC to MDT for each
1420 * read, this will hurt performance.
1422 if (test_and_clear_bit(LLIF_UPDATE_ATIME, &lli->lli_flags) ||
1423 inode->i_atime.tv_sec < lli->lli_atime)
1424 inode->i_atime.tv_sec = lli->lli_atime;
1426 inode->i_mtime.tv_sec = lli->lli_mtime;
1427 inode->i_ctime.tv_sec = lli->lli_ctime;
1429 mtime = inode->i_mtime.tv_sec;
1430 atime = inode->i_atime.tv_sec;
1431 ctime = inode->i_ctime.tv_sec;
1433 cl_object_attr_lock(obj);
1434 if (OBD_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
1437 rc = cl_object_attr_get(env, obj, attr);
1438 cl_object_attr_unlock(obj);
1441 GOTO(out_size_unlock, rc = (rc == -ENODATA ? 0 : rc));
1443 if (atime < attr->cat_atime)
1444 atime = attr->cat_atime;
1446 if (ctime < attr->cat_ctime)
1447 ctime = attr->cat_ctime;
1449 if (mtime < attr->cat_mtime)
1450 mtime = attr->cat_mtime;
1452 CDEBUG(D_VFSTRACE, DFID" updating i_size %llu\n",
1453 PFID(&lli->lli_fid), attr->cat_size);
1455 i_size_write(inode, attr->cat_size);
1456 inode->i_blocks = attr->cat_blocks;
1458 inode->i_mtime.tv_sec = mtime;
1459 inode->i_atime.tv_sec = atime;
1460 inode->i_ctime.tv_sec = ctime;
1463 ll_inode_size_unlock(inode);
1469 * Set designated mirror for I/O.
1471 * So far only read, write, and truncated can support to issue I/O to
1472 * designated mirror.
1474 void ll_io_set_mirror(struct cl_io *io, const struct file *file)
1476 struct ll_file_data *fd = file->private_data;
1478 /* clear layout version for generic(non-resync) I/O in case it carries
1479 * stale layout version due to I/O restart */
1480 io->ci_layout_version = 0;
1482 /* FLR: disable non-delay for designated mirror I/O because obviously
1483 * only one mirror is available */
1484 if (fd->fd_designated_mirror > 0) {
1486 io->ci_designated_mirror = fd->fd_designated_mirror;
1487 io->ci_layout_version = fd->fd_layout_version;
1490 CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
1491 file->f_path.dentry->d_name.name, io->ci_designated_mirror);
1494 static bool file_is_noatime(const struct file *file)
1496 const struct vfsmount *mnt = file->f_path.mnt;
1497 const struct inode *inode = file_inode((struct file *)file);
1499 /* Adapted from file_accessed() and touch_atime().*/
1500 if (file->f_flags & O_NOATIME)
1503 if (inode->i_flags & S_NOATIME)
1506 if (IS_NOATIME(inode))
1509 if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1512 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1515 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
1521 void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot,
1522 struct vvp_io_args *args)
1524 struct inode *inode = file_inode(file);
1525 struct ll_file_data *fd = file->private_data;
1527 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1528 io->ci_lock_no_expand = fd->ll_lock_no_expand;
1530 if (iot == CIT_WRITE) {
1531 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1532 io->u.ci_wr.wr_sync = !!(file->f_flags & O_SYNC ||
1533 file->f_flags & O_DIRECT ||
1535 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1536 io->u.ci_wr.wr_sync |= !!(args &&
1537 (args->u.normal.via_iocb->ki_flags &
1542 io->ci_obj = ll_i2info(inode)->lli_clob;
1543 io->ci_lockreq = CILR_MAYBE;
1544 if (ll_file_nolock(file)) {
1545 io->ci_lockreq = CILR_NEVER;
1546 io->ci_no_srvlock = 1;
1547 } else if (file->f_flags & O_APPEND) {
1548 io->ci_lockreq = CILR_MANDATORY;
1550 io->ci_noatime = file_is_noatime(file);
1551 io->ci_async_readahead = false;
1553 /* FLR: only use non-delay I/O for read as there is only one
1554 * avaliable mirror for write. */
1555 io->ci_ndelay = !(iot == CIT_WRITE);
1557 ll_io_set_mirror(io, file);
1560 static void ll_heat_add(struct inode *inode, enum cl_io_type iot,
1563 struct ll_inode_info *lli = ll_i2info(inode);
1564 struct ll_sb_info *sbi = ll_i2sbi(inode);
1565 enum obd_heat_type sample_type;
1566 enum obd_heat_type iobyte_type;
1567 __u64 now = ktime_get_real_seconds();
1569 if (!ll_sbi_has_file_heat(sbi) ||
1570 lli->lli_heat_flags & LU_HEAT_FLAG_OFF)
1573 if (iot == CIT_READ) {
1574 sample_type = OBD_HEAT_READSAMPLE;
1575 iobyte_type = OBD_HEAT_READBYTE;
1576 } else if (iot == CIT_WRITE) {
1577 sample_type = OBD_HEAT_WRITESAMPLE;
1578 iobyte_type = OBD_HEAT_WRITEBYTE;
1583 spin_lock(&lli->lli_heat_lock);
1584 obd_heat_add(&lli->lli_heat_instances[sample_type], now, 1,
1585 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1586 obd_heat_add(&lli->lli_heat_instances[iobyte_type], now, count,
1587 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1588 spin_unlock(&lli->lli_heat_lock);
1592 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1593 struct file *file, enum cl_io_type iot,
1594 loff_t *ppos, size_t count)
1596 struct vvp_io *vio = vvp_env_io(env);
1597 struct inode *inode = file_inode(file);
1598 struct ll_inode_info *lli = ll_i2info(inode);
1599 struct ll_sb_info *sbi = ll_i2sbi(inode);
1600 struct ll_file_data *fd = file->private_data;
1601 struct range_lock range;
1602 bool range_locked = false;
1607 unsigned int retried = 0, dio_lock = 0;
1608 bool is_aio = false;
1609 bool is_parallel_dio = false;
1610 struct cl_dio_aio *ci_aio = NULL;
1612 bool partial_io = false;
1613 size_t max_io_pages, max_cached_pages;
1617 CDEBUG(D_VFSTRACE, "%s: %s ppos: %llu, count: %zu\n",
1618 file_dentry(file)->d_name.name,
1619 iot == CIT_READ ? "read" : "write", *ppos, count);
1621 max_io_pages = PTLRPC_MAX_BRW_PAGES * OBD_MAX_RIF_DEFAULT;
1622 max_cached_pages = sbi->ll_cache->ccc_lru_max;
1623 if (max_io_pages > (max_cached_pages >> 2))
1624 max_io_pages = max_cached_pages >> 2;
1626 io = vvp_env_thread_io(env);
1627 if (file->f_flags & O_DIRECT) {
1628 if (!is_sync_kiocb(args->u.normal.via_iocb))
1631 /* the kernel does not support AIO on pipes, and parallel DIO
1632 * uses part of the AIO path, so we must not do parallel dio
1635 is_parallel_dio = !iov_iter_is_pipe(args->u.normal.via_iter) &&
1638 if (!ll_sbi_has_parallel_dio(sbi))
1639 is_parallel_dio = false;
1641 ci_aio = cl_aio_alloc(args->u.normal.via_iocb,
1642 ll_i2info(inode)->lli_clob);
1644 GOTO(out, rc = -ENOMEM);
1649 * IO block size need be aware of cached page limit, otherwise
1650 * if we have small max_cached_mb but large block IO issued, io
1651 * could not be finished and blocked whole client.
1653 if (file->f_flags & O_DIRECT)
1656 per_bytes = min(max_io_pages << PAGE_SHIFT, count);
1657 partial_io = per_bytes < count;
1658 io = vvp_env_thread_io(env);
1659 ll_io_init(io, file, iot, args);
1660 io->ci_aio = ci_aio;
1661 io->ci_dio_lock = dio_lock;
1662 io->ci_ndelay_tried = retried;
1663 io->ci_parallel_dio = is_parallel_dio;
1665 if (cl_io_rw_init(env, io, iot, *ppos, per_bytes) == 0) {
1666 if (file->f_flags & O_APPEND)
1667 range_lock_init(&range, 0, LUSTRE_EOF);
1669 range_lock_init(&range, *ppos, *ppos + per_bytes - 1);
1671 vio->vui_fd = file->private_data;
1672 vio->vui_iter = args->u.normal.via_iter;
1673 vio->vui_iocb = args->u.normal.via_iocb;
1674 /* Direct IO reads must also take range lock,
1675 * or multiple reads will try to work on the same pages
1676 * See LU-6227 for details.
1678 if (((iot == CIT_WRITE) ||
1679 (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1680 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1681 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1683 rc = range_lock(&lli->lli_write_tree, &range);
1687 range_locked = true;
1690 ll_cl_add(file, env, io, LCC_RW);
1691 rc = cl_io_loop(env, io);
1692 ll_cl_remove(file, env);
1694 if (range_locked && !is_parallel_dio) {
1695 CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1697 range_unlock(&lli->lli_write_tree, &range);
1698 range_locked = false;
1701 /* cl_io_rw_init() handled IO */
1705 /* N/B: parallel DIO may be disabled during i/o submission;
1706 * if that occurs, async RPCs are resolved before we get here, and this
1707 * wait call completes immediately.
1709 if (is_parallel_dio) {
1710 struct cl_sync_io *anchor = &io->ci_aio->cda_sync;
1712 /* for dio, EIOCBQUEUED is an implementation detail,
1713 * and we don't return it to userspace
1715 if (rc == -EIOCBQUEUED)
1718 rc2 = cl_sync_io_wait_recycle(env, anchor, 0, 0);
1723 range_unlock(&lli->lli_write_tree, &range);
1724 range_locked = false;
1729 * In order to move forward AIO, ci_nob was increased,
1730 * but that doesn't mean io have been finished, it just
1731 * means io have been submited, we will always return
1732 * EIOCBQUEUED to the caller, So we could only return
1733 * number of bytes in non-AIO case.
1735 if (io->ci_nob > 0) {
1738 result += io->ci_nob;
1739 *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
1744 count -= io->ci_nob;
1746 /* prepare IO restart */
1748 args->u.normal.via_iter = vio->vui_iter;
1752 * Reexpand iov count because it was zero
1755 iov_iter_reexpand(vio->vui_iter, count);
1756 if (per_bytes == io->ci_nob)
1757 io->ci_need_restart = 1;
1761 cl_io_fini(env, io);
1764 "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1765 file->f_path.dentry->d_name.name,
1766 iot, rc, result, io->ci_need_restart);
1768 if ((rc == 0 || rc == -ENODATA || rc == -ENOLCK) &&
1769 count > 0 && io->ci_need_restart) {
1771 "%s: restart %s from %lld, count: %zu, ret: %zd, rc: %d\n",
1772 file_dentry(file)->d_name.name,
1773 iot == CIT_READ ? "read" : "write",
1774 *ppos, count, result, rc);
1775 /* preserve the tried count for FLR */
1776 retried = io->ci_ndelay_tried;
1777 dio_lock = io->ci_dio_lock;
1783 * VFS will call aio_complete() if no -EIOCBQUEUED
1784 * is returned for AIO, so we can not call aio_complete()
1787 if (rc != -EIOCBQUEUED)
1788 io->ci_aio->cda_no_aio_complete = 1;
1790 * Drop one extra reference so that end_io() could be
1791 * called for this IO context, we could call it after
1792 * we make sure all AIO requests have been proceed.
1794 cl_sync_io_note(env, &io->ci_aio->cda_sync,
1795 rc == -EIOCBQUEUED ? 0 : rc);
1797 cl_aio_free(env, io->ci_aio);
1802 if (iot == CIT_READ) {
1804 ll_stats_ops_tally(ll_i2sbi(inode),
1805 LPROC_LL_READ_BYTES, result);
1806 } else if (iot == CIT_WRITE) {
1808 ll_stats_ops_tally(ll_i2sbi(inode),
1809 LPROC_LL_WRITE_BYTES, result);
1810 fd->fd_write_failed = false;
1811 } else if (result == 0 && rc == 0) {
1814 fd->fd_write_failed = true;
1816 fd->fd_write_failed = false;
1817 } else if (rc != -ERESTARTSYS) {
1818 fd->fd_write_failed = true;
1822 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1824 ll_heat_add(inode, iot, result);
1826 RETURN(result > 0 ? result : rc);
1830 * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1831 * especially for small I/O.
1833 * To serve a read request, CLIO has to create and initialize a cl_io and
1834 * then request DLM lock. This has turned out to have siginificant overhead
1835 * and affects the performance of small I/O dramatically.
1837 * It's not necessary to create a cl_io for each I/O. Under the help of read
1838 * ahead, most of the pages being read are already in memory cache and we can
1839 * read those pages directly because if the pages exist, the corresponding DLM
1840 * lock must exist so that page content must be valid.
1842 * In fast read implementation, the llite speculatively finds and reads pages
1843 * in memory cache. There are three scenarios for fast read:
1844 * - If the page exists and is uptodate, kernel VM will provide the data and
1845 * CLIO won't be intervened;
1846 * - If the page was brought into memory by read ahead, it will be exported
1847 * and read ahead parameters will be updated;
1848 * - Otherwise the page is not in memory, we can't do fast read. Therefore,
1849 * it will go back and invoke normal read, i.e., a cl_io will be created
1850 * and DLM lock will be requested.
1852 * POSIX compliance: posix standard states that read is intended to be atomic.
1853 * Lustre read implementation is in line with Linux kernel read implementation
1854 * and neither of them complies with POSIX standard in this matter. Fast read
1855 * doesn't make the situation worse on single node but it may interleave write
1856 * results from multiple nodes due to short read handling in ll_file_aio_read().
1858 * \param env - lu_env
1859 * \param iocb - kiocb from kernel
1860 * \param iter - user space buffers where the data will be copied
1862 * \retval - number of bytes have been read, or error code if error occurred.
1865 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1869 if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1872 /* NB: we can't do direct IO for fast read because it will need a lock
1873 * to make IO engine happy. */
1874 if (iocb->ki_filp->f_flags & O_DIRECT)
1877 result = generic_file_read_iter(iocb, iter);
1879 /* If the first page is not in cache, generic_file_aio_read() will be
1880 * returned with -ENODATA.
1881 * See corresponding code in ll_readpage(). */
1882 if (result == -ENODATA)
1886 ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
1887 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1888 LPROC_LL_READ_BYTES, result);
1895 * Read from a file (through the page cache).
1897 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1900 struct vvp_io_args *args;
1901 struct file *file = iocb->ki_filp;
1905 ktime_t kstart = ktime_get();
1908 if (!iov_iter_count(to))
1912 * Currently when PCC read failed, we do not fall back to the
1913 * normal read path, just return the error.
1914 * The resaon is that: for RW-PCC, the file data may be modified
1915 * in the PCC and inconsistent with the data on OSTs (or file
1916 * data has been removed from the Lustre file system), at this
1917 * time, fallback to the normal read path may read the wrong
1919 * TODO: for RO-PCC (readonly PCC), fall back to normal read
1920 * path: read data from data copy on OSTs.
1922 result = pcc_file_read_iter(iocb, to, &cached);
1926 ll_ras_enter(file, iocb->ki_pos, iov_iter_count(to));
1928 result = ll_do_fast_read(iocb, to);
1929 if (result < 0 || iov_iter_count(to) == 0)
1932 env = cl_env_get(&refcheck);
1934 return PTR_ERR(env);
1936 args = ll_env_args(env);
1937 args->u.normal.via_iter = to;
1938 args->u.normal.via_iocb = iocb;
1940 rc2 = ll_file_io_generic(env, args, file, CIT_READ,
1941 &iocb->ki_pos, iov_iter_count(to));
1944 else if (result == 0)
1947 cl_env_put(env, &refcheck);
1950 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
1951 file->private_data, iocb->ki_pos, result,
1953 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_READ,
1954 ktime_us_delta(ktime_get(), kstart));
1961 * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
1962 * If a page is already in the page cache and dirty (and some other things -
1963 * See ll_tiny_write_begin for the instantiation of these rules), then we can
1964 * write to it without doing a full I/O, because Lustre already knows about it
1965 * and will write it out. This saves a lot of processing time.
1967 * All writes here are within one page, so exclusion is handled by the page
1968 * lock on the vm page. We do not do tiny writes for writes which touch
1969 * multiple pages because it's very unlikely multiple sequential pages are
1970 * are already dirty.
1972 * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
1973 * and are unlikely to be to already dirty pages.
1975 * Attribute updates are important here, we do them in ll_tiny_write_end.
1977 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
1979 ssize_t count = iov_iter_count(iter);
1980 struct file *file = iocb->ki_filp;
1981 struct inode *inode = file_inode(file);
1982 bool lock_inode = !IS_NOSEC(inode);
1987 /* Restrict writes to single page and < PAGE_SIZE. See comment at top
1988 * of function for why.
1990 if (count >= PAGE_SIZE ||
1991 (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
1994 if (unlikely(lock_inode))
1996 result = __generic_file_write_iter(iocb, iter);
1998 if (unlikely(lock_inode))
1999 inode_unlock(inode);
2001 /* If the page is not already dirty, ll_tiny_write_begin returns
2002 * -ENODATA. We continue on to normal write.
2004 if (result == -ENODATA)
2008 ll_heat_add(inode, CIT_WRITE, result);
2009 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_WRITE_BYTES,
2011 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
2014 CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
2020 * Write to a file (through the page cache).
2022 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2024 struct vvp_io_args *args;
2026 ssize_t rc_tiny = 0, rc_normal;
2027 struct file *file = iocb->ki_filp;
2030 ktime_t kstart = ktime_get();
2035 if (!iov_iter_count(from))
2036 GOTO(out, rc_normal = 0);
2039 * When PCC write failed, we usually do not fall back to the normal
2040 * write path, just return the error. But there is a special case when
2041 * returned error code is -ENOSPC due to running out of space on PCC HSM
2042 * bakcend. At this time, it will fall back to normal I/O path and
2043 * retry the I/O. As the file is in HSM released state, it will restore
2044 * the file data to OSTs first and redo the write again. And the
2045 * restore process will revoke the layout lock and detach the file
2046 * from PCC cache automatically.
2048 result = pcc_file_write_iter(iocb, from, &cached);
2049 if (cached && result != -ENOSPC && result != -EDQUOT)
2050 GOTO(out, rc_normal = result);
2052 /* NB: we can't do direct IO for tiny writes because they use the page
2053 * cache, we can't do sync writes because tiny writes can't flush
2054 * pages, and we can't do append writes because we can't guarantee the
2055 * required DLM locks are held to protect file size.
2057 if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(file))) &&
2058 !(file->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
2059 rc_tiny = ll_do_tiny_write(iocb, from);
2061 /* In case of error, go on and try normal write - Only stop if tiny
2062 * write completed I/O.
2064 if (iov_iter_count(from) == 0)
2065 GOTO(out, rc_normal = rc_tiny);
2067 env = cl_env_get(&refcheck);
2069 return PTR_ERR(env);
2071 args = ll_env_args(env);
2072 args->u.normal.via_iter = from;
2073 args->u.normal.via_iocb = iocb;
2075 rc_normal = ll_file_io_generic(env, args, file, CIT_WRITE,
2076 &iocb->ki_pos, iov_iter_count(from));
2078 /* On success, combine bytes written. */
2079 if (rc_tiny >= 0 && rc_normal > 0)
2080 rc_normal += rc_tiny;
2081 /* On error, only return error from normal write if tiny write did not
2082 * write any bytes. Otherwise return bytes written by tiny write.
2084 else if (rc_tiny > 0)
2085 rc_normal = rc_tiny;
2087 cl_env_put(env, &refcheck);
2089 if (rc_normal > 0) {
2090 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
2091 file->private_data, iocb->ki_pos,
2093 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_WRITE,
2094 ktime_us_delta(ktime_get(), kstart));
2100 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
2102 * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
2104 static int ll_file_get_iov_count(const struct iovec *iov,
2105 unsigned long *nr_segs, size_t *count,
2111 for (seg = 0; seg < *nr_segs; seg++) {
2112 const struct iovec *iv = &iov[seg];
2115 * If any segment has a negative length, or the cumulative
2116 * length ever wraps negative then return -EINVAL.
2119 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
2121 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
2126 cnt -= iv->iov_len; /* This segment is no good */
2133 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2134 unsigned long nr_segs, loff_t pos)
2141 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_READ);
2148 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2149 iov_iter_init(&to, READ, iov, nr_segs, iov_count);
2150 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2151 iov_iter_init(&to, iov, nr_segs, iov_count, 0);
2152 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2154 result = ll_file_read_iter(iocb, &to);
2159 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
2162 struct iovec iov = { .iov_base = buf, .iov_len = count };
2171 init_sync_kiocb(&kiocb, file);
2172 kiocb.ki_pos = *ppos;
2173 #ifdef HAVE_KIOCB_KI_LEFT
2174 kiocb.ki_left = count;
2175 #elif defined(HAVE_KI_NBYTES)
2176 kiocb.i_nbytes = count;
2179 result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
2180 *ppos = kiocb.ki_pos;
2186 * Write to a file (through the page cache).
2189 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2190 unsigned long nr_segs, loff_t pos)
2192 struct iov_iter from;
2197 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_WRITE);
2204 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2205 iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
2206 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2207 iov_iter_init(&from, iov, nr_segs, iov_count, 0);
2208 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2210 result = ll_file_write_iter(iocb, &from);
2215 static ssize_t ll_file_write(struct file *file, const char __user *buf,
2216 size_t count, loff_t *ppos)
2218 struct iovec iov = { .iov_base = (void __user *)buf,
2228 init_sync_kiocb(&kiocb, file);
2229 kiocb.ki_pos = *ppos;
2230 #ifdef HAVE_KIOCB_KI_LEFT
2231 kiocb.ki_left = count;
2232 #elif defined(HAVE_KI_NBYTES)
2233 kiocb.ki_nbytes = count;
2236 result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
2237 *ppos = kiocb.ki_pos;
2241 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
2243 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
2244 __u64 flags, struct lov_user_md *lum, int lum_size)
2246 struct lookup_intent oit = {
2248 .it_flags = flags | MDS_OPEN_BY_FID,
2253 if ((__swab32(lum->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
2254 le32_to_cpu(LOV_MAGIC_MAGIC)) {
2255 /* this code will only exist for big-endian systems */
2256 lustre_swab_lov_user_md(lum, 0);
2259 ll_inode_size_lock(inode);
2260 rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
2262 GOTO(out_unlock, rc);
2264 ll_release_openhandle(dentry, &oit);
2267 ll_inode_size_unlock(inode);
2268 ll_intent_release(&oit);
2273 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
2274 struct lov_mds_md **lmmp, int *lmm_size,
2275 struct ptlrpc_request **request)
2277 struct ll_sb_info *sbi = ll_i2sbi(inode);
2278 struct mdt_body *body;
2279 struct lov_mds_md *lmm = NULL;
2280 struct ptlrpc_request *req = NULL;
2281 struct md_op_data *op_data;
2286 rc = ll_get_default_mdsize(sbi, &lmmsize);
2290 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
2291 strlen(filename), lmmsize,
2292 LUSTRE_OPC_ANY, NULL);
2293 if (IS_ERR(op_data))
2294 RETURN(PTR_ERR(op_data));
2296 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
2297 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
2298 ll_finish_md_op_data(op_data);
2300 CDEBUG(D_INFO, "md_getattr_name failed "
2301 "on %s: rc %d\n", filename, rc);
2305 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2306 LASSERT(body != NULL); /* checked by mdc_getattr_name */
2308 lmmsize = body->mbo_eadatasize;
2310 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
2312 GOTO(out, rc = -ENODATA);
2314 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
2315 LASSERT(lmm != NULL);
2317 if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
2318 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
2319 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1) &&
2320 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_FOREIGN))
2321 GOTO(out, rc = -EPROTO);
2324 * This is coming from the MDS, so is probably in
2325 * little endian. We convert it to host endian before
2326 * passing it to userspace.
2328 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) {
2329 int stripe_count = 0;
2331 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
2332 lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2333 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
2334 if (le32_to_cpu(lmm->lmm_pattern) &
2335 LOV_PATTERN_F_RELEASED)
2337 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
2339 /* if function called for directory - we should
2340 * avoid swab not existent lsm objects
2342 if (lmm->lmm_magic == LOV_MAGIC_V1 &&
2343 S_ISREG(body->mbo_mode))
2344 lustre_swab_lov_user_md_objects(
2345 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
2347 else if (lmm->lmm_magic == LOV_MAGIC_V3 &&
2348 S_ISREG(body->mbo_mode))
2349 lustre_swab_lov_user_md_objects(
2350 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
2352 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1)) {
2353 lustre_swab_lov_comp_md_v1(
2354 (struct lov_comp_md_v1 *)lmm);
2358 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
2359 struct lov_comp_md_v1 *comp_v1 = NULL;
2360 struct lov_comp_md_entry_v1 *ent;
2361 struct lov_user_md_v1 *v1;
2365 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2366 /* Dump the striping information */
2367 for (; i < comp_v1->lcm_entry_count; i++) {
2368 ent = &comp_v1->lcm_entries[i];
2369 off = ent->lcme_offset;
2370 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2372 "comp[%d]: stripe_count=%u, stripe_size=%u\n",
2373 i, v1->lmm_stripe_count, v1->lmm_stripe_size);
2377 * Return valid stripe_count and stripe_size instead of 0 for
2378 * DoM files to avoid divide-by-zero for older userspace that
2379 * calls this ioctl, e.g. lustre ADIO driver.
2381 if (lmm->lmm_stripe_count == 0)
2382 lmm->lmm_stripe_count = 1;
2383 if (lmm->lmm_stripe_size == 0) {
2384 /* Since the first component of the file data is placed
2385 * on the MDT for faster access, the stripe_size of the
2386 * second one is always that applications which are
2389 if (lmm->lmm_pattern == LOV_PATTERN_MDT)
2390 i = comp_v1->lcm_entry_count > 1 ? 1 : 0;
2392 i = comp_v1->lcm_entry_count > 1 ?
2393 comp_v1->lcm_entry_count - 1 : 0;
2394 ent = &comp_v1->lcm_entries[i];
2395 off = ent->lcme_offset;
2396 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2397 lmm->lmm_stripe_size = v1->lmm_stripe_size;
2402 *lmm_size = lmmsize;
2407 static int ll_lov_setea(struct inode *inode, struct file *file,
2410 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
2411 struct lov_user_md *lump;
2412 int lum_size = sizeof(struct lov_user_md) +
2413 sizeof(struct lov_user_ost_data);
2417 if (!capable(CAP_SYS_ADMIN))
2420 OBD_ALLOC_LARGE(lump, lum_size);
2424 if (copy_from_user(lump, arg, lum_size))
2425 GOTO(out_lump, rc = -EFAULT);
2427 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
2429 cl_lov_delay_create_clear(&file->f_flags);
2432 OBD_FREE_LARGE(lump, lum_size);
2436 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
2443 env = cl_env_get(&refcheck);
2445 RETURN(PTR_ERR(env));
2447 rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
2448 cl_env_put(env, &refcheck);
2452 static int ll_lov_setstripe(struct inode *inode, struct file *file,
2455 struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
2456 struct lov_user_md *klum;
2458 __u64 flags = FMODE_WRITE;
2461 rc = ll_copy_user_md(lum, &klum);
2466 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2471 rc = put_user(0, &lum->lmm_stripe_count);
2475 rc = ll_layout_refresh(inode, &gen);
2479 rc = ll_file_getstripe(inode, arg, lum_size);
2480 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode) &&
2481 ll_i2info(inode)->lli_clob) {
2482 struct iattr attr = { 0 };
2484 rc = cl_setattr_ost(ll_i2info(inode)->lli_clob, &attr,
2485 OP_XVALID_FLAGS, LUSTRE_ENCRYPT_FL);
2488 cl_lov_delay_create_clear(&file->f_flags);
2491 OBD_FREE_LARGE(klum, lum_size);
2497 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2499 struct ll_inode_info *lli = ll_i2info(inode);
2500 struct cl_object *obj = lli->lli_clob;
2501 struct ll_file_data *fd = file->private_data;
2502 struct ll_grouplock grouplock;
2507 CWARN("group id for group lock must not be 0\n");
2511 if (ll_file_nolock(file))
2512 RETURN(-EOPNOTSUPP);
2514 if (file->f_flags & O_NONBLOCK) {
2515 if (!mutex_trylock(&lli->lli_group_mutex))
2518 mutex_lock(&lli->lli_group_mutex);
2520 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2521 CWARN("group lock already existed with gid %lu\n",
2522 fd->fd_grouplock.lg_gid);
2523 GOTO(out, rc = -EINVAL);
2525 if (arg != lli->lli_group_gid && lli->lli_group_users != 0) {
2526 if (file->f_flags & O_NONBLOCK)
2527 GOTO(out, rc = -EAGAIN);
2528 mutex_unlock(&lli->lli_group_mutex);
2529 wait_var_event(&lli->lli_group_users, !lli->lli_group_users);
2530 GOTO(retry, rc = 0);
2532 LASSERT(fd->fd_grouplock.lg_lock == NULL);
2535 * XXX: group lock needs to protect all OST objects while PFL
2536 * can add new OST objects during the IO, so we'd instantiate
2537 * all OST objects before getting its group lock.
2542 struct cl_layout cl = {
2543 .cl_is_composite = false,
2545 struct lu_extent ext = {
2547 .e_end = OBD_OBJECT_EOF,
2550 env = cl_env_get(&refcheck);
2552 GOTO(out, rc = PTR_ERR(env));
2554 rc = cl_object_layout_get(env, obj, &cl);
2555 if (rc >= 0 && cl.cl_is_composite)
2556 rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2559 cl_env_put(env, &refcheck);
2564 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2565 arg, (file->f_flags & O_NONBLOCK), &grouplock);
2570 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2571 fd->fd_grouplock = grouplock;
2572 if (lli->lli_group_users == 0)
2573 lli->lli_group_gid = grouplock.lg_gid;
2574 lli->lli_group_users++;
2576 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2578 mutex_unlock(&lli->lli_group_mutex);
2583 static int ll_put_grouplock(struct inode *inode, struct file *file,
2586 struct ll_inode_info *lli = ll_i2info(inode);
2587 struct ll_file_data *fd = file->private_data;
2588 struct ll_grouplock grouplock;
2592 mutex_lock(&lli->lli_group_mutex);
2593 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2594 CWARN("no group lock held\n");
2595 GOTO(out, rc = -EINVAL);
2598 LASSERT(fd->fd_grouplock.lg_lock != NULL);
2600 if (fd->fd_grouplock.lg_gid != arg) {
2601 CWARN("group lock %lu doesn't match current id %lu\n",
2602 arg, fd->fd_grouplock.lg_gid);
2603 GOTO(out, rc = -EINVAL);
2606 grouplock = fd->fd_grouplock;
2607 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2608 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2610 cl_put_grouplock(&grouplock);
2612 lli->lli_group_users--;
2613 if (lli->lli_group_users == 0) {
2614 lli->lli_group_gid = 0;
2615 wake_up_var(&lli->lli_group_users);
2617 CDEBUG(D_INFO, "group lock %lu released\n", arg);
2620 mutex_unlock(&lli->lli_group_mutex);
2626 * Close inode open handle
2628 * \param dentry [in] dentry which contains the inode
2629 * \param it [in,out] intent which contains open info and result
2632 * \retval <0 failure
2634 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2636 struct inode *inode = dentry->d_inode;
2637 struct obd_client_handle *och;
2643 /* Root ? Do nothing. */
2644 if (is_root_inode(inode))
2647 /* No open handle to close? Move away */
2648 if (!it_disposition(it, DISP_OPEN_OPEN))
2651 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2653 OBD_ALLOC(och, sizeof(*och));
2655 GOTO(out, rc = -ENOMEM);
2657 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2661 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2663 /* this one is in place of ll_file_open */
2664 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2665 ptlrpc_req_finished(it->it_request);
2666 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2672 * Get size for inode for which FIEMAP mapping is requested.
2673 * Make the FIEMAP get_info call and returns the result.
2674 * \param fiemap kernel buffer to hold extens
2675 * \param num_bytes kernel buffer size
2677 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2683 struct ll_fiemap_info_key fmkey = { .lfik_name = KEY_FIEMAP, };
2686 /* Checks for fiemap flags */
2687 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2688 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2692 /* Check for FIEMAP_FLAG_SYNC */
2693 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2694 rc = filemap_fdatawrite(inode->i_mapping);
2699 env = cl_env_get(&refcheck);
2701 RETURN(PTR_ERR(env));
2703 if (i_size_read(inode) == 0) {
2704 rc = ll_glimpse_size(inode);
2709 fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2710 obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2711 obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2713 /* If filesize is 0, then there would be no objects for mapping */
2714 if (fmkey.lfik_oa.o_size == 0) {
2715 fiemap->fm_mapped_extents = 0;
2719 fmkey.lfik_fiemap = *fiemap;
2721 rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2722 &fmkey, fiemap, &num_bytes);
2724 cl_env_put(env, &refcheck);
2728 int ll_fid2path(struct inode *inode, void __user *arg)
2730 struct obd_export *exp = ll_i2mdexp(inode);
2731 const struct getinfo_fid2path __user *gfin = arg;
2733 struct getinfo_fid2path *gfout;
2739 if (!capable(CAP_DAC_READ_SEARCH) &&
2740 !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2743 /* Only need to get the buflen */
2744 if (get_user(pathlen, &gfin->gf_pathlen))
2747 if (pathlen > PATH_MAX)
2750 outsize = sizeof(*gfout) + pathlen;
2751 OBD_ALLOC(gfout, outsize);
2755 if (copy_from_user(gfout, arg, sizeof(*gfout)))
2756 GOTO(gf_free, rc = -EFAULT);
2757 /* append root FID after gfout to let MDT know the root FID so that it
2758 * can lookup the correct path, this is mainly for fileset.
2759 * old server without fileset mount support will ignore this. */
2760 *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
2762 /* Call mdc_iocontrol */
2763 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
2767 if (copy_to_user(arg, gfout, outsize))
2771 OBD_FREE(gfout, outsize);
2776 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
2778 struct cl_object *obj = ll_i2info(inode)->lli_clob;
2786 ioc->idv_version = 0;
2787 ioc->idv_layout_version = UINT_MAX;
2789 /* If no file object initialized, we consider its version is 0. */
2793 env = cl_env_get(&refcheck);
2795 RETURN(PTR_ERR(env));
2797 io = vvp_env_thread_io(env);
2799 io->u.ci_data_version.dv_data_version = 0;
2800 io->u.ci_data_version.dv_layout_version = UINT_MAX;
2801 io->u.ci_data_version.dv_flags = ioc->idv_flags;
2804 if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
2805 result = cl_io_loop(env, io);
2807 result = io->ci_result;
2809 ioc->idv_version = io->u.ci_data_version.dv_data_version;
2810 ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
2812 cl_io_fini(env, io);
2814 if (unlikely(io->ci_need_restart))
2817 cl_env_put(env, &refcheck);
2823 * Read the data_version for inode.
2825 * This value is computed using stripe object version on OST.
2826 * Version is computed using server side locking.
2828 * @param flags if do sync on the OST side;
2830 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
2831 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
2833 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
2835 struct ioc_data_version ioc = { .idv_flags = flags };
2838 rc = ll_ioc_data_version(inode, &ioc);
2840 *data_version = ioc.idv_version;
2846 * Trigger a HSM release request for the provided inode.
2848 int ll_hsm_release(struct inode *inode)
2851 struct obd_client_handle *och = NULL;
2852 __u64 data_version = 0;
2857 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2858 ll_i2sbi(inode)->ll_fsname,
2859 PFID(&ll_i2info(inode)->lli_fid));
2861 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2863 GOTO(out, rc = PTR_ERR(och));
2865 /* Grab latest data_version and [am]time values */
2866 rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
2870 env = cl_env_get(&refcheck);
2872 GOTO(out, rc = PTR_ERR(env));
2874 rc = ll_merge_attr(env, inode);
2875 cl_env_put(env, &refcheck);
2877 /* If error happen, we have the wrong size for a file.
2883 /* Release the file.
2884 * NB: lease lock handle is released in mdc_hsm_release_pack() because
2885 * we still need it to pack l_remote_handle to MDT. */
2886 rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
2892 if (och != NULL && !IS_ERR(och)) /* close the file */
2893 ll_lease_close(och, inode, NULL);
2898 struct ll_swap_stack {
2901 struct inode *inode1;
2902 struct inode *inode2;
2907 static int ll_swap_layouts(struct file *file1, struct file *file2,
2908 struct lustre_swap_layouts *lsl)
2910 struct mdc_swap_layouts msl;
2911 struct md_op_data *op_data;
2914 struct ll_swap_stack *llss = NULL;
2917 OBD_ALLOC_PTR(llss);
2921 llss->inode1 = file_inode(file1);
2922 llss->inode2 = file_inode(file2);
2924 rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
2928 /* we use 2 bool because it is easier to swap than 2 bits */
2929 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2930 llss->check_dv1 = true;
2932 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2933 llss->check_dv2 = true;
2935 /* we cannot use lsl->sl_dvX directly because we may swap them */
2936 llss->dv1 = lsl->sl_dv1;
2937 llss->dv2 = lsl->sl_dv2;
2939 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2940 if (rc == 0) /* same file, done! */
2943 if (rc < 0) { /* sequentialize it */
2944 swap(llss->inode1, llss->inode2);
2946 swap(llss->dv1, llss->dv2);
2947 swap(llss->check_dv1, llss->check_dv2);
2951 if (gid != 0) { /* application asks to flush dirty cache */
2952 rc = ll_get_grouplock(llss->inode1, file1, gid);
2956 rc = ll_get_grouplock(llss->inode2, file2, gid);
2958 ll_put_grouplock(llss->inode1, file1, gid);
2963 /* ultimate check, before swaping the layouts we check if
2964 * dataversion has changed (if requested) */
2965 if (llss->check_dv1) {
2966 rc = ll_data_version(llss->inode1, &dv, 0);
2969 if (dv != llss->dv1)
2970 GOTO(putgl, rc = -EAGAIN);
2973 if (llss->check_dv2) {
2974 rc = ll_data_version(llss->inode2, &dv, 0);
2977 if (dv != llss->dv2)
2978 GOTO(putgl, rc = -EAGAIN);
2981 /* struct md_op_data is used to send the swap args to the mdt
2982 * only flags is missing, so we use struct mdc_swap_layouts
2983 * through the md_op_data->op_data */
2984 /* flags from user space have to be converted before they are send to
2985 * server, no flag is sent today, they are only used on the client */
2988 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
2989 0, LUSTRE_OPC_ANY, &msl);
2990 if (IS_ERR(op_data))
2991 GOTO(free, rc = PTR_ERR(op_data));
2993 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
2994 sizeof(*op_data), op_data, NULL);
2995 ll_finish_md_op_data(op_data);
3002 ll_put_grouplock(llss->inode2, file2, gid);
3003 ll_put_grouplock(llss->inode1, file1, gid);
3013 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
3015 struct obd_export *exp = ll_i2mdexp(inode);
3016 struct md_op_data *op_data;
3020 /* Detect out-of range masks */
3021 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
3024 /* Non-root users are forbidden to set or clear flags which are
3025 * NOT defined in HSM_USER_MASK. */
3026 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
3027 !capable(CAP_SYS_ADMIN))
3030 if (!exp_connect_archive_id_array(exp)) {
3031 /* Detect out-of range archive id */
3032 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
3033 (hss->hss_archive_id > LL_HSM_ORIGIN_MAX_ARCHIVE))
3037 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3038 LUSTRE_OPC_ANY, hss);
3039 if (IS_ERR(op_data))
3040 RETURN(PTR_ERR(op_data));
3042 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, exp, sizeof(*op_data),
3045 ll_finish_md_op_data(op_data);
3050 static int ll_hsm_import(struct inode *inode, struct file *file,
3051 struct hsm_user_import *hui)
3053 struct hsm_state_set *hss = NULL;
3054 struct iattr *attr = NULL;
3058 if (!S_ISREG(inode->i_mode))
3064 GOTO(out, rc = -ENOMEM);
3066 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
3067 hss->hss_archive_id = hui->hui_archive_id;
3068 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
3069 rc = ll_hsm_state_set(inode, hss);
3073 OBD_ALLOC_PTR(attr);
3075 GOTO(out, rc = -ENOMEM);
3077 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
3078 attr->ia_mode |= S_IFREG;
3079 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
3080 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
3081 attr->ia_size = hui->hui_size;
3082 attr->ia_mtime.tv_sec = hui->hui_mtime;
3083 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
3084 attr->ia_atime.tv_sec = hui->hui_atime;
3085 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
3087 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
3088 ATTR_UID | ATTR_GID |
3089 ATTR_MTIME | ATTR_MTIME_SET |
3090 ATTR_ATIME | ATTR_ATIME_SET;
3094 rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
3098 inode_unlock(inode);
3110 static inline long ll_lease_type_from_fmode(fmode_t fmode)
3112 return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
3113 ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
3116 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
3118 struct inode *inode = file_inode(file);
3120 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
3121 ATTR_MTIME | ATTR_MTIME_SET |
3124 .tv_sec = lfu->lfu_atime_sec,
3125 .tv_nsec = lfu->lfu_atime_nsec,
3128 .tv_sec = lfu->lfu_mtime_sec,
3129 .tv_nsec = lfu->lfu_mtime_nsec,
3132 .tv_sec = lfu->lfu_ctime_sec,
3133 .tv_nsec = lfu->lfu_ctime_nsec,
3139 if (!capable(CAP_SYS_ADMIN))
3142 if (!S_ISREG(inode->i_mode))
3146 rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
3148 inode_unlock(inode);
3153 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
3156 case MODE_READ_USER:
3158 case MODE_WRITE_USER:
3165 static const char *const user_lockname[] = LOCK_MODE_NAMES;
3167 /* Used to allow the upper layers of the client to request an LDLM lock
3168 * without doing an actual read or write.
3170 * Used for ladvise lockahead to manually request specific locks.
3172 * \param[in] file file this ladvise lock request is on
3173 * \param[in] ladvise ladvise struct describing this lock request
3175 * \retval 0 success, no detailed result available (sync requests
3176 * and requests sent to the server [not handled locally]
3177 * cannot return detailed results)
3178 * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
3179 * see definitions for details.
3180 * \retval negative negative errno on error
3182 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
3184 struct lu_env *env = NULL;
3185 struct cl_io *io = NULL;
3186 struct cl_lock *lock = NULL;
3187 struct cl_lock_descr *descr = NULL;
3188 struct dentry *dentry = file->f_path.dentry;
3189 struct inode *inode = dentry->d_inode;
3190 enum cl_lock_mode cl_mode;
3191 off_t start = ladvise->lla_start;
3192 off_t end = ladvise->lla_end;
3199 "Lock request: file=%pd, inode=%p, mode=%s start=%llu, end=%llu\n",
3200 dentry, dentry->d_inode,
3201 user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
3204 cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
3206 GOTO(out, result = cl_mode);
3208 /* Get IO environment */
3209 result = cl_io_get(inode, &env, &io, &refcheck);
3213 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
3216 * nothing to do for this io. This currently happens when
3217 * stripe sub-object's are not yet created.
3219 result = io->ci_result;
3220 } else if (result == 0) {
3221 lock = vvp_env_lock(env);
3222 descr = &lock->cll_descr;
3224 descr->cld_obj = io->ci_obj;
3225 /* Convert byte offsets to pages */
3226 descr->cld_start = cl_index(io->ci_obj, start);
3227 descr->cld_end = cl_index(io->ci_obj, end);
3228 descr->cld_mode = cl_mode;
3229 /* CEF_MUST is used because we do not want to convert a
3230 * lockahead request to a lockless lock */
3231 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND;
3233 if (ladvise->lla_peradvice_flags & LF_ASYNC)
3234 descr->cld_enq_flags |= CEF_SPECULATIVE;
3236 result = cl_lock_request(env, io, lock);
3238 /* On success, we need to release the lock */
3240 cl_lock_release(env, lock);
3242 cl_io_fini(env, io);
3243 cl_env_put(env, &refcheck);
3245 /* -ECANCELED indicates a matching lock with a different extent
3246 * was already present, and -EEXIST indicates a matching lock
3247 * on exactly the same extent was already present.
3248 * We convert them to positive values for userspace to make
3249 * recognizing true errors easier.
3250 * Note we can only return these detailed results on async requests,
3251 * as sync requests look the same as i/o requests for locking. */
3252 if (result == -ECANCELED)
3253 result = LLA_RESULT_DIFFERENT;
3254 else if (result == -EEXIST)
3255 result = LLA_RESULT_SAME;
3260 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
3262 static int ll_ladvise_sanity(struct inode *inode,
3263 struct llapi_lu_ladvise *ladvise)
3265 struct ll_sb_info *sbi = ll_i2sbi(inode);
3266 enum lu_ladvise_type advice = ladvise->lla_advice;
3267 /* Note the peradvice flags is a 32 bit field, so per advice flags must
3268 * be in the first 32 bits of enum ladvise_flags */
3269 __u32 flags = ladvise->lla_peradvice_flags;
3270 /* 3 lines at 80 characters per line, should be plenty */
3273 if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
3276 "%s: advice with value '%d' not recognized, last supported advice is %s (value '%d'): rc = %d\n",
3277 sbi->ll_fsname, advice,
3278 ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
3282 /* Per-advice checks */
3284 case LU_LADVISE_LOCKNOEXPAND:
3285 if (flags & ~LF_LOCKNOEXPAND_MASK) {
3287 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3288 "rc = %d\n", sbi->ll_fsname, flags,
3289 ladvise_names[advice], rc);
3293 case LU_LADVISE_LOCKAHEAD:
3294 /* Currently only READ and WRITE modes can be requested */
3295 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
3296 ladvise->lla_lockahead_mode == 0) {
3298 CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
3299 "rc = %d\n", sbi->ll_fsname,
3300 ladvise->lla_lockahead_mode,
3301 ladvise_names[advice], rc);
3305 case LU_LADVISE_WILLREAD:
3306 case LU_LADVISE_DONTNEED:
3308 /* Note fall through above - These checks apply to all advices
3309 * except LOCKNOEXPAND */
3310 if (flags & ~LF_DEFAULT_MASK) {
3312 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3313 "rc = %d\n", sbi->ll_fsname, flags,
3314 ladvise_names[advice], rc);
3317 if (ladvise->lla_start >= ladvise->lla_end) {
3319 CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
3320 "for %s: rc = %d\n", sbi->ll_fsname,
3321 ladvise->lla_start, ladvise->lla_end,
3322 ladvise_names[advice], rc);
3334 * Give file access advices
3336 * The ladvise interface is similar to Linux fadvise() system call, except it
3337 * forwards the advices directly from Lustre client to server. The server side
3338 * codes will apply appropriate read-ahead and caching techniques for the
3339 * corresponding files.
3341 * A typical workload for ladvise is e.g. a bunch of different clients are
3342 * doing small random reads of a file, so prefetching pages into OSS cache
3343 * with big linear reads before the random IO is a net benefit. Fetching
3344 * all that data into each client cache with fadvise() may not be, due to
3345 * much more data being sent to the client.
3347 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
3348 struct llapi_lu_ladvise *ladvise)
3352 struct cl_ladvise_io *lio;
3357 env = cl_env_get(&refcheck);
3359 RETURN(PTR_ERR(env));
3361 io = vvp_env_thread_io(env);
3362 io->ci_obj = ll_i2info(inode)->lli_clob;
3364 /* initialize parameters for ladvise */
3365 lio = &io->u.ci_ladvise;
3366 lio->li_start = ladvise->lla_start;
3367 lio->li_end = ladvise->lla_end;
3368 lio->li_fid = ll_inode2fid(inode);
3369 lio->li_advice = ladvise->lla_advice;
3370 lio->li_flags = flags;
3372 if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
3373 rc = cl_io_loop(env, io);
3377 cl_io_fini(env, io);
3378 cl_env_put(env, &refcheck);
3382 static int ll_lock_noexpand(struct file *file, int flags)
3384 struct ll_file_data *fd = file->private_data;
3386 fd->ll_lock_no_expand = !(flags & LF_UNSET);
3391 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
3394 struct fsxattr fsxattr;
3396 if (copy_from_user(&fsxattr,
3397 (const struct fsxattr __user *)arg,
3401 fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
3402 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
3403 fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
3404 fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
3405 if (copy_to_user((struct fsxattr __user *)arg,
3406 &fsxattr, sizeof(fsxattr)))
3412 int ll_ioctl_check_project(struct inode *inode, __u32 xflags,
3416 * Project Quota ID state is only allowed to change from within the init
3417 * namespace. Enforce that restriction only if we are trying to change
3418 * the quota ID state. Everything else is allowed in user namespaces.
3420 if (current_user_ns() == &init_user_ns) {
3422 * Caller is allowed to change the project ID. if it is being
3423 * changed, make sure that the new value is valid.
3425 if (ll_i2info(inode)->lli_projid != projid &&
3426 !projid_valid(make_kprojid(&init_user_ns, projid)))
3432 if (ll_i2info(inode)->lli_projid != projid)
3435 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags)) {
3436 if (!(xflags & FS_XFLAG_PROJINHERIT))
3439 if (xflags & FS_XFLAG_PROJINHERIT)
3446 static int ll_set_project(struct inode *inode, __u32 xflags, __u32 projid)
3448 struct md_op_data *op_data;
3449 struct ptlrpc_request *req = NULL;
3450 struct cl_object *obj;
3451 unsigned int inode_flags;
3454 rc = ll_ioctl_check_project(inode, xflags, projid);
3458 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3459 LUSTRE_OPC_ANY, NULL);
3460 if (IS_ERR(op_data))
3461 RETURN(PTR_ERR(op_data));
3463 inode_flags = ll_xflags_to_inode_flags(xflags);
3464 op_data->op_attr_flags = ll_inode_to_ext_flags(inode_flags);
3465 if (xflags & FS_XFLAG_PROJINHERIT)
3466 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
3467 op_data->op_projid = projid;
3468 op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
3469 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL, 0, &req);
3470 ptlrpc_req_finished(req);
3472 GOTO(out_fsxattr, rc);
3473 ll_update_inode_flags(inode, op_data->op_attr_flags);
3475 /* Avoid OST RPC if this is only ioctl setting project inherit flag */
3476 if (xflags == 0 || xflags == FS_XFLAG_PROJINHERIT)
3477 GOTO(out_fsxattr, rc);
3479 obj = ll_i2info(inode)->lli_clob;
3481 struct iattr attr = { 0 };
3483 rc = cl_setattr_ost(obj, &attr, OP_XVALID_FLAGS, xflags);
3487 ll_finish_md_op_data(op_data);
3491 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
3494 struct fsxattr fsxattr;
3498 if (copy_from_user(&fsxattr,
3499 (const struct fsxattr __user *)arg,
3503 RETURN(ll_set_project(inode, fsxattr.fsx_xflags,
3504 fsxattr.fsx_projid));
3507 int ll_ioctl_project(struct file *file, unsigned int cmd,
3510 struct lu_project lu_project;
3511 struct dentry *dentry = file_dentry(file);
3512 struct inode *inode = file_inode(file);
3513 struct dentry *child_dentry = NULL;
3514 int rc = 0, name_len;
3516 if (copy_from_user(&lu_project,
3517 (const struct lu_project __user *)arg,
3518 sizeof(lu_project)))
3521 /* apply child dentry if name is valid */
3522 name_len = strnlen(lu_project.project_name, NAME_MAX);
3523 if (name_len > 0 && name_len <= NAME_MAX) {
3525 child_dentry = lookup_one_len(lu_project.project_name,
3527 inode_unlock(inode);
3528 if (IS_ERR(child_dentry)) {
3529 rc = PTR_ERR(child_dentry);
3532 inode = child_dentry->d_inode;
3537 } else if (name_len > NAME_MAX) {
3542 switch (lu_project.project_type) {
3543 case LU_PROJECT_SET:
3544 rc = ll_set_project(inode, lu_project.project_xflags,
3545 lu_project.project_id);
3547 case LU_PROJECT_GET:
3548 lu_project.project_xflags =
3549 ll_inode_flags_to_xflags(inode->i_flags);
3550 if (test_bit(LLIF_PROJECT_INHERIT,
3551 &ll_i2info(inode)->lli_flags))
3552 lu_project.project_xflags |= FS_XFLAG_PROJINHERIT;
3553 lu_project.project_id = ll_i2info(inode)->lli_projid;
3554 if (copy_to_user((struct lu_project __user *)arg,
3555 &lu_project, sizeof(lu_project))) {
3565 if (!IS_ERR_OR_NULL(child_dentry))
3570 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
3573 struct inode *inode = file_inode(file);
3574 struct ll_file_data *fd = file->private_data;
3575 struct ll_inode_info *lli = ll_i2info(inode);
3576 struct obd_client_handle *och = NULL;
3577 struct split_param sp;
3578 struct pcc_param param;
3579 bool lease_broken = false;
3581 enum mds_op_bias bias = 0;
3582 struct file *layout_file = NULL;
3584 size_t data_size = 0;
3585 bool attached = false;
3590 mutex_lock(&lli->lli_och_mutex);
3591 if (fd->fd_lease_och != NULL) {
3592 och = fd->fd_lease_och;
3593 fd->fd_lease_och = NULL;
3595 mutex_unlock(&lli->lli_och_mutex);
3600 fmode = och->och_flags;
3602 switch (ioc->lil_flags) {
3603 case LL_LEASE_RESYNC_DONE:
3604 if (ioc->lil_count > IOC_IDS_MAX)
3605 GOTO(out_lease_close, rc = -EINVAL);
3607 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
3608 OBD_ALLOC(data, data_size);
3610 GOTO(out_lease_close, rc = -ENOMEM);
3612 if (copy_from_user(data, (void __user *)arg, data_size))
3613 GOTO(out_lease_close, rc = -EFAULT);
3615 bias = MDS_CLOSE_RESYNC_DONE;
3617 case LL_LEASE_LAYOUT_MERGE: {
3620 if (ioc->lil_count != 1)
3621 GOTO(out_lease_close, rc = -EINVAL);
3623 arg += sizeof(*ioc);
3624 if (copy_from_user(&fd, (void __user *)arg, sizeof(__u32)))
3625 GOTO(out_lease_close, rc = -EFAULT);
3627 layout_file = fget(fd);
3629 GOTO(out_lease_close, rc = -EBADF);
3631 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
3632 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
3633 GOTO(out_lease_close, rc = -EPERM);
3635 data = file_inode(layout_file);
3636 bias = MDS_CLOSE_LAYOUT_MERGE;
3639 case LL_LEASE_LAYOUT_SPLIT: {
3643 if (ioc->lil_count != 2)
3644 GOTO(out_lease_close, rc = -EINVAL);
3646 arg += sizeof(*ioc);
3647 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3648 GOTO(out_lease_close, rc = -EFAULT);
3650 arg += sizeof(__u32);
3651 if (copy_from_user(&mirror_id, (void __user *)arg,
3653 GOTO(out_lease_close, rc = -EFAULT);
3655 layout_file = fget(fdv);
3657 GOTO(out_lease_close, rc = -EBADF);
3659 /* if layout_file == file, it means to destroy the mirror */
3660 sp.sp_inode = file_inode(layout_file);
3661 sp.sp_mirror_id = (__u16)mirror_id;
3663 bias = MDS_CLOSE_LAYOUT_SPLIT;
3666 case LL_LEASE_PCC_ATTACH:
3667 if (ioc->lil_count != 1)
3670 arg += sizeof(*ioc);
3671 if (copy_from_user(¶m.pa_archive_id, (void __user *)arg,
3673 GOTO(out_lease_close, rc2 = -EFAULT);
3675 rc2 = pcc_readwrite_attach(file, inode, param.pa_archive_id);
3677 GOTO(out_lease_close, rc2);
3680 /* Grab latest data version */
3681 rc2 = ll_data_version(inode, ¶m.pa_data_version,
3684 GOTO(out_lease_close, rc2);
3687 bias = MDS_PCC_ATTACH;
3690 /* without close intent */
3695 rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
3699 rc = ll_lease_och_release(inode, file);
3708 switch (ioc->lil_flags) {
3709 case LL_LEASE_RESYNC_DONE:
3711 OBD_FREE(data, data_size);
3713 case LL_LEASE_LAYOUT_MERGE:
3714 case LL_LEASE_LAYOUT_SPLIT:
3718 ll_layout_refresh(inode, &fd->fd_layout_version);
3720 case LL_LEASE_PCC_ATTACH:
3723 rc = pcc_readwrite_attach_fini(file, inode,
3724 param.pa_layout_gen,
3731 rc = ll_lease_type_from_fmode(fmode);
3735 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
3738 struct inode *inode = file_inode(file);
3739 struct ll_inode_info *lli = ll_i2info(inode);
3740 struct ll_file_data *fd = file->private_data;
3741 struct obd_client_handle *och = NULL;
3742 __u64 open_flags = 0;
3748 switch (ioc->lil_mode) {
3749 case LL_LEASE_WRLCK:
3750 if (!(file->f_mode & FMODE_WRITE))
3752 fmode = FMODE_WRITE;
3754 case LL_LEASE_RDLCK:
3755 if (!(file->f_mode & FMODE_READ))
3759 case LL_LEASE_UNLCK:
3760 RETURN(ll_file_unlock_lease(file, ioc, arg));
3765 CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
3767 /* apply for lease */
3768 if (ioc->lil_flags & LL_LEASE_RESYNC)
3769 open_flags = MDS_OPEN_RESYNC;
3770 och = ll_lease_open(inode, file, fmode, open_flags);
3772 RETURN(PTR_ERR(och));
3774 if (ioc->lil_flags & LL_LEASE_RESYNC) {
3775 rc = ll_lease_file_resync(och, inode, arg);
3777 ll_lease_close(och, inode, NULL);
3780 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
3782 ll_lease_close(och, inode, NULL);
3788 mutex_lock(&lli->lli_och_mutex);
3789 if (fd->fd_lease_och == NULL) {
3790 fd->fd_lease_och = och;
3793 mutex_unlock(&lli->lli_och_mutex);
3795 /* impossible now that only excl is supported for now */
3796 ll_lease_close(och, inode, &lease_broken);
3802 static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
3804 struct ll_inode_info *lli = ll_i2info(inode);
3805 struct ll_sb_info *sbi = ll_i2sbi(inode);
3806 __u64 now = ktime_get_real_seconds();
3809 spin_lock(&lli->lli_heat_lock);
3810 heat->lh_flags = lli->lli_heat_flags;
3811 for (i = 0; i < heat->lh_count; i++)
3812 heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
3813 now, sbi->ll_heat_decay_weight,
3814 sbi->ll_heat_period_second);
3815 spin_unlock(&lli->lli_heat_lock);
3818 static int ll_heat_set(struct inode *inode, enum lu_heat_flag flags)
3820 struct ll_inode_info *lli = ll_i2info(inode);
3823 spin_lock(&lli->lli_heat_lock);
3824 if (flags & LU_HEAT_FLAG_CLEAR)
3825 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
3827 if (flags & LU_HEAT_FLAG_OFF)
3828 lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
3830 lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
3832 spin_unlock(&lli->lli_heat_lock);
3838 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3840 struct inode *inode = file_inode(file);
3841 struct ll_file_data *fd = file->private_data;
3845 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
3846 PFID(ll_inode2fid(inode)), inode, cmd);
3847 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
3849 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
3850 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
3854 case LL_IOC_GETFLAGS:
3855 /* Get the current value of the file flags */
3856 return put_user(fd->fd_flags, (int __user *)arg);
3857 case LL_IOC_SETFLAGS:
3858 case LL_IOC_CLRFLAGS:
3859 /* Set or clear specific file flags */
3860 /* XXX This probably needs checks to ensure the flags are
3861 * not abused, and to handle any flag side effects.
3863 if (get_user(flags, (int __user *) arg))
3866 if (cmd == LL_IOC_SETFLAGS) {
3867 if ((flags & LL_FILE_IGNORE_LOCK) &&
3868 !(file->f_flags & O_DIRECT)) {
3869 CERROR("%s: unable to disable locking on "
3870 "non-O_DIRECT file\n", current->comm);
3874 fd->fd_flags |= flags;
3876 fd->fd_flags &= ~flags;
3879 case LL_IOC_LOV_SETSTRIPE:
3880 case LL_IOC_LOV_SETSTRIPE_NEW:
3881 RETURN(ll_lov_setstripe(inode, file, (void __user *)arg));
3882 case LL_IOC_LOV_SETEA:
3883 RETURN(ll_lov_setea(inode, file, (void __user *)arg));
3884 case LL_IOC_LOV_SWAP_LAYOUTS: {
3886 struct lustre_swap_layouts lsl;
3888 if (copy_from_user(&lsl, (char __user *)arg,
3889 sizeof(struct lustre_swap_layouts)))
3892 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
3895 file2 = fget(lsl.sl_fd);
3899 /* O_WRONLY or O_RDWR */
3900 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
3901 GOTO(out, rc = -EPERM);
3903 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
3904 struct inode *inode2;
3905 struct ll_inode_info *lli;
3906 struct obd_client_handle *och = NULL;
3908 lli = ll_i2info(inode);
3909 mutex_lock(&lli->lli_och_mutex);
3910 if (fd->fd_lease_och != NULL) {
3911 och = fd->fd_lease_och;
3912 fd->fd_lease_och = NULL;
3914 mutex_unlock(&lli->lli_och_mutex);
3916 GOTO(out, rc = -ENOLCK);
3917 inode2 = file_inode(file2);
3918 rc = ll_swap_layouts_close(och, inode, inode2);
3920 rc = ll_swap_layouts(file, file2, &lsl);
3926 case LL_IOC_LOV_GETSTRIPE:
3927 case LL_IOC_LOV_GETSTRIPE_NEW:
3928 RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
3929 case FS_IOC_GETFLAGS:
3930 case FS_IOC_SETFLAGS:
3931 RETURN(ll_iocontrol(inode, file, cmd, arg));
3932 case FSFILT_IOC_GETVERSION:
3933 case FS_IOC_GETVERSION:
3934 RETURN(put_user(inode->i_generation, (int __user *)arg));
3935 /* We need to special case any other ioctls we want to handle,
3936 * to send them to the MDS/OST as appropriate and to properly
3937 * network encode the arg field. */
3938 case FS_IOC_SETVERSION:
3941 case LL_IOC_GROUP_LOCK:
3942 RETURN(ll_get_grouplock(inode, file, arg));
3943 case LL_IOC_GROUP_UNLOCK:
3944 RETURN(ll_put_grouplock(inode, file, arg));
3945 case IOC_OBD_STATFS:
3946 RETURN(ll_obd_statfs(inode, (void __user *)arg));
3948 case LL_IOC_FLUSHCTX:
3949 RETURN(ll_flush_ctx(inode));
3950 case LL_IOC_PATH2FID: {
3951 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
3952 sizeof(struct lu_fid)))
3957 case LL_IOC_GETPARENT:
3958 RETURN(ll_getparent(file, (struct getparent __user *)arg));
3960 case OBD_IOC_FID2PATH:
3961 RETURN(ll_fid2path(inode, (void __user *)arg));
3962 case LL_IOC_DATA_VERSION: {
3963 struct ioc_data_version idv;
3966 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
3969 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
3970 rc = ll_ioc_data_version(inode, &idv);
3973 copy_to_user((char __user *)arg, &idv, sizeof(idv)))
3979 case LL_IOC_GET_MDTIDX: {
3982 mdtidx = ll_get_mdt_idx(inode);
3986 if (put_user((int)mdtidx, (int __user *)arg))
3991 case OBD_IOC_GETNAME_OLD:
3993 case OBD_IOC_GETDTNAME:
3995 case OBD_IOC_GETMDNAME:
3996 RETURN(ll_get_obd_name(inode, cmd, arg));
3997 case LL_IOC_HSM_STATE_GET: {
3998 struct md_op_data *op_data;
3999 struct hsm_user_state *hus;
4006 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4007 LUSTRE_OPC_ANY, hus);
4008 if (IS_ERR(op_data)) {
4010 RETURN(PTR_ERR(op_data));
4013 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4016 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
4019 ll_finish_md_op_data(op_data);
4023 case LL_IOC_HSM_STATE_SET: {
4024 struct hsm_state_set *hss;
4031 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
4036 rc = ll_hsm_state_set(inode, hss);
4041 case LL_IOC_HSM_ACTION: {
4042 struct md_op_data *op_data;
4043 struct hsm_current_action *hca;
4050 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4051 LUSTRE_OPC_ANY, hca);
4052 if (IS_ERR(op_data)) {
4054 RETURN(PTR_ERR(op_data));
4057 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
4060 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
4063 ll_finish_md_op_data(op_data);
4067 case LL_IOC_SET_LEASE_OLD: {
4068 struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
4070 RETURN(ll_file_set_lease(file, &ioc, 0));
4072 case LL_IOC_SET_LEASE: {
4073 struct ll_ioc_lease ioc;
4075 if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
4078 RETURN(ll_file_set_lease(file, &ioc, arg));
4080 case LL_IOC_GET_LEASE: {
4081 struct ll_inode_info *lli = ll_i2info(inode);
4082 struct ldlm_lock *lock = NULL;
4085 mutex_lock(&lli->lli_och_mutex);
4086 if (fd->fd_lease_och != NULL) {
4087 struct obd_client_handle *och = fd->fd_lease_och;
4089 lock = ldlm_handle2lock(&och->och_lease_handle);
4091 lock_res_and_lock(lock);
4092 if (!ldlm_is_cancel(lock))
4093 fmode = och->och_flags;
4095 unlock_res_and_lock(lock);
4096 LDLM_LOCK_PUT(lock);
4099 mutex_unlock(&lli->lli_och_mutex);
4101 RETURN(ll_lease_type_from_fmode(fmode));
4103 case LL_IOC_HSM_IMPORT: {
4104 struct hsm_user_import *hui;
4110 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
4115 rc = ll_hsm_import(inode, file, hui);
4120 case LL_IOC_FUTIMES_3: {
4121 struct ll_futimes_3 lfu;
4123 if (copy_from_user(&lfu,
4124 (const struct ll_futimes_3 __user *)arg,
4128 RETURN(ll_file_futimes_3(file, &lfu));
4130 case LL_IOC_LADVISE: {
4131 struct llapi_ladvise_hdr *k_ladvise_hdr;
4132 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
4135 int alloc_size = sizeof(*k_ladvise_hdr);
4138 u_ladvise_hdr = (void __user *)arg;
4139 OBD_ALLOC_PTR(k_ladvise_hdr);
4140 if (k_ladvise_hdr == NULL)
4143 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4144 GOTO(out_ladvise, rc = -EFAULT);
4146 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
4147 k_ladvise_hdr->lah_count < 1)
4148 GOTO(out_ladvise, rc = -EINVAL);
4150 num_advise = k_ladvise_hdr->lah_count;
4151 if (num_advise >= LAH_COUNT_MAX)
4152 GOTO(out_ladvise, rc = -EFBIG);
4154 OBD_FREE_PTR(k_ladvise_hdr);
4155 alloc_size = offsetof(typeof(*k_ladvise_hdr),
4156 lah_advise[num_advise]);
4157 OBD_ALLOC(k_ladvise_hdr, alloc_size);
4158 if (k_ladvise_hdr == NULL)
4162 * TODO: submit multiple advices to one server in a single RPC
4164 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
4165 GOTO(out_ladvise, rc = -EFAULT);
4167 for (i = 0; i < num_advise; i++) {
4168 struct llapi_lu_ladvise *k_ladvise =
4169 &k_ladvise_hdr->lah_advise[i];
4170 struct llapi_lu_ladvise __user *u_ladvise =
4171 &u_ladvise_hdr->lah_advise[i];
4173 rc = ll_ladvise_sanity(inode, k_ladvise);
4175 GOTO(out_ladvise, rc);
4177 switch (k_ladvise->lla_advice) {
4178 case LU_LADVISE_LOCKNOEXPAND:
4179 rc = ll_lock_noexpand(file,
4180 k_ladvise->lla_peradvice_flags);
4181 GOTO(out_ladvise, rc);
4182 case LU_LADVISE_LOCKAHEAD:
4184 rc = ll_file_lock_ahead(file, k_ladvise);
4187 GOTO(out_ladvise, rc);
4190 &u_ladvise->lla_lockahead_result))
4191 GOTO(out_ladvise, rc = -EFAULT);
4194 rc = ll_ladvise(inode, file,
4195 k_ladvise_hdr->lah_flags,
4198 GOTO(out_ladvise, rc);
4205 OBD_FREE(k_ladvise_hdr, alloc_size);
4208 case LL_IOC_FLR_SET_MIRROR: {
4209 /* mirror I/O must be direct to avoid polluting page cache
4211 if (!(file->f_flags & O_DIRECT))
4214 fd->fd_designated_mirror = (__u32)arg;
4217 case FS_IOC_FSGETXATTR:
4218 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
4219 case FS_IOC_FSSETXATTR:
4220 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
4221 case LL_IOC_PROJECT:
4222 RETURN(ll_ioctl_project(file, cmd, arg));
4224 RETURN(put_user(PAGE_SIZE, (int __user *)arg));
4225 case LL_IOC_HEAT_GET: {
4226 struct lu_heat uheat;
4227 struct lu_heat *heat;
4230 if (copy_from_user(&uheat, (void __user *)arg, sizeof(uheat)))
4233 if (uheat.lh_count > OBD_HEAT_COUNT)
4234 uheat.lh_count = OBD_HEAT_COUNT;
4236 size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
4237 OBD_ALLOC(heat, size);
4241 heat->lh_count = uheat.lh_count;
4242 ll_heat_get(inode, heat);
4243 rc = copy_to_user((char __user *)arg, heat, size);
4244 OBD_FREE(heat, size);
4245 RETURN(rc ? -EFAULT : 0);
4247 case LL_IOC_HEAT_SET: {
4250 if (copy_from_user(&flags, (void __user *)arg, sizeof(flags)))
4253 rc = ll_heat_set(inode, flags);
4256 case LL_IOC_PCC_DETACH: {
4257 struct lu_pcc_detach *detach;
4259 OBD_ALLOC_PTR(detach);
4263 if (copy_from_user(detach,
4264 (const struct lu_pcc_detach __user *)arg,
4266 GOTO(out_detach_free, rc = -EFAULT);
4268 if (!S_ISREG(inode->i_mode))
4269 GOTO(out_detach_free, rc = -EINVAL);
4271 if (!inode_owner_or_capable(inode))
4272 GOTO(out_detach_free, rc = -EPERM);
4274 rc = pcc_ioctl_detach(inode, detach->pccd_opt);
4276 OBD_FREE_PTR(detach);
4279 case LL_IOC_PCC_STATE: {
4280 struct lu_pcc_state __user *ustate =
4281 (struct lu_pcc_state __user *)arg;
4282 struct lu_pcc_state *state;
4284 OBD_ALLOC_PTR(state);
4288 if (copy_from_user(state, ustate, sizeof(*state)))
4289 GOTO(out_state, rc = -EFAULT);
4291 rc = pcc_ioctl_state(file, inode, state);
4293 GOTO(out_state, rc);
4295 if (copy_to_user(ustate, state, sizeof(*state)))
4296 GOTO(out_state, rc = -EFAULT);
4299 OBD_FREE_PTR(state);
4302 #ifdef HAVE_LUSTRE_CRYPTO
4303 case LL_IOC_SET_ENCRYPTION_POLICY:
4304 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4306 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
4307 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
4308 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4310 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
4311 case LL_IOC_ADD_ENCRYPTION_KEY:
4312 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4314 return llcrypt_ioctl_add_key(file, (void __user *)arg);
4315 case LL_IOC_REMOVE_ENCRYPTION_KEY:
4316 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4318 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
4319 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4320 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4322 return llcrypt_ioctl_remove_key_all_users(file,
4323 (void __user *)arg);
4324 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
4325 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4327 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
4330 case LL_IOC_UNLOCK_FOREIGN: {
4331 struct dentry *dentry = file_dentry(file);
4333 /* if not a foreign symlink do nothing */
4334 if (ll_foreign_is_removable(dentry, true)) {
4336 "prevent unlink of non-foreign file ("DFID")\n",
4337 PFID(ll_inode2fid(inode)));
4338 RETURN(-EOPNOTSUPP);
4344 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
4345 (void __user *)arg));
4349 loff_t ll_lseek(struct file *file, loff_t offset, int whence)
4351 struct inode *inode = file_inode(file);
4354 struct cl_lseek_io *lsio;
4361 env = cl_env_get(&refcheck);
4363 RETURN(PTR_ERR(env));
4365 io = vvp_env_thread_io(env);
4366 io->ci_obj = ll_i2info(inode)->lli_clob;
4367 ll_io_set_mirror(io, file);
4369 lsio = &io->u.ci_lseek;
4370 lsio->ls_start = offset;
4371 lsio->ls_whence = whence;
4372 lsio->ls_result = -ENXIO;
4375 rc = cl_io_init(env, io, CIT_LSEEK, io->ci_obj);
4377 struct vvp_io *vio = vvp_env_io(env);
4379 vio->vui_fd = file->private_data;
4380 rc = cl_io_loop(env, io);
4384 retval = rc ? : lsio->ls_result;
4385 cl_io_fini(env, io);
4386 } while (unlikely(io->ci_need_restart));
4388 cl_env_put(env, &refcheck);
4393 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
4395 struct inode *inode = file_inode(file);
4396 loff_t retval = offset, eof = 0;
4397 ktime_t kstart = ktime_get();
4401 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
4402 PFID(ll_inode2fid(inode)), inode, retval, retval,
4405 if (origin == SEEK_END) {
4406 retval = ll_glimpse_size(inode);
4409 eof = i_size_read(inode);
4412 if (origin == SEEK_HOLE || origin == SEEK_DATA) {
4416 /* flush local cache first if any */
4417 cl_sync_file_range(inode, offset, OBD_OBJECT_EOF,
4420 retval = ll_lseek(file, offset, origin);
4423 retval = vfs_setpos(file, retval, ll_file_maxbytes(inode));
4425 retval = generic_file_llseek_size(file, offset, origin,
4426 ll_file_maxbytes(inode), eof);
4429 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK,
4430 ktime_us_delta(ktime_get(), kstart));
4434 static int ll_flush(struct file *file, fl_owner_t id)
4436 struct inode *inode = file_inode(file);
4437 struct ll_inode_info *lli = ll_i2info(inode);
4438 struct ll_file_data *fd = file->private_data;
4441 LASSERT(!S_ISDIR(inode->i_mode));
4443 /* catch async errors that were recorded back when async writeback
4444 * failed for pages in this mapping. */
4445 rc = lli->lli_async_rc;
4446 lli->lli_async_rc = 0;
4447 if (lli->lli_clob != NULL) {
4448 err = lov_read_and_clear_async_rc(lli->lli_clob);
4453 /* The application has been told write failure already.
4454 * Do not report failure again. */
4455 if (fd->fd_write_failed)
4457 return rc ? -EIO : 0;
4461 * Called to make sure a portion of file has been written out.
4462 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
4464 * Return how many pages have been written.
4466 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
4467 enum cl_fsync_mode mode, int ignore_layout)
4471 struct cl_fsync_io *fio;
4476 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
4477 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
4480 env = cl_env_get(&refcheck);
4482 RETURN(PTR_ERR(env));
4484 io = vvp_env_thread_io(env);
4485 io->ci_obj = ll_i2info(inode)->lli_clob;
4486 io->ci_ignore_layout = ignore_layout;
4488 /* initialize parameters for sync */
4489 fio = &io->u.ci_fsync;
4490 fio->fi_start = start;
4492 fio->fi_fid = ll_inode2fid(inode);
4493 fio->fi_mode = mode;
4494 fio->fi_nr_written = 0;
4496 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
4497 result = cl_io_loop(env, io);
4499 result = io->ci_result;
4501 result = fio->fi_nr_written;
4502 cl_io_fini(env, io);
4503 cl_env_put(env, &refcheck);
4509 * When dentry is provided (the 'else' case), file_dentry() may be
4510 * null and dentry must be used directly rather than pulled from
4511 * file_dentry() as is done otherwise.
4514 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
4516 struct dentry *dentry = file_dentry(file);
4517 struct inode *inode = dentry->d_inode;
4518 struct ll_inode_info *lli = ll_i2info(inode);
4519 struct ptlrpc_request *req;
4520 ktime_t kstart = ktime_get();
4526 "VFS Op:inode="DFID"(%p), start %lld, end %lld, datasync %d\n",
4527 PFID(ll_inode2fid(inode)), inode, start, end, datasync);
4529 /* fsync's caller has already called _fdata{sync,write}, we want
4530 * that IO to finish before calling the osc and mdc sync methods */
4531 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
4534 /* catch async errors that were recorded back when async writeback
4535 * failed for pages in this mapping. */
4536 if (!S_ISDIR(inode->i_mode)) {
4537 err = lli->lli_async_rc;
4538 lli->lli_async_rc = 0;
4541 if (lli->lli_clob != NULL) {
4542 err = lov_read_and_clear_async_rc(lli->lli_clob);
4548 err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
4552 ptlrpc_req_finished(req);
4554 if (S_ISREG(inode->i_mode)) {
4555 struct ll_file_data *fd = file->private_data;
4558 /* Sync metadata on MDT first, and then sync the cached data
4561 err = pcc_fsync(file, start, end, datasync, &cached);
4563 err = cl_sync_file_range(inode, start, end,
4565 if (rc == 0 && err < 0)
4568 fd->fd_write_failed = true;
4570 fd->fd_write_failed = false;
4573 inode_unlock(inode);
4576 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC,
4577 ktime_us_delta(ktime_get(), kstart));
4582 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
4584 struct inode *inode = file_inode(file);
4585 struct ll_sb_info *sbi = ll_i2sbi(inode);
4586 struct ldlm_enqueue_info einfo = {
4587 .ei_type = LDLM_FLOCK,
4588 .ei_cb_cp = ldlm_flock_completion_ast,
4589 .ei_cbdata = file_lock,
4591 struct md_op_data *op_data;
4592 struct lustre_handle lockh = { 0 };
4593 union ldlm_policy_data flock = { { 0 } };
4594 int fl_type = file_lock->fl_type;
4595 ktime_t kstart = ktime_get();
4601 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
4602 PFID(ll_inode2fid(inode)), file_lock);
4604 if (file_lock->fl_flags & FL_FLOCK) {
4605 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
4606 /* flocks are whole-file locks */
4607 flock.l_flock.end = OFFSET_MAX;
4608 /* For flocks owner is determined by the local file desctiptor*/
4609 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
4610 } else if (file_lock->fl_flags & FL_POSIX) {
4611 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
4612 flock.l_flock.start = file_lock->fl_start;
4613 flock.l_flock.end = file_lock->fl_end;
4617 flock.l_flock.pid = file_lock->fl_pid;
4619 #if defined(HAVE_LM_COMPARE_OWNER) || defined(lm_compare_owner)
4620 /* Somewhat ugly workaround for svc lockd.
4621 * lockd installs custom fl_lmops->lm_compare_owner that checks
4622 * for the fl_owner to be the same (which it always is on local node
4623 * I guess between lockd processes) and then compares pid.
4624 * As such we assign pid to the owner field to make it all work,
4625 * conflict with normal locks is unlikely since pid space and
4626 * pointer space for current->files are not intersecting */
4627 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
4628 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
4633 einfo.ei_mode = LCK_PR;
4636 /* An unlock request may or may not have any relation to
4637 * existing locks so we may not be able to pass a lock handle
4638 * via a normal ldlm_lock_cancel() request. The request may even
4639 * unlock a byte range in the middle of an existing lock. In
4640 * order to process an unlock request we need all of the same
4641 * information that is given with a normal read or write record
4642 * lock request. To avoid creating another ldlm unlock (cancel)
4643 * message we'll treat a LCK_NL flock request as an unlock. */
4644 einfo.ei_mode = LCK_NL;
4647 einfo.ei_mode = LCK_PW;
4650 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
4665 flags = LDLM_FL_BLOCK_NOWAIT;
4671 flags = LDLM_FL_TEST_LOCK;
4674 CERROR("unknown fcntl lock command: %d\n", cmd);
4678 /* Save the old mode so that if the mode in the lock changes we
4679 * can decrement the appropriate reader or writer refcount. */
4680 file_lock->fl_type = einfo.ei_mode;
4682 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4683 LUSTRE_OPC_ANY, NULL);
4684 if (IS_ERR(op_data))
4685 RETURN(PTR_ERR(op_data));
4687 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
4688 "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
4689 flock.l_flock.pid, flags, einfo.ei_mode,
4690 flock.l_flock.start, flock.l_flock.end);
4692 rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
4695 /* Restore the file lock type if not TEST lock. */
4696 if (!(flags & LDLM_FL_TEST_LOCK))
4697 file_lock->fl_type = fl_type;
4699 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
4700 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
4701 !(flags & LDLM_FL_TEST_LOCK))
4702 rc2 = locks_lock_file_wait(file, file_lock);
4704 if ((file_lock->fl_flags & FL_FLOCK) &&
4705 (rc == 0 || file_lock->fl_type == F_UNLCK))
4706 rc2 = flock_lock_file_wait(file, file_lock);
4707 if ((file_lock->fl_flags & FL_POSIX) &&
4708 (rc == 0 || file_lock->fl_type == F_UNLCK) &&
4709 !(flags & LDLM_FL_TEST_LOCK))
4710 rc2 = posix_lock_file_wait(file, file_lock);
4711 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
4713 if (rc2 && file_lock->fl_type != F_UNLCK) {
4714 einfo.ei_mode = LCK_NL;
4715 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
4720 ll_finish_md_op_data(op_data);
4723 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK,
4724 ktime_us_delta(ktime_get(), kstart));
4728 int ll_get_fid_by_name(struct inode *parent, const char *name,
4729 int namelen, struct lu_fid *fid,
4730 struct inode **inode)
4732 struct md_op_data *op_data = NULL;
4733 struct mdt_body *body;
4734 struct ptlrpc_request *req;
4738 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
4739 LUSTRE_OPC_ANY, NULL);
4740 if (IS_ERR(op_data))
4741 RETURN(PTR_ERR(op_data));
4743 op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
4744 rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
4745 ll_finish_md_op_data(op_data);
4749 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4751 GOTO(out_req, rc = -EFAULT);
4753 *fid = body->mbo_fid1;
4756 rc = ll_prep_inode(inode, &req->rq_pill, parent->i_sb, NULL);
4758 ptlrpc_req_finished(req);
4762 int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
4765 struct dentry *dchild = NULL;
4766 struct inode *child_inode = NULL;
4767 struct md_op_data *op_data;
4768 struct ptlrpc_request *request = NULL;
4769 struct obd_client_handle *och = NULL;
4771 struct mdt_body *body;
4772 __u64 data_version = 0;
4773 size_t namelen = strlen(name);
4774 int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
4778 CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
4779 PFID(ll_inode2fid(parent)), name,
4780 lum->lum_stripe_offset, lum->lum_stripe_count);
4782 if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
4783 lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
4784 lustre_swab_lmv_user_md(lum);
4786 /* Get child FID first */
4787 qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
4790 dchild = d_lookup(file_dentry(file), &qstr);
4792 if (dchild->d_inode)
4793 child_inode = igrab(dchild->d_inode);
4798 rc = ll_get_fid_by_name(parent, name, namelen, NULL,
4807 if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
4808 OBD_CONNECT2_DIR_MIGRATE)) {
4809 if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
4810 ll_dir_striped(child_inode)) {
4811 CERROR("%s: MDT doesn't support stripe directory "
4812 "migration!\n", ll_i2sbi(parent)->ll_fsname);
4813 GOTO(out_iput, rc = -EOPNOTSUPP);
4818 * lfs migrate command needs to be blocked on the client
4819 * by checking the migrate FID against the FID of the
4822 if (is_root_inode(child_inode))
4823 GOTO(out_iput, rc = -EINVAL);
4825 if (IS_ENCRYPTED(child_inode)) {
4826 rc = llcrypt_get_encryption_info(child_inode);
4829 if (!llcrypt_has_encryption_key(child_inode)) {
4830 CDEBUG(D_SEC, "no enc key for "DFID"\n",
4831 PFID(ll_inode2fid(child_inode)));
4832 GOTO(out_iput, rc = -ENOKEY);
4834 if (unlikely(!llcrypt_policy_has_filename_enc(child_inode))) {
4836 "cannot migrate old format encrypted "DFID", please move to new enc dir first\n",
4837 PFID(ll_inode2fid(child_inode)));
4838 GOTO(out_iput, rc = -EUCLEAN);
4842 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
4843 child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
4844 if (IS_ERR(op_data))
4845 GOTO(out_iput, rc = PTR_ERR(op_data));
4847 inode_lock(child_inode);
4848 op_data->op_fid3 = *ll_inode2fid(child_inode);
4849 if (!fid_is_sane(&op_data->op_fid3)) {
4850 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
4851 ll_i2sbi(parent)->ll_fsname, name,
4852 PFID(&op_data->op_fid3));
4853 GOTO(out_unlock, rc = -EINVAL);
4856 op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
4857 op_data->op_data = lum;
4858 op_data->op_data_size = lumlen;
4861 if (S_ISREG(child_inode->i_mode)) {
4862 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
4866 GOTO(out_unlock, rc);
4869 rc = ll_data_version(child_inode, &data_version,
4872 GOTO(out_close, rc);
4874 op_data->op_open_handle = och->och_open_handle;
4875 op_data->op_data_version = data_version;
4876 op_data->op_lease_handle = och->och_lease_handle;
4877 op_data->op_bias |= MDS_CLOSE_MIGRATE;
4879 spin_lock(&och->och_mod->mod_open_req->rq_lock);
4880 och->och_mod->mod_open_req->rq_replay = 0;
4881 spin_unlock(&och->och_mod->mod_open_req->rq_lock);
4884 rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name, namelen,
4885 name, namelen, &request);
4887 LASSERT(request != NULL);
4888 ll_update_times(request, parent);
4891 if (rc == 0 || rc == -EAGAIN) {
4892 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
4893 LASSERT(body != NULL);
4895 /* If the server does release layout lock, then we cleanup
4896 * the client och here, otherwise release it in out_close: */
4897 if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
4898 obd_mod_put(och->och_mod);
4899 md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
4901 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
4907 if (request != NULL) {
4908 ptlrpc_req_finished(request);
4912 /* Try again if the lease has cancelled. */
4913 if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
4918 ll_lease_close(och, child_inode, NULL);
4920 clear_nlink(child_inode);
4922 inode_unlock(child_inode);
4923 ll_finish_md_op_data(op_data);
4930 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
4932 struct ll_file_data *fd = file->private_data;
4936 * In order to avoid flood of warning messages, only print one message
4937 * for one file. And the entire message rate on the client is limited
4938 * by CDEBUG_LIMIT too.
4940 if (!(fd->fd_flags & LL_FILE_FLOCK_WARNING)) {
4941 fd->fd_flags |= LL_FILE_FLOCK_WARNING;
4942 CDEBUG_LIMIT(D_TTY | D_CONSOLE,
4943 "flock disabled, mount with '-o [local]flock' to enable\r\n");
4949 * test if some locks matching bits and l_req_mode are acquired
4950 * - bits can be in different locks
4951 * - if found clear the common lock bits in *bits
4952 * - the bits not found, are kept in *bits
4954 * \param bits [IN] searched lock bits [IN]
4955 * \param l_req_mode [IN] searched lock mode
4956 * \retval boolean, true iff all bits are found
4958 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
4960 struct lustre_handle lockh;
4961 union ldlm_policy_data policy;
4962 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
4963 (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
4972 fid = &ll_i2info(inode)->lli_fid;
4973 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
4974 ldlm_lockname[mode]);
4976 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
4977 for (i = 0; i < MDS_INODELOCK_NUMBITS && *bits != 0; i++) {
4978 policy.l_inodebits.bits = *bits & BIT(i);
4979 if (policy.l_inodebits.bits == 0)
4982 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
4983 &policy, mode, &lockh)) {
4984 struct ldlm_lock *lock;
4986 lock = ldlm_handle2lock(&lockh);
4989 ~(lock->l_policy_data.l_inodebits.bits);
4990 LDLM_LOCK_PUT(lock);
4992 *bits &= ~policy.l_inodebits.bits;
4999 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
5000 struct lustre_handle *lockh, __u64 flags,
5001 enum ldlm_mode mode)
5003 union ldlm_policy_data policy = { .l_inodebits = { bits } };
5008 fid = &ll_i2info(inode)->lli_fid;
5009 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
5011 rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
5012 fid, LDLM_IBITS, &policy, mode, lockh);
5017 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
5019 /* Already unlinked. Just update nlink and return success */
5020 if (rc == -ENOENT) {
5022 /* If it is striped directory, and there is bad stripe
5023 * Let's revalidate the dentry again, instead of returning
5025 if (ll_dir_striped(inode))
5028 /* This path cannot be hit for regular files unless in
5029 * case of obscure races, so no need to to validate
5031 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
5033 } else if (rc != 0) {
5034 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
5035 "%s: revalidate FID "DFID" error: rc = %d\n",
5036 ll_i2sbi(inode)->ll_fsname,
5037 PFID(ll_inode2fid(inode)), rc);
5043 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
5045 struct inode *parent;
5046 struct inode *inode = dentry->d_inode;
5047 struct obd_export *exp = ll_i2mdexp(inode);
5048 struct lookup_intent oit = {
5051 struct ptlrpc_request *req = NULL;
5052 struct md_op_data *op_data;
5053 const char *name = NULL;
5058 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
5059 PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
5061 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID) {
5062 parent = dentry->d_parent->d_inode;
5063 name = dentry->d_name.name;
5064 namelen = dentry->d_name.len;
5069 op_data = ll_prep_md_op_data(NULL, parent, inode, name, namelen, 0,
5070 LUSTRE_OPC_ANY, NULL);
5071 if (IS_ERR(op_data))
5072 RETURN(PTR_ERR(op_data));
5074 /* Call getattr by fid */
5075 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID)
5076 op_data->op_flags = MF_GETATTR_BY_FID;
5077 rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
5078 ll_finish_md_op_data(op_data);
5080 rc = ll_inode_revalidate_fini(inode, rc);
5084 rc = ll_revalidate_it_finish(req, &oit, dentry);
5086 ll_intent_release(&oit);
5090 /* Unlinked? Unhash dentry, so it is not picked up later by
5091 * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
5092 * here to preserve get_cwd functionality on 2.6.
5094 if (!dentry->d_inode->i_nlink)
5095 d_lustre_invalidate(dentry);
5097 ll_lookup_finish_locks(&oit, dentry);
5099 ptlrpc_req_finished(req);
5104 static int ll_merge_md_attr(struct inode *inode)
5106 struct ll_inode_info *lli = ll_i2info(inode);
5107 struct cl_attr attr = { 0 };
5110 LASSERT(lli->lli_lsm_md != NULL);
5112 if (!lmv_dir_striped(lli->lli_lsm_md))
5115 down_read(&lli->lli_lsm_sem);
5116 rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
5117 &attr, ll_md_blocking_ast);
5118 up_read(&lli->lli_lsm_sem);
5122 set_nlink(inode, attr.cat_nlink);
5123 inode->i_blocks = attr.cat_blocks;
5124 i_size_write(inode, attr.cat_size);
5126 ll_i2info(inode)->lli_atime = attr.cat_atime;
5127 ll_i2info(inode)->lli_mtime = attr.cat_mtime;
5128 ll_i2info(inode)->lli_ctime = attr.cat_ctime;
5133 int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
5134 unsigned int flags, bool foreign)
5136 struct inode *inode = de->d_inode;
5137 struct ll_sb_info *sbi = ll_i2sbi(inode);
5138 struct ll_inode_info *lli = ll_i2info(inode);
5139 struct inode *dir = de->d_parent->d_inode;
5140 bool need_glimpse = true;
5141 ktime_t kstart = ktime_get();
5144 /* The OST object(s) determine the file size, blocks and mtime. */
5145 if (!(request_mask & STATX_SIZE || request_mask & STATX_BLOCKS ||
5146 request_mask & STATX_MTIME))
5147 need_glimpse = false;
5149 if (dentry_may_statahead(dir, de))
5150 ll_start_statahead(dir, de, need_glimpse &&
5151 !(flags & AT_STATX_DONT_SYNC));
5153 if (flags & AT_STATX_DONT_SYNC)
5154 GOTO(fill_attr, rc = 0);
5156 rc = ll_inode_revalidate(de, IT_GETATTR);
5160 /* foreign file/dir are always of zero length, so don't
5161 * need to validate size.
5163 if (S_ISREG(inode->i_mode) && !foreign) {
5167 GOTO(fill_attr, rc);
5169 rc = pcc_inode_getattr(inode, request_mask, flags, &cached);
5170 if (cached && rc < 0)
5174 GOTO(fill_attr, rc);
5177 * If the returned attr is masked with OBD_MD_FLSIZE &
5178 * OBD_MD_FLBLOCKS & OBD_MD_FLMTIME, it means that the file size
5179 * or blocks obtained from MDT is strictly correct, and the file
5180 * is usually not being modified by clients, and the [a|m|c]time
5181 * got from MDT is also strictly correct.
5182 * Under this circumstance, it does not need to send glimpse
5183 * RPCs to OSTs for file attributes such as the size and blocks.
5185 if (lli->lli_attr_valid & OBD_MD_FLSIZE &&
5186 lli->lli_attr_valid & OBD_MD_FLBLOCKS &&
5187 lli->lli_attr_valid & OBD_MD_FLMTIME) {
5188 inode->i_mtime.tv_sec = lli->lli_mtime;
5189 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5190 inode->i_atime.tv_sec = lli->lli_atime;
5191 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5192 inode->i_ctime.tv_sec = lli->lli_ctime;
5193 GOTO(fill_attr, rc);
5196 /* In case of restore, the MDT has the right size and has
5197 * already send it back without granting the layout lock,
5198 * inode is up-to-date so glimpse is useless.
5199 * Also to glimpse we need the layout, in case of a running
5200 * restore the MDT holds the layout lock so the glimpse will
5201 * block up to the end of restore (getattr will block)
5203 if (!test_bit(LLIF_FILE_RESTORING, &lli->lli_flags)) {
5204 rc = ll_glimpse_size(inode);
5209 /* If object isn't regular a file then don't validate size. */
5210 /* foreign dir is not striped dir */
5211 if (ll_dir_striped(inode) && !foreign) {
5212 rc = ll_merge_md_attr(inode);
5217 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5218 inode->i_atime.tv_sec = lli->lli_atime;
5219 if (lli->lli_attr_valid & OBD_MD_FLMTIME)
5220 inode->i_mtime.tv_sec = lli->lli_mtime;
5221 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5222 inode->i_ctime.tv_sec = lli->lli_ctime;
5226 OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
5228 if (ll_need_32bit_api(sbi)) {
5229 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
5230 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
5231 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
5233 stat->ino = inode->i_ino;
5234 stat->dev = inode->i_sb->s_dev;
5235 stat->rdev = inode->i_rdev;
5238 /* foreign symlink to be exposed as a real symlink */
5240 stat->mode = inode->i_mode;
5242 stat->mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
5244 stat->uid = inode->i_uid;
5245 stat->gid = inode->i_gid;
5246 stat->atime = inode->i_atime;
5247 stat->mtime = inode->i_mtime;
5248 stat->ctime = inode->i_ctime;
5249 /* stat->blksize is used to tell about preferred IO size */
5250 if (sbi->ll_stat_blksize)
5251 stat->blksize = sbi->ll_stat_blksize;
5252 else if (S_ISREG(inode->i_mode))
5253 stat->blksize = 1 << min(PTLRPC_MAX_BRW_BITS + 1,
5254 LL_MAX_BLKSIZE_BITS);
5256 stat->blksize = 1 << inode->i_sb->s_blocksize_bits;
5258 stat->nlink = inode->i_nlink;
5259 stat->size = i_size_read(inode);
5260 stat->blocks = inode->i_blocks;
5262 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5263 if (flags & AT_STATX_DONT_SYNC) {
5264 if (stat->size == 0 &&
5265 lli->lli_attr_valid & OBD_MD_FLLAZYSIZE)
5266 stat->size = lli->lli_lazysize;
5267 if (stat->blocks == 0 &&
5268 lli->lli_attr_valid & OBD_MD_FLLAZYBLOCKS)
5269 stat->blocks = lli->lli_lazyblocks;
5272 if (lli->lli_attr_valid & OBD_MD_FLBTIME) {
5273 stat->result_mask |= STATX_BTIME;
5274 stat->btime.tv_sec = lli->lli_btime;
5277 stat->attributes_mask = STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
5278 stat->attributes |= ll_inode_to_ext_flags(inode->i_flags);
5279 stat->result_mask &= request_mask;
5282 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR,
5283 ktime_us_delta(ktime_get(), kstart));
5288 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5289 int ll_getattr(const struct path *path, struct kstat *stat,
5290 u32 request_mask, unsigned int flags)
5292 return ll_getattr_dentry(path->dentry, stat, request_mask, flags,
5296 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
5298 return ll_getattr_dentry(de, stat, STATX_BASIC_STATS,
5299 AT_STATX_SYNC_AS_STAT, false);
5303 int cl_falloc(struct inode *inode, int mode, loff_t offset, loff_t len)
5309 loff_t size = i_size_read(inode);
5313 env = cl_env_get(&refcheck);
5315 RETURN(PTR_ERR(env));
5317 io = vvp_env_thread_io(env);
5318 io->ci_obj = ll_i2info(inode)->lli_clob;
5319 io->ci_verify_layout = 1;
5320 io->u.ci_setattr.sa_parent_fid = lu_object_fid(&io->ci_obj->co_lu);
5321 io->u.ci_setattr.sa_falloc_mode = mode;
5322 io->u.ci_setattr.sa_falloc_offset = offset;
5323 io->u.ci_setattr.sa_falloc_end = offset + len;
5324 io->u.ci_setattr.sa_subtype = CL_SETATTR_FALLOCATE;
5325 if (io->u.ci_setattr.sa_falloc_end > size) {
5326 loff_t newsize = io->u.ci_setattr.sa_falloc_end;
5328 /* Check new size against VFS/VM file size limit and rlimit */
5329 rc = inode_newsize_ok(inode, newsize);
5332 if (newsize > ll_file_maxbytes(inode)) {
5333 CDEBUG(D_INODE, "file size too large %llu > %llu\n",
5334 (unsigned long long)newsize,
5335 ll_file_maxbytes(inode));
5342 rc = cl_io_init(env, io, CIT_SETATTR, io->ci_obj);
5344 rc = cl_io_loop(env, io);
5347 cl_io_fini(env, io);
5348 } while (unlikely(io->ci_need_restart));
5351 cl_env_put(env, &refcheck);
5355 long ll_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
5357 struct inode *inode = filp->f_path.dentry->d_inode;
5360 if (offset < 0 || len <= 0)
5363 * Encrypted inodes can't handle collapse range or zero range or insert
5364 * range since we would need to re-encrypt blocks with a different IV or
5365 * XTS tweak (which are based on the logical block number).
5366 * Similar to what ext4 does.
5368 if (IS_ENCRYPTED(inode) &&
5369 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
5370 FALLOC_FL_ZERO_RANGE)))
5371 RETURN(-EOPNOTSUPP);
5374 * mode == 0 (which is standard prealloc) and PUNCH is supported
5375 * Rest of mode options are not supported yet.
5377 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
5378 RETURN(-EOPNOTSUPP);
5380 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FALLOCATE, 1);
5382 rc = cl_falloc(inode, mode, offset, len);
5384 * ENOTSUPP (524) is an NFSv3 specific error code erroneously
5385 * used by Lustre in several places. Retuning it here would
5386 * confuse applications that explicity test for EOPNOTSUPP
5387 * (95) and fall back to ftruncate().
5389 if (rc == -ENOTSUPP)
5395 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5396 __u64 start, __u64 len)
5400 struct fiemap *fiemap;
5401 unsigned int extent_count = fieinfo->fi_extents_max;
5403 num_bytes = sizeof(*fiemap) + (extent_count *
5404 sizeof(struct fiemap_extent));
5405 OBD_ALLOC_LARGE(fiemap, num_bytes);
5410 fiemap->fm_flags = fieinfo->fi_flags;
5411 fiemap->fm_extent_count = fieinfo->fi_extents_max;
5412 fiemap->fm_start = start;
5413 fiemap->fm_length = len;
5414 if (extent_count > 0 &&
5415 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
5416 sizeof(struct fiemap_extent)) != 0)
5417 GOTO(out, rc = -EFAULT);
5419 rc = ll_do_fiemap(inode, fiemap, num_bytes);
5421 if (IS_ENCRYPTED(inode)) {
5424 for (i = 0; i < fiemap->fm_mapped_extents; i++)
5425 fiemap->fm_extents[i].fe_flags |=
5426 FIEMAP_EXTENT_DATA_ENCRYPTED |
5427 FIEMAP_EXTENT_ENCODED;
5430 fieinfo->fi_flags = fiemap->fm_flags;
5431 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
5432 if (extent_count > 0 &&
5433 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
5434 fiemap->fm_mapped_extents *
5435 sizeof(struct fiemap_extent)) != 0)
5436 GOTO(out, rc = -EFAULT);
5438 OBD_FREE_LARGE(fiemap, num_bytes);
5442 int ll_inode_permission(struct inode *inode, int mask)
5445 struct ll_sb_info *sbi;
5446 struct root_squash_info *squash;
5447 struct cred *cred = NULL;
5448 const struct cred *old_cred = NULL;
5449 bool squash_id = false;
5450 ktime_t kstart = ktime_get();
5454 if (mask & MAY_NOT_BLOCK)
5458 * as root inode are NOT getting validated in lookup operation,
5459 * need to do it before permission check.
5462 if (is_root_inode(inode)) {
5463 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
5468 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
5469 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
5471 /* squash fsuid/fsgid if needed */
5472 sbi = ll_i2sbi(inode);
5473 squash = &sbi->ll_squash;
5474 if (unlikely(squash->rsi_uid != 0 &&
5475 uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
5476 !(sbi->ll_flags & LL_SBI_NOROOTSQUASH))) {
5480 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
5481 __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
5482 squash->rsi_uid, squash->rsi_gid);
5484 /* update current process's credentials
5485 * and FS capability */
5486 cred = prepare_creds();
5490 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
5491 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
5492 cred->cap_effective = cap_drop_nfsd_set(cred->cap_effective);
5493 cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
5495 old_cred = override_creds(cred);
5498 rc = generic_permission(inode, mask);
5499 /* restore current process's credentials and FS capability */
5501 revert_creds(old_cred);
5506 ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM,
5507 ktime_us_delta(ktime_get(), kstart));
5512 /* -o localflock - only provides locally consistent flock locks */
5513 static const struct file_operations ll_file_operations = {
5514 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5515 # ifdef HAVE_SYNC_READ_WRITE
5516 .read = new_sync_read,
5517 .write = new_sync_write,
5519 .read_iter = ll_file_read_iter,
5520 .write_iter = ll_file_write_iter,
5521 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5522 .read = ll_file_read,
5523 .aio_read = ll_file_aio_read,
5524 .write = ll_file_write,
5525 .aio_write = ll_file_aio_write,
5526 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5527 .unlocked_ioctl = ll_file_ioctl,
5528 .open = ll_file_open,
5529 .release = ll_file_release,
5530 .mmap = ll_file_mmap,
5531 .llseek = ll_file_seek,
5532 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5533 .splice_read = generic_file_splice_read,
5535 .splice_read = pcc_file_splice_read,
5539 .fallocate = ll_fallocate,
5542 static const struct file_operations ll_file_operations_flock = {
5543 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5544 # ifdef HAVE_SYNC_READ_WRITE
5545 .read = new_sync_read,
5546 .write = new_sync_write,
5547 # endif /* HAVE_SYNC_READ_WRITE */
5548 .read_iter = ll_file_read_iter,
5549 .write_iter = ll_file_write_iter,
5550 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5551 .read = ll_file_read,
5552 .aio_read = ll_file_aio_read,
5553 .write = ll_file_write,
5554 .aio_write = ll_file_aio_write,
5555 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5556 .unlocked_ioctl = ll_file_ioctl,
5557 .open = ll_file_open,
5558 .release = ll_file_release,
5559 .mmap = ll_file_mmap,
5560 .llseek = ll_file_seek,
5561 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5562 .splice_read = generic_file_splice_read,
5564 .splice_read = pcc_file_splice_read,
5568 .flock = ll_file_flock,
5569 .lock = ll_file_flock,
5570 .fallocate = ll_fallocate,
5573 /* These are for -o noflock - to return ENOSYS on flock calls */
5574 static const struct file_operations ll_file_operations_noflock = {
5575 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5576 # ifdef HAVE_SYNC_READ_WRITE
5577 .read = new_sync_read,
5578 .write = new_sync_write,
5579 # endif /* HAVE_SYNC_READ_WRITE */
5580 .read_iter = ll_file_read_iter,
5581 .write_iter = ll_file_write_iter,
5582 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5583 .read = ll_file_read,
5584 .aio_read = ll_file_aio_read,
5585 .write = ll_file_write,
5586 .aio_write = ll_file_aio_write,
5587 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5588 .unlocked_ioctl = ll_file_ioctl,
5589 .open = ll_file_open,
5590 .release = ll_file_release,
5591 .mmap = ll_file_mmap,
5592 .llseek = ll_file_seek,
5593 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5594 .splice_read = generic_file_splice_read,
5596 .splice_read = pcc_file_splice_read,
5600 .flock = ll_file_noflock,
5601 .lock = ll_file_noflock,
5602 .fallocate = ll_fallocate,
5605 const struct inode_operations ll_file_inode_operations = {
5606 .setattr = ll_setattr,
5607 .getattr = ll_getattr,
5608 .permission = ll_inode_permission,
5609 #ifdef HAVE_IOP_XATTR
5610 .setxattr = ll_setxattr,
5611 .getxattr = ll_getxattr,
5612 .removexattr = ll_removexattr,
5614 .listxattr = ll_listxattr,
5615 .fiemap = ll_fiemap,
5616 .get_acl = ll_get_acl,
5617 #ifdef HAVE_IOP_SET_ACL
5618 .set_acl = ll_set_acl,
5622 const struct file_operations *ll_select_file_operations(struct ll_sb_info *sbi)
5624 const struct file_operations *fops = &ll_file_operations_noflock;
5626 if (sbi->ll_flags & LL_SBI_FLOCK)
5627 fops = &ll_file_operations_flock;
5628 else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
5629 fops = &ll_file_operations;
5634 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
5636 struct ll_inode_info *lli = ll_i2info(inode);
5637 struct cl_object *obj = lli->lli_clob;
5646 env = cl_env_get(&refcheck);
5648 RETURN(PTR_ERR(env));
5650 rc = cl_conf_set(env, lli->lli_clob, conf);
5654 if (conf->coc_opc == OBJECT_CONF_SET) {
5655 struct ldlm_lock *lock = conf->coc_lock;
5656 struct cl_layout cl = {
5660 LASSERT(lock != NULL);
5661 LASSERT(ldlm_has_layout(lock));
5663 /* it can only be allowed to match after layout is
5664 * applied to inode otherwise false layout would be
5665 * seen. Applying layout shoud happen before dropping
5666 * the intent lock. */
5667 ldlm_lock_allow_match(lock);
5669 rc = cl_object_layout_get(env, obj, &cl);
5674 DFID": layout version change: %u -> %u\n",
5675 PFID(&lli->lli_fid), ll_layout_version_get(lli),
5677 ll_layout_version_set(lli, cl.cl_layout_gen);
5681 cl_env_put(env, &refcheck);
5683 RETURN(rc < 0 ? rc : 0);
5686 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
5687 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
5690 struct ll_sb_info *sbi = ll_i2sbi(inode);
5691 struct ptlrpc_request *req;
5698 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
5699 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
5700 lock->l_lvb_data, lock->l_lvb_len);
5702 if (lock->l_lvb_data != NULL)
5705 /* if layout lock was granted right away, the layout is returned
5706 * within DLM_LVB of dlm reply; otherwise if the lock was ever
5707 * blocked and then granted via completion ast, we have to fetch
5708 * layout here. Please note that we can't use the LVB buffer in
5709 * completion AST because it doesn't have a large enough buffer */
5710 rc = ll_get_default_mdsize(sbi, &lmmsize);
5714 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), OBD_MD_FLXATTR,
5715 XATTR_NAME_LOV, lmmsize, &req);
5718 GOTO(out, rc = 0); /* empty layout */
5725 if (lmmsize == 0) /* empty layout */
5728 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
5730 GOTO(out, rc = -EFAULT);
5732 OBD_ALLOC_LARGE(lvbdata, lmmsize);
5733 if (lvbdata == NULL)
5734 GOTO(out, rc = -ENOMEM);
5736 memcpy(lvbdata, lmm, lmmsize);
5737 lock_res_and_lock(lock);
5738 if (unlikely(lock->l_lvb_data == NULL)) {
5739 lock->l_lvb_type = LVB_T_LAYOUT;
5740 lock->l_lvb_data = lvbdata;
5741 lock->l_lvb_len = lmmsize;
5744 unlock_res_and_lock(lock);
5747 OBD_FREE_LARGE(lvbdata, lmmsize);
5752 ptlrpc_req_finished(req);
5757 * Apply the layout to the inode. Layout lock is held and will be released
5760 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
5761 struct inode *inode)
5763 struct ll_inode_info *lli = ll_i2info(inode);
5764 struct ll_sb_info *sbi = ll_i2sbi(inode);
5765 struct ldlm_lock *lock;
5766 struct cl_object_conf conf;
5769 bool wait_layout = false;
5772 LASSERT(lustre_handle_is_used(lockh));
5774 lock = ldlm_handle2lock(lockh);
5775 LASSERT(lock != NULL);
5777 if (!ldlm_has_layout(lock))
5778 GOTO(out, rc = -EAGAIN);
5780 LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
5781 PFID(&lli->lli_fid), inode);
5783 /* in case this is a caching lock and reinstate with new inode */
5784 md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
5786 lock_res_and_lock(lock);
5787 lvb_ready = ldlm_is_lvb_ready(lock);
5788 unlock_res_and_lock(lock);
5790 /* checking lvb_ready is racy but this is okay. The worst case is
5791 * that multi processes may configure the file on the same time. */
5795 rc = ll_layout_fetch(inode, lock);
5799 /* for layout lock, lmm is stored in lock's lvb.
5800 * lvb_data is immutable if the lock is held so it's safe to access it
5803 * set layout to file. Unlikely this will fail as old layout was
5804 * surely eliminated */
5805 memset(&conf, 0, sizeof conf);
5806 conf.coc_opc = OBJECT_CONF_SET;
5807 conf.coc_inode = inode;
5808 conf.coc_lock = lock;
5809 conf.u.coc_layout.lb_buf = lock->l_lvb_data;
5810 conf.u.coc_layout.lb_len = lock->l_lvb_len;
5811 rc = ll_layout_conf(inode, &conf);
5813 /* refresh layout failed, need to wait */
5814 wait_layout = rc == -EBUSY;
5817 LDLM_LOCK_PUT(lock);
5818 ldlm_lock_decref(lockh, mode);
5820 /* wait for IO to complete if it's still being used. */
5822 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
5823 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5825 memset(&conf, 0, sizeof conf);
5826 conf.coc_opc = OBJECT_CONF_WAIT;
5827 conf.coc_inode = inode;
5828 rc = ll_layout_conf(inode, &conf);
5832 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
5833 sbi->ll_fsname, PFID(&lli->lli_fid), rc);
5839 * Issue layout intent RPC to MDS.
5840 * \param inode [in] file inode
5841 * \param intent [in] layout intent
5843 * \retval 0 on success
5844 * \retval < 0 error code
5846 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
5848 struct ll_inode_info *lli = ll_i2info(inode);
5849 struct ll_sb_info *sbi = ll_i2sbi(inode);
5850 struct md_op_data *op_data;
5851 struct lookup_intent it;
5852 struct ptlrpc_request *req;
5856 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
5857 0, 0, LUSTRE_OPC_ANY, NULL);
5858 if (IS_ERR(op_data))
5859 RETURN(PTR_ERR(op_data));
5861 op_data->op_data = intent;
5862 op_data->op_data_size = sizeof(*intent);
5864 memset(&it, 0, sizeof(it));
5865 it.it_op = IT_LAYOUT;
5866 if (intent->li_opc == LAYOUT_INTENT_WRITE ||
5867 intent->li_opc == LAYOUT_INTENT_TRUNC)
5868 it.it_flags = FMODE_WRITE;
5870 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
5871 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5873 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
5874 &ll_md_blocking_ast, 0);
5875 if (it.it_request != NULL)
5876 ptlrpc_req_finished(it.it_request);
5877 it.it_request = NULL;
5879 ll_finish_md_op_data(op_data);
5881 /* set lock data in case this is a new lock */
5883 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
5885 ll_intent_drop_lock(&it);
5891 * This function checks if there exists a LAYOUT lock on the client side,
5892 * or enqueues it if it doesn't have one in cache.
5894 * This function will not hold layout lock so it may be revoked any time after
5895 * this function returns. Any operations depend on layout should be redone
5898 * This function should be called before lov_io_init() to get an uptodate
5899 * layout version, the caller should save the version number and after IO
5900 * is finished, this function should be called again to verify that layout
5901 * is not changed during IO time.
5903 int ll_layout_refresh(struct inode *inode, __u32 *gen)
5905 struct ll_inode_info *lli = ll_i2info(inode);
5906 struct ll_sb_info *sbi = ll_i2sbi(inode);
5907 struct lustre_handle lockh;
5908 struct layout_intent intent = {
5909 .li_opc = LAYOUT_INTENT_ACCESS,
5911 enum ldlm_mode mode;
5915 *gen = ll_layout_version_get(lli);
5916 if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != CL_LAYOUT_GEN_NONE)
5920 LASSERT(fid_is_sane(ll_inode2fid(inode)));
5921 LASSERT(S_ISREG(inode->i_mode));
5923 /* take layout lock mutex to enqueue layout lock exclusively. */
5924 mutex_lock(&lli->lli_layout_mutex);
5927 /* mostly layout lock is caching on the local side, so try to
5928 * match it before grabbing layout lock mutex. */
5929 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
5930 LCK_CR | LCK_CW | LCK_PR |
5932 if (mode != 0) { /* hit cached lock */
5933 rc = ll_layout_lock_set(&lockh, mode, inode);
5939 rc = ll_layout_intent(inode, &intent);
5945 *gen = ll_layout_version_get(lli);
5946 mutex_unlock(&lli->lli_layout_mutex);
5952 * Issue layout intent RPC indicating where in a file an IO is about to write.
5954 * \param[in] inode file inode.
5955 * \param[in] ext write range with start offset of fille in bytes where
5956 * an IO is about to write, and exclusive end offset in
5959 * \retval 0 on success
5960 * \retval < 0 error code
5962 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
5963 struct lu_extent *ext)
5965 struct layout_intent intent = {
5967 .li_extent.e_start = ext->e_start,
5968 .li_extent.e_end = ext->e_end,
5973 rc = ll_layout_intent(inode, &intent);
5979 * This function send a restore request to the MDT
5981 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
5983 struct hsm_user_request *hur;
5987 len = sizeof(struct hsm_user_request) +
5988 sizeof(struct hsm_user_item);
5989 OBD_ALLOC(hur, len);
5993 hur->hur_request.hr_action = HUA_RESTORE;
5994 hur->hur_request.hr_archive_id = 0;
5995 hur->hur_request.hr_flags = 0;
5996 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
5997 sizeof(hur->hur_user_item[0].hui_fid));
5998 hur->hur_user_item[0].hui_extent.offset = offset;
5999 hur->hur_user_item[0].hui_extent.length = length;
6000 hur->hur_request.hr_itemcount = 1;
6001 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,