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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Peter Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_LLITE
40 #include <lustre_dlm.h>
41 #include <linux/pagemap.h>
42 #include <linux/file.h>
43 #include <linux/sched.h>
44 #include <linux/user_namespace.h>
45 #include <linux/uidgid.h>
46 #include <linux/falloc.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(rc ? D_ERROR : D_INODE, "Clean up lease "DFID" %d/%d\n",
328 PFID(&lli->lli_fid), rc, lease_broken);
330 fd->fd_lease_och = NULL;
333 if (fd->fd_och != NULL) {
334 rc = ll_close_inode_openhandle(inode, fd->fd_och, 0, NULL);
339 /* Let's see if we have good enough OPEN lock on the file and if
340 we can skip talking to MDS */
341 mutex_lock(&lli->lli_och_mutex);
342 if (fd->fd_omode & FMODE_WRITE) {
344 LASSERT(lli->lli_open_fd_write_count);
345 lli->lli_open_fd_write_count--;
346 } else if (fd->fd_omode & FMODE_EXEC) {
348 LASSERT(lli->lli_open_fd_exec_count);
349 lli->lli_open_fd_exec_count--;
352 LASSERT(lli->lli_open_fd_read_count);
353 lli->lli_open_fd_read_count--;
355 mutex_unlock(&lli->lli_och_mutex);
357 /* LU-4398: do not cache write open lock if the file has exec bit */
358 if ((lockmode == LCK_CW && inode->i_mode & S_IXUGO) ||
359 !md_lock_match(ll_i2mdexp(inode), flags, ll_inode2fid(inode),
360 LDLM_IBITS, &policy, lockmode, &lockh))
361 rc = ll_md_real_close(inode, fd->fd_omode);
364 file->private_data = NULL;
365 ll_file_data_put(fd);
370 /* While this returns an error code, fput() the caller does not, so we need
371 * to make every effort to clean up all of our state here. Also, applications
372 * rarely check close errors and even if an error is returned they will not
373 * re-try the close call.
375 int ll_file_release(struct inode *inode, struct file *file)
377 struct ll_file_data *fd;
378 struct ll_sb_info *sbi = ll_i2sbi(inode);
379 struct ll_inode_info *lli = ll_i2info(inode);
380 ktime_t kstart = ktime_get();
385 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p)\n",
386 PFID(ll_inode2fid(inode)), inode);
388 fd = file->private_data;
391 /* The last ref on @file, maybe not the the owner pid of statahead,
392 * because parent and child process can share the same file handle. */
393 if (S_ISDIR(inode->i_mode) && lli->lli_opendir_key == fd)
394 ll_deauthorize_statahead(inode, fd);
396 if (is_root_inode(inode)) {
397 file->private_data = NULL;
398 ll_file_data_put(fd);
402 pcc_file_release(inode, file);
404 if (!S_ISDIR(inode->i_mode)) {
405 if (lli->lli_clob != NULL)
406 lov_read_and_clear_async_rc(lli->lli_clob);
407 lli->lli_async_rc = 0;
410 rc = ll_md_close(inode, file);
412 if (CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_DUMP_LOG, cfs_fail_val))
413 libcfs_debug_dumplog();
416 if (!rc && !is_root_inode(inode))
417 ll_stats_ops_tally(sbi, LPROC_LL_RELEASE,
418 ktime_us_delta(ktime_get(), kstart));
422 static inline int ll_dom_readpage(void *data, struct page *page)
424 struct niobuf_local *lnb = data;
428 struct inode *inode = page2inode(page);
430 kaddr = kmap_atomic(page);
431 memcpy(kaddr, lnb->lnb_data, lnb->lnb_len);
432 if (lnb->lnb_len < PAGE_SIZE)
433 memset(kaddr + lnb->lnb_len, 0,
434 PAGE_SIZE - lnb->lnb_len);
435 flush_dcache_page(page);
436 SetPageUptodate(page);
437 kunmap_atomic(kaddr);
439 if (inode && IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
440 if (!llcrypt_has_encryption_key(inode))
441 CDEBUG(D_SEC, "no enc key for "DFID"\n",
442 PFID(ll_inode2fid(inode)));
444 unsigned int offs = 0;
446 while (offs < PAGE_SIZE) {
447 /* decrypt only if page is not empty */
448 if (memcmp(page_address(page) + offs,
449 page_address(ZERO_PAGE(0)),
450 LUSTRE_ENCRYPTION_UNIT_SIZE) == 0)
453 rc = llcrypt_decrypt_pagecache_blocks(page,
454 LUSTRE_ENCRYPTION_UNIT_SIZE,
459 offs += LUSTRE_ENCRYPTION_UNIT_SIZE;
468 void ll_dom_finish_open(struct inode *inode, struct ptlrpc_request *req)
472 struct ll_inode_info *lli = ll_i2info(inode);
473 struct cl_object *obj = lli->lli_clob;
474 struct address_space *mapping = inode->i_mapping;
476 struct niobuf_remote *rnb;
477 struct mdt_body *body;
479 unsigned long index, start;
480 struct niobuf_local lnb;
489 if (!req_capsule_field_present(&req->rq_pill, &RMF_NIOBUF_INLINE,
493 rnb = req_capsule_server_get(&req->rq_pill, &RMF_NIOBUF_INLINE);
494 if (rnb == NULL || rnb->rnb_len == 0)
497 /* LU-11595: Server may return whole file and that is OK always or
498 * it may return just file tail and its offset must be aligned with
499 * client PAGE_SIZE to be used on that client, if server's PAGE_SIZE is
500 * smaller then offset may be not aligned and that data is just ignored.
502 if (rnb->rnb_offset & ~PAGE_MASK)
505 /* Server returns whole file or just file tail if it fills in reply
506 * buffer, in both cases total size should be equal to the file size.
508 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
509 if (rnb->rnb_offset + rnb->rnb_len != body->mbo_dom_size &&
510 !(inode && IS_ENCRYPTED(inode))) {
511 CERROR("%s: server returns off/len %llu/%u but size %llu\n",
512 ll_i2sbi(inode)->ll_fsname, rnb->rnb_offset,
513 rnb->rnb_len, body->mbo_dom_size);
517 env = cl_env_get(&refcheck);
520 io = vvp_env_thread_io(env);
522 io->ci_ignore_layout = 1;
523 rc = cl_io_init(env, io, CIT_MISC, obj);
527 CDEBUG(D_INFO, "Get data along with open at %llu len %i, size %llu\n",
528 rnb->rnb_offset, rnb->rnb_len, body->mbo_dom_size);
530 data = (char *)rnb + sizeof(*rnb);
532 lnb.lnb_file_offset = rnb->rnb_offset;
533 start = lnb.lnb_file_offset >> PAGE_SHIFT;
535 LASSERT((lnb.lnb_file_offset & ~PAGE_MASK) == 0);
536 lnb.lnb_page_offset = 0;
538 struct cl_page *page;
540 lnb.lnb_data = data + (index << PAGE_SHIFT);
541 lnb.lnb_len = rnb->rnb_len - (index << PAGE_SHIFT);
542 if (lnb.lnb_len > PAGE_SIZE)
543 lnb.lnb_len = PAGE_SIZE;
545 vmpage = read_cache_page(mapping, index + start,
546 ll_dom_readpage, &lnb);
547 if (IS_ERR(vmpage)) {
548 CWARN("%s: cannot fill page %lu for "DFID
549 " with data: rc = %li\n",
550 ll_i2sbi(inode)->ll_fsname, index + start,
551 PFID(lu_object_fid(&obj->co_lu)),
556 if (vmpage->mapping == NULL) {
559 /* page was truncated */
562 /* attach VM page to CL page cache */
563 page = cl_page_find(env, obj, vmpage->index, vmpage,
566 ClearPageUptodate(vmpage);
571 cl_page_export(env, page, 1);
572 cl_page_put(env, page);
576 } while (rnb->rnb_len > (index << PAGE_SHIFT));
580 cl_env_put(env, &refcheck);
585 static int ll_intent_file_open(struct dentry *de, void *lmm, int lmmsize,
586 struct lookup_intent *itp)
588 struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
589 struct dentry *parent = de->d_parent;
592 struct md_op_data *op_data;
593 struct ptlrpc_request *req = NULL;
597 LASSERT(parent != NULL);
598 LASSERT(itp->it_flags & MDS_OPEN_BY_FID);
600 /* if server supports open-by-fid, or file name is invalid, don't pack
601 * name in open request */
602 if (OBD_FAIL_CHECK(OBD_FAIL_LLITE_OPEN_BY_NAME) ||
603 !(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_OPEN_BY_FID)) {
605 len = de->d_name.len;
606 name = kmalloc(len + 1, GFP_NOFS);
611 spin_lock(&de->d_lock);
612 if (len != de->d_name.len) {
613 spin_unlock(&de->d_lock);
617 memcpy(name, de->d_name.name, len);
619 spin_unlock(&de->d_lock);
621 if (!lu_name_is_valid_2(name, len)) {
627 op_data = ll_prep_md_op_data(NULL, parent->d_inode, de->d_inode,
628 name, len, 0, LUSTRE_OPC_ANY, NULL);
629 if (IS_ERR(op_data)) {
631 RETURN(PTR_ERR(op_data));
633 op_data->op_data = lmm;
634 op_data->op_data_size = lmmsize;
636 rc = md_intent_lock(sbi->ll_md_exp, op_data, itp, &req,
637 &ll_md_blocking_ast, 0);
639 ll_finish_md_op_data(op_data);
641 /* reason for keep own exit path - don`t flood log
642 * with messages with -ESTALE errors.
644 if (!it_disposition(itp, DISP_OPEN_OPEN) ||
645 it_open_error(DISP_OPEN_OPEN, itp))
647 ll_release_openhandle(de, itp);
651 if (it_disposition(itp, DISP_LOOKUP_NEG))
652 GOTO(out, rc = -ENOENT);
654 if (rc != 0 || it_open_error(DISP_OPEN_OPEN, itp)) {
655 rc = rc ? rc : it_open_error(DISP_OPEN_OPEN, itp);
656 CDEBUG(D_VFSTRACE, "lock enqueue: err: %d\n", rc);
660 rc = ll_prep_inode(&de->d_inode, req, NULL, itp);
662 if (!rc && itp->it_lock_mode) {
665 /* If we got a lock back and it has a LOOKUP bit set,
666 * make sure the dentry is marked as valid so we can find it.
667 * We don't need to care about actual hashing since other bits
668 * of kernel will deal with that later.
670 ll_set_lock_data(sbi->ll_md_exp, de->d_inode, itp, &bits);
671 if (bits & MDS_INODELOCK_LOOKUP)
672 d_lustre_revalidate(de);
673 /* if DoM bit returned along with LAYOUT bit then there
674 * can be read-on-open data returned.
676 if (bits & MDS_INODELOCK_DOM && bits & MDS_INODELOCK_LAYOUT)
677 ll_dom_finish_open(de->d_inode, req);
681 ptlrpc_req_finished(req);
682 ll_intent_drop_lock(itp);
684 /* We did open by fid, but by the time we got to the server,
685 * the object disappeared. If this is a create, we cannot really
686 * tell the userspace that the file it was trying to create
687 * does not exist. Instead let's return -ESTALE, and the VFS will
688 * retry the create with LOOKUP_REVAL that we are going to catch
689 * in ll_revalidate_dentry() and use lookup then.
691 if (rc == -ENOENT && itp->it_op & IT_CREAT)
697 static int ll_och_fill(struct obd_export *md_exp, struct lookup_intent *it,
698 struct obd_client_handle *och)
700 struct mdt_body *body;
702 body = req_capsule_server_get(&it->it_request->rq_pill, &RMF_MDT_BODY);
703 och->och_open_handle = body->mbo_open_handle;
704 och->och_fid = body->mbo_fid1;
705 och->och_lease_handle.cookie = it->it_lock_handle;
706 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
707 och->och_flags = it->it_flags;
709 return md_set_open_replay_data(md_exp, och, it);
712 static int ll_local_open(struct file *file, struct lookup_intent *it,
713 struct ll_file_data *fd, struct obd_client_handle *och)
715 struct inode *inode = file_inode(file);
718 LASSERT(!file->private_data);
725 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
730 file->private_data = fd;
731 ll_readahead_init(inode, &fd->fd_ras);
732 fd->fd_omode = it->it_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
733 /* turn off the kernel's read-ahead */
734 file->f_ra.ra_pages = 0;
736 /* ll_cl_context initialize */
737 rwlock_init(&fd->fd_lock);
738 INIT_LIST_HEAD(&fd->fd_lccs);
743 /* Open a file, and (for the very first open) create objects on the OSTs at
744 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
745 * creation or open until ll_lov_setstripe() ioctl is called.
747 * If we already have the stripe MD locally then we don't request it in
748 * md_open(), by passing a lmm_size = 0.
750 * It is up to the application to ensure no other processes open this file
751 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
752 * used. We might be able to avoid races of that sort by getting lli_open_sem
753 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
754 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
756 int ll_file_open(struct inode *inode, struct file *file)
758 struct ll_inode_info *lli = ll_i2info(inode);
759 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
760 .it_flags = file->f_flags };
761 struct obd_client_handle **och_p = NULL;
762 __u64 *och_usecount = NULL;
763 struct ll_file_data *fd;
764 ktime_t kstart = ktime_get();
768 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), flags %o\n",
769 PFID(ll_inode2fid(inode)), inode, file->f_flags);
771 it = file->private_data; /* XXX: compat macro */
772 file->private_data = NULL; /* prevent ll_local_open assertion */
774 if (S_ISREG(inode->i_mode)) {
775 rc = llcrypt_file_open(inode, file);
777 GOTO(out_nofiledata, rc);
780 fd = ll_file_data_get();
782 GOTO(out_nofiledata, rc = -ENOMEM);
785 if (S_ISDIR(inode->i_mode))
786 ll_authorize_statahead(inode, fd);
788 if (is_root_inode(inode)) {
789 file->private_data = fd;
793 if (!it || !it->it_disposition) {
794 /* Convert f_flags into access mode. We cannot use file->f_mode,
795 * because everything but O_ACCMODE mask was stripped from
797 if ((oit.it_flags + 1) & O_ACCMODE)
799 if (file->f_flags & O_TRUNC)
800 oit.it_flags |= FMODE_WRITE;
802 /* kernel only call f_op->open in dentry_open. filp_open calls
803 * dentry_open after call to open_namei that checks permissions.
804 * Only nfsd_open call dentry_open directly without checking
805 * permissions and because of that this code below is safe.
807 if (oit.it_flags & (FMODE_WRITE | FMODE_READ))
808 oit.it_flags |= MDS_OPEN_OWNEROVERRIDE;
810 /* We do not want O_EXCL here, presumably we opened the file
811 * already? XXX - NFS implications? */
812 oit.it_flags &= ~O_EXCL;
814 /* bug20584, if "it_flags" contains O_CREAT, the file will be
815 * created if necessary, then "IT_CREAT" should be set to keep
816 * consistent with it */
817 if (oit.it_flags & O_CREAT)
818 oit.it_op |= IT_CREAT;
824 /* Let's see if we have file open on MDS already. */
825 if (it->it_flags & FMODE_WRITE) {
826 och_p = &lli->lli_mds_write_och;
827 och_usecount = &lli->lli_open_fd_write_count;
828 } else if (it->it_flags & FMODE_EXEC) {
829 och_p = &lli->lli_mds_exec_och;
830 och_usecount = &lli->lli_open_fd_exec_count;
832 och_p = &lli->lli_mds_read_och;
833 och_usecount = &lli->lli_open_fd_read_count;
836 mutex_lock(&lli->lli_och_mutex);
837 if (*och_p) { /* Open handle is present */
838 if (it_disposition(it, DISP_OPEN_OPEN)) {
839 /* Well, there's extra open request that we do not need,
840 * let's close it somehow. This will decref request. */
841 rc = it_open_error(DISP_OPEN_OPEN, it);
843 mutex_unlock(&lli->lli_och_mutex);
844 GOTO(out_openerr, rc);
847 ll_release_openhandle(file_dentry(file), it);
851 rc = ll_local_open(file, it, fd, NULL);
854 mutex_unlock(&lli->lli_och_mutex);
855 GOTO(out_openerr, rc);
858 LASSERT(*och_usecount == 0);
859 if (!it->it_disposition) {
860 struct dentry *dentry = file_dentry(file);
861 struct ll_dentry_data *ldd;
863 /* We cannot just request lock handle now, new ELC code
864 * means that one of other OPEN locks for this file
865 * could be cancelled, and since blocking ast handler
866 * would attempt to grab och_mutex as well, that would
867 * result in a deadlock
869 mutex_unlock(&lli->lli_och_mutex);
871 * Normally called under two situations:
873 * 2. A race/condition on MDS resulting in no open
874 * handle to be returned from LOOKUP|OPEN request,
875 * for example if the target entry was a symlink.
877 * Only fetch MDS_OPEN_LOCK if this is in NFS path,
878 * marked by a bit set in ll_iget_for_nfs. Clear the
879 * bit so that it's not confusing later callers.
881 * NB; when ldd is NULL, it must have come via normal
882 * lookup path only, since ll_iget_for_nfs always calls
885 ldd = ll_d2d(dentry);
886 if (ldd && ldd->lld_nfs_dentry) {
887 ldd->lld_nfs_dentry = 0;
888 if (!filename_is_volatile(dentry->d_name.name,
891 it->it_flags |= MDS_OPEN_LOCK;
895 * Always specify MDS_OPEN_BY_FID because we don't want
896 * to get file with different fid.
898 it->it_flags |= MDS_OPEN_BY_FID;
899 rc = ll_intent_file_open(dentry, NULL, 0, it);
901 GOTO(out_openerr, rc);
905 OBD_ALLOC(*och_p, sizeof(struct obd_client_handle));
907 GOTO(out_och_free, rc = -ENOMEM);
911 /* md_intent_lock() didn't get a request ref if there was an
912 * open error, so don't do cleanup on the request here
914 /* XXX (green): Should not we bail out on any error here, not
915 * just open error? */
916 rc = it_open_error(DISP_OPEN_OPEN, it);
918 GOTO(out_och_free, rc);
920 LASSERTF(it_disposition(it, DISP_ENQ_OPEN_REF),
921 "inode %p: disposition %x, status %d\n", inode,
922 it_disposition(it, ~0), it->it_status);
924 rc = ll_local_open(file, it, fd, *och_p);
926 GOTO(out_och_free, rc);
929 rc = pcc_file_open(inode, file);
931 GOTO(out_och_free, rc);
933 mutex_unlock(&lli->lli_och_mutex);
937 /* Must do this outside lli_och_mutex lock to prevent deadlock where
938 different kind of OPEN lock for this same inode gets cancelled
939 by ldlm_cancel_lru */
940 if (!S_ISREG(inode->i_mode))
941 GOTO(out_och_free, rc);
942 cl_lov_delay_create_clear(&file->f_flags);
943 GOTO(out_och_free, rc);
947 if (och_p && *och_p) {
948 OBD_FREE(*och_p, sizeof(struct obd_client_handle));
949 *och_p = NULL; /* OBD_FREE writes some magic there */
952 mutex_unlock(&lli->lli_och_mutex);
955 if (lli->lli_opendir_key == fd)
956 ll_deauthorize_statahead(inode, fd);
959 ll_file_data_put(fd);
961 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_OPEN,
962 ktime_us_delta(ktime_get(), kstart));
966 if (it && it_disposition(it, DISP_ENQ_OPEN_REF)) {
967 ptlrpc_req_finished(it->it_request);
968 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
974 static int ll_md_blocking_lease_ast(struct ldlm_lock *lock,
975 struct ldlm_lock_desc *desc, void *data, int flag)
978 struct lustre_handle lockh;
982 case LDLM_CB_BLOCKING:
983 ldlm_lock2handle(lock, &lockh);
984 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
986 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
990 case LDLM_CB_CANCELING:
998 * When setting a lease on a file, we take ownership of the lli_mds_*_och
999 * and save it as fd->fd_och so as to force client to reopen the file even
1000 * if it has an open lock in cache already.
1002 static int ll_lease_och_acquire(struct inode *inode, struct file *file,
1003 struct lustre_handle *old_open_handle)
1005 struct ll_inode_info *lli = ll_i2info(inode);
1006 struct ll_file_data *fd = file->private_data;
1007 struct obd_client_handle **och_p;
1008 __u64 *och_usecount;
1012 /* Get the openhandle of the file */
1013 mutex_lock(&lli->lli_och_mutex);
1014 if (fd->fd_lease_och != NULL)
1015 GOTO(out_unlock, rc = -EBUSY);
1017 if (fd->fd_och == NULL) {
1018 if (file->f_mode & FMODE_WRITE) {
1019 LASSERT(lli->lli_mds_write_och != NULL);
1020 och_p = &lli->lli_mds_write_och;
1021 och_usecount = &lli->lli_open_fd_write_count;
1023 LASSERT(lli->lli_mds_read_och != NULL);
1024 och_p = &lli->lli_mds_read_och;
1025 och_usecount = &lli->lli_open_fd_read_count;
1028 if (*och_usecount > 1)
1029 GOTO(out_unlock, rc = -EBUSY);
1031 fd->fd_och = *och_p;
1036 *old_open_handle = fd->fd_och->och_open_handle;
1040 mutex_unlock(&lli->lli_och_mutex);
1045 * Release ownership on lli_mds_*_och when putting back a file lease.
1047 static int ll_lease_och_release(struct inode *inode, struct file *file)
1049 struct ll_inode_info *lli = ll_i2info(inode);
1050 struct ll_file_data *fd = file->private_data;
1051 struct obd_client_handle **och_p;
1052 struct obd_client_handle *old_och = NULL;
1053 __u64 *och_usecount;
1057 mutex_lock(&lli->lli_och_mutex);
1058 if (file->f_mode & FMODE_WRITE) {
1059 och_p = &lli->lli_mds_write_och;
1060 och_usecount = &lli->lli_open_fd_write_count;
1062 och_p = &lli->lli_mds_read_och;
1063 och_usecount = &lli->lli_open_fd_read_count;
1066 /* The file may have been open by another process (broken lease) so
1067 * *och_p is not NULL. In this case we should simply increase usecount
1070 if (*och_p != NULL) {
1071 old_och = fd->fd_och;
1074 *och_p = fd->fd_och;
1078 mutex_unlock(&lli->lli_och_mutex);
1080 if (old_och != NULL)
1081 rc = ll_close_inode_openhandle(inode, old_och, 0, NULL);
1087 * Acquire a lease and open the file.
1089 static struct obd_client_handle *
1090 ll_lease_open(struct inode *inode, struct file *file, fmode_t fmode,
1093 struct lookup_intent it = { .it_op = IT_OPEN };
1094 struct ll_sb_info *sbi = ll_i2sbi(inode);
1095 struct md_op_data *op_data;
1096 struct ptlrpc_request *req = NULL;
1097 struct lustre_handle old_open_handle = { 0 };
1098 struct obd_client_handle *och = NULL;
1103 if (fmode != FMODE_WRITE && fmode != FMODE_READ)
1104 RETURN(ERR_PTR(-EINVAL));
1107 if (!(fmode & file->f_mode) || (file->f_mode & FMODE_EXEC))
1108 RETURN(ERR_PTR(-EPERM));
1110 rc = ll_lease_och_acquire(inode, file, &old_open_handle);
1112 RETURN(ERR_PTR(rc));
1117 RETURN(ERR_PTR(-ENOMEM));
1119 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
1120 LUSTRE_OPC_ANY, NULL);
1121 if (IS_ERR(op_data))
1122 GOTO(out, rc = PTR_ERR(op_data));
1124 /* To tell the MDT this openhandle is from the same owner */
1125 op_data->op_open_handle = old_open_handle;
1127 it.it_flags = fmode | open_flags;
1128 it.it_flags |= MDS_OPEN_LOCK | MDS_OPEN_BY_FID | MDS_OPEN_LEASE;
1129 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
1130 &ll_md_blocking_lease_ast,
1131 /* LDLM_FL_NO_LRU: To not put the lease lock into LRU list, otherwise
1132 * it can be cancelled which may mislead applications that the lease is
1134 * LDLM_FL_EXCL: Set this flag so that it won't be matched by normal
1135 * open in ll_md_blocking_ast(). Otherwise as ll_md_blocking_lease_ast
1136 * doesn't deal with openhandle, so normal openhandle will be leaked. */
1137 LDLM_FL_NO_LRU | LDLM_FL_EXCL);
1138 ll_finish_md_op_data(op_data);
1139 ptlrpc_req_finished(req);
1141 GOTO(out_release_it, rc);
1143 if (it_disposition(&it, DISP_LOOKUP_NEG))
1144 GOTO(out_release_it, rc = -ENOENT);
1146 rc = it_open_error(DISP_OPEN_OPEN, &it);
1148 GOTO(out_release_it, rc);
1150 LASSERT(it_disposition(&it, DISP_ENQ_OPEN_REF));
1151 rc = ll_och_fill(sbi->ll_md_exp, &it, och);
1153 GOTO(out_release_it, rc);
1155 if (!it_disposition(&it, DISP_OPEN_LEASE)) /* old server? */
1156 GOTO(out_close, rc = -EOPNOTSUPP);
1158 /* already get lease, handle lease lock */
1159 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
1160 if (!it.it_lock_mode ||
1161 !(it.it_lock_bits & MDS_INODELOCK_OPEN)) {
1162 /* open lock must return for lease */
1163 CERROR(DFID "lease granted but no open lock, %d/%llu.\n",
1164 PFID(ll_inode2fid(inode)), it.it_lock_mode,
1166 GOTO(out_close, rc = -EPROTO);
1169 ll_intent_release(&it);
1173 /* Cancel open lock */
1174 if (it.it_lock_mode != 0) {
1175 ldlm_lock_decref_and_cancel(&och->och_lease_handle,
1177 it.it_lock_mode = 0;
1178 och->och_lease_handle.cookie = 0ULL;
1180 rc2 = ll_close_inode_openhandle(inode, och, 0, NULL);
1182 CERROR("%s: error closing file "DFID": %d\n",
1183 sbi->ll_fsname, PFID(&ll_i2info(inode)->lli_fid), rc2);
1184 och = NULL; /* och has been freed in ll_close_inode_openhandle() */
1186 ll_intent_release(&it);
1190 RETURN(ERR_PTR(rc));
1194 * Check whether a layout swap can be done between two inodes.
1196 * \param[in] inode1 First inode to check
1197 * \param[in] inode2 Second inode to check
1199 * \retval 0 on success, layout swap can be performed between both inodes
1200 * \retval negative error code if requirements are not met
1202 static int ll_check_swap_layouts_validity(struct inode *inode1,
1203 struct inode *inode2)
1205 if (!S_ISREG(inode1->i_mode) || !S_ISREG(inode2->i_mode))
1208 if (inode_permission(inode1, MAY_WRITE) ||
1209 inode_permission(inode2, MAY_WRITE))
1212 if (inode1->i_sb != inode2->i_sb)
1218 static int ll_swap_layouts_close(struct obd_client_handle *och,
1219 struct inode *inode, struct inode *inode2)
1221 const struct lu_fid *fid1 = ll_inode2fid(inode);
1222 const struct lu_fid *fid2;
1226 CDEBUG(D_INODE, "%s: biased close of file "DFID"\n",
1227 ll_i2sbi(inode)->ll_fsname, PFID(fid1));
1229 rc = ll_check_swap_layouts_validity(inode, inode2);
1231 GOTO(out_free_och, rc);
1233 /* We now know that inode2 is a lustre inode */
1234 fid2 = ll_inode2fid(inode2);
1236 rc = lu_fid_cmp(fid1, fid2);
1238 GOTO(out_free_och, rc = -EINVAL);
1240 /* Close the file and {swap,merge} layouts between inode & inode2.
1241 * NB: lease lock handle is released in mdc_close_layout_swap_pack()
1242 * because we still need it to pack l_remote_handle to MDT. */
1243 rc = ll_close_inode_openhandle(inode, och, MDS_CLOSE_LAYOUT_SWAP,
1246 och = NULL; /* freed in ll_close_inode_openhandle() */
1256 * Release lease and close the file.
1257 * It will check if the lease has ever broken.
1259 static int ll_lease_close_intent(struct obd_client_handle *och,
1260 struct inode *inode,
1261 bool *lease_broken, enum mds_op_bias bias,
1264 struct ldlm_lock *lock;
1265 bool cancelled = true;
1269 lock = ldlm_handle2lock(&och->och_lease_handle);
1271 lock_res_and_lock(lock);
1272 cancelled = ldlm_is_cancel(lock);
1273 unlock_res_and_lock(lock);
1274 LDLM_LOCK_PUT(lock);
1277 CDEBUG(D_INODE, "lease for "DFID" broken? %d, bias: %x\n",
1278 PFID(&ll_i2info(inode)->lli_fid), cancelled, bias);
1280 if (lease_broken != NULL)
1281 *lease_broken = cancelled;
1283 if (!cancelled && !bias)
1284 ldlm_cli_cancel(&och->och_lease_handle, 0);
1286 if (cancelled) { /* no need to excute intent */
1291 rc = ll_close_inode_openhandle(inode, och, bias, data);
1295 static int ll_lease_close(struct obd_client_handle *och, struct inode *inode,
1298 return ll_lease_close_intent(och, inode, lease_broken, 0, NULL);
1302 * After lease is taken, send the RPC MDS_REINT_RESYNC to the MDT
1304 static int ll_lease_file_resync(struct obd_client_handle *och,
1305 struct inode *inode, unsigned long arg)
1307 struct ll_sb_info *sbi = ll_i2sbi(inode);
1308 struct md_op_data *op_data;
1309 struct ll_ioc_lease_id ioc;
1310 __u64 data_version_unused;
1314 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
1315 LUSTRE_OPC_ANY, NULL);
1316 if (IS_ERR(op_data))
1317 RETURN(PTR_ERR(op_data));
1319 if (copy_from_user(&ioc, (struct ll_ioc_lease_id __user *)arg,
1323 /* before starting file resync, it's necessary to clean up page cache
1324 * in client memory, otherwise once the layout version is increased,
1325 * writing back cached data will be denied the OSTs. */
1326 rc = ll_data_version(inode, &data_version_unused, LL_DV_WR_FLUSH);
1330 op_data->op_lease_handle = och->och_lease_handle;
1331 op_data->op_mirror_id = ioc.lil_mirror_id;
1332 rc = md_file_resync(sbi->ll_md_exp, op_data);
1338 ll_finish_md_op_data(op_data);
1342 int ll_merge_attr(const struct lu_env *env, struct inode *inode)
1344 struct ll_inode_info *lli = ll_i2info(inode);
1345 struct cl_object *obj = lli->lli_clob;
1346 struct cl_attr *attr = vvp_env_thread_attr(env);
1354 ll_inode_size_lock(inode);
1356 /* Merge timestamps the most recently obtained from MDS with
1357 * timestamps obtained from OSTs.
1359 * Do not overwrite atime of inode because it may be refreshed
1360 * by file_accessed() function. If the read was served by cache
1361 * data, there is no RPC to be sent so that atime may not be
1362 * transferred to OSTs at all. MDT only updates atime at close time
1363 * if it's at least 'mdd.*.atime_diff' older.
1364 * All in all, the atime in Lustre does not strictly comply with
1365 * POSIX. Solving this problem needs to send an RPC to MDT for each
1366 * read, this will hurt performance.
1368 if (test_and_clear_bit(LLIF_UPDATE_ATIME, &lli->lli_flags) ||
1369 inode->i_atime.tv_sec < lli->lli_atime)
1370 inode->i_atime.tv_sec = lli->lli_atime;
1372 inode->i_mtime.tv_sec = lli->lli_mtime;
1373 inode->i_ctime.tv_sec = lli->lli_ctime;
1375 mtime = inode->i_mtime.tv_sec;
1376 atime = inode->i_atime.tv_sec;
1377 ctime = inode->i_ctime.tv_sec;
1379 cl_object_attr_lock(obj);
1380 if (OBD_FAIL_CHECK(OBD_FAIL_MDC_MERGE))
1383 rc = cl_object_attr_get(env, obj, attr);
1384 cl_object_attr_unlock(obj);
1387 GOTO(out_size_unlock, rc = (rc == -ENODATA ? 0 : rc));
1389 if (atime < attr->cat_atime)
1390 atime = attr->cat_atime;
1392 if (ctime < attr->cat_ctime)
1393 ctime = attr->cat_ctime;
1395 if (mtime < attr->cat_mtime)
1396 mtime = attr->cat_mtime;
1398 CDEBUG(D_VFSTRACE, DFID" updating i_size %llu\n",
1399 PFID(&lli->lli_fid), attr->cat_size);
1401 i_size_write(inode, attr->cat_size);
1402 inode->i_blocks = attr->cat_blocks;
1404 inode->i_mtime.tv_sec = mtime;
1405 inode->i_atime.tv_sec = atime;
1406 inode->i_ctime.tv_sec = ctime;
1409 ll_inode_size_unlock(inode);
1415 * Set designated mirror for I/O.
1417 * So far only read, write, and truncated can support to issue I/O to
1418 * designated mirror.
1420 void ll_io_set_mirror(struct cl_io *io, const struct file *file)
1422 struct ll_file_data *fd = file->private_data;
1424 /* clear layout version for generic(non-resync) I/O in case it carries
1425 * stale layout version due to I/O restart */
1426 io->ci_layout_version = 0;
1428 /* FLR: disable non-delay for designated mirror I/O because obviously
1429 * only one mirror is available */
1430 if (fd->fd_designated_mirror > 0) {
1432 io->ci_designated_mirror = fd->fd_designated_mirror;
1433 io->ci_layout_version = fd->fd_layout_version;
1436 CDEBUG(D_VFSTRACE, "%s: desiginated mirror: %d\n",
1437 file->f_path.dentry->d_name.name, io->ci_designated_mirror);
1440 static bool file_is_noatime(const struct file *file)
1442 const struct vfsmount *mnt = file->f_path.mnt;
1443 const struct inode *inode = file_inode((struct file *)file);
1445 /* Adapted from file_accessed() and touch_atime().*/
1446 if (file->f_flags & O_NOATIME)
1449 if (inode->i_flags & S_NOATIME)
1452 if (IS_NOATIME(inode))
1455 if (mnt->mnt_flags & (MNT_NOATIME | MNT_READONLY))
1458 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1461 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
1467 void ll_io_init(struct cl_io *io, struct file *file, enum cl_io_type iot,
1468 struct vvp_io_args *args)
1470 struct inode *inode = file_inode(file);
1471 struct ll_file_data *fd = file->private_data;
1473 io->u.ci_rw.crw_nonblock = file->f_flags & O_NONBLOCK;
1474 io->ci_lock_no_expand = fd->ll_lock_no_expand;
1476 if (iot == CIT_WRITE) {
1477 io->u.ci_wr.wr_append = !!(file->f_flags & O_APPEND);
1478 io->u.ci_wr.wr_sync = !!(file->f_flags & O_SYNC ||
1479 file->f_flags & O_DIRECT ||
1481 #ifdef HAVE_GENERIC_WRITE_SYNC_2ARGS
1482 io->u.ci_wr.wr_sync |= !!(args &&
1483 (args->u.normal.via_iocb->ki_flags &
1488 io->ci_obj = ll_i2info(inode)->lli_clob;
1489 io->ci_lockreq = CILR_MAYBE;
1490 if (ll_file_nolock(file)) {
1491 io->ci_lockreq = CILR_NEVER;
1492 io->ci_no_srvlock = 1;
1493 } else if (file->f_flags & O_APPEND) {
1494 io->ci_lockreq = CILR_MANDATORY;
1496 io->ci_noatime = file_is_noatime(file);
1497 io->ci_async_readahead = false;
1499 /* FLR: only use non-delay I/O for read as there is only one
1500 * avaliable mirror for write. */
1501 io->ci_ndelay = !(iot == CIT_WRITE);
1503 ll_io_set_mirror(io, file);
1506 static void ll_heat_add(struct inode *inode, enum cl_io_type iot,
1509 struct ll_inode_info *lli = ll_i2info(inode);
1510 struct ll_sb_info *sbi = ll_i2sbi(inode);
1511 enum obd_heat_type sample_type;
1512 enum obd_heat_type iobyte_type;
1513 __u64 now = ktime_get_real_seconds();
1515 if (!ll_sbi_has_file_heat(sbi) ||
1516 lli->lli_heat_flags & LU_HEAT_FLAG_OFF)
1519 if (iot == CIT_READ) {
1520 sample_type = OBD_HEAT_READSAMPLE;
1521 iobyte_type = OBD_HEAT_READBYTE;
1522 } else if (iot == CIT_WRITE) {
1523 sample_type = OBD_HEAT_WRITESAMPLE;
1524 iobyte_type = OBD_HEAT_WRITEBYTE;
1529 spin_lock(&lli->lli_heat_lock);
1530 obd_heat_add(&lli->lli_heat_instances[sample_type], now, 1,
1531 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1532 obd_heat_add(&lli->lli_heat_instances[iobyte_type], now, count,
1533 sbi->ll_heat_decay_weight, sbi->ll_heat_period_second);
1534 spin_unlock(&lli->lli_heat_lock);
1538 ll_file_io_generic(const struct lu_env *env, struct vvp_io_args *args,
1539 struct file *file, enum cl_io_type iot,
1540 loff_t *ppos, size_t count)
1542 struct vvp_io *vio = vvp_env_io(env);
1543 struct inode *inode = file_inode(file);
1544 struct ll_inode_info *lli = ll_i2info(inode);
1545 struct ll_file_data *fd = file->private_data;
1546 struct range_lock range;
1550 unsigned int retried = 0, dio_lock = 0;
1551 bool is_aio = false;
1552 struct cl_dio_aio *ci_aio = NULL;
1556 CDEBUG(D_VFSTRACE, "%s: %s ppos: %llu, count: %zu\n",
1557 file_dentry(file)->d_name.name,
1558 iot == CIT_READ ? "read" : "write", *ppos, count);
1560 io = vvp_env_thread_io(env);
1561 if (file->f_flags & O_DIRECT) {
1562 if (!is_sync_kiocb(args->u.normal.via_iocb))
1564 ci_aio = cl_aio_alloc(args->u.normal.via_iocb);
1566 GOTO(out, rc = -ENOMEM);
1570 io = vvp_env_thread_io(env);
1571 ll_io_init(io, file, iot, args);
1572 io->ci_aio = ci_aio;
1573 io->ci_dio_lock = dio_lock;
1574 io->ci_ndelay_tried = retried;
1576 if (cl_io_rw_init(env, io, iot, *ppos, count) == 0) {
1577 bool range_locked = false;
1579 if (file->f_flags & O_APPEND)
1580 range_lock_init(&range, 0, LUSTRE_EOF);
1582 range_lock_init(&range, *ppos, *ppos + count - 1);
1584 vio->vui_fd = file->private_data;
1585 vio->vui_iter = args->u.normal.via_iter;
1586 vio->vui_iocb = args->u.normal.via_iocb;
1587 /* Direct IO reads must also take range lock,
1588 * or multiple reads will try to work on the same pages
1589 * See LU-6227 for details.
1591 if (((iot == CIT_WRITE) ||
1592 (iot == CIT_READ && (file->f_flags & O_DIRECT))) &&
1593 !(vio->vui_fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1594 CDEBUG(D_VFSTRACE, "Range lock "RL_FMT"\n",
1596 rc = range_lock(&lli->lli_write_tree, &range);
1600 range_locked = true;
1603 ll_cl_add(file, env, io, LCC_RW);
1604 rc = cl_io_loop(env, io);
1605 ll_cl_remove(file, env);
1608 CDEBUG(D_VFSTRACE, "Range unlock "RL_FMT"\n",
1610 range_unlock(&lli->lli_write_tree, &range);
1613 /* cl_io_rw_init() handled IO */
1618 * In order to move forward AIO, ci_nob was increased,
1619 * but that doesn't mean io have been finished, it just
1620 * means io have been submited, we will always return
1621 * EIOCBQUEUED to the caller, So we could only return
1622 * number of bytes in non-AIO case.
1624 if (io->ci_nob > 0) {
1626 result += io->ci_nob;
1627 *ppos = io->u.ci_wr.wr.crw_pos; /* for splice */
1629 count -= io->ci_nob;
1631 /* prepare IO restart */
1633 args->u.normal.via_iter = vio->vui_iter;
1636 cl_io_fini(env, io);
1639 "%s: %d io complete with rc: %d, result: %zd, restart: %d\n",
1640 file->f_path.dentry->d_name.name,
1641 iot, rc, result, io->ci_need_restart);
1643 if ((rc == 0 || rc == -ENODATA || rc == -ENOLCK) &&
1644 count > 0 && io->ci_need_restart) {
1646 "%s: restart %s from %lld, count: %zu, ret: %zd, rc: %d\n",
1647 file_dentry(file)->d_name.name,
1648 iot == CIT_READ ? "read" : "write",
1649 *ppos, count, result, rc);
1650 /* preserve the tried count for FLR */
1651 retried = io->ci_ndelay_tried;
1652 dio_lock = io->ci_dio_lock;
1658 * VFS will call aio_complete() if no -EIOCBQUEUED
1659 * is returned for AIO, so we can not call aio_complete()
1662 if (rc != -EIOCBQUEUED)
1663 io->ci_aio->cda_no_aio_complete = 1;
1665 * Drop one extra reference so that end_io() could be
1666 * called for this IO context, we could call it after
1667 * we make sure all AIO requests have been proceed.
1669 cl_sync_io_note(env, &io->ci_aio->cda_sync,
1670 rc == -EIOCBQUEUED ? 0 : rc);
1672 cl_aio_free(io->ci_aio);
1677 if (iot == CIT_READ) {
1679 ll_stats_ops_tally(ll_i2sbi(inode),
1680 LPROC_LL_READ_BYTES, result);
1681 } else if (iot == CIT_WRITE) {
1683 ll_stats_ops_tally(ll_i2sbi(inode),
1684 LPROC_LL_WRITE_BYTES, result);
1685 fd->fd_write_failed = false;
1686 } else if (result == 0 && rc == 0) {
1689 fd->fd_write_failed = true;
1691 fd->fd_write_failed = false;
1692 } else if (rc != -ERESTARTSYS) {
1693 fd->fd_write_failed = true;
1697 CDEBUG(D_VFSTRACE, "iot: %d, result: %zd\n", iot, result);
1699 ll_heat_add(inode, iot, result);
1701 RETURN(result > 0 ? result : rc);
1705 * The purpose of fast read is to overcome per I/O overhead and improve IOPS
1706 * especially for small I/O.
1708 * To serve a read request, CLIO has to create and initialize a cl_io and
1709 * then request DLM lock. This has turned out to have siginificant overhead
1710 * and affects the performance of small I/O dramatically.
1712 * It's not necessary to create a cl_io for each I/O. Under the help of read
1713 * ahead, most of the pages being read are already in memory cache and we can
1714 * read those pages directly because if the pages exist, the corresponding DLM
1715 * lock must exist so that page content must be valid.
1717 * In fast read implementation, the llite speculatively finds and reads pages
1718 * in memory cache. There are three scenarios for fast read:
1719 * - If the page exists and is uptodate, kernel VM will provide the data and
1720 * CLIO won't be intervened;
1721 * - If the page was brought into memory by read ahead, it will be exported
1722 * and read ahead parameters will be updated;
1723 * - Otherwise the page is not in memory, we can't do fast read. Therefore,
1724 * it will go back and invoke normal read, i.e., a cl_io will be created
1725 * and DLM lock will be requested.
1727 * POSIX compliance: posix standard states that read is intended to be atomic.
1728 * Lustre read implementation is in line with Linux kernel read implementation
1729 * and neither of them complies with POSIX standard in this matter. Fast read
1730 * doesn't make the situation worse on single node but it may interleave write
1731 * results from multiple nodes due to short read handling in ll_file_aio_read().
1733 * \param env - lu_env
1734 * \param iocb - kiocb from kernel
1735 * \param iter - user space buffers where the data will be copied
1737 * \retval - number of bytes have been read, or error code if error occurred.
1740 ll_do_fast_read(struct kiocb *iocb, struct iov_iter *iter)
1744 if (!ll_sbi_has_fast_read(ll_i2sbi(file_inode(iocb->ki_filp))))
1747 /* NB: we can't do direct IO for fast read because it will need a lock
1748 * to make IO engine happy. */
1749 if (iocb->ki_filp->f_flags & O_DIRECT)
1752 result = generic_file_read_iter(iocb, iter);
1754 /* If the first page is not in cache, generic_file_aio_read() will be
1755 * returned with -ENODATA.
1756 * See corresponding code in ll_readpage(). */
1757 if (result == -ENODATA)
1761 ll_heat_add(file_inode(iocb->ki_filp), CIT_READ, result);
1762 ll_stats_ops_tally(ll_i2sbi(file_inode(iocb->ki_filp)),
1763 LPROC_LL_READ_BYTES, result);
1770 * Read from a file (through the page cache).
1772 static ssize_t ll_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1775 struct vvp_io_args *args;
1776 struct file *file = iocb->ki_filp;
1780 ktime_t kstart = ktime_get();
1783 if (!iov_iter_count(to))
1787 * Currently when PCC read failed, we do not fall back to the
1788 * normal read path, just return the error.
1789 * The resaon is that: for RW-PCC, the file data may be modified
1790 * in the PCC and inconsistent with the data on OSTs (or file
1791 * data has been removed from the Lustre file system), at this
1792 * time, fallback to the normal read path may read the wrong
1794 * TODO: for RO-PCC (readonly PCC), fall back to normal read
1795 * path: read data from data copy on OSTs.
1797 result = pcc_file_read_iter(iocb, to, &cached);
1801 ll_ras_enter(file, iocb->ki_pos, iov_iter_count(to));
1803 result = ll_do_fast_read(iocb, to);
1804 if (result < 0 || iov_iter_count(to) == 0)
1807 env = cl_env_get(&refcheck);
1809 return PTR_ERR(env);
1811 args = ll_env_args(env);
1812 args->u.normal.via_iter = to;
1813 args->u.normal.via_iocb = iocb;
1815 rc2 = ll_file_io_generic(env, args, file, CIT_READ,
1816 &iocb->ki_pos, iov_iter_count(to));
1819 else if (result == 0)
1822 cl_env_put(env, &refcheck);
1825 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
1826 file->private_data, iocb->ki_pos, result,
1828 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_READ,
1829 ktime_us_delta(ktime_get(), kstart));
1836 * Similar trick to ll_do_fast_read, this improves write speed for tiny writes.
1837 * If a page is already in the page cache and dirty (and some other things -
1838 * See ll_tiny_write_begin for the instantiation of these rules), then we can
1839 * write to it without doing a full I/O, because Lustre already knows about it
1840 * and will write it out. This saves a lot of processing time.
1842 * All writes here are within one page, so exclusion is handled by the page
1843 * lock on the vm page. We do not do tiny writes for writes which touch
1844 * multiple pages because it's very unlikely multiple sequential pages are
1845 * are already dirty.
1847 * We limit these to < PAGE_SIZE because PAGE_SIZE writes are relatively common
1848 * and are unlikely to be to already dirty pages.
1850 * Attribute updates are important here, we do them in ll_tiny_write_end.
1852 static ssize_t ll_do_tiny_write(struct kiocb *iocb, struct iov_iter *iter)
1854 ssize_t count = iov_iter_count(iter);
1855 struct file *file = iocb->ki_filp;
1856 struct inode *inode = file_inode(file);
1857 bool lock_inode = !IS_NOSEC(inode);
1862 /* Restrict writes to single page and < PAGE_SIZE. See comment at top
1863 * of function for why.
1865 if (count >= PAGE_SIZE ||
1866 (iocb->ki_pos & (PAGE_SIZE-1)) + count > PAGE_SIZE)
1869 if (unlikely(lock_inode))
1871 result = __generic_file_write_iter(iocb, iter);
1873 if (unlikely(lock_inode))
1874 inode_unlock(inode);
1876 /* If the page is not already dirty, ll_tiny_write_begin returns
1877 * -ENODATA. We continue on to normal write.
1879 if (result == -ENODATA)
1883 ll_heat_add(inode, CIT_WRITE, result);
1884 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_WRITE_BYTES,
1886 set_bit(LLIF_DATA_MODIFIED, &ll_i2info(inode)->lli_flags);
1889 CDEBUG(D_VFSTRACE, "result: %zu, original count %zu\n", result, count);
1895 * Write to a file (through the page cache).
1897 static ssize_t ll_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1899 struct vvp_io_args *args;
1901 ssize_t rc_tiny = 0, rc_normal;
1902 struct file *file = iocb->ki_filp;
1905 ktime_t kstart = ktime_get();
1910 if (!iov_iter_count(from))
1911 GOTO(out, rc_normal = 0);
1914 * When PCC write failed, we usually do not fall back to the normal
1915 * write path, just return the error. But there is a special case when
1916 * returned error code is -ENOSPC due to running out of space on PCC HSM
1917 * bakcend. At this time, it will fall back to normal I/O path and
1918 * retry the I/O. As the file is in HSM released state, it will restore
1919 * the file data to OSTs first and redo the write again. And the
1920 * restore process will revoke the layout lock and detach the file
1921 * from PCC cache automatically.
1923 result = pcc_file_write_iter(iocb, from, &cached);
1924 if (cached && result != -ENOSPC && result != -EDQUOT)
1925 GOTO(out, rc_normal = result);
1927 /* NB: we can't do direct IO for tiny writes because they use the page
1928 * cache, we can't do sync writes because tiny writes can't flush
1929 * pages, and we can't do append writes because we can't guarantee the
1930 * required DLM locks are held to protect file size.
1932 if (ll_sbi_has_tiny_write(ll_i2sbi(file_inode(file))) &&
1933 !(file->f_flags & (O_DIRECT | O_SYNC | O_APPEND)))
1934 rc_tiny = ll_do_tiny_write(iocb, from);
1936 /* In case of error, go on and try normal write - Only stop if tiny
1937 * write completed I/O.
1939 if (iov_iter_count(from) == 0)
1940 GOTO(out, rc_normal = rc_tiny);
1942 env = cl_env_get(&refcheck);
1944 return PTR_ERR(env);
1946 args = ll_env_args(env);
1947 args->u.normal.via_iter = from;
1948 args->u.normal.via_iocb = iocb;
1950 rc_normal = ll_file_io_generic(env, args, file, CIT_WRITE,
1951 &iocb->ki_pos, iov_iter_count(from));
1953 /* On success, combine bytes written. */
1954 if (rc_tiny >= 0 && rc_normal > 0)
1955 rc_normal += rc_tiny;
1956 /* On error, only return error from normal write if tiny write did not
1957 * write any bytes. Otherwise return bytes written by tiny write.
1959 else if (rc_tiny > 0)
1960 rc_normal = rc_tiny;
1962 cl_env_put(env, &refcheck);
1964 if (rc_normal > 0) {
1965 ll_rw_stats_tally(ll_i2sbi(file_inode(file)), current->pid,
1966 file->private_data, iocb->ki_pos,
1968 ll_stats_ops_tally(ll_i2sbi(file_inode(file)), LPROC_LL_WRITE,
1969 ktime_us_delta(ktime_get(), kstart));
1975 #ifndef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
1977 * XXX: exact copy from kernel code (__generic_file_aio_write_nolock)
1979 static int ll_file_get_iov_count(const struct iovec *iov,
1980 unsigned long *nr_segs, size_t *count,
1986 for (seg = 0; seg < *nr_segs; seg++) {
1987 const struct iovec *iv = &iov[seg];
1990 * If any segment has a negative length, or the cumulative
1991 * length ever wraps negative then return -EINVAL.
1994 if (unlikely((ssize_t)(cnt|iv->iov_len) < 0))
1996 if (access_ok(access_flags, iv->iov_base, iv->iov_len))
2001 cnt -= iv->iov_len; /* This segment is no good */
2008 static ssize_t ll_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
2009 unsigned long nr_segs, loff_t pos)
2016 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_READ);
2023 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2024 iov_iter_init(&to, READ, iov, nr_segs, iov_count);
2025 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2026 iov_iter_init(&to, iov, nr_segs, iov_count, 0);
2027 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2029 result = ll_file_read_iter(iocb, &to);
2034 static ssize_t ll_file_read(struct file *file, char __user *buf, size_t count,
2037 struct iovec iov = { .iov_base = buf, .iov_len = count };
2046 init_sync_kiocb(&kiocb, file);
2047 kiocb.ki_pos = *ppos;
2048 #ifdef HAVE_KIOCB_KI_LEFT
2049 kiocb.ki_left = count;
2050 #elif defined(HAVE_KI_NBYTES)
2051 kiocb.i_nbytes = count;
2054 result = ll_file_aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
2055 *ppos = kiocb.ki_pos;
2061 * Write to a file (through the page cache).
2064 static ssize_t ll_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2065 unsigned long nr_segs, loff_t pos)
2067 struct iov_iter from;
2072 result = ll_file_get_iov_count(iov, &nr_segs, &iov_count, VERIFY_WRITE);
2079 # ifdef HAVE_IOV_ITER_INIT_DIRECTION
2080 iov_iter_init(&from, WRITE, iov, nr_segs, iov_count);
2081 # else /* !HAVE_IOV_ITER_INIT_DIRECTION */
2082 iov_iter_init(&from, iov, nr_segs, iov_count, 0);
2083 # endif /* HAVE_IOV_ITER_INIT_DIRECTION */
2085 result = ll_file_write_iter(iocb, &from);
2090 static ssize_t ll_file_write(struct file *file, const char __user *buf,
2091 size_t count, loff_t *ppos)
2093 struct iovec iov = { .iov_base = (void __user *)buf,
2103 init_sync_kiocb(&kiocb, file);
2104 kiocb.ki_pos = *ppos;
2105 #ifdef HAVE_KIOCB_KI_LEFT
2106 kiocb.ki_left = count;
2107 #elif defined(HAVE_KI_NBYTES)
2108 kiocb.ki_nbytes = count;
2111 result = ll_file_aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
2112 *ppos = kiocb.ki_pos;
2116 #endif /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
2118 int ll_lov_setstripe_ea_info(struct inode *inode, struct dentry *dentry,
2119 __u64 flags, struct lov_user_md *lum, int lum_size)
2121 struct lookup_intent oit = {
2123 .it_flags = flags | MDS_OPEN_BY_FID,
2128 if ((__swab32(lum->lmm_magic) & le32_to_cpu(LOV_MAGIC_MASK)) ==
2129 le32_to_cpu(LOV_MAGIC_MAGIC)) {
2130 /* this code will only exist for big-endian systems */
2131 lustre_swab_lov_user_md(lum, 0);
2134 ll_inode_size_lock(inode);
2135 rc = ll_intent_file_open(dentry, lum, lum_size, &oit);
2137 GOTO(out_unlock, rc);
2139 ll_release_openhandle(dentry, &oit);
2142 ll_inode_size_unlock(inode);
2143 ll_intent_release(&oit);
2148 int ll_lov_getstripe_ea_info(struct inode *inode, const char *filename,
2149 struct lov_mds_md **lmmp, int *lmm_size,
2150 struct ptlrpc_request **request)
2152 struct ll_sb_info *sbi = ll_i2sbi(inode);
2153 struct mdt_body *body;
2154 struct lov_mds_md *lmm = NULL;
2155 struct ptlrpc_request *req = NULL;
2156 struct md_op_data *op_data;
2161 rc = ll_get_default_mdsize(sbi, &lmmsize);
2165 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename,
2166 strlen(filename), lmmsize,
2167 LUSTRE_OPC_ANY, NULL);
2168 if (IS_ERR(op_data))
2169 RETURN(PTR_ERR(op_data));
2171 op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
2172 rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
2173 ll_finish_md_op_data(op_data);
2175 CDEBUG(D_INFO, "md_getattr_name failed "
2176 "on %s: rc %d\n", filename, rc);
2180 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2181 LASSERT(body != NULL); /* checked by mdc_getattr_name */
2183 lmmsize = body->mbo_eadatasize;
2185 if (!(body->mbo_valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
2187 GOTO(out, rc = -ENODATA);
2189 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD, lmmsize);
2190 LASSERT(lmm != NULL);
2192 if (lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V1) &&
2193 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_V3) &&
2194 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_COMP_V1) &&
2195 lmm->lmm_magic != cpu_to_le32(LOV_MAGIC_FOREIGN))
2196 GOTO(out, rc = -EPROTO);
2199 * This is coming from the MDS, so is probably in
2200 * little endian. We convert it to host endian before
2201 * passing it to userspace.
2203 if (cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) {
2204 int stripe_count = 0;
2206 if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1) ||
2207 lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3)) {
2208 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
2209 if (le32_to_cpu(lmm->lmm_pattern) &
2210 LOV_PATTERN_F_RELEASED)
2212 lustre_swab_lov_user_md((struct lov_user_md *)lmm, 0);
2214 /* if function called for directory - we should
2215 * avoid swab not existent lsm objects
2217 if (lmm->lmm_magic == LOV_MAGIC_V1 &&
2218 S_ISREG(body->mbo_mode))
2219 lustre_swab_lov_user_md_objects(
2220 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
2222 else if (lmm->lmm_magic == LOV_MAGIC_V3 &&
2223 S_ISREG(body->mbo_mode))
2224 lustre_swab_lov_user_md_objects(
2225 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
2227 } else if (lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_COMP_V1)) {
2228 lustre_swab_lov_comp_md_v1(
2229 (struct lov_comp_md_v1 *)lmm);
2233 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
2234 struct lov_comp_md_v1 *comp_v1 = NULL;
2235 struct lov_comp_md_entry_v1 *ent;
2236 struct lov_user_md_v1 *v1;
2240 comp_v1 = (struct lov_comp_md_v1 *)lmm;
2241 /* Dump the striping information */
2242 for (; i < comp_v1->lcm_entry_count; i++) {
2243 ent = &comp_v1->lcm_entries[i];
2244 off = ent->lcme_offset;
2245 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2247 "comp[%d]: stripe_count=%u, stripe_size=%u\n",
2248 i, v1->lmm_stripe_count, v1->lmm_stripe_size);
2252 * Return valid stripe_count and stripe_size instead of 0 for
2253 * DoM files to avoid divide-by-zero for older userspace that
2254 * calls this ioctl, e.g. lustre ADIO driver.
2256 if (lmm->lmm_stripe_count == 0)
2257 lmm->lmm_stripe_count = 1;
2258 if (lmm->lmm_stripe_size == 0) {
2259 /* Since the first component of the file data is placed
2260 * on the MDT for faster access, the stripe_size of the
2261 * second one is always that applications which are
2264 if (lmm->lmm_pattern == LOV_PATTERN_MDT)
2265 i = comp_v1->lcm_entry_count > 1 ? 1 : 0;
2267 i = comp_v1->lcm_entry_count > 1 ?
2268 comp_v1->lcm_entry_count - 1 : 0;
2269 ent = &comp_v1->lcm_entries[i];
2270 off = ent->lcme_offset;
2271 v1 = (struct lov_user_md_v1 *)((char *)lmm + off);
2272 lmm->lmm_stripe_size = v1->lmm_stripe_size;
2277 *lmm_size = lmmsize;
2282 static int ll_lov_setea(struct inode *inode, struct file *file,
2285 __u64 flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
2286 struct lov_user_md *lump;
2287 int lum_size = sizeof(struct lov_user_md) +
2288 sizeof(struct lov_user_ost_data);
2292 if (!capable(CAP_SYS_ADMIN))
2295 OBD_ALLOC_LARGE(lump, lum_size);
2299 if (copy_from_user(lump, arg, lum_size))
2300 GOTO(out_lump, rc = -EFAULT);
2302 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, lump,
2304 cl_lov_delay_create_clear(&file->f_flags);
2307 OBD_FREE_LARGE(lump, lum_size);
2311 static int ll_file_getstripe(struct inode *inode, void __user *lum, size_t size)
2318 env = cl_env_get(&refcheck);
2320 RETURN(PTR_ERR(env));
2322 rc = cl_object_getstripe(env, ll_i2info(inode)->lli_clob, lum, size);
2323 cl_env_put(env, &refcheck);
2327 static int ll_lov_setstripe(struct inode *inode, struct file *file,
2330 struct lov_user_md __user *lum = (struct lov_user_md __user *)arg;
2331 struct lov_user_md *klum;
2333 __u64 flags = FMODE_WRITE;
2336 rc = ll_copy_user_md(lum, &klum);
2341 rc = ll_lov_setstripe_ea_info(inode, file_dentry(file), flags, klum,
2346 rc = put_user(0, &lum->lmm_stripe_count);
2350 rc = ll_layout_refresh(inode, &gen);
2354 rc = ll_file_getstripe(inode, arg, lum_size);
2355 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode) &&
2356 ll_i2info(inode)->lli_clob) {
2357 struct iattr attr = { 0 };
2359 rc = cl_setattr_ost(ll_i2info(inode)->lli_clob, &attr,
2360 OP_XVALID_FLAGS, LUSTRE_ENCRYPT_FL);
2363 cl_lov_delay_create_clear(&file->f_flags);
2366 OBD_FREE_LARGE(klum, lum_size);
2372 ll_get_grouplock(struct inode *inode, struct file *file, unsigned long arg)
2374 struct ll_inode_info *lli = ll_i2info(inode);
2375 struct cl_object *obj = lli->lli_clob;
2376 struct ll_file_data *fd = file->private_data;
2377 struct ll_grouplock grouplock;
2382 CWARN("group id for group lock must not be 0\n");
2386 if (ll_file_nolock(file))
2387 RETURN(-EOPNOTSUPP);
2389 if (file->f_flags & O_NONBLOCK) {
2390 if (!mutex_trylock(&lli->lli_group_mutex))
2393 mutex_lock(&lli->lli_group_mutex);
2395 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
2396 CWARN("group lock already existed with gid %lu\n",
2397 fd->fd_grouplock.lg_gid);
2398 GOTO(out, rc = -EINVAL);
2400 if (arg != lli->lli_group_gid && lli->lli_group_users != 0) {
2401 if (file->f_flags & O_NONBLOCK)
2402 GOTO(out, rc = -EAGAIN);
2403 mutex_unlock(&lli->lli_group_mutex);
2404 wait_var_event(&lli->lli_group_users, !lli->lli_group_users);
2405 GOTO(retry, rc = 0);
2407 LASSERT(fd->fd_grouplock.lg_lock == NULL);
2410 * XXX: group lock needs to protect all OST objects while PFL
2411 * can add new OST objects during the IO, so we'd instantiate
2412 * all OST objects before getting its group lock.
2417 struct cl_layout cl = {
2418 .cl_is_composite = false,
2420 struct lu_extent ext = {
2422 .e_end = OBD_OBJECT_EOF,
2425 env = cl_env_get(&refcheck);
2427 GOTO(out, rc = PTR_ERR(env));
2429 rc = cl_object_layout_get(env, obj, &cl);
2430 if (rc >= 0 && cl.cl_is_composite)
2431 rc = ll_layout_write_intent(inode, LAYOUT_INTENT_WRITE,
2434 cl_env_put(env, &refcheck);
2439 rc = cl_get_grouplock(ll_i2info(inode)->lli_clob,
2440 arg, (file->f_flags & O_NONBLOCK), &grouplock);
2445 fd->fd_flags |= LL_FILE_GROUP_LOCKED;
2446 fd->fd_grouplock = grouplock;
2447 if (lli->lli_group_users == 0)
2448 lli->lli_group_gid = grouplock.lg_gid;
2449 lli->lli_group_users++;
2451 CDEBUG(D_INFO, "group lock %lu obtained\n", arg);
2453 mutex_unlock(&lli->lli_group_mutex);
2458 static int ll_put_grouplock(struct inode *inode, struct file *file,
2461 struct ll_inode_info *lli = ll_i2info(inode);
2462 struct ll_file_data *fd = file->private_data;
2463 struct ll_grouplock grouplock;
2467 mutex_lock(&lli->lli_group_mutex);
2468 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
2469 CWARN("no group lock held\n");
2470 GOTO(out, rc = -EINVAL);
2473 LASSERT(fd->fd_grouplock.lg_lock != NULL);
2475 if (fd->fd_grouplock.lg_gid != arg) {
2476 CWARN("group lock %lu doesn't match current id %lu\n",
2477 arg, fd->fd_grouplock.lg_gid);
2478 GOTO(out, rc = -EINVAL);
2481 grouplock = fd->fd_grouplock;
2482 memset(&fd->fd_grouplock, 0, sizeof(fd->fd_grouplock));
2483 fd->fd_flags &= ~LL_FILE_GROUP_LOCKED;
2485 cl_put_grouplock(&grouplock);
2487 lli->lli_group_users--;
2488 if (lli->lli_group_users == 0) {
2489 lli->lli_group_gid = 0;
2490 wake_up_var(&lli->lli_group_users);
2492 CDEBUG(D_INFO, "group lock %lu released\n", arg);
2495 mutex_unlock(&lli->lli_group_mutex);
2501 * Close inode open handle
2503 * \param dentry [in] dentry which contains the inode
2504 * \param it [in,out] intent which contains open info and result
2507 * \retval <0 failure
2509 int ll_release_openhandle(struct dentry *dentry, struct lookup_intent *it)
2511 struct inode *inode = dentry->d_inode;
2512 struct obd_client_handle *och;
2518 /* Root ? Do nothing. */
2519 if (is_root_inode(inode))
2522 /* No open handle to close? Move away */
2523 if (!it_disposition(it, DISP_OPEN_OPEN))
2526 LASSERT(it_open_error(DISP_OPEN_OPEN, it) == 0);
2528 OBD_ALLOC(och, sizeof(*och));
2530 GOTO(out, rc = -ENOMEM);
2532 rc = ll_och_fill(ll_i2sbi(inode)->ll_md_exp, it, och);
2536 rc = ll_close_inode_openhandle(inode, och, 0, NULL);
2538 /* this one is in place of ll_file_open */
2539 if (it_disposition(it, DISP_ENQ_OPEN_REF)) {
2540 ptlrpc_req_finished(it->it_request);
2541 it_clear_disposition(it, DISP_ENQ_OPEN_REF);
2547 * Get size for inode for which FIEMAP mapping is requested.
2548 * Make the FIEMAP get_info call and returns the result.
2549 * \param fiemap kernel buffer to hold extens
2550 * \param num_bytes kernel buffer size
2552 static int ll_do_fiemap(struct inode *inode, struct fiemap *fiemap,
2558 struct ll_fiemap_info_key fmkey = { .lfik_name = KEY_FIEMAP, };
2561 /* Checks for fiemap flags */
2562 if (fiemap->fm_flags & ~LUSTRE_FIEMAP_FLAGS_COMPAT) {
2563 fiemap->fm_flags &= ~LUSTRE_FIEMAP_FLAGS_COMPAT;
2567 /* Check for FIEMAP_FLAG_SYNC */
2568 if (fiemap->fm_flags & FIEMAP_FLAG_SYNC) {
2569 rc = filemap_fdatawrite(inode->i_mapping);
2574 env = cl_env_get(&refcheck);
2576 RETURN(PTR_ERR(env));
2578 if (i_size_read(inode) == 0) {
2579 rc = ll_glimpse_size(inode);
2584 fmkey.lfik_oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2585 obdo_from_inode(&fmkey.lfik_oa, inode, OBD_MD_FLSIZE);
2586 obdo_set_parent_fid(&fmkey.lfik_oa, &ll_i2info(inode)->lli_fid);
2588 /* If filesize is 0, then there would be no objects for mapping */
2589 if (fmkey.lfik_oa.o_size == 0) {
2590 fiemap->fm_mapped_extents = 0;
2594 fmkey.lfik_fiemap = *fiemap;
2596 rc = cl_object_fiemap(env, ll_i2info(inode)->lli_clob,
2597 &fmkey, fiemap, &num_bytes);
2599 cl_env_put(env, &refcheck);
2603 int ll_fid2path(struct inode *inode, void __user *arg)
2605 struct obd_export *exp = ll_i2mdexp(inode);
2606 const struct getinfo_fid2path __user *gfin = arg;
2608 struct getinfo_fid2path *gfout;
2614 if (!capable(CAP_DAC_READ_SEARCH) &&
2615 !(ll_i2sbi(inode)->ll_flags & LL_SBI_USER_FID2PATH))
2618 /* Only need to get the buflen */
2619 if (get_user(pathlen, &gfin->gf_pathlen))
2622 if (pathlen > PATH_MAX)
2625 outsize = sizeof(*gfout) + pathlen;
2626 OBD_ALLOC(gfout, outsize);
2630 if (copy_from_user(gfout, arg, sizeof(*gfout)))
2631 GOTO(gf_free, rc = -EFAULT);
2632 /* append root FID after gfout to let MDT know the root FID so that it
2633 * can lookup the correct path, this is mainly for fileset.
2634 * old server without fileset mount support will ignore this. */
2635 *gfout->gf_u.gf_root_fid = *ll_inode2fid(inode);
2637 /* Call mdc_iocontrol */
2638 rc = obd_iocontrol(OBD_IOC_FID2PATH, exp, outsize, gfout, NULL);
2642 if (copy_to_user(arg, gfout, outsize))
2646 OBD_FREE(gfout, outsize);
2651 ll_ioc_data_version(struct inode *inode, struct ioc_data_version *ioc)
2653 struct cl_object *obj = ll_i2info(inode)->lli_clob;
2661 ioc->idv_version = 0;
2662 ioc->idv_layout_version = UINT_MAX;
2664 /* If no file object initialized, we consider its version is 0. */
2668 env = cl_env_get(&refcheck);
2670 RETURN(PTR_ERR(env));
2672 io = vvp_env_thread_io(env);
2674 io->u.ci_data_version.dv_data_version = 0;
2675 io->u.ci_data_version.dv_layout_version = UINT_MAX;
2676 io->u.ci_data_version.dv_flags = ioc->idv_flags;
2679 if (cl_io_init(env, io, CIT_DATA_VERSION, io->ci_obj) == 0)
2680 result = cl_io_loop(env, io);
2682 result = io->ci_result;
2684 ioc->idv_version = io->u.ci_data_version.dv_data_version;
2685 ioc->idv_layout_version = io->u.ci_data_version.dv_layout_version;
2687 cl_io_fini(env, io);
2689 if (unlikely(io->ci_need_restart))
2692 cl_env_put(env, &refcheck);
2698 * Read the data_version for inode.
2700 * This value is computed using stripe object version on OST.
2701 * Version is computed using server side locking.
2703 * @param flags if do sync on the OST side;
2705 * LL_DV_RD_FLUSH: flush dirty pages, LCK_PR on OSTs
2706 * LL_DV_WR_FLUSH: drop all caching pages, LCK_PW on OSTs
2708 int ll_data_version(struct inode *inode, __u64 *data_version, int flags)
2710 struct ioc_data_version ioc = { .idv_flags = flags };
2713 rc = ll_ioc_data_version(inode, &ioc);
2715 *data_version = ioc.idv_version;
2721 * Trigger a HSM release request for the provided inode.
2723 int ll_hsm_release(struct inode *inode)
2726 struct obd_client_handle *och = NULL;
2727 __u64 data_version = 0;
2732 CDEBUG(D_INODE, "%s: Releasing file "DFID".\n",
2733 ll_i2sbi(inode)->ll_fsname,
2734 PFID(&ll_i2info(inode)->lli_fid));
2736 och = ll_lease_open(inode, NULL, FMODE_WRITE, MDS_OPEN_RELEASE);
2738 GOTO(out, rc = PTR_ERR(och));
2740 /* Grab latest data_version and [am]time values */
2741 rc = ll_data_version(inode, &data_version, LL_DV_WR_FLUSH);
2745 env = cl_env_get(&refcheck);
2747 GOTO(out, rc = PTR_ERR(env));
2749 rc = ll_merge_attr(env, inode);
2750 cl_env_put(env, &refcheck);
2752 /* If error happen, we have the wrong size for a file.
2758 /* Release the file.
2759 * NB: lease lock handle is released in mdc_hsm_release_pack() because
2760 * we still need it to pack l_remote_handle to MDT. */
2761 rc = ll_close_inode_openhandle(inode, och, MDS_HSM_RELEASE,
2767 if (och != NULL && !IS_ERR(och)) /* close the file */
2768 ll_lease_close(och, inode, NULL);
2773 struct ll_swap_stack {
2776 struct inode *inode1;
2777 struct inode *inode2;
2782 static int ll_swap_layouts(struct file *file1, struct file *file2,
2783 struct lustre_swap_layouts *lsl)
2785 struct mdc_swap_layouts msl;
2786 struct md_op_data *op_data;
2789 struct ll_swap_stack *llss = NULL;
2792 OBD_ALLOC_PTR(llss);
2796 llss->inode1 = file_inode(file1);
2797 llss->inode2 = file_inode(file2);
2799 rc = ll_check_swap_layouts_validity(llss->inode1, llss->inode2);
2803 /* we use 2 bool because it is easier to swap than 2 bits */
2804 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV1)
2805 llss->check_dv1 = true;
2807 if (lsl->sl_flags & SWAP_LAYOUTS_CHECK_DV2)
2808 llss->check_dv2 = true;
2810 /* we cannot use lsl->sl_dvX directly because we may swap them */
2811 llss->dv1 = lsl->sl_dv1;
2812 llss->dv2 = lsl->sl_dv2;
2814 rc = lu_fid_cmp(ll_inode2fid(llss->inode1), ll_inode2fid(llss->inode2));
2815 if (rc == 0) /* same file, done! */
2818 if (rc < 0) { /* sequentialize it */
2819 swap(llss->inode1, llss->inode2);
2821 swap(llss->dv1, llss->dv2);
2822 swap(llss->check_dv1, llss->check_dv2);
2826 if (gid != 0) { /* application asks to flush dirty cache */
2827 rc = ll_get_grouplock(llss->inode1, file1, gid);
2831 rc = ll_get_grouplock(llss->inode2, file2, gid);
2833 ll_put_grouplock(llss->inode1, file1, gid);
2838 /* ultimate check, before swaping the layouts we check if
2839 * dataversion has changed (if requested) */
2840 if (llss->check_dv1) {
2841 rc = ll_data_version(llss->inode1, &dv, 0);
2844 if (dv != llss->dv1)
2845 GOTO(putgl, rc = -EAGAIN);
2848 if (llss->check_dv2) {
2849 rc = ll_data_version(llss->inode2, &dv, 0);
2852 if (dv != llss->dv2)
2853 GOTO(putgl, rc = -EAGAIN);
2856 /* struct md_op_data is used to send the swap args to the mdt
2857 * only flags is missing, so we use struct mdc_swap_layouts
2858 * through the md_op_data->op_data */
2859 /* flags from user space have to be converted before they are send to
2860 * server, no flag is sent today, they are only used on the client */
2863 op_data = ll_prep_md_op_data(NULL, llss->inode1, llss->inode2, NULL, 0,
2864 0, LUSTRE_OPC_ANY, &msl);
2865 if (IS_ERR(op_data))
2866 GOTO(free, rc = PTR_ERR(op_data));
2868 rc = obd_iocontrol(LL_IOC_LOV_SWAP_LAYOUTS, ll_i2mdexp(llss->inode1),
2869 sizeof(*op_data), op_data, NULL);
2870 ll_finish_md_op_data(op_data);
2877 ll_put_grouplock(llss->inode2, file2, gid);
2878 ll_put_grouplock(llss->inode1, file1, gid);
2888 int ll_hsm_state_set(struct inode *inode, struct hsm_state_set *hss)
2890 struct obd_export *exp = ll_i2mdexp(inode);
2891 struct md_op_data *op_data;
2895 /* Detect out-of range masks */
2896 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK)
2899 /* Non-root users are forbidden to set or clear flags which are
2900 * NOT defined in HSM_USER_MASK. */
2901 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
2902 !capable(CAP_SYS_ADMIN))
2905 if (!exp_connect_archive_id_array(exp)) {
2906 /* Detect out-of range archive id */
2907 if ((hss->hss_valid & HSS_ARCHIVE_ID) &&
2908 (hss->hss_archive_id > LL_HSM_ORIGIN_MAX_ARCHIVE))
2912 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
2913 LUSTRE_OPC_ANY, hss);
2914 if (IS_ERR(op_data))
2915 RETURN(PTR_ERR(op_data));
2917 rc = obd_iocontrol(LL_IOC_HSM_STATE_SET, exp, sizeof(*op_data),
2920 ll_finish_md_op_data(op_data);
2925 static int ll_hsm_import(struct inode *inode, struct file *file,
2926 struct hsm_user_import *hui)
2928 struct hsm_state_set *hss = NULL;
2929 struct iattr *attr = NULL;
2933 if (!S_ISREG(inode->i_mode))
2939 GOTO(out, rc = -ENOMEM);
2941 hss->hss_valid = HSS_SETMASK | HSS_ARCHIVE_ID;
2942 hss->hss_archive_id = hui->hui_archive_id;
2943 hss->hss_setmask = HS_ARCHIVED | HS_EXISTS | HS_RELEASED;
2944 rc = ll_hsm_state_set(inode, hss);
2948 OBD_ALLOC_PTR(attr);
2950 GOTO(out, rc = -ENOMEM);
2952 attr->ia_mode = hui->hui_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
2953 attr->ia_mode |= S_IFREG;
2954 attr->ia_uid = make_kuid(&init_user_ns, hui->hui_uid);
2955 attr->ia_gid = make_kgid(&init_user_ns, hui->hui_gid);
2956 attr->ia_size = hui->hui_size;
2957 attr->ia_mtime.tv_sec = hui->hui_mtime;
2958 attr->ia_mtime.tv_nsec = hui->hui_mtime_ns;
2959 attr->ia_atime.tv_sec = hui->hui_atime;
2960 attr->ia_atime.tv_nsec = hui->hui_atime_ns;
2962 attr->ia_valid = ATTR_SIZE | ATTR_MODE | ATTR_FORCE |
2963 ATTR_UID | ATTR_GID |
2964 ATTR_MTIME | ATTR_MTIME_SET |
2965 ATTR_ATIME | ATTR_ATIME_SET;
2969 rc = ll_setattr_raw(file_dentry(file), attr, 0, true);
2973 inode_unlock(inode);
2985 static inline long ll_lease_type_from_fmode(fmode_t fmode)
2987 return ((fmode & FMODE_READ) ? LL_LEASE_RDLCK : 0) |
2988 ((fmode & FMODE_WRITE) ? LL_LEASE_WRLCK : 0);
2991 static int ll_file_futimes_3(struct file *file, const struct ll_futimes_3 *lfu)
2993 struct inode *inode = file_inode(file);
2995 .ia_valid = ATTR_ATIME | ATTR_ATIME_SET |
2996 ATTR_MTIME | ATTR_MTIME_SET |
2999 .tv_sec = lfu->lfu_atime_sec,
3000 .tv_nsec = lfu->lfu_atime_nsec,
3003 .tv_sec = lfu->lfu_mtime_sec,
3004 .tv_nsec = lfu->lfu_mtime_nsec,
3007 .tv_sec = lfu->lfu_ctime_sec,
3008 .tv_nsec = lfu->lfu_ctime_nsec,
3014 if (!capable(CAP_SYS_ADMIN))
3017 if (!S_ISREG(inode->i_mode))
3021 rc = ll_setattr_raw(file_dentry(file), &ia, OP_XVALID_CTIME_SET,
3023 inode_unlock(inode);
3028 static enum cl_lock_mode cl_mode_user_to_kernel(enum lock_mode_user mode)
3031 case MODE_READ_USER:
3033 case MODE_WRITE_USER:
3040 static const char *const user_lockname[] = LOCK_MODE_NAMES;
3042 /* Used to allow the upper layers of the client to request an LDLM lock
3043 * without doing an actual read or write.
3045 * Used for ladvise lockahead to manually request specific locks.
3047 * \param[in] file file this ladvise lock request is on
3048 * \param[in] ladvise ladvise struct describing this lock request
3050 * \retval 0 success, no detailed result available (sync requests
3051 * and requests sent to the server [not handled locally]
3052 * cannot return detailed results)
3053 * \retval LLA_RESULT_{SAME,DIFFERENT} - detailed result of the lock request,
3054 * see definitions for details.
3055 * \retval negative negative errno on error
3057 int ll_file_lock_ahead(struct file *file, struct llapi_lu_ladvise *ladvise)
3059 struct lu_env *env = NULL;
3060 struct cl_io *io = NULL;
3061 struct cl_lock *lock = NULL;
3062 struct cl_lock_descr *descr = NULL;
3063 struct dentry *dentry = file->f_path.dentry;
3064 struct inode *inode = dentry->d_inode;
3065 enum cl_lock_mode cl_mode;
3066 off_t start = ladvise->lla_start;
3067 off_t end = ladvise->lla_end;
3074 "Lock request: file=%pd, inode=%p, mode=%s start=%llu, end=%llu\n",
3075 dentry, dentry->d_inode,
3076 user_lockname[ladvise->lla_lockahead_mode], (__u64) start,
3079 cl_mode = cl_mode_user_to_kernel(ladvise->lla_lockahead_mode);
3081 GOTO(out, result = cl_mode);
3083 /* Get IO environment */
3084 result = cl_io_get(inode, &env, &io, &refcheck);
3088 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
3091 * nothing to do for this io. This currently happens when
3092 * stripe sub-object's are not yet created.
3094 result = io->ci_result;
3095 } else if (result == 0) {
3096 lock = vvp_env_lock(env);
3097 descr = &lock->cll_descr;
3099 descr->cld_obj = io->ci_obj;
3100 /* Convert byte offsets to pages */
3101 descr->cld_start = cl_index(io->ci_obj, start);
3102 descr->cld_end = cl_index(io->ci_obj, end);
3103 descr->cld_mode = cl_mode;
3104 /* CEF_MUST is used because we do not want to convert a
3105 * lockahead request to a lockless lock */
3106 descr->cld_enq_flags = CEF_MUST | CEF_LOCK_NO_EXPAND |
3109 if (ladvise->lla_peradvice_flags & LF_ASYNC)
3110 descr->cld_enq_flags |= CEF_SPECULATIVE;
3112 result = cl_lock_request(env, io, lock);
3114 /* On success, we need to release the lock */
3116 cl_lock_release(env, lock);
3118 cl_io_fini(env, io);
3119 cl_env_put(env, &refcheck);
3121 /* -ECANCELED indicates a matching lock with a different extent
3122 * was already present, and -EEXIST indicates a matching lock
3123 * on exactly the same extent was already present.
3124 * We convert them to positive values for userspace to make
3125 * recognizing true errors easier.
3126 * Note we can only return these detailed results on async requests,
3127 * as sync requests look the same as i/o requests for locking. */
3128 if (result == -ECANCELED)
3129 result = LLA_RESULT_DIFFERENT;
3130 else if (result == -EEXIST)
3131 result = LLA_RESULT_SAME;
3136 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
3138 static int ll_ladvise_sanity(struct inode *inode,
3139 struct llapi_lu_ladvise *ladvise)
3141 struct ll_sb_info *sbi = ll_i2sbi(inode);
3142 enum lu_ladvise_type advice = ladvise->lla_advice;
3143 /* Note the peradvice flags is a 32 bit field, so per advice flags must
3144 * be in the first 32 bits of enum ladvise_flags */
3145 __u32 flags = ladvise->lla_peradvice_flags;
3146 /* 3 lines at 80 characters per line, should be plenty */
3149 if (advice > LU_LADVISE_MAX || advice == LU_LADVISE_INVALID) {
3152 "%s: advice with value '%d' not recognized, last supported advice is %s (value '%d'): rc = %d\n",
3153 sbi->ll_fsname, advice,
3154 ladvise_names[LU_LADVISE_MAX-1], LU_LADVISE_MAX-1, rc);
3158 /* Per-advice checks */
3160 case LU_LADVISE_LOCKNOEXPAND:
3161 if (flags & ~LF_LOCKNOEXPAND_MASK) {
3163 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3164 "rc = %d\n", sbi->ll_fsname, flags,
3165 ladvise_names[advice], rc);
3169 case LU_LADVISE_LOCKAHEAD:
3170 /* Currently only READ and WRITE modes can be requested */
3171 if (ladvise->lla_lockahead_mode >= MODE_MAX_USER ||
3172 ladvise->lla_lockahead_mode == 0) {
3174 CDEBUG(D_VFSTRACE, "%s: Invalid mode (%d) for %s: "
3175 "rc = %d\n", sbi->ll_fsname,
3176 ladvise->lla_lockahead_mode,
3177 ladvise_names[advice], rc);
3181 case LU_LADVISE_WILLREAD:
3182 case LU_LADVISE_DONTNEED:
3184 /* Note fall through above - These checks apply to all advices
3185 * except LOCKNOEXPAND */
3186 if (flags & ~LF_DEFAULT_MASK) {
3188 CDEBUG(D_VFSTRACE, "%s: Invalid flags (%x) for %s: "
3189 "rc = %d\n", sbi->ll_fsname, flags,
3190 ladvise_names[advice], rc);
3193 if (ladvise->lla_start >= ladvise->lla_end) {
3195 CDEBUG(D_VFSTRACE, "%s: Invalid range (%llu to %llu) "
3196 "for %s: rc = %d\n", sbi->ll_fsname,
3197 ladvise->lla_start, ladvise->lla_end,
3198 ladvise_names[advice], rc);
3210 * Give file access advices
3212 * The ladvise interface is similar to Linux fadvise() system call, except it
3213 * forwards the advices directly from Lustre client to server. The server side
3214 * codes will apply appropriate read-ahead and caching techniques for the
3215 * corresponding files.
3217 * A typical workload for ladvise is e.g. a bunch of different clients are
3218 * doing small random reads of a file, so prefetching pages into OSS cache
3219 * with big linear reads before the random IO is a net benefit. Fetching
3220 * all that data into each client cache with fadvise() may not be, due to
3221 * much more data being sent to the client.
3223 static int ll_ladvise(struct inode *inode, struct file *file, __u64 flags,
3224 struct llapi_lu_ladvise *ladvise)
3228 struct cl_ladvise_io *lio;
3233 env = cl_env_get(&refcheck);
3235 RETURN(PTR_ERR(env));
3237 io = vvp_env_thread_io(env);
3238 io->ci_obj = ll_i2info(inode)->lli_clob;
3240 /* initialize parameters for ladvise */
3241 lio = &io->u.ci_ladvise;
3242 lio->li_start = ladvise->lla_start;
3243 lio->li_end = ladvise->lla_end;
3244 lio->li_fid = ll_inode2fid(inode);
3245 lio->li_advice = ladvise->lla_advice;
3246 lio->li_flags = flags;
3248 if (cl_io_init(env, io, CIT_LADVISE, io->ci_obj) == 0)
3249 rc = cl_io_loop(env, io);
3253 cl_io_fini(env, io);
3254 cl_env_put(env, &refcheck);
3258 static int ll_lock_noexpand(struct file *file, int flags)
3260 struct ll_file_data *fd = file->private_data;
3262 fd->ll_lock_no_expand = !(flags & LF_UNSET);
3267 int ll_ioctl_fsgetxattr(struct inode *inode, unsigned int cmd,
3270 struct fsxattr fsxattr;
3272 if (copy_from_user(&fsxattr,
3273 (const struct fsxattr __user *)arg,
3277 fsxattr.fsx_xflags = ll_inode_flags_to_xflags(inode->i_flags);
3278 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags))
3279 fsxattr.fsx_xflags |= FS_XFLAG_PROJINHERIT;
3280 fsxattr.fsx_projid = ll_i2info(inode)->lli_projid;
3281 if (copy_to_user((struct fsxattr __user *)arg,
3282 &fsxattr, sizeof(fsxattr)))
3288 int ll_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
3291 * Project Quota ID state is only allowed to change from within the init
3292 * namespace. Enforce that restriction only if we are trying to change
3293 * the quota ID state. Everything else is allowed in user namespaces.
3295 if (current_user_ns() == &init_user_ns)
3298 if (ll_i2info(inode)->lli_projid != fa->fsx_projid)
3301 if (test_bit(LLIF_PROJECT_INHERIT, &ll_i2info(inode)->lli_flags)) {
3302 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
3305 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
3312 int ll_ioctl_fssetxattr(struct inode *inode, unsigned int cmd,
3315 struct md_op_data *op_data;
3316 struct ptlrpc_request *req = NULL;
3317 struct fsxattr fsxattr;
3318 struct cl_object *obj;
3319 unsigned int inode_flags;
3322 if (copy_from_user(&fsxattr,
3323 (const struct fsxattr __user *)arg,
3327 rc = ll_ioctl_check_project(inode, &fsxattr);
3331 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3332 LUSTRE_OPC_ANY, NULL);
3333 if (IS_ERR(op_data))
3334 RETURN(PTR_ERR(op_data));
3336 inode_flags = ll_xflags_to_inode_flags(fsxattr.fsx_xflags);
3337 op_data->op_attr_flags = ll_inode_to_ext_flags(inode_flags);
3338 if (fsxattr.fsx_xflags & FS_XFLAG_PROJINHERIT)
3339 op_data->op_attr_flags |= LUSTRE_PROJINHERIT_FL;
3340 op_data->op_projid = fsxattr.fsx_projid;
3341 op_data->op_xvalid |= OP_XVALID_PROJID | OP_XVALID_FLAGS;
3342 rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, NULL, 0, &req);
3343 ptlrpc_req_finished(req);
3345 GOTO(out_fsxattr, rc);
3346 ll_update_inode_flags(inode, op_data->op_attr_flags);
3348 /* Avoid OST RPC if this is only ioctl setting project inherit flag */
3349 if (fsxattr.fsx_xflags == 0 ||
3350 fsxattr.fsx_xflags == FS_XFLAG_PROJINHERIT)
3351 GOTO(out_fsxattr, rc);
3353 obj = ll_i2info(inode)->lli_clob;
3355 struct iattr attr = { 0 };
3357 rc = cl_setattr_ost(obj, &attr, OP_XVALID_FLAGS,
3358 fsxattr.fsx_xflags);
3362 ll_finish_md_op_data(op_data);
3366 static long ll_file_unlock_lease(struct file *file, struct ll_ioc_lease *ioc,
3369 struct inode *inode = file_inode(file);
3370 struct ll_file_data *fd = file->private_data;
3371 struct ll_inode_info *lli = ll_i2info(inode);
3372 struct obd_client_handle *och = NULL;
3373 struct split_param sp;
3374 struct pcc_param param;
3375 bool lease_broken = false;
3377 enum mds_op_bias bias = 0;
3378 struct file *layout_file = NULL;
3380 size_t data_size = 0;
3381 bool attached = false;
3386 mutex_lock(&lli->lli_och_mutex);
3387 if (fd->fd_lease_och != NULL) {
3388 och = fd->fd_lease_och;
3389 fd->fd_lease_och = NULL;
3391 mutex_unlock(&lli->lli_och_mutex);
3396 fmode = och->och_flags;
3398 switch (ioc->lil_flags) {
3399 case LL_LEASE_RESYNC_DONE:
3400 if (ioc->lil_count > IOC_IDS_MAX)
3401 GOTO(out_lease_close, rc = -EINVAL);
3403 data_size = offsetof(typeof(*ioc), lil_ids[ioc->lil_count]);
3404 OBD_ALLOC(data, data_size);
3406 GOTO(out_lease_close, rc = -ENOMEM);
3408 if (copy_from_user(data, (void __user *)arg, data_size))
3409 GOTO(out_lease_close, rc = -EFAULT);
3411 bias = MDS_CLOSE_RESYNC_DONE;
3413 case LL_LEASE_LAYOUT_MERGE: {
3416 if (ioc->lil_count != 1)
3417 GOTO(out_lease_close, rc = -EINVAL);
3419 arg += sizeof(*ioc);
3420 if (copy_from_user(&fd, (void __user *)arg, sizeof(__u32)))
3421 GOTO(out_lease_close, rc = -EFAULT);
3423 layout_file = fget(fd);
3425 GOTO(out_lease_close, rc = -EBADF);
3427 if ((file->f_flags & O_ACCMODE) == O_RDONLY ||
3428 (layout_file->f_flags & O_ACCMODE) == O_RDONLY)
3429 GOTO(out_lease_close, rc = -EPERM);
3431 data = file_inode(layout_file);
3432 bias = MDS_CLOSE_LAYOUT_MERGE;
3435 case LL_LEASE_LAYOUT_SPLIT: {
3439 if (ioc->lil_count != 2)
3440 GOTO(out_lease_close, rc = -EINVAL);
3442 arg += sizeof(*ioc);
3443 if (copy_from_user(&fdv, (void __user *)arg, sizeof(__u32)))
3444 GOTO(out_lease_close, rc = -EFAULT);
3446 arg += sizeof(__u32);
3447 if (copy_from_user(&mirror_id, (void __user *)arg,
3449 GOTO(out_lease_close, rc = -EFAULT);
3451 layout_file = fget(fdv);
3453 GOTO(out_lease_close, rc = -EBADF);
3455 sp.sp_inode = file_inode(layout_file);
3456 sp.sp_mirror_id = (__u16)mirror_id;
3458 bias = MDS_CLOSE_LAYOUT_SPLIT;
3461 case LL_LEASE_PCC_ATTACH:
3462 if (ioc->lil_count != 1)
3465 arg += sizeof(*ioc);
3466 if (copy_from_user(¶m.pa_archive_id, (void __user *)arg,
3468 GOTO(out_lease_close, rc2 = -EFAULT);
3470 rc2 = pcc_readwrite_attach(file, inode, param.pa_archive_id);
3472 GOTO(out_lease_close, rc2);
3475 /* Grab latest data version */
3476 rc2 = ll_data_version(inode, ¶m.pa_data_version,
3479 GOTO(out_lease_close, rc2);
3482 bias = MDS_PCC_ATTACH;
3485 /* without close intent */
3490 rc = ll_lease_close_intent(och, inode, &lease_broken, bias, data);
3494 rc = ll_lease_och_release(inode, file);
3503 switch (ioc->lil_flags) {
3504 case LL_LEASE_RESYNC_DONE:
3506 OBD_FREE(data, data_size);
3508 case LL_LEASE_LAYOUT_MERGE:
3509 case LL_LEASE_LAYOUT_SPLIT:
3513 case LL_LEASE_PCC_ATTACH:
3516 rc = pcc_readwrite_attach_fini(file, inode,
3517 param.pa_layout_gen,
3524 rc = ll_lease_type_from_fmode(fmode);
3528 static long ll_file_set_lease(struct file *file, struct ll_ioc_lease *ioc,
3531 struct inode *inode = file_inode(file);
3532 struct ll_inode_info *lli = ll_i2info(inode);
3533 struct ll_file_data *fd = file->private_data;
3534 struct obd_client_handle *och = NULL;
3535 __u64 open_flags = 0;
3541 switch (ioc->lil_mode) {
3542 case LL_LEASE_WRLCK:
3543 if (!(file->f_mode & FMODE_WRITE))
3545 fmode = FMODE_WRITE;
3547 case LL_LEASE_RDLCK:
3548 if (!(file->f_mode & FMODE_READ))
3552 case LL_LEASE_UNLCK:
3553 RETURN(ll_file_unlock_lease(file, ioc, arg));
3558 CDEBUG(D_INODE, "Set lease with mode %u\n", fmode);
3560 /* apply for lease */
3561 if (ioc->lil_flags & LL_LEASE_RESYNC)
3562 open_flags = MDS_OPEN_RESYNC;
3563 och = ll_lease_open(inode, file, fmode, open_flags);
3565 RETURN(PTR_ERR(och));
3567 if (ioc->lil_flags & LL_LEASE_RESYNC) {
3568 rc = ll_lease_file_resync(och, inode, arg);
3570 ll_lease_close(och, inode, NULL);
3573 rc = ll_layout_refresh(inode, &fd->fd_layout_version);
3575 ll_lease_close(och, inode, NULL);
3581 mutex_lock(&lli->lli_och_mutex);
3582 if (fd->fd_lease_och == NULL) {
3583 fd->fd_lease_och = och;
3586 mutex_unlock(&lli->lli_och_mutex);
3588 /* impossible now that only excl is supported for now */
3589 ll_lease_close(och, inode, &lease_broken);
3595 static void ll_heat_get(struct inode *inode, struct lu_heat *heat)
3597 struct ll_inode_info *lli = ll_i2info(inode);
3598 struct ll_sb_info *sbi = ll_i2sbi(inode);
3599 __u64 now = ktime_get_real_seconds();
3602 spin_lock(&lli->lli_heat_lock);
3603 heat->lh_flags = lli->lli_heat_flags;
3604 for (i = 0; i < heat->lh_count; i++)
3605 heat->lh_heat[i] = obd_heat_get(&lli->lli_heat_instances[i],
3606 now, sbi->ll_heat_decay_weight,
3607 sbi->ll_heat_period_second);
3608 spin_unlock(&lli->lli_heat_lock);
3611 static int ll_heat_set(struct inode *inode, enum lu_heat_flag flags)
3613 struct ll_inode_info *lli = ll_i2info(inode);
3616 spin_lock(&lli->lli_heat_lock);
3617 if (flags & LU_HEAT_FLAG_CLEAR)
3618 obd_heat_clear(lli->lli_heat_instances, OBD_HEAT_COUNT);
3620 if (flags & LU_HEAT_FLAG_OFF)
3621 lli->lli_heat_flags |= LU_HEAT_FLAG_OFF;
3623 lli->lli_heat_flags &= ~LU_HEAT_FLAG_OFF;
3625 spin_unlock(&lli->lli_heat_lock);
3631 ll_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3633 struct inode *inode = file_inode(file);
3634 struct ll_file_data *fd = file->private_data;
3638 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%x\n",
3639 PFID(ll_inode2fid(inode)), inode, cmd);
3640 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
3642 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
3643 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
3647 case LL_IOC_GETFLAGS:
3648 /* Get the current value of the file flags */
3649 return put_user(fd->fd_flags, (int __user *)arg);
3650 case LL_IOC_SETFLAGS:
3651 case LL_IOC_CLRFLAGS:
3652 /* Set or clear specific file flags */
3653 /* XXX This probably needs checks to ensure the flags are
3654 * not abused, and to handle any flag side effects.
3656 if (get_user(flags, (int __user *) arg))
3659 if (cmd == LL_IOC_SETFLAGS) {
3660 if ((flags & LL_FILE_IGNORE_LOCK) &&
3661 !(file->f_flags & O_DIRECT)) {
3662 CERROR("%s: unable to disable locking on "
3663 "non-O_DIRECT file\n", current->comm);
3667 fd->fd_flags |= flags;
3669 fd->fd_flags &= ~flags;
3672 case LL_IOC_LOV_SETSTRIPE:
3673 case LL_IOC_LOV_SETSTRIPE_NEW:
3674 RETURN(ll_lov_setstripe(inode, file, (void __user *)arg));
3675 case LL_IOC_LOV_SETEA:
3676 RETURN(ll_lov_setea(inode, file, (void __user *)arg));
3677 case LL_IOC_LOV_SWAP_LAYOUTS: {
3679 struct lustre_swap_layouts lsl;
3681 if (copy_from_user(&lsl, (char __user *)arg,
3682 sizeof(struct lustre_swap_layouts)))
3685 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
3688 file2 = fget(lsl.sl_fd);
3692 /* O_WRONLY or O_RDWR */
3693 if ((file2->f_flags & O_ACCMODE) == O_RDONLY)
3694 GOTO(out, rc = -EPERM);
3696 if (lsl.sl_flags & SWAP_LAYOUTS_CLOSE) {
3697 struct inode *inode2;
3698 struct ll_inode_info *lli;
3699 struct obd_client_handle *och = NULL;
3701 lli = ll_i2info(inode);
3702 mutex_lock(&lli->lli_och_mutex);
3703 if (fd->fd_lease_och != NULL) {
3704 och = fd->fd_lease_och;
3705 fd->fd_lease_och = NULL;
3707 mutex_unlock(&lli->lli_och_mutex);
3709 GOTO(out, rc = -ENOLCK);
3710 inode2 = file_inode(file2);
3711 rc = ll_swap_layouts_close(och, inode, inode2);
3713 rc = ll_swap_layouts(file, file2, &lsl);
3719 case LL_IOC_LOV_GETSTRIPE:
3720 case LL_IOC_LOV_GETSTRIPE_NEW:
3721 RETURN(ll_file_getstripe(inode, (void __user *)arg, 0));
3722 case FS_IOC_GETFLAGS:
3723 case FS_IOC_SETFLAGS:
3724 RETURN(ll_iocontrol(inode, file, cmd, arg));
3725 case FSFILT_IOC_GETVERSION:
3726 case FS_IOC_GETVERSION:
3727 RETURN(put_user(inode->i_generation, (int __user *)arg));
3728 /* We need to special case any other ioctls we want to handle,
3729 * to send them to the MDS/OST as appropriate and to properly
3730 * network encode the arg field. */
3731 case FS_IOC_SETVERSION:
3734 case LL_IOC_GROUP_LOCK:
3735 RETURN(ll_get_grouplock(inode, file, arg));
3736 case LL_IOC_GROUP_UNLOCK:
3737 RETURN(ll_put_grouplock(inode, file, arg));
3738 case IOC_OBD_STATFS:
3739 RETURN(ll_obd_statfs(inode, (void __user *)arg));
3741 case LL_IOC_FLUSHCTX:
3742 RETURN(ll_flush_ctx(inode));
3743 case LL_IOC_PATH2FID: {
3744 if (copy_to_user((void __user *)arg, ll_inode2fid(inode),
3745 sizeof(struct lu_fid)))
3750 case LL_IOC_GETPARENT:
3751 RETURN(ll_getparent(file, (struct getparent __user *)arg));
3753 case OBD_IOC_FID2PATH:
3754 RETURN(ll_fid2path(inode, (void __user *)arg));
3755 case LL_IOC_DATA_VERSION: {
3756 struct ioc_data_version idv;
3759 if (copy_from_user(&idv, (char __user *)arg, sizeof(idv)))
3762 idv.idv_flags &= LL_DV_RD_FLUSH | LL_DV_WR_FLUSH;
3763 rc = ll_ioc_data_version(inode, &idv);
3766 copy_to_user((char __user *)arg, &idv, sizeof(idv)))
3772 case LL_IOC_GET_MDTIDX: {
3775 mdtidx = ll_get_mdt_idx(inode);
3779 if (put_user((int)mdtidx, (int __user *)arg))
3784 case OBD_IOC_GETDTNAME:
3785 case OBD_IOC_GETMDNAME:
3786 RETURN(ll_get_obd_name(inode, cmd, arg));
3787 case LL_IOC_HSM_STATE_GET: {
3788 struct md_op_data *op_data;
3789 struct hsm_user_state *hus;
3796 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3797 LUSTRE_OPC_ANY, hus);
3798 if (IS_ERR(op_data)) {
3800 RETURN(PTR_ERR(op_data));
3803 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
3806 if (copy_to_user((void __user *)arg, hus, sizeof(*hus)))
3809 ll_finish_md_op_data(op_data);
3813 case LL_IOC_HSM_STATE_SET: {
3814 struct hsm_state_set *hss;
3821 if (copy_from_user(hss, (char __user *)arg, sizeof(*hss))) {
3826 rc = ll_hsm_state_set(inode, hss);
3831 case LL_IOC_HSM_ACTION: {
3832 struct md_op_data *op_data;
3833 struct hsm_current_action *hca;
3840 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
3841 LUSTRE_OPC_ANY, hca);
3842 if (IS_ERR(op_data)) {
3844 RETURN(PTR_ERR(op_data));
3847 rc = obd_iocontrol(cmd, ll_i2mdexp(inode), sizeof(*op_data),
3850 if (copy_to_user((char __user *)arg, hca, sizeof(*hca)))
3853 ll_finish_md_op_data(op_data);
3857 case LL_IOC_SET_LEASE_OLD: {
3858 struct ll_ioc_lease ioc = { .lil_mode = (__u32)arg };
3860 RETURN(ll_file_set_lease(file, &ioc, 0));
3862 case LL_IOC_SET_LEASE: {
3863 struct ll_ioc_lease ioc;
3865 if (copy_from_user(&ioc, (void __user *)arg, sizeof(ioc)))
3868 RETURN(ll_file_set_lease(file, &ioc, arg));
3870 case LL_IOC_GET_LEASE: {
3871 struct ll_inode_info *lli = ll_i2info(inode);
3872 struct ldlm_lock *lock = NULL;
3875 mutex_lock(&lli->lli_och_mutex);
3876 if (fd->fd_lease_och != NULL) {
3877 struct obd_client_handle *och = fd->fd_lease_och;
3879 lock = ldlm_handle2lock(&och->och_lease_handle);
3881 lock_res_and_lock(lock);
3882 if (!ldlm_is_cancel(lock))
3883 fmode = och->och_flags;
3885 unlock_res_and_lock(lock);
3886 LDLM_LOCK_PUT(lock);
3889 mutex_unlock(&lli->lli_och_mutex);
3891 RETURN(ll_lease_type_from_fmode(fmode));
3893 case LL_IOC_HSM_IMPORT: {
3894 struct hsm_user_import *hui;
3900 if (copy_from_user(hui, (void __user *)arg, sizeof(*hui))) {
3905 rc = ll_hsm_import(inode, file, hui);
3910 case LL_IOC_FUTIMES_3: {
3911 struct ll_futimes_3 lfu;
3913 if (copy_from_user(&lfu,
3914 (const struct ll_futimes_3 __user *)arg,
3918 RETURN(ll_file_futimes_3(file, &lfu));
3920 case LL_IOC_LADVISE: {
3921 struct llapi_ladvise_hdr *k_ladvise_hdr;
3922 struct llapi_ladvise_hdr __user *u_ladvise_hdr;
3925 int alloc_size = sizeof(*k_ladvise_hdr);
3928 u_ladvise_hdr = (void __user *)arg;
3929 OBD_ALLOC_PTR(k_ladvise_hdr);
3930 if (k_ladvise_hdr == NULL)
3933 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
3934 GOTO(out_ladvise, rc = -EFAULT);
3936 if (k_ladvise_hdr->lah_magic != LADVISE_MAGIC ||
3937 k_ladvise_hdr->lah_count < 1)
3938 GOTO(out_ladvise, rc = -EINVAL);
3940 num_advise = k_ladvise_hdr->lah_count;
3941 if (num_advise >= LAH_COUNT_MAX)
3942 GOTO(out_ladvise, rc = -EFBIG);
3944 OBD_FREE_PTR(k_ladvise_hdr);
3945 alloc_size = offsetof(typeof(*k_ladvise_hdr),
3946 lah_advise[num_advise]);
3947 OBD_ALLOC(k_ladvise_hdr, alloc_size);
3948 if (k_ladvise_hdr == NULL)
3952 * TODO: submit multiple advices to one server in a single RPC
3954 if (copy_from_user(k_ladvise_hdr, u_ladvise_hdr, alloc_size))
3955 GOTO(out_ladvise, rc = -EFAULT);
3957 for (i = 0; i < num_advise; i++) {
3958 struct llapi_lu_ladvise *k_ladvise =
3959 &k_ladvise_hdr->lah_advise[i];
3960 struct llapi_lu_ladvise __user *u_ladvise =
3961 &u_ladvise_hdr->lah_advise[i];
3963 rc = ll_ladvise_sanity(inode, k_ladvise);
3965 GOTO(out_ladvise, rc);
3967 switch (k_ladvise->lla_advice) {
3968 case LU_LADVISE_LOCKNOEXPAND:
3969 rc = ll_lock_noexpand(file,
3970 k_ladvise->lla_peradvice_flags);
3971 GOTO(out_ladvise, rc);
3972 case LU_LADVISE_LOCKAHEAD:
3974 rc = ll_file_lock_ahead(file, k_ladvise);
3977 GOTO(out_ladvise, rc);
3980 &u_ladvise->lla_lockahead_result))
3981 GOTO(out_ladvise, rc = -EFAULT);
3984 rc = ll_ladvise(inode, file,
3985 k_ladvise_hdr->lah_flags,
3988 GOTO(out_ladvise, rc);
3995 OBD_FREE(k_ladvise_hdr, alloc_size);
3998 case LL_IOC_FLR_SET_MIRROR: {
3999 /* mirror I/O must be direct to avoid polluting page cache
4001 if (!(file->f_flags & O_DIRECT))
4004 fd->fd_designated_mirror = (__u32)arg;
4007 case FS_IOC_FSGETXATTR:
4008 RETURN(ll_ioctl_fsgetxattr(inode, cmd, arg));
4009 case FS_IOC_FSSETXATTR:
4010 RETURN(ll_ioctl_fssetxattr(inode, cmd, arg));
4012 RETURN(put_user(PAGE_SIZE, (int __user *)arg));
4013 case LL_IOC_HEAT_GET: {
4014 struct lu_heat uheat;
4015 struct lu_heat *heat;
4018 if (copy_from_user(&uheat, (void __user *)arg, sizeof(uheat)))
4021 if (uheat.lh_count > OBD_HEAT_COUNT)
4022 uheat.lh_count = OBD_HEAT_COUNT;
4024 size = offsetof(typeof(uheat), lh_heat[uheat.lh_count]);
4025 OBD_ALLOC(heat, size);
4029 heat->lh_count = uheat.lh_count;
4030 ll_heat_get(inode, heat);
4031 rc = copy_to_user((char __user *)arg, heat, size);
4032 OBD_FREE(heat, size);
4033 RETURN(rc ? -EFAULT : 0);
4035 case LL_IOC_HEAT_SET: {
4038 if (copy_from_user(&flags, (void __user *)arg, sizeof(flags)))
4041 rc = ll_heat_set(inode, flags);
4044 case LL_IOC_PCC_DETACH: {
4045 struct lu_pcc_detach *detach;
4047 OBD_ALLOC_PTR(detach);
4051 if (copy_from_user(detach,
4052 (const struct lu_pcc_detach __user *)arg,
4054 GOTO(out_detach_free, rc = -EFAULT);
4056 if (!S_ISREG(inode->i_mode))
4057 GOTO(out_detach_free, rc = -EINVAL);
4059 if (!inode_owner_or_capable(inode))
4060 GOTO(out_detach_free, rc = -EPERM);
4062 rc = pcc_ioctl_detach(inode, detach->pccd_opt);
4064 OBD_FREE_PTR(detach);
4067 case LL_IOC_PCC_STATE: {
4068 struct lu_pcc_state __user *ustate =
4069 (struct lu_pcc_state __user *)arg;
4070 struct lu_pcc_state *state;
4072 OBD_ALLOC_PTR(state);
4076 if (copy_from_user(state, ustate, sizeof(*state)))
4077 GOTO(out_state, rc = -EFAULT);
4079 rc = pcc_ioctl_state(file, inode, state);
4081 GOTO(out_state, rc);
4083 if (copy_to_user(ustate, state, sizeof(*state)))
4084 GOTO(out_state, rc = -EFAULT);
4087 OBD_FREE_PTR(state);
4090 #ifdef HAVE_LUSTRE_CRYPTO
4091 case LL_IOC_SET_ENCRYPTION_POLICY:
4092 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4094 return llcrypt_ioctl_set_policy(file, (const void __user *)arg);
4095 case LL_IOC_GET_ENCRYPTION_POLICY_EX:
4096 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4098 return llcrypt_ioctl_get_policy_ex(file, (void __user *)arg);
4099 case LL_IOC_ADD_ENCRYPTION_KEY:
4100 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4102 return llcrypt_ioctl_add_key(file, (void __user *)arg);
4103 case LL_IOC_REMOVE_ENCRYPTION_KEY:
4104 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4106 return llcrypt_ioctl_remove_key(file, (void __user *)arg);
4107 case LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
4108 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4110 return llcrypt_ioctl_remove_key_all_users(file,
4111 (void __user *)arg);
4112 case LL_IOC_GET_ENCRYPTION_KEY_STATUS:
4113 if (!ll_sbi_has_encrypt(ll_i2sbi(inode)))
4115 return llcrypt_ioctl_get_key_status(file, (void __user *)arg);
4118 case LL_IOC_UNLOCK_FOREIGN: {
4119 struct dentry *dentry = file_dentry(file);
4121 /* if not a foreign symlink do nothing */
4122 if (ll_foreign_is_removable(dentry, true)) {
4124 "prevent unlink of non-foreign file ("DFID")\n",
4125 PFID(ll_inode2fid(inode)));
4126 RETURN(-EOPNOTSUPP);
4132 RETURN(obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
4133 (void __user *)arg));
4137 loff_t ll_lseek(struct file *file, loff_t offset, int whence)
4139 struct inode *inode = file_inode(file);
4142 struct cl_lseek_io *lsio;
4149 env = cl_env_get(&refcheck);
4151 RETURN(PTR_ERR(env));
4153 io = vvp_env_thread_io(env);
4154 io->ci_obj = ll_i2info(inode)->lli_clob;
4155 ll_io_set_mirror(io, file);
4157 lsio = &io->u.ci_lseek;
4158 lsio->ls_start = offset;
4159 lsio->ls_whence = whence;
4160 lsio->ls_result = -ENXIO;
4163 rc = cl_io_init(env, io, CIT_LSEEK, io->ci_obj);
4165 struct vvp_io *vio = vvp_env_io(env);
4167 vio->vui_fd = file->private_data;
4168 rc = cl_io_loop(env, io);
4172 retval = rc ? : lsio->ls_result;
4173 cl_io_fini(env, io);
4174 } while (unlikely(io->ci_need_restart));
4176 cl_env_put(env, &refcheck);
4181 static loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
4183 struct inode *inode = file_inode(file);
4184 loff_t retval = offset, eof = 0;
4185 ktime_t kstart = ktime_get();
4189 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), to=%llu=%#llx(%d)\n",
4190 PFID(ll_inode2fid(inode)), inode, retval, retval,
4193 if (origin == SEEK_END) {
4194 retval = ll_glimpse_size(inode);
4197 eof = i_size_read(inode);
4200 if (origin == SEEK_HOLE || origin == SEEK_DATA) {
4204 /* flush local cache first if any */
4205 cl_sync_file_range(inode, offset, OBD_OBJECT_EOF,
4208 retval = ll_lseek(file, offset, origin);
4211 retval = vfs_setpos(file, retval, ll_file_maxbytes(inode));
4213 retval = generic_file_llseek_size(file, offset, origin,
4214 ll_file_maxbytes(inode), eof);
4217 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_LLSEEK,
4218 ktime_us_delta(ktime_get(), kstart));
4222 static int ll_flush(struct file *file, fl_owner_t id)
4224 struct inode *inode = file_inode(file);
4225 struct ll_inode_info *lli = ll_i2info(inode);
4226 struct ll_file_data *fd = file->private_data;
4229 LASSERT(!S_ISDIR(inode->i_mode));
4231 /* catch async errors that were recorded back when async writeback
4232 * failed for pages in this mapping. */
4233 rc = lli->lli_async_rc;
4234 lli->lli_async_rc = 0;
4235 if (lli->lli_clob != NULL) {
4236 err = lov_read_and_clear_async_rc(lli->lli_clob);
4241 /* The application has been told write failure already.
4242 * Do not report failure again. */
4243 if (fd->fd_write_failed)
4245 return rc ? -EIO : 0;
4249 * Called to make sure a portion of file has been written out.
4250 * if @mode is not CL_FSYNC_LOCAL, it will send OST_SYNC RPCs to OST.
4252 * Return how many pages have been written.
4254 int cl_sync_file_range(struct inode *inode, loff_t start, loff_t end,
4255 enum cl_fsync_mode mode, int ignore_layout)
4259 struct cl_fsync_io *fio;
4264 if (mode != CL_FSYNC_NONE && mode != CL_FSYNC_LOCAL &&
4265 mode != CL_FSYNC_DISCARD && mode != CL_FSYNC_ALL)
4268 env = cl_env_get(&refcheck);
4270 RETURN(PTR_ERR(env));
4272 io = vvp_env_thread_io(env);
4273 io->ci_obj = ll_i2info(inode)->lli_clob;
4274 io->ci_ignore_layout = ignore_layout;
4276 /* initialize parameters for sync */
4277 fio = &io->u.ci_fsync;
4278 fio->fi_start = start;
4280 fio->fi_fid = ll_inode2fid(inode);
4281 fio->fi_mode = mode;
4282 fio->fi_nr_written = 0;
4284 if (cl_io_init(env, io, CIT_FSYNC, io->ci_obj) == 0)
4285 result = cl_io_loop(env, io);
4287 result = io->ci_result;
4289 result = fio->fi_nr_written;
4290 cl_io_fini(env, io);
4291 cl_env_put(env, &refcheck);
4297 * When dentry is provided (the 'else' case), file_dentry() may be
4298 * null and dentry must be used directly rather than pulled from
4299 * file_dentry() as is done otherwise.
4302 int ll_fsync(struct file *file, loff_t start, loff_t end, int datasync)
4304 struct dentry *dentry = file_dentry(file);
4305 struct inode *inode = dentry->d_inode;
4306 struct ll_inode_info *lli = ll_i2info(inode);
4307 struct ptlrpc_request *req;
4308 ktime_t kstart = ktime_get();
4314 "VFS Op:inode="DFID"(%p), start %lld, end %lld, datasync %d\n",
4315 PFID(ll_inode2fid(inode)), inode, start, end, datasync);
4317 /* fsync's caller has already called _fdata{sync,write}, we want
4318 * that IO to finish before calling the osc and mdc sync methods */
4319 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
4322 /* catch async errors that were recorded back when async writeback
4323 * failed for pages in this mapping. */
4324 if (!S_ISDIR(inode->i_mode)) {
4325 err = lli->lli_async_rc;
4326 lli->lli_async_rc = 0;
4329 if (lli->lli_clob != NULL) {
4330 err = lov_read_and_clear_async_rc(lli->lli_clob);
4336 err = md_fsync(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), &req);
4340 ptlrpc_req_finished(req);
4342 if (S_ISREG(inode->i_mode)) {
4343 struct ll_file_data *fd = file->private_data;
4346 /* Sync metadata on MDT first, and then sync the cached data
4349 err = pcc_fsync(file, start, end, datasync, &cached);
4351 err = cl_sync_file_range(inode, start, end,
4353 if (rc == 0 && err < 0)
4356 fd->fd_write_failed = true;
4358 fd->fd_write_failed = false;
4361 inode_unlock(inode);
4364 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FSYNC,
4365 ktime_us_delta(ktime_get(), kstart));
4370 ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
4372 struct inode *inode = file_inode(file);
4373 struct ll_sb_info *sbi = ll_i2sbi(inode);
4374 struct ldlm_enqueue_info einfo = {
4375 .ei_type = LDLM_FLOCK,
4376 .ei_cb_cp = ldlm_flock_completion_ast,
4377 .ei_cbdata = file_lock,
4379 struct md_op_data *op_data;
4380 struct lustre_handle lockh = { 0 };
4381 union ldlm_policy_data flock = { { 0 } };
4382 int fl_type = file_lock->fl_type;
4383 ktime_t kstart = ktime_get();
4389 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID" file_lock=%p\n",
4390 PFID(ll_inode2fid(inode)), file_lock);
4392 if (file_lock->fl_flags & FL_FLOCK) {
4393 LASSERT((cmd == F_SETLKW) || (cmd == F_SETLK));
4394 /* flocks are whole-file locks */
4395 flock.l_flock.end = OFFSET_MAX;
4396 /* For flocks owner is determined by the local file desctiptor*/
4397 flock.l_flock.owner = (unsigned long)file_lock->fl_file;
4398 } else if (file_lock->fl_flags & FL_POSIX) {
4399 flock.l_flock.owner = (unsigned long)file_lock->fl_owner;
4400 flock.l_flock.start = file_lock->fl_start;
4401 flock.l_flock.end = file_lock->fl_end;
4405 flock.l_flock.pid = file_lock->fl_pid;
4407 #if defined(HAVE_LM_COMPARE_OWNER) || defined(lm_compare_owner)
4408 /* Somewhat ugly workaround for svc lockd.
4409 * lockd installs custom fl_lmops->lm_compare_owner that checks
4410 * for the fl_owner to be the same (which it always is on local node
4411 * I guess between lockd processes) and then compares pid.
4412 * As such we assign pid to the owner field to make it all work,
4413 * conflict with normal locks is unlikely since pid space and
4414 * pointer space for current->files are not intersecting */
4415 if (file_lock->fl_lmops && file_lock->fl_lmops->lm_compare_owner)
4416 flock.l_flock.owner = (unsigned long)file_lock->fl_pid;
4421 einfo.ei_mode = LCK_PR;
4424 /* An unlock request may or may not have any relation to
4425 * existing locks so we may not be able to pass a lock handle
4426 * via a normal ldlm_lock_cancel() request. The request may even
4427 * unlock a byte range in the middle of an existing lock. In
4428 * order to process an unlock request we need all of the same
4429 * information that is given with a normal read or write record
4430 * lock request. To avoid creating another ldlm unlock (cancel)
4431 * message we'll treat a LCK_NL flock request as an unlock. */
4432 einfo.ei_mode = LCK_NL;
4435 einfo.ei_mode = LCK_PW;
4438 CDEBUG(D_INFO, "Unknown fcntl lock type: %d\n", fl_type);
4453 flags = LDLM_FL_BLOCK_NOWAIT;
4459 flags = LDLM_FL_TEST_LOCK;
4462 CERROR("unknown fcntl lock command: %d\n", cmd);
4466 /* Save the old mode so that if the mode in the lock changes we
4467 * can decrement the appropriate reader or writer refcount. */
4468 file_lock->fl_type = einfo.ei_mode;
4470 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
4471 LUSTRE_OPC_ANY, NULL);
4472 if (IS_ERR(op_data))
4473 RETURN(PTR_ERR(op_data));
4475 CDEBUG(D_DLMTRACE, "inode="DFID", pid=%u, flags=%#llx, mode=%u, "
4476 "start=%llu, end=%llu\n", PFID(ll_inode2fid(inode)),
4477 flock.l_flock.pid, flags, einfo.ei_mode,
4478 flock.l_flock.start, flock.l_flock.end);
4480 rc = md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data, &lockh,
4483 /* Restore the file lock type if not TEST lock. */
4484 if (!(flags & LDLM_FL_TEST_LOCK))
4485 file_lock->fl_type = fl_type;
4487 #ifdef HAVE_LOCKS_LOCK_FILE_WAIT
4488 if ((rc == 0 || file_lock->fl_type == F_UNLCK) &&
4489 !(flags & LDLM_FL_TEST_LOCK))
4490 rc2 = locks_lock_file_wait(file, file_lock);
4492 if ((file_lock->fl_flags & FL_FLOCK) &&
4493 (rc == 0 || file_lock->fl_type == F_UNLCK))
4494 rc2 = flock_lock_file_wait(file, file_lock);
4495 if ((file_lock->fl_flags & FL_POSIX) &&
4496 (rc == 0 || file_lock->fl_type == F_UNLCK) &&
4497 !(flags & LDLM_FL_TEST_LOCK))
4498 rc2 = posix_lock_file_wait(file, file_lock);
4499 #endif /* HAVE_LOCKS_LOCK_FILE_WAIT */
4501 if (rc2 && file_lock->fl_type != F_UNLCK) {
4502 einfo.ei_mode = LCK_NL;
4503 md_enqueue(sbi->ll_md_exp, &einfo, &flock, op_data,
4508 ll_finish_md_op_data(op_data);
4511 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FLOCK,
4512 ktime_us_delta(ktime_get(), kstart));
4516 int ll_get_fid_by_name(struct inode *parent, const char *name,
4517 int namelen, struct lu_fid *fid,
4518 struct inode **inode)
4520 struct md_op_data *op_data = NULL;
4521 struct mdt_body *body;
4522 struct ptlrpc_request *req;
4526 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen, 0,
4527 LUSTRE_OPC_ANY, NULL);
4528 if (IS_ERR(op_data))
4529 RETURN(PTR_ERR(op_data));
4531 op_data->op_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
4532 rc = md_getattr_name(ll_i2sbi(parent)->ll_md_exp, op_data, &req);
4533 ll_finish_md_op_data(op_data);
4537 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
4539 GOTO(out_req, rc = -EFAULT);
4541 *fid = body->mbo_fid1;
4544 rc = ll_prep_inode(inode, req, parent->i_sb, NULL);
4546 ptlrpc_req_finished(req);
4550 int ll_migrate(struct inode *parent, struct file *file, struct lmv_user_md *lum,
4553 struct dentry *dchild = NULL;
4554 struct inode *child_inode = NULL;
4555 struct md_op_data *op_data;
4556 struct ptlrpc_request *request = NULL;
4557 struct obd_client_handle *och = NULL;
4559 struct mdt_body *body;
4560 __u64 data_version = 0;
4561 size_t namelen = strlen(name);
4562 int lumlen = lmv_user_md_size(lum->lum_stripe_count, lum->lum_magic);
4566 CDEBUG(D_VFSTRACE, "migrate "DFID"/%s to MDT%04x stripe count %d\n",
4567 PFID(ll_inode2fid(parent)), name,
4568 lum->lum_stripe_offset, lum->lum_stripe_count);
4570 if (lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC) &&
4571 lum->lum_magic != cpu_to_le32(LMV_USER_MAGIC_SPECIFIC))
4572 lustre_swab_lmv_user_md(lum);
4574 /* Get child FID first */
4575 qstr.hash = ll_full_name_hash(file_dentry(file), name, namelen);
4578 dchild = d_lookup(file_dentry(file), &qstr);
4580 if (dchild->d_inode)
4581 child_inode = igrab(dchild->d_inode);
4586 rc = ll_get_fid_by_name(parent, name, namelen, NULL,
4595 if (!(exp_connect_flags2(ll_i2sbi(parent)->ll_md_exp) &
4596 OBD_CONNECT2_DIR_MIGRATE)) {
4597 if (le32_to_cpu(lum->lum_stripe_count) > 1 ||
4598 ll_dir_striped(child_inode)) {
4599 CERROR("%s: MDT doesn't support stripe directory "
4600 "migration!\n", ll_i2sbi(parent)->ll_fsname);
4601 GOTO(out_iput, rc = -EOPNOTSUPP);
4606 * lfs migrate command needs to be blocked on the client
4607 * by checking the migrate FID against the FID of the
4610 if (is_root_inode(child_inode))
4611 GOTO(out_iput, rc = -EINVAL);
4613 if (IS_ENCRYPTED(child_inode)) {
4614 rc = llcrypt_get_encryption_info(child_inode);
4617 if (!llcrypt_has_encryption_key(child_inode)) {
4618 CDEBUG(D_SEC, "no enc key for "DFID"\n",
4619 PFID(ll_inode2fid(child_inode)));
4620 GOTO(out_iput, rc = -ENOKEY);
4624 op_data = ll_prep_md_op_data(NULL, parent, NULL, name, namelen,
4625 child_inode->i_mode, LUSTRE_OPC_ANY, NULL);
4626 if (IS_ERR(op_data))
4627 GOTO(out_iput, rc = PTR_ERR(op_data));
4629 inode_lock(child_inode);
4630 op_data->op_fid3 = *ll_inode2fid(child_inode);
4631 if (!fid_is_sane(&op_data->op_fid3)) {
4632 CERROR("%s: migrate %s, but FID "DFID" is insane\n",
4633 ll_i2sbi(parent)->ll_fsname, name,
4634 PFID(&op_data->op_fid3));
4635 GOTO(out_unlock, rc = -EINVAL);
4638 op_data->op_cli_flags |= CLI_MIGRATE | CLI_SET_MEA;
4639 op_data->op_data = lum;
4640 op_data->op_data_size = lumlen;
4643 if (S_ISREG(child_inode->i_mode)) {
4644 och = ll_lease_open(child_inode, NULL, FMODE_WRITE, 0);
4648 GOTO(out_unlock, rc);
4651 rc = ll_data_version(child_inode, &data_version,
4654 GOTO(out_close, rc);
4656 op_data->op_open_handle = och->och_open_handle;
4657 op_data->op_data_version = data_version;
4658 op_data->op_lease_handle = och->och_lease_handle;
4659 op_data->op_bias |= MDS_CLOSE_MIGRATE;
4661 spin_lock(&och->och_mod->mod_open_req->rq_lock);
4662 och->och_mod->mod_open_req->rq_replay = 0;
4663 spin_unlock(&och->och_mod->mod_open_req->rq_lock);
4666 rc = md_rename(ll_i2sbi(parent)->ll_md_exp, op_data, name, namelen,
4667 name, namelen, &request);
4669 LASSERT(request != NULL);
4670 ll_update_times(request, parent);
4673 if (rc == 0 || rc == -EAGAIN) {
4674 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
4675 LASSERT(body != NULL);
4677 /* If the server does release layout lock, then we cleanup
4678 * the client och here, otherwise release it in out_close: */
4679 if (och && body->mbo_valid & OBD_MD_CLOSE_INTENT_EXECED) {
4680 obd_mod_put(och->och_mod);
4681 md_clear_open_replay_data(ll_i2sbi(parent)->ll_md_exp,
4683 och->och_open_handle.cookie = DEAD_HANDLE_MAGIC;
4689 if (request != NULL) {
4690 ptlrpc_req_finished(request);
4694 /* Try again if the lease has cancelled. */
4695 if (rc == -EAGAIN && S_ISREG(child_inode->i_mode))
4700 ll_lease_close(och, child_inode, NULL);
4702 clear_nlink(child_inode);
4704 inode_unlock(child_inode);
4705 ll_finish_md_op_data(op_data);
4712 ll_file_noflock(struct file *file, int cmd, struct file_lock *file_lock)
4714 struct ll_file_data *fd = file->private_data;
4718 * In order to avoid flood of warning messages, only print one message
4719 * for one file. And the entire message rate on the client is limited
4720 * by CDEBUG_LIMIT too.
4722 if (!(fd->fd_flags & LL_FILE_FLOCK_WARNING)) {
4723 fd->fd_flags |= LL_FILE_FLOCK_WARNING;
4724 CDEBUG_LIMIT(D_TTY | D_CONSOLE,
4725 "flock disabled, mount with '-o [local]flock' to enable\r\n");
4731 * test if some locks matching bits and l_req_mode are acquired
4732 * - bits can be in different locks
4733 * - if found clear the common lock bits in *bits
4734 * - the bits not found, are kept in *bits
4736 * \param bits [IN] searched lock bits [IN]
4737 * \param l_req_mode [IN] searched lock mode
4738 * \retval boolean, true iff all bits are found
4740 int ll_have_md_lock(struct inode *inode, __u64 *bits, enum ldlm_mode l_req_mode)
4742 struct lustre_handle lockh;
4743 union ldlm_policy_data policy;
4744 enum ldlm_mode mode = (l_req_mode == LCK_MINMODE) ?
4745 (LCK_CR | LCK_CW | LCK_PR | LCK_PW) : l_req_mode;
4754 fid = &ll_i2info(inode)->lli_fid;
4755 CDEBUG(D_INFO, "trying to match res "DFID" mode %s\n", PFID(fid),
4756 ldlm_lockname[mode]);
4758 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING | LDLM_FL_TEST_LOCK;
4759 for (i = 0; i < MDS_INODELOCK_NUMBITS && *bits != 0; i++) {
4760 policy.l_inodebits.bits = *bits & BIT(i);
4761 if (policy.l_inodebits.bits == 0)
4764 if (md_lock_match(ll_i2mdexp(inode), flags, fid, LDLM_IBITS,
4765 &policy, mode, &lockh)) {
4766 struct ldlm_lock *lock;
4768 lock = ldlm_handle2lock(&lockh);
4771 ~(lock->l_policy_data.l_inodebits.bits);
4772 LDLM_LOCK_PUT(lock);
4774 *bits &= ~policy.l_inodebits.bits;
4781 enum ldlm_mode ll_take_md_lock(struct inode *inode, __u64 bits,
4782 struct lustre_handle *lockh, __u64 flags,
4783 enum ldlm_mode mode)
4785 union ldlm_policy_data policy = { .l_inodebits = { bits } };
4790 fid = &ll_i2info(inode)->lli_fid;
4791 CDEBUG(D_INFO, "trying to match res "DFID"\n", PFID(fid));
4793 rc = md_lock_match(ll_i2mdexp(inode), LDLM_FL_BLOCK_GRANTED|flags,
4794 fid, LDLM_IBITS, &policy, mode, lockh);
4799 static int ll_inode_revalidate_fini(struct inode *inode, int rc)
4801 /* Already unlinked. Just update nlink and return success */
4802 if (rc == -ENOENT) {
4804 /* If it is striped directory, and there is bad stripe
4805 * Let's revalidate the dentry again, instead of returning
4807 if (ll_dir_striped(inode))
4810 /* This path cannot be hit for regular files unless in
4811 * case of obscure races, so no need to to validate
4813 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
4815 } else if (rc != 0) {
4816 CDEBUG_LIMIT((rc == -EACCES || rc == -EIDRM) ? D_INFO : D_ERROR,
4817 "%s: revalidate FID "DFID" error: rc = %d\n",
4818 ll_i2sbi(inode)->ll_fsname,
4819 PFID(ll_inode2fid(inode)), rc);
4825 static int ll_inode_revalidate(struct dentry *dentry, enum ldlm_intent_flags op)
4827 struct inode *parent;
4828 struct inode *inode = dentry->d_inode;
4829 struct obd_export *exp = ll_i2mdexp(inode);
4830 struct lookup_intent oit = {
4833 struct ptlrpc_request *req = NULL;
4834 struct md_op_data *op_data;
4835 const char *name = NULL;
4840 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p),name=%s\n",
4841 PFID(ll_inode2fid(inode)), inode, dentry->d_name.name);
4843 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID) {
4844 parent = dentry->d_parent->d_inode;
4845 name = dentry->d_name.name;
4846 namelen = dentry->d_name.len;
4851 op_data = ll_prep_md_op_data(NULL, parent, inode, name, namelen, 0,
4852 LUSTRE_OPC_ANY, NULL);
4853 if (IS_ERR(op_data))
4854 RETURN(PTR_ERR(op_data));
4856 /* Call getattr by fid */
4857 if (exp_connect_flags2(exp) & OBD_CONNECT2_GETATTR_PFID)
4858 op_data->op_flags = MF_GETATTR_BY_FID;
4859 rc = md_intent_lock(exp, op_data, &oit, &req, &ll_md_blocking_ast, 0);
4860 ll_finish_md_op_data(op_data);
4862 rc = ll_inode_revalidate_fini(inode, rc);
4866 rc = ll_revalidate_it_finish(req, &oit, dentry);
4868 ll_intent_release(&oit);
4872 /* Unlinked? Unhash dentry, so it is not picked up later by
4873 * do_lookup() -> ll_revalidate_it(). We cannot use d_drop
4874 * here to preserve get_cwd functionality on 2.6.
4876 if (!dentry->d_inode->i_nlink)
4877 d_lustre_invalidate(dentry);
4879 ll_lookup_finish_locks(&oit, dentry);
4881 ptlrpc_req_finished(req);
4886 static int ll_merge_md_attr(struct inode *inode)
4888 struct ll_inode_info *lli = ll_i2info(inode);
4889 struct cl_attr attr = { 0 };
4892 LASSERT(lli->lli_lsm_md != NULL);
4894 if (!lmv_dir_striped(lli->lli_lsm_md))
4897 down_read(&lli->lli_lsm_sem);
4898 rc = md_merge_attr(ll_i2mdexp(inode), ll_i2info(inode)->lli_lsm_md,
4899 &attr, ll_md_blocking_ast);
4900 up_read(&lli->lli_lsm_sem);
4904 set_nlink(inode, attr.cat_nlink);
4905 inode->i_blocks = attr.cat_blocks;
4906 i_size_write(inode, attr.cat_size);
4908 ll_i2info(inode)->lli_atime = attr.cat_atime;
4909 ll_i2info(inode)->lli_mtime = attr.cat_mtime;
4910 ll_i2info(inode)->lli_ctime = attr.cat_ctime;
4915 int ll_getattr_dentry(struct dentry *de, struct kstat *stat, u32 request_mask,
4916 unsigned int flags, bool foreign)
4918 struct inode *inode = de->d_inode;
4919 struct ll_sb_info *sbi = ll_i2sbi(inode);
4920 struct ll_inode_info *lli = ll_i2info(inode);
4921 struct inode *dir = de->d_parent->d_inode;
4922 bool need_glimpse = true;
4923 ktime_t kstart = ktime_get();
4926 /* The OST object(s) determine the file size, blocks and mtime. */
4927 if (!(request_mask & STATX_SIZE || request_mask & STATX_BLOCKS ||
4928 request_mask & STATX_MTIME))
4929 need_glimpse = false;
4931 if (dentry_may_statahead(dir, de))
4932 ll_start_statahead(dir, de, need_glimpse &&
4933 !(flags & AT_STATX_DONT_SYNC));
4935 if (flags & AT_STATX_DONT_SYNC)
4936 GOTO(fill_attr, rc = 0);
4938 rc = ll_inode_revalidate(de, IT_GETATTR);
4942 /* foreign file/dir are always of zero length, so don't
4943 * need to validate size.
4945 if (S_ISREG(inode->i_mode) && !foreign) {
4949 GOTO(fill_attr, rc);
4951 rc = pcc_inode_getattr(inode, request_mask, flags, &cached);
4952 if (cached && rc < 0)
4956 GOTO(fill_attr, rc);
4959 * If the returned attr is masked with OBD_MD_FLSIZE &
4960 * OBD_MD_FLBLOCKS & OBD_MD_FLMTIME, it means that the file size
4961 * or blocks obtained from MDT is strictly correct, and the file
4962 * is usually not being modified by clients, and the [a|m|c]time
4963 * got from MDT is also strictly correct.
4964 * Under this circumstance, it does not need to send glimpse
4965 * RPCs to OSTs for file attributes such as the size and blocks.
4967 if (lli->lli_attr_valid & OBD_MD_FLSIZE &&
4968 lli->lli_attr_valid & OBD_MD_FLBLOCKS &&
4969 lli->lli_attr_valid & OBD_MD_FLMTIME) {
4970 inode->i_mtime.tv_sec = lli->lli_mtime;
4971 if (lli->lli_attr_valid & OBD_MD_FLATIME)
4972 inode->i_atime.tv_sec = lli->lli_atime;
4973 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
4974 inode->i_ctime.tv_sec = lli->lli_ctime;
4975 GOTO(fill_attr, rc);
4978 /* In case of restore, the MDT has the right size and has
4979 * already send it back without granting the layout lock,
4980 * inode is up-to-date so glimpse is useless.
4981 * Also to glimpse we need the layout, in case of a running
4982 * restore the MDT holds the layout lock so the glimpse will
4983 * block up to the end of restore (getattr will block)
4985 if (!test_bit(LLIF_FILE_RESTORING, &lli->lli_flags)) {
4986 rc = ll_glimpse_size(inode);
4991 /* If object isn't regular a file then don't validate size. */
4992 /* foreign dir is not striped dir */
4993 if (ll_dir_striped(inode) && !foreign) {
4994 rc = ll_merge_md_attr(inode);
4999 if (lli->lli_attr_valid & OBD_MD_FLATIME)
5000 inode->i_atime.tv_sec = lli->lli_atime;
5001 if (lli->lli_attr_valid & OBD_MD_FLMTIME)
5002 inode->i_mtime.tv_sec = lli->lli_mtime;
5003 if (lli->lli_attr_valid & OBD_MD_FLCTIME)
5004 inode->i_ctime.tv_sec = lli->lli_ctime;
5008 OBD_FAIL_TIMEOUT(OBD_FAIL_GETATTR_DELAY, 30);
5010 if (ll_need_32bit_api(sbi)) {
5011 stat->ino = cl_fid_build_ino(&lli->lli_fid, 1);
5012 stat->dev = ll_compat_encode_dev(inode->i_sb->s_dev);
5013 stat->rdev = ll_compat_encode_dev(inode->i_rdev);
5015 stat->ino = inode->i_ino;
5016 stat->dev = inode->i_sb->s_dev;
5017 stat->rdev = inode->i_rdev;
5020 /* foreign symlink to be exposed as a real symlink */
5022 stat->mode = inode->i_mode;
5024 stat->mode = (inode->i_mode & ~S_IFMT) | S_IFLNK;
5026 stat->uid = inode->i_uid;
5027 stat->gid = inode->i_gid;
5028 stat->atime = inode->i_atime;
5029 stat->mtime = inode->i_mtime;
5030 stat->ctime = inode->i_ctime;
5031 /* stat->blksize is used to tell about preferred IO size */
5032 if (sbi->ll_stat_blksize)
5033 stat->blksize = sbi->ll_stat_blksize;
5034 else if (S_ISREG(inode->i_mode))
5035 stat->blksize = 1 << min(PTLRPC_MAX_BRW_BITS + 1,
5036 LL_MAX_BLKSIZE_BITS);
5038 stat->blksize = 1 << inode->i_sb->s_blocksize_bits;
5040 stat->nlink = inode->i_nlink;
5041 stat->size = i_size_read(inode);
5042 stat->blocks = inode->i_blocks;
5044 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5045 if (flags & AT_STATX_DONT_SYNC) {
5046 if (stat->size == 0 &&
5047 lli->lli_attr_valid & OBD_MD_FLLAZYSIZE)
5048 stat->size = lli->lli_lazysize;
5049 if (stat->blocks == 0 &&
5050 lli->lli_attr_valid & OBD_MD_FLLAZYBLOCKS)
5051 stat->blocks = lli->lli_lazyblocks;
5054 if (lli->lli_attr_valid & OBD_MD_FLBTIME) {
5055 stat->result_mask |= STATX_BTIME;
5056 stat->btime.tv_sec = lli->lli_btime;
5059 stat->attributes_mask = STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
5060 stat->attributes |= ll_inode_to_ext_flags(inode->i_flags);
5061 stat->result_mask &= request_mask;
5064 ll_stats_ops_tally(sbi, LPROC_LL_GETATTR,
5065 ktime_us_delta(ktime_get(), kstart));
5070 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
5071 int ll_getattr(const struct path *path, struct kstat *stat,
5072 u32 request_mask, unsigned int flags)
5074 return ll_getattr_dentry(path->dentry, stat, request_mask, flags,
5078 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
5080 return ll_getattr_dentry(de, stat, STATX_BASIC_STATS,
5081 AT_STATX_SYNC_AS_STAT, false);
5085 int cl_falloc(struct inode *inode, int mode, loff_t offset, loff_t len)
5090 int rc; loff_t sa_falloc_end;
5091 loff_t size = i_size_read(inode);
5095 env = cl_env_get(&refcheck);
5097 RETURN(PTR_ERR(env));
5099 io = vvp_env_thread_io(env);
5100 io->ci_obj = ll_i2info(inode)->lli_clob;
5101 io->ci_verify_layout = 1;
5102 io->u.ci_setattr.sa_parent_fid = lu_object_fid(&io->ci_obj->co_lu);
5103 io->u.ci_setattr.sa_falloc_mode = mode;
5104 io->u.ci_setattr.sa_falloc_offset = offset;
5105 io->u.ci_setattr.sa_falloc_len = len;
5106 io->u.ci_setattr.sa_falloc_end = io->u.ci_setattr.sa_falloc_offset +
5107 io->u.ci_setattr.sa_falloc_len;
5108 io->u.ci_setattr.sa_subtype = CL_SETATTR_FALLOCATE;
5109 sa_falloc_end = io->u.ci_setattr.sa_falloc_end;
5110 if (sa_falloc_end > size) {
5111 /* Check new size against VFS/VM file size limit and rlimit */
5112 rc = inode_newsize_ok(inode, sa_falloc_end);
5115 if (sa_falloc_end > ll_file_maxbytes(inode)) {
5116 CDEBUG(D_INODE, "file size too large %llu > %llu\n",
5117 (unsigned long long)(sa_falloc_end),
5118 ll_file_maxbytes(inode));
5122 io->u.ci_setattr.sa_attr.lvb_size = sa_falloc_end;
5123 if (!(mode & FALLOC_FL_KEEP_SIZE))
5124 io->u.ci_setattr.sa_avalid |= ATTR_SIZE;
5126 io->u.ci_setattr.sa_attr.lvb_size = size;
5130 if (cl_io_init(env, io, CIT_SETATTR, io->ci_obj) == 0)
5131 rc = cl_io_loop(env, io);
5135 cl_io_fini(env, io);
5136 if (unlikely(io->ci_need_restart))
5140 cl_env_put(env, &refcheck);
5144 long ll_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
5146 struct inode *inode = filp->f_path.dentry->d_inode;
5150 * Encrypted inodes can't handle collapse range or zero range or insert
5151 * range since we would need to re-encrypt blocks with a different IV or
5152 * XTS tweak (which are based on the logical block number).
5153 * Similar to what ext4 does.
5155 if (IS_ENCRYPTED(inode) &&
5156 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
5157 FALLOC_FL_ZERO_RANGE)))
5158 RETURN(-EOPNOTSUPP);
5161 * Only mode == 0 (which is standard prealloc) is supported now.
5162 * Punch is not supported yet.
5164 if (mode & ~FALLOC_FL_KEEP_SIZE)
5165 RETURN(-EOPNOTSUPP);
5167 ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_FALLOCATE, 1);
5169 rc = cl_falloc(inode, mode, offset, len);
5171 * ENOTSUPP (524) is an NFSv3 specific error code erroneously
5172 * used by Lustre in several places. Retuning it here would
5173 * confuse applications that explicity test for EOPNOTSUPP
5174 * (95) and fall back to ftruncate().
5176 if (rc == -ENOTSUPP)
5182 static int ll_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5183 __u64 start, __u64 len)
5187 struct fiemap *fiemap;
5188 unsigned int extent_count = fieinfo->fi_extents_max;
5190 num_bytes = sizeof(*fiemap) + (extent_count *
5191 sizeof(struct fiemap_extent));
5192 OBD_ALLOC_LARGE(fiemap, num_bytes);
5197 fiemap->fm_flags = fieinfo->fi_flags;
5198 fiemap->fm_extent_count = fieinfo->fi_extents_max;
5199 fiemap->fm_start = start;
5200 fiemap->fm_length = len;
5201 if (extent_count > 0 &&
5202 copy_from_user(&fiemap->fm_extents[0], fieinfo->fi_extents_start,
5203 sizeof(struct fiemap_extent)) != 0)
5204 GOTO(out, rc = -EFAULT);
5206 rc = ll_do_fiemap(inode, fiemap, num_bytes);
5208 if (IS_ENCRYPTED(inode)) {
5211 for (i = 0; i < fiemap->fm_mapped_extents; i++)
5212 fiemap->fm_extents[i].fe_flags |=
5213 FIEMAP_EXTENT_DATA_ENCRYPTED |
5214 FIEMAP_EXTENT_ENCODED;
5217 fieinfo->fi_flags = fiemap->fm_flags;
5218 fieinfo->fi_extents_mapped = fiemap->fm_mapped_extents;
5219 if (extent_count > 0 &&
5220 copy_to_user(fieinfo->fi_extents_start, &fiemap->fm_extents[0],
5221 fiemap->fm_mapped_extents *
5222 sizeof(struct fiemap_extent)) != 0)
5223 GOTO(out, rc = -EFAULT);
5225 OBD_FREE_LARGE(fiemap, num_bytes);
5229 int ll_inode_permission(struct inode *inode, int mask)
5232 struct ll_sb_info *sbi;
5233 struct root_squash_info *squash;
5234 struct cred *cred = NULL;
5235 const struct cred *old_cred = NULL;
5237 bool squash_id = false;
5238 ktime_t kstart = ktime_get();
5242 if (mask & MAY_NOT_BLOCK)
5246 * as root inode are NOT getting validated in lookup operation,
5247 * need to do it before permission check.
5250 if (is_root_inode(inode)) {
5251 rc = ll_inode_revalidate(inode->i_sb->s_root, IT_LOOKUP);
5256 CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), inode mode %x mask %o\n",
5257 PFID(ll_inode2fid(inode)), inode, inode->i_mode, mask);
5259 /* squash fsuid/fsgid if needed */
5260 sbi = ll_i2sbi(inode);
5261 squash = &sbi->ll_squash;
5262 if (unlikely(squash->rsi_uid != 0 &&
5263 uid_eq(current_fsuid(), GLOBAL_ROOT_UID) &&
5264 !(sbi->ll_flags & LL_SBI_NOROOTSQUASH))) {
5268 CDEBUG(D_OTHER, "squash creds (%d:%d)=>(%d:%d)\n",
5269 __kuid_val(current_fsuid()), __kgid_val(current_fsgid()),
5270 squash->rsi_uid, squash->rsi_gid);
5272 /* update current process's credentials
5273 * and FS capability */
5274 cred = prepare_creds();
5278 cred->fsuid = make_kuid(&init_user_ns, squash->rsi_uid);
5279 cred->fsgid = make_kgid(&init_user_ns, squash->rsi_gid);
5280 for (cap = 0; cap < sizeof(cfs_cap_t) * 8; cap++) {
5281 if (BIT(cap) & CFS_CAP_FS_MASK)
5282 cap_lower(cred->cap_effective, cap);
5284 old_cred = override_creds(cred);
5287 rc = generic_permission(inode, mask);
5288 /* restore current process's credentials and FS capability */
5290 revert_creds(old_cred);
5295 ll_stats_ops_tally(sbi, LPROC_LL_INODE_PERM,
5296 ktime_us_delta(ktime_get(), kstart));
5301 /* -o localflock - only provides locally consistent flock locks */
5302 static const struct file_operations ll_file_operations = {
5303 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5304 # ifdef HAVE_SYNC_READ_WRITE
5305 .read = new_sync_read,
5306 .write = new_sync_write,
5308 .read_iter = ll_file_read_iter,
5309 .write_iter = ll_file_write_iter,
5310 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5311 .read = ll_file_read,
5312 .aio_read = ll_file_aio_read,
5313 .write = ll_file_write,
5314 .aio_write = ll_file_aio_write,
5315 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5316 .unlocked_ioctl = ll_file_ioctl,
5317 .open = ll_file_open,
5318 .release = ll_file_release,
5319 .mmap = ll_file_mmap,
5320 .llseek = ll_file_seek,
5321 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5322 .splice_read = generic_file_splice_read,
5324 .splice_read = pcc_file_splice_read,
5328 .fallocate = ll_fallocate,
5331 static const struct file_operations ll_file_operations_flock = {
5332 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5333 # ifdef HAVE_SYNC_READ_WRITE
5334 .read = new_sync_read,
5335 .write = new_sync_write,
5336 # endif /* HAVE_SYNC_READ_WRITE */
5337 .read_iter = ll_file_read_iter,
5338 .write_iter = ll_file_write_iter,
5339 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5340 .read = ll_file_read,
5341 .aio_read = ll_file_aio_read,
5342 .write = ll_file_write,
5343 .aio_write = ll_file_aio_write,
5344 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5345 .unlocked_ioctl = ll_file_ioctl,
5346 .open = ll_file_open,
5347 .release = ll_file_release,
5348 .mmap = ll_file_mmap,
5349 .llseek = ll_file_seek,
5350 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5351 .splice_read = generic_file_splice_read,
5353 .splice_read = pcc_file_splice_read,
5357 .flock = ll_file_flock,
5358 .lock = ll_file_flock,
5359 .fallocate = ll_fallocate,
5362 /* These are for -o noflock - to return ENOSYS on flock calls */
5363 static const struct file_operations ll_file_operations_noflock = {
5364 #ifdef HAVE_FILE_OPERATIONS_READ_WRITE_ITER
5365 # ifdef HAVE_SYNC_READ_WRITE
5366 .read = new_sync_read,
5367 .write = new_sync_write,
5368 # endif /* HAVE_SYNC_READ_WRITE */
5369 .read_iter = ll_file_read_iter,
5370 .write_iter = ll_file_write_iter,
5371 #else /* !HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5372 .read = ll_file_read,
5373 .aio_read = ll_file_aio_read,
5374 .write = ll_file_write,
5375 .aio_write = ll_file_aio_write,
5376 #endif /* HAVE_FILE_OPERATIONS_READ_WRITE_ITER */
5377 .unlocked_ioctl = ll_file_ioctl,
5378 .open = ll_file_open,
5379 .release = ll_file_release,
5380 .mmap = ll_file_mmap,
5381 .llseek = ll_file_seek,
5382 #ifndef HAVE_DEFAULT_FILE_SPLICE_READ_EXPORT
5383 .splice_read = generic_file_splice_read,
5385 .splice_read = pcc_file_splice_read,
5389 .flock = ll_file_noflock,
5390 .lock = ll_file_noflock,
5391 .fallocate = ll_fallocate,
5394 const struct inode_operations ll_file_inode_operations = {
5395 .setattr = ll_setattr,
5396 .getattr = ll_getattr,
5397 .permission = ll_inode_permission,
5398 #ifdef HAVE_IOP_XATTR
5399 .setxattr = ll_setxattr,
5400 .getxattr = ll_getxattr,
5401 .removexattr = ll_removexattr,
5403 .listxattr = ll_listxattr,
5404 .fiemap = ll_fiemap,
5405 .get_acl = ll_get_acl,
5406 #ifdef HAVE_IOP_SET_ACL
5407 .set_acl = ll_set_acl,
5411 const struct file_operations *ll_select_file_operations(struct ll_sb_info *sbi)
5413 const struct file_operations *fops = &ll_file_operations_noflock;
5415 if (sbi->ll_flags & LL_SBI_FLOCK)
5416 fops = &ll_file_operations_flock;
5417 else if (sbi->ll_flags & LL_SBI_LOCALFLOCK)
5418 fops = &ll_file_operations;
5423 int ll_layout_conf(struct inode *inode, const struct cl_object_conf *conf)
5425 struct ll_inode_info *lli = ll_i2info(inode);
5426 struct cl_object *obj = lli->lli_clob;
5435 env = cl_env_get(&refcheck);
5437 RETURN(PTR_ERR(env));
5439 rc = cl_conf_set(env, lli->lli_clob, conf);
5443 if (conf->coc_opc == OBJECT_CONF_SET) {
5444 struct ldlm_lock *lock = conf->coc_lock;
5445 struct cl_layout cl = {
5449 LASSERT(lock != NULL);
5450 LASSERT(ldlm_has_layout(lock));
5452 /* it can only be allowed to match after layout is
5453 * applied to inode otherwise false layout would be
5454 * seen. Applying layout shoud happen before dropping
5455 * the intent lock. */
5456 ldlm_lock_allow_match(lock);
5458 rc = cl_object_layout_get(env, obj, &cl);
5463 DFID": layout version change: %u -> %u\n",
5464 PFID(&lli->lli_fid), ll_layout_version_get(lli),
5466 ll_layout_version_set(lli, cl.cl_layout_gen);
5470 cl_env_put(env, &refcheck);
5472 RETURN(rc < 0 ? rc : 0);
5475 /* Fetch layout from MDT with getxattr request, if it's not ready yet */
5476 static int ll_layout_fetch(struct inode *inode, struct ldlm_lock *lock)
5479 struct ll_sb_info *sbi = ll_i2sbi(inode);
5480 struct ptlrpc_request *req;
5487 CDEBUG(D_INODE, DFID" LVB_READY=%d l_lvb_data=%p l_lvb_len=%d\n",
5488 PFID(ll_inode2fid(inode)), ldlm_is_lvb_ready(lock),
5489 lock->l_lvb_data, lock->l_lvb_len);
5491 if (lock->l_lvb_data != NULL)
5494 /* if layout lock was granted right away, the layout is returned
5495 * within DLM_LVB of dlm reply; otherwise if the lock was ever
5496 * blocked and then granted via completion ast, we have to fetch
5497 * layout here. Please note that we can't use the LVB buffer in
5498 * completion AST because it doesn't have a large enough buffer */
5499 rc = ll_get_default_mdsize(sbi, &lmmsize);
5503 rc = md_getxattr(sbi->ll_md_exp, ll_inode2fid(inode), OBD_MD_FLXATTR,
5504 XATTR_NAME_LOV, lmmsize, &req);
5507 GOTO(out, rc = 0); /* empty layout */
5514 if (lmmsize == 0) /* empty layout */
5517 lmm = req_capsule_server_sized_get(&req->rq_pill, &RMF_EADATA, lmmsize);
5519 GOTO(out, rc = -EFAULT);
5521 OBD_ALLOC_LARGE(lvbdata, lmmsize);
5522 if (lvbdata == NULL)
5523 GOTO(out, rc = -ENOMEM);
5525 memcpy(lvbdata, lmm, lmmsize);
5526 lock_res_and_lock(lock);
5527 if (unlikely(lock->l_lvb_data == NULL)) {
5528 lock->l_lvb_type = LVB_T_LAYOUT;
5529 lock->l_lvb_data = lvbdata;
5530 lock->l_lvb_len = lmmsize;
5533 unlock_res_and_lock(lock);
5536 OBD_FREE_LARGE(lvbdata, lmmsize);
5541 ptlrpc_req_finished(req);
5546 * Apply the layout to the inode. Layout lock is held and will be released
5549 static int ll_layout_lock_set(struct lustre_handle *lockh, enum ldlm_mode mode,
5550 struct inode *inode)
5552 struct ll_inode_info *lli = ll_i2info(inode);
5553 struct ll_sb_info *sbi = ll_i2sbi(inode);
5554 struct ldlm_lock *lock;
5555 struct cl_object_conf conf;
5558 bool wait_layout = false;
5561 LASSERT(lustre_handle_is_used(lockh));
5563 lock = ldlm_handle2lock(lockh);
5564 LASSERT(lock != NULL);
5565 LASSERT(ldlm_has_layout(lock));
5567 LDLM_DEBUG(lock, "file "DFID"(%p) being reconfigured",
5568 PFID(&lli->lli_fid), inode);
5570 /* in case this is a caching lock and reinstate with new inode */
5571 md_set_lock_data(sbi->ll_md_exp, lockh, inode, NULL);
5573 lock_res_and_lock(lock);
5574 lvb_ready = ldlm_is_lvb_ready(lock);
5575 unlock_res_and_lock(lock);
5577 /* checking lvb_ready is racy but this is okay. The worst case is
5578 * that multi processes may configure the file on the same time. */
5582 rc = ll_layout_fetch(inode, lock);
5586 /* for layout lock, lmm is stored in lock's lvb.
5587 * lvb_data is immutable if the lock is held so it's safe to access it
5590 * set layout to file. Unlikely this will fail as old layout was
5591 * surely eliminated */
5592 memset(&conf, 0, sizeof conf);
5593 conf.coc_opc = OBJECT_CONF_SET;
5594 conf.coc_inode = inode;
5595 conf.coc_lock = lock;
5596 conf.u.coc_layout.lb_buf = lock->l_lvb_data;
5597 conf.u.coc_layout.lb_len = lock->l_lvb_len;
5598 rc = ll_layout_conf(inode, &conf);
5600 /* refresh layout failed, need to wait */
5601 wait_layout = rc == -EBUSY;
5604 LDLM_LOCK_PUT(lock);
5605 ldlm_lock_decref(lockh, mode);
5607 /* wait for IO to complete if it's still being used. */
5609 CDEBUG(D_INODE, "%s: "DFID"(%p) wait for layout reconf\n",
5610 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5612 memset(&conf, 0, sizeof conf);
5613 conf.coc_opc = OBJECT_CONF_WAIT;
5614 conf.coc_inode = inode;
5615 rc = ll_layout_conf(inode, &conf);
5619 CDEBUG(D_INODE, "%s file="DFID" waiting layout return: %d\n",
5620 sbi->ll_fsname, PFID(&lli->lli_fid), rc);
5626 * Issue layout intent RPC to MDS.
5627 * \param inode [in] file inode
5628 * \param intent [in] layout intent
5630 * \retval 0 on success
5631 * \retval < 0 error code
5633 static int ll_layout_intent(struct inode *inode, struct layout_intent *intent)
5635 struct ll_inode_info *lli = ll_i2info(inode);
5636 struct ll_sb_info *sbi = ll_i2sbi(inode);
5637 struct md_op_data *op_data;
5638 struct lookup_intent it;
5639 struct ptlrpc_request *req;
5643 op_data = ll_prep_md_op_data(NULL, inode, inode, NULL,
5644 0, 0, LUSTRE_OPC_ANY, NULL);
5645 if (IS_ERR(op_data))
5646 RETURN(PTR_ERR(op_data));
5648 op_data->op_data = intent;
5649 op_data->op_data_size = sizeof(*intent);
5651 memset(&it, 0, sizeof(it));
5652 it.it_op = IT_LAYOUT;
5653 if (intent->li_opc == LAYOUT_INTENT_WRITE ||
5654 intent->li_opc == LAYOUT_INTENT_TRUNC)
5655 it.it_flags = FMODE_WRITE;
5657 LDLM_DEBUG_NOLOCK("%s: requeue layout lock for file "DFID"(%p)",
5658 sbi->ll_fsname, PFID(&lli->lli_fid), inode);
5660 rc = md_intent_lock(sbi->ll_md_exp, op_data, &it, &req,
5661 &ll_md_blocking_ast, 0);
5662 if (it.it_request != NULL)
5663 ptlrpc_req_finished(it.it_request);
5664 it.it_request = NULL;
5666 ll_finish_md_op_data(op_data);
5668 /* set lock data in case this is a new lock */
5670 ll_set_lock_data(sbi->ll_md_exp, inode, &it, NULL);
5672 ll_intent_drop_lock(&it);
5678 * This function checks if there exists a LAYOUT lock on the client side,
5679 * or enqueues it if it doesn't have one in cache.
5681 * This function will not hold layout lock so it may be revoked any time after
5682 * this function returns. Any operations depend on layout should be redone
5685 * This function should be called before lov_io_init() to get an uptodate
5686 * layout version, the caller should save the version number and after IO
5687 * is finished, this function should be called again to verify that layout
5688 * is not changed during IO time.
5690 int ll_layout_refresh(struct inode *inode, __u32 *gen)
5692 struct ll_inode_info *lli = ll_i2info(inode);
5693 struct ll_sb_info *sbi = ll_i2sbi(inode);
5694 struct lustre_handle lockh;
5695 struct layout_intent intent = {
5696 .li_opc = LAYOUT_INTENT_ACCESS,
5698 enum ldlm_mode mode;
5702 *gen = ll_layout_version_get(lli);
5703 if (!(sbi->ll_flags & LL_SBI_LAYOUT_LOCK) || *gen != CL_LAYOUT_GEN_NONE)
5707 LASSERT(fid_is_sane(ll_inode2fid(inode)));
5708 LASSERT(S_ISREG(inode->i_mode));
5710 /* take layout lock mutex to enqueue layout lock exclusively. */
5711 mutex_lock(&lli->lli_layout_mutex);
5714 /* mostly layout lock is caching on the local side, so try to
5715 * match it before grabbing layout lock mutex. */
5716 mode = ll_take_md_lock(inode, MDS_INODELOCK_LAYOUT, &lockh, 0,
5717 LCK_CR | LCK_CW | LCK_PR |
5719 if (mode != 0) { /* hit cached lock */
5720 rc = ll_layout_lock_set(&lockh, mode, inode);
5726 rc = ll_layout_intent(inode, &intent);
5732 *gen = ll_layout_version_get(lli);
5733 mutex_unlock(&lli->lli_layout_mutex);
5739 * Issue layout intent RPC indicating where in a file an IO is about to write.
5741 * \param[in] inode file inode.
5742 * \param[in] ext write range with start offset of fille in bytes where
5743 * an IO is about to write, and exclusive end offset in
5746 * \retval 0 on success
5747 * \retval < 0 error code
5749 int ll_layout_write_intent(struct inode *inode, enum layout_intent_opc opc,
5750 struct lu_extent *ext)
5752 struct layout_intent intent = {
5754 .li_extent.e_start = ext->e_start,
5755 .li_extent.e_end = ext->e_end,
5760 rc = ll_layout_intent(inode, &intent);
5766 * This function send a restore request to the MDT
5768 int ll_layout_restore(struct inode *inode, loff_t offset, __u64 length)
5770 struct hsm_user_request *hur;
5774 len = sizeof(struct hsm_user_request) +
5775 sizeof(struct hsm_user_item);
5776 OBD_ALLOC(hur, len);
5780 hur->hur_request.hr_action = HUA_RESTORE;
5781 hur->hur_request.hr_archive_id = 0;
5782 hur->hur_request.hr_flags = 0;
5783 memcpy(&hur->hur_user_item[0].hui_fid, &ll_i2info(inode)->lli_fid,
5784 sizeof(hur->hur_user_item[0].hui_fid));
5785 hur->hur_user_item[0].hui_extent.offset = offset;
5786 hur->hur_user_item[0].hui_extent.length = length;
5787 hur->hur_request.hr_itemcount = 1;
5788 rc = obd_iocontrol(LL_IOC_HSM_REQUEST, ll_i2sbi(inode)->ll_md_exp,